fix(主系统导入): 已实际出库的日单不再追加新主系统行
status=50 本身不等于出库;按 inventory_logs/picking_items.outbound_at 判断,已出库则另开补单,避免新行把已完成/已出库的日单重新拉回未完成。
This commit is contained in:
@@ -519,6 +519,33 @@ const APPEND_BLOCKED_DISPOSITIONS = new Set([
|
||||
*/
|
||||
const DAY_APPEND_CEILING = 60;
|
||||
|
||||
/**
|
||||
* 该本地订单是否**已经实际出库**(`inventory_logs` 或拣货行 outbound_at)。
|
||||
* 2026-10-28:status=50 本身不等于出库;只有真正出库后才禁止追加/重开日单。
|
||||
*/
|
||||
async function orderHasActualOutbound(branchCode: string, orderCode: string): Promise<boolean> {
|
||||
const code = String(orderCode || '').trim();
|
||||
if (!code) return false;
|
||||
return runWithTenantDataAccess(branchCode, async (client) => {
|
||||
const logs = await client.query(
|
||||
`SELECT 1 FROM inventory_logs
|
||||
WHERE related_order_code = $1
|
||||
AND operation_type IN ('picking_out', 'delivery_loading_out')
|
||||
AND COALESCE(quantity, 0) > 0
|
||||
LIMIT 1`,
|
||||
[code]
|
||||
);
|
||||
if (logs.rowCount) return true;
|
||||
const picks = await client.query(
|
||||
`SELECT 1 FROM picking_items
|
||||
WHERE order_code = $1 AND outbound_at IS NOT NULL
|
||||
LIMIT 1`,
|
||||
[code]
|
||||
);
|
||||
return Boolean(picks.rowCount);
|
||||
});
|
||||
}
|
||||
|
||||
/** 导出仅供用例:追加判据是"台账 ↔ 我方订单行"一致性的唯一闸门,必须能被钉住 */
|
||||
export function needsAppendToOrder(
|
||||
row: { id: string; isStockout?: boolean | null },
|
||||
@@ -682,6 +709,7 @@ async function appendNewRowsToDayOrder(params: {
|
||||
})),
|
||||
};
|
||||
}
|
||||
const snapshotOutbound = await orderHasActualOutbound(branchCode, ourOrderCode);
|
||||
|
||||
/**
|
||||
* 落台账:**整单行全量**传入(按「商品名 + 出现次序」配到本系统订单行,与建单同一套映射),
|
||||
@@ -795,7 +823,7 @@ async function appendNewRowsToDayOrder(params: {
|
||||
|
||||
// 订单已达「配送中/已完成」(≥ 60),追加行就不再自动落:
|
||||
// 要动的是已经发车/已结案的量,必须人工确认(正常已由上层分流成"另开补单")。
|
||||
if (snapshot.orderStatus >= DAY_APPEND_CEILING) {
|
||||
if (snapshot.orderStatus >= DAY_APPEND_CEILING || snapshotOutbound) {
|
||||
const reason = `订单已进入履约(状态 ${snapshot.orderStatus}),新分配的商品需人工处理`;
|
||||
await persistRows(
|
||||
pending.map((entry) => entry.group),
|
||||
@@ -2260,7 +2288,13 @@ async function importDayBucket(params: {
|
||||
? { status: Number(row.order_status) || 0, trashed: Boolean(row.deleted_at) }
|
||||
: null;
|
||||
});
|
||||
if (currentStatus && (currentStatus.trashed || currentStatus.status >= DAY_APPEND_CEILING)) {
|
||||
const currentOutbound = currentStatus
|
||||
? await orderHasActualOutbound(branchCode, claimOurOrderCode)
|
||||
: false;
|
||||
if (
|
||||
currentStatus &&
|
||||
(currentStatus.trashed || currentStatus.status >= DAY_APPEND_CEILING || currentOutbound)
|
||||
) {
|
||||
const created = await rotateToNewDayOrder({
|
||||
branchCode,
|
||||
userId,
|
||||
|
||||
@@ -337,12 +337,16 @@ describe('importMainSystemGroups 占坑式幂等', () => {
|
||||
expect(outcome.created).toBe(1);
|
||||
});
|
||||
|
||||
it('源码钉住:阀门 = 60(配送中/已完成另开补单;待取货 50 仍追加)', () => {
|
||||
it('源码钉住:未出库才可追加;已实际出库/配送中/完成另开补单', () => {
|
||||
const src = readFileSync(new URL('../src/services/mainSystemOrderImport.ts', import.meta.url), 'utf8');
|
||||
expect(src).toContain('const DAY_APPEND_CEILING = 60');
|
||||
expect(src).toContain('snapshot.orderStatus >= DAY_APPEND_CEILING');
|
||||
expect(src).toContain('currentStatus.status >= DAY_APPEND_CEILING');
|
||||
expect(src).toContain('currentStatus.trashed'); // 方案 B:垃圾桶里的当日单一律另开新单
|
||||
// 2026-10-28:status=50 本身不等于出库;真正出库后禁止追加/重开日单
|
||||
expect(src).toContain('orderHasActualOutbound');
|
||||
expect(src).toContain('const currentOutbound');
|
||||
expect(src).toContain('snapshotOutbound');
|
||||
expect(src).toContain('rotateToNewDayOrder');
|
||||
expect(src).toContain('AND our_order_code = $4'); // CAS:只有指针仍等于旧单号才抢到建单权
|
||||
expect(src).toContain('failureRestoreCode');
|
||||
|
||||
Reference in New Issue
Block a user