fix(财务台账): 直连无流水时期末回退锁定期初 #1406
@ -1469,6 +1469,31 @@ final class NonStoreAccountMonitorPageData
|
||||
. (preg_replace('/\D+/', '', (string) $row->account_no) ?? '');
|
||||
});
|
||||
|
||||
$applyLockedOpeningFallback = static function (array &$row, array &$fixedOpenings): void {
|
||||
$accountShort = trim((string) ($row['account_short'] ?? ''));
|
||||
if (! array_key_exists($accountShort, $fixedOpenings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$openingBalance = round((float) $fixedOpenings[$accountShort], 2);
|
||||
$income = round((float) ($row['bank_bill_period_income'] ?? 0), 2);
|
||||
$expense = round((float) ($row['bank_bill_period_expense'] ?? 0), 2);
|
||||
$closingBalance = round($openingBalance + $income - $expense, 2);
|
||||
$blocked = round(
|
||||
min(max(0.0, (float) ($row['blocked_balance'] ?? 0)), max(0.0, $closingBalance)),
|
||||
2,
|
||||
);
|
||||
$row['opening_balance'] = $openingBalance;
|
||||
$row['inflow'] = $income;
|
||||
$row['outflow'] = $expense;
|
||||
$row['closing_balance'] = $closingBalance;
|
||||
$row['current_balance'] = $closingBalance;
|
||||
$row['blocked_balance'] = $blocked;
|
||||
$row['available_balance'] = round($closingBalance - $blocked, 2);
|
||||
$row['balance_source'] = 'monthly_opening_fallback';
|
||||
$row['monthly_opening_locked'] = true;
|
||||
};
|
||||
|
||||
foreach ($rows as &$row) {
|
||||
if (! ($row['uses_bank_balance'] ?? false)) {
|
||||
continue;
|
||||
@ -1477,6 +1502,7 @@ final class NonStoreAccountMonitorPageData
|
||||
$key = (string) ($row['platform_label'] ?? '') . '|' . $accountNo;
|
||||
$accountBills = $bankBills->get($key);
|
||||
if (! $accountBills instanceof Collection || $accountBills->isEmpty()) {
|
||||
$applyLockedOpeningFallback($row, $fixedOpenings);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -1493,6 +1519,9 @@ final class NonStoreAccountMonitorPageData
|
||||
);
|
||||
$latestBalance = $balanceRows->last();
|
||||
if ($latestBalance === null) {
|
||||
$row['bank_bill_period_income'] = round((float) $inPeriod->sum('income'), 2);
|
||||
$row['bank_bill_period_expense'] = round((float) $inPeriod->sum('expense'), 2);
|
||||
$applyLockedOpeningFallback($row, $fixedOpenings);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@ -453,6 +453,62 @@ class NonStoreAccountMonitorTest extends FeatureTestCase
|
||||
$this->assertStringNotContainsString('-99,999.00', $html);
|
||||
}
|
||||
|
||||
public function test_direct_account_without_period_bills_uses_locked_opening_as_closing(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
$connection->table('fin_accounts')->insert([
|
||||
'company_name' => '直连无流水公司',
|
||||
'account_short' => '直连无流水招行',
|
||||
'account_type' => '公户',
|
||||
'bank_name' => '招商银行常州分行',
|
||||
'account_no' => '519900000000001',
|
||||
'company_plan' => '保留',
|
||||
'account_should_keep' => '是',
|
||||
'current_balance' => 999,
|
||||
'daily_available' => 999,
|
||||
'blocked_amount' => 0,
|
||||
]);
|
||||
$connection->table('fin_flow_summary_account_sources')->insert([
|
||||
'account_short' => '直连无流水招行',
|
||||
'data_source' => '银企直连',
|
||||
'updated_by' => '测试',
|
||||
]);
|
||||
$connection->table('fin_account_opening')->insert([
|
||||
'account_short' => '直连无流水招行',
|
||||
'opening_date' => '2026-11-01',
|
||||
'opening_balance' => 1234.56,
|
||||
'remark' => '系统锁定:2026-10-31 最后一笔实际余额',
|
||||
]);
|
||||
|
||||
$this->loginAs()->get($this->pageUrl([
|
||||
'view' => 'accounts',
|
||||
'keep' => 'retained',
|
||||
'account_category' => 'bank',
|
||||
'account_search' => '直连无流水招行',
|
||||
'period_from' => '2026-11-01',
|
||||
'period_to' => '2026-11-30',
|
||||
]))->assertOk();
|
||||
|
||||
request()->merge([
|
||||
'view' => 'accounts',
|
||||
'keep' => 'retained',
|
||||
'account_category' => 'bank',
|
||||
'account_search' => '直连无流水招行',
|
||||
'period_from' => '2026-11-01',
|
||||
'period_to' => '2026-11-30',
|
||||
]);
|
||||
$page = app(\Modules\EcommerceFinance\View\NonStoreAccountMonitorPageData::class)->page();
|
||||
$row = collect($page['fundOverview']['bank_accounts'])
|
||||
->firstWhere('account_short', '直连无流水招行');
|
||||
|
||||
$this->assertNotNull($row);
|
||||
$this->assertSame(1234.56, $row['opening_balance']);
|
||||
$this->assertSame(1234.56, $row['closing_balance']);
|
||||
$this->assertSame(1234.56, $row['current_balance']);
|
||||
$this->assertSame('monthly_opening_fallback', $row['balance_source']);
|
||||
$this->assertTrue($row['monthly_opening_locked']);
|
||||
}
|
||||
|
||||
public function test_missing_monthly_opening_is_not_calculated_from_zero(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user