CaiWuXiTong/tests/Unit/EcommerceFinance/VipshopMpOpeningBalanceRepositoryTest.php

133 lines
6.0 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Repositories\VipshopMpOpeningBalanceRepository;
use Modules\EcommerceFinance\Services\VipshopMpOpeningBalanceImporter;
use Tests\TestCase;
class VipshopMpOpeningBalanceRepositoryTest 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_06_180000_create_vipshop_mp_opening_balance_snapshots.php'))->up();
(require database_path('migrations/2026_12_01_180000_add_settlement_to_vipshop_mp_opening_balance_snapshots.php'))->up();
}
public function test_opening_balance_subtracts_prior_settlements_and_exposes_current_settlement(): void
{
$companyId = (int) DB::table('companies')->value('id');
$this->snapshot('2026-06-30', '唯品会MP-亿家达家具旗舰店', '100', '0', 0);
$this->snapshot('2026-06-30', '唯品会MP-朴美家具特卖旗舰店', '50', '0', 0);
$this->snapshot('2026-07-31', '唯品会MP-亿家达家具旗舰店', '115', '15', 1);
$this->snapshot('2026-07-31', '唯品会MP-朴美家具特卖旗舰店', '50', '0', 0);
$this->snapshot('2026-08-31', '唯品会MP-亿家达家具旗舰店', '118', '10', 1);
$this->snapshot('2026-08-31', '唯品会MP-朴美家具特卖旗舰店', '58', '0', 0);
$this->snapshot('2026-09-30', '唯品会MP-亿家达家具旗舰店', '128', '20', 1);
$this->snapshot('2026-09-30', '唯品会MP-朴美家具特卖旗舰店', '58', '0', 0);
$this->bill($companyId, 'JUL-A', '唯品会MP-亿家达家具旗舰店', '2026-07', '30', '5');
$this->bill($companyId, 'AUG-A', '唯品会MP-亿家达家具旗舰店', '2026-08', '10', '2');
$this->bill($companyId, 'AUG-B', '唯品会MP-朴美家具特卖旗舰店', '2026-08', '8', '0');
$repository = app(VipshopMpOpeningBalanceRepository::class);
$july = $repository->openingBalance(null, '2026-07-01', '2026-07-31');
$august = $repository->openingBalance(null, '2026-08-01', '2026-08-31');
$september = $repository->openingBalance(null, '2026-09-01', '2026-09-30');
$julyToSeptember = $repository->openingBalance(null, '2026-07-01', '2026-09-30');
$oneStore = $repository->openingBalance('唯品会MP-亿家达家具旗舰店', '2026-08-01', '2026-08-31');
$this->assertSame(150.0, $july['balance']);
$this->assertSame('2026-06-30', $july['date']);
$this->assertSame('june_ending', $july['source']);
$this->assertSame(15.0, $july['settlement_amount']);
$this->assertSame(1, $july['settlement_count']);
$this->assertTrue($july['settlement_available']);
$this->assertTrue($july['settlement_history_complete']);
$this->assertSame([], $july['settlement_missing_periods']);
$this->assertSame(160.0, $august['balance']);
$this->assertSame('2026-07-31', $august['date']);
$this->assertSame('previous_period', $august['source']);
$this->assertSame(10.0, $august['settlement_amount']);
$this->assertSame(166.0, $september['balance']);
$this->assertSame(20.0, $september['settlement_amount']);
$this->assertSame(150.0, $julyToSeptember['balance']);
$this->assertSame(45.0, $julyToSeptember['settlement_amount']);
$this->assertSame(3, $julyToSeptember['settlement_count']);
$this->assertSame(110.0, $oneStore['balance']);
}
public function test_missing_monthly_settlement_is_not_reported_as_zero(): void
{
$this->snapshot('2026-06-30', '唯品会MP-亿家达家具旗舰店', '100', '0', 0);
$result = app(VipshopMpOpeningBalanceRepository::class)
->openingBalance(null, '2026-10-01', '2026-10-31');
$this->assertFalse($result['settlement_available']);
$this->assertFalse($result['settlement_history_complete']);
$this->assertSame(
['2026-07-31', '2026-08-31', '2026-09-30', '2026-10-31'],
$result['settlement_missing_periods'],
);
$this->assertSame(0.0, $result['settlement_amount']);
}
private function snapshot(
string $periodEnd,
string $storeName,
string $balance,
string $settledAmount,
int $settledRowCount,
): void {
DB::connection('business_mirror')->table(VipshopMpOpeningBalanceImporter::TABLE)->insert([
'period_end' => $periodEnd,
'store_name' => $storeName,
'balance' => $balance,
'settled_amount' => $settledAmount,
'settled_row_count' => $settledRowCount,
'source_filenames' => 'snapshot.xlsx',
'source_sha256' => str_repeat('a', 64),
'imported_at' => now(),
'created_at' => now(),
'updated_at' => now(),
]);
}
private function bill(
int $companyId,
string $recordCode,
string $storeName,
string $period,
string $income,
string $expense,
): void {
DB::table('ec_account_bills')->insert([
'company_id' => $companyId,
'bill_type' => 'vipshop',
'record_code' => $recordCode,
'store_name' => $storeName,
'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(),
]);
}
}