From 2d5da837801bd2b661592a9bc2bcd0fcfefbf998 Mon Sep 17 00:00:00 2001 From: qiuxiang <394405259@qq.com> Date: Wed, 11 Nov 2026 20:21:00 +0800 Subject: [PATCH] fix: preserve reconciliation financial summary Restore the existing financial overview totals while keeping lazy trend loading. --- .../PeriodBalanceReconciliationService.php | 82 +------------------ ...PeriodBalanceReconciliationServiceTest.php | 14 ++++ 2 files changed, 15 insertions(+), 81 deletions(-) diff --git a/modules/EcommerceFinance/Services/PeriodBalanceReconciliationService.php b/modules/EcommerceFinance/Services/PeriodBalanceReconciliationService.php index ba8d5628..84ce8f38 100644 --- a/modules/EcommerceFinance/Services/PeriodBalanceReconciliationService.php +++ b/modules/EcommerceFinance/Services/PeriodBalanceReconciliationService.php @@ -37,15 +37,12 @@ class PeriodBalanceReconciliationService public function build(string $periodStart, ?string $periodEnd = null): array { [$start, $end, $month] = $this->period($periodStart, $periodEnd); + $financial = $this->overview->overview($start, $end); $financialStoreData = $this->overview->storeBalancesForPlatforms( array_column(StoreReceivingBalanceOverviewService::PLATFORMS, 'slug'), $start, $end, ); - $financial = $this->financialOverview( - $financialStoreData['rows'] ?? [], - $financialStoreData['errors'] ?? [], - ); $lastJushuitan = $this->jushuitanBalances->latestComparisonSnapshots( array_column(self::JUSHUITAN_SOURCES, 'source_scope'), $end, @@ -648,83 +645,6 @@ class PeriodBalanceReconciliationService return round((float) array_sum($amounts), 2); } - /** - * 从店铺期末余额聚合财务总览,避免同一账期重复查询各平台汇总。 - * - * @param list> $rows - * @param array $errors - * @return array{summary:array,platforms:list>} - */ - private function financialOverview(array $rows, array $errors): array - { - $platforms = []; - $totals = [ - 'opening_balance' => 0.0, - 'income' => 0.0, - 'expense' => 0.0, - ]; - $availableCount = 0; - - foreach ($this->platformDefinitions() as $definition) { - $platformRows = array_values(array_filter( - $rows, - static fn (array $row): bool => ($row['platform_slug'] ?? null) === $definition['slug'], - )); - $available = ! isset($errors[$definition['slug']]) - && $platformRows !== [] - && ! in_array(false, array_column($platformRows, 'available'), true); - if (! $available) { - $platforms[] = [ - 'slug' => $definition['slug'], - 'name' => $definition['name'], - 'available' => false, - 'opening_balance' => null, - 'income' => null, - 'expense' => null, - 'balance' => null, - 'error' => $errors[$definition['slug']] ?? '平台期初余额未就绪', - ]; - continue; - } - - $availableCount++; - $opening = round(array_sum(array_column($platformRows, 'opening_balance')), 2); - $income = round(array_sum(array_column($platformRows, 'income')), 2); - $expense = round(array_sum(array_column($platformRows, 'expense')), 2); - $totals['opening_balance'] += $opening; - $totals['income'] += $income; - $totals['expense'] += $expense; - $platforms[] = [ - 'slug' => $definition['slug'], - 'name' => $definition['name'], - 'available' => true, - 'opening_balance' => $opening, - 'income' => $income, - 'expense' => $expense, - 'balance' => round($opening + $income - $expense, 2), - 'error' => null, - ]; - } - - $complete = $availableCount === count($this->platformDefinitions()); - $opening = $complete ? round($totals['opening_balance'], 2) : null; - $income = $complete ? round($totals['income'], 2) : null; - $expense = $complete ? round($totals['expense'], 2) : null; - - return [ - 'summary' => [ - 'opening_balance' => $opening, - 'income' => $income, - 'expense' => $expense, - 'balance' => $complete ? round($opening + $income - $expense, 2) : null, - 'platform_count' => count($this->platformDefinitions()), - 'available_platform_count' => $availableCount, - 'complete' => $complete, - ], - 'platforms' => $platforms, - ]; - } - /** @return list */ private function platformDefinitions(): array { diff --git a/tests/Unit/EcommerceFinance/PeriodBalanceReconciliationServiceTest.php b/tests/Unit/EcommerceFinance/PeriodBalanceReconciliationServiceTest.php index d4b246f5..c69746fc 100644 --- a/tests/Unit/EcommerceFinance/PeriodBalanceReconciliationServiceTest.php +++ b/tests/Unit/EcommerceFinance/PeriodBalanceReconciliationServiceTest.php @@ -105,6 +105,20 @@ class PeriodBalanceReconciliationServiceTest extends TestCase $this->financial('taobao-wechat', '淘系微信账户', '微信店铺', 2000), ]; $overview = Mockery::mock(StoreReceivingBalanceOverviewService::class); + $overview->shouldReceive('overview') + ->once() + ->andReturn([ + 'summary' => [ + 'opening_balance' => 20000, + 'income' => 5000, + 'expense' => 4000, + 'balance' => 21000, + 'platform_count' => 8, + 'available_platform_count' => 8, + 'complete' => true, + ], + 'platforms' => [], + ]); $overview->shouldReceive('storeBalancesForPlatforms') ->andReturn(['rows' => $financialRows, 'errors' => []]);