fix(缺货): 主系统写回前预检本地库存抵扣/履约事实
避免主系统已转自采、本地因库存抵扣或已完成事实无法清理却只返回错误;库存抵扣/已入库/备货完成/采购完成先拒绝,不发主系统状态。
This commit is contained in:
@@ -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)的存量行仍放行 —— 只为了把本地残留行幂等收口。
|
||||
|
||||
@@ -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<string, unknown>).itemId).toBe(ITEM);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user