CaiWuXiTong/tests/Feature/Business/PerformanceDetailsMetricDetailsTest.php
yueyang eeaa386b04 feat: 增加业绩明细金额指标与账单明细汇总
- 业绩明细改为可点击金额指标卡
- 明细弹层支持账单名称汇总与平台/店铺明细双页签
- 支持平台、店铺、账单名称筛选与金额合计校验
- 新增账单项目明细镜像及同步一致性校验
2026-10-24 16:27:19 +08:00

153 lines
5.6 KiB
PHP

<?php
namespace Tests\Feature\Business;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\Feature\FeatureTestCase;
class PerformanceDetailsMetricDetailsTest extends FeatureTestCase
{
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->createMirrorTables();
}
public function test_offset_metric_details_group_by_store_and_bill_item_and_reconcile_to_total(): void
{
$mirror = DB::connection('business_mirror');
$mirror->table('store_receipt_order_match_rows')->insert([
$this->row('row-a1', '店铺A', -100),
$this->row('row-a2', '店铺A', -200),
$this->row('row-b1', '店铺B', -50),
]);
$mirror->table('store_receipt_order_match_metric_details')->insert([
$this->detail('detail-a1', 'row-a1', '账单扣款', -60),
$this->detail('detail-a2', 'row-a1', '售后扣减', -40),
$this->detail('detail-a3', 'row-a2', '账单扣款', -200),
$this->detail('detail-b1', 'row-b1', '账单扣款', -50),
]);
$response = $this->loginAs()->getJson(route(
'business.financial-operations.performance-details.metric-details',
[
'metric' => 'offset_amount',
'month' => '2026-09',
'store_name' => ['店铺A'],
],
));
$response
->assertOk()
->assertJsonPath('label', '冲减收入')
->assertJsonPath('total', -300)
->assertJsonPath('detail_total', -300)
->assertJsonPath('balance', 0)
->assertJsonPath('balanced', true);
$rows = $response->json('rows');
$this->assertCount(2, $rows);
$this->assertContains([
'platform' => '京东账单',
'store_name' => '店铺A',
'bill_item' => '账单扣款',
'amount' => -260,
'line_count' => 2,
], $rows);
$this->assertContains([
'platform' => '京东账单',
'store_name' => '店铺A',
'bill_item' => '售后扣减',
'amount' => -40,
'line_count' => 1,
], $rows);
}
private function row(string $rowKey, string $storeName, float $offset): array
{
return [
'report_month' => '2026-09-01',
'platform' => '京东账单',
'row_key' => $rowKey,
'occurred_date' => '2026-09-01',
'store_name' => $storeName,
'online_order_no' => 'ORDER-' . $rowKey,
'payment_count' => 1,
'income_amount' => 0,
'discount_amount' => 0,
'offset_amount' => $offset,
'default_compensation_amount' => 0,
'refund_amount' => 0,
'planting_amount' => null,
'paid_amount' => 0,
'difference' => 0,
'match_status' => '已匹配',
'remark' => null,
'imported_at' => now(),
];
}
private function detail(string $detailKey, string $rowKey, string $billItem, float $amount): array
{
return [
'report_month' => '2026-09-01',
'detail_key' => $detailKey,
'row_key' => $rowKey,
'platform' => '京东账单',
'metric' => 'offset',
'bill_item' => $billItem,
'amount' => $amount,
'line_count' => 1,
'imported_at' => now(),
];
}
private function createMirrorTables(): void
{
$schema = Schema::connection('business_mirror');
$schema->create('store_receipt_order_match_rows', function (Blueprint $table): void {
$table->date('report_month');
$table->string('platform');
$table->string('row_key');
$table->date('occurred_date')->nullable();
$table->string('store_name')->nullable();
$table->string('online_order_no')->nullable();
$table->unsignedInteger('payment_count')->default(0);
$table->decimal('income_amount', 20, 4)->default(0);
$table->decimal('discount_amount', 20, 4)->nullable();
$table->decimal('offset_amount', 20, 4)->nullable();
$table->decimal('default_compensation_amount', 20, 4)->nullable();
$table->decimal('refund_amount', 20, 4)->nullable();
$table->decimal('planting_amount', 20, 4)->nullable();
$table->decimal('paid_amount', 20, 4)->nullable();
$table->decimal('difference', 20, 4)->nullable();
$table->string('match_status', 32)->nullable();
$table->string('remark', 191)->nullable();
$table->dateTime('imported_at');
});
$schema->create('store_receipt_order_match_metric_details', function (Blueprint $table): void {
$table->date('report_month');
$table->string('detail_key');
$table->string('row_key');
$table->string('platform');
$table->string('metric');
$table->string('bill_item');
$table->decimal('amount', 20, 4)->default(0);
$table->unsignedInteger('line_count')->default(1);
$table->dateTime('imported_at');
});
}
}