283 lines
13 KiB
PHP
283 lines
13 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Business;
|
||
|
||
use App\Models\Auth\Permission;
|
||
use Illuminate\Database\Schema\Blueprint;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Modules\EcommerceFinance\Services\AccountLedgerRemarkMap;
|
||
use Modules\EcommerceFinance\Models\AccountBill;
|
||
use Modules\EcommerceFinance\Repositories\VipshopJxBankPaymentRepository;
|
||
use Modules\EcommerceFinance\Repositories\VipshopJxOpeningBalanceRepository;
|
||
use Tests\Feature\FeatureTestCase;
|
||
|
||
/**
|
||
* 店铺收款账户账单 › 唯品会JX账户(bill_type = vipshop-jx,读取 ec_account_bills)。
|
||
*/
|
||
class VipshopJxReceivingBillsTest extends FeatureTestCase
|
||
{
|
||
public function test_vipshop_jx_tab_reads_only_its_own_bill_type(): void
|
||
{
|
||
$this->allowPage();
|
||
$this->bill('vipshop-jx', '26060921475309', '销售', '2026-06-09 00:00:00', 82.48, 0);
|
||
$this->bill('vipshop-jx', '26070393670736', '客退', '2026-07-03 00:00:00', 0, 68.60);
|
||
$this->bill('vipshop', '26060100000001', '订单退款', '2026-06-01 00:00:00', 0, 999.00);
|
||
|
||
$response = $this->loginAs()
|
||
->getJson(route('ecommerce-finance.account-bills.index', [
|
||
'bill_type' => 'vipshop-jx',
|
||
'limit' => 20,
|
||
]))
|
||
->assertOk()
|
||
->assertJsonPath('columns.title', '唯品会JX账户')
|
||
->assertJsonPath('total', 2)
|
||
->assertJsonPath('summary.count', 2)
|
||
->assertJsonPath('summary.income', 82.48)
|
||
->assertJsonPath('summary.expense', 68.60)
|
||
// 未选择账期时没有期初基准,汇总仍等于本月收支净额。
|
||
->assertJsonPath('summary.balance', 13.88)
|
||
// 日期按 Y-m-d H:i:s 返回,前端直接展示,不再出现 ISO8601(UTC)。
|
||
->assertJsonPath('data.0.occurred_at', '2026-07-03 00:00:00')
|
||
->assertJsonPath('data.1.occurred_at', '2026-06-09 00:00:00')
|
||
// 订单结算时间来自账单结算期间(现系统内全部为 2026-07)
|
||
->assertJsonPath('data.0.payload.settle_period', '2026-07')
|
||
->assertJsonPath('data.1.payload.settle_period', '2026-07');
|
||
|
||
$this->assertSame(
|
||
['记录编码', '店铺', '明细类型', '账单项目', '总账备注', '订单号/SO', '账单期间', '订单日期(按订单号)', '收入金额', '支出金额', '合计金额'],
|
||
array_column($response->json('columns.columns'), 'label'),
|
||
);
|
||
}
|
||
|
||
public function test_vipshop_jx_summary_uses_the_rolled_opening_balance_for_the_selected_month(): void
|
||
{
|
||
$this->allowPage();
|
||
$this->prepareOpeningBalanceTable();
|
||
DB::connection('business_mirror')->table(VipshopJxOpeningBalanceRepository::TABLE)->insert([
|
||
'period_end' => '2026-06-30',
|
||
'balance' => '491948.53',
|
||
'source' => '唯品会结算平台历史账单累计',
|
||
'imported_at' => now(),
|
||
'created_at' => now(),
|
||
'updated_at' => now(),
|
||
]);
|
||
DB::connection('business_mirror')->table(VipshopJxBankPaymentRepository::TABLE)->insert([
|
||
[
|
||
'source_payment_id' => 1,
|
||
'source_bill_number' => 'BILL-2026-06',
|
||
'bill_period' => '2026-06',
|
||
'pay_date' => '2026-07-01',
|
||
'batch_number' => 'BATCH-1',
|
||
'vendor_account_num' => '1105055609100026936',
|
||
'amount' => '491948.53',
|
||
'currency_code' => 'CNY',
|
||
'pay_status' => 'SUCCESS',
|
||
'source_hash' => str_repeat('a', 64),
|
||
'imported_at' => now(),
|
||
'created_at' => now(),
|
||
'updated_at' => now(),
|
||
],
|
||
[
|
||
'source_payment_id' => 2,
|
||
'source_bill_number' => 'BILL-2026-07',
|
||
'bill_period' => '2026-07',
|
||
'pay_date' => '2026-10-10',
|
||
'batch_number' => 'BATCH-2',
|
||
'vendor_account_num' => '1105055609100026936',
|
||
'amount' => '100',
|
||
'currency_code' => 'CNY',
|
||
'pay_status' => 'SUCCESS',
|
||
'source_hash' => str_repeat('a', 64),
|
||
'imported_at' => now(),
|
||
'created_at' => now(),
|
||
'updated_at' => now(),
|
||
],
|
||
[
|
||
'source_payment_id' => 3,
|
||
'source_bill_number' => 'BILL-2026-08',
|
||
'bill_period' => '2026-08',
|
||
'pay_date' => '2026-10-10',
|
||
'batch_number' => 'BATCH-3',
|
||
'vendor_account_num' => '1105055609100026936',
|
||
'amount' => '30',
|
||
'currency_code' => 'CNY',
|
||
'pay_status' => 'SUCCESS',
|
||
'source_hash' => str_repeat('a', 64),
|
||
'imported_at' => now(),
|
||
'created_at' => now(),
|
||
'updated_at' => now(),
|
||
],
|
||
]);
|
||
$this->bill('vipshop-jx', 'JUL-ROLL', '销售明细', '2026-07-09 00:00:00', 300, 50, '订单', '2026-07');
|
||
$this->bill('vipshop-jx', 'AUG-VIEW', '销售明细', '2026-08-09 00:00:00', 100, 20, '订单', '2026-08');
|
||
|
||
$this->loginAs()
|
||
->getJson(route('ecommerce-finance.account-bills.index', [
|
||
'bill_type' => 'vipshop-jx',
|
||
'start' => '2026-08-01',
|
||
'end' => '2026-08-31',
|
||
'limit' => 20,
|
||
]))
|
||
->assertOk()
|
||
->assertJsonPath('summary.income', 100)
|
||
->assertJsonPath('summary.expense', 20)
|
||
->assertJsonPath('summary.opening_balance', 150)
|
||
->assertJsonPath('summary.opening_balance_date', '2026-07-31')
|
||
->assertJsonPath('summary.opening_balance_source', 'previous_period')
|
||
->assertJsonPath('summary.withdrawal_amount', 30)
|
||
->assertJsonPath('summary.withdrawal_count', 1)
|
||
->assertJsonPath('summary.balance', 200);
|
||
}
|
||
|
||
public function test_vipshop_jx_filter_options_and_export_stay_inside_the_tab(): void
|
||
{
|
||
$this->allowPage();
|
||
$this->bill('vipshop-jx', '26060921475309', '销售', '2026-06-09 00:00:00', 82.48, 0, '处罚款');
|
||
$this->bill('vipshop-jx', '26070393670736', '客退', '2026-07-03 00:00:00', 0, 68.60, '客服特殊退款-仅退款');
|
||
$this->bill('vipshop', '26060100000001', '订单退款', '2026-06-01 00:00:00', 0, 999.00);
|
||
|
||
$options = $this->loginAs()
|
||
->getJson(route('ecommerce-finance.account-bills.filter-options', ['bill_type' => 'vipshop-jx']))
|
||
->assertOk()
|
||
->json();
|
||
$this->assertSame(['唯品会JITX_测试店'], $options['shops']);
|
||
// 唯品会JX 的「账单项目」筛选取行内业务备注(与页面「账单项目」列同源)
|
||
$billItems = $options['bill_items'];
|
||
sort($billItems);
|
||
$this->assertSame(['处罚款', '客服特殊退款-仅退款'], $billItems);
|
||
$this->assertSame([], $options['ledger_remarks']);
|
||
|
||
$export = $this->loginAs()->get(route('ecommerce-finance.account-bills.export', [
|
||
'bill_type' => 'vipshop-jx',
|
||
'bill_item' => '处罚款',
|
||
]));
|
||
|
||
$export->assertOk()->assertDownload('vipshop-jx_account_bills.csv');
|
||
$csv = $export->streamedContent();
|
||
$this->assertStringContainsString('26060921475309', $csv);
|
||
$this->assertStringContainsString('账单项目', $csv);
|
||
$this->assertStringContainsString('2026-07', $csv);
|
||
$this->assertStringNotContainsString('26070393670736', $csv);
|
||
$this->assertStringNotContainsString('999.00', $csv);
|
||
}
|
||
|
||
public function test_ledger_remark_filter_uses_mapped_bill_items_on_page_and_export(): void
|
||
{
|
||
$this->allowPage();
|
||
$this->bill('vipshop-jx', '26060921475309', '销售', '2026-06-09 00:00:00', 82.48, 0, '处罚款');
|
||
$this->bill('vipshop-jx', '26070393670736', '客退', '2026-07-03 00:00:00', 0, 68.60, '客服特殊退款-仅退款/补寄应收费用-配件');
|
||
$this->bill('vipshop', '26060100000001', '订单退款', '2026-06-01 00:00:00', 0, 999.00);
|
||
|
||
$now = now();
|
||
DB::table('ec_account_ledger_remarks')->insert([
|
||
['platform' => '唯品会jx', 'bill_item' => '处罚款', 'ledger_remark' => '营业外支出', 'created_at' => $now, 'updated_at' => $now],
|
||
['platform' => '唯品会jx', 'bill_item' => '客服特殊退款-仅退款/补寄应收费用-配件', 'ledger_remark' => '销售费用/售后补偿', 'created_at' => $now, 'updated_at' => $now],
|
||
]);
|
||
app(AccountLedgerRemarkMap::class)->forget();
|
||
|
||
$options = $this->loginAs()
|
||
->getJson(route('ecommerce-finance.account-bills.filter-options', ['bill_type' => 'vipshop-jx']))
|
||
->assertOk()
|
||
->json();
|
||
$this->assertSame(['唯品会JITX_测试店'], $options['shops']);
|
||
$this->assertSame(['营业外支出', '销售费用/售后补偿'], $options['ledger_remarks']);
|
||
|
||
$page = $this->loginAs()->getJson(route('ecommerce-finance.account-bills.index', [
|
||
'bill_type' => 'vipshop-jx',
|
||
'ledger_remark' => '营业外支出',
|
||
'limit' => 20,
|
||
]))->assertOk();
|
||
$this->assertSame(1, $page->json('total'));
|
||
$this->assertSame('销售', $page->json('data.0.bill_item'));
|
||
$this->assertSame(82.48, (float) $page->json('data.0.income'));
|
||
|
||
$export = $this->loginAs()->get(route('ecommerce-finance.account-bills.export', [
|
||
'bill_type' => 'vipshop-jx',
|
||
'ledger_remark' => '营业外支出',
|
||
]));
|
||
$export->assertOk()->assertDownload('vipshop-jx_account_bills.csv');
|
||
$csv = $export->streamedContent();
|
||
$this->assertStringContainsString('26060921475309', $csv);
|
||
$this->assertStringNotContainsString('26070393670736', $csv);
|
||
}
|
||
|
||
/**
|
||
* @param string $billItemRemark 页面「账单项目」列取值(payload.second_remark),JX 页签的账单项目筛选按它过滤
|
||
*/
|
||
private function bill(
|
||
string $billType,
|
||
string $orderNo,
|
||
string $item,
|
||
string $occurredAt,
|
||
float $income,
|
||
float $expense,
|
||
string $billItemRemark = '订单',
|
||
string $period = '2026-07',
|
||
): void
|
||
{
|
||
AccountBill::create([
|
||
'company_id' => $this->company->id,
|
||
'bill_type' => $billType,
|
||
'record_code' => $orderNo . '-' . $item,
|
||
'store_name' => $billType === 'vipshop-jx' ? '唯品会JITX_测试店' : '唯品会MP-测试店',
|
||
'order_no' => $orderNo,
|
||
'amount' => $income - $expense,
|
||
'bill_item' => $item,
|
||
'occurred_at' => $occurredAt,
|
||
'income' => $income,
|
||
'expense' => $expense,
|
||
'payload' => ['second_remark' => $billItemRemark, 'settle_period' => $period],
|
||
]);
|
||
}
|
||
|
||
private function prepareOpeningBalanceTable(): void
|
||
{
|
||
config([
|
||
'database.connections.business_mirror' => [
|
||
'driver' => 'sqlite',
|
||
'database' => ':memory:',
|
||
'prefix' => '',
|
||
],
|
||
'upstream_mirrors.connection' => 'business_mirror',
|
||
]);
|
||
DB::purge('business_mirror');
|
||
$schema = Schema::connection('business_mirror');
|
||
if (! $schema->hasTable(VipshopJxOpeningBalanceRepository::TABLE)) {
|
||
$schema->create(VipshopJxOpeningBalanceRepository::TABLE, function (Blueprint $table): void {
|
||
$table->id();
|
||
$table->date('period_end');
|
||
$table->decimal('balance', 20, 6);
|
||
$table->string('source');
|
||
$table->dateTime('imported_at', 6);
|
||
$table->timestamps(6);
|
||
$table->unique('period_end');
|
||
});
|
||
}
|
||
if (! $schema->hasTable(VipshopJxBankPaymentRepository::TABLE)) {
|
||
$schema->create(VipshopJxBankPaymentRepository::TABLE, function (Blueprint $table): void {
|
||
$table->id();
|
||
$table->unsignedBigInteger('source_payment_id')->unique();
|
||
$table->string('source_bill_number', 128);
|
||
$table->char('bill_period', 7);
|
||
$table->date('pay_date')->nullable();
|
||
$table->string('batch_number', 64)->nullable();
|
||
$table->string('vendor_account_num', 64);
|
||
$table->decimal('amount', 20, 6);
|
||
$table->string('currency_code', 8);
|
||
$table->string('pay_status', 24);
|
||
$table->string('source_hash', 64);
|
||
$table->dateTime('imported_at', 6);
|
||
$table->timestamps(6);
|
||
});
|
||
}
|
||
}
|
||
|
||
private function allowPage(): void
|
||
{
|
||
$this->user->attachPermission(
|
||
Permission::where('name', 'read-admin-panel')->firstOrFail()
|
||
);
|
||
}
|
||
}
|