From 832fe6399ad2241fa180d7cfca586d0f0b1eb6d2 Mon Sep 17 00:00:00 2001 From: yueyang <2495544247@qq.com> Date: Thu, 26 Nov 2026 17:40:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=89=E8=A3=85=E8=B4=B9=E7=94=A8?= =?UTF-8?q?=E8=B6=8B=E5=8A=BF=E6=94=B9=E6=8C=89=E5=AE=89=E8=A3=85=E5=85=AC?= =?UTF-8?q?=E5=8F=B8=E5=8C=BA=E5=88=86=20(#1479)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../InstallationDetailMirrorRepository.php | 13 +- .../View/InstallationFeeUsagePageData.php | 212 +++----- .../installation-fee-usage.blade.php | 459 +++++++++--------- tests/Feature/Business/ManagementCrudTest.php | 24 +- ...InstallationDetailMirrorRepositoryTest.php | 30 +- 5 files changed, 300 insertions(+), 438 deletions(-) diff --git a/modules/EcommerceFinance/Repositories/InstallationDetailMirrorRepository.php b/modules/EcommerceFinance/Repositories/InstallationDetailMirrorRepository.php index eb92a4f9..1dbc3a15 100644 --- a/modules/EcommerceFinance/Repositories/InstallationDetailMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/InstallationDetailMirrorRepository.php @@ -225,15 +225,12 @@ class InstallationDetailMirrorRepository } /** - * 按发货月份、平台和安装公司汇总安装单量,并返回已审核结算金额。 - * - * 平台与安装公司用于分析供应商切换;审核状态用于区分已确认金额和待审核单量。 + * 按发货月份和安装公司汇总安装单量,并返回已审核结算金额。 * * @return array{ * ready:bool, * rows:list * } */ - public function platformFeeUsageTrend(string $fromMonth, string $toMonth): array + public function installationCompanyFeeUsageTrend(string $fromMonth, string $toMonth): array { $dataset = $this->datasets->get(self::DATASET); $items = $this->datasets->get(self::ITEM_DATASET); @@ -285,25 +282,21 @@ class InstallationDetailMirrorRepository $periodSql = $connection->getDriverName() === 'sqlite' ? "strftime('%Y-%m', install_main.ShipTIme)" : "DATE_FORMAT(install_main.ShipTIme, '%Y-%m')"; - $platformSql = $this->platformSql('install_main'); $companySql = "COALESCE(NULLIF(TRIM(install_main.installCompanyName), ''), '未填写安装公司')"; $rows = (clone $main) ->leftJoinSub($itemReview, 'item_review', static fn ($join) => $join->on('item_review.guid', '=', 'install_main.SourceGuid')) ->selectRaw($periodSql . ' AS period') - ->selectRaw($platformSql . ' AS platform') ->selectRaw($companySql . ' AS installation_company') ->selectRaw('COUNT(*) AS orders') ->selectRaw('SUM(CASE WHEN item_review.reviewed = 1 THEN 1 ELSE 0 END) AS reviewed_orders') ->selectRaw('ROUND(COALESCE(SUM(item_review.settled_amount), 0), 2) AS settled_amount') - ->groupByRaw($periodSql . ', ' . $platformSql . ', ' . $companySql) + ->groupByRaw($periodSql . ', ' . $companySql) ->orderByRaw($periodSql) - ->orderByRaw($platformSql) ->orderByRaw($companySql) ->get() ->map(static fn (object $row): array => [ 'period' => trim((string) ($row->period ?? '')), - 'platform' => trim((string) ($row->platform ?? '其他')), 'installation_company' => trim((string) ($row->installation_company ?? '未填写安装公司')), 'orders' => (int) ($row->orders ?? 0), 'reviewed_orders' => (int) ($row->reviewed_orders ?? 0), diff --git a/modules/EcommerceFinance/View/InstallationFeeUsagePageData.php b/modules/EcommerceFinance/View/InstallationFeeUsagePageData.php index 24eb560f..66c125a8 100644 --- a/modules/EcommerceFinance/View/InstallationFeeUsagePageData.php +++ b/modules/EcommerceFinance/View/InstallationFeeUsagePageData.php @@ -6,7 +6,7 @@ use Illuminate\Support\Carbon; use Modules\EcommerceFinance\Repositories\InstallationDetailMirrorRepository; use Throwable; -/** 安装费汇总:按平台分析安装单量、已审核金额和供应商切换影响。 */ +/** 安装费汇总:按安装公司分析单量、金额、单均和供应商切换影响。 */ class InstallationFeeUsagePageData { public const RANGE_OPTIONS = [ @@ -15,17 +15,6 @@ class InstallationFeeUsagePageData 'ytd' => '本年至今', ]; - private const PLATFORM_ORDER = [ - '淘系' => 10, - '京东' => 20, - '拼多多' => 30, - '抖音' => 40, - '唯品会' => 50, - '得物' => 60, - '京喜' => 70, - '其他' => 80, - ]; - public function __construct( private readonly InstallationDetailMirrorRepository $repository, ) { @@ -42,7 +31,7 @@ class InstallationFeeUsagePageData $rangeLabel = $fromMonth . ' 至 ' . $toMonth; try { - $result = $this->repository->platformFeeUsageTrend($fromMonth, $toMonth); + $result = $this->repository->installationCompanyFeeUsageTrend($fromMonth, $toMonth); } catch (Throwable $exception) { report($exception); @@ -53,7 +42,7 @@ class InstallationFeeUsagePageData $toMonth, $this->emptyPeriods($fromMonth, $toMonth), [], - '安装费平台分析暂时无法读取,请稍后重试。', + '安装费安装公司分析暂时无法读取,请稍后重试。', ); } @@ -97,7 +86,6 @@ class InstallationFeeUsagePageData /** * @param listvendorKey($row['installation_company']); + $vendorKey = $this->vendorKey($row['installation_company']); $orders = (int) $row['orders']; $reviewedOrders = (int) $row['reviewed_orders']; $settledAmount = (float) $row['settled_amount']; @@ -128,30 +115,9 @@ class InstallationFeeUsagePageData $periods[$index]['total_orders'] += $orders; $periods[$index]['reviewed_orders'] += $reviewedOrders; $periods[$index]['settled_amount'] += $settledAmount; - $periods[$index]['vendors'][$vendor]['orders'] += $orders; - $periods[$index]['vendors'][$vendor]['reviewed_orders'] += $reviewedOrders; - $periods[$index]['vendors'][$vendor]['settled_amount'] += $settledAmount; - - if (! isset($periods[$index]['platforms'][$platform])) { - $periods[$index]['platforms'][$platform] = [ - 'platform' => $platform, - 'orders' => 0, - 'reviewed_orders' => 0, - 'settled_amount' => 0.0, - 'review_rate' => 0.0, - 'unit_cost' => 0.0, - 'vendors' => $this->emptyVendors(), - ]; - } - - $platformData = &$periods[$index]['platforms'][$platform]; - $platformData['orders'] += $orders; - $platformData['reviewed_orders'] += $reviewedOrders; - $platformData['settled_amount'] += $settledAmount; - $platformData['vendors'][$vendor]['orders'] += $orders; - $platformData['vendors'][$vendor]['reviewed_orders'] += $reviewedOrders; - $platformData['vendors'][$vendor]['settled_amount'] += $settledAmount; - unset($platformData); + $periods[$index]['vendors'][$vendorKey]['orders'] += $orders; + $periods[$index]['vendors'][$vendorKey]['reviewed_orders'] += $reviewedOrders; + $periods[$index]['vendors'][$vendorKey]['settled_amount'] += $settledAmount; } foreach ($periods as &$period) { @@ -167,26 +133,6 @@ class InstallationFeeUsagePageData } unset($vendor); - foreach ($period['platforms'] as &$platform) { - $platform['settled_amount'] = round((float) $platform['settled_amount'], 2); - $platform['review_rate'] = $this->ratio($platform['reviewed_orders'], $platform['orders']); - $platform['unit_cost'] = $this->unitCost($platform['settled_amount'], $platform['reviewed_orders']); - - foreach ($platform['vendors'] as &$vendor) { - $vendor['settled_amount'] = round((float) $vendor['settled_amount'], 2); - $vendor['review_rate'] = $this->ratio($vendor['reviewed_orders'], $vendor['orders']); - $vendor['unit_cost'] = $this->unitCost($vendor['settled_amount'], $vendor['reviewed_orders']); - $vendor['order_share'] = $this->ratio($vendor['orders'], $platform['orders']); - } - unset($vendor); - } - unset($platform); - - uasort($period['platforms'], static function (array $left, array $right): int { - return ($right['orders'] <=> $left['orders']) - ?: ((self::PLATFORM_ORDER[$left['platform']] ?? 99) <=> (self::PLATFORM_ORDER[$right['platform']] ?? 99)); - }); - $period['platforms'] = array_values($period['platforms']); } unset($period); @@ -208,7 +154,6 @@ class InstallationFeeUsagePageData 'settled_amount' => 0.0, 'unit_cost' => 0.0, 'vendors' => $this->emptyVendors(), - 'platforms' => [], ]; $cursor->addMonthNoOverflow(); } @@ -290,62 +235,42 @@ class InstallationFeeUsagePageData $benchmarkIndexes, ); sort($benchmarkPeriods, SORT_STRING); - $rates = $this->benchmarkRates($periods, $benchmarkIndexes); - $previousPlatforms = collect($previous['platforms'] ?? [])->keyBy('platform'); + $benchmarkRates = $this->benchmarkRates($periods, $benchmarkIndexes); + $previousVendors = collect($previous['vendors'] ?? [])->keyBy('key'); - $platformRows = []; - $estimatedOrders = 0; + $wanOrders = (int) $current['vendors']['wan']['orders']; + $lubanOrders = (int) $current['vendors']['luban']['orders']; + $switchOrders = $wanOrders + $lubanOrders; + $wanRate = $benchmarkRates['wan'] ?? null; + $lubanRate = $benchmarkRates['luban'] ?? null; + + $vendorRows = []; $estimatedMixedAmount = 0.0; $estimatedAllWanAmount = 0.0; - foreach ($current['platforms'] as $platform) { - $wanOrders = (int) ($platform['vendors']['wan']['orders'] ?? 0); - $lubanOrders = (int) ($platform['vendors']['luban']['orders'] ?? 0); - - $wanRate = $this->benchmarkRate($rates, $platform['platform'], 'wan'); - $lubanRate = $this->benchmarkRate($rates, $platform['platform'], 'luban'); - $mixedAmount = null; - $allWanAmount = null; - $switchImpact = null; - if ($wanOrders + $lubanOrders > 0 && $wanRate !== null && $lubanRate !== null) { - $mixedAmount = $wanOrders * $wanRate + $lubanOrders * $lubanRate; - $allWanAmount = ($wanOrders + $lubanOrders) * $wanRate; - $switchImpact = $mixedAmount - $allWanAmount; - $estimatedOrders += $wanOrders + $lubanOrders; - $estimatedMixedAmount += $mixedAmount; - $estimatedAllWanAmount += $allWanAmount; + foreach ($current['vendors'] as $vendor) { + $rate = $benchmarkRates[$vendor['key']] ?? null; + $estimatedAmount = $rate === null ? null : $vendor['orders'] * $rate; + if ($vendor['key'] !== 'other' && $estimatedAmount !== null) { + $estimatedMixedAmount += $estimatedAmount; + } + if ($vendor['key'] === 'wan' && $rate !== null) { + $estimatedAllWanAmount = $switchOrders * $rate; } - $prior = $previousPlatforms->get($platform['platform']); - $platformRows[] = [ - 'platform' => $platform['platform'], - 'orders' => (int) $platform['orders'], - 'wan_orders' => $wanOrders, - 'luban_orders' => $lubanOrders, - 'other_orders' => (int) ($platform['vendors']['other']['orders'] ?? 0), - 'luban_share' => $this->ratio( - $lubanOrders, - $wanOrders + $lubanOrders + (int) ($platform['vendors']['other']['orders'] ?? 0), - ), - 'reviewed_orders' => (int) $platform['reviewed_orders'], - 'review_rate' => (float) $platform['review_rate'], - 'settled_amount' => (float) $platform['settled_amount'], - 'confirmed_unit_cost' => (float) $platform['unit_cost'], - 'wan_benchmark_rate' => $wanRate, - 'luban_benchmark_rate' => $lubanRate, - 'estimated_amount' => $mixedAmount === null ? null : round($mixedAmount, 2), - 'all_wan_amount' => $allWanAmount === null ? null : round($allWanAmount, 2), - 'switch_impact' => $switchImpact === null ? null : round($switchImpact, 2), + $prior = $previousVendors->get($vendor['key']); + $vendorRows[] = [ + ...$vendor, + 'benchmark_rate' => $rate, + 'estimated_amount' => $estimatedAmount === null ? null : round($estimatedAmount, 2), 'previous_orders' => (int) ($prior['orders'] ?? 0), - 'previous_settled_amount' => round((float) ($prior['settled_amount'] ?? 0), 2), - 'previous_unit_cost' => round((float) ($prior['unit_cost'] ?? 0), 2), + 'previous_reviewed_orders' => (int) ($prior['reviewed_orders'] ?? 0), + 'previous_review_rate' => (float) ($prior['review_rate'] ?? 0), + 'previous_settled_amount' => (float) ($prior['settled_amount'] ?? 0), + 'previous_unit_cost' => (float) ($prior['unit_cost'] ?? 0), ]; } - usort($platformRows, static fn (array $left, array $right): int => - ($right['orders'] <=> $left['orders']) - ?: ((self::PLATFORM_ORDER[$left['platform']] ?? 99) <=> (self::PLATFORM_ORDER[$right['platform']] ?? 99))); - - $switchImpact = $estimatedOrders > 0 + $switchImpact = $switchOrders > 0 && $wanRate !== null && $lubanRate !== null ? round($estimatedMixedAmount - $estimatedAllWanAmount, 2) : null; @@ -366,16 +291,17 @@ class InstallationFeeUsagePageData 'benchmark_label' => $benchmarkPeriods === [] ? null : $benchmarkPeriods[0] . ' 至 ' . $benchmarkPeriods[count($benchmarkPeriods) - 1], - 'estimated_orders' => $estimatedOrders, + 'benchmark_rates' => $benchmarkRates, + 'estimated_orders' => $switchOrders, 'estimated_mixed_amount' => round($estimatedMixedAmount, 2), 'estimated_all_wan_amount' => round($estimatedAllWanAmount, 2), - 'estimated_mixed_unit_cost' => $this->unitCost($estimatedMixedAmount, $estimatedOrders), - 'estimated_all_wan_unit_cost' => $this->unitCost($estimatedAllWanAmount, $estimatedOrders), + 'estimated_mixed_unit_cost' => $this->unitCost($estimatedMixedAmount, $switchOrders), + 'estimated_all_wan_unit_cost' => $this->unitCost($estimatedAllWanAmount, $switchOrders), 'switch_impact' => $switchImpact, 'switch_impact_rate' => $estimatedAllWanAmount > 0.004 && $switchImpact !== null ? round(($switchImpact / $estimatedAllWanAmount) * 100, 1) : null, - 'platforms' => $platformRows, + 'vendors' => $vendorRows, ]; } @@ -396,6 +322,7 @@ class InstallationFeeUsagePageData 'previous_confirmed_unit_cost' => 0.0, 'benchmark_periods' => [], 'benchmark_label' => null, + 'benchmark_rates' => [], 'estimated_orders' => 0, 'estimated_mixed_amount' => 0.0, 'estimated_all_wan_amount' => 0.0, @@ -403,7 +330,7 @@ class InstallationFeeUsagePageData 'estimated_all_wan_unit_cost' => 0.0, 'switch_impact' => null, 'switch_impact_rate' => null, - 'platforms' => [], + 'vendors' => [], ]; } @@ -426,52 +353,27 @@ class InstallationFeeUsagePageData /** @param list $periodIndexes */ private function benchmarkRates(array $periods, array $periodIndexes): array { - $platforms = []; + $rates = [ + 'wan' => ['reviewed_orders' => 0, 'settled_amount' => 0.0], + 'luban' => ['reviewed_orders' => 0, 'settled_amount' => 0.0], + ]; foreach ($periodIndexes as $periodIndex) { - foreach ($periods[$periodIndex]['platforms'] as $platform) { - foreach (['wan', 'luban'] as $vendorKey) { - $vendor = $platform['vendors'][$vendorKey]; - $platforms[$platform['platform']][$vendorKey] ??= [ - 'reviewed_orders' => 0, - 'settled_amount' => 0.0, - ]; - $platforms[$platform['platform']][$vendorKey]['reviewed_orders'] += (int) $vendor['reviewed_orders']; - $platforms[$platform['platform']][$vendorKey]['settled_amount'] += (float) $vendor['settled_amount']; - } - } - } - - $companies = []; - foreach ($platforms as &$vendors) { - foreach ($vendors as $vendorKey => &$rate) { - $rate['rate'] = $this->unitCost($rate['settled_amount'], $rate['reviewed_orders']); - $companies[$vendorKey]['reviewed_orders'] = ($companies[$vendorKey]['reviewed_orders'] ?? 0) - + $rate['reviewed_orders']; - $companies[$vendorKey]['settled_amount'] = ($companies[$vendorKey]['settled_amount'] ?? 0.0) - + $rate['settled_amount']; + foreach ($rates as $vendorKey => &$rate) { + $vendor = $periods[$periodIndex]['vendors'][$vendorKey]; + $rate['reviewed_orders'] += (int) $vendor['reviewed_orders']; + $rate['settled_amount'] += (float) $vendor['settled_amount']; } unset($rate); } - unset($vendors); - foreach ($companies as &$company) { - $company['rate'] = $this->unitCost($company['settled_amount'], $company['reviewed_orders']); - } - unset($company); - - return ['platforms' => $platforms, 'companies' => $companies]; - } - - private function benchmarkRate(array $rates, string $platform, string $vendorKey): ?float - { - $platformRate = $rates['platforms'][$platform][$vendorKey] ?? null; - if ($platformRate !== null && $platformRate['reviewed_orders'] >= 10) { - return $platformRate['rate'] > 0 ? (float) $platformRate['rate'] : null; - } - - $companyRate = $rates['companies'][$vendorKey]['rate'] ?? null; - - return $companyRate !== null && $companyRate > 0 ? (float) $companyRate : null; + return [ + 'wan' => $rates['wan']['reviewed_orders'] > 0 + ? $this->unitCost($rates['wan']['settled_amount'], $rates['wan']['reviewed_orders']) + : null, + 'luban' => $rates['luban']['reviewed_orders'] > 0 + ? $this->unitCost($rates['luban']['settled_amount'], $rates['luban']['reviewed_orders']) + : null, + ]; } private function vendorKey(string $company): string diff --git a/resources/views/business/pages/financial-operations/installation-fee-usage.blade.php b/resources/views/business/pages/financial-operations/installation-fee-usage.blade.php index 0bad8bb4..54fdf8dc 100644 --- a/resources/views/business/pages/financial-operations/installation-fee-usage.blade.php +++ b/resources/views/business/pages/financial-operations/installation-fee-usage.blade.php @@ -7,7 +7,7 @@ $error = $installationFeeUsageError ?? null; $currentPeriod = $summary['latest_period'] ?? null; $previousPeriod = $summary['previous_period'] ?? null; - $platformRows = $summary['platforms'] ?? []; + $vendorRows = $summary['vendors'] ?? []; $moneyWan = static fn (mixed $value): string => number_format((float) $value / 10000, 2); $money = static fn (mixed $value): string => number_format((float) $value, 2); $percent = static fn (mixed $value): string => number_format((float) $value, 1); @@ -28,105 +28,97 @@ }; $switchImpact = $summary['switch_impact'] ?? null; $switchImpactDisplay = $impact($switchImpact); - $currentPlatformOrders = (int) collect($platformRows)->sum('orders'); - $currentWanOrders = (int) collect($platformRows)->sum('wan_orders'); - $currentLubanOrders = (int) collect($platformRows)->sum('luban_orders'); - $currentSettledAmount = (float) collect($platformRows)->sum('settled_amount'); - $currentEstimatedAmount = (float) collect($platformRows)->sum(fn (array $row): float => (float) ($row['estimated_amount'] ?? 0)); - $currentAllWanAmount = (float) collect($platformRows)->sum(fn (array $row): float => (float) ($row['all_wan_amount'] ?? 0)); - $previousOrders = (int) collect($platformRows)->sum('previous_orders'); - $previousSettledAmount = (float) collect($platformRows)->sum('previous_settled_amount'); $hasBenchmark = ! empty($summary['benchmark_periods']); @endphp -
+
-
-
-