From f56bd9d3ea41c039e73e5b28ff1ee96b39144e1a Mon Sep 17 00:00:00 2001 From: yueyang <2495544247@qq.com> Date: Fri, 9 Oct 2026 10:30:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=A6=E5=8D=95=E6=98=8E=E7=BB=86=EF=BC=9A?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=B4=A2=E5=8A=A1=E5=A4=87=E6=B3=A8=E7=AD=9B?= =?UTF-8?q?=E9=80=89=E3=80=81=E7=AD=9B=E9=80=89=E8=81=94=E5=8A=A8=EF=BC=88?= =?UTF-8?q?=E8=A1=8C=E6=95=B0=E4=B8=BA0=E7=9A=84=E5=8F=96=E5=80=BC?= =?UTF-8?q?=E4=B8=8D=E5=B1=95=E7=A4=BA=EF=BC=89=E3=80=81=E8=A1=8C=E8=B7=9D?= =?UTF-8?q?=E6=94=BE=E5=AE=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Business/Pages.php | 13 + .../View/BillingDetailsPageData.php | 381 ++++++++++++++++++ .../billing-details.blade.php | 75 +++- routes/admin.php | 4 + 4 files changed, 470 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 1ce24f58f..65785ccad 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -394,6 +394,19 @@ class Pages extends Controller ]); } + /** 账单明细筛选联动:按当前其它条件返回各筛选项可选值与行数 */ + public function billingDetailsFilterOptions(): \Illuminate\Http\JsonResponse + { + $section = collect(SidebarPermissions::navigationFor(user()))->firstWhere('slug', 'financial-operations'); + abort_unless($section && collect($section['pages'] ?? [])->contains('slug', 'billing-details'), 403, '当前账号没有该页面权限。'); + + $data = app(\Modules\EcommerceFinance\View\BillingDetailsPageData::class); + + return response()->json(['facets' => $data->facets(request()->all())], 200, [ + 'Cache-Control' => 'private, no-store', + ]); + } + /** 账单明细导出进度(前端每秒轮询一次) */ public function billingDetailsExportProgress(Request $request): \Illuminate\Http\JsonResponse { diff --git a/modules/EcommerceFinance/View/BillingDetailsPageData.php b/modules/EcommerceFinance/View/BillingDetailsPageData.php index 7e2874329..c23d54960 100644 --- a/modules/EcommerceFinance/View/BillingDetailsPageData.php +++ b/modules/EcommerceFinance/View/BillingDetailsPageData.php @@ -321,11 +321,354 @@ class BillingDetailsPageData 'summary' => ['rows' => $total, 'income' => round($income, 2), 'expense' => round($expense, 2), 'net' => round($income - $expense, 2)], 'platforms' => array_keys(self::PLATFORM_FILTERS), 'types' => $this->types(), + 'remarks' => $this->remarks(), 'error' => $error, 'status' => $status, ]; } + /** + * 筛选联动:按「除该筛选项以外的其它条件」统计各筛选项的可选值与行数,行数为 0 的取值不返回。 + * 结果按筛选条件签名缓存 60 秒,避免连续勾选时反复扫同一批数据。 + * + * @return array,counts:array}> + */ + public function facets(?array $input = null): array + { + $input ??= request()->all(); + $filters = $this->parseFilters($input); + $signature = md5((string) json_encode($filters, JSON_UNESCAPED_UNICODE)); + + try { + return cache()->remember( + 'billing-details-facets-v1:' . $signature, + now()->addSeconds(60), + fn (): array => $this->computeFacets($filters), + ); + } catch (Throwable $exception) { + report($exception); + + return $this->emptyFacets(); + } + } + + /** + * @param array $filters + * @return array,counts:array}> + */ + private function computeFacets(array $filters): array + { + $conn = $this->connection(); + $rules = $this->rules(); + + $facets = $this->emptyFacets(); + $facets['platform'] = $this->facetPlatform($conn, $this->withoutFilter($filters, 'platform')); + $facets['store'] = $this->shape( + $this->facetCounts($conn, $this->withoutFilter($filters, 'store'), 'store_name'), + $this->options()['stores'] ?? [], + ); + // 账单项目按原始 bill_item 分组(命中 bill_item 索引),wx 前缀在 PHP 里归一,避免逐行函数计算拖慢扫描。 + $itemByBranch = $this->facetCountsByBranch($conn, $this->withoutFilter($filters, 'item'), 'bill_item'); + $itemCounts = []; + foreach ($itemByBranch as $counts) { + foreach ($this->normalizeItemCounts($counts) as $item => $count) { + $itemCounts[$item] = ($itemCounts[$item] ?? 0) + $count; + } + } + $facets['item'] = $this->shape($itemCounts, $this->options()['items'] ?? []); + + // 财务类型 / 财务备注:按分支折算(分支内平台集合已知),避免跨平台把同一账单项目的行数重复计入。 + $itemIndex = $this->ruleIndexByItem($rules); + $contextPlatforms = $this->rulePlatforms($filters['platform']); + $typeCounts = []; + $remarkCounts = []; + foreach ($itemByBranch as $label => $counts) { + $platforms = self::BRANCH_PLATFORMS[$label] ?? []; + if ($contextPlatforms !== []) { + $platforms = array_values(array_intersect($platforms, $contextPlatforms)); + } + $normalized = $this->normalizeItemCounts($counts); + foreach ($this->foldItemCounts($normalized, $itemIndex, 'category', $platforms) as $value => $count) { + $typeCounts[$value] = ($typeCounts[$value] ?? 0) + $count; + } + foreach ($this->foldItemCounts($normalized, $itemIndex, 'memo', $platforms) as $value => $count) { + $remarkCounts[$value] = ($remarkCounts[$value] ?? 0) + $count; + } + } + $facets['type'] = $this->shape($typeCounts, $this->types()); + $facets['remark'] = $this->shape($remarkCounts, $this->remarks()); + // 未限定平台时,同一账单项目在不同平台可能挂不同备注/类型,行数无法精确拆分: + // 这种情况只给可选值(用于隐藏真的没有数据的取值),不展示可能偏大的行数。 + if ($contextPlatforms === []) { + $facets['type']['counts'] = []; + $facets['remark']['counts'] = []; + } + + return $facets; + } + + /** @return array,counts:array}> */ + private function emptyFacets(): array + { + return [ + 'platform' => ['values' => [], 'counts' => []], + 'store' => ['values' => [], 'counts' => []], + 'item' => ['values' => [], 'counts' => []], + 'type' => ['values' => [], 'counts' => []], + 'remark' => ['values' => [], 'counts' => []], + ]; + } + + /** @param array $filters */ + private function withoutFilter(array $filters, string $key): array + { + if ($key === 'keyword') { + $filters['keyword'] = ''; + } else { + $filters[$key] = []; + } + + return $filters; + } + + /** + * 平台联动:按「账户/数据来源」分支 + 店铺前缀统计行数。 + * + * @param array $filters + * @return array{values:list,counts:array} + */ + private function facetPlatform(mixed $conn, array $filters): array + { + $counts = []; + foreach (self::SOURCES as $label => $meta) { + $cases = []; + $labels = []; + foreach (self::PLATFORM_FILTERS as $platformLabel => $mapping) { + if ($mapping['source'] !== $label) { + continue; + } + $cases[] = $mapping['prefix'] === null + ? 'COUNT(*)' + : 'SUM(CASE WHEN store_name LIKE ' . $this->likeLiteral((string) $mapping['prefix']) . ' THEN 1 ELSE 0 END)'; + $labels[] = $platformLabel; + } + if ($cases === []) { + continue; + } + + $select = []; + foreach ($cases as $index => $case) { + $select[] = 'COALESCE(' . $case . ', 0) AS c' . $index; + } + + $branch = $this->branch($label, $meta, $filters); + foreach ($branch['scopes'] as $scope) { + $row = $conn->selectOne( + 'SELECT ' . implode(', ', $select) . ' FROM ' . $branch['table'] . ' WHERE ' . $scope['where'], + $scope['bind'], + ); + foreach ($labels as $index => $platformLabel) { + $counts[$platformLabel] = ($counts[$platformLabel] ?? 0) + (int) ($row->{'c' . $index} ?? 0); + } + } + } + + return $this->shape($counts, array_keys(self::PLATFORM_FILTERS)); + } + + /** + * 单列联动:按该表达式分组统计行数。 + * + * @param array $filters + * @return array + */ + /** 分支 → 该分支可能命中的科目映射表平台 */ + private const BRANCH_PLATFORMS = [ + '淘系支付宝' => ['天猫', '淘宝', '阿里巴巴', '支付宝'], + '淘系微信' => ['天猫', '淘宝'], + '拼多多' => ['拼多多'], + '京东账单' => ['京东', '京喜'], + '京东钱包' => ['京东'], + '抖音' => ['抖音'], + '得物' => ['得物'], + ]; + + private function facetCounts(mixed $conn, array $filters, string $expression): array + { + $counts = []; + foreach ($this->facetCountsByBranch($conn, $filters, $expression) as $branchCounts) { + foreach ($branchCounts as $key => $count) { + $counts[$key] = ($counts[$key] ?? 0) + $count; + } + } + + return $counts; + } + + /** + * 单列联动(按分支返回):用于只在本分支的平台集合内折算财务类型 / 财务备注。 + * + * @param array $filters + * @return array> 分支标签 → [取值 => 行数] + */ + private function facetCountsByBranch(mixed $conn, array $filters, string $expression): array + { + $result = []; + foreach (self::SOURCES as $label => $meta) { + $branch = $this->branch($label, $meta, $filters); + $parts = []; + $bind = []; + foreach ($branch['scopes'] as $scope) { + $parts[] = 'SELECT ' . $expression . ' AS k, COUNT(*) AS c FROM ' . $branch['table'] + . ' WHERE ' . $scope['where'] . ' GROUP BY k'; + $bind = array_merge($bind, $scope['bind']); + } + if ($parts === []) { + continue; + } + + $counts = []; + foreach ($conn->select(implode(' UNION ALL ', $parts), $bind) as $row) { + $key = trim((string) $row->k); + if ($key !== '') { + $counts[$key] = ($counts[$key] ?? 0) + (int) $row->c; + } + } + $result[$label] = $counts; + } + + return $result; + } + + /** + * wx 前缀归一:把「wx 前缀」的账单项目并到去前缀后的名称上。 + * + * @param array $counts + * @return array + */ + /** + * 平台筛选值(账户口径)→ 科目映射表里的平台取值。 + * + * @param list $labels + * @return list + */ + private function rulePlatforms(array $labels): array + { + $map = [ + '淘系支付宝' => ['天猫', '淘宝', '阿里巴巴', '支付宝'], + '淘系微信' => ['天猫', '淘宝'], + '拼多多' => ['拼多多'], + '京东账单' => ['京东'], + '京喜账单' => ['京喜'], + '京东网银钱包' => ['京东'], + '抖音' => ['抖音'], + '得物' => ['得物'], + ]; + $platforms = []; + foreach ($labels as $label) { + foreach ($map[$label] ?? [$label] as $platform) { + $platforms[$platform] = true; + } + } + + return array_keys($platforms); + } + + private function normalizeItemCounts(array $counts): array + { + $normalized = []; + foreach ($counts as $item => $count) { + $key = $this->normalizeItem($item); + if ($key === '') { + continue; + } + $normalized[$key] = ($normalized[$key] ?? 0) + $count; + } + + return $normalized; + } + + /** + * 账单项目 → [平台 => ['memo'=>..,'category'=>..]] 预索引,避免逐项遍历全部规则。 + * + * @param array $rules + * @return array> + */ + private function ruleIndexByItem(array $rules): array + { + $index = []; + foreach ($rules as $key => $rule) { + [$platform, $item] = array_pad(explode('|', $key, 2), 2, ''); + if ($platform === '' || $item === '') { + continue; + } + $index[$item][$platform] = ['memo' => $rule['memo'], 'category' => $rule['category']]; + } + + return $index; + } + + /** + * 账单项目行数 → 财务类型 / 财务备注行数。 + * + * @param array $itemCounts + * @param array> $itemIndex + * @param list $platforms 平台筛选(为空表示不限平台) + * @return array + */ + private function foldItemCounts(array $itemCounts, array $itemIndex, string $field, array $platforms): array + { + $counts = []; + foreach ($itemCounts as $item => $count) { + $values = []; + foreach ($itemIndex[$item] ?? [] as $platform => $rule) { + $value = (string) ($rule[$field] ?? ''); + if ($value === '') { + continue; + } + if ($platforms !== [] && ! in_array($platform, $platforms, true)) { + continue; + } + $values[$value] = true; + } + foreach (array_keys($values) as $value) { + $counts[$value] = ($counts[$value] ?? 0) + $count; + } + } + + return $counts; + } + + /** + * @param array $counts + * @param list $order + * @return array{values:list,counts:array} + */ + private function shape(array $counts, array $order = []): array + { + $values = []; + foreach ($order as $value) { + if (isset($counts[$value])) { + $values[] = $value; + } + } + $rest = array_values(array_diff(array_keys($counts), $values)); + sort($rest, SORT_NATURAL | SORT_FLAG_CASE); + $values = array_merge($values, $rest); + + $shaped = []; + foreach ($values as $value) { + $shaped[$value] = (int) $counts[$value]; + } + + return ['values' => $values, 'counts' => $shaped]; + } + + private function likeLiteral(string $prefix): string + { + return "'" . str_replace(["\\", "'", '%', '_'], ["\\\\", "''", '\\%', '\\_'], $prefix) . "%'"; + } + /** * 组装单个平台分支:统一的输出列 + 每个账期版本一份「筛选已下推」的 WHERE。 * @@ -404,6 +747,13 @@ class BillingDetailsPageData $conditions[] = $this->normalizedItemSql() . ' IN (' . $this->literals($allowed) . ')'; } } + if (($filters['remark'] ?? []) !== []) { + // 财务备注筛选:与财务类型同一口径,先落回「账单项目」集合再下推 SQL。 + $allowed = $this->itemsForRemarks($filters['remark']); + $conditions[] = $allowed === [] + ? '1 = 0' + : $this->normalizedItemSql() . ' IN (' . $this->literals($allowed) . ')'; + } $dateBind = $dateCol === 'bill_date' ? [$filters['start'], $filters['end']] @@ -509,6 +859,7 @@ class BillingDetailsPageData 'store' => $this->values($input['store'] ?? []), 'item' => $this->values($input['item'] ?? []), 'type' => $this->values($input['type'] ?? []), + 'remark' => $this->values($input['remark'] ?? []), 'keyword' => trim((string) ($input['keyword'] ?? '')), 'per_page' => $this->perPage($input), ]; @@ -644,6 +995,36 @@ class BillingDetailsPageData return $types; } + /** @return list 科目映射表里出现过的「财务备注」取值 */ + private function remarks(): array + { + $remarks = []; + foreach ($this->rules() as $rule) { + if ($rule['memo'] !== '' && ! in_array($rule['memo'], $remarks, true)) { $remarks[] = $rule['memo']; } + } + sort($remarks); + return $remarks; + } + + /** + * 多个财务备注 → 其下所有账单项目的并集。 + * + * @param list $remarks + * @return list + */ + private function itemsForRemarks(array $remarks): array + { + $wanted = array_flip($remarks); + $items = []; + foreach ($this->rules() as $key => $rule) { + if (isset($wanted[$rule['memo']])) { + $items[explode('|', $key, 2)[1]] = true; + } + } + + return array_keys($items); + } + /** 财务类型 → 该类型下的账单项目集合(用于按类型筛选时保持分页正确) */ private function itemsForType(string $type): array { 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 497c36779..fc0cd5807 100644 --- a/resources/views/business/pages/financial-operations/billing-details.blade.php +++ b/resources/views/business/pages/financial-operations/billing-details.blade.php @@ -13,7 +13,8 @@ .pd-table-wrap { border:1px solid #d8d2c7; border-radius:.75rem; overflow:auto; background:#fff; } .pd-table { width:100%; border-collapse:collapse; font-size:.8125rem; } .pd-table th { background:#1f2937; color:#fff; font-weight:600; text-align:center; padding:.5rem .4rem; white-space:nowrap; } - .pd-table td { border-bottom:1px solid #eee7da; padding:.45rem .5rem; white-space:nowrap; } + .pd-table td { border-bottom:1px solid #eee7da; padding:.6rem .5rem; line-height:1.6; white-space:nowrap; } + .pd-table th { line-height:1.6; } .pd-table td.num { text-align:right; } .pd-tag { display:inline-block; padding:.05rem .4rem; border:1px solid #e0d6c2; border-radius:999px; background:#faf6ee; } .pd-miss { color:#b04a4a; } @@ -101,6 +102,17 @@ +
+ 财务备注 + + + + + +
发生时间
@@ -254,19 +266,76 @@