fix(storage.entry): 入库超量静默裁剪 + containerList 按可用量过滤

1. inbound_direct_log._addSingleItem: 入库数量超过计划时不再拒绝,改为静默裁剪为 maxAllowed(仅无可入数量时拒绝)
2. storage/entry.containerList: NOT EXISTS 一刀切排除改为计算 actual_quantity - SUM(已占用),只返回可用量>0的收货明细
This commit is contained in:
liufumingren 2026-07-23 17:42:28 +08:00
parent 52aa76af60
commit a69fd3f393
2 changed files with 1131 additions and 143 deletions

View File

@ -373,23 +373,26 @@ class InboundDirectLogService extends Service {
// CREATED 人工导入允许超过计划数量
const isManualImport = entryOrder.order_status === 'CREATED' && !isBindingMode;
if (!isManualImport) {
const newTotal = existingTotal + effectiveQuantity;
if (newTotal > totalPlanQty) {
const maxAllowed = totalPlanQty - existingTotal;
if (maxAllowed <= 0) {
return {
ok: false,
error: {
code: 400,
message: `入库数量超过计划:商品计划 ${totalPlanQty},已入库 ${existingTotal},本次 ${effectiveQuantity},合计 ${newTotal}`,
message: `入库数量超过计划:商品计划 ${totalPlanQty},已入库 ${existingTotal},本次 ${effectiveQuantity},合计 ${existingTotal + effectiveQuantity}`,
data: {
stock_id: stockId,
plan_quantity: totalPlanQty,
existing_quantity: existingTotal,
requested_quantity: effectiveQuantity,
exceeded_by: newTotal - totalPlanQty,
exceeded_by: effectiveQuantity,
},
},
};
}
if (effectiveQuantity > maxAllowed) {
effectiveQuantity = maxAllowed;
}
}
// 剩余可入库数 = 计划数 - max(已收货量, 已写入 direct_log 的待入库量)
@ -853,7 +856,7 @@ class InboundDirectLogService extends Service {
itemId: item.item_id || '',
inventoryType: item.inventory_type || 'ZP',
planQty: Number(item.count) || 0,
actualQty: Number(item.count) || 0,
actualQty: stockSummary[String(item.stock_id)] || 0,
}));
const syncResult = await this.qimenEntrySync({
@ -957,11 +960,18 @@ class InboundDirectLogService extends Service {
transaction,
});
// 同一 stock_id 只写一次库存
if (processedStockIds.has(String(stockId))) continue;
processedStockIds.add(String(stockId));
if (!isBinding) {
nonBindingStockProcessed.add(String(stockId));
// 同一 stock_id 只写一次 StockWarehouse 总库存(聚合量),但 Storage 按入库记录逐条写入
const isStockWarehouseDone = processedStockIds.has(String(stockId));
if (isStockWarehouseDone && !isBinding) {
// 非绑定模式:StockWarehouse 已写但 Storage 还未写,不能 continue,继续写 Storage
} else if (isStockWarehouseDone) {
continue;
}
if (!isStockWarehouseDone) {
processedStockIds.add(String(stockId));
if (!isBinding) {
nonBindingStockProcessed.add(String(stockId));
}
}
// 使用入库单号作为批次号;若有收货绑定则使用收货明细信息
@ -972,137 +982,162 @@ class InboundDirectLogService extends Service {
const batchNo = eo.entry_order_code;
const inventoryType = item.inventory_type || 'ZP';
// 从该 stock_id 的入库记录中取第一个非空 space_id,兜底 1
const logForSpace = directLogs.find(l => String(l.stock_id) === String(stockId) && l.space_id);
const spaceId = logForSpace ? Number(logForSpace.space_id) : 1;
// StockWarehouse 总库存:每个 stock_id 聚合写入一次
if (!isStockWarehouseDone) {
// 锁定并更新 StockWarehouse 总库存
const stockRecord = await ctx.model.StockWarehouse.findOne({
where: { id: stockId, is_valid: 'Y', deleted_at: null },
lock: transaction.LOCK.UPDATE,
transaction,
});
// 锁定并更新 StockWarehouse 总库存
const stockRecord = await ctx.model.StockWarehouse.findOne({
where: { id: stockId, is_valid: 'Y', deleted_at: null },
lock: transaction.LOCK.UPDATE,
transaction,
});
if (stockRecord) {
const beforeQuantity = Number(stockRecord.quantity) || 0;
const beforeLockedQuantity = Number(stockRecord.locked_quantity) || 0;
if (stockRecord) {
const beforeQuantity = Number(stockRecord.quantity) || 0;
const beforeLockedQuantity = Number(stockRecord.locked_quantity) || 0;
await stockRecord.increment('quantity', { by: inboundQty, transaction, silent: true });
await stockRecord.reload({ transaction });
await stockRecord.increment('quantity', { by: inboundQty, transaction, silent: true });
await stockRecord.reload({ transaction });
const afterQuantity = Number(stockRecord.quantity) || 0;
const afterLockedQuantity = Number(stockRecord.locked_quantity) || 0;
const afterQuantity = Number(stockRecord.quantity) || 0;
const afterLockedQuantity = Number(stockRecord.locked_quantity) || 0;
// 写入库存流水(聚合维度)
await ctx.service.systems.stockTransaction.create({
transaction_type: 'INBOUND',
warehouse_id: warehouseId,
store_id: storeId,
stock_id: stockId,
quantity: inboundQty,
batch_no: batchNo,
inventory_type: inventoryType,
order_type: eo.order_type,
order_no: eo.entry_order_code,
entry_order_id: eo.id,
operator_id: String(ctx.user?.employees_id || ctx.user?.id || ''),
operator_name: ctx.user?.employees_name || '',
remark: '导入/PDA直接入库提交',
before_quantity: beforeQuantity,
after_quantity: afterQuantity,
before_locked_quantity: beforeLockedQuantity,
after_locked_quantity: afterLockedQuantity,
created_at: new Date(),
}, { transaction });
}
}
// 写入库存流水
await ctx.service.systems.stockTransaction.create({
/**
* 写入 Storage 库位库存 + storage_transaction 的通用函数
* @param {number} spaceId 库位ID
* @param {number} qty 入库数量
* @param {number|null} rdId 收货单ID
* @param {string|null} prodDate 生产日期
* @param {string|null} expDate 到期日期
*/
const writeStorageRecord = async (spaceId, qty, rdId, prodDate, expDate) => {
if (qty <= 0) return;
const sRecord = await ctx.model.Storage.findOne({
where: {
space_id: spaceId,
stock_id: stockId,
batch_no: batchNo,
inventory_type: inventoryType,
is_enabled: 1,
delete_at: null,
},
lock: transaction.LOCK.UPDATE,
transaction,
});
let sId = null;
let sBeforeQty = 0;
let sAfterQty = 0;
if (sRecord) {
sBeforeQty = Number(sRecord.quantity) || 0;
sAfterQty = sBeforeQty + qty;
await ctx.model.Storage.update({
quantity: app.Sequelize.literal(`quantity + ${qty}`),
available_quantity: app.Sequelize.literal(`available_quantity + ${qty}`),
entry_order_id: eo.id,
inbound_time: new Date(),
inbound_order_no: eo.entry_order_code,
production_date: prodDate,
expiry_date: expDate,
receiving_id: rdId,
updated_at: new Date(),
}, {
where: { id: sRecord.id },
transaction,
});
sId = sRecord.id;
} else {
sBeforeQty = 0;
sAfterQty = qty;
const createdStorage = await ctx.model.Storage.create({
warehouse_id: warehouseId,
store_id: storeId,
space_id: spaceId,
stock_id: stockId,
batch_no: effectiveBatchNo,
inventory_type: inventoryType,
quantity: qty,
locked_quantity: 0,
available_quantity: qty,
inbound_time: new Date(),
inbound_order_no: eo.entry_order_code,
receiving_id: rdId,
entry_order_id: eo.id,
supplier_id: null,
supplier_batch_no: null,
production_date: prodDate,
expiry_date: expDate,
is_enabled: 1,
created_at: new Date(),
updated_at: new Date(),
}, { transaction });
sId = createdStorage.id;
}
// 写入库位维度库存流水
await ctx.model.StorageTransaction.create({
transaction_type: 'INBOUND',
warehouse_id: warehouseId,
store_id: storeId,
stock_id: stockId,
quantity: inboundQty,
batch_no: batchNo,
storage_id: sId,
space_id: spaceId,
quantity: qty,
before_quantity: sBeforeQty,
after_quantity: sAfterQty,
batch_no: effectiveBatchNo,
inventory_type: inventoryType,
order_type: eo.order_type,
order_type: 'entry_order',
order_no: eo.entry_order_code,
entry_order_id: eo.id,
order_id: eo.id,
operator_id: String(ctx.user?.employees_id || ctx.user?.id || ''),
operator_name: ctx.user?.employees_name || '',
remark: '导入/PDA直接入库提交',
before_quantity: beforeQuantity,
after_quantity: afterQuantity,
before_locked_quantity: beforeLockedQuantity,
after_locked_quantity: afterLockedQuantity,
created_at: new Date(),
}, { transaction });
}
};
// 创建/更新 Storage 库位库存
const storageRecord = await ctx.model.Storage.findOne({
where: {
space_id: spaceId,
stock_id: stockId,
batch_no: batchNo,
inventory_type: inventoryType,
is_enabled: 1,
delete_at: null,
},
lock: transaction.LOCK.UPDATE,
transaction,
});
let storageId = null;
let storageBeforeQty = 0;
let storageAfterQty = 0;
if (storageRecord) {
storageBeforeQty = Number(storageRecord.quantity) || 0;
storageAfterQty = storageBeforeQty + inboundQty;
await ctx.model.Storage.update({
quantity: app.Sequelize.literal(`quantity + ${inboundQty}`),
available_quantity: app.Sequelize.literal(`available_quantity + ${inboundQty}`),
entry_order_id: eo.id,
inbound_time: new Date(),
inbound_order_no: eo.entry_order_code,
production_date: rdProductionDate,
expiry_date: rdExpiryDate,
receiving_id: rdReceivingId,
updated_at: new Date(),
}, {
where: { id: storageRecord.id },
transaction,
});
storageId = storageRecord.id;
if (isBinding) {
// 绑定模式:按 entryItem 维度写 Storage(与收货明细绑定,space_id 取第一条入库记录的库位)
const logForSpace = directLogs.find(l => String(l.stock_id) === String(stockId) && l.space_id);
const spaceId = logForSpace ? Number(logForSpace.space_id) : 1;
await writeStorageRecord(spaceId, inboundQty, rdReceivingId, rdProductionDate, rdExpiryDate);
} else {
storageBeforeQty = 0;
storageAfterQty = inboundQty;
const createdStorage = await ctx.model.Storage.create({
warehouse_id: warehouseId,
store_id: storeId,
space_id: spaceId,
stock_id: stockId,
batch_no: effectiveBatchNo,
inventory_type: inventoryType,
quantity: inboundQty,
locked_quantity: 0,
available_quantity: inboundQty,
inbound_time: new Date(),
inbound_order_no: eo.entry_order_code,
receiving_id: rdReceivingId,
entry_order_id: eo.id,
supplier_id: null,
supplier_batch_no: null,
production_date: rdProductionDate,
expiry_date: rdExpiryDate,
is_enabled: 1,
created_at: new Date(),
updated_at: new Date(),
}, { transaction });
storageId = createdStorage.id;
// 非绑定模式:按每个 InboundDirectLog 记录独立写 Storage,每条记录保有自己指定的 space_id 和 quantity
const stockDirectLogs = directLogs.filter(l => String(l.stock_id) === String(stockId) && Number(l.quantity) > 0);
for (const dlog of stockDirectLogs) {
const dlogSpaceId = dlog.space_id || 1;
const dlogQty = Number(dlog.quantity) || 0;
await writeStorageRecord(dlogSpaceId, dlogQty, null, null, null);
}
}
// 写入库位维度库存流水
await ctx.model.StorageTransaction.create({
transaction_type: 'INBOUND',
warehouse_id: warehouseId,
store_id: storeId,
stock_id: stockId,
storage_id: storageId,
space_id: spaceId,
quantity: inboundQty,
before_quantity: storageBeforeQty,
after_quantity: storageAfterQty,
batch_no: effectiveBatchNo,
inventory_type: inventoryType,
order_type: 'entry_order',
order_no: eo.entry_order_code,
order_id: eo.id,
operator_id: String(ctx.user?.employees_id || ctx.user?.id || ''),
operator_name: ctx.user?.employees_name || '',
remark: '导入/PDA直接入库提交',
created_at: new Date(),
}, { transaction });
}
// 将 pending 绑定升级为 committed(在 InboundDirectLog 中更新状态)

File diff suppressed because it is too large Load Diff