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, ], [ 'company_name' => '余额测试公司', 'account_short' => '测试手动招行', 'account_type' => '公户', 'bank_name' => '招商银行测试支行', 'current_balance' => 290, ], ]); $connection->table('fin_flow_summary_account_sources')->insert([ [ 'account_short' => '测试非直连华夏', 'data_source' => '手动', ], [ 'account_short' => '测试直连招行', 'data_source' => '银企直连', ], [ 'account_short' => '测试手动招行', 'data_source' => '手动', ], ]); $connection->table('fin_account_opening')->insert([ [ 'account_short' => '测试非直连华夏', 'opening_date' => '2026-10-01', 'opening_balance' => 0, ], [ 'account_short' => '测试直连招行', 'opening_date' => '2026-10-01', 'opening_balance' => 0, ], [ 'account_short' => '测试手动招行', 'opening_date' => '2026-10-01', 'opening_balance' => 0, ], ]); $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, ], [ 'flow_date' => '2026-10-18', 'company_name' => '余额测试公司', 'account_short' => '测试手动招行', 'opening_balance' => 300, 'inflow' => 0, 'outflow' => 10, 'closing_balance' => 290, ], ]); $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'), ], [ 'txn_date' => '2026-10-18', 'account_short' => '测试手动招行', 'detail' => '手动招行支出', 'income' => 0, 'expense' => 10, 'approval_no' => 'TEST-MANUAL-CMB-EXPENSE', 'source' => 'manual', 'source_value' => null, 'row_key' => hash('sha256', 'test-manual-cmb-expense'), ], ]); $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') ->assertSee('290.00'); $html = $response->getContent(); $this->assertMatchesRegularExpression( '/TEST-DIRECT-INCOME.*?]*>—<\/td>/s', $html, ); } public function test_transaction_rows_roll_from_the_latest_account_opening_without_daily_flow(): void { $connection = DB::connection('business_mirror'); $connection->table('fin_accounts')->insert([ 'company_name' => '期初滚动测试公司', 'account_short' => '测试期初滚动招行', 'account_type' => '公户', 'bank_name' => '招商银行测试支行', 'current_balance' => 175, ]); $connection->table('fin_flow_summary_account_sources')->insert([ 'account_short' => '测试期初滚动招行', 'data_source' => '手动', ]); $connection->table('fin_account_opening')->insert([ [ 'account_short' => '测试期初滚动招行', 'opening_date' => '2026-10-01', 'opening_balance' => 100, ], [ 'account_short' => '测试期初滚动招行', 'opening_date' => '2026-11-01', 'opening_balance' => 180, ], ]); $connection->table('fin_transactions')->insert([ [ 'txn_date' => '2026-10-18', 'account_short' => '测试期初滚动招行', 'detail' => '十月动账', 'income' => 30, 'expense' => 10, 'approval_no' => 'TEST-OPENING-ROLL-OCT', 'source' => 'manual', 'source_value' => null, 'row_key' => hash('sha256', 'test-opening-roll-oct'), ], [ 'txn_date' => '2026-11-20', 'account_short' => '测试期初滚动招行', 'detail' => '十一月支出', 'income' => 0, 'expense' => 20, 'approval_no' => 'TEST-OPENING-ROLL-NOV', 'source' => 'manual', 'source_value' => null, 'row_key' => hash('sha256', 'test-opening-roll-nov'), ], [ 'txn_date' => '2026-11-24', 'account_short' => '测试期初滚动招行', 'detail' => '目标日支出', 'income' => 0, 'expense' => 5, 'approval_no' => 'TEST-OPENING-ROLL-TARGET', 'source' => 'manual', 'source_value' => null, 'row_key' => hash('sha256', 'test-opening-roll-target'), ], ]); $this->loginAs(); $response = $this->get(route('business.pages.show', [ 'section' => 'financial-operations', 'page' => 'fund-ledger', ]) . '?tab=transactions&txns_from=2026-11-24&txns_to=2026-11-24') ->assertOk(); $response ->assertSee('TEST-OPENING-ROLL-TARGET') ->assertSee('155.00'); } public function test_non_direct_balance_uses_latest_stage_bill_and_later_movements(): void { $connection = DB::connection('business_mirror'); $accountId = $connection->table('fin_accounts')->insertGetId([ 'company_name' => '阶段账单测试公司', 'account_short' => '测试阶段账单华夏', 'account_type' => '公户', 'bank_name' => '华夏银行测试支行', 'account_no' => '1315000000000099', 'current_balance' => 0, ]); $connection->table('fin_account_opening')->insert([ 'account_short' => '测试阶段账单华夏', 'opening_date' => '2026-11-01', 'opening_balance' => 1000, ]); $connection->table('fin_transactions')->insert([ [ 'txn_date' => '2026-11-02', 'account_short' => '测试阶段账单华夏', 'detail' => '账单内收入', 'income' => 100, 'expense' => 0, 'source' => 'manual', 'row_key' => hash('sha256', 'stage-bill-in-bill-income'), ], [ 'txn_date' => '2026-11-03', 'account_short' => '测试阶段账单华夏', 'detail' => '账单内支出', 'income' => 0, 'expense' => 50, 'source' => 'manual', 'row_key' => hash('sha256', 'stage-bill-in-bill-expense'), ], [ 'txn_date' => '2026-11-03', 'account_short' => '测试阶段账单华夏', 'detail' => '同日不应重复追加', 'income' => 999, 'expense' => 0, 'source' => 'manual', 'row_key' => hash('sha256', 'stage-bill-same-day-income'), ], [ 'txn_date' => '2026-11-05', 'account_short' => '测试阶段账单华夏', 'detail' => '账单后收入', 'income' => 200, 'expense' => 0, 'source' => 'manual', 'row_key' => hash('sha256', 'stage-bill-after-income'), ], ]); $connection->table('fin_bank_direct_bills')->insert([ 'bank_name' => '华夏银行', 'finance_account_id' => $accountId, 'company_name' => '阶段账单测试公司', 'account_short' => '测试阶段账单华夏', 'account_no' => '1315000000000099', 'txn_date' => '2026-11-03', 'txn_at' => '2026-11-03 18:00:00', 'record_code' => 'STAGE-BILL-001', 'income' => 0, 'expense' => 0, 'balance' => 1050, 'source' => 'bank-monthly', 'row_key' => hash('sha256', 'stage-bill-row-1'), 'imported_at' => '2026-11-04 08:00:00', ]); $calculator = app(FinanceLedgerBankBalanceCurrentCalculator::class); $details = $calculator->balanceDetails(['测试阶段账单华夏'], '2026-11-06'); $this->assertSame(1250.0, $details['测试阶段账单华夏']['balance']); $this->assertSame('bank_bill_plus_transactions', $details['测试阶段账单华夏']['balance_source']); $this->assertSame('2026-11-03', $details['测试阶段账单华夏']['baseline_date']); $this->assertSame(200.0, $details['测试阶段账单华夏']['post_baseline_income']); $this->assertSame(0.0, $details['测试阶段账单华夏']['post_baseline_expense']); $this->assertSame(1, $details['测试阶段账单华夏']['movement_count']); $historical = $calculator->balanceDetails(['测试阶段账单华夏'], '2026-11-02'); $this->assertSame(1100.0, $historical['测试阶段账单华夏']['balance']); $this->assertSame('transactions', $historical['测试阶段账单华夏']['balance_source']); $connection->table('fin_bank_direct_bills')->insert([ 'bank_name' => '华夏银行', 'finance_account_id' => $accountId, 'company_name' => '阶段账单测试公司', 'account_short' => '测试阶段账单华夏', 'account_no' => '1315000000000099', 'txn_date' => '2026-11-05', 'txn_at' => '2026-11-05 18:00:00', 'record_code' => 'STAGE-BILL-002', 'income' => 0, 'expense' => 0, 'balance' => 1300, 'source' => 'bank-monthly', 'row_key' => hash('sha256', 'stage-bill-row-2'), 'imported_at' => '2026-11-06 08:00:00', ]); $updated = $calculator->balanceDetails(['测试阶段账单华夏'], '2026-11-06'); $this->assertSame(1300.0, $updated['测试阶段账单华夏']['balance']); $this->assertSame('2026-11-05', $updated['测试阶段账单华夏']['baseline_date']); $this->assertSame(0, $updated['测试阶段账单华夏']['movement_count']); } }