CaiWuXiTong/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewJdWalletOpeningTest.php

109 lines
4.1 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Services\StoreReceivingBalanceOverviewService;
use Tests\TestCase;
class StoreReceivingBalanceOverviewJdWalletOpeningTest 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('jd_wallet_bill_periods', function (Blueprint $table): void {
$table->id();
$table->string('active_generation');
$table->date('period_start');
$table->timestamp('imported_at')->nullable();
});
$schema->create('jd_wallet_bill_rows', function (Blueprint $table): void {
$table->string('generation');
$table->string('record_code');
$table->string('store_name')->nullable();
$table->string('account_name')->nullable();
$table->dateTime('occurred_at');
$table->decimal('income', 20, 2)->default(0);
$table->decimal('expense', 20, 2)->default(0);
$table->decimal('balance', 20, 2)->nullable();
$table->primary(['generation', 'record_code']);
});
DB::connection('business_mirror')->table('jd_wallet_bill_periods')->insert([
'active_generation' => 'generation-1',
'period_start' => '2026-10-01',
'imported_at' => '2026-11-01 10:00:00',
]);
}
public function test_new_jd_wallet_store_gets_zero_opening_but_existing_store_without_balance_stays_unavailable(): void
{
$this->insertBill('老店', '2026-09-30 10:00:00', 100, 0, 100);
$this->insertBill('老店', '2026-10-10 10:00:00', 10, 0, 110);
$this->insertBill('10月新店', '2026-10-10 10:00:00', 100, 0, 158.57);
$this->insertBill('10月新店', '2026-10-11 10:00:00', 50, 0, 208.57);
$this->insertBill('历史缺余额店', '2026-09-01 10:00:00', 0, 0, null);
$this->insertBill('历史缺余额店', '2026-10-12 10:00:00', 20, 0, 20);
$service = new class extends StoreReceivingBalanceOverviewService {
public function __construct()
{
}
};
$result = $service->dailyBalanceStores(
'2026-10-01',
'2026-10-31',
'jd-wallet',
'',
1,
50,
);
$rows = collect($result['rows'])->keyBy('store_name');
$this->assertSame(0.0, $rows['10月新店']['opening_balance']);
$this->assertSame(150.0, $rows['10月新店']['amount']);
$this->assertTrue($rows['10月新店']['available']);
$this->assertSame(100.0, $rows['老店']['opening_balance']);
$this->assertSame(110.0, $rows['老店']['amount']);
$this->assertNull($rows['历史缺余额店']['opening_balance']);
$this->assertNull($rows['历史缺余额店']['amount']);
$this->assertFalse($rows['历史缺余额店']['available']);
$this->assertSame(2, $result['summary']['available_store_count']);
}
private function insertBill(
string $storeName,
string $occurredAt,
float $income,
float $expense,
?float $balance,
): void {
static $sequence = 0;
$sequence++;
DB::connection('business_mirror')->table('jd_wallet_bill_rows')->insert([
'generation' => 'generation-1',
'record_code' => 'JD-OPENING-' . $sequence,
'store_name' => $storeName,
'account_name' => '账户A',
'occurred_at' => $occurredAt,
'income' => $income,
'expense' => $expense,
'balance' => $balance,
]);
}
}