CaiWuXiTong/tests/Unit/EcommerceFinance/VipshopJxOpeningBalanceRepositoryTest.php

89 lines
3.3 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Repositories\VipshopJxOpeningBalanceRepository;
use Tests\TestCase;
class VipshopJxOpeningBalanceRepositoryTest 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');
(require database_path('migrations/2026_11_07_120000_create_vipshop_jx_opening_balance_snapshots.php'))->up();
}
public function test_opening_balance_rolls_from_june_ending_by_settlement_period(): void
{
$companyId = (int) DB::table('companies')->value('id');
DB::connection('business_mirror')->table(VipshopJxOpeningBalanceRepository::TABLE)->insert([
'period_end' => '2026-06-30',
'balance' => '491948.53',
'source' => '唯品会结算平台历史账单累计',
'imported_at' => now(),
'created_at' => now(),
'updated_at' => now(),
]);
$this->bill($companyId, 'JUL-A', '2026-07', '300', '50');
$this->bill($companyId, 'AUG-A', '2026-08', '100', '20');
$repository = app(VipshopJxOpeningBalanceRepository::class);
$july = $repository->openingBalance('2026-07-01', '2026-07-31');
$august = $repository->openingBalance('2026-08-01', '2026-08-31');
$september = $repository->openingBalance('2026-09-01', '2026-09-30');
$this->assertSame(491948.53, $july['balance']);
$this->assertSame('2026-06-30', $july['date']);
$this->assertSame('june_ending', $july['source']);
$this->assertSame(492198.53, $august['balance']);
$this->assertSame('2026-07-31', $august['date']);
$this->assertSame('previous_period', $august['source']);
$this->assertSame(492278.53, $september['balance']);
}
public function test_it_returns_no_opening_balance_before_the_june_baseline(): void
{
$result = app(VipshopJxOpeningBalanceRepository::class)
->openingBalance('2026-06-01', '2026-06-30');
$this->assertSame(0.0, $result['balance']);
$this->assertNull($result['date']);
$this->assertSame('none', $result['source']);
}
private function bill(
int $companyId,
string $recordCode,
string $period,
string $income,
string $expense,
): void {
DB::table('ec_account_bills')->insert([
'company_id' => $companyId,
'bill_type' => 'vipshop-jx',
'record_code' => $recordCode,
'store_name' => '唯品会JITX_亿家达',
'order_no' => 'ORDER-' . $recordCode,
'bill_item' => '销售明细',
'occurred_at' => $period . '-15 10:00:00',
'income' => $income,
'expense' => $expense,
'amount' => bcsub($income, $expense, 2),
'balance' => 0,
'payload' => json_encode(['settle_period' => $period], JSON_THROW_ON_ERROR),
'created_at' => now(),
'updated_at' => now(),
]);
}
}