limeng 1cf60dd7b0 feat(发货数据): 按报价管理的报价核算运费,结果显示在发货数据页面
从 2026-11-01 起,按发货日在报价管理里生效的报价版本(与报价管理「当前生效」同一判断:未作废、
已审核/签署生效、执行期间覆盖发货日,取执行日期最新)逐包裹核算运费,写入 jst_oms_shipping_freight。
- 结算重量:实称>线上>重量;按各快递计泡系数取 max(重量, 长×宽×高÷系数);免抛仓(含「配件」仓)不计泡、不收多件费;
  超长费计入,其他附加费不计;城市单独报价、整省「X元/公斤」、洞洞板 9000、个别版本系数等按 10 月逐单核对的口径;
- 计算引擎移植自 10 月离线核算脚本,10 月 29.5 万个包裹逐单比对结果一致(仅 x.xx5 边界按正确四舍五入进位);
- 发货数据同步完成后核算该窗口;报价版本新增/作废/签署归档后按该快递从执行日期起自动重算;
- 新命令 jushuitan:calc-freight --from --to [--carrier];
- 发货数据页面/导出新增「报价核算」列:核算运费、核算-聚水潭运费、报价版本、结算重量依据、核算结算重量、
  计费重量、首重、续重/费率、面单、一口价、多件/超长加收、核算公式、核算状态(合并口径每包裹一行,明细只在第一行)。

同时修复发货数据同步 11 月起频繁失败:副本库查发货视图超时后报 20047 DBPROCESS is dead,
原重试只识别 20003/dbnextrow,整次同步失败(11-17 只同步到 62%)。现把 20047 视为可重连重试,
发货视图每批出库单由 300 降到 100。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014nvGZkg27YMEdhry6eiuwf
2026-11-18 13:55:40 +08:00

