发货整单重量校正与称重运费核算 (#1552)
This commit is contained in:
parent
b4ee49e7ef
commit
f7100aa3bf
@ -178,6 +178,8 @@ final class ShippingLookup
|
||||
'qc_charge_weight' => $sum('charge_weight'), 'qc_first_price' => $first->first_price, 'qc_rate' => $first->rate,
|
||||
'qc_miandan' => $sum('miandan'), 'qc_flat_price' => $first->flat_price, 'qc_extra_fee' => $sum('extra_fee'),
|
||||
'qc_formula' => $first->formula,
|
||||
'qc_measured_weight' => array_reduce($parts, static fn (?float $carry, object $p): ?float
|
||||
=> ($p->base_source ?? null) === '称重重量' && $p->base_weight !== null ? (float) $carry + (float) $p->base_weight : $carry, null),
|
||||
'qc_status' => implode(';', array_unique(array_map(static fn (object $p): string => (string) $p->status, $parts))),
|
||||
];
|
||||
if (count($set) > 1) {
|
||||
|
||||
@ -334,7 +334,7 @@ final class StatementExporter
|
||||
private function front(string $field, object $line, array $ship): string|float|null
|
||||
{
|
||||
return match ($field) {
|
||||
'_measured' => null,
|
||||
'_measured' => $ship['qc_measured_weight'] ?? null,
|
||||
'_unshipped' => $line->unshipped_sources,
|
||||
'_weight_diff' => $line->bill_weight !== null && ($ship['qc_charge_weight'] ?? null) !== null
|
||||
? round((float) $line->bill_weight - (float) $ship['qc_charge_weight'], 4) : null,
|
||||
|
||||
@ -25,8 +25,8 @@ final class FreightCarrierRules
|
||||
'北京韵达-永超' => '北京韵达',
|
||||
];
|
||||
|
||||
/** 发货日期早于此日不核算(报价核算从 2026-11 起)。 */
|
||||
public const START_DATE = '2026-10-01';
|
||||
/** 发货日期早于此日不核算(2026-11-29 用户要求从 9 月起核算;9 月没有生效报价的包裹状态为「发货日没有生效的报价」)。 */
|
||||
public const START_DATE = '2026-09-01';
|
||||
|
||||
/** @return array<string,array<string,mixed>> */
|
||||
public static function all(): array
|
||||
|
||||
@ -5,14 +5,14 @@ namespace Modules\EcommerceFinance\Services\Freight;
|
||||
/**
|
||||
* 按报价管理的报价核算一个包裹的运费(合并口径:一个包裹一行)。
|
||||
*
|
||||
* 结算重量 = 实称重量(有则用) 否则线上重量 否则重量;按快递计泡系数取 max(重量, 长×宽×高÷系数),
|
||||
* 结算重量 = 称重重量(导入的「条码×快递」称重,有则用) 否则实称重量 否则线上重量 否则重量;按快递计泡系数取 max(重量, 长×宽×高÷系数),
|
||||
* 免抛仓(及仓库名含「配件」)不计泡、不收多件费;超长费计入,其他附加费不计。
|
||||
* 每种报价版式(mode)的计算与 10 月离线逐单核对过的口径一致。
|
||||
*/
|
||||
final class ShippingFreightCalculator
|
||||
{
|
||||
/**
|
||||
* @param array<string,mixed> $pkg 包裹:express_name1, receiver_state, receiver_city, online_weight, actual_weight,
|
||||
* @param array<string,mixed> $pkg 包裹:express_name1, receiver_state, receiver_city, measured_weight, online_weight, actual_weight,
|
||||
* weight_kg, length_cm, width_cm, height_cm, volume_cm3, warehouse_name, pkg_count, barcodes
|
||||
* @param array{id:string,p:?array,rules:string,vals:array}|null $quote 发货日生效的报价版本
|
||||
* @param array<string,mixed>|null $cfg 该快递的计费方式(FreightCarrierRules),含 _name=报价名称
|
||||
@ -70,7 +70,8 @@ final class ShippingFreightCalculator
|
||||
$on = self::pos($pkg['online_weight'] ?? null);
|
||||
$act = self::pos($pkg['actual_weight'] ?? null);
|
||||
$wt = self::pos($pkg['weight_kg'] ?? null);
|
||||
[$base, $src] = $act ? [$act, '实称重量'] : ($on ? [$on, '线上重量'] : [$wt, '重量']);
|
||||
$meas = self::pos($pkg['measured_weight'] ?? null);
|
||||
[$base, $src] = $meas ? [$meas, '称重重量'] : ($act ? [$act, '实称重量'] : ($on ? [$on, '线上重量'] : [$wt, '重量']));
|
||||
$d['base_w'] = $base;
|
||||
$d['base_src'] = $src;
|
||||
$L = self::pos($pkg['length_cm'] ?? null);
|
||||
|
||||
@ -57,6 +57,7 @@ class ShippingFreightService
|
||||
}
|
||||
$this->versions = null;
|
||||
$this->resolved = [];
|
||||
$this->measured = null;
|
||||
$names = null;
|
||||
if ($carriers !== null) {
|
||||
$names = [];
|
||||
@ -179,13 +180,19 @@ class ShippingFreightService
|
||||
}
|
||||
}
|
||||
|
||||
/** 核算所属公司:有当前公司用当前公司,命令行等没有当前公司时用 jushuitan.freight_company_id。 */
|
||||
private function companyId(): int
|
||||
{
|
||||
return function_exists('company_id') && company_id() ? (int) company_id() : (int) config('jushuitan.freight_company_id', 1);
|
||||
}
|
||||
|
||||
/** @return array<string,Collection<int,CourierQuoteVersion>> */
|
||||
private function versions(): array
|
||||
{
|
||||
if ($this->versions !== null) {
|
||||
return $this->versions;
|
||||
}
|
||||
$companyId = function_exists('company_id') && company_id() ? (int) company_id() : (int) config('jushuitan.freight_company_id', 1);
|
||||
$companyId = $this->companyId();
|
||||
$all = CourierQuoteVersion::query()->allCompanies()
|
||||
->where('company_id', $companyId)
|
||||
->with([
|
||||
@ -234,8 +241,10 @@ class ShippingFreightService
|
||||
$rows = $this->db->table('jst_oms_shipping')->whereIn('source_id', $ids)->orderBy('id')
|
||||
->get(['id', 'source_id', 'express_no', 'express_name1', 'warehouse_name', 'receiver_state', 'receiver_city', 'ship_time',
|
||||
'pkg_count', 'online_weight', 'actual_weight', 'weight_kg', 'length_cm', 'width_cm', 'height_cm', 'volume_cm3',
|
||||
'm_length_cm', 'm_width_cm', 'm_height_cm', 'm_volume_cm3', 'barcode', 'freight']);
|
||||
'm_length_cm', 'm_width_cm', 'm_height_cm', 'm_volume_cm3', 'barcode', 'qty', 'freight']);
|
||||
$measured = $this->measuredWeights();
|
||||
$out = [];
|
||||
$mw = [];
|
||||
foreach ($rows as $r) {
|
||||
$sid = (int) $r->source_id;
|
||||
$p = &$out[$sid];
|
||||
@ -270,16 +279,67 @@ class ShippingFreightService
|
||||
if ($bc !== '' && ! in_array($bc, $p['barcodes'], true)) {
|
||||
$p['barcodes'][] = $bc;
|
||||
}
|
||||
unset($p);
|
||||
// 称重重量:包裹里每个条码都有导入称重时,按 Σ 称重×数量 作为整单重量;
|
||||
// 只有部分条码有称重时不用(线上/实称重量是整单重量,拆不出单件,没法和称重拼在一起)
|
||||
$m = &$mw[$sid];
|
||||
$m ??= ['sum' => 0.0, 'hit' => false, 'ok' => true];
|
||||
$carrier = trim((string) $r->express_name1);
|
||||
$unit = $bc === '' ? null : ($measured[$carrier . "\0" . mb_strtoupper($bc)]
|
||||
?? $measured[(FreightCarrierRules::ALIAS[$carrier] ?? $carrier) . "\0" . mb_strtoupper($bc)] ?? null);
|
||||
if ($unit !== null) {
|
||||
$qty = (float) ($r->qty ?? 0);
|
||||
$m['sum'] += $unit * ($qty > 0 ? $qty : 1);
|
||||
$m['hit'] = true;
|
||||
} else {
|
||||
$m['ok'] = false;
|
||||
}
|
||||
unset($p, $m);
|
||||
}
|
||||
foreach ($out as &$p) {
|
||||
foreach ($out as $sid => &$p) {
|
||||
$p['barcodes'] = implode(',', $p['barcodes']);
|
||||
$p['measured_weight'] = ! empty($mw[$sid]['hit']) && $mw[$sid]['ok'] ? round($mw[$sid]['sum'], 4) : null;
|
||||
}
|
||||
unset($p);
|
||||
|
||||
return array_values($out);
|
||||
}
|
||||
|
||||
/** @var array<string,float>|null 快递名称\0条码(大写) => 每件称重重量 kg */
|
||||
private ?array $measured = null;
|
||||
|
||||
/**
|
||||
* 发货数据页导入的称重重量(条码 × 适用快递名称)。表在系统主库;读不到时按没有称重处理。
|
||||
*
|
||||
* @return array<string,float>
|
||||
*/
|
||||
private function measuredWeights(): array
|
||||
{
|
||||
if ($this->measured !== null) {
|
||||
return $this->measured;
|
||||
}
|
||||
$this->measured = [];
|
||||
try {
|
||||
// 和报价同一个公司:命令行没有当前公司时也不能混用别家公司的称重重量
|
||||
$query = DB::table('shipping_measured_weights')->where('company_id', $this->companyId());
|
||||
foreach ($query->get(['carrier_name', 'barcode', 'weight_kg']) as $row) {
|
||||
$this->measured[trim((string) $row->carrier_name) . "\0" . mb_strtoupper(trim((string) $row->barcode))] = (float) $row->weight_kg;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
|
||||
return $this->measured;
|
||||
}
|
||||
|
||||
/** 导入称重重量后,这些快递从核算起始日起重算(请求结束后执行)。 */
|
||||
public static function queueMeasuredCarriers(array $carriers): void
|
||||
{
|
||||
foreach (array_unique($carriers) as $carrier) {
|
||||
$carrier = trim((string) $carrier);
|
||||
self::queueCarrier(FreightCarrierRules::ALIAS[$carrier] ?? $carrier, FreightCarrierRules::START_DATE);
|
||||
}
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
private function resultRow(array $pkg, array $r, string $now): array
|
||||
{
|
||||
|
||||
@ -147,6 +147,11 @@ class ShippingReplicaSynchronizer
|
||||
/** @var array<string,string> io_id => 出库单快递公司(工作平台按实际出库单取,发货明细可能停在作废前的旧分配) */
|
||||
private array $ioLc = [];
|
||||
|
||||
/** @var array<int|string,float> io_id => 出库单线上重量 weight(整单重量,聚水潭销售出库单口径;0 表示出库单没有) */
|
||||
private array $ioWeight = [];
|
||||
/** @var array<int|string,float> io_id => 出库单实称重量 f_weight(只存称过重的) */
|
||||
private array $ioFWeight = [];
|
||||
|
||||
/** @var array<string,string> io_id => 出库单发货仓 wms_co_id */
|
||||
private array $ioWms = [];
|
||||
|
||||
@ -246,6 +251,8 @@ class ShippingReplicaSynchronizer
|
||||
$this->ioDate = [];
|
||||
$this->ioLc = [];
|
||||
$this->ioWms = [];
|
||||
$this->ioWeight = [];
|
||||
$this->ioFWeight = [];
|
||||
$this->wmsNames = [];
|
||||
$this->pendingWarehouse = [];
|
||||
$this->ioOk = [];
|
||||
@ -595,7 +602,7 @@ class ShippingReplicaSynchronizer
|
||||
// 发货时间 = 出库单 io_date(工作平台口径,样本 400/400 一致),缺失时回落发货明细时间
|
||||
$shipTime = ($io !== '' ? $this->ts($this->ioDate[$io] ?? null) : null)
|
||||
?? $this->ts($r['send_time'] ?? null) ?? $this->ts($r['jo_send'] ?? null);
|
||||
$pkg = $this->pkgFromExpress($r, $tracking, $shipTime);
|
||||
$pkg = $this->withIoWeight($io, $this->pkgFromExpress($r, $tracking, $shipTime));
|
||||
$pkg = $this->withOutboundCarrier($io, $r, $pkg, $pkgId);
|
||||
$pkg = $this->withViewCarrier($io, $pkg, $pkgId);
|
||||
$pkg['platform'] = $this->platformOf($io, $pkg['platform']);
|
||||
@ -818,7 +825,7 @@ class ShippingReplicaSynchronizer
|
||||
}
|
||||
$items = $this->packageItems($io, $viewItems, $byOid, $bySo, $dws, $oid, $so, $lid);
|
||||
$shipTime = ($io !== '' ? $this->ts($this->ioDate[$io] ?? null) : null) ?? $this->ts($r['send_date'] ?? null);
|
||||
$pkg = $this->pkgFromOrder($r, $tracking, $shipTime);
|
||||
$pkg = $this->withIoWeight($io, $this->pkgFromOrder($r, $tracking, $shipTime));
|
||||
$pkg['platform'] = $this->platformOf($io, $pkg['platform']);
|
||||
$pkg = $this->withViewAddr($io, $pkg);
|
||||
$pkg = $this->withViewCarrier($io, $pkg, $pkgId);
|
||||
@ -1020,7 +1027,7 @@ class ShippingReplicaSynchronizer
|
||||
return 0;
|
||||
}
|
||||
$step = max(5000, (int) ceil(($hi - $lo + 1) / 400));
|
||||
$cols = 'io_id, o_id, so_id, l_id, io_date, pay_date, shop_name, labels, status, logistics_company, weight, '
|
||||
$cols = 'io_id, o_id, so_id, l_id, io_date, pay_date, shop_name, labels, status, logistics_company, weight, f_weight, '
|
||||
. 'buyer_message, receiver_state, receiver_city, receiver_district, receiver_phone';
|
||||
|
||||
$total = 0;
|
||||
@ -1032,7 +1039,7 @@ class ShippingReplicaSynchronizer
|
||||
$page = 0;
|
||||
for ($a = $lo; $a <= $hi; $a += $step) {
|
||||
$page++;
|
||||
$rows = $this->selectWithReconnect(
|
||||
$rows = $this->selectOutbound(
|
||||
"SELECT {$cols} FROM " . self::OUTBOUND . ' WHERE Id >= ? AND Id < ? AND io_date >= ? AND io_date < ? ORDER BY Id',
|
||||
[$a, $a + $step, $fromS, $toS]
|
||||
);
|
||||
@ -1045,6 +1052,7 @@ class ShippingReplicaSynchronizer
|
||||
continue;
|
||||
}
|
||||
$seen[$io] = true;
|
||||
$this->rememberIoWeight($io, $r);
|
||||
$lid = trim((string) ($r['l_id'] ?? ''));
|
||||
// 无单号出库单一般跳过;但"无需物流"的虚拟发货(信誉/补差/运费等)工作平台收在自动发货仓、快递单号为空,需补吐
|
||||
$virtual = trim((string) ($r['logistics_company'] ?? '')) === self::NO_LOGISTICS;
|
||||
@ -1102,7 +1110,7 @@ class ShippingReplicaSynchronizer
|
||||
$this->repeatedOutboundSources[$previous[$so]] = true;
|
||||
$this->repeatedOutboundSources[$pkgId] = true;
|
||||
}
|
||||
$pkg = $this->pkgFromOutbound($r, $tracking !== '' ? $tracking : null, $this->ts($r['io_date'] ?? null), $h, $so);
|
||||
$pkg = $this->withIoWeight($io, $this->pkgFromOutbound($r, $tracking !== '' ? $tracking : null, $this->ts($r['io_date'] ?? null), $h, $so));
|
||||
$pkg = $this->withViewAddr($io, $pkg);
|
||||
$pkg = $this->withViewCarrier($io, $pkg, $pkgId);
|
||||
if (trim((string) ($pkg['express_name1'] ?? '')) === self::NO_LOGISTICS) {
|
||||
@ -1382,6 +1390,7 @@ class ShippingReplicaSynchronizer
|
||||
// 进而误用同交易单号其它包裹的运费和快递名称。
|
||||
$f = $freight[$so . '|' . $tk] ?? ($freight[$so . '|'] ?? []);
|
||||
$hasFreight = isset($f['cost']);
|
||||
[$pkg['pkg_online'], $pkg['pkg_actual']] = $this->packageWeights($pkg, $items, $skus);
|
||||
// 聚水潭按「交易单号+单号」返回运费合计。同单号补吐使用本包裹重量,合计只留在首包。
|
||||
// 已取到上游合计时继续标记运费完整,以免重跑时把旧的重复运费带回来。
|
||||
if ($sameTrackAgain) {
|
||||
@ -1485,9 +1494,11 @@ class ShippingReplicaSynchronizer
|
||||
? round((float) $it['vol'] * (($it['volume_is_total'] ?? false) ? 1 : max($qty, 0)), 4)
|
||||
: $detailVol;
|
||||
|
||||
$pkgWeight = (float) ($pkg['weight'] ?? 0) ?: null;
|
||||
// 线上重量、实称重量都是整单(包裹)重量,只写在包裹第一行(聚水潭销售出库单口径)
|
||||
$pkgOnline = array_key_exists('pkg_online', $pkg) ? $pkg['pkg_online'] : $onlineW;
|
||||
$pkgActual = array_key_exists('pkg_actual', $pkg) ? $pkg['pkg_actual'] : null;
|
||||
$freightW = (float) ($f['w'] ?? 0);
|
||||
$settleW = $this->skipFreight ? null : ($freightW > 0 ? $freightW : ($pkgWeight ?: $onlineW));
|
||||
$settleW = $this->skipFreight ? null : ($freightW > 0 ? $freightW : ($pkgActual ?: $pkgOnline));
|
||||
|
||||
return $this->cleanPayload([
|
||||
'express_no' => $pkg['tracking'],
|
||||
@ -1514,8 +1525,8 @@ class ShippingReplicaSynchronizer
|
||||
'receiver_name' => $pkg['r_name'],
|
||||
'receiver_mobile' => $pkg['r_mobile'],
|
||||
'receiver_phone' => $pkg['r_phone'],
|
||||
'online_weight' => $onlineW,
|
||||
'actual_weight' => $first ? $pkgWeight : null,
|
||||
'online_weight' => $first ? $pkgOnline : null,
|
||||
'actual_weight' => $first ? $pkgActual : null,
|
||||
'length_cm' => $L,
|
||||
'width_cm' => $W,
|
||||
'height_cm' => $H,
|
||||
@ -1634,6 +1645,74 @@ class ShippingReplicaSynchronizer
|
||||
$this->pendingWarehouse = [];
|
||||
}
|
||||
|
||||
/** 出库单行里的整单重量:线上重量 weight、实称重量 f_weight(没称重为 0/空)。 */
|
||||
private function rememberIoWeight(string $io, array $r): void
|
||||
{
|
||||
if ($io === '' || ! array_key_exists('weight', $r)) {
|
||||
return;
|
||||
}
|
||||
$w = (float) ($r['weight'] ?? 0);
|
||||
$fw = (float) ($r['f_weight'] ?? 0);
|
||||
// 只存标量:窗口同步一次预载几十万张出库单,嵌套数组内存约是标量的十倍
|
||||
$this->ioWeight[$io] = $w > 0 ? round($w, 4) : 0.0;
|
||||
if ($fw > 0) {
|
||||
$this->ioFWeight[$io] = round($fw, 4);
|
||||
} else {
|
||||
unset($this->ioFWeight[$io]);
|
||||
}
|
||||
}
|
||||
|
||||
/** 包裹对应的出库单有重量时,带上整单线上/实称重量。 */
|
||||
private function withIoWeight(string $io, array $pkg): array
|
||||
{
|
||||
if ($io !== '' && isset($this->ioWeight[$io])) {
|
||||
$pkg['io_weight'] = $this->ioWeight[$io] > 0 ? $this->ioWeight[$io] : null;
|
||||
$pkg['io_fweight'] = $this->ioFWeight[$io] ?? null;
|
||||
$pkg['has_io_weight'] = true;
|
||||
}
|
||||
|
||||
return $pkg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 包裹整单重量 [线上重量, 实称重量]:
|
||||
* - 有出库单:线上 = 出库单 weight,实称 = 出库单 f_weight(没称重为空,不拿预估重量冒充实称);
|
||||
* - 没有出库单:线上 = 各商品单重×数量之和(没有单重时用发货明细重量),实称 = 发货明细 f_weight。
|
||||
*
|
||||
* @return array{0:?float,1:?float}
|
||||
*/
|
||||
private function packageWeights(array $pkg, array $items, array $skus): array
|
||||
{
|
||||
if (! empty($pkg['has_io_weight'])) {
|
||||
return [$pkg['io_weight'] ?? null, $pkg['io_fweight'] ?? null];
|
||||
}
|
||||
$sum = 0.0;
|
||||
foreach ($items as $it) {
|
||||
$sk = $skus[mb_strtolower(trim((string) ($it['sku'] ?? '')))] ?? [];
|
||||
$w = (float) (($it['weight'] ?? null) ?: ($sk['weight'] ?? 0));
|
||||
$qty = (float) ($it['qty'] ?? 0);
|
||||
$sum += $w > 0 ? $w * ($qty > 0 ? $qty : 1) : 0;
|
||||
}
|
||||
$pkgW = (float) ($pkg['weight'] ?? 0);
|
||||
$fw = (float) ($pkg['f_weight'] ?? 0);
|
||||
|
||||
return [$sum > 0 ? round($sum, 4) : ($pkgW > 0 ? round($pkgW, 4) : null), $fw > 0 ? round($fw, 4) : null];
|
||||
}
|
||||
|
||||
/** 查出库单:副本库没有 weight/f_weight 列时退回不带重量的查询(不影响其它字段)。 */
|
||||
private function selectOutbound(string $sql, array $bindings): array
|
||||
{
|
||||
try {
|
||||
return $this->selectWithReconnect($sql, $bindings);
|
||||
} catch (Throwable $e) {
|
||||
if (! $this->isMissingColumnError($e) || ! str_contains($sql, 'f_weight')) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $this->selectWithReconnect(str_replace(['f_weight, ', ', f_weight'], ['', ''], $sql), $bindings);
|
||||
}
|
||||
}
|
||||
|
||||
/** 发货明细表行 → 包裹级字段(省市区从地址拆) */
|
||||
private function pkgFromExpress(array $r, ?string $tracking, ?string $shipTime): array
|
||||
{
|
||||
@ -1659,6 +1738,7 @@ class ShippingReplicaSynchronizer
|
||||
'r_phone' => null,
|
||||
'buyer_memo' => $r['buyer_message'] ?? null,
|
||||
'weight' => $r['weight'] ?? null,
|
||||
'f_weight' => $r['f_weight'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
@ -1973,8 +2053,8 @@ class ShippingReplicaSynchronizer
|
||||
/** 窗口同步按 io_date 一次预载,避免对无可用索引的 l_id/o_id 反复执行 IN 查询。 */
|
||||
private function preloadOutboundMap(string $fromS, string $toS): void
|
||||
{
|
||||
$sql = 'SELECT l_id, o_id, io_id, io_date, status, logistics_company, wms_co_id FROM ' . self::OUTBOUND . ' WHERE io_date >= ? AND io_date < ?';
|
||||
$this->outboundMap = $this->mapOutboundRows($this->selectWithReconnect($sql, [$fromS, $toS]));
|
||||
$sql = 'SELECT l_id, o_id, io_id, io_date, status, logistics_company, wms_co_id, weight, f_weight FROM ' . self::OUTBOUND . ' WHERE io_date >= ? AND io_date < ?';
|
||||
$this->outboundMap = $this->mapOutboundRows($this->selectOutbound($sql, [$fromS, $toS]));
|
||||
}
|
||||
|
||||
/** 出库单行 -> 映射(单号/订单/单号+订单 三种定位,作废重出按有效优先、io_date 最新择优)。 */
|
||||
@ -2020,6 +2100,7 @@ class ShippingReplicaSynchronizer
|
||||
if ($wms !== '' && $wms !== '0') {
|
||||
$this->ioWms[$rio] = $wms;
|
||||
}
|
||||
$this->rememberIoWeight($rio, $r);
|
||||
$l = ltrim(trim((string) ($r['l_id'] ?? '')), '@');
|
||||
$o = (string) ($r['o_id'] ?? '');
|
||||
$m['ioL'][$rio] = $l;
|
||||
@ -2066,7 +2147,7 @@ class ShippingReplicaSynchronizer
|
||||
}
|
||||
$q = array_keys($q);
|
||||
$ph = implode(',', array_fill(0, count($q), '?'));
|
||||
foreach ($this->selectWithReconnect('SELECT l_id, io_id, io_date, status, o_id, logistics_company, wms_co_id FROM ' . self::OUTBOUND . " WHERE l_id IN ({$ph})", $q) as $r) {
|
||||
foreach ($this->selectOutbound('SELECT l_id, io_id, io_date, status, o_id, logistics_company, wms_co_id, weight, f_weight FROM ' . self::OUTBOUND . " WHERE l_id IN ({$ph})", $q) as $r) {
|
||||
$this->addIoRow($map, $r, true, false);
|
||||
}
|
||||
}
|
||||
@ -2075,7 +2156,7 @@ class ShippingReplicaSynchronizer
|
||||
continue;
|
||||
}
|
||||
$ph = implode(',', array_fill(0, count($chunk), '?'));
|
||||
foreach ($this->selectWithReconnect('SELECT o_id, io_id, io_date, l_id, status, logistics_company, wms_co_id FROM ' . self::OUTBOUND . " WHERE o_id IN ({$ph})", $chunk) as $r) {
|
||||
foreach ($this->selectOutbound('SELECT o_id, io_id, io_date, l_id, status, logistics_company, wms_co_id, weight, f_weight FROM ' . self::OUTBOUND . " WHERE o_id IN ({$ph})", $chunk) as $r) {
|
||||
$this->addIoRow($map, $r, false, true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,6 +33,9 @@ class ShippingMeasuredWeightImporter
|
||||
}
|
||||
}, 3);
|
||||
|
||||
// 核算运费用称重重量:导入后这些快递的核算结果从核算起始日起重算
|
||||
\Modules\EcommerceFinance\Services\Freight\ShippingFreightService::queueMeasuredCarriers(array_column($rows['weights'], 'carrier_name'));
|
||||
|
||||
return ['source_rows' => $rows['source_rows'], 'keys' => count($rows['weights'])];
|
||||
}
|
||||
|
||||
|
||||
@ -31,13 +31,16 @@ class ShippingFreightServiceTest extends FeatureTestCase
|
||||
// 10 月历史数据使用当时生效的报价
|
||||
$row(3, 1, '2026-10-01 00:00:00', ['online_weight' => 4]),
|
||||
$row(5, 1, '2026-09-30 23:59:59', ['online_weight' => 4]),
|
||||
// 核算起始日(9-01)之前的不核算
|
||||
$row(6, 1, '2026-08-31 23:59:59', ['online_weight' => 4]),
|
||||
// 报价管理里没有的快递
|
||||
$row(4, 1, '2026-11-12 09:00:00', ['online_weight' => 4, 'express_name1' => '某某快递']),
|
||||
]);
|
||||
|
||||
$res = app(ShippingFreightService::class)->recalculate('2026-09-30', '2026-12-01');
|
||||
$res = app(ShippingFreightService::class)->recalculate('2026-08-01', '2026-12-01');
|
||||
|
||||
$this->assertSame(['packages' => 4, 'ok' => 3], $res);
|
||||
// 9-30 的包裹也核算了,但新北中通 9 月没有生效报价
|
||||
$this->assertSame(['packages' => 5, 'ok' => 3], $res);
|
||||
$r1 = $db->table('jst_oms_shipping_freight')->where('source_id', 1)->sole();
|
||||
$this->assertSame('QB-901', $r1->quote_version);
|
||||
$this->assertEquals(8.75, $r1->freight);
|
||||
@ -49,7 +52,8 @@ class ShippingFreightServiceTest extends FeatureTestCase
|
||||
$r3 = $db->table('jst_oms_shipping_freight')->where('source_id', 3)->sole();
|
||||
$this->assertSame('QB-901', $r3->quote_version);
|
||||
$this->assertEquals(6.3, $r3->freight);
|
||||
$this->assertSame(0, $db->table('jst_oms_shipping_freight')->where('source_id', 5)->count());
|
||||
$this->assertSame('发货日没有生效的报价', $db->table('jst_oms_shipping_freight')->where('source_id', 5)->value('status'));
|
||||
$this->assertSame(0, $db->table('jst_oms_shipping_freight')->where('source_id', 6)->count());
|
||||
$this->assertSame('报价管理里没有「某某快递」的报价', $db->table('jst_oms_shipping_freight')->where('source_id', 4)->value('status'));
|
||||
|
||||
// 发货数据页面:合并口径每个包裹带核算列,明细口径只在包裹第一行
|
||||
@ -66,6 +70,70 @@ class ShippingFreightServiceTest extends FeatureTestCase
|
||||
$this->assertArrayNotHasKey('qc_freight', $detail[1]);
|
||||
}
|
||||
|
||||
public function test_imported_measured_weights_take_priority_when_every_barcode_is_measured(): void
|
||||
{
|
||||
$this->version('QB-911', '新北中通', '2026-10-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]);
|
||||
$db = DB::connection('business_mirror');
|
||||
$row = static fn (int $sid, int $sku, string $barcode, array $extra = []) => $extra + [
|
||||
'source_id' => $sid, 'sku_source_id' => $sku, 'express_no' => 'ZT' . $sid, 'express_name1' => '新北中通',
|
||||
'warehouse_name' => '常州简尚家居有限公司', 'receiver_state' => '江苏省', 'receiver_city' => '南京市',
|
||||
'ship_time' => '2026-11-05 10:00:00', 'pkg_count' => '1/1', 'barcode' => $barcode, 'qty' => 1,
|
||||
'online_weight' => null, 'actual_weight' => null,
|
||||
];
|
||||
$db->table('jst_oms_shipping')->insert([
|
||||
// 一个包裹两个条码都有称重:A 6kg×2 件 + B 3kg → 15kg;实称重量 9kg 不用
|
||||
$row(21, 1, 'BC-A', ['qty' => 2, 'online_weight' => 8, 'actual_weight' => 9]),
|
||||
$row(21, 2, 'bc-b'),
|
||||
// 只有部分条码有称重:整单重量拆不出单件,不用称重,照旧按实称重量
|
||||
$row(23, 1, 'BC-A', ['online_weight' => 10, 'actual_weight' => 11]),
|
||||
$row(23, 2, 'BC-X'),
|
||||
// 称重只登记给别的快递:照旧按实称重量
|
||||
$row(22, 1, 'BC-C', ['online_weight' => 4, 'actual_weight' => 5]),
|
||||
]);
|
||||
DB::table('shipping_measured_weights')->insert([
|
||||
['company_id' => company_id() ?? 1, 'carrier_name' => '新北中通', 'barcode' => 'BC-A', 'weight_kg' => 6, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()],
|
||||
['company_id' => company_id() ?? 1, 'carrier_name' => '新北中通', 'barcode' => 'BC-B', 'weight_kg' => 3, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()],
|
||||
['company_id' => company_id() ?? 1, 'carrier_name' => '武进韵达', 'barcode' => 'BC-C', 'weight_kg' => 1, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()],
|
||||
]);
|
||||
|
||||
app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01');
|
||||
|
||||
$r21 = $db->table('jst_oms_shipping_freight')->where('source_id', 21)->sole();
|
||||
$this->assertSame('称重重量', $r21->base_source);
|
||||
$this->assertEquals(15, $r21->base_weight);
|
||||
$r23 = $db->table('jst_oms_shipping_freight')->where('source_id', 23)->sole();
|
||||
$this->assertSame('实称重量', $r23->base_source);
|
||||
$this->assertEquals(11, $r23->base_weight);
|
||||
$r22 = $db->table('jst_oms_shipping_freight')->where('source_id', 22)->sole();
|
||||
$this->assertSame('实称重量', $r22->base_source);
|
||||
$this->assertEquals(5, $r22->base_weight);
|
||||
}
|
||||
|
||||
public function test_measured_weights_from_another_company_are_not_used_without_a_current_company(): void
|
||||
{
|
||||
// 命令行(calc-freight)没有当前公司:按 jushuitan.freight_company_id 取本公司称重,不能取到别家公司同条码的称重
|
||||
$this->version('QB-931', '新北中通', '2026-10-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]);
|
||||
DB::connection('business_mirror')->table('jst_oms_shipping')->insert([
|
||||
'source_id' => 31, 'sku_source_id' => 1, 'express_no' => 'ZT31', 'express_name1' => '新北中通',
|
||||
'warehouse_name' => '常州简尚家居有限公司', 'receiver_state' => '江苏省', 'ship_time' => '2026-11-05 10:00:00',
|
||||
'pkg_count' => '1/1', 'barcode' => 'BC-M', 'qty' => 1, 'online_weight' => 8, 'actual_weight' => 9,
|
||||
]);
|
||||
$other = (int) $this->company->id + 1000;
|
||||
DB::table('shipping_measured_weights')->insert([
|
||||
['company_id' => $this->company->id, 'carrier_name' => '新北中通', 'barcode' => 'BC-M', 'weight_kg' => 5, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()],
|
||||
['company_id' => $other, 'carrier_name' => '新北中通', 'barcode' => 'BC-M', 'weight_kg' => 12, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()],
|
||||
]);
|
||||
config(['jushuitan.freight_company_id' => $this->company->id]);
|
||||
\App\Models\Common\Company::forgetCurrent();
|
||||
$this->assertNull(company_id());
|
||||
|
||||
app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01');
|
||||
|
||||
$r = DB::connection('business_mirror')->table('jst_oms_shipping_freight')->where('source_id', 31)->sole();
|
||||
$this->assertSame('称重重量', $r->base_source);
|
||||
$this->assertEquals(5, $r->base_weight);
|
||||
}
|
||||
|
||||
public function test_quote_changes_recalculate_that_carrier_from_the_effective_date(): void
|
||||
{
|
||||
DB::connection('business_mirror')->table('jst_oms_shipping')->insert([
|
||||
|
||||
@ -532,7 +532,9 @@ class ShippingReplicaSynchronizerTest extends TestCase
|
||||
$this->assertNull($new->settle_freight);
|
||||
$this->assertNull($new->weight_kg);
|
||||
$this->assertNull($new->settle_weight_kg);
|
||||
$this->assertEquals(2, $new->actual_weight);
|
||||
// 发货明细 weight 是预估重量:记为线上重量,不能冒充实称(实称只取 f_weight)
|
||||
$this->assertEquals(2, $new->online_weight);
|
||||
$this->assertNull($new->actual_weight);
|
||||
|
||||
(new ShippingReplicaSynchronizer($factory))->sync(
|
||||
Carbon::parse('2026-09-04'), Carbon::parse('2026-09-05'),
|
||||
@ -1205,6 +1207,44 @@ class ShippingReplicaSynchronizerTest extends TestCase
|
||||
return ['same page' => [false], 'different pages' => [true]];
|
||||
}
|
||||
|
||||
public function test_package_weights_are_whole_package_from_the_sales_outbound_on_the_first_row_only(): void
|
||||
{
|
||||
// 聚水潭销售出库单:线上重量、实称重量都是整单重量。两件商品的包裹,重量只记在第一行;没称重时实称为空
|
||||
$db = DB::connection('business_mirror');
|
||||
$upstream = $this->createMock(UpstreamReadOnlyQuery::class);
|
||||
$upstream->method('select')->willReturnCallback(function (string $sql, array $bindings = []): array {
|
||||
if (str_contains($sql, 'MIN(Id) AS lo')) {
|
||||
return [['lo' => 1, 'hi' => 2]];
|
||||
}
|
||||
if (str_contains($sql, 'WHERE Id >= ?')) {
|
||||
return [
|
||||
['io_id' => '450', 'so_id' => 'trade-450', 'l_id' => 'W-450', 'io_date' => '2026-09-02 09:00:00', 'weight' => 49.3, 'f_weight' => 0],
|
||||
['io_id' => '451', 'so_id' => 'trade-451', 'l_id' => 'W-451', 'io_date' => '2026-09-02 10:00:00', 'weight' => 0.04, 'f_weight' => 0.28],
|
||||
];
|
||||
}
|
||||
if (str_contains($sql, 'SELECT io_id, sku_ids')) {
|
||||
return [
|
||||
['io_id' => '450', 'sku_ids' => 'SG-A', 'qty' => 1, 'type' => '0', 'volume' => 100, 'l' => 10, 'w' => 5, 'h' => 2],
|
||||
['io_id' => '450', 'sku_ids' => 'SG-B', 'qty' => 1, 'type' => '0', 'volume' => 100, 'l' => 10, 'w' => 5, 'h' => 2],
|
||||
['io_id' => '451', 'sku_ids' => 'PJ-1', 'qty' => 3, 'type' => '0', 'volume' => 10, 'l' => 1, 'w' => 1, 'h' => 1],
|
||||
];
|
||||
}
|
||||
if (str_contains($sql, 'FROM rpt.jst_sku')) {
|
||||
return [['sku_id' => 'SG-A', 'weight' => 30], ['sku_id' => 'SG-B', 'weight' => 30], ['sku_id' => 'PJ-1', 'weight' => 0.5]];
|
||||
}
|
||||
|
||||
return [];
|
||||
});
|
||||
$factory = $this->createMock(UpstreamReadOnlyQueryFactory::class);
|
||||
$factory->method('forSource')->willReturn($upstream);
|
||||
(new ShippingReplicaSynchronizer($factory))->sync(Carbon::parse('2026-09-01'), Carbon::parse('2026-09-05'), 2000, null, true);
|
||||
|
||||
$rows = fn (string $t) => $db->table('jst_oms_shipping')->where('express_no', $t)->orderBy('id')->get(['online_weight', 'actual_weight'])
|
||||
->map(fn ($r) => [$r->online_weight === null ? null : (float) $r->online_weight, $r->actual_weight === null ? null : (float) $r->actual_weight])->all();
|
||||
$this->assertSame([[49.3, null], [null, null]], $rows('W-450'));
|
||||
$this->assertSame([[0.04, 0.28]], $rows('W-451'));
|
||||
}
|
||||
|
||||
public function test_outbound_mapping_keeps_shared_tracking_orders_distinct_and_ignores_cancelled(): void
|
||||
{
|
||||
$upstream = $this->createMock(UpstreamReadOnlyQuery::class);
|
||||
@ -1626,6 +1666,22 @@ class ShippingReplicaSynchronizerTest extends TestCase
|
||||
$this->assertEquals(2.04, $w(8004, 1)->actual_weight);
|
||||
$this->assertEquals(3.0, $w(8005, 1)->online_weight);
|
||||
$this->assertEquals(3.0, $w(8005, 1)->actual_weight);
|
||||
|
||||
// 同一单号两张有效出库单(分批发货):各包裹保留按自己出库单写入的重量,不按单号统一覆盖
|
||||
$db->table('jst_oms_shipping')->insert([
|
||||
['source_id' => 2000009006, 'sku_source_id' => 1, 'ship_time' => '2026-10-20 10:00:00', 'express_no' => 'JDMULTI', 'online_weight' => 11.6, 'actual_weight' => null],
|
||||
['source_id' => 2000009007, 'sku_source_id' => 1, 'ship_time' => '2026-10-20 10:00:05', 'express_no' => 'JDMULTI', 'online_weight' => 3.78, 'actual_weight' => null],
|
||||
]);
|
||||
$upstream2 = $this->createMock(UpstreamReadOnlyQuery::class);
|
||||
$upstream2->method('select')->willReturnCallback(fn (string $sql): array => str_contains($sql, 'so.io_date >= ?') ? [
|
||||
['io_id' => 9006, 'l_id' => 'JDMULTI', 'io_date' => '2026-10-20 10:00:00', 'status' => '已出库', 'weight' => 11.6, 'f_weight' => null],
|
||||
['io_id' => 9007, 'l_id' => 'JDMULTI', 'io_date' => '2026-10-20 10:00:05', 'status' => '已出库', 'weight' => 3.78, 'f_weight' => null],
|
||||
] : []);
|
||||
$factory2 = $this->createMock(UpstreamReadOnlyQueryFactory::class);
|
||||
$factory2->method('forSource')->willReturn($upstream2);
|
||||
(new ShippingReplicaSynchronizer($factory2))->alignWithOutbound('2026-10-19 00:00:00', '2026-10-21 00:00:00');
|
||||
$this->assertEquals(11.6, $w(2000009006, 1)->online_weight);
|
||||
$this->assertEquals(3.78, $w(2000009007, 1)->online_weight);
|
||||
}
|
||||
|
||||
public function test_outbound_weight_alignment_does_not_silently_skip_weights_on_timeout(): void
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user