fix(进销存/自采): 部分抵扣缺口默认自采同样在无采购员时按自由池建单
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s

上一提交(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)。
设计文档同步两处入口。未部署。
This commit is contained in:
Ubuntu
2026-10-22 15:13:25 +08:00
parent 59dcfd578c
commit 06233ff0b0
3 changed files with 23 additions and 2 deletions
@@ -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. 兼容与回滚
@@ -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;
}
@@ -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} 行但未建任何采购单');
});