diff --git a/app/service/systems/order_storage_allocation.js b/app/service/systems/order_storage_allocation.js index 7d3473f..ce26e4d 100644 --- a/app/service/systems/order_storage_allocation.js +++ b/app/service/systems/order_storage_allocation.js @@ -2228,6 +2228,7 @@ class OrderStorageAllocationService extends MainService { orders: skippedOrders, picking_count: 0, storage_unlock_summary: [], + storage_return_summary: [], stock_unlock_summary: [], }, }; @@ -2249,7 +2250,8 @@ class OrderStorageAllocationService extends MainService { }); const pickingRowsByOrderId = new Map(); - const storageQuantityMap = new Map(); + const pendingStorageQuantityMap = new Map(); + const finStorageQuantityMap = new Map(); const storageIdSet = new Set(); for (const pickingRow of Array.isArray(pickingRows) ? pickingRows : []) { @@ -2267,7 +2269,14 @@ class OrderStorageAllocationService extends MainService { pickingRowsByOrderId.set(orderId, orderPickingRows); storageIdSet.add(storageId); - storageQuantityMap.set(storageId, (storageQuantityMap.get(storageId) || 0) + quantity); + // 按状态分流:pending 记录解除锁定(实物不动); + // fin 记录(已拣货)返还实物数量,锁定已在拣货完成时扣减,不再重复扣除 + const pickingStatus = String(plainPicking?.status || '').trim(); + if (pickingStatus === 'fin') { + finStorageQuantityMap.set(storageId, (finStorageQuantityMap.get(storageId) || 0) + quantity); + } else { + pendingStorageQuantityMap.set(storageId, (pendingStorageQuantityMap.get(storageId) || 0) + quantity); + } } const cancellableOrderIds = []; @@ -2298,6 +2307,7 @@ class OrderStorageAllocationService extends MainService { orders: skippedOrders, picking_count: 0, storage_unlock_summary: [], + storage_return_summary: [], stock_unlock_summary: [], }, }; @@ -2326,8 +2336,10 @@ class OrderStorageAllocationService extends MainService { } const storageUnlockSummary = []; + const storageReturnSummary = []; - for (const [ storageId, quantity ] of storageQuantityMap.entries()) { + // pending 记录:解除锁定,实物库存不动 + for (const [ storageId, quantity ] of pendingStorageQuantityMap.entries()) { if (quantity <= 0) { continue; } @@ -2372,6 +2384,53 @@ class OrderStorageAllocationService extends MainService { }); } + // fin 记录(已拣货):返还实物数量,锁定已在拣货完成时扣减,不再重复扣除 + for (const [ storageId, returnQuantity ] of finStorageQuantityMap.entries()) { + if (returnQuantity <= 0) { + continue; + } + + const storageRow = storageMap.get(storageId); + if (!storageRow) { + throw new Error(`库存记录不存在: ${storageId}`); + } + + const beforeQuantity = toNumber(storageRow.quantity, 0); + const beforeLockedQuantity = toNumber(storageRow.locked_quantity, 0); + + await ctx.service.systems.storage.index.updateQuantity(storageId, returnQuantity, 0, { + transaction, + }); + + // 写入库存返还流水(实物数量增加,锁定不变) + await ctx.model.StorageTransaction.create({ + transaction_type: 'INBOUND', + warehouse_id: warehouseId, + store_id: storageRow.store_id, + stock_id: storageRow.stock_id, + storage_id: storageId, + space_id: storageRow.space_id, + quantity: returnQuantity, + before_quantity: beforeQuantity, + after_quantity: beforeQuantity + returnQuantity, + before_locked_quantity: beforeLockedQuantity, + after_locked_quantity: beforeLockedQuantity, + batch_no: storageRow.batch_no, + inventory_type: storageRow.inventory_type, + order_type: 'PICK_RETURN', + order_no: 'CANCEL_BATCH', + order_id: 0, + operator_id: String(ctx.user?.employees_id ?? ctx.user?.id ?? ''), + operator_name: ctx.user?.employees_name ?? ctx.user?.real_name ?? ctx.user?.name ?? '', + remark: '取消分配储位(已拣货返还库存)', + }, { transaction }); + + storageReturnSummary.push({ + storage_id: storageId, + returned_quantity: returnQuantity, + }); + } + const cancelPickingIds = []; for (const orderId of cancellableOrderIds) { const orderPickingRows = pickingRowsByOrderId.get(orderId) || []; @@ -2439,6 +2498,7 @@ class OrderStorageAllocationService extends MainService { orders: [ ...skippedOrders, ...orderResults ], picking_count: cancelPickingIds.length, storage_unlock_summary: storageUnlockSummary, + storage_return_summary: storageReturnSummary, stock_unlock_summary: [], };