快递运费:其他附加费汇总后面新增「退件费」列(按交易单号取快递应付的退件费结算金额)

退件费快照的表头跟另外两个不一样:单号列叫「交易订单」、金额列叫「结算金额」,所以 CourierPayableSummaryMap 的列定位改成按候选关键字找(先全等、再包含),三个汇总页共用一套逻辑。
This commit is contained in:
qiuxiang 2026-10-08 15:39:51 +08:00
parent a0ce32ead1
commit ddbf32a0ee
4 changed files with 41 additions and 21 deletions

View File

@ -9,7 +9,7 @@ use Throwable;
/**
* 快递应付的两个汇总页 → 「交易单号 ⇒ 金额类取值」映射。
*
* 这两份数据都是财务在「应付款项 › 快递应付 › 运费差 / 其他附加费汇总」页面上传的汇总快照
* 这些数据都是财务在「应付款项 › 快递应付 › 运费差 / 其他附加费汇总 / 退件费」页面上传的汇总快照
* (JSON 文件,不是数据库表),快递运费页面按交易单号把对应金额带出来展示。
* 快照重新导入后文件时间会变,缓存键跟着变,自动失效。
*/
@ -21,10 +21,19 @@ class CourierPayableSummaryMap
/** 其他附加费汇总:金额。 */
public const PAGE_OTHER_SURCHARGE = 'other-surcharge-summary';
/** 汇总页 → [交易单号所在列的关键字, 取值列的关键字]。 */
/** 退件费:结算金额(单号列叫「交易订单」)。 */
public const PAGE_RETURN_FEE = 'return-fee';
/**
* 汇总页 → [单号列的关键字候选, 取值列的关键字候选]。
*
* 各页表头写法不统一(「交易单号」/「交易订单」、「金额」/「结算金额」),
* 所以按候选顺序找:先全等匹配,再退化成包含匹配。
*/
private const PAGES = [
self::PAGE_FREIGHT_DIFFERENCE => ['交易单号', '差异'],
self::PAGE_OTHER_SURCHARGE => ['交易单号', '金额'],
self::PAGE_FREIGHT_DIFFERENCE => [['交易单号', '交易订单'], ['差异', '金额']],
self::PAGE_OTHER_SURCHARGE => [['交易单号', '交易订单'], ['金额']],
self::PAGE_RETURN_FEE => [['交易订单', '交易单号'], ['结算金额', '金额']],
];
private const CACHE_MINUTES = 10;
@ -69,10 +78,10 @@ class CourierPayableSummaryMap
return [];
}
[$orderHeader, $valueHeader] = self::PAGES[$page];
[$orderHeaders, $valueHeaders] = 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);
$orderColumn = $this->columnIndex($headers, $orderHeaders);
$valueColumn = $this->columnIndex($headers, $valueHeaders);
if ($orderColumn === null || $valueColumn === null) {
return [];
}
@ -101,19 +110,27 @@ class CourierPayableSummaryMap
return $map;
}
/** 按表头名模糊定位(表头可能是「差异(结算运费-提供成本运费)」等写法)。 */
private function columnIndex(array $headers, string $needle): ?int
/**
* 按表头名定位:候选顺序优先,每个候选先找完全相等的表头,再退化成包含匹配
* (例如「差异(结算运费-提供成本运费)」只能靠包含匹配)。
*
* @param list<string> $needles
*/
private function columnIndex(array $headers, array $needles): ?int
{
// 先找完全相等的表头(避免「金额」误命中「结算金额」之类的近似列),再退化成包含匹配。
foreach ($headers as $index => $header) {
if ($header === $needle) {
return (int) $index;
foreach ($needles as $needle) {
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;
foreach ($needles as $needle) {
foreach ($headers as $index => $header) {
if ($header !== '' && mb_strpos($header, $needle) !== false) {
return (int) $index;
}
}
}

View File

@ -905,15 +905,17 @@ class Pages extends Controller
$rows = $query->orderByDesc('id')->paginate($limit)->withQueryString();
// 「运费差」「其他附加费汇总」不在本表里,按交易单号去「快递应付」对应汇总快照里取。
// 「运费差」「其他附加费汇总」「退件费」不在本表里,按交易单号去「快递应付」对应汇总快照里取。
$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) {
$returnFees = $summaries->map((int) company_id(), \App\Business\CourierPayableSummaryMap::PAGE_RETURN_FEE);
$rows->setCollection($rows->getCollection()->map(static function ($row) use ($differences, $surcharges, $returnFees) {
$orderNo = trim((string) $row->transaction_no);
$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));
$row->setAttribute('return_fee_amount', $pick($returnFees));
return $row;
}));

View File

@ -818,7 +818,7 @@ $navigation = [
'directory' => [
'icon' => 'local_shipping',
'status' => '本地导入',
'description' => '快递方提供的成本运费明细(来源 / 成品配件 / 交易单号 / 提供成本运费 / 运费差 / 其他附加费汇总),由「5-7快递提供成本运费.xlsx」等原始表导入;成品配件含「成品运费」「配件运费」两类;「运费差」「其他附加费汇总」按交易单号分别取自「应付款项 › 快递应付 › 运费差 / 其他附加费汇总」的汇总数据;可按来源(月份)与交易单号查询、导出。',
'description' => '快递方提供的成本运费明细(来源 / 成品配件 / 交易单号 / 提供成本运费 / 运费差 / 其他附加费汇总 / 退件费),由「5-7快递提供成本运费.xlsx」等原始表导入;成品配件含「成品运费」「配件运费」两类;后三列按交易单号分别取自「应付款项 › 快递应付 › 运费差 / 其他附加费汇总 / 退件费」的汇总数据;可按来源(月份)与交易单号查询、导出。',
'empty' => '快递运费明细尚未导入,请先执行 courier-freight:import。',
'fields' => [
['name' => 'source', 'label' => '来源'],
@ -827,6 +827,7 @@ $navigation = [
['name' => 'freight_amount', 'label' => '提供成本运费', 'kind' => 'number'],
['name' => 'freight_difference', 'label' => '运费差', 'kind' => 'number'],
['name' => 'other_surcharge_amount', 'label' => '其他附加费汇总', 'kind' => 'number'],
['name' => 'return_fee_amount', 'label' => '退件费', 'kind' => 'number'],
],
],
],

View File

@ -1085,9 +1085,9 @@
$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', 'other_surcharge_amount',
'part', 'freight_amount', 'freight_difference', 'other_surcharge_amount', 'return_fee_amount',
],
// 流程统计:查询条件保留外部店铺申请页那一套(单据编号/申请人/日期/审批人/店铺/平台/负责人/利润中心/商标/类型/状态),其余列只做展示。
'financial-risk-control/process-statistics' => [