fix(发货核算): 分批出库重量按出库单只计一次 (#1650)

This commit is contained in:
limeng 2026-12-02 13:56:51 +08:00
parent 87c7338203
commit 75455e4a7e
2 changed files with 55 additions and 4 deletions

View File

@ -3804,6 +3804,7 @@ class ShippingReplicaSynchronizer
}, 'id');
$fixTime = [];
$orphans = []; // 快递单号 => [行 id]
$owner = []; // 单号 => 唯一的主包裹
$weighed = []; // 包裹或单号已经写入整包重量
$seenSid = []; // source_id => 已校正时间
@ -3813,7 +3814,7 @@ class ShippingReplicaSynchronizer
->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, &$owner, &$weighed): void {
->chunkById(20000, function ($chunk) use (&$fixTime, &$seenSid, &$setWeight, &$clearIds, &$orphans, $best, $multiple, $itemSid, &$owner, &$weighed): void {
foreach ($chunk as $row) {
$sid = (int) $row->source_id;
$l = ltrim(trim((string) $row->express_no), '@');
@ -3821,6 +3822,9 @@ class ShippingReplicaSynchronizer
continue;
}
$b = $this->outboundForMirrorRow($sid, (string) $row->ship_time, $best[$l], $multiple[$l] ?? []);
if ($b === null && isset($multiple[$l]) && ($row->online_weight !== null || $row->actual_weight !== null)) {
$orphans[$l][] = (int) $row->id; // 分批出库的单号里对不上任何一张出库单的行
}
if ($b === null || (! isset($multiple[$l]) && ! $this->sameShipment($b, (string) $row->trade_no, (string) $row->ship_time))) {
continue;
}
@ -3835,8 +3839,9 @@ class ShippingReplicaSynchronizer
continue;
}
$split = isset($multiple[$l]);
$key = $split ? 's' . $sid : 'l' . $l;
$own = $split ? $sid : ($owner[$l] ??= ($itemSid[$l] ?? $sid));
// 分批出库:每张出库单的重量只记一次(同一张出库单被两个包裹行重复对上时只记第一个)
$key = $split ? 'i' . $b[5] : 'l' . $l;
$own = $split ? ($owner[$key] ??= $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;
@ -3851,7 +3856,17 @@ class ShippingReplicaSynchronizer
}
}
}, 'id');
unset($best, $multiple, $itemSid, $owner, $weighed, $seenSid);
// 分批出库的每张出库单都已记上重量,其余对不上的行(多出来的空行/旧包裹)不能再带重量
foreach ($orphans as $l => $ids) {
$all = true;
foreach (array_keys($multiple[$l]) as $io) {
$all = $all && isset($weighed['i' . $io]);
}
if ($all) {
array_push($clearIds, ...$ids);
}
}
unset($best, $multiple, $itemSid, $owner, $weighed, $seenSid, $orphans);
$nTime = 0;
foreach ($fixTime as $new => $ids) {

View File

@ -2122,6 +2122,42 @@ class ShippingReplicaSynchronizerTest extends TestCase
$this->assertEquals(115, $db->table('jst_oms_shipping')->where('source_id', 8202)->value('online_weight'));
}
public function test_split_shipments_keep_each_outbound_weight_once(): void
{
$db = DB::connection('business_mirror');
$row = static fn (int $sid, string $no, string $time, ?string $bc, float $w) => ['source_id' => $sid, 'sku_source_id' => 1,
'ship_time' => $time, 'express_no' => $no, 'trade_no' => 'T' . $no, 'barcode' => $bc, 'online_weight' => $w, 'actual_weight' => null];
$db->table('jst_oms_shipping')->insert([
// 同一订单分两张出库单;第二张被两个包裹行重复对上
$row(8301, 'TEST-SPLIT-WAYBILL-A', '2026-11-22 08:40:53', 'TEST-SKU-A', 5.95),
$row(8302, 'TEST-SPLIT-WAYBILL-A', '2026-11-22 15:10:31', 'TEST-SKU-B', 22.4),
$row(8303, 'TEST-SPLIT-WAYBILL-A', '2026-11-22 15:10:31', 'TEST-SKU-B', 22.4),
// 同一订单分两张出库单;另有一行没有商品、时间对不上任何出库单,还带着 3.8
$row(8304, 'TEST-SPLIT-WAYBILL-B', '2026-11-25 10:22:22', null, 3.8),
$row(8305, 'TEST-SPLIT-WAYBILL-B', '2026-11-25 15:35:31', 'TEST-SKU-C', 1.9),
$row(8306, 'TEST-SPLIT-WAYBILL-B', '2026-11-25 16:36:35', 'TEST-SKU-C', 1.9),
]);
$upstream = $this->createMock(UpstreamReadOnlyQuery::class);
$upstream->method('select')->willReturnCallback(fn (string $sql): array => str_contains($sql, 'so.io_date >= ?') ? [
['io_id' => 96001, 'so_id' => 'TTEST-SPLIT-WAYBILL-A', 'l_id' => 'TEST-SPLIT-WAYBILL-A', 'io_date' => '2026-11-22 08:40:53', 'status' => '已出库', 'weight' => 5.95, 'f_weight' => null],
['io_id' => 96002, 'so_id' => 'TTEST-SPLIT-WAYBILL-A', 'l_id' => 'TEST-SPLIT-WAYBILL-A', 'io_date' => '2026-11-22 15:10:31', 'status' => '已出库', 'weight' => 22.4, 'f_weight' => null],
['io_id' => 96003, 'so_id' => 'TTEST-SPLIT-WAYBILL-B', 'l_id' => 'TEST-SPLIT-WAYBILL-B', 'io_date' => '2026-11-25 15:35:31', 'status' => '已出库', 'weight' => 1.9, 'f_weight' => null],
['io_id' => 96004, 'so_id' => 'TTEST-SPLIT-WAYBILL-B', 'l_id' => 'TEST-SPLIT-WAYBILL-B', 'io_date' => '2026-11-25 16:36:35', 'status' => '已出库', 'weight' => 1.9, 'f_weight' => null],
] : []);
$factory = $this->createMock(UpstreamReadOnlyQueryFactory::class);
$factory->method('forSource')->willReturn($upstream);
(new ShippingReplicaSynchronizer($factory))->alignWithOutbound('2026-11-01 00:00:00', '2026-12-01 00:00:00');
$w = fn (int $sid) => $db->table('jst_oms_shipping')->where('source_id', $sid)->value('online_weight');
$this->assertEquals(5.95, $w(8301));
$this->assertEquals(22.4, $w(8302));
$this->assertNull($w(8303));
$this->assertNull($w(8304));
$this->assertEquals(1.9, $w(8305));
$this->assertEquals(1.9, $w(8306));
}
public function test_view_line_total_weight_is_not_multiplied_by_quantity(): void
{
$sync = new ShippingReplicaSynchronizer($this->createMock(UpstreamReadOnlyQueryFactory::class));