快递运费:运费差后面新增「其他附加费汇总」列(按交易单号取快递应付的附加费金额)
把上一版的 CourierFreightDifferenceMap 改成通用的 CourierPayableSummaryMap,同时支持「运费差」和「其他附加费汇总」两个汇总快照(都是上传在 uploads 盘的 JSON):运费差取「差异(结算运费-提供成本运费)」,附加费取「金额」,都按交易单号挂到快递运费页面对应行;快照文件时间戳进缓存键,财务重导后自动失效。
This commit is contained in:
parent
d69a7d2ca7
commit
0c84afb875
@ -1,99 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Business;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 快递应付 › 运费差 → 「交易单号 ⇒ 差异(结算运费-提供成本运费)」映射。
|
||||
*
|
||||
* 运费差是财务在「应付款项 › 快递应付 › 运费差」页面上传的汇总快照(JSON 文件,不是数据库表),
|
||||
* 快递运费页面按交易单号把差异带出来展示。快照重新导入后文件时间会变,缓存键跟着变,自动失效。
|
||||
*/
|
||||
class CourierFreightDifferenceMap
|
||||
{
|
||||
/** 对应 CourierPayableSummaryStore::PAGES 里的运费差页面。 */
|
||||
public const PAGE = 'freight-difference';
|
||||
|
||||
private const CACHE_MINUTES = 10;
|
||||
|
||||
public function __construct(private readonly CourierPayableSummaryStore $store) {}
|
||||
|
||||
/**
|
||||
* @return array<string,string> 交易单号 => 运费差(保留源文件里的原值字符串)
|
||||
*/
|
||||
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<string,string> */
|
||||
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;
|
||||
}
|
||||
}
|
||||
122
app/Business/CourierPayableSummaryMap.php
Normal file
122
app/Business/CourierPayableSummaryMap.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}));
|
||||
|
||||
@ -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'],
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
@ -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',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user