215 lines
7.7 KiB
PHP
215 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Console;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\EcommerceFinance\Models\AccountBill;
|
|
use Modules\EcommerceFinance\Services\XiaohongshuSettlementBillImporter;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use RuntimeException;
|
|
use Tests\TestCase;
|
|
|
|
class XiaohongshuSettlementBillImporterTest extends TestCase
|
|
{
|
|
/** @var list<string> */
|
|
private array $files = [];
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
foreach ($this->files as $file) {
|
|
@unlink($file);
|
|
}
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_it_validates_and_replaces_only_the_xiaohongshu_period(): void
|
|
{
|
|
$companyId = (int) DB::table('companies')->value('id');
|
|
$this->existingBill($companyId, 'OLD-IN-PERIOD', 'xiaohongshu', '2026-10-15 00:00:00');
|
|
$this->existingBill($companyId, 'KEEP-OUTSIDE-PERIOD', 'xiaohongshu', '2026-09-30 23:59:59');
|
|
$this->existingBill($companyId, 'KEEP-OTHER-TYPE', 'vipshop-jx', '2026-10-15 00:00:00');
|
|
$path = $this->xlsx([
|
|
$this->row('REC-INCOME', '结算入账', '100.00', '0.00'),
|
|
$this->row('REC-EXPENSE', '退款', '0.00', '30.00'),
|
|
]);
|
|
|
|
$result = app(XiaohongshuSettlementBillImporter::class)->import(
|
|
$path,
|
|
$companyId,
|
|
'2026-10-01',
|
|
'2026-10-31',
|
|
2,
|
|
'100.00',
|
|
'30.00',
|
|
100,
|
|
);
|
|
|
|
$this->assertSame('succeeded', $result['status']);
|
|
$this->assertSame(2, $result['row_count']);
|
|
$this->assertSame('100.00', $result['income_total']);
|
|
$this->assertSame('30.00', $result['expense_total']);
|
|
$this->assertSame('70.00', $result['net_total']);
|
|
$this->assertSame(['结算入账' => 1, '退款' => 1], $result['transaction_type_counts']);
|
|
|
|
$this->assertDatabaseMissing('ec_account_bills', ['record_code' => 'OLD-IN-PERIOD']);
|
|
$this->assertDatabaseHas('ec_account_bills', ['record_code' => 'KEEP-OUTSIDE-PERIOD']);
|
|
$this->assertDatabaseHas('ec_account_bills', ['record_code' => 'KEEP-OTHER-TYPE']);
|
|
$this->assertDatabaseHas('ec_account_bills', [
|
|
'company_id' => $companyId,
|
|
'bill_type' => 'xiaohongshu',
|
|
'record_code' => 'REC-INCOME',
|
|
'store_name' => '小红书-索菲亚生活家具',
|
|
'bill_item' => '佣金',
|
|
'order_no' => 'P-REC-INCOME',
|
|
'income' => 100,
|
|
'expense' => 0,
|
|
'amount' => 100,
|
|
'modifier' => 'api',
|
|
'created_from' => 'jushuitan-xiaohongshu-import',
|
|
]);
|
|
|
|
$row = AccountBill::query()
|
|
->where('record_code', 'REC-INCOME')
|
|
->where('bill_type', 'xiaohongshu')
|
|
->firstOrFail();
|
|
$this->assertSame('P-REC-INCOME', $row->payload['online_order_no']);
|
|
$this->assertSame('结算入账', $row->payload['transaction_type']);
|
|
$this->assertSame('-', $row->payload['after_sale_no']);
|
|
$this->assertSame('2026-10-19 14:40:39', $row->payload['ordered_at']);
|
|
$this->assertSame('2026-10-31 06:00:44', $row->payload['source_created_at']);
|
|
$this->assertSame('2026-11-05 14:39:13', $row->payload['source_updated_at']);
|
|
|
|
$again = app(XiaohongshuSettlementBillImporter::class)->import(
|
|
$path,
|
|
$companyId,
|
|
'2026-10-01',
|
|
'2026-10-31',
|
|
2,
|
|
'100.00',
|
|
'30.00',
|
|
100,
|
|
);
|
|
$this->assertSame('succeeded', $again['status']);
|
|
$this->assertSame(
|
|
3,
|
|
DB::table('ec_account_bills')
|
|
->where('company_id', $companyId)
|
|
->whereIn('record_code', ['REC-INCOME', 'REC-EXPENSE', 'KEEP-OUTSIDE-PERIOD'])
|
|
->count(),
|
|
);
|
|
}
|
|
|
|
public function test_control_or_period_mismatch_does_not_replace_the_active_period(): void
|
|
{
|
|
$companyId = (int) DB::table('companies')->value('id');
|
|
$this->existingBill($companyId, 'ACTIVE-ROW', 'xiaohongshu', '2026-10-15 00:00:00');
|
|
$importer = app(XiaohongshuSettlementBillImporter::class);
|
|
|
|
try {
|
|
$importer->import(
|
|
$this->xlsx([$this->row('NEW-ROW', '结算入账', '10.00', '0.00')]),
|
|
$companyId,
|
|
'2026-10-01',
|
|
'2026-10-31',
|
|
2,
|
|
'10.00',
|
|
'0.00',
|
|
100,
|
|
);
|
|
$this->fail('控制数不一致时必须拒绝导入。');
|
|
} catch (RuntimeException $exception) {
|
|
$this->assertStringContainsString('权威控制数不一致', $exception->getMessage());
|
|
}
|
|
|
|
$this->assertDatabaseHas('ec_account_bills', ['record_code' => 'ACTIVE-ROW']);
|
|
$this->assertDatabaseMissing('ec_account_bills', ['record_code' => 'NEW-ROW']);
|
|
|
|
try {
|
|
$importer->import(
|
|
$this->xlsx([$this->row('OUTSIDE', '结算入账', '10.00', '0.00', '2026-11-01 00:00:00')]),
|
|
$companyId,
|
|
'2026-10-01',
|
|
'2026-10-31',
|
|
1,
|
|
'10.00',
|
|
'0.00',
|
|
100,
|
|
);
|
|
$this->fail('结算时间超出账期时必须拒绝导入。');
|
|
} catch (RuntimeException $exception) {
|
|
$this->assertStringContainsString('结算时间超出指定账期', $exception->getMessage());
|
|
}
|
|
|
|
$this->assertDatabaseHas('ec_account_bills', ['record_code' => 'ACTIVE-ROW']);
|
|
$this->assertDatabaseMissing('ec_account_bills', ['record_code' => 'OUTSIDE']);
|
|
}
|
|
|
|
private function existingBill(int $companyId, string $recordCode, string $billType, string $occurredAt): void
|
|
{
|
|
DB::table('ec_account_bills')->insert([
|
|
'company_id' => $companyId,
|
|
'bill_type' => $billType,
|
|
'record_code' => $recordCode,
|
|
'store_name' => '原有店铺',
|
|
'order_no' => 'OLD',
|
|
'bill_item' => '原项目',
|
|
'occurred_at' => $occurredAt,
|
|
'income' => 1,
|
|
'expense' => 0,
|
|
'balance' => 0,
|
|
'payload' => json_encode([], JSON_THROW_ON_ERROR),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
private function row(
|
|
string $recordCode,
|
|
string $transactionType,
|
|
string $income,
|
|
string $expense,
|
|
string $occurredAt = '2026-10-29 07:55:02',
|
|
): array {
|
|
return [
|
|
$recordCode,
|
|
'小红书-索菲亚生活家具',
|
|
'佣金',
|
|
'P-' . $recordCode,
|
|
$transactionType,
|
|
'P-' . $recordCode,
|
|
'-',
|
|
'2026-10-19 14:40:39',
|
|
$occurredAt,
|
|
$income,
|
|
$expense,
|
|
'佣金',
|
|
'2026-10-31 06:00:44',
|
|
'2026-11-05 14:39:13',
|
|
'api',
|
|
];
|
|
}
|
|
|
|
/** @param list<list<string>> $rows */
|
|
private function xlsx(array $rows): string
|
|
{
|
|
$temporary = tempnam(sys_get_temp_dir(), 'xiaohongshu-authoritative-');
|
|
$path = $temporary . '.xlsx';
|
|
rename($temporary, $path);
|
|
$spreadsheet = new Spreadsheet();
|
|
$spreadsheet->getActiveSheet()->fromArray(array_merge([[
|
|
'记录编码', '店铺', '账单项目', '线上单号', '交易类型', '订单号', '售后单号',
|
|
'下单时间', '结算时间', '收入金额', '支出金额', '账单备注', '创建时间', '修改时间', '修改人',
|
|
]], $rows));
|
|
(new Xlsx($spreadsheet))->save($path);
|
|
$spreadsheet->disconnectWorksheets();
|
|
$this->files[] = $path;
|
|
|
|
return $path;
|
|
}
|
|
}
|