CaiWuXiTong/tests/Feature/Business/FinanceLedgerTransactionBalanceTest.php
shanshi b33b87a161 fix: 统一非直连账户余额基准 (#1705)
修复非直连账户在月度期初与阶段银行账单并存时的余额基准选择,并移除保留账户期初覆盖户数展示。
2026-12-03 17:37:50 +08:00

405 lines
16 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\Business;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Services\FinanceLedgerBankBalanceCurrentCalculator;
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,
],
[
'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 class="num"[^>]*>—<\/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']);
}
public function test_non_direct_balance_prefers_newer_monthly_opening_over_older_stage_bill(): void
{
$connection = DB::connection('business_mirror');
$accountId = $connection->table('fin_accounts')->insertGetId([
'company_name' => '新旧基准测试公司',
'account_short' => '测试新期初旧账单工行',
'account_type' => '公户',
'bank_name' => '中国工商银行测试支行',
'account_no' => '1105000000006936',
'current_balance' => 346101.84,
]);
$connection->table('fin_account_opening')->insert([
'account_short' => '测试新期初旧账单工行',
'opening_date' => '2026-12-01',
'opening_balance' => 28764.32,
'remark' => '期初表导入:2026-11-30 期末余额',
]);
$connection->table('fin_transactions')->insert([
'txn_date' => '2026-12-03',
'account_short' => '测试新期初旧账单工行',
'detail' => '唯品会回款',
'income' => 317337.52,
'expense' => 0,
'source' => 'manual',
'source_value' => null,
'row_key' => hash('sha256', 'new-opening-old-stage-bill-income'),
]);
$connection->table('fin_bank_direct_bills')->insert([
'bank_name' => '中国工商银行',
'finance_account_id' => $accountId,
'company_name' => '新旧基准测试公司',
'account_short' => '测试新期初旧账单工行',
'account_no' => '1105000000006936',
'txn_date' => '2026-11-25',
'txn_at' => '2026-11-25 17:13:31',
'record_code' => '000000000',
'income' => 0,
'expense' => 0,
'balance' => 33557.68,
'source' => 'bank-monthly',
'row_key' => hash('sha256', 'new-opening-old-stage-bill'),
'imported_at' => '2026-11-25 10:53:51',
]);
$details = app(FinanceLedgerBankBalanceCurrentCalculator::class)
->balanceDetails(['测试新期初旧账单工行'], '2026-12-03');
$detail = $details['测试新期初旧账单工行'];
$this->assertSame(346101.84, $detail['balance']);
$this->assertSame('transactions', $detail['balance_source']);
$this->assertSame('2026-12-01', $detail['baseline_date']);
$this->assertSame(28764.32, $detail['baseline_balance']);
$this->assertSame(317337.52, $detail['post_baseline_income']);
$this->assertSame(1, $detail['movement_count']);
}
}