From 0c84afb8758a1d067f741e9a46fe804183f4f70b Mon Sep 17 00:00:00 2001 From: qiuxiang <394405259@qq.com> Date: Thu, 8 Oct 2026 15:20:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=AB=E9=80=92=E8=BF=90=E8=B4=B9=EF=BC=9A?= =?UTF-8?q?=E8=BF=90=E8=B4=B9=E5=B7=AE=E5=90=8E=E9=9D=A2=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E3=80=8C=E5=85=B6=E4=BB=96=E9=99=84=E5=8A=A0=E8=B4=B9=E6=B1=87?= =?UTF-8?q?=E6=80=BB=E3=80=8D=E5=88=97=EF=BC=88=E6=8C=89=E4=BA=A4=E6=98=93?= =?UTF-8?q?=E5=8D=95=E5=8F=B7=E5=8F=96=E5=BF=AB=E9=80=92=E5=BA=94=E4=BB=98?= =?UTF-8?q?=E7=9A=84=E9=99=84=E5=8A=A0=E8=B4=B9=E9=87=91=E9=A2=9D=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把上一版的 CourierFreightDifferenceMap 改成通用的 CourierPayableSummaryMap,同时支持「运费差」和「其他附加费汇总」两个汇总快照(都是上传在 uploads 盘的 JSON):运费差取「差异(结算运费-提供成本运费)」,附加费取「金额」,都按交易单号挂到快递运费页面对应行;快照文件时间戳进缓存键,财务重导后自动失效。 --- app/Business/CourierFreightDifferenceMap.php | 99 -------------- app/Business/CourierPayableSummaryMap.php | 122 ++++++++++++++++++ app/Http/Controllers/Business/Pages.php | 15 ++- config/business_navigation.php | 3 +- .../pages/partials/directory-table.blade.php | 6 +- 5 files changed, 136 insertions(+), 109 deletions(-) delete mode 100644 app/Business/CourierFreightDifferenceMap.php create mode 100644 app/Business/CourierPayableSummaryMap.php diff --git a/app/Business/CourierFreightDifferenceMap.php b/app/Business/CourierFreightDifferenceMap.php deleted file mode 100644 index f81e7e099..000000000 --- a/app/Business/CourierFreightDifferenceMap.php +++ /dev/null @@ -1,99 +0,0 @@ - 交易单号 => 运费差(保留源文件里的原值字符串) - */ - public function map(int $companyId): array - { - $disk = Storage::disk('uploads'); - $path = $this->store->path($companyId, self::PAGE); - if (! $disk->exists($path)) { - return []; - } - - // 文件时间戳进缓存键:财务重新导入运费差后不用等缓存过期。 - $key = sprintf('courier-freight-difference:v1:%d:%d', $companyId, (int) $disk->lastModified($path)); - - return Cache::remember($key, now()->addMinutes(self::CACHE_MINUTES), function () use ($companyId): array { - try { - return $this->build($companyId); - } catch (Throwable $exception) { - report($exception); - - return []; - } - }); - } - - /** @return array */ - private function build(int $companyId): array - { - $snapshot = $this->store->snapshot($companyId, self::PAGE); - if ($snapshot === null) { - return []; - } - - $headers = array_map(static fn ($value): string => trim((string) $value), (array) $snapshot['headers']); - $orderColumn = $this->columnIndex($headers, '交易单号'); - $differenceColumn = $this->columnIndex($headers, '差异'); - if ($orderColumn === null || $differenceColumn === null) { - return []; - } - - $map = []; - foreach ((array) $snapshot['rows'] as $row) { - $orderNos = trim((string) ($row[$orderColumn] ?? '')); - if ($orderNos === '') { - continue; - } - - $difference = trim((string) ($row[$differenceColumn] ?? '')); - if ($difference === '') { - continue; - } - - // 源表一个格子可能放多个交易单号(逗号分隔),逐个挂到同一个差异值上。 - foreach (explode(',', $orderNos) as $orderNo) { - $orderNo = trim($orderNo); - if ($orderNo !== '' && ! isset($map[$orderNo])) { - $map[$orderNo] = $difference; - } - } - } - - return $map; - } - - /** 按表头名模糊定位(表头可能是「差异(结算运费-提供成本运费)」等写法)。 */ - private function columnIndex(array $headers, string $needle): ?int - { - foreach ($headers as $index => $header) { - if ($header !== '' && mb_strpos($header, $needle) !== false) { - return (int) $index; - } - } - - return null; - } -} diff --git a/app/Business/CourierPayableSummaryMap.php b/app/Business/CourierPayableSummaryMap.php new file mode 100644 index 000000000..0756e2471 --- /dev/null +++ b/app/Business/CourierPayableSummaryMap.php @@ -0,0 +1,122 @@ + ['交易单号', '差异'], + self::PAGE_OTHER_SURCHARGE => ['交易单号', '金额'], + ]; + + private const CACHE_MINUTES = 10; + + public function __construct(private readonly CourierPayableSummaryStore $store) {} + + /** + * @param string $page self::PAGE_* 之一 + * @return array 交易单号 => 金额(保留源文件里的原值字符串) + */ + public function map(int $companyId, string $page): array + { + if (! isset(self::PAGES[$page])) { + return []; + } + + $disk = Storage::disk('uploads'); + $path = $this->store->path($companyId, $page); + if (! $disk->exists($path)) { + return []; + } + + // 文件时间戳进缓存键:财务重新导入运费差后不用等缓存过期。 + $key = sprintf('courier-payable-summary-map:v1:%s:%d:%d', $page, $companyId, (int) $disk->lastModified($path)); + + return Cache::remember($key, now()->addMinutes(self::CACHE_MINUTES), function () use ($companyId, $page): array { + try { + return $this->build($companyId, $page); + } catch (Throwable $exception) { + report($exception); + + return []; + } + }); + } + + /** @return array */ + private function build(int $companyId, string $page): array + { + $snapshot = $this->store->snapshot($companyId, $page); + if ($snapshot === null) { + return []; + } + + [$orderHeader, $valueHeader] = self::PAGES[$page]; + $headers = array_map(static fn ($value): string => trim((string) $value), (array) $snapshot['headers']); + $orderColumn = $this->columnIndex($headers, $orderHeader); + $valueColumn = $this->columnIndex($headers, $valueHeader); + if ($orderColumn === null || $valueColumn === null) { + return []; + } + + $map = []; + foreach ((array) $snapshot['rows'] as $row) { + $orderNos = trim((string) ($row[$orderColumn] ?? '')); + if ($orderNos === '') { + continue; + } + + $value = trim((string) ($row[$valueColumn] ?? '')); + if ($value === '') { + continue; + } + + // 源表一个格子可能放多个交易单号(逗号分隔),逐个挂到同一个金额上。 + foreach (explode(',', $orderNos) as $orderNo) { + $orderNo = trim($orderNo); + if ($orderNo !== '' && ! isset($map[$orderNo])) { + $map[$orderNo] = $value; + } + } + } + + return $map; + } + + /** 按表头名模糊定位(表头可能是「差异(结算运费-提供成本运费)」等写法)。 */ + private function columnIndex(array $headers, string $needle): ?int + { + // 先找完全相等的表头(避免「金额」误命中「结算金额」之类的近似列),再退化成包含匹配。 + foreach ($headers as $index => $header) { + if ($header === $needle) { + return (int) $index; + } + } + + foreach ($headers as $index => $header) { + if ($header !== '' && mb_strpos($header, $needle) !== false) { + return (int) $index; + } + } + + return null; + } +} diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 7befe018a..9a4582a8f 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -905,14 +905,15 @@ class Pages extends Controller $rows = $query->orderByDesc('id')->paginate($limit)->withQueryString(); - // 「运费差」不在本表里,按交易单号去「快递应付 › 运费差」的汇总快照里取。 - $differences = app(\App\Business\CourierFreightDifferenceMap::class)->map((int) company_id()); - $rows->setCollection($rows->getCollection()->map(static function ($row) use ($differences) { + // 「运费差」「其他附加费汇总」不在本表里,按交易单号去「快递应付」对应汇总快照里取。 + $summaries = app(\App\Business\CourierPayableSummaryMap::class); + $differences = $summaries->map((int) company_id(), \App\Business\CourierPayableSummaryMap::PAGE_FREIGHT_DIFFERENCE); + $surcharges = $summaries->map((int) company_id(), \App\Business\CourierPayableSummaryMap::PAGE_OTHER_SURCHARGE); + $rows->setCollection($rows->getCollection()->map(static function ($row) use ($differences, $surcharges) { $orderNo = trim((string) $row->transaction_no); - $row->setAttribute( - 'freight_difference', - $orderNo !== '' && isset($differences[$orderNo]) ? $differences[$orderNo] : null, - ); + $pick = static fn (array $map): ?string => $orderNo !== '' && isset($map[$orderNo]) ? $map[$orderNo] : null; + $row->setAttribute('freight_difference', $pick($differences)); + $row->setAttribute('other_surcharge_amount', $pick($surcharges)); return $row; })); diff --git a/config/business_navigation.php b/config/business_navigation.php index 591247382..dba63f42c 100644 --- a/config/business_navigation.php +++ b/config/business_navigation.php @@ -817,7 +817,7 @@ $navigation = [ 'directory' => [ 'icon' => 'local_shipping', 'status' => '本地导入', - 'description' => '快递方提供的成本运费明细(来源 / 成品配件 / 交易单号 / 提供成本运费 / 运费差),由「5-7快递提供成本运费.xlsx」等原始表导入;成品配件含「成品运费」「配件运费」两类;「运费差」按交易单号取自「应付款项 › 快递应付 › 运费差」的汇总数据;可按来源(月份)与交易单号查询、导出。', + 'description' => '快递方提供的成本运费明细(来源 / 成品配件 / 交易单号 / 提供成本运费 / 运费差 / 其他附加费汇总),由「5-7快递提供成本运费.xlsx」等原始表导入;成品配件含「成品运费」「配件运费」两类;「运费差」「其他附加费汇总」按交易单号分别取自「应付款项 › 快递应付 › 运费差 / 其他附加费汇总」的汇总数据;可按来源(月份)与交易单号查询、导出。', 'empty' => '快递运费明细尚未导入,请先执行 courier-freight:import。', 'fields' => [ ['name' => 'source', 'label' => '来源'], @@ -825,6 +825,7 @@ $navigation = [ ['name' => 'transaction_no', 'label' => '交易单号'], ['name' => 'freight_amount', 'label' => '提供成本运费', 'kind' => 'number'], ['name' => 'freight_difference', 'label' => '运费差', 'kind' => 'number'], + ['name' => 'other_surcharge_amount', 'label' => '其他附加费汇总', 'kind' => 'number'], ], ], ], diff --git a/resources/views/business/pages/partials/directory-table.blade.php b/resources/views/business/pages/partials/directory-table.blade.php index 31b161862..9a1fcd014 100644 --- a/resources/views/business/pages/partials/directory-table.blade.php +++ b/resources/views/business/pages/partials/directory-table.blade.php @@ -1085,8 +1085,10 @@ $directoryExcludedQueryFields = match ($directoryPageKey) { 'store-management/basic-information' => ['opened_at', 'renewed_at'], 'trademark-management/information' => ['registration_date', 'expiry_date'], - // 快递运费:金额 / 配件类别 / 运费差都不做查询条件(运费差来自快递应付的运费差快照)。 - 'financial-operations/courier-freight' => ['part', 'freight_amount', 'freight_difference'], + // 快递运费:金额 / 配件类别 / 运费差 / 其他附加费都不做查询条件(后两列来自快递应付的汇总快照)。 + 'financial-operations/courier-freight' => [ + 'part', 'freight_amount', 'freight_difference', 'other_surcharge_amount', + ], // 流程统计:查询条件保留外部店铺申请页那一套(单据编号/申请人/日期/审批人/店铺/平台/负责人/利润中心/商标/类型/状态),其余列只做展示。 'financial-risk-control/process-statistics' => [ 'shop_name', 'customer_manager', 'change_reason', 'close_reason',