From ac31ca56ea3b604dfb50b7970d0229ccb91298b3 Mon Sep 17 00:00:00 2001 From: shanshi <3316401569@qq.com> Date: Wed, 25 Nov 2026 15:40:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E8=B4=A2=E5=8A=A1=E5=8F=B0=E8=B4=A6):=20?= =?UTF-8?q?=E8=A1=A5=E9=BD=90=E9=9D=9E=E7=9B=B4=E8=BF=9E=E5=8A=A8=E8=B4=A6?= =?UTF-8?q?=E4=BD=99=E9=A2=9D=E5=B1=95=E7=A4=BA=20(#1415)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...anceLedgerBankBalanceCurrentCalculator.php | 105 ++++++++++++++++ .../View/FinanceLedgerPageData.php | 49 ++++++++ .../pages/financial-ledger/ledger.blade.php | 4 +- .../FinanceLedgerTransactionBalanceTest.php | 116 ++++++++++++++++++ 4 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/Business/FinanceLedgerTransactionBalanceTest.php diff --git a/modules/EcommerceFinance/Services/FinanceLedgerBankBalanceCurrentCalculator.php b/modules/EcommerceFinance/Services/FinanceLedgerBankBalanceCurrentCalculator.php index 85f38c6b..f500cd6c 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerBankBalanceCurrentCalculator.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerBankBalanceCurrentCalculator.php @@ -160,6 +160,111 @@ final class FinanceLedgerBankBalanceCurrentCalculator return $movements; } + /** + * 按期初和当日逐笔动账计算非银企直连账户的交易后余额。 + * 以每日流水中的期初余额为起点,同一账户同一天按交易 ID 顺序滚动。 + * + * @param iterable $rows + * @param array $excludedAccountShorts + * @return array + */ + public function runningBalancesForTransactions( + iterable $rows, + array $excludedAccountShorts = [], + ): array { + $targets = []; + $accountShorts = []; + $dates = []; + foreach ($rows as $row) { + $id = (int) ($row->id ?? 0); + $accountShort = trim((string) ( + $row->effective_account_short + ?? $row->account_short + ?? '' + )); + $date = substr((string) ($row->txn_date ?? ''), 0, 10); + if ( + $id <= 0 + || $accountShort === '' + || $date === '' + || isset($excludedAccountShorts[$accountShort]) + ) { + continue; + } + $targets[$id] = [ + 'account_short' => $accountShort, + 'date' => $date, + ]; + $accountShorts[$accountShort] = true; + $dates[$date] = true; + } + $accountShorts = array_keys($accountShorts); + $dates = array_keys($dates); + if ($targets === [] || $accountShorts === [] || $dates === []) { + return []; + } + + $openingBalances = $this->connection->table('fin_daily_flows') + ->whereIn('account_short', $accountShorts) + ->whereIn('flow_date', $dates) + ->get(['account_short', 'flow_date', 'opening_balance']) + ->mapWithKeys(static fn (object $row): array => [ + substr((string) $row->flow_date, 0, 10) . "\0" . (string) $row->account_short + => round((float) $row->opening_balance, 2), + ]); + if ($openingBalances->isEmpty()) { + return []; + } + + $mappings = $this->connection->table('fin_account_map') + ->whereIn('account_short', $accountShorts) + ->get(['source', 'source_value', 'account_short']); + $mappingByKey = $mappings->keyBy( + static fn (object $row): string => (string) $row->source . "\0" . (string) $row->source_value, + ); + $transactions = $this->connection->table('fin_transactions') + ->whereNull('deleted_at') + ->whereIn('txn_date', $dates) + ->orderBy('txn_date') + ->orderBy('id') + ->get(['id', 'txn_date', 'account_short', 'source', 'source_value', 'income', 'expense']); + + $running = []; + $result = []; + foreach ($transactions as $transaction) { + $mappingKey = (string) $transaction->source . "\0" . (string) $transaction->source_value; + $accountShort = (string) ( + $mappingByKey->get($mappingKey)?->account_short + ?? $transaction->account_short + ?? '' + ); + $date = substr((string) $transaction->txn_date, 0, 10); + $balanceKey = $date . "\0" . $accountShort; + if (! $openingBalances->has($balanceKey)) { + continue; + } + $running[$balanceKey] ??= (float) $openingBalances->get($balanceKey); + $running[$balanceKey] = round( + $running[$balanceKey] + + (float) $transaction->income + - (float) $transaction->expense, + 2, + ); + + $id = (int) $transaction->id; + $target = $targets[$id] ?? null; + if ( + $target !== null + && $target['account_short'] === $accountShort + && $target['date'] === $date + ) { + $result[$id] = $running[$balanceKey]; + } + } + + return $result; + } + /** @return list */ private function normalizeAccountShorts(array $accountShorts): array { diff --git a/modules/EcommerceFinance/View/FinanceLedgerPageData.php b/modules/EcommerceFinance/View/FinanceLedgerPageData.php index 9f0aeea1..400ff119 100644 --- a/modules/EcommerceFinance/View/FinanceLedgerPageData.php +++ b/modules/EcommerceFinance/View/FinanceLedgerPageData.php @@ -8,6 +8,7 @@ use App\Utilities\FinanceLedgerAccountStatus; use App\Utilities\FinanceLedgerAccountShouldKeep; use App\Utilities\FinanceLedgerAccountType; use App\Utilities\FinanceLedgerAccess; +use App\Utilities\FinanceLedgerBankDirectAccounts; use App\Utilities\FinanceLedgerFlowSummaryDataSource; use App\Utilities\FinanceLedgerIndependentAccounts; use Carbon\CarbonImmutable; @@ -1918,6 +1919,7 @@ class FinanceLedgerPageData if ($customFields !== []) { $this->attachTransactionCustomValues($rows->getCollection(), $customFields); } + $this->applyTransactionDisplayBalances($rows->getCollection()); return [ 'rows' => $rows, @@ -2015,6 +2017,53 @@ class FinanceLedgerPageData ]; } + private function applyTransactionDisplayBalances($rows): void + { + if ($rows->isEmpty()) { + return; + } + + $connection = $this->db(); + $directAccounts = FinanceLedgerBankDirectAccounts::map($connection); + $eligibleRows = $rows->filter(static function (object $row) use ($directAccounts): bool { + $accountShort = trim((string) ($row->effective_account_short ?? '')); + + return $accountShort !== '' + && $accountShort !== '未匹配' + && ! isset($directAccounts[$accountShort]); + }); + $balances = $this->bankBalanceCurrentCalculator->runningBalancesForTransactions( + $eligibleRows, + $directAccounts, + ); + + $rows->each(static function (object $row) use ($directAccounts, $balances): void { + $accountShort = trim((string) ($row->effective_account_short ?? '')); + $row->raw_balance = $row->balance; + + if (isset($directAccounts[$accountShort])) { + $row->balance = null; + $row->balance_source = 'direct'; + return; + } + if ($accountShort === '' || $accountShort === '未匹配') { + $row->balance = null; + $row->balance_source = 'unmatched'; + return; + } + + $id = (int) $row->id; + if (array_key_exists($id, $balances)) { + $row->balance = round((float) $balances[$id], 2); + $row->balance_source = 'calculated'; + return; + } + + $row->balance = null; + $row->balance_source = 'missing_opening'; + }); + } + /** * @return list */ diff --git a/resources/views/business/pages/financial-ledger/ledger.blade.php b/resources/views/business/pages/financial-ledger/ledger.blade.php index 9b6a2c4d..565f1949 100644 --- a/resources/views/business/pages/financial-ledger/ledger.blade.php +++ b/resources/views/business/pages/financial-ledger/ledger.blade.php @@ -2357,7 +2357,7 @@
refresh重置
上月期末余额(万元){{ ($ledgerTransactionSummary['previous_closing'] ?? null) === null ? '—' : $moneyWan($ledgerTransactionSummary['previous_closing']) }}
当月收入(万元){{ $moneyWan($ledgerTransactionSummary['income'] ?? 0) }}
当月支出(万元){{ $moneyWan($ledgerTransactionSummary['expense'] ?? 0) }}
当前余额(万元){{ ($ledgerTransactionSummary['current_balance'] ?? null) === null ? '—' : $moneyWan($ledgerTransactionSummary['current_balance']) }}
-
{{ $transactionViewIsIndependent ? '本页导入和新增的每条明细均可编辑、删除;可像 Excel 一样在任意行上下插入、在自定义列左右插入。自定义列及列数据只属于当前独立账单,不进入其他页面。拖动表头右侧边界可调整列宽,双击边界恢复默认。' : '账户简称按“映射表 → 来源原始值 → 未匹配”解析;手工录入使用账户总表简称,并自动计入每日资金流水,不回写上游。拖动表头右侧边界可调整列宽,双击边界恢复默认。' }}
+
{{ $transactionViewIsIndependent ? '本页导入和新增的每条明细均可编辑、删除;可像 Excel 一样在任意行上下插入、在自定义列左右插入。自定义列及列数据只属于当前独立账单,不进入其他页面。拖动表头右侧边界可调整列宽,双击边界恢复默认。' : '账户简称按“映射表 → 来源原始值 → 未匹配”解析;手工录入使用账户总表简称,并自动计入每日资金流水,不回写上游。非银企直连账户的余额按期初余额和当日动账滚动计算;银企直连账户显示 —。拖动表头右侧边界可调整列宽,双击边界恢复默认。' }}
@foreach ($ledgerTableHeaders['transactions'] as $header) @@ -2488,7 +2488,7 @@ - + diff --git a/tests/Feature/Business/FinanceLedgerTransactionBalanceTest.php b/tests/Feature/Business/FinanceLedgerTransactionBalanceTest.php new file mode 100644 index 00000000..5cbaff3d --- /dev/null +++ b/tests/Feature/Business/FinanceLedgerTransactionBalanceTest.php @@ -0,0 +1,116 @@ +table('fin_accounts')->insert([ + [ + 'company_name' => '余额测试公司', + 'account_short' => '测试非直连华夏', + 'account_type' => '公户', + 'bank_name' => '华夏银行测试支行', + 'current_balance' => 120, + ], + [ + 'company_name' => '余额测试公司', + 'account_short' => '测试直连招行', + 'account_type' => '公户', + 'bank_name' => '招商银行测试支行', + 'current_balance' => 250, + ], + ]); + $connection->table('fin_flow_summary_account_sources')->insert([ + [ + 'account_short' => '测试非直连华夏', + 'data_source' => '手动', + ], + [ + 'account_short' => '测试直连招行', + 'data_source' => '银企直连', + ], + ]); + $connection->table('fin_daily_flows')->insert([ + [ + 'flow_date' => '2026-10-18', + 'company_name' => '余额测试公司', + 'account_short' => '测试非直连华夏', + 'opening_balance' => 100, + 'inflow' => 30, + 'outflow' => 10, + 'closing_balance' => 120, + ], + [ + 'flow_date' => '2026-10-18', + 'company_name' => '余额测试公司', + 'account_short' => '测试直连招行', + 'opening_balance' => 200, + 'inflow' => 50, + 'outflow' => 0, + 'closing_balance' => 250, + ], + ]); + $connection->table('fin_account_map')->insert([ + 'source' => 'payable', + 'source_value' => 'supplier-balance-test', + 'account_short' => '测试非直连华夏', + ]); + $connection->table('fin_transactions')->insert([ + [ + 'txn_date' => '2026-10-18', + 'account_short' => '测试供应商原始名称', + 'detail' => '非直连收入', + 'income' => 30, + 'expense' => 0, + 'approval_no' => 'TEST-NONDIRECT-INCOME', + 'source' => 'payable', + 'source_value' => 'supplier-balance-test', + 'row_key' => hash('sha256', 'test-nondirect-income'), + ], + [ + 'txn_date' => '2026-10-18', + 'account_short' => '测试非直连华夏', + 'detail' => '非直连支出', + 'income' => 0, + 'expense' => 10, + 'approval_no' => 'TEST-NONDIRECT-EXPENSE', + 'source' => 'manual', + 'source_value' => null, + 'row_key' => hash('sha256', 'test-nondirect-expense'), + ], + [ + 'txn_date' => '2026-10-18', + 'account_short' => '测试直连招行', + 'detail' => '直连收入', + 'income' => 50, + 'expense' => 0, + 'approval_no' => 'TEST-DIRECT-INCOME', + 'source' => 'manual', + 'source_value' => null, + 'row_key' => hash('sha256', 'test-direct-income'), + ], + ]); + + $this->loginAs(); + $response = $this->get(route('business.pages.show', [ + 'section' => 'financial-operations', + 'page' => 'fund-ledger', + ]) . '?tab=transactions&txns_from=2026-10-18&txns_to=2026-10-18') + ->assertOk() + ->assertSee('测试非直连华夏') + ->assertSee('130.00') + ->assertSee('120.00'); + + $html = $response->getContent(); + $this->assertMatchesRegularExpression( + '/TEST-DIRECT-INCOME.*?
{{ $row->counterparty_info }} {{ $row->income > 0 ? $money($row->income) : '' }} {{ $row->expense > 0 ? $money($row->expense) : '' }}{{ $row->balance === null ? '' : $money($row->balance) }}{{ $row->balance === null ? '—' : $money($row->balance) }} {{ $row->payment_remark }} {{ $row->detail }} {{ $transactionSourceLabel((string) $row->source, $row->effective_account_short) }}]*>—<\/td>/s', + $html, + ); + } +}