fix(发货数据): 采购退货出库商品明细以采购退货明细为准 (#865)

发货视图对采购退货单会重复出很多行(每行都写整单数量),拼接条码还会被截断出残码,
按视图累加把数量放大(如 1190330222661 聚水潭 93 件、财务 1083 件)。
现改为商品行以 rpt.jst_purchaseoutItem 为准:视图同条码沿用名称/长宽高,数量取明细;
视图没有的条码按明细补行;视图多出的残码/重复行丢弃;明细为空时仍按视图。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014nvGZkg27YMEdhry6eiuwf

> 开工时标题使用 `WIP: <任务名称>` 并先填写“任务占用与边界”。Codex 完成独立合并前自审后补齐其余内容并移除 `WIP:`。

## 任务占用与边界

- 任务名称:
- 实际开发者:
- 任务来源/团队分工:
- 目标结果:
- 预计修改路径:
- 将写入的公共契约(路由、Controller 方法、Repository、表/视图、`source_key`、`scope`、权限名、菜单键或共享组件):
- 仅复用、明确不修改的公共契约:
- 数据源、表/视图和字段:
- 上游 `source_key` / `scope`:
- 权限名、菜单键或共享组件:
- 明确不修改的范围:

## 开工碰撞检查

- [ ] Codex 已执行 `scripts/codex-task-preflight.ps1`
- [ ] 已检查最新 `main` 和所有远程任务分支
- [ ] 已搜索主线既有页面、接口、Repository 和数据查询
- [ ] 已把“将写入”和“仅复用、不修改”的公共契约分开;仅复用项不作为单独阻断条件
- [ ] 未发现实际写入重叠;或已在下方写明相关分支、具体重叠项和协调结论

相关分支、重叠项与协调结论:

## 实际改动

- 完成内容:
- Codex 实际修改的文件:
- 新增或变更的公共契约:
- 与原计划的差异:

## 验证证据

- 正常流程:
- 空数据/错误输入/无权限场景:
- 受影响旧功能:
- 自动测试及结果:
- 实际页面、接口或只读业务查询结果:

## 风险检查

- [ ] 不涉及金额、权限、审批、数据库迁移、生产配置或数据修复
- [ ] 如涉及上述高风险内容,已在下方写明影响、回滚和所需授权
- [ ] 未提交 `.env`、密码、Token、私钥、真实财务数据、生产日志或构建产物
- [ ] 前端改动符合 `DESIGN.md`;无前端改动则写“不适用”

高风险影响、回滚和授权状态:

## 合并前复查

- [ ] 已重新拉取远程引用并用实际修改范围再次运行碰撞检查
- [ ] PR 不再是 `WIP:`,所有测试结果已经补齐
- [ ] Codex 已重新读取原始需求和验收标准,并逐文件审核 `origin/main...任务分支` 的完整 diff
- [ ] Codex 自审确认无越界修改、重复实现、敏感内容、未处理冲突和未验证结果
- [ ] 合并顺序不会覆盖其他未合并任务
- [ ] 已授权当前任务的 Codex 执行 Squash merge、验证 `main` 并清理任务分支
- [ ] 如涉及生产迁移、数据修复、删除、覆盖配置或部署,已另行取得对应高风险授权

Reviewed-on: #865
This commit is contained in:
limeng 2026-11-04 16:07:35 +08:00
parent f3a5788c67
commit 10e20f0df7
2 changed files with 84 additions and 13 deletions

View File

@ -1001,6 +1001,43 @@ class ShippingReplicaSynchronizer
return $total;
}
/**
* 采购退货出库的商品行:以采购退货明细为准。
* 发货视图对采购退货单会重复出很多行、拼接条码还会被截断,按视图累加会把数量放大(如 93 件变成 1083 件)。
* 明细里有的条码:沿用视图同条码的名称/长宽高等字段,数量取明细;视图没有的条码按明细补一行;
* 视图里多出来的条码(截断残码、重复行)丢弃。明细为空时仍按视图。
*
* @param list<array<string,mixed>> $viewItems
* @param array<string,array{sku:string,qty:float,name:?string}> $poRows 小写条码 => 明细
* @return list<array<string,mixed>>
*/
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));

View File

@ -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');