From 06233ff0b00bb38d98a7ee7bf6a7056d97d4f3d4 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 22 Oct 2026 15:13:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E8=BF=9B=E9=94=80=E5=AD=98/=E8=87=AA?= =?UTF-8?q?=E9=87=87):=20=E9=83=A8=E5=88=86=E6=8A=B5=E6=89=A3=E7=BC=BA?= =?UTF-8?q?=E5=8F=A3=E9=BB=98=E8=AE=A4=E8=87=AA=E9=87=87=E5=90=8C=E6=A0=B7?= =?UTF-8?q?=E5=9C=A8=E6=97=A0=E9=87=87=E8=B4=AD=E5=91=98=E6=97=B6=E6=8C=89?= =?UTF-8?q?=E8=87=AA=E7=94=B1=E6=B1=A0=E5=BB=BA=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上一提交(59dcfd5)修的是「主系统导入单无抵扣 ⇒ 默认自采」入口;代码复查发现**同类的第二个入口**: `ensureSelfPurchaseForCoverGap`(库存部分抵扣后的缺口默认自采,在 reprocessOrderAllocation 内, 导入单提交也会经过)仍调用 createPurchaseOrderForItem 不传 allowMissingPurchaser ⇒ 分公司无采购员时 每行静默返回 false,而 `STOCK_COVER_SUPPLIER_SQL` 已把行标 INTERNAL ⇒ 同样留下 「界面自采、自采表无数据、订单卡 20 待分配」的半成品(与刚修复的事故同类,尚未在真机触发)。 修复:该入口也传 allowMissingPurchaser: true(无采购员按自由池建单);并在 「有行需转自采但 created=0」时打 warn(原先完全静默)。 验证:契约测试 4/4(新增部分抵扣分支断言);typecheck 通过;后端全量 3 failed / 579 passed (18 failed / 4145 passed,红均为既有 mainSystemReturnCancel/opsGuardHardening/routeLayerBoundary)。 设计文档同步两处入口。未部署。 --- .../20261022-主系统导入单无采购员按自由池建自采单.md | 6 ++++-- server/src/services/orderAllocationService.ts | 11 +++++++++++ ...mportSelfPurchaseMissingPurchaser.contract.test.ts | 8 ++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/docs/design/20261022-主系统导入单无采购员按自由池建自采单.md b/docs/design/20261022-主系统导入单无采购员按自由池建自采单.md index 75af4bc..442218d 100644 --- a/docs/design/20261022-主系统导入单无采购员按自由池建自采单.md +++ b/docs/design/20261022-主系统导入单无采购员按自由池建自采单.md @@ -21,12 +21,14 @@ ## 2. 方案(最小改动) +0. 覆盖**同类的两个入口**(都属"无抵扣/缺口默认自采"):① 主系统导入单无抵扣 `ensureSelfPurchaseForImportedOrder`; + ② 部分抵扣后缺口 `ensureSelfPurchaseForCoverGap`(`reprocessOrderAllocation` 内,导入单提交也会经过)——两者都传新参数。 1. `createPurchaseOrderForItem` 新增参数 `allowMissingPurchaser?: boolean`(默认 false,**其它调用方行为逐字不变**): - `resolvePurchaserForNewPurchaseOrder` 返回 `unavailable` 且 `allowMissingPurchaser=true` ⇒ 按 **自由池** 建单:`purchaser_code/purchaser_name = NULL`(采购员后续在采购列表认领),并打一条 warn; - 不传该参数 ⇒ 仍返回 false(保持手动分配/竞价等路径原语义)。 -2. `ensureSelfPurchaseForImportedOrder`(**仅主系统导入单的无抵扣默认自采收口**)传 `allowMissingPurchaser: true`。 -3. 可观测性:行命中但 `created === 0` 时打 warn(以前完全静默),便于下次一眼定位。 +2. `ensureSelfPurchaseForImportedOrder` 与 `ensureSelfPurchaseForCoverGap`(两处默认自采收口)传 `allowMissingPurchaser: true`。 +3. 可观测性:两处入口在"有行需转自采但 created=0"时都打 warn(以前完全静默),便于下次一眼定位。 ## 3. 兼容与回滚 diff --git a/server/src/services/orderAllocationService.ts b/server/src/services/orderAllocationService.ts index 8efc7b2..efb700d 100644 --- a/server/src/services/orderAllocationService.ts +++ b/server/src/services/orderAllocationService.ts @@ -694,10 +694,12 @@ async function ensureSelfPurchaseForCoverGap( const primaryPurchaser = await fetchPrimaryPurchaserForBranch(params.branchCode); let created = 0; + let attempted = 0; for (const row of rows.rows as OrderItemRow[]) { if (exclude.has(String(row.id))) continue; try { if (await isOrderItemFulfillmentValid(client, String(row.id))) continue; + attempted += 1; const ok = await createPurchaseOrderForItem( { orderCode: params.orderCode, @@ -706,6 +708,9 @@ async function ensureSelfPurchaseForCoverGap( purchaser: primaryPurchaser, branchCode: params.branchCode, customerCode: params.customerCode, + // 与「导入单无抵扣默认自采」同口径(2026-10-22):无可用采购员时按自由池建单, + // 不能静默不建 ⇒ 行已标 INTERNAL 会让订单卡在 20 待分配、自采表又没有数据。 + allowMissingPurchaser: true, }, client ); @@ -715,6 +720,12 @@ async function ensureSelfPurchaseForCoverGap( console.warn('[stock-cover] 缺口默认自采补采购单失败:', (err as Error)?.message || err); } } + if (attempted > 0 && created === 0) { + console.warn( + `[stock-cover] ${params.orderCode} 部分抵扣缺口 ${attempted} 行需转自采但一张采购单未建` + + `(可能:已有履约/建单失败),订单可能停在待分配,请排查` + ); + } return created; } diff --git a/server/tests/importSelfPurchaseMissingPurchaser.contract.test.ts b/server/tests/importSelfPurchaseMissingPurchaser.contract.test.ts index 0eb58de..5db9c54 100644 --- a/server/tests/importSelfPurchaseMissingPurchaser.contract.test.ts +++ b/server/tests/importSelfPurchaseMissingPurchaser.contract.test.ts @@ -32,6 +32,14 @@ describe('导入单默认自采:无采购员按自由池建单(不静默卡 expect(body).toContain('allowMissingPurchaser: true'); }); + it('部分抵扣缺口默认自采分支同样传 allowMissingPurchaser(同一类半成品)', () => { + const gapStart = service.indexOf('async function ensureSelfPurchaseForCoverGap'); + expect(gapStart).toBeGreaterThan(0); + const body = service.slice(gapStart, gapStart + 6000); + expect(body).toContain('allowMissingPurchaser: true'); + expect(body).toContain('部分抵扣缺口 ${attempted} 行需转自采但一张采购单未建'); + }); + it('行命中却一张没建时留 warn(不再完全静默)', () => { expect(service).toContain('标记自采 ${rows.rowCount} 行但未建任何采购单'); });