diff --git a/modules/EcommerceFinance/Services/Jushuitan/ShippingReplicaSynchronizer.php b/modules/EcommerceFinance/Services/Jushuitan/ShippingReplicaSynchronizer.php index eadd753c..319a4abd 100644 --- a/modules/EcommerceFinance/Services/Jushuitan/ShippingReplicaSynchronizer.php +++ b/modules/EcommerceFinance/Services/Jushuitan/ShippingReplicaSynchronizer.php @@ -1001,6 +1001,43 @@ class ShippingReplicaSynchronizer return $total; } + /** + * 采购退货出库的商品行:以采购退货明细为准。 + * 发货视图对采购退货单会重复出很多行、拼接条码还会被截断,按视图累加会把数量放大(如 93 件变成 1083 件)。 + * 明细里有的条码:沿用视图同条码的名称/长宽高等字段,数量取明细;视图没有的条码按明细补一行; + * 视图里多出来的条码(截断残码、重复行)丢弃。明细为空时仍按视图。 + * + * @param list> $viewItems + * @param array $poRows 小写条码 => 明细 + * @return list> + */ + private function purchaseOutItems(array $viewItems, array $poRows, ?string $warehouse): array + { + $byKey = []; + foreach ($viewItems as $it) { + $k = mb_strtolower(trim((string) ($it['sku'] ?? ''))); + if ($k !== '' && ! isset($byKey[$k])) { + $byKey[$k] = $it; + } + } + $out = []; + foreach ($poRows as $k => $p) { + $qty = (int) round((float) ($p['qty'] ?? 0)); + if ($qty <= 0) { + continue; + } + $it = $byKey[(string) $k] ?? [ + 'id' => null, 'sku' => $p['sku'], 'iid' => null, 'shopid' => null, 'name' => $p['name'] ?? null, + 'amount' => null, 'warehouse' => $warehouse, + 'l' => null, 'w' => null, 'h' => null, 'vol' => null, 'weight' => null, 'supplier' => null, + ]; + $it['qty'] = $qty; + $out[] = $it; + } + + return $out === [] ? $viewItems : $out; + } + /** * 采购退货出库(退回供应商):工作平台的发货数据视图收录了其中走物流的那部分,按视图补吐。 * 无交易单号/店铺/平台;仓库取视图(发货仓),收货信息取采购退货单。 @@ -1036,23 +1073,27 @@ class ShippingReplicaSynchronizer if ($pick !== []) { $ioIds = array_map('strval', array_keys($pick)); $viewItems = $this->loadViewItems($ioIds); - // 采购退货:每个条码数量取采购退货明细(jst_purchaseoutItem),体积=单件长宽高×数量(对齐工作平台) - $poQty = []; + // 采购退货:商品明细以采购退货明细(jst_purchaseoutItem)为准,体积=单件长宽高×数量(对齐工作平台) + $poItems = []; foreach (array_chunk($ioIds, 800) as $ck) { $ph = implode(',', array_fill(0, count($ck), '?')); - foreach ($this->selectWithReconnect("SELECT io_id, sku_id, qty FROM rpt.jst_purchaseoutItem WHERE io_id IN ({$ph})", $ck) as $pr) { - $pk = mb_strtolower(trim((string) ($pr['sku_id'] ?? ''))); - $poQty[(string) $pr['io_id']][$pk] = ($poQty[(string) $pr['io_id']][$pk] ?? 0) + (float) ($pr['qty'] ?? 0); - } - } - foreach ($viewItems as $vio => $vitems) { - foreach ($vitems as $vi => $it) { - $pq = $poQty[(string) $vio][mb_strtolower((string) $it['sku'])] ?? null; - if ($pq === null || $pq <= 0) { + foreach ($this->selectWithReconnect("SELECT io_id, sku_id, qty, name FROM rpt.jst_purchaseoutItem WHERE io_id IN ({$ph})", $ck) as $pr) { + $sku = trim((string) ($pr['sku_id'] ?? '')); + if ($sku === '') { continue; } - $pq = (int) round($pq); - $viewItems[$vio][$vi]['qty'] = $pq; + $pk = mb_strtolower($sku); + $pio = (string) $pr['io_id']; + if (! isset($poItems[$pio][$pk])) { + $name = trim((string) ($pr['name'] ?? '')); + $poItems[$pio][$pk] = ['sku' => $sku, 'qty' => 0.0, 'name' => $name !== '' ? $name : null]; + } + $poItems[$pio][$pk]['qty'] += (float) ($pr['qty'] ?? 0); + } + } + foreach ($poItems as $pio => $prows) { + if (! empty($viewItems[$pio])) { + $viewItems[$pio] = $this->purchaseOutItems($viewItems[$pio], $prows, $this->ioWare[$pio] ?? null); } } $skus = $this->loadSkus($this->collectSkuIds($viewItems)); diff --git a/tests/Unit/EcommerceFinance/ShippingReplicaSynchronizerTest.php b/tests/Unit/EcommerceFinance/ShippingReplicaSynchronizerTest.php index 2d6b517c..3718e893 100644 --- a/tests/Unit/EcommerceFinance/ShippingReplicaSynchronizerTest.php +++ b/tests/Unit/EcommerceFinance/ShippingReplicaSynchronizerTest.php @@ -1587,6 +1587,36 @@ class ShippingReplicaSynchronizerTest extends TestCase $this->assertSame(2, $db->table('jst_oms_shipping')->where('source_id', 9004)->count()); } + public function test_purchase_out_items_follow_purchase_out_detail_instead_of_repeated_view_rows(): void + { + $factory = $this->createMock(UpstreamReadOnlyQueryFactory::class); + $factory->method('forSource')->willReturn($this->createMock(UpstreamReadOnlyQuery::class)); + $sync = new ShippingReplicaSynchronizer($factory); + $method = new \ReflectionMethod($sync, 'purchaseOutItems'); + // 视图重复行把数量放大(每行都写整单93件),拼接条码被截断出残码 GBGJ-SZ0 + $view = [ + ['id' => null, 'sku' => 'GBGJ-SZ020401N1280V01', 'iid' => null, 'shopid' => null, 'name' => '视图名称A', 'qty' => 93, + 'amount' => null, 'warehouse' => '青岗集货退上架仓', 'l' => 180, 'w' => 70, 'h' => 75, 'vol' => null, 'weight' => 30, 'supplier' => 'S1'], + ['id' => null, 'sku' => 'GBGJ-SZ0', 'iid' => null, 'shopid' => null, 'name' => null, 'qty' => 93, + 'amount' => null, 'warehouse' => '青岗集货退上架仓', 'l' => null, 'w' => null, 'h' => null, 'vol' => null, 'weight' => null, 'supplier' => null], + ]; + $po = [ + 'gbgj-sz020401n1280v01' => ['sku' => 'GBGJ-SZ020401N1280V01', 'qty' => 4.0, 'name' => '明细名称A'], + 'gbpm-sj035402n1280v01' => ['sku' => 'GBPM-SJ035402N1280V01', 'qty' => 2.0, 'name' => '明细名称B'], + 'gbgj-zero' => ['sku' => 'GBGJ-ZERO', 'qty' => 0.0, 'name' => null], + ]; + $items = $method->invoke($sync, $view, $po, '青岗集货退上架仓'); + $this->assertSame(['GBGJ-SZ020401N1280V01', 'GBPM-SJ035402N1280V01'], array_column($items, 'sku')); + $this->assertSame([4, 2], array_column($items, 'qty')); + // 视图有的条码沿用视图的名称/长宽高;视图没有的按明细补一行 + $this->assertSame('视图名称A', $items[0]['name']); + $this->assertSame(180, $items[0]['l']); + $this->assertSame('明细名称B', $items[1]['name']); + $this->assertSame('青岗集货退上架仓', $items[1]['warehouse']); + // 明细为空时仍按视图 + $this->assertSame($view, $method->invoke($sync, $view, [], null)); + } + public function test_outbound_view_items_replace_stale_order_fallback_items_when_barcodes_differ(): void { $db = DB::connection('business_mirror');