diff --git a/app/Console/Commands/LockFinanceLedgerMonthlyOpening.php b/app/Console/Commands/LockFinanceLedgerMonthlyOpening.php index 2522cd71..49c57a25 100644 --- a/app/Console/Commands/LockFinanceLedgerMonthlyOpening.php +++ b/app/Console/Commands/LockFinanceLedgerMonthlyOpening.php @@ -123,6 +123,30 @@ class LockFinanceLedgerMonthlyOpening extends Command $snapshotBefore[$accountNo] = $row; } } + $openingBefore = $connection->table('fin_account_opening') + ->where('opening_date', '<', $date->toDateString()) + ->orderBy('opening_date') + ->orderBy('id') + ->get(['account_short', 'opening_date', 'opening_balance']) + ->keyBy('account_short'); + $latestTransactionByShort = []; + $latestTransactionByAccountNo = []; + foreach ($connection->table('fin_transactions') + ->whereNull('deleted_at') + ->where('txn_date', '<=', $previousEnd->toDateString()) + ->cursor() as $row) { + $transactionDate = substr((string) $row->txn_date, 0, 10); + $short = trim((string) ($row->account_short ?? '')); + if ($short !== '' + && ($latestTransactionByShort[$short] ?? '') < $transactionDate) { + $latestTransactionByShort[$short] = $transactionDate; + } + $accountNo = $this->accountNo($row->source_value ?? null); + if ($accountNo !== '' + && ($latestTransactionByAccountNo[$accountNo] ?? '') < $transactionDate) { + $latestTransactionByAccountNo[$accountNo] = $transactionDate; + } + } $existingOpenings = $connection->table('fin_account_opening') ->where('opening_date', $date->toDateString()) ->get() @@ -138,6 +162,19 @@ class LockFinanceLedgerMonthlyOpening extends Command $bankBillBefore, $snapshotBefore, ); + [$carriedBalance, $carriedSource, $carriedSourceDate] = $this->carriedOpening( + $short, + $accountNo, + $openingBefore, + $latestTransactionByShort, + $latestTransactionByAccountNo, + ); + if ($carriedBalance !== null + && ($balance === null || $carriedSourceDate > $sourceDate)) { + $balance = $carriedBalance; + $source = $carriedSource; + $sourceDate = $carriedSourceDate; + } if ($balance === null) { $byShort[$short][] = [ 'account_id' => (int) $account->id, @@ -298,6 +335,37 @@ class LockFinanceLedgerMonthlyOpening extends Command return [null, '', '']; } + /** + * @return array{0:?float,1:string,2:string} + */ + private function carriedOpening( + string $accountShort, + string $accountNo, + mixed $openingBefore, + array $latestTransactionByShort, + array $latestTransactionByAccountNo, + ): array { + $opening = $openingBefore->get($accountShort); + if ($opening === null) { + return [null, '', '']; + } + + $openingDate = substr((string) $opening->opening_date, 0, 10); + $latestTransactionDate = max( + (string) ($latestTransactionByShort[$accountShort] ?? ''), + $accountNo !== '' ? (string) ($latestTransactionByAccountNo[$accountNo] ?? '') : '', + ); + if ($latestTransactionDate !== '' && $latestTransactionDate >= $openingDate) { + return [null, '', '']; + } + + return [ + round((float) $opening->opening_balance, 2), + 'opening_carry_forward', + $openingDate, + ]; + } + private function accountNo(mixed $value): string { return preg_replace('/\D+/', '', trim((string) $value)) ?? ''; diff --git a/tests/Unit/EcommerceFinance/LockFinanceLedgerMonthlyOpeningTest.php b/tests/Unit/EcommerceFinance/LockFinanceLedgerMonthlyOpeningTest.php index b11dd417..4fe29626 100644 --- a/tests/Unit/EcommerceFinance/LockFinanceLedgerMonthlyOpeningTest.php +++ b/tests/Unit/EcommerceFinance/LockFinanceLedgerMonthlyOpeningTest.php @@ -39,11 +39,13 @@ class LockFinanceLedgerMonthlyOpeningTest extends TestCase $schema->create('fin_transactions', function (Blueprint $table): void { $table->increments('id'); $table->date('txn_date'); + $table->string('account_short')->nullable(); $table->string('source'); $table->string('source_value')->nullable(); $table->decimal('income', 18, 2)->default(0); $table->decimal('expense', 18, 2)->default(0); $table->decimal('balance', 18, 2)->nullable(); + $table->timestamp('deleted_at')->nullable(); }); $schema->create('fin_bank_direct_bills', function (Blueprint $table): void { $table->increments('id'); @@ -141,4 +143,68 @@ class LockFinanceLedgerMonthlyOpeningTest extends TestCase $this->assertSame('手工导入期初', $opening->remark); $this->assertSame('2026-11-01 08:00:00', (string) $opening->updated_at); } + + public function test_it_carries_forward_an_opening_when_no_later_transactions_exist(): void + { + DB::connection('business_mirror')->table('fin_accounts')->insert([ + 'account_short' => '无交易沿用账户', + 'account_no' => '6200000000000003', + ]); + DB::connection('business_mirror')->table('fin_account_opening')->insert([ + 'account_short' => '无交易沿用账户', + 'opening_date' => '2026-10-01', + 'opening_balance' => 23.47, + 'remark' => 'PDF银行对账单期初', + 'created_at' => '2026-10-01 08:00:00', + 'updated_at' => '2026-10-01 08:00:00', + ]); + + $this->artisan('finance-ledger:lock-monthly-opening', [ + 'date' => '2026-11-01', + '--apply' => true, + ])->assertSuccessful(); + + $this->assertDatabaseHas('fin_account_opening', [ + 'account_short' => '无交易沿用账户', + 'opening_date' => '2026-11-01', + 'opening_balance' => 23.47, + 'remark' => '系统锁定:2026-10-01 最后一笔实际余额', + ], 'business_mirror'); + } + + public function test_it_does_not_carry_forward_past_unpriced_transactions(): void + { + $db = DB::connection('business_mirror'); + $db->table('fin_accounts')->insert([ + 'account_short' => '有交易无余额账户', + 'account_no' => '6200000000000004', + ]); + $db->table('fin_account_opening')->insert([ + 'account_short' => '有交易无余额账户', + 'opening_date' => '2026-10-01', + 'opening_balance' => 100, + 'remark' => 'PDF银行对账单期初', + 'created_at' => '2026-10-01 08:00:00', + 'updated_at' => '2026-10-01 08:00:00', + ]); + $db->table('fin_transactions')->insert([ + 'txn_date' => '2026-10-02', + 'account_short' => '有交易无余额账户', + 'source' => 'bank_statement', + 'source_value' => '6200000000000004', + 'income' => 0, + 'expense' => 10, + 'balance' => null, + ]); + + $this->artisan('finance-ledger:lock-monthly-opening', [ + 'date' => '2026-11-01', + '--apply' => true, + ])->assertSuccessful(); + + $this->assertDatabaseMissing('fin_account_opening', [ + 'account_short' => '有交易无余额账户', + 'opening_date' => '2026-11-01', + ], 'business_mirror'); + } }