174 lines
6.9 KiB
PHP
174 lines
6.9 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\PromotionExpenseMirrorRepository;
|
|
use Tests\TestCase;
|
|
|
|
class PromotionExpenseMirrorRepositoryTest extends TestCase
|
|
{
|
|
private PromotionExpenseMirrorRepository $repository;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->createMirrorTables();
|
|
$this->repository = new PromotionExpenseMirrorRepository();
|
|
}
|
|
|
|
public function test_it_builds_monthly_and_store_summaries_from_the_daily_mirror(): void
|
|
{
|
|
DB::connection('business_mirror')->table(PromotionExpenseMirrorRepository::TABLE)->insert([
|
|
[
|
|
'report_date' => '2026-10-01',
|
|
'platform' => '淘系',
|
|
'store_name' => '天猫-甲店',
|
|
'source_rows' => 10,
|
|
'push_fee' => 20,
|
|
'shared_push_fee' => 30,
|
|
'push_fee_total' => 50,
|
|
'marketing_fee' => 5,
|
|
'shared_marketing_fee' => 10,
|
|
'marketing_fee_total' => 15,
|
|
'total_fee' => 65,
|
|
'synced_at' => '2026-11-27 03:10:00',
|
|
],
|
|
[
|
|
'report_date' => '2026-10-02',
|
|
'platform' => '拼多多',
|
|
'store_name' => '拼多多-乙店',
|
|
'source_rows' => 20,
|
|
'push_fee' => 40,
|
|
'shared_push_fee' => 10,
|
|
'push_fee_total' => 50,
|
|
'marketing_fee' => 5,
|
|
'shared_marketing_fee' => 0,
|
|
'marketing_fee_total' => 5,
|
|
'total_fee' => 55,
|
|
'synced_at' => '2026-11-27 03:10:00',
|
|
],
|
|
[
|
|
'report_date' => '2026-11-25',
|
|
'platform' => '抖音',
|
|
'store_name' => '抖音-丙店',
|
|
'source_rows' => 30,
|
|
'push_fee' => 60,
|
|
'shared_push_fee' => 10,
|
|
'push_fee_total' => 70,
|
|
'marketing_fee' => 8,
|
|
'shared_marketing_fee' => 2,
|
|
'marketing_fee_total' => 10,
|
|
'total_fee' => 80,
|
|
'synced_at' => '2026-11-27 03:10:00',
|
|
],
|
|
]);
|
|
|
|
$this->assertSame(
|
|
[['period' => '2026-10', 'source_rows' => 30, 'push_fee_total' => 100.0, 'marketing_fee_total' => 20.0, 'total_fee' => 120.0]],
|
|
$this->repository->periods('2026-10', '2026-10'),
|
|
);
|
|
|
|
$details = $this->repository->details('2026-10', '', '', 1, 1);
|
|
$this->assertSame(2, $details['total']);
|
|
$this->assertCount(1, $details['rows']);
|
|
$this->assertSame('天猫-甲店', $details['rows'][0]['store_name']);
|
|
$this->assertSame(120.0, $details['totals']['total_fee']);
|
|
|
|
$filtered = $this->repository->details('2026-10', '拼多多', '拼多多-乙店', 1, 50);
|
|
$this->assertSame(1, $filtered['total']);
|
|
$this->assertSame(55.0, $filtered['rows'][0]['total_fee']);
|
|
}
|
|
|
|
public function test_replacing_one_date_removes_stale_store_rows_for_that_date_only(): void
|
|
{
|
|
DB::connection('business_mirror')->table(PromotionExpenseMirrorRepository::TABLE)->insert([
|
|
[
|
|
'report_date' => '2026-10-01',
|
|
'platform' => '淘系',
|
|
'store_name' => '旧店',
|
|
'source_rows' => 1,
|
|
'push_fee_total' => 1,
|
|
'marketing_fee_total' => 0,
|
|
'total_fee' => 1,
|
|
],
|
|
[
|
|
'report_date' => '2026-10-02',
|
|
'platform' => '淘系',
|
|
'store_name' => '保留店',
|
|
'source_rows' => 1,
|
|
'push_fee_total' => 1,
|
|
'marketing_fee_total' => 0,
|
|
'total_fee' => 1,
|
|
],
|
|
]);
|
|
|
|
$stored = $this->repository->replaceDate('2026-10-01', [[
|
|
'platform' => '淘系',
|
|
'store_name' => '新店',
|
|
'source_rows' => 2,
|
|
'push_fee' => 10,
|
|
'shared_push_fee' => 5,
|
|
'push_fee_total' => 15,
|
|
'marketing_fee' => 3,
|
|
'shared_marketing_fee' => 2,
|
|
'marketing_fee_total' => 5,
|
|
]]);
|
|
|
|
$this->assertSame(1, $stored);
|
|
$this->assertDatabaseMissing(PromotionExpenseMirrorRepository::TABLE, [
|
|
'report_date' => '2026-10-01',
|
|
'store_name' => '旧店',
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas(PromotionExpenseMirrorRepository::TABLE, [
|
|
'report_date' => '2026-10-01',
|
|
'store_name' => '新店',
|
|
'total_fee' => 20,
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas(PromotionExpenseMirrorRepository::TABLE, [
|
|
'report_date' => '2026-10-02',
|
|
'store_name' => '保留店',
|
|
], 'business_mirror');
|
|
}
|
|
|
|
private function createMirrorTables(): void
|
|
{
|
|
$schema = Schema::connection('business_mirror');
|
|
if (! $schema->hasTable(PromotionExpenseMirrorRepository::TABLE)) {
|
|
$schema->create(PromotionExpenseMirrorRepository::TABLE, function (Blueprint $table): void {
|
|
$table->bigIncrements('id');
|
|
$table->date('report_date');
|
|
$table->string('platform', 64);
|
|
$table->string('store_name', 255);
|
|
$table->unsignedBigInteger('source_rows')->default(0);
|
|
$table->decimal('push_fee', 20, 2)->default(0);
|
|
$table->decimal('shared_push_fee', 20, 2)->default(0);
|
|
$table->decimal('push_fee_total', 20, 2)->default(0);
|
|
$table->decimal('marketing_fee', 20, 2)->default(0);
|
|
$table->decimal('shared_marketing_fee', 20, 2)->default(0);
|
|
$table->decimal('marketing_fee_total', 20, 2)->default(0);
|
|
$table->decimal('total_fee', 20, 2)->default(0);
|
|
$table->timestamp('synced_at')->nullable();
|
|
});
|
|
}
|
|
if (! $schema->hasTable(PromotionExpenseMirrorRepository::STATE_TABLE)) {
|
|
$schema->create(PromotionExpenseMirrorRepository::STATE_TABLE, function (Blueprint $table): void {
|
|
$table->string('dataset_key', 191)->primary();
|
|
$table->string('source_key', 64);
|
|
$table->string('source_object', 191);
|
|
$table->string('target_table', 64);
|
|
$table->string('status', 24);
|
|
$table->dateTime('initial_completed_at')->nullable();
|
|
$table->dateTime('last_attempted_at')->nullable();
|
|
$table->dateTime('last_succeeded_at')->nullable();
|
|
$table->unsignedBigInteger('mirror_row_count')->default(0);
|
|
$table->string('last_error_code', 120)->nullable();
|
|
$table->timestamps();
|
|
});
|
|
}
|
|
}
|
|
}
|