ming #1

Merged
liufumingren merged 558 commits from ming into main 2026-11-18 16:13:25 +08:00
2 changed files with 1131 additions and 143 deletions
Showing only changes of commit a69fd3f393 - Show all commits

View File

@ -373,23 +373,26 @@ class InboundDirectLogService extends Service {
// CREATED 人工导入允许超过计划数量 // CREATED 人工导入允许超过计划数量
const isManualImport = entryOrder.order_status === 'CREATED' && !isBindingMode; const isManualImport = entryOrder.order_status === 'CREATED' && !isBindingMode;
if (!isManualImport) { if (!isManualImport) {
const newTotal = existingTotal + effectiveQuantity; const maxAllowed = totalPlanQty - existingTotal;
if (newTotal > totalPlanQty) { if (maxAllowed <= 0) {
return { return {
ok: false, ok: false,
error: { error: {
code: 400, code: 400,
message: `入库数量超过计划:商品计划 ${totalPlanQty},已入库 ${existingTotal},本次 ${effectiveQuantity},合计 ${newTotal}`, message: `入库数量超过计划:商品计划 ${totalPlanQty},已入库 ${existingTotal},本次 ${effectiveQuantity},合计 ${existingTotal + effectiveQuantity}`,
data: { data: {
stock_id: stockId, stock_id: stockId,
plan_quantity: totalPlanQty, plan_quantity: totalPlanQty,
existing_quantity: existingTotal, existing_quantity: existingTotal,
requested_quantity: effectiveQuantity, requested_quantity: effectiveQuantity,
exceeded_by: newTotal - totalPlanQty, exceeded_by: effectiveQuantity,
}, },
}, },
}; };
} }
if (effectiveQuantity > maxAllowed) {
effectiveQuantity = maxAllowed;
}
} }
// 剩余可入库数 = 计划数 - max(已收货量, 已写入 direct_log 的待入库量) // 剩余可入库数 = 计划数 - max(已收货量, 已写入 direct_log 的待入库量)
@ -853,7 +856,7 @@ class InboundDirectLogService extends Service {
itemId: item.item_id || '', itemId: item.item_id || '',
inventoryType: item.inventory_type || 'ZP', inventoryType: item.inventory_type || 'ZP',
planQty: Number(item.count) || 0, planQty: Number(item.count) || 0,
actualQty: Number(item.count) || 0, actualQty: stockSummary[String(item.stock_id)] || 0,
})); }));
const syncResult = await this.qimenEntrySync({ const syncResult = await this.qimenEntrySync({
@ -957,12 +960,19 @@ class InboundDirectLogService extends Service {
transaction, transaction,
}); });
// 同一 stock_id 只写一次库存 // 同一 stock_id 只写一次 StockWarehouse 总库存(聚合量),但 Storage 按入库记录逐条写入
if (processedStockIds.has(String(stockId))) continue; const isStockWarehouseDone = processedStockIds.has(String(stockId));
if (isStockWarehouseDone && !isBinding) {
// 非绑定模式:StockWarehouse 已写但 Storage 还未写,不能 continue,继续写 Storage
} else if (isStockWarehouseDone) {
continue;
}
if (!isStockWarehouseDone) {
processedStockIds.add(String(stockId)); processedStockIds.add(String(stockId));
if (!isBinding) { if (!isBinding) {
nonBindingStockProcessed.add(String(stockId)); nonBindingStockProcessed.add(String(stockId));
} }
}
// 使用入库单号作为批次号;若有收货绑定则使用收货明细信息 // 使用入库单号作为批次号;若有收货绑定则使用收货明细信息
const rdReceivingId = bindInfo ? bindInfo.receiving_id : null; const rdReceivingId = bindInfo ? bindInfo.receiving_id : null;
@ -972,10 +982,8 @@ class InboundDirectLogService extends Service {
const batchNo = eo.entry_order_code; const batchNo = eo.entry_order_code;
const inventoryType = item.inventory_type || 'ZP'; const inventoryType = item.inventory_type || 'ZP';
// 从该 stock_id 的入库记录中取第一个非空 space_id,兜底 1 // StockWarehouse 总库存:每个 stock_id 聚合写入一次
const logForSpace = directLogs.find(l => String(l.stock_id) === String(stockId) && l.space_id); if (!isStockWarehouseDone) {
const spaceId = logForSpace ? Number(logForSpace.space_id) : 1;
// 锁定并更新 StockWarehouse 总库存 // 锁定并更新 StockWarehouse 总库存
const stockRecord = await ctx.model.StockWarehouse.findOne({ const stockRecord = await ctx.model.StockWarehouse.findOne({
where: { id: stockId, is_valid: 'Y', deleted_at: null }, where: { id: stockId, is_valid: 'Y', deleted_at: null },
@ -993,7 +1001,7 @@ class InboundDirectLogService extends Service {
const afterQuantity = Number(stockRecord.quantity) || 0; const afterQuantity = Number(stockRecord.quantity) || 0;
const afterLockedQuantity = Number(stockRecord.locked_quantity) || 0; const afterLockedQuantity = Number(stockRecord.locked_quantity) || 0;
// 写入库存流水 // 写入库存流水(聚合维度)
await ctx.service.systems.stockTransaction.create({ await ctx.service.systems.stockTransaction.create({
transaction_type: 'INBOUND', transaction_type: 'INBOUND',
warehouse_id: warehouseId, warehouse_id: warehouseId,
@ -1015,9 +1023,20 @@ class InboundDirectLogService extends Service {
created_at: new Date(), created_at: new Date(),
}, { transaction }); }, { transaction });
} }
}
// 创建/更新 Storage 库位库存 /**
const storageRecord = await ctx.model.Storage.findOne({ * 写入 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: { where: {
space_id: spaceId, space_id: spaceId,
stock_id: stockId, stock_id: stockId,
@ -1030,32 +1049,32 @@ class InboundDirectLogService extends Service {
transaction, transaction,
}); });
let storageId = null; let sId = null;
let storageBeforeQty = 0; let sBeforeQty = 0;
let storageAfterQty = 0; let sAfterQty = 0;
if (storageRecord) { if (sRecord) {
storageBeforeQty = Number(storageRecord.quantity) || 0; sBeforeQty = Number(sRecord.quantity) || 0;
storageAfterQty = storageBeforeQty + inboundQty; sAfterQty = sBeforeQty + qty;
await ctx.model.Storage.update({ await ctx.model.Storage.update({
quantity: app.Sequelize.literal(`quantity + ${inboundQty}`), quantity: app.Sequelize.literal(`quantity + ${qty}`),
available_quantity: app.Sequelize.literal(`available_quantity + ${inboundQty}`), available_quantity: app.Sequelize.literal(`available_quantity + ${qty}`),
entry_order_id: eo.id, entry_order_id: eo.id,
inbound_time: new Date(), inbound_time: new Date(),
inbound_order_no: eo.entry_order_code, inbound_order_no: eo.entry_order_code,
production_date: rdProductionDate, production_date: prodDate,
expiry_date: rdExpiryDate, expiry_date: expDate,
receiving_id: rdReceivingId, receiving_id: rdId,
updated_at: new Date(), updated_at: new Date(),
}, { }, {
where: { id: storageRecord.id }, where: { id: sRecord.id },
transaction, transaction,
}); });
storageId = storageRecord.id; sId = sRecord.id;
} else { } else {
storageBeforeQty = 0; sBeforeQty = 0;
storageAfterQty = inboundQty; sAfterQty = qty;
const createdStorage = await ctx.model.Storage.create({ const createdStorage = await ctx.model.Storage.create({
warehouse_id: warehouseId, warehouse_id: warehouseId,
@ -1064,22 +1083,22 @@ class InboundDirectLogService extends Service {
stock_id: stockId, stock_id: stockId,
batch_no: effectiveBatchNo, batch_no: effectiveBatchNo,
inventory_type: inventoryType, inventory_type: inventoryType,
quantity: inboundQty, quantity: qty,
locked_quantity: 0, locked_quantity: 0,
available_quantity: inboundQty, available_quantity: qty,
inbound_time: new Date(), inbound_time: new Date(),
inbound_order_no: eo.entry_order_code, inbound_order_no: eo.entry_order_code,
receiving_id: rdReceivingId, receiving_id: rdId,
entry_order_id: eo.id, entry_order_id: eo.id,
supplier_id: null, supplier_id: null,
supplier_batch_no: null, supplier_batch_no: null,
production_date: rdProductionDate, production_date: prodDate,
expiry_date: rdExpiryDate, expiry_date: expDate,
is_enabled: 1, is_enabled: 1,
created_at: new Date(), created_at: new Date(),
updated_at: new Date(), updated_at: new Date(),
}, { transaction }); }, { transaction });
storageId = createdStorage.id; sId = createdStorage.id;
} }
// 写入库位维度库存流水 // 写入库位维度库存流水
@ -1088,11 +1107,11 @@ class InboundDirectLogService extends Service {
warehouse_id: warehouseId, warehouse_id: warehouseId,
store_id: storeId, store_id: storeId,
stock_id: stockId, stock_id: stockId,
storage_id: storageId, storage_id: sId,
space_id: spaceId, space_id: spaceId,
quantity: inboundQty, quantity: qty,
before_quantity: storageBeforeQty, before_quantity: sBeforeQty,
after_quantity: storageAfterQty, after_quantity: sAfterQty,
batch_no: effectiveBatchNo, batch_no: effectiveBatchNo,
inventory_type: inventoryType, inventory_type: inventoryType,
order_type: 'entry_order', order_type: 'entry_order',
@ -1103,6 +1122,22 @@ class InboundDirectLogService extends Service {
remark: '导入/PDA直接入库提交', remark: '导入/PDA直接入库提交',
created_at: new Date(), created_at: new Date(),
}, { transaction }); }, { transaction });
};
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 {
// 非绑定模式:按每个 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);
}
}
} }
// 将 pending 绑定升级为 committed(在 InboundDirectLog 中更新状态) // 将 pending 绑定升级为 committed(在 InboundDirectLog 中更新状态)

File diff suppressed because it is too large Load Diff