CaiWuXiTong/app/Support/CourierBills/SummaryMapper.php

146 lines
5.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\Support\CourierBills;
/**
* 对账单「小汇总」→ 快递应付汇总一行的金额列。
*
* 规则来自财务 10 月的手工做法:36 份对账表里的 42 个小汇总按这里归集,
* 与手工快递汇总表逐行、逐列一致。项目名称决定落在哪一列,所以名称按财务习惯写,系统不改名。
*/
final class SummaryMapper
{
/** 差异明细 13 列(快递汇总表 AF–AR),键 = 列字母。 */
public const DIFF_COLUMNS = [
'AF' => '运费差异-成品', 'AG' => '运费差异-配件', 'AH' => '未发出', 'AI' => '匹配不到', 'AJ' => '重复',
'AK' => '快递记账', 'AL' => '原单补发配件', 'AM' => '违约金', 'AN' => '退件费', 'AO' => '叉车电费',
'AP' => '面单费', 'AQ' => '借支', 'AR' => '其他',
];
/**
* 单个项目归到哪里:差异明细列字母,或 supplement(补给快递)/ waybill(面单)。
*/
public static function classify(string $label): string
{
$l = preg_replace('/\s+/u', '', $label) ?? '';
if (str_contains($l, '补发配件') || str_contains($l, '配件补发')) {
return 'AL';
}
if (str_starts_with($l, '补') || str_contains($l, '记账补款') || preg_match('/需补$/u', $l)) {
return 'supplement';
}
if ($l === '面单') {
return 'waybill';
}
if ($l === '借支') {
return 'AQ';
}
if (str_contains($l, '违约金')) {
return 'AM';
}
if (preg_match('/重复|查重|已结算/u', $l)) {
return 'AJ';
}
if (str_contains($l, '记账')) {
return 'AK';
}
if (str_contains($l, '未发出') && ! str_contains($l, '实际已发出')) {
return 'AH';
}
if (preg_match('/匹配不到|查不到|未匹配|未提供|无发货数据|不结算|匹配失败/u', $l)) {
return 'AI';
}
if (in_array($l, ['退件费', '退货费用', '未退回运费'], true)) {
return 'AN';
}
if ($l === '叉车电费') {
return 'AO';
}
if ($l === '面单费') {
return 'AP';
}
if (preg_match('/运费|入库管理费|出库费用|仓储|订单操作费|装卸费/u', $l) && ! preg_match('/车费|保费/u', $l)) {
return 'AF';
}
return 'AR';
}
/**
* @param iterable<array{label:string,bill:?float,self:?float}> $items 小汇总各行(不含合计行)
* @return array<string,float|string> 快递汇总表列字母 => 值(J、K、L、N、O、P、AF–AR、AS、AT)
*/
public static function map(iterable $items): array
{
$diff = array_fill_keys(array_keys(self::DIFF_COLUMNS), 0.0);
$provided = 0.0;
$supplement = 0.0;
$bookkeepingSupplement = 0.0;
$waybill = 0.0;
$advance = 0.0;
$notes = [];
foreach ($items as $item) {
$label = (string) $item['label'];
$bill = $item['bill'];
$self = $item['self'];
$provided += (float) $bill;
$d = (float) $bill - (float) $self;
$target = self::classify($label);
if ($target === 'supplement') {
$supplement -= $d;
if (str_contains($label, '记账')) {
$bookkeepingSupplement -= $d;
}
continue;
}
if ($target === 'waybill') {
$waybill += $d;
continue;
}
// 快递未报、我方代垫要抵扣的车费:算借支,同时照常列在「其他」里说明。
if ($target === 'AR' && ($bill === null || (float) $bill == 0.0) && (float) $self < 0 && str_contains($label, '车费')) {
$advance += -(float) $self;
}
$diff[$target] += $d;
if ($target === 'AR' && abs($d) > 0.005) {
$notes[] = $label . self::number($d) . '元';
}
}
$penalty = $diff['AM'];
$advance += $diff['AQ'];
$reconDiff = array_sum($diff) - $penalty - $advance;
return [
'J' => round($supplement, 2),
'K' => round($provided, 2),
'L' => round($reconDiff, 2),
'N' => round($waybill, 2),
'O' => round($advance, 2),
'P' => round($penalty, 2),
...array_map(static fn (float $v): float => round($v, 2), $diff),
'AS' => round($bookkeepingSupplement, 2),
'AT' => implode(';', $notes),
];
}
/** 本月应付 R = K − L − N − O − P + Q + J + I;核对确定 M = K − L。 */
public static function totals(array $cells): array
{
$n = static fn (string $key): float => (float) ($cells[$key] ?? 0);
$cells['M'] = round($n('K') - $n('L'), 2);
$cells['R'] = round($n('K') - $n('L') - $n('N') - $n('O') - $n('P') + $n('Q') + $n('J') + $n('I'), 2);
return $cells;
}
private static function number(float $value): string
{
$text = rtrim(rtrim(number_format(round($value, 2), 2, '.', ''), '0'), '.');
return $text === '-0' ? '0' : $text;
}
}