fix(财务台账): 补齐非直连动账余额展示 (#1415)
This commit is contained in:
parent
267d62ee43
commit
ac31ca56ea
@ -160,6 +160,111 @@ final class FinanceLedgerBankBalanceCurrentCalculator
|
||||
return $movements;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按期初和当日逐笔动账计算非银企直连账户的交易后余额。
|
||||
* 以每日流水中的期初余额为起点,同一账户同一天按交易 ID 顺序滚动。
|
||||
*
|
||||
* @param iterable<object> $rows
|
||||
* @param array<string,true> $excludedAccountShorts
|
||||
* @return array<int,float>
|
||||
*/
|
||||
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<string> */
|
||||
private function normalizeAccountShorts(array $accountShorts): array
|
||||
{
|
||||
|
||||
@ -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<array{id:int,field_key:string,field_name:string}>
|
||||
*/
|
||||
|
||||
@ -2357,7 +2357,7 @@
|
||||
<div class="ledger-actions"><button class="ledger-btn primary" type="submit"><span class="material-icons-outlined">search</span>查询</button><a class="ledger-btn" href="{{ $transactionViewResetUrl }}"><span class="material-icons-outlined">refresh</span>重置</a></div>
|
||||
</form>
|
||||
<div class="ledger-summary" data-ledger-transaction-summary><div class="ledger-summary__item"><span>上月期末余额(万元)</span><b title="{{ $moneyYuanTitle($ledgerTransactionSummary['previous_closing'] ?? null) }}" data-ledger-transaction-summary-previous-closing>{{ ($ledgerTransactionSummary['previous_closing'] ?? null) === null ? '—' : $moneyWan($ledgerTransactionSummary['previous_closing']) }}</b></div><div class="ledger-summary__item"><span>当月收入(万元)</span><b title="{{ $moneyYuanTitle($ledgerTransactionSummary['income'] ?? 0) }}" data-ledger-transaction-summary-income>{{ $moneyWan($ledgerTransactionSummary['income'] ?? 0) }}</b></div><div class="ledger-summary__item"><span>当月支出(万元)</span><b title="{{ $moneyYuanTitle($ledgerTransactionSummary['expense'] ?? 0) }}" data-ledger-transaction-summary-expense>{{ $moneyWan($ledgerTransactionSummary['expense'] ?? 0) }}</b></div><div class="ledger-summary__item"><span>当前余额(万元)</span><b title="{{ $moneyYuanTitle($ledgerTransactionSummary['current_balance'] ?? null) }}" data-ledger-transaction-summary-current-balance>{{ ($ledgerTransactionSummary['current_balance'] ?? null) === null ? '—' : $moneyWan($ledgerTransactionSummary['current_balance']) }}</b></div></div>
|
||||
<div class="ledger-note">{{ $transactionViewIsIndependent ? '本页导入和新增的每条明细均可编辑、删除;可像 Excel 一样在任意行上下插入、在自定义列左右插入。自定义列及列数据只属于当前独立账单,不进入其他页面。拖动表头右侧边界可调整列宽,双击边界恢复默认。' : '账户简称按“映射表 → 来源原始值 → 未匹配”解析;手工录入使用账户总表简称,并自动计入每日资金流水,不回写上游。拖动表头右侧边界可调整列宽,双击边界恢复默认。' }}</div>
|
||||
<div class="ledger-note">{{ $transactionViewIsIndependent ? '本页导入和新增的每条明细均可编辑、删除;可像 Excel 一样在任意行上下插入、在自定义列左右插入。自定义列及列数据只属于当前独立账单,不进入其他页面。拖动表头右侧边界可调整列宽,双击边界恢复默认。' : '账户简称按“映射表 → 来源原始值 → 未匹配”解析;手工录入使用账户总表简称,并自动计入每日资金流水,不回写上游。非银企直连账户的余额按期初余额和当日动账滚动计算;银企直连账户显示 —。拖动表头右侧边界可调整列宽,双击边界恢复默认。' }}</div>
|
||||
<div class="ledger-table-wrap">
|
||||
<table class="ledger-table ledger-transaction-table" style="min-width:92rem"><thead><tr>
|
||||
@foreach ($ledgerTableHeaders['transactions'] as $header)
|
||||
@ -2488,7 +2488,7 @@
|
||||
<td class="wrap">{{ $row->counterparty_info }}</td>
|
||||
<td class="num">{{ $row->income > 0 ? $money($row->income) : '' }}</td>
|
||||
<td class="num">{{ $row->expense > 0 ? $money($row->expense) : '' }}</td>
|
||||
<td class="num">{{ $row->balance === null ? '' : $money($row->balance) }}</td>
|
||||
<td class="num" title="{{ match ($row->balance_source ?? null) { 'calculated' => '非银企直连账户:按期初余额和当日逐笔动账滚动计算', 'direct' => '银企直连账户:逐笔余额不在本列展示', 'unmatched' => '尚未匹配账户,无法计算余额', default => '缺少有效期初或每日流水,暂无法计算余额' } }}">{{ $row->balance === null ? '—' : $money($row->balance) }}</td>
|
||||
<td class="wrap">{{ $row->payment_remark }}</td>
|
||||
<td class="wrap">{{ $row->detail }}</td>
|
||||
<td><span class="ledger-source-tag">{{ $transactionSourceLabel((string) $row->source, $row->effective_account_short) }}</span></td>
|
||||
|
||||
116
tests/Feature/Business/FinanceLedgerTransactionBalanceTest.php
Normal file
116
tests/Feature/Business/FinanceLedgerTransactionBalanceTest.php
Normal file
@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Business;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\Feature\FeatureTestCase;
|
||||
|
||||
class FinanceLedgerTransactionBalanceTest extends FeatureTestCase
|
||||
{
|
||||
public function test_transaction_rows_calculate_non_direct_balances_and_hide_direct_balances(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
$connection->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.*?<td class="num"[^>]*>—<\/td>/s',
|
||||
$html,
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user