fix: 费用明细按发生时间匹配主体

This commit is contained in:
yueyang 2026-12-03 15:41:47 +08:00
parent 00811970fc
commit ea3d428262
3 changed files with 87 additions and 16 deletions

View File

@ -298,12 +298,14 @@ final class PerformanceDetailSubjectMatcher
private function rowDate(object $row): string
{
$date = trim((string) ($row->occurred_date ?? ''));
if ($date !== '') {
return substr($date, 0, 10);
foreach (['occurred_date', 'occurred_at', 'report_month'] as $property) {
$date = trim((string) ($row->{$property} ?? ''));
if ($date !== '') {
return substr($date, 0, 10);
}
}
return substr(trim((string) ($row->report_month ?? '')), 0, 10);
return '';
}
private function normalize(mixed $value): string

View File

@ -113,7 +113,7 @@ class BillingDetailsPageData
'remark' => ['备注', 'text'],
];
/** 费用明细精简列:平台、店铺、当前主体资料、线上单号、账单费用、收支净额。 */
/** 费用明细精简列:平台、店铺、按发生时间命中的主体资料、线上单号、账单费用、收支净额。 */
public const EXPENSE_COLUMNS = [
'platform' => ['平台', 'text'],
'store_name' => ['店铺', 'text'],
@ -248,7 +248,7 @@ class BillingDetailsPageData
if (isset($want['custom_remark'])) { $add('custom_remark', $has('custom_remark') ? 'custom_remark' : null); }
if (isset($want['remark'])) { $add('remark', $has('remark') ? 'remark' : null); }
if (isset($want['type_text'])) { $add('type_text', $pick(is_string($meta['type']) ? (string) $meta['type'] : null)); }
if (isset($want['occurred_at'])) { $add('occurred_at', (string) $meta['date']); }
if (isset($want['occurred_at']) || $subject) { $add('occurred_at', (string) $meta['date']); }
if (isset($want['income']) || isset($want['amount'])) { $add('income', (string) $meta['income']); }
if (isset($want['expense']) || isset($want['amount'])) { $add('expense', (string) $meta['expense']); }
@ -1476,7 +1476,7 @@ class BillingDetailsPageData
$this->subjectMatcher ??= app(PerformanceDetailSubjectMatcher::class);
return $this->subjectMatcher->applyArrayCurrent($row, $subjectMap);
return $this->subjectMatcher->applyArray($row, $subjectMap);
}
/** @param list<string> $storeNames */

View File

@ -31,7 +31,7 @@ class BillingDetailsExpenseFilterTest extends TestCase
$this->assertSame('账单费用', BillingDetailsPageData::EXPENSE_COLUMNS['finance_remark'][0]);
}
public function test_expense_rows_include_current_company_and_taxpayer_type(): void
public function test_expense_rows_use_the_company_period_matching_occurred_at(): void
{
$data = app(BillingDetailsPageData::class);
$apply = new ReflectionMethod(BillingDetailsPageData::class, 'applySubjectRow');
@ -40,16 +40,29 @@ class BillingDetailsExpenseFilterTest extends TestCase
$row = $apply->invoke($data, [
'store_name' => '天猫-亿家达旗舰店',
'online_order_no' => '512789121088013442',
'occurred_at' => '2026-09-15 12:00:00',
], [
'periods' => [
'天猫-亿家达旗舰店' => [[
'company_name' => '常州市测试家居有限公司',
'effective_at' => '2026-07-01',
'expires_at' => null,
'source' => 'initial',
]],
'天猫-亿家达旗舰店' => [
[
'company_name' => '旧测试家居有限公司',
'effective_at' => '2026-07-01',
'expires_at' => '2026-09-30',
'source' => 'initial',
],
[
'company_name' => '常州市测试家居有限公司',
'effective_at' => '2026-10-01',
'expires_at' => null,
'source' => 'initial',
],
],
],
'plans' => [
'旧测试家居有限公司' => [
'company_name' => '旧测试家居有限公司',
'taxpayer_type' => '小规模纳税人',
],
'常州市测试家居有限公司' => [
'company_name' => '常州市测试家居有限公司',
'taxpayer_type' => '一般纳税人',
@ -58,8 +71,64 @@ class BillingDetailsExpenseFilterTest extends TestCase
'company_plan_shops' => [],
]);
$this->assertSame('一般纳税人', $row['taxpayer_type']);
$this->assertSame('常州市测试家居有限公司', $row['company_name']);
$this->assertSame('小规模纳税人', $row['taxpayer_type']);
$this->assertSame('旧测试家居有限公司', $row['company_name']);
$newPeriod = $apply->invoke($data, [
'store_name' => '天猫-亿家达旗舰店',
'online_order_no' => '512789121088013443',
'occurred_at' => '2026-10-01 00:00:00',
], [
'periods' => [
'天猫-亿家达旗舰店' => [
[
'company_name' => '旧测试家居有限公司',
'effective_at' => '2026-07-01',
'expires_at' => '2026-09-30',
'source' => 'initial',
],
[
'company_name' => '常州市测试家居有限公司',
'effective_at' => '2026-10-01',
'expires_at' => null,
'source' => 'initial',
],
],
],
'plans' => [
'旧测试家居有限公司' => [
'company_name' => '旧测试家居有限公司',
'taxpayer_type' => '小规模纳税人',
],
'常州市测试家居有限公司' => [
'company_name' => '常州市测试家居有限公司',
'taxpayer_type' => '一般纳税人',
],
],
'company_plan_shops' => [],
]);
$this->assertSame('一般纳税人', $newPeriod['taxpayer_type']);
$this->assertSame('常州市测试家居有限公司', $newPeriod['company_name']);
}
public function test_expense_export_selects_occurred_at_for_period_matching(): void
{
$data = app(BillingDetailsPageData::class);
$select = new ReflectionMethod(BillingDetailsPageData::class, 'exportSelectList');
$select->setAccessible(true);
$sql = $select->invoke($data, [
'table' => 'taobao_alipay_bill_rows',
'date' => 'occurred_at',
'income' => 'income_amount',
'expense' => 'expense_amount',
'online_order' => 'online_order_no',
'merchant_order' => 'merchant_order_no',
'type' => null,
], BillingDetailsPageData::EXPENSE_COLUMNS);
$this->assertContains('occurred_at', explode(', ', $sql));
}
public function test_ledger_memo_totals_split_invoice_buckets(): void