CaiWuXiTong/tests/Unit/EcommerceFinance/LockFinanceLedgerMonthlyOpeningTest.php

211 lines
8.0 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\Unit\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class LockFinanceLedgerMonthlyOpeningTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config([
'database.connections.business_mirror' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
'upstream_mirrors.connection' => 'business_mirror',
]);
DB::purge('business_mirror');
$schema = Schema::connection('business_mirror');
$schema->create('fin_accounts', function (Blueprint $table): void {
$table->increments('id');
$table->string('account_short');
$table->string('account_no')->nullable();
});
$schema->create('fin_account_opening', function (Blueprint $table): void {
$table->increments('id');
$table->string('account_short');
$table->date('opening_date');
$table->decimal('opening_balance', 18, 2);
$table->string('remark')->nullable();
$table->timestamps();
});
$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');
$table->string('account_no');
$table->date('txn_date');
$table->timestamp('txn_at')->nullable();
$table->decimal('income', 18, 2)->default(0);
$table->decimal('expense', 18, 2)->default(0);
$table->decimal('balance', 18, 2)->nullable();
});
$schema->create('fin_bank_direct_balance_snapshots', function (Blueprint $table): void {
$table->increments('id');
$table->string('account_no');
$table->date('balance_date');
$table->timestamp('balance_at')->nullable();
$table->decimal('account_balance', 18, 2)->nullable();
$table->decimal('available_balance', 18, 2);
});
$db = DB::connection('business_mirror');
$db->table('fin_accounts')->insert([
['account_short' => '有银行余额账户', 'account_no' => '6200000000000001'],
['account_short' => '无依据账户', 'account_no' => '6200000000000002'],
]);
$db->table('fin_transactions')->insert([
[
'txn_date' => '2026-10-31',
'source' => 'bank_statement',
'source_value' => '6200000000000001',
'income' => 0,
'expense' => 85000,
'balance' => 412.44,
],
[
'txn_date' => '2026-11-01',
'source' => 'bank_statement',
'source_value' => '6200000000000002',
'income' => 100,
'expense' => 0,
'balance' => 110,
],
]);
}
protected function tearDown(): void
{
DB::purge('business_mirror');
parent::tearDown();
}
public function test_it_locks_the_previous_month_last_real_balance(): void
{
$this->artisan('finance-ledger:lock-monthly-opening', [
'date' => '2026-11-01',
'--apply' => true,
])->expectsOutputToContain('新增:1')
->expectsOutputToContain('无真实余额 1')
->assertSuccessful();
$this->assertDatabaseHas('fin_account_opening', [
'account_short' => '有银行余额账户',
'opening_date' => '2026-11-01',
'opening_balance' => 412.44,
], 'business_mirror');
$this->assertDatabaseMissing('fin_account_opening', [
'account_short' => '无依据账户',
'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);
}
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');
}
}