From 9de660ae5542fe510cbf8a1889111a6f082c12be Mon Sep 17 00:00:00 2001 From: yueyang <2495544247@qq.com> Date: Fri, 20 Nov 2026 13:49:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B4=B9=E7=94=A8=E6=98=8E=E7=BB=86?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A8=8E=E5=8A=A1=E7=B1=BB=E5=9E=8B=E5=92=8C?= =?UTF-8?q?=E5=85=AC=E5=8F=B8=E4=B8=BB=E4=BD=93=20(#1311)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../View/BillingDetailsPageData.php | 92 ++++++++++++++++++- .../billing-details.blade.php | 6 +- .../ExpenseDetailsDrilldownPageTest.php | 4 + .../BillingDetailsExpenseFilterTest.php | 25 +++++ 4 files changed, 121 insertions(+), 6 deletions(-) diff --git a/modules/EcommerceFinance/View/BillingDetailsPageData.php b/modules/EcommerceFinance/View/BillingDetailsPageData.php index 640932fa..d0e23666 100644 --- a/modules/EcommerceFinance/View/BillingDetailsPageData.php +++ b/modules/EcommerceFinance/View/BillingDetailsPageData.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\DB; use Modules\EcommerceFinance\Services\AccountLedgerRemarkMap; use Modules\EcommerceFinance\Services\AccountMappingMatcher; use Modules\EcommerceFinance\Services\AccountMappingStore; +use Modules\EcommerceFinance\Services\PerformanceDetailSubjectMatcher; use Throwable; /** @@ -24,6 +25,8 @@ class BillingDetailsPageData { private const EXPENSE_FORCED_FILTERS = ['type' => ['费用']]; + private ?PerformanceDetailSubjectMatcher $subjectMatcher = null; + /** * 各平台账单表;periods 非空表示该表按 active_generation 区分版本,必须用 periods 表锁定当前版本。 */ @@ -87,7 +90,7 @@ class BillingDetailsPageData 'columns' => self::COLUMNS, 'pageTitle' => '费用明细', 'expenseOnly' => true, - ] + $this->data(null, self::EXPENSE_FORCED_FILTERS); + ] + $this->data(null, self::EXPENSE_FORCED_FILTERS, true); } /** @@ -110,10 +113,12 @@ class BillingDetailsPageData 'remark' => ['备注', 'text'], ]; - /** 费用明细精简列:平台、店铺、线上单号、账单费用、收支净额。 */ + /** 费用明细精简列:平台、店铺、当前主体资料、线上单号、账单费用、收支净额。 */ public const EXPENSE_COLUMNS = [ 'platform' => ['平台', 'text'], 'store_name' => ['店铺', 'text'], + 'taxpayer_type' => ['税务类型', 'text'], + 'company_name' => ['公司主体', 'text'], 'online_order_no' => ['线上单号', 'text'], 'finance_remark' => ['账单费用', 'text'], 'amount' => ['金额(收-支)', 'num'], @@ -183,6 +188,14 @@ class BillingDetailsPageData { $connection = $this->connection(); $rules = $this->rules(); + $withStoreSubjects = isset($columns['taxpayer_type']) || isset($columns['company_name']); + $subjectMap = $withStoreSubjects + ? $this->storeSubjectMap( + $filters['store'] !== [] + ? $filters['store'] + : ($this->options()['stores'] ?? []), + ) + : []; foreach (self::SOURCES as $label => $meta) { $branch = $this->branch($label, $meta, $filters); @@ -195,7 +208,10 @@ class BillingDetailsPageData . ' ORDER BY ' . $branch['date'] . ' DESC, record_code DESC', $scope['bind'] ) as $raw) { - yield $this->presentRow((array) $raw, $rules); + $row = $this->presentRow((array) $raw, $rules); + yield $withStoreSubjects + ? $this->applySubjectRow($row, $subjectMap) + : $row; } } } @@ -214,6 +230,7 @@ class BillingDetailsPageData $pick = static fn (?string $column): ?string => $column !== null && $column !== '' ? $column : null; $want = array_flip(array_keys($columns)); $finance = isset($want['finance_remark']) || isset($want['finance_type']); + $subject = isset($want['company_name']) || isset($want['taxpayer_type']); $select = []; $add = static function (string $alias, ?string $expr) use (&$select): void { @@ -224,7 +241,7 @@ class BillingDetailsPageData }; if (isset($want['record_code'])) { $add('record_code', 'record_code'); } - if (isset($want['store_name']) || $finance) { $add('store_name', 'store_name'); } + if (isset($want['store_name']) || $finance || $subject) { $add('store_name', 'store_name'); } if (isset($want['bill_item']) || $finance) { $add('bill_item', 'bill_item'); } if (isset($want['online_order_no'])) { $add('online_order_no', $pick(is_string($meta['online_order']) ? (string) $meta['online_order'] : null)); } if (isset($want['merchant_order_no'])) { $add('merchant_order_no', $pick(is_string($meta['merchant_order']) ? (string) $meta['merchant_order'] : null)); } @@ -282,7 +299,7 @@ class BillingDetailsPageData } /** @return array */ - public function data(?array $input = null, array $forcedFilters = []): array + public function data(?array $input = null, array $forcedFilters = [], bool $withStoreSubjects = false): array { $input ??= request()->all(); $filters = array_replace($this->parseFilters($input), $forcedFilters); @@ -348,6 +365,12 @@ class BillingDetailsPageData $rules = $this->rules(); $rows = array_map(fn ($row): array => $this->presentRow((array) $row, $rules), $rows); + if ($withStoreSubjects) { + $rows = $this->applySubjectColumns( + $rows, + $this->storeSubjectMap(array_column($rows, 'store_name')), + ); + } return [ 'filters' => $filters, @@ -1419,6 +1442,65 @@ class BillingDetailsPageData return $row; } + /** + * @param list> $rows + * @param array{ + * current_companies:array, + * taxpayer_types:array, + * company_plan_shops:array> + * } $subjectMap + * @return list> + */ + private function applySubjectColumns(array $rows, array $subjectMap): array + { + return array_map( + fn (array $row): array => $this->applySubjectRow($row, $subjectMap), + $rows, + ); + } + + /** + * @param array $row + * @param array{ + * current_companies:array, + * taxpayer_types:array, + * company_plan_shops:array> + * } $subjectMap + * @return array + */ + private function applySubjectRow(array $row, array $subjectMap): array + { + if (($subjectMap['current_companies'] ?? []) === []) { + return $row + ['taxpayer_type' => '', 'company_name' => '']; + } + + $this->subjectMatcher ??= app(PerformanceDetailSubjectMatcher::class); + + return $this->subjectMatcher->applyArray($row, $subjectMap); + } + + /** @param list $storeNames */ + private function storeSubjectMap(array $storeNames): array + { + $names = array_values(array_unique(array_filter(array_map( + static fn (mixed $name): string => trim((string) $name), + $storeNames, + )))); + if ($names === []) { + return ['current_companies' => [], 'taxpayer_types' => [], 'company_plan_shops' => []]; + } + sort($names, SORT_NATURAL | SORT_FLAG_CASE); + $companyId = (int) (company_id() ?: 1); + $cacheKey = 'billing-details-store-subject-map-v1:' + . $companyId . ':' . sha1(implode("\n", $names)); + + return cache()->remember( + $cacheKey, + now()->addMinutes(5), + static fn (): array => app(PerformanceDetailSubjectMatcher::class)->map($names, $companyId), + ); + } + private function normalizedItemSql(): string { return "(CASE WHEN bill_item LIKE 'wx%' THEN LTRIM(SUBSTRING(bill_item, 3)) ELSE bill_item END)"; diff --git a/resources/views/business/pages/financial-operations/billing-details.blade.php b/resources/views/business/pages/financial-operations/billing-details.blade.php index e6dfa58c..f3e2ec3e 100644 --- a/resources/views/business/pages/financial-operations/billing-details.blade.php +++ b/resources/views/business/pages/financial-operations/billing-details.blade.php @@ -169,6 +169,7 @@ 数据源=各平台已导入账单(与「店铺收款账户账单」同源);财务备注、财务类型由科目映射表按「平台+账单项目(wx 自动去前缀)」带出,匹配不到的留空并标红。 @if ($expenseOnly) 本页只展示科目映射表中财务类型=费用的账单;费用按账单费用分类汇总,金额单位为万。 + 税务类型和公司主体按店铺当前存续主体带出。 @else 同一交易单号跨月合并后已与订单实付对平的,差额备注优先显示为差额=跨月收款(合并已对平);未对平仍显示差额待核实。 @endif @@ -406,6 +407,7 @@ @if ($expenseOnly) 平台店铺 + 税务类型公司主体 线上单号账单费用 金额(收-支) @else @@ -423,6 +425,8 @@ @if ($expenseOnly) {{ $row['platform'] }} {{ $row['store_name'] }} + {{ $row['taxpayer_type'] ?? '' }} + {{ $row['company_name'] ?? '' }} {{ $row['online_order_no'] }} {!! $row['finance_remark'] !== '' ? e($row['finance_remark']) : '—' !!} {{ number_format((float) $row['income'] - (float) $row['expense'], 2) }} @@ -444,7 +448,7 @@ @endif @empty - 当前条件下没有数据。 + 当前条件下没有数据。 @endforelse diff --git a/tests/Feature/EcommerceFinance/ExpenseDetailsDrilldownPageTest.php b/tests/Feature/EcommerceFinance/ExpenseDetailsDrilldownPageTest.php index d31357c5..24f297b4 100644 --- a/tests/Feature/EcommerceFinance/ExpenseDetailsDrilldownPageTest.php +++ b/tests/Feature/EcommerceFinance/ExpenseDetailsDrilldownPageTest.php @@ -30,6 +30,8 @@ class ExpenseDetailsDrilldownPageTest extends FeatureTestCase $template = (string) file_get_contents(resource_path( 'views/business/pages/financial-operations/billing-details.blade.php', )); + $this->assertStringContainsString('data-col="taxpayer_type">税务类型', $template); + $this->assertStringContainsString('data-col="company_name">公司主体', $template); $this->assertSame(1, preg_match( '/\s*\s*\s*@if \(\$expenseOnly\)(.*?)@else/s', $template, @@ -39,6 +41,8 @@ class ExpenseDetailsDrilldownPageTest extends FeatureTestCase $this->assertSame([ 'platform', 'store_name', + 'taxpayer_type', + 'company_name', 'online_order_no', 'finance_remark', 'amount', diff --git a/tests/Unit/EcommerceFinance/BillingDetailsExpenseFilterTest.php b/tests/Unit/EcommerceFinance/BillingDetailsExpenseFilterTest.php index fbc5f253..524c6365 100644 --- a/tests/Unit/EcommerceFinance/BillingDetailsExpenseFilterTest.php +++ b/tests/Unit/EcommerceFinance/BillingDetailsExpenseFilterTest.php @@ -19,6 +19,8 @@ class BillingDetailsExpenseFilterTest extends TestCase $this->assertSame([ 'platform', 'store_name', + 'taxpayer_type', + 'company_name', 'online_order_no', 'finance_remark', 'amount', @@ -29,6 +31,29 @@ class BillingDetailsExpenseFilterTest extends TestCase $this->assertSame('账单费用', BillingDetailsPageData::EXPENSE_COLUMNS['finance_remark'][0]); } + public function test_expense_rows_include_current_company_and_taxpayer_type(): void + { + $data = app(BillingDetailsPageData::class); + $apply = new ReflectionMethod(BillingDetailsPageData::class, 'applySubjectRow'); + $apply->setAccessible(true); + + $row = $apply->invoke($data, [ + 'store_name' => '天猫-亿家达旗舰店', + 'online_order_no' => '512789121088013442', + ], [ + 'current_companies' => [ + '天猫-亿家达旗舰店' => '常州市测试家居有限公司', + ], + 'taxpayer_types' => [ + '常州市测试家居有限公司' => '一般纳税人', + ], + 'company_plan_shops' => [], + ]); + + $this->assertSame('一般纳税人', $row['taxpayer_type']); + $this->assertSame('常州市测试家居有限公司', $row['company_name']); + } + public function test_ledger_memo_totals_split_invoice_buckets(): void { $data = app(BillingDetailsPageData::class);