fix(财务台账): 严格锁定期初并保护手工余额 (#1410)
This commit is contained in:
parent
b0319e1cd1
commit
cda29213a1
@ -36,6 +36,7 @@ class LockFinanceLedgerMonthlyOpening extends Command
|
||||
. ';新增:' . $result['created']
|
||||
. ';更新:' . $result['updated']
|
||||
. ';无变化:' . $result['unchanged']
|
||||
. ';保留手工期初:' . $result['preserved_existing']
|
||||
);
|
||||
$this->line(
|
||||
'跳过:无真实余额 ' . $result['skipped_no_basis']
|
||||
@ -183,6 +184,7 @@ class LockFinanceLedgerMonthlyOpening extends Command
|
||||
$created = 0;
|
||||
$updated = 0;
|
||||
$unchanged = 0;
|
||||
$preservedExisting = 0;
|
||||
if ($apply) {
|
||||
$connection->transaction(function () use (
|
||||
$connection,
|
||||
@ -192,6 +194,7 @@ class LockFinanceLedgerMonthlyOpening extends Command
|
||||
&$created,
|
||||
&$updated,
|
||||
&$unchanged,
|
||||
&$preservedExisting,
|
||||
): void {
|
||||
foreach ($candidates as $short => $candidate) {
|
||||
$existing = $existingOpenings->get($short);
|
||||
@ -208,6 +211,10 @@ class LockFinanceLedgerMonthlyOpening extends Command
|
||||
$created++;
|
||||
continue;
|
||||
}
|
||||
if (! str_starts_with((string) $existing->remark, '系统锁定:')) {
|
||||
$preservedExisting++;
|
||||
continue;
|
||||
}
|
||||
if (abs((float) $existing->opening_balance - (float) $candidate['opening_balance']) < 0.005
|
||||
&& (string) $existing->remark === $remark) {
|
||||
$unchanged++;
|
||||
@ -234,8 +241,17 @@ class LockFinanceLedgerMonthlyOpening extends Command
|
||||
'created' => $created,
|
||||
'updated' => $updated,
|
||||
'unchanged' => $unchanged,
|
||||
'preserved_existing' => $preservedExisting,
|
||||
'skipped_no_basis' => $skippedNoBasis,
|
||||
'skipped_conflicts' => $skippedConflicts,
|
||||
'candidates' => array_map(
|
||||
static fn (string $short, array $candidate): array => [
|
||||
'account_short' => $short,
|
||||
...$candidate,
|
||||
],
|
||||
array_keys($candidates),
|
||||
array_values($candidates),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@ -52,7 +52,10 @@ final class FinanceLedgerBankDirectAccounts
|
||||
if (str_contains($accountShort, '招商')
|
||||
|| str_contains($accountShort, '招行')
|
||||
|| str_contains($bank, '招商')
|
||||
|| str_contains($bank, '招行')) {
|
||||
|| str_contains($bank, '招行')
|
||||
|| $accountType === '网商银行'
|
||||
|| str_contains($accountShort, '网商')
|
||||
|| str_contains($bank, '网商')) {
|
||||
$direct[$accountShort] = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ final class FinanceLedgerBankBalanceCurrentCalculator
|
||||
/**
|
||||
* 按账户期初和未删除动账明细计算滚动余额。
|
||||
* 该计算不读取 fin_accounts.current_balance 或 fin_daily_flows。
|
||||
* 没有实际期初的账户不返回余额,避免把缺失期初当成 0 后推算金额。
|
||||
*
|
||||
* @param list<string> $accountShorts
|
||||
* @return array<string,float>
|
||||
@ -30,14 +31,17 @@ final class FinanceLedgerBankBalanceCurrentCalculator
|
||||
return [];
|
||||
}
|
||||
|
||||
$openingDate = (string) config('finance_ledger.opening_date', '2026-10-17');
|
||||
$openingRows = $this->connection->table('fin_account_opening')
|
||||
->whereIn('account_short', $accountShorts)
|
||||
->when($asOf !== null, fn ($query) => $query->where('opening_date', '<=', $asOf))
|
||||
->orderBy('opening_date')
|
||||
->orderBy('id')
|
||||
->get(['account_short', 'opening_date', 'opening_balance'])
|
||||
->groupBy('account_short')
|
||||
->map(static fn ($rows) => $rows->last());
|
||||
if ($openingRows->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
$mappings = $this->connection->table('fin_account_map')
|
||||
->whereIn('account_short', $accountShorts)
|
||||
->get(['source', 'source_value', 'account_short']);
|
||||
@ -46,12 +50,9 @@ final class FinanceLedgerBankBalanceCurrentCalculator
|
||||
);
|
||||
$balances = [];
|
||||
$starts = [];
|
||||
foreach ($accountShorts as $accountShort) {
|
||||
$opening = $openingRows->get($accountShort);
|
||||
$balances[$accountShort] = round((float) ($opening->opening_balance ?? 0), 2);
|
||||
$starts[$accountShort] = $opening !== null
|
||||
? substr((string) $opening->opening_date, 0, 10)
|
||||
: $openingDate;
|
||||
foreach ($openingRows as $accountShort => $opening) {
|
||||
$balances[(string) $accountShort] = round((float) $opening->opening_balance, 2);
|
||||
$starts[(string) $accountShort] = substr((string) $opening->opening_date, 0, 10);
|
||||
}
|
||||
$earliestStart = min($starts);
|
||||
|
||||
|
||||
@ -677,6 +677,10 @@ final class NonStoreAccountMonitorPageData
|
||||
}
|
||||
$accountShort = (string) ($row['account_short'] ?? '');
|
||||
if (! array_key_exists($accountShort, $balances)) {
|
||||
$row['current_balance'] = null;
|
||||
$row['blocked_balance'] = null;
|
||||
$row['available_balance'] = null;
|
||||
$row['balance_source'] = 'missing_opening';
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -1319,6 +1323,21 @@ final class NonStoreAccountMonitorPageData
|
||||
->pluck('opening_balance', 'account_short')
|
||||
->map(static fn (mixed $value): float => round((float) $value, 2))
|
||||
->all();
|
||||
$metrics = [];
|
||||
foreach ($accountShorts as $accountShort) {
|
||||
$hasFixedOpening = array_key_exists((string) $accountShort, $fixedOpenings);
|
||||
$openingBalance = $hasFixedOpening
|
||||
? $fixedOpenings[(string) $accountShort]
|
||||
: null;
|
||||
$metrics[(string) $accountShort] = [
|
||||
'opening_balance' => $openingBalance,
|
||||
'inflow' => 0.0,
|
||||
'outflow' => 0.0,
|
||||
'closing_balance' => $openingBalance,
|
||||
'activity_days' => 0,
|
||||
'monthly_opening_locked' => $hasFixedOpening,
|
||||
];
|
||||
}
|
||||
|
||||
$query = $this->db()->table('fin_daily_flows')
|
||||
->whereIn('account_short', $accountShorts)
|
||||
@ -1326,31 +1345,22 @@ final class NonStoreAccountMonitorPageData
|
||||
->orderBy('account_short')
|
||||
->orderBy('flow_date')
|
||||
->orderBy('id');
|
||||
$metrics = [];
|
||||
|
||||
foreach ($query->get([
|
||||
'account_short',
|
||||
'flow_date',
|
||||
'opening_balance',
|
||||
'inflow',
|
||||
'outflow',
|
||||
'closing_balance',
|
||||
])->groupBy('account_short') as $accountShort => $flows) {
|
||||
$before = $flows->filter(
|
||||
static fn (object $row): bool => substr((string) $row->flow_date, 0, 10) < $period['from'],
|
||||
);
|
||||
$inPeriod = $flows->filter(static function (object $row) use ($period): bool {
|
||||
$date = substr((string) $row->flow_date, 0, 10);
|
||||
|
||||
return $date >= $period['from'] && $date <= $period['to'];
|
||||
});
|
||||
$latestBefore = $before->last();
|
||||
$firstInPeriod = $inPeriod->first();
|
||||
$openingBalance = array_key_exists((string) $accountShort, $fixedOpenings)
|
||||
? $fixedOpenings[(string) $accountShort]
|
||||
: ($latestBefore !== null
|
||||
? round((float) $latestBefore->closing_balance, 2)
|
||||
: ($firstInPeriod === null ? null : round((float) $firstInPeriod->opening_balance, 2)));
|
||||
: null;
|
||||
$inflow = round((float) $inPeriod->sum('inflow'), 2);
|
||||
$outflow = round((float) $inPeriod->sum('outflow'), 2);
|
||||
$metrics[(string) $accountShort] = [
|
||||
@ -1365,6 +1375,7 @@ final class NonStoreAccountMonitorPageData
|
||||
->map(static fn (mixed $date): string => substr((string) $date, 0, 10))
|
||||
->unique()
|
||||
->count(),
|
||||
'monthly_opening_locked' => array_key_exists((string) $accountShort, $fixedOpenings),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@ -453,6 +453,78 @@ class NonStoreAccountMonitorTest extends FeatureTestCase
|
||||
$this->assertStringNotContainsString('-99,999.00', $html);
|
||||
}
|
||||
|
||||
public function test_missing_monthly_opening_is_not_calculated_from_zero(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
$connection->table('fin_accounts')->insert([
|
||||
'company_name' => '缺失期初公司',
|
||||
'account_short' => '缺失期初工行',
|
||||
'account_type' => '公户',
|
||||
'bank_name' => '工商银行',
|
||||
'account_no' => '6222000099990001',
|
||||
'company_plan' => '保留',
|
||||
'account_should_keep' => '是',
|
||||
'current_balance' => 0,
|
||||
'daily_available' => 0,
|
||||
'blocked_amount' => 0,
|
||||
]);
|
||||
$connection->table('fin_flow_summary_account_sources')->insert([
|
||||
'account_short' => '缺失期初工行',
|
||||
'data_source' => '手动',
|
||||
'updated_by' => '测试',
|
||||
]);
|
||||
$connection->table('fin_daily_flows')->insert([
|
||||
'flow_date' => '2026-11-10',
|
||||
'company_name' => '缺失期初公司',
|
||||
'account_short' => '缺失期初工行',
|
||||
'opening_balance' => 0,
|
||||
'inflow' => 98765.43,
|
||||
'outflow' => 0,
|
||||
'closing_balance' => 98765.43,
|
||||
]);
|
||||
$connection->table('fin_transactions')->insert([
|
||||
'txn_date' => '2026-11-10',
|
||||
'account_short' => '缺失期初工行',
|
||||
'detail' => '无期初收入',
|
||||
'income' => 98765.43,
|
||||
'expense' => 0,
|
||||
'source' => 'manual',
|
||||
'source_value' => null,
|
||||
'row_key' => hash('sha256', 'missing-monthly-opening-income'),
|
||||
'synced_at' => '2026-11-10 10:00:00',
|
||||
'created_at' => '2026-11-10 10:00:00',
|
||||
'updated_at' => '2026-11-10 10:00:00',
|
||||
]);
|
||||
|
||||
$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->assertNull($row['opening_balance']);
|
||||
$this->assertNull($row['closing_balance']);
|
||||
$this->assertNull($row['current_balance']);
|
||||
$this->assertSame('missing_opening', $row['balance_source']);
|
||||
$this->assertFalse($row['monthly_opening_locked']);
|
||||
}
|
||||
|
||||
public function test_wangshang_account_balance_uses_bank_bills_instead_of_transaction_flows(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
@ -994,6 +1066,11 @@ class NonStoreAccountMonitorTest extends FeatureTestCase
|
||||
'outflow' => 0,
|
||||
'closing_balance' => 123456.78,
|
||||
]);
|
||||
$connection->table('fin_account_opening')->insert([
|
||||
'account_short' => '彰崚中行',
|
||||
'opening_date' => '2026-11-01',
|
||||
'opening_balance' => 123456.78,
|
||||
]);
|
||||
|
||||
$html = $this->loginAs()->get($this->pageUrl([
|
||||
'view' => 'accounts',
|
||||
@ -1063,6 +1140,11 @@ class NonStoreAccountMonitorTest extends FeatureTestCase
|
||||
->where('account_short', '保留直连工行')
|
||||
->where('flow_date', '2026-11-01')
|
||||
->update(['closing_balance' => 999999]);
|
||||
$connection->table('fin_account_opening')->insert([
|
||||
'account_short' => '保留直连工行',
|
||||
'opening_date' => '2026-11-01',
|
||||
'opening_balance' => 90000,
|
||||
]);
|
||||
$connection->table('fin_daily_flows')->insert([
|
||||
'flow_date' => '2026-11-02',
|
||||
'account_short' => '保留直连工行',
|
||||
|
||||
@ -113,4 +113,32 @@ class LockFinanceLedgerMonthlyOpeningTest extends TestCase
|
||||
'opening_date' => '2026-11-01',
|
||||
], 'business_mirror');
|
||||
}
|
||||
|
||||
public function test_it_preserves_an_existing_manual_monthly_opening(): void
|
||||
{
|
||||
DB::connection('business_mirror')->table('fin_account_opening')->insert([
|
||||
'account_short' => '有银行余额账户',
|
||||
'opening_date' => '2026-11-01',
|
||||
'opening_balance' => 399.99,
|
||||
'remark' => '手工导入期初',
|
||||
'created_at' => '2026-11-01 08:00:00',
|
||||
'updated_at' => '2026-11-01 08:00:00',
|
||||
]);
|
||||
|
||||
$this->artisan('finance-ledger:lock-monthly-opening', [
|
||||
'date' => '2026-11-01',
|
||||
'--apply' => true,
|
||||
])->expectsOutputToContain('保留手工期初:1')
|
||||
->assertSuccessful();
|
||||
|
||||
$opening = DB::connection('business_mirror')
|
||||
->table('fin_account_opening')
|
||||
->where('account_short', '有银行余额账户')
|
||||
->where('opening_date', '2026-11-01')
|
||||
->first();
|
||||
|
||||
$this->assertSame('399.99', (string) $opening->opening_balance);
|
||||
$this->assertSame('手工导入期初', $opening->remark);
|
||||
$this->assertSame('2026-11-01 08:00:00', (string) $opening->updated_at);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user