CaiWuXiTong/tests/Feature/Business/KukaStorePerformancePageTest.php
chenxi 814f9a4520 feat: 汇总灵珊店铺月度收入 (#1060)
feat: 汇总灵珊店铺月度收入 (#1060)
2026-11-10 19:19:34 +08:00

136 lines
5.2 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Business\KukaPostingPerformance\KukaPostingPerformancePageData;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\Feature\FeatureTestCase;
class KukaStorePerformancePageTest 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');
Schema::connection('business_mirror')->create(
'store_receipt_order_match_rows',
function (Blueprint $table): void {
$table->date('report_month');
$table->string('row_key');
$table->string('platform');
$table->string('store_name');
$table->decimal('income_amount', 20, 4)->default(0);
$table->decimal('discount_amount', 20, 4)->default(0);
$table->decimal('offset_amount', 20, 4)->default(0);
$table->decimal('refund_amount', 20, 4)->default(0);
$table->decimal('default_compensation_amount', 20, 4)->default(0);
$table->dateTime('imported_at')->nullable();
$table->primary(['report_month', 'row_key']);
},
);
cache()->flush();
}
public function test_monthly_income_uses_the_five_component_formula_and_only_requested_stores(): void
{
$stores = array_values(config('kuka_posting.stores'));
$firstStore = $stores[0];
$secondStore = $stores[1];
DB::connection('business_mirror')->table('store_receipt_order_match_rows')->insert([
$this->row('oct-first', '2026-10-01', $firstStore, 100, 10, -5, -20, -1),
$this->row('oct-second', '2026-10-01', $secondStore, 50, 0, 0, -10, 2),
$this->row('sep-first', '2026-09-01', $firstStore, 1000, 0, 0, 0, 0),
$this->row('oct-outside', '2026-10-01', '其他店铺', 999999, 0, 0, 0, 0),
$this->row('oct-wallet', '2026-10-01', $firstStore, 888888, 0, 0, 0, 0, '京东网银钱包'),
]);
$data = app(KukaPostingPerformancePageData::class)->build(
Request::create('/posting-data/kuka-basic-data', 'GET', ['month' => '2026-10']),
);
$this->assertSame('2026-10', $data['month']);
$this->assertCount(12, $data['stores']);
$this->assertSame(2, $data['summary']['row_count']);
$this->assertSame(2, $data['summary']['stores_with_data']);
$this->assertSame(150.0, $data['summary']['income_amount']);
$this->assertSame(10.0, $data['summary']['discount_amount']);
$this->assertSame(-5.0, $data['summary']['offset_amount']);
$this->assertSame(-30.0, $data['summary']['refund_amount']);
$this->assertSame(1.0, $data['summary']['default_compensation_amount']);
$this->assertSame(126.0, $data['summary']['total_income']);
$this->assertSame([
'month' => '2026-10',
'label' => '2026年10月',
'total_income' => 126.0,
'row_count' => 2,
], $data['monthly_totals'][0]);
$this->assertSame([
'month' => '2026-09',
'label' => '2026年9月',
'total_income' => 1000.0,
'row_count' => 1,
], $data['monthly_totals'][1]);
$first = collect($data['stores'])->firstWhere('store_name', $firstStore);
$second = collect($data['stores'])->firstWhere('store_name', $secondStore);
$this->assertSame(84.0, $first['total_income']);
$this->assertSame(42.0, $second['total_income']);
$this->loginAs()
->get(route('business.pages.show', [
'section' => 'posting-data',
'page' => 'kuka-basic-data',
'submenu' => 'performance',
'month' => '2026-10',
]))
->assertOk()
->assertSeeText('灵珊店铺业绩 · 月度收入')
->assertSeeText('2026年10月合计收入')
->assertSeeText('¥126.00')
->assertSeeText($firstStore)
->assertSeeText($secondStore)
->assertSeeText('退款金额')
->assertSeeText('违约赔付(冲减收入)')
->assertDontSeeText('其他店铺');
}
private function row(
string $key,
string $month,
string $store,
float $income,
float $discount,
float $offset,
float $refund,
float $defaultCompensation,
string $platform = '京东账单',
): array {
return [
'report_month' => $month,
'row_key' => $key,
'platform' => $platform,
'store_name' => $store,
'income_amount' => $income,
'discount_amount' => $discount,
'offset_amount' => $offset,
'refund_amount' => $refund,
'default_compensation_amount' => $defaultCompensation,
'imported_at' => '2026-10-31 23:00:00',
];
}
}