CaiWuXiTong/tests/Unit/EcommerceFinance/VipshopMpOpeningBalanceRepositoryTest.php
qiuxiang 378cf52fa7 feat: 增加唯品会MP账户月度期初余额
从唯品会后台账单余额表导入店铺月末余额,7月按期初加收入减支出滚动,后续月份自动承接上月期末;新增余额快照表、导入命令和回归测试。
2026-11-06 17:57:53 +08:00

98 lines
3.9 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();
}
public function test_opening_balance_rolls_from_june_ending_using_settlement_periods(): void
{
$companyId = (int) DB::table('companies')->value('id');
DB::connection('business_mirror')->table(VipshopMpOpeningBalanceImporter::TABLE)->insert([
[
'period_end' => '2026-06-30',
'store_name' => '唯品会MP-亿家达家具旗舰店',
'balance' => '100',
'source_filenames' => 'a.xlsx',
'source_sha256' => str_repeat('a', 64),
'imported_at' => now(),
'created_at' => now(),
'updated_at' => now(),
],
[
'period_end' => '2026-06-30',
'store_name' => '唯品会MP-朴美家具特卖旗舰店',
'balance' => '50',
'source_filenames' => 'b.xlsx',
'source_sha256' => str_repeat('b', 64),
'imported_at' => now(),
'created_at' => now(),
'updated_at' => now(),
],
]);
$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');
$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(175.0, $august['balance']);
$this->assertSame('2026-07-31', $august['date']);
$this->assertSame('previous_period', $august['source']);
$this->assertSame(191.0, $september['balance']);
$this->assertSame(125.0, $oneStore['balance']);
}
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(),
]);
}
}