CaiWuXiTong/app/Business/CourierPayableSummaryMap.php
qiuxiang 0c84afb875 快递运费:运费差后面新增「其他附加费汇总」列(按交易单号取快递应付的附加费金额)
把上一版的 CourierFreightDifferenceMap 改成通用的 CourierPayableSummaryMap,同时支持「运费差」和「其他附加费汇总」两个汇总快照(都是上传在 uploads 盘的 JSON):运费差取「差异(结算运费-提供成本运费)」,附加费取「金额」,都按交易单号挂到快递运费页面对应行;快照文件时间戳进缓存键,财务重导后自动失效。
2026-10-08 15:21:25 +08:00

123 lines
4.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Business;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Throwable;
/**
* 快递应付的两个汇总页 → 「交易单号 ⇒ 金额类取值」映射。
*
* 这两份数据都是财务在「应付款项 › 快递应付 › 运费差 / 其他附加费汇总」页面上传的汇总快照
* (JSON 文件,不是数据库表),快递运费页面按交易单号把对应金额带出来展示。
* 快照重新导入后文件时间会变,缓存键跟着变,自动失效。
*/
class CourierPayableSummaryMap
{
/** 运费差:差异(结算运费-提供成本运费)。 */
public const PAGE_FREIGHT_DIFFERENCE = 'freight-difference';
/** 其他附加费汇总:金额。 */
public const PAGE_OTHER_SURCHARGE = 'other-surcharge-summary';
/** 汇总页 → [交易单号所在列的关键字, 取值列的关键字]。 */
private const PAGES = [
self::PAGE_FREIGHT_DIFFERENCE => ['交易单号', '差异'],
self::PAGE_OTHER_SURCHARGE => ['交易单号', '金额'],
];
private const CACHE_MINUTES = 10;
public function __construct(private readonly CourierPayableSummaryStore $store) {}
/**
* @param string $page self::PAGE_* 之一
* @return array<string,string> 交易单号 => 金额(保留源文件里的原值字符串)
*/
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<string,string> */
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;
}
}