CaiWuXiTong/tests/Unit/EcommerceFinance/TaobaoWechatOpeningBalanceRepositoryTest.php
qiuxiang cb4fc5af0d fix: 补充华斯旗舰店期初余额 (#1472)
增加天猫-华斯旗舰店零期初余额,并补充生产数据修正迁移。
2026-11-26 16:58:13 +08:00

257 lines
11 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\Repositories\TaobaoWechatOpeningBalanceRepository;
use Modules\EcommerceFinance\Support\TaobaoWechatBackofficeJuneEndingBalances;
use Tests\TestCase;
class TaobaoWechatOpeningBalanceRepositoryTest 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');
$this->createTables();
}
public function test_july_opening_uses_the_june_ending_balance(): void
{
$this->insertOpening('淘系微信一店', '669385.40');
$result = app(TaobaoWechatOpeningBalanceRepository::class)
->openingBalance('淘系微信一店', '2026-07-01');
$this->assertSame(669385.4, $result['balance']);
$this->assertSame('2026-06-30', $result['date']);
$this->assertSame(TaobaoWechatBackofficeJuneEndingBalances::SOURCE, $result['source']);
$this->assertSame(1, $result['store_count']);
}
public function test_later_periods_carry_the_opening_balance_forward(): void
{
$this->insertOpening('淘系微信一店', '100.00');
$this->insertBill('淘系微信一店', '2026-07-05 10:00:00', '50.00', '20.00');
$this->insertBill('淘系微信一店', '2026-08-02 10:00:00', '10.00', '5.00');
$result = app(TaobaoWechatOpeningBalanceRepository::class)
->openingBalance('淘系微信一店', '2026-08-01');
$this->assertSame(130.0, $result['balance']);
$this->assertSame('2026-07-31', $result['date']);
$this->assertSame(
TaobaoWechatBackofficeJuneEndingBalances::SOURCE . '_carried_forward',
$result['source'],
);
}
public function test_all_store_openings_are_summed_when_no_store_is_selected(): void
{
$this->insertOpening('淘系微信一店', '100.00');
$this->insertOpening('淘系微信二店', '200.25');
$result = app(TaobaoWechatOpeningBalanceRepository::class)
->openingBalance(null, '2026-07-01');
$this->assertSame(300.25, $result['balance']);
$this->assertSame(2, $result['store_count']);
}
public function test_backoffice_dataset_contains_the_twenty_one_confirmed_store_balances(): void
{
$balances = TaobaoWechatBackofficeJuneEndingBalances::balances();
$totalCents = array_sum(array_map(
static fn (string $balance): int => (int) round((float) $balance * 100),
array_values($balances),
));
$this->assertCount(22, $balances);
$this->assertSame(83248878, $totalCents);
$this->assertSame('27846.22', $balances['天猫-HX旗舰店101']);
$this->assertSame('290881.84', $balances['天猫-亿家达旗舰店']);
$this->assertSame('5155.88', $balances['天猫-亿家达宜彩专卖店']);
$this->assertSame('0.00', $balances['天猫-华斯旗舰店']);
$this->assertSame('1371.70', $balances['天猫-斯福恩旗舰店']);
$this->assertSame('16486.66', $balances['淘宝-欧意朗森屿家栖企业店']);
$this->assertSame('86802.00', $balances['淘宝-全颐家具工厂店']);
}
public function test_sifuen_june_ending_balance_rolls_forward_when_june_has_no_activity(): void
{
$this->insertOpening('天猫-斯福恩旗舰店', '1371.70');
$result = app(TaobaoWechatOpeningBalanceRepository::class)
->openingBalance('天猫-斯福恩旗舰店', '2026-07-01');
$this->assertSame(1371.7, $result['balance']);
$this->assertSame('2026-06-30', $result['date']);
$this->assertSame(TaobaoWechatBackofficeJuneEndingBalances::SOURCE, $result['source']);
}
public function test_sifuen_october_balance_rolls_forward_through_prior_month_activity(): void
{
$this->insertOpening('天猫-斯福恩旗舰店', '1371.70');
$this->insertBill('天猫-斯福恩旗舰店', '2026-07-31 10:00:00', '1984.89', '11.92');
$this->insertBill('天猫-斯福恩旗舰店', '2026-08-31 10:00:00', '1412.45', '8.47');
$this->insertBill('天猫-斯福恩旗舰店', '2026-09-30 10:00:00', '3850.85', '23.11');
$this->insertBill('天猫-斯福恩旗舰店', '2026-10-31 10:00:00', '912.12', '503.47');
$opening = app(TaobaoWechatOpeningBalanceRepository::class)
->openingBalance('天猫-斯福恩旗舰店', '2026-10-01');
$october = DB::connection('business_mirror')->table('taobao_wechat_bill_rows')
->where('store_name', '天猫-斯福恩旗舰店')
->where('occurred_at', '>=', '2026-10-01 00:00:00')
->where('occurred_at', '<', '2026-11-01 00:00:00')
->selectRaw(
'COALESCE(SUM(income_amount), 0) AS income, '
. 'COALESCE(SUM(expense_amount), 0) AS expense'
)
->first();
$this->assertSame(8576.39, $opening['balance']);
$this->assertSame('2026-09-30', $opening['date']);
$this->assertSame(8985.04, round(
(float) $opening['balance']
+ (float) $october->income
- (float) $october->expense,
2,
));
}
public function test_senyu_july_opening_rolls_the_june_ending_balance_before_withdrawal(): void
{
$this->insertOpening('淘宝-欧意朗森屿家栖企业店', '16486.66');
$this->insertBill('淘宝-欧意朗森屿家栖企业店', '2026-07-13 17:21:38', '0.00', '16486.66');
$opening = app(TaobaoWechatOpeningBalanceRepository::class)
->openingBalance('淘宝-欧意朗森屿家栖企业店', '2026-07-01');
$july = DB::connection('business_mirror')->table('taobao_wechat_bill_rows')
->where('store_name', '淘宝-欧意朗森屿家栖企业店')
->where('occurred_at', '>=', '2026-07-01 00:00:00')
->where('occurred_at', '<', '2026-08-01 00:00:00')
->selectRaw(
'COALESCE(SUM(income_amount), 0) AS income, '
. 'COALESCE(SUM(expense_amount), 0) AS expense'
)
->first();
$this->assertSame(16486.66, $opening['balance']);
$this->assertSame('2026-06-30', $opening['date']);
$this->assertSame(0.0, round(
(float) $opening['balance']
+ (float) $july->income
- (float) $july->expense,
2,
));
}
public function test_huas_zero_opening_rolls_into_october_activity_without_a_prior_balance_error(): void
{
$this->insertOpening('天猫-华斯旗舰店', '0.00');
$this->insertBill('天猫-华斯旗舰店', '2026-10-02 10:00:00', '10615.30', '0.00');
$this->insertBill('天猫-华斯旗舰店', '2026-10-03 10:00:00', '0.00', '10000.00');
$this->insertBill('天猫-华斯旗舰店', '2026-10-04 10:00:00', '0.00', '500.00');
$this->insertBill('天猫-华斯旗舰店', '2026-10-05 10:00:00', '0.00', '15.30');
$septemberOpening = app(TaobaoWechatOpeningBalanceRepository::class)
->openingBalance('天猫-华斯旗舰店', '2026-09-01');
$octoberOpening = app(TaobaoWechatOpeningBalanceRepository::class)
->openingBalance('天猫-华斯旗舰店', '2026-10-01');
$october = DB::connection('business_mirror')->table('taobao_wechat_bill_rows')
->where('store_name', '天猫-华斯旗舰店')
->where('occurred_at', '>=', '2026-10-01 00:00:00')
->where('occurred_at', '<', '2026-11-01 00:00:00')
->selectRaw(
'COALESCE(SUM(income_amount), 0) AS income, '
. 'COALESCE(SUM(expense_amount), 0) AS expense'
)
->first();
$this->assertSame(0.0, $septemberOpening['balance']);
$this->assertSame('2026-08-31', $septemberOpening['date']);
$this->assertSame(0.0, $octoberOpening['balance']);
$this->assertSame('2026-09-30', $octoberOpening['date']);
$this->assertSame(100.0, round(
(float) $octoberOpening['balance']
+ (float) $october->income
- (float) $october->expense,
2,
));
}
public function test_missing_opening_balance_is_unavailable(): void
{
$result = app(TaobaoWechatOpeningBalanceRepository::class)
->openingBalance('未录入店铺', '2026-07-01');
$this->assertNull($result['balance']);
$this->assertNull($result['date']);
$this->assertSame('unavailable', $result['source']);
$this->assertSame(0, $result['store_count']);
}
private function createTables(): void
{
$schema = Schema::connection('business_mirror');
$schema->create(TaobaoWechatOpeningBalanceRepository::TABLE, function (Blueprint $table): void {
$table->id();
$table->date('balance_date');
$table->string('store_name', 191);
$table->decimal('balance', 20, 2);
$table->string('source', 191);
$table->text('source_note')->nullable();
$table->timestamp('imported_at');
$table->timestamps();
$table->unique(['balance_date', 'store_name']);
});
$schema->create('taobao_wechat_bill_rows', function (Blueprint $table): void {
$table->string('record_code', 191)->primary();
$table->string('store_name')->nullable();
$table->dateTime('occurred_at');
$table->decimal('income_amount', 20, 2);
$table->decimal('expense_amount', 20, 2);
});
}
private function insertOpening(string $storeName, string $balance): void
{
DB::connection('business_mirror')->table(TaobaoWechatOpeningBalanceRepository::TABLE)->insert([
'balance_date' => '2026-06-30',
'store_name' => $storeName,
'balance' => $balance,
'source' => TaobaoWechatBackofficeJuneEndingBalances::SOURCE,
'source_note' => TaobaoWechatBackofficeJuneEndingBalances::SOURCE_NOTE,
'imported_at' => '2026-11-09 10:00:00',
]);
}
private function insertBill(
string $storeName,
string $occurredAt,
string $income,
string $expense,
): void {
static $sequence = 0;
$sequence++;
DB::connection('business_mirror')->table('taobao_wechat_bill_rows')->insert([
'record_code' => 'TEST-' . $sequence,
'store_name' => $storeName,
'occurred_at' => $occurredAt,
'income_amount' => $income,
'expense_amount' => $expense,
]);
}
}