293 lines
11 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 Modules\EcommerceFinance\Services\Freight;
/**
* 报价表(报价管理 values_json 单元格)解析:找表头、按省份取行、识别重量段表头、条码报价表。
* 与离线核算脚本(10 月核对过的口径)一一对应,单元格里的数字/文字按原样参与判断。
*/
final class QuoteSheet
{
public const CITY_KEYS = ['深圳', '舟山', '拉萨', '广州'];
public const PROVINCES = ['北京', '天津', '上海', '重庆', '河北', '山西', '辽宁', '吉林', '黑龙', '江苏', '浙江', '安徽', '福建', '江西', '山东', '河南', '湖北', '湖南', '广东', '海南', '四川', '贵州', '云南', '陕西', '甘肃', '青海', '台湾', '内蒙', '广西', '西藏', '宁夏', '新疆'];
private const HEADER_FIRST_CELLS = ['省份', '目的地', '地区', '公斤段', '条形码'];
private const BARCODE_RE = '/[A-Za-z]{2,}\d[A-Za-z0-9\-]{5,}/u';
/** 单元格转文字(对齐 Python str:整数值的小数写成 3.0,空/0 视为空)。 */
public static function text(mixed $cell): string
{
if ($cell === null || $cell === false || $cell === '' || $cell === 0 || $cell === 0.0) {
return '';
}
if (is_float($cell)) {
return floor($cell) == $cell && abs($cell) < 1e15 ? sprintf('%.1f', $cell) : (string) $cell;
}
return (string) $cell;
}
/** 数字单元格转 float;文字原样(去空白)返回;空返回 null。 */
public static function num(mixed $cell): float|string|null
{
if ($cell === null) {
return null;
}
if (is_int($cell) || is_float($cell)) {
return (float) $cell;
}
$t = trim((string) $cell);
if ($t !== '' && is_numeric($t)) {
return (float) $t;
}
return $t === '' ? null : $t;
}
/** 省份名归一到两字(黑龙/内蒙/广西/宁夏/新疆/西藏 前缀)。 */
public static function province(mixed $name): string
{
$s = (string) preg_replace('/\s/u', '', (string) ($name ?? ''));
foreach (['黑龙', '内蒙', '广西', '宁夏', '新疆', '西藏'] as $p) {
if (str_starts_with($s, $p)) {
return $p;
}
}
$s = (string) preg_replace('/((维吾尔|壮族|回族)?自治区|省|市)$/u', '', $s);
return mb_substr($s, 0, 2);
}
/**
* @param list<list<mixed>> $vals
* @return array{head:list<string>,rows:array<string,list<mixed>>,top:string}|null
*/
public static function parse(array $vals): ?array
{
$h = null;
foreach ($vals as $i => $r) {
if (is_array($r) && $r !== [] && in_array(str_replace(' ', '', trim(self::text($r[0] ?? null))), self::HEADER_FIRST_CELLS, true)) {
$h = $i;
break;
}
}
if ($h === null) {
return null;
}
$ncol = 0;
foreach ($vals as $r) {
$ncol = max($ncol, is_array($r) ? count($r) : 0);
}
$head = [];
for ($i = 0; $i < $ncol; $i++) {
$head[] = str_replace("\n", ' ', self::text($vals[$h][$i] ?? null));
}
$start = $h + 1;
$next = $vals[$h + 1] ?? [];
$c0 = is_array($next) && $next !== [] ? trim(self::text($next[0] ?? null)) : '';
if (is_array($next) && $next !== [] && in_array($c0, ['', '省份'], true) && self::anyTruthy(array_slice($next, 1))) {
for ($i = 0; $i < $ncol; $i++) {
$head[$i] = $head[$i] . ' ' . self::text($next[$i] ?? null);
}
$start = $h + 2;
}
$rows = [];
foreach (array_slice($vals, $start) as $r) {
if (! is_array($r) || $r === [] || ($r[0] ?? null) === null) {
continue;
}
$name = (string) preg_replace('/\s/u', '', self::rawText($r[0]));
if ($name === '' || mb_strlen($name) > 8) {
continue;
}
$key = in_array($name, ['深圳', '舟山', '拉萨'], true) ? $name : self::province($name);
if (! array_key_exists($key, $rows)) {
$row = [];
for ($i = 0; $i < $ncol; $i++) {
$row[] = self::num($r[$i] ?? null);
}
$rows[$key] = $row;
}
foreach (self::CITY_KEYS as $ck) { // 城市单独报价行(如 深圳市 / 深圳广州)
if (str_contains($name, $ck) && $ck !== $key && ! array_key_exists($ck, $rows)) {
$rows[$ck] = $rows[$key];
}
}
}
$top = implode(' ', array_map(static fn ($c) => self::text($c), is_array($vals[0] ?? null) ? $vals[0] : []));
return ['head' => $head, 'rows' => $rows, 'top' => $top];
}
/** 第一个匹配任一正则、且不匹配排除正则的表头列号(从 $start 起)。 */
public static function col(array $head, array $patterns, int $start = 1, array $exclude = []): ?int
{
for ($i = $start, $n = count($head); $i < $n; $i++) {
$t = $head[$i];
$hit = false;
foreach ($patterns as $p) {
if (preg_match('/' . $p . '/u', $t)) {
$hit = true;
break;
}
}
if (! $hit) {
continue;
}
foreach ($exclude as $e) {
if (preg_match('/' . $e . '/u', $t)) {
continue 2;
}
}
return $i;
}
return null;
}
/**
* 表头里的重量段:[下限, 上限, 列号]。识别 0-1kg、500g-1kg、300g、3kg、一口价 等写法。
*
* @param iterable<int> $cols
* @return list<array{0:float,1:float,2:int}>
*/
public static function bands(array $head, iterable $cols): array
{
$out = [];
foreach ($cols as $i) {
$t = str_replace([' ', '~', '~'], ['', '-', '-'], (string) ($head[$i] ?? ''));
if (preg_match('/(\d+)g-([\d.]+)kg/iu', $t, $m)) {
$out[] = [(float) $m[1] / 1000, (float) $m[2], $i];
} elseif (preg_match('/^(\d+)g(一口价)?$/iu', $t, $m)) {
$out[] = [0.0, (float) $m[1] / 1000, $i];
} elseif (preg_match('/([\d.]+)\s*[-~]\s*([\d.]+)/u', $t, $m) && is_numeric($m[1]) && is_numeric($m[2])) {
$out[] = [(float) $m[1], (float) $m[2], $i];
} elseif (preg_match('/([\d.]+)\s*(kg|公斤|KG)/u', $t, $m) && is_numeric($m[1])) {
$out[] = [0.0, (float) $m[1], $i];
} elseif (str_contains($t, '一口价')) {
$out[] = [0.0, 1e9, $i];
}
}
return $out;
}
/** 文字里出现的省份(两字简称)。 */
public static function provincesIn(mixed $text): array
{
$t = (string) preg_replace('/\s/u', '', self::text($text));
$out = [];
foreach (self::PROVINCES as $p) {
if (str_contains($t, $p)) {
$out[$p] = true;
}
}
return $out;
}
/**
* 按条码报价的表:{条码: [[省份集合|null(其余), 价格]]}、不发省份、布局。
*
* @return array{0:array<string,list<array{0:?array,1:mixed}>>,1:array<string,bool>,2:string}
*/
public static function barcodeTable(array $vals): array
{
$table = [];
$nogo = [];
foreach ($vals as $r) {
foreach ((array) $r as $c) {
$s = self::text($c);
if ($s !== '' && str_contains($s, '不发') && ! preg_match(self::BARCODE_RE, $s)) {
$nogo += self::provincesIn($c);
}
}
}
$isBarcode = static fn ($c): bool => (bool) preg_match('/^' . trim(self::BARCODE_RE, '/u') . '$/u', trim(self::text($c)));
$rowsBc = array_values(array_filter($vals, static fn ($r) => is_array($r) && ($r[0] ?? null) && $isBarcode($r[0])));
if ($rowsBc !== []) {
$hdr = null;
foreach ($vals as $r) {
foreach ((array) $r as $c) {
$s = self::text($c);
if ($s !== '' && (str_contains($s, '剩余') || str_contains($s, '其余') || str_contains($s, '其他'))) {
$hdr = $r;
break 2;
}
}
}
$regions = [];
if ($hdr !== null) {
foreach ($hdr as $i => $c) {
$s = self::text($c);
if ($i === 0 || $s === '') {
continue;
}
$regions[] = [$i, (str_contains($s, '剩余') || str_contains($s, '其余') || str_contains($s, '其他')) ? null : self::provincesIn($c)];
}
} else {
$regions = [[1, null]];
}
foreach ($rowsBc as $r) {
$bc = mb_strtoupper(trim(self::text($r[0])));
$table[$bc] = array_map(static fn ($reg) => [$reg[1], self::num($r[$reg[0]] ?? null)], $regions);
}
return [$table, $nogo, 'rows'];
}
foreach ($vals as $hi => $r) {
$groups = [];
foreach ((array) $r as $i => $c) {
$s = self::text($c);
if ($s !== '' && preg_match_all(self::BARCODE_RE, $s, $mm)) {
$groups[] = [$i, array_map('mb_strtoupper', $mm[0])];
}
}
if ($groups === []) {
continue;
}
$provRows = [];
foreach (array_slice($vals, $hi + 1) as $rr) {
if (is_array($rr) && ($rr[0] ?? null)) {
$k = self::province($rr[0]);
if (in_array($k, self::PROVINCES, true)) {
$provRows[$k] = $rr;
}
}
}
foreach ($groups as [$i, $bcs]) {
foreach ($bcs as $bc) {
$list = [];
foreach ($provRows as $p => $rr) {
$list[] = [[$p => true], self::num($rr[$i] ?? null)];
}
$table[$bc] = $list;
}
}
return [$table, $nogo, 'cols'];
}
return [[], $nogo, 'none'];
}
private static function rawText(mixed $cell): string
{
return is_float($cell) ? self::text($cell) : (string) $cell;
}
private static function anyTruthy(array $cells): bool
{
foreach ($cells as $c) {
if ($c !== null && $c !== '' && $c !== 0 && $c !== 0.0 && $c !== false) {
return true;
}
}
return false;
}
}