fix: preserve reconciliation financial summary

Restore the existing financial overview totals while keeping lazy trend loading.
This commit is contained in:
qiuxiang 2026-11-11 20:21:00 +08:00
parent 45b88d1528
commit 2d5da83780
2 changed files with 15 additions and 81 deletions

View File

@ -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<array<string,mixed>> $rows
* @param array<string,string> $errors
* @return array{summary:array<string,mixed>,platforms:list<array<string,mixed>>}
*/
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<array{slug:string,name:string}> */
private function platformDefinitions(): array
{

View File

@ -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' => []]);