From de67d6ca3a2fb90a754b354b35d1c328fe22efd1 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 28 Oct 2026 10:49:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=BC=BA=E8=B4=A7):=20=E4=B8=BB=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E5=86=99=E5=9B=9E=E5=89=8D=E9=A2=84=E6=A3=80=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E5=BA=93=E5=AD=98=E6=8A=B5=E6=89=A3/=E5=B1=A5?= =?UTF-8?q?=E7=BA=A6=E4=BA=8B=E5=AE=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免主系统已转自采、本地因库存抵扣或已完成事实无法清理却只返回错误;库存抵扣/已入库/备货完成/采购完成先拒绝,不发主系统状态。 --- server/src/services/mainSystemStockout.ts | 30 +++++++++++++++++++ .../mainSystemDayMergeAndStockout.test.ts | 6 ++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/server/src/services/mainSystemStockout.ts b/server/src/services/mainSystemStockout.ts index 4dbe206..8b8f96e 100644 --- a/server/src/services/mainSystemStockout.ts +++ b/server/src/services/mainSystemStockout.ts @@ -408,9 +408,11 @@ export async function reportMainSystemStockout(params: { is_stockout: boolean; stockout_reported_at: string | null; order_status: number | string | null; + stock_cover_qty: number | string | null; }>( `SELECT id::text AS id, item_name, sku_code, COALESCE(is_returned, false) AS is_returned, COALESCE(is_stockout, false) AS is_stockout, stockout_reported_at, + COALESCE(stock_cover_qty, 0) AS stock_cover_qty, (SELECT o.order_status FROM orders o WHERE o.order_code = order_items.order_code LIMIT 1) AS order_status FROM order_items WHERE id = $1::uuid AND order_code = $2 @@ -425,6 +427,34 @@ export async function reportMainSystemStockout(params: { if (item.is_returned) { return { kind: 'invalid_state', reason: '该商品已退货,不能再报缺货' }; } + + /** + * 报主系统前先做本地可清理性预检(2026-10-28)。 + * + * 原顺序是「先写主系统 status=40,再清本地」,如果本地因库存抵扣/已完成备货/已完成采购 + * 无法硬删,会出现:主系统已转自采、本地行还在、前端只看到一个错误提示 —— 两边越改越不一致。 + * 这些事实必须先拦住,且**不能先写主系统**。 + */ + if (Number(item.stock_cover_qty || 0) > 0.000001) { + return { + kind: 'invalid_state', + reason: '该商品已有库存抵扣,不能报缺货(请先取消抵扣或走售后退货处理)', + }; + } + const localBlockReason = await runWithTenantDataAccess(branchCode, async (client) => { + const target = { id: itemId, sku_code: item.sku_code, item_name: item.item_name }; + if (await orderItemHasInspectionInbound(client, orderCode, target)) return '已验收入库'; + if (await orderItemHasCompletedPreparation(client, orderCode, target)) return '备货已完成'; + if (await orderItemHasCompletedPurchase(client, orderCode, target)) return '采购已完成'; + return null; + }); + if (localBlockReason) { + return { + kind: 'invalid_state', + reason: `该商品${localBlockReason},不能报缺货(请走售后退货处理)`, + }; + } + // 已分拣出库(order_status ≥ 50)后禁止「报缺货」(开发者 2026-10-12): // 货已出库/在途,再报缺货会让主系统把已履约的行改判转自采(界面也已隐藏该按钮)。 // 例外:已报过(is_stockout)的存量行仍放行 —— 只为了把本地残留行幂等收口。 diff --git a/server/tests/mainSystemDayMergeAndStockout.test.ts b/server/tests/mainSystemDayMergeAndStockout.test.ts index 41a080c..acdcfb0 100644 --- a/server/tests/mainSystemDayMergeAndStockout.test.ts +++ b/server/tests/mainSystemDayMergeAndStockout.test.ts @@ -368,9 +368,9 @@ describe('报缺货:先主系统、后本地', () => { expect(params.preparationId).toBe(PREP); // 先按既有"取消履约"判据确认还能硬删,再调删除原语(它连待处理采购/备货一起清) - expect(hasInbound).toHaveBeenCalledTimes(1); - expect(hasPrepDone).toHaveBeenCalledTimes(1); - expect(hasPurchaseDone).toHaveBeenCalledTimes(1); + expect(hasInbound).toHaveBeenCalledTimes(2); // 预检 + 删除原语内部复检 + expect(hasPrepDone).toHaveBeenCalledTimes(2); // 预检 + 删除原语内部复检 + expect(hasPurchaseDone).toHaveBeenCalledTimes(2); // 预检 + 删除原语内部复检 expect(removeItem).toHaveBeenCalledTimes(1); expect((removeItem.mock.calls[0][0] as Record).itemId).toBe(ITEM);