fix(发货核算): 补齐新版补丁21重量隔离与行总重处理 (#1641)

This commit is contained in:
limeng 2026-12-02 12:10:05 +08:00
parent 97d4d2a998
commit 501c973216
4 changed files with 86 additions and 16 deletions

View File

@ -410,7 +410,7 @@ class ShippingFreightService
$pt = strtotime($sids[$p][3]);
foreach ($sids as $sid => $e) {
$hasItems = $e[1] !== PHP_INT_MAX;
if ($sid === $p || ($hasItems && $e[2]) || abs(strtotime($e[3]) - $pt) > 10 * 86400) {
if ($sid === $p || ($hasItems && $e[2]) || abs(strtotime($e[3]) - $pt) > 86400) {
continue;
}
$primaryOf[$sid] = $p;

View File

@ -1879,7 +1879,8 @@ class ShippingReplicaSynchronizer
{
$qty = (float) ($it['qty'] ?? 0);
$skuW = (float) (($it['weight'] ?? null) ?: ($sk['weight'] ?? 0));
$onlineW = $skuW > 0 ? round($skuW * ($qty > 0 ? $qty : 1), 4) : null;
$lineTotal = ! empty($it['weight_is_total']) && ! empty($it['weight']);
$onlineW = $skuW > 0 ? round($lineTotal ? $skuW : $skuW * ($qty > 0 ? $qty : 1), 4) : null;
$L = ($it['l'] ?? null) ?: ($sk['l'] ?? null);
$W = ($it['w'] ?? null) ?: ($sk['w'] ?? null);
$H = ($it['h'] ?? null) ?: ($sk['h'] ?? null);
@ -2230,9 +2231,10 @@ class ShippingReplicaSynchronizer
$sum = 0.0;
foreach ($items as $it) {
$sk = $skus[mb_strtolower(trim((string) ($it['sku'] ?? '')))] ?? [];
$total = ! empty($it['weight_is_total']) && ! empty($it['weight']);
$w = (float) (($it['weight'] ?? null) ?: ($sk['weight'] ?? 0));
$qty = (float) ($it['qty'] ?? 0);
$sum += $w > 0 ? $w * ($qty > 0 ? $qty : 1) : 0;
$sum += $w > 0 ? ($total ? $w : $w * ($qty > 0 ? $qty : 1)) : 0;
}
$pkgW = (float) ($pkg['weight'] ?? 0);
$fw = (float) ($pkg['f_weight'] ?? 0);
@ -2931,7 +2933,8 @@ class ShippingReplicaSynchronizer
'shopid' => $a['shopid'], 'name' => $a['name'],
'qty' => $qty, 'amount' => null, 'warehouse' => $a['wh'],
'l' => $a['l'], 'w' => $a['w'], 'h' => $a['h'], 'vol' => $vol, 'volume_is_total' => true,
'weight' => $a['weight'], 'supplier' => $a['supplier'],
// 视图 weight 是整行合计重量,不能再乘这一行的商品数量。
'weight' => $a['weight'], 'weight_is_total' => true, 'supplier' => $a['supplier'],
];
}
@ -3758,8 +3761,8 @@ class ShippingReplicaSynchronizer
$hasW = $withWeight && array_key_exists('weight', $r);
$w = $hasW ? (float) ($r['weight'] ?? 0) : 0.0;
$fw = $hasW ? (float) ($r['f_weight'] ?? 0) : 0.0;
$candidate = [$rk, $d, $hasW, $w > 0 ? round($w, 4) : null, $fw > 0 ? round($fw, 4) : null, $io];
$so = trim((string) ($r['so_id'] ?? ''));
$candidate = [$rk, $d, $hasW, $w > 0 ? round($w, 4) : null, $fw > 0 ? round($fw, 4) : null, $io, $so];
if ($ok && $so !== '') {
$bySo[$so][$l] = trim((string) ($r['logistics_company'] ?? ''));
}
@ -3787,27 +3790,30 @@ class ShippingReplicaSynchronizer
// 同一单号多张有效出库单(分批发货)仍按各自出库单各记各的。
$itemSid = []; // 快递单号 => 最早一行有商品明细的 source_id
$this->db->table('jst_oms_shipping')
->select(['id', 'source_id', 'express_no'])
->select(['id', 'source_id', 'express_no', 'trade_no', 'ship_time'])
->where('ship_time', '>=', $mirrorFrom)->where('ship_time', '<', $toS)
->whereNotNull('express_no')->whereNotNull('barcode')->where('barcode', '<>', '')
->chunkById(20000, function ($chunk) use (&$itemSid, $best, $multiple): void {
foreach ($chunk as $row) {
$l = ltrim(trim((string) $row->express_no), '@');
if ($l !== '' && isset($best[$l]) && ! isset($multiple[$l]) && ! isset($itemSid[$l])) {
if ($l !== '' && isset($best[$l]) && ! isset($multiple[$l]) && ! isset($itemSid[$l])
&& $this->sameShipment($best[$l], (string) $row->trade_no, (string) $row->ship_time)) {
$itemSid[$l] = (int) $row->source_id;
}
}
}, 'id');
$fixTime = [];
$seenSid = []; // source_id => 1 已校正时间,2 已处理首行重量
$owner = []; // 单号 => 唯一的主包裹
$weighed = []; // 包裹或单号已经写入整包重量
$seenSid = []; // source_id => 已校正时间
$setWeight = []; // [id, online, actual]
$clearIds = []; // 不该带重量的行 -> 置空
$this->db->table('jst_oms_shipping')
->select(['id', 'source_id', 'express_no', 'ship_time', 'online_weight', 'actual_weight'])
->select(['id', 'source_id', 'express_no', 'trade_no', 'ship_time', 'online_weight', 'actual_weight'])
->where('ship_time', '>=', $mirrorFrom)->where('ship_time', '<', $toS)
->whereNotNull('express_no')
->chunkById(20000, function ($chunk) use (&$fixTime, &$seenSid, &$setWeight, &$clearIds, $best, $multiple, &$itemSid): void {
->chunkById(20000, function ($chunk) use (&$fixTime, &$seenSid, &$setWeight, &$clearIds, $best, $multiple, $itemSid, &$owner, &$weighed): void {
foreach ($chunk as $row) {
$sid = (int) $row->source_id;
$l = ltrim(trim((string) $row->express_no), '@');
@ -3815,11 +3821,11 @@ class ShippingReplicaSynchronizer
continue;
}
$b = $this->outboundForMirrorRow($sid, (string) $row->ship_time, $best[$l], $multiple[$l] ?? []);
if ($b === null) {
if ($b === null || (! isset($multiple[$l]) && ! $this->sameShipment($b, (string) $row->trade_no, (string) $row->ship_time))) {
continue;
}
if (! isset($seenSid[$sid])) {
$seenSid[$sid] = 1;
$seenSid[$sid] = true;
$diff = strtotime($b[1]) - strtotime((string) $row->ship_time);
if ($diff >= 60 && $diff <= 7 * 86400) {
$fixTime[$b[1]][] = $sid;
@ -3829,14 +3835,15 @@ class ShippingReplicaSynchronizer
continue;
}
$split = isset($multiple[$l]);
$own = $split ? $sid : ($itemSid[$l] ??= $sid);
if ($sid !== $own || $seenSid[$sid] === 2) {
$key = $split ? 's' . $sid : 'l' . $l;
$own = $split ? $sid : ($owner[$l] ??= ($itemSid[$l] ?? $sid));
if ($sid !== $own || isset($weighed[$key])) {
if ($row->online_weight !== null || $row->actual_weight !== null) {
$clearIds[] = (int) $row->id;
}
continue;
}
$seenSid[$sid] = 2;
$weighed[$key] = true;
$on = $row->online_weight === null ? null : round((float) $row->online_weight, 4);
$ac = $row->actual_weight === null ? null : round((float) $row->actual_weight, 4);
if ($on !== $b[3] || $ac !== $b[4]) {
@ -3844,7 +3851,7 @@ class ShippingReplicaSynchronizer
}
}
}, 'id');
unset($best, $multiple, $itemSid, $seenSid);
unset($best, $multiple, $itemSid, $owner, $weighed, $seenSid);
$nTime = 0;
foreach ($fixTime as $new => $ids) {
@ -3961,6 +3968,18 @@ class ShippingReplicaSynchronizer
return $n;
}
/** 单号复用且交易单号不同、发货日期相差超过一天时,不套用另一票的重量。 */
private function sameShipment(array $outbound, string $trade, string $shipTime): bool
{
$so = (string) ($outbound[6] ?? '');
$trade = trim($trade);
if ($so === '' || $trade === '' || $so === $trade) {
return true;
}
return abs(strtotime((string) $outbound[1]) - strtotime($shipTime)) <= 86400;
}
private function outboundForMirrorRow(int $sourceId, string $shipTime, array $best, array $multiple): ?array
{
if ($sourceId >= self::OUTBOUND_OFFSET && $sourceId < self::PURCHASE_OUT_OFFSET) {

View File

@ -275,6 +275,24 @@ class ShippingFreightServiceTest extends FeatureTestCase
$this->assertEquals(7.0, $details['ZTX']['qc_freight']);
}
public function test_reused_waybill_on_different_days_is_not_folded_into_one_package(): void
{
$this->version('QB-TEST-REUSE', '新北中通', '2026-10-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]);
$base = ['sku_source_id' => 1, 'express_no' => 'TEST-REUSED', 'express_name1' => '新北中通',
'warehouse_name' => '测试仓', 'receiver_state' => '江苏省', 'qty' => 1, 'pkg_count' => '1/1'];
$db = DB::connection('business_mirror');
$db->table('jst_oms_shipping')->insert([
$base + ['source_id' => 801, 'trade_no' => 'TEST-ORDER-A', 'ship_time' => '2026-11-01 10:00:00', 'barcode' => 'TEST-A', 'online_weight' => 4],
$base + ['source_id' => 802, 'trade_no' => 'TEST-ORDER-B', 'ship_time' => '2026-11-03 10:00:00', 'barcode' => 'TEST-B', 'online_weight' => null],
]);
app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01');
$first = $db->table('jst_oms_shipping_freight')->where('source_id', 801)->sole();
$second = $db->table('jst_oms_shipping_freight')->where('source_id', 802)->sole();
$this->assertEquals(7, $first->freight);
$this->assertNull($second->freight);
$this->assertSame('缺少有效计费重量', $second->status);
}
public function test_numeric_waybills_and_trade_numbers_are_queried_as_strings(): void
{
$this->version('QB-922', '新北中通', '2026-10-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]);

View File

@ -2101,6 +2101,39 @@ class ShippingReplicaSynchronizerTest extends TestCase
$this->assertEquals(1.0, $w(8105)->online_weight);
}
public function test_reused_waybill_does_not_take_a_later_orders_weight(): void
{
$db = DB::connection('business_mirror');
$base = ['sku_source_id' => 1, 'express_no' => 'TEST-REUSED-WAYBILL', 'actual_weight' => null];
$db->table('jst_oms_shipping')->insert([
$base + ['source_id' => 8201, 'ship_time' => '2026-11-11 15:00:00', 'trade_no' => 'TEST-OLD-ORDER', 'barcode' => 'TEST-ITEM-A', 'online_weight' => 700],
$base + ['source_id' => 8202, 'ship_time' => '2026-11-22 10:00:00', 'trade_no' => 'TEST-NEW-ORDER', 'barcode' => 'TEST-ITEM-B', 'online_weight' => 690],
]);
$upstream = $this->createMock(UpstreamReadOnlyQuery::class);
$upstream->method('select')->willReturnCallback(fn (string $sql): array => str_contains($sql, 'so.io_date >= ?') ? [
['io_id' => 9210, 'so_id' => 'TEST-NEW-ORDER', 'l_id' => 'TEST-REUSED-WAYBILL', 'io_date' => '2026-11-22 10:00:00', 'status' => '已出库', 'weight' => 115, 'f_weight' => null],
] : []);
$factory = $this->createMock(UpstreamReadOnlyQueryFactory::class);
$factory->method('forSource')->willReturn($upstream);
(new ShippingReplicaSynchronizer($factory))->alignWithOutbound('2026-11-15 00:00:00', '2026-12-01 00:00:00');
$this->assertEquals(700, $db->table('jst_oms_shipping')->where('source_id', 8201)->value('online_weight'));
$this->assertEquals(115, $db->table('jst_oms_shipping')->where('source_id', 8202)->value('online_weight'));
}
public function test_view_line_total_weight_is_not_multiplied_by_quantity(): void
{
$sync = new ShippingReplicaSynchronizer($this->createMock(UpstreamReadOnlyQueryFactory::class));
$method = new \ReflectionMethod($sync, 'packageWeights');
$this->assertSame([2500.0, null], $method->invoke($sync, [], [
['sku' => 'TEST-LINE', 'qty' => 500, 'weight' => 2500, 'weight_is_total' => true],
], []));
$this->assertSame([2500.0, null], $method->invoke($sync, [], [
['sku' => 'TEST-LINE', 'qty' => 500],
], ['test-line' => ['weight' => 5]]));
}
public function test_outbound_weight_alignment_does_not_silently_skip_weights_on_timeout(): void
{
$db = DB::connection('business_mirror');