feat(storage): 新增 storage.inventory.directPutaway 收货单直入库存接口(一步完成)
- systems/storage/inventory.js#directPutaway:一步写库存并生成入库单(SCRK,直接 COMPLETED) - 入库单明细与收货明细一一对应(不合并),按收货明细库位 space_id 入库,批次/效期取收货明细 - 悲观行锁更新 wms_stock_warehouse 总库存 + 写 wms_storage 库位库存(同键累加) - 全量日志:wms_stock_transaction(SCRK)+ wms_storage_transaction(entry_order)+ wms_inbound_direct_log(committed)+ 操作日志(entry_order/storage/stock 三维度) - 收货单锁定防重、重复直入 400 拒绝 - systemMethods/storage/inventory.js:注册 directPutaway 入口(_noNeedAuth) - store_warehouse.js:POLICY_CONFIG_FIELDS 白名单新增 allow_receiving_direct_putaway 开关(修复 getter sanitize 过滤导致开关读不到) - database/migrations/20260824-add-direct-putaway-permission.js:新增 API 权限记录(幂等,含 down) - methods/system.json / methods/changes/system.json:接口文档与变更记录 与 quickPutaway(转入库单)并存:转单链路为 quickPutaway→entry.addItem→entry.commit; 直入为 directPutaway 一步完成。两链路开发环境真实接口全量对比(15+ 表)一致。
This commit is contained in:
parent
705678487d
commit
0fe7b724ed
@ -37,6 +37,14 @@ module.exports = app => {
|
||||
required: false,
|
||||
description: '控制该货主在当前仓库下是否允许使用收货单快捷入库',
|
||||
}),
|
||||
Object.freeze({
|
||||
key: 'allow_receiving_direct_putaway',
|
||||
name: '收货单直入库存',
|
||||
type: 'flag',
|
||||
default: 0,
|
||||
required: false,
|
||||
description: '控制该货主在当前仓库下是否允许使用收货单直入库存(一步写库存并生成入库单)',
|
||||
}),
|
||||
Object.freeze({
|
||||
key: 'allow_mix_batch',
|
||||
name: '允许混批',
|
||||
|
||||
@ -15,7 +15,7 @@ class InventoryMethodService extends MainService {
|
||||
const _noNeedLogin = '';
|
||||
const _noNeedRight = 'list,export,exportTaskProgress,listByStockId';
|
||||
const _noNeedLog = 'list,export,exportTaskProgress,listByStockId';
|
||||
const _noNeedAuth = 'list,quickPutaway,export,exportTaskProgress,listByStockId';
|
||||
const _noNeedAuth = 'list,quickPutaway,directPutaway,export,exportTaskProgress,listByStockId';
|
||||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||||
}
|
||||
|
||||
@ -125,6 +125,21 @@ class InventoryMethodService extends MainService {
|
||||
const result = await service.systems.storage.inventory.quickPutaway({ receiving_id });
|
||||
return this.success('ok', result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收货单直入库存(一步完成):直接写 wms_storage 库位库存并生成入库单。
|
||||
* 入库单明细与收货明细一一对应,按收货明细的库位 space_id 入库。
|
||||
* 要求货主配置中允许收货单直入库存,否则直接返回失败。
|
||||
* @param {Object} data 请求体
|
||||
* @param {number} data.receiving_id 收货单ID(必填)
|
||||
* @return {Promise<Object>} 统一 success 包装,返回创建的入库单信息
|
||||
*/
|
||||
async directPutaway(data = {}) {
|
||||
const { service } = this;
|
||||
const { receiving_id } = data;
|
||||
const result = await service.systems.storage.inventory.directPutaway({ receiving_id });
|
||||
return this.success('ok', result);
|
||||
}
|
||||
/**
|
||||
* 提交库存导出异步任务(入参与 list 一致,复用相同的 where/sort/group_by_batch 语义)。
|
||||
*
|
||||
|
||||
@ -1941,6 +1941,505 @@ class InventoryService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 收货单直入库存(一步完成):直接写入 wms_storage 库位库存并生成入库单。
|
||||
*
|
||||
* 与 quickPutaway(转入库单,不写库存,明细按 stock_id 合并)的区别:
|
||||
* - 入库单明细与收货单明细一一对应(不合并),每行按收货明细的库位 space_id 入库;
|
||||
* - 直接写 wms_storage / wms_stock_warehouse 库存并写入出入库流水,入库单一步置为 COMPLETED。
|
||||
*
|
||||
* @param {Object} data 请求体
|
||||
* @param {number} data.receiving_id 收货单ID(必填)
|
||||
* @return {Promise<Object>} code/data/message 结构,data 中包含创建的入库单信息
|
||||
*/
|
||||
async directPutaway(data = {}) {
|
||||
const { ctx, app } = this;
|
||||
const receivingId = Number(data.receiving_id || 0);
|
||||
|
||||
// ────────── 1. 参数校验 ──────────
|
||||
if (!receivingId) {
|
||||
return { code: 400, message: 'receiving_id 必填', data: null };
|
||||
}
|
||||
|
||||
const warehouseId = ctx.user.store_id;
|
||||
if (!warehouseId) {
|
||||
return { code: 400, message: '无法获取当前仓库信息', data: null };
|
||||
}
|
||||
|
||||
// ────────── 2. 加载收货单主表 ──────────
|
||||
const receiving = await ctx.model.Receiving.findOne({
|
||||
where: { id: receivingId, warehouse_id: warehouseId, is_deleted: false },
|
||||
});
|
||||
|
||||
if (!receiving) {
|
||||
return { code: 404, message: `收货单不存在或无权访问: receiving_id=${receivingId}`, data: null };
|
||||
}
|
||||
|
||||
if (receiving.status === 'cancelled') {
|
||||
return { code: 400, message: '收货单已作废,不允许直入库存', data: null };
|
||||
}
|
||||
|
||||
if (receiving.status !== 'completed') {
|
||||
return { code: 400, message: `收货单状态不允许直入库存,当前状态: ${receiving.status},仅 completed 状态可操作`, data: null };
|
||||
}
|
||||
|
||||
if (!receiving.is_review) {
|
||||
return { code: 400, message: '收货单未审核,不允许直入库存', data: null };
|
||||
}
|
||||
|
||||
const storeId = receiving.store_id;
|
||||
|
||||
// ────────── 3. 检查货主配置:是否允许收货单直入库存 ──────────
|
||||
const storeWarehouse = await ctx.model.StoreWarehouse.findOne({
|
||||
where: { store_id: storeId, warehouse_id: warehouseId, warehouse_use_status: 1 },
|
||||
attributes: [ 'id', 'policy_config' ],
|
||||
});
|
||||
|
||||
if (!storeWarehouse) {
|
||||
return { code: 400, message: '货主仓库签约关系不存在', data: null };
|
||||
}
|
||||
|
||||
const policyConfig = storeWarehouse.policy_config || {};
|
||||
const allowDirectPutaway = Number(policyConfig.allow_receiving_direct_putaway) === 1;
|
||||
|
||||
if (!allowDirectPutaway) {
|
||||
return { code: 400, message: '货主未开启收货单直入库存功能,请联系管理员开启', data: null };
|
||||
}
|
||||
|
||||
// ────────── 4. 加载收货单明细 ──────────
|
||||
const receivingDetails = await ctx.model.ReceivingDetail.findAll({
|
||||
where: { receiving_id: receivingId },
|
||||
attributes: [
|
||||
'id', 'system_product_id', 'actual_quantity', 'product_name',
|
||||
'batch_number', 'production_date', 'expiry_date',
|
||||
'space_id', 'storage_status', 'stored_quantity',
|
||||
],
|
||||
});
|
||||
|
||||
if (!receivingDetails || receivingDetails.length === 0) {
|
||||
return { code: 400, message: '收货单无明细,无法直入库存', data: null };
|
||||
}
|
||||
|
||||
// 过滤出需要入库的明细(排除已全部入库的)
|
||||
const pendingDetails = receivingDetails.filter(
|
||||
d => d.storage_status !== 'all' && Number(d.actual_quantity) > 0
|
||||
);
|
||||
|
||||
if (pendingDetails.length === 0) {
|
||||
return { code: 400, message: '收货单所有明细均已入库,禁止重复直入库存', data: null };
|
||||
}
|
||||
|
||||
// 待入库明细必须全部已匹配系统商品
|
||||
const unmatchedDetails = pendingDetails.filter(d => !d.system_product_id);
|
||||
if (unmatchedDetails.length > 0) {
|
||||
const names = unmatchedDetails.map(d => d.product_name).join('、');
|
||||
return { code: 400, message: `以下商品未匹配系统商品,请先绑定: ${names}`, data: null };
|
||||
}
|
||||
|
||||
// ────────── 5. 批量查询 stock_warehouse 校验商品有效 ──────────
|
||||
const stockIds = [ ...new Set(pendingDetails.map(d => Number(d.system_product_id))) ];
|
||||
const stockRows = await ctx.model.StockWarehouse.findAll({
|
||||
where: { id: { [app.Sequelize.Op.in]: stockIds }, warehouse_id: warehouseId },
|
||||
attributes: [ 'id', 'item_code', 'item_name', 'stock_unit', 'is_valid' ],
|
||||
raw: true,
|
||||
});
|
||||
const stockMap = new Map();
|
||||
for (const row of stockRows) {
|
||||
stockMap.set(Number(row.id), row);
|
||||
}
|
||||
|
||||
const missingStockIds = stockIds.filter(sid => !stockMap.has(sid));
|
||||
if (missingStockIds.length > 0) {
|
||||
return {
|
||||
code: 400,
|
||||
message: `以下商品在当前仓库中不存在或已失效: stock_id=${missingStockIds.join(',')}`,
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
// ────────── 6. 开事务:创建入库单 + 直接写库存 ──────────
|
||||
const transaction = await ctx.model.transaction();
|
||||
|
||||
try {
|
||||
// 锁定收货单行,防止并发重复直入
|
||||
const lockedReceiving = await ctx.model.Receiving.findOne({
|
||||
where: { id: receivingId },
|
||||
lock: transaction.LOCK.UPDATE,
|
||||
transaction,
|
||||
attributes: [ 'id', 'storage_status' ],
|
||||
});
|
||||
if (!lockedReceiving) {
|
||||
await transaction.rollback();
|
||||
return { code: 404, message: '收货单不存在', data: null };
|
||||
}
|
||||
if (lockedReceiving.storage_status === 'all') {
|
||||
await transaction.rollback();
|
||||
return { code: 400, message: '收货单已全部入库,禁止重复直入库存', data: null };
|
||||
}
|
||||
|
||||
// 获取货主和仓库名称快照
|
||||
const storeData = await ctx.service.systems.store.getStoreName(storeId);
|
||||
const storeName = (storeData.code === 200 && storeData.data) ? storeData.data.company_name : '';
|
||||
const warehouseData = await ctx.service.systems.store.getStoreName(warehouseId);
|
||||
const warehouseName = (warehouseData.code === 200 && warehouseData.data) ? warehouseData.data.company_name : '';
|
||||
|
||||
// 生成入库单号(SCRK)
|
||||
const entryOrderCode = await this._generateEntryOrderCode(warehouseId, storeId, transaction);
|
||||
|
||||
// 创建入库单主表(直入:一步完成,直接 COMPLETED)
|
||||
const entryOrder = await ctx.model.EntryOrder.create({
|
||||
entry_order_code: entryOrderCode,
|
||||
order_type: 'SCRK',
|
||||
total_order_lines: pendingDetails.length,
|
||||
order_create_time: new Date(),
|
||||
source_type: 'WMS',
|
||||
order_status: 'COMPLETED',
|
||||
sync_status: 'SUCCESS',
|
||||
is_enabled: 1,
|
||||
warehouse_id: warehouseId,
|
||||
store_id: storeId,
|
||||
store_name: storeName,
|
||||
warehouse_name: warehouseName,
|
||||
remark: `收货单${receiving.receiving_number}直入库存`,
|
||||
expense_record: 0,
|
||||
}, { transaction });
|
||||
|
||||
const entryOrderId = entryOrder.id;
|
||||
|
||||
// ────────── 7. 创建入库单明细:与收货明细一一对应(不合并) ──────────
|
||||
// 记录 收货明细ID → 入库明细ID 映射,用于写入 wms_inbound_direct_log
|
||||
const entryItemIdByDetailId = new Map();
|
||||
for (let i = 0; i < pendingDetails.length; i++) {
|
||||
const detail = pendingDetails[i];
|
||||
const stockInfo = stockMap.get(Number(detail.system_product_id));
|
||||
const detailQty = Number(detail.actual_quantity) || 0;
|
||||
|
||||
const entryItem = await ctx.model.EntryItems.create({
|
||||
order_id: entryOrderId,
|
||||
out_biz_code: '',
|
||||
order_line_no: i + 1,
|
||||
owner_code: '',
|
||||
item_code: stockInfo ? stockInfo.item_code : '',
|
||||
item_id: 0,
|
||||
count: detailQty,
|
||||
inventory_type: 'ZP',
|
||||
stock_id: Number(detail.system_product_id),
|
||||
received_quantity: detailQty,
|
||||
confirm_quantity: 0,
|
||||
}, { transaction });
|
||||
|
||||
entryItemIdByDetailId.set(detail.id, entryItem.id);
|
||||
}
|
||||
|
||||
// ────────── 8. 写入 wms_entry_order_receiving 主表级绑定 ──────────
|
||||
await ctx.model.EntryOrderReceiving.create({
|
||||
entry_order_id: entryOrderId,
|
||||
receiving_id: receivingId,
|
||||
}, { transaction });
|
||||
|
||||
// ────────── 9. 更新 StockWarehouse 总库存(按 stock_id 聚合) ──────────
|
||||
const stockQtyMap = new Map();
|
||||
for (const detail of pendingDetails) {
|
||||
const sid = Number(detail.system_product_id);
|
||||
const qty = Number(detail.actual_quantity) || 0;
|
||||
stockQtyMap.set(sid, (stockQtyMap.get(sid) || 0) + qty);
|
||||
}
|
||||
|
||||
// stock(wms_stock_warehouse)变更摘要,供操作日志记录
|
||||
const stockChangeSummaries = [];
|
||||
|
||||
for (const [ sid, inboundQty ] of stockQtyMap) {
|
||||
// 悲观行锁
|
||||
const stockRecord = await ctx.model.StockWarehouse.findOne({
|
||||
where: { id: sid, is_valid: 'Y', deleted_at: null },
|
||||
lock: transaction.LOCK.UPDATE,
|
||||
transaction,
|
||||
});
|
||||
|
||||
if (!stockRecord) {
|
||||
throw new Error(`商品库存记录不存在或已失效,无法直入库存: stock_id=${sid}`);
|
||||
}
|
||||
|
||||
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 });
|
||||
|
||||
const afterQuantity = Number(stockRecord.quantity) || 0;
|
||||
const afterLockedQuantity = Number(stockRecord.locked_quantity) || 0;
|
||||
|
||||
// 商品维度流水(SCRK)
|
||||
await ctx.service.systems.stockTransaction.create({
|
||||
transaction_type: 'INBOUND',
|
||||
warehouse_id: warehouseId,
|
||||
store_id: storeId,
|
||||
stock_id: sid,
|
||||
quantity: inboundQty,
|
||||
batch_no: entryOrderCode,
|
||||
inventory_type: 'ZP',
|
||||
order_type: 'SCRK',
|
||||
order_no: entryOrderCode,
|
||||
entry_order_id: entryOrderId,
|
||||
receiving_id: receivingId,
|
||||
operator_id: String(ctx.user?.employees_id || ctx.user?.id || ''),
|
||||
operator_name: ctx.user?.employees_name || '',
|
||||
remark: '收货单直入库存',
|
||||
before_quantity: beforeQuantity,
|
||||
after_quantity: afterQuantity,
|
||||
before_locked_quantity: beforeLockedQuantity,
|
||||
after_locked_quantity: afterLockedQuantity,
|
||||
created_at: new Date(),
|
||||
}, { transaction });
|
||||
|
||||
// 收集 stock 变更摘要(wms_stock_warehouse 总量变更)
|
||||
stockChangeSummaries.push({
|
||||
stock_id: sid,
|
||||
before_quantity: beforeQuantity,
|
||||
after_quantity: afterQuantity,
|
||||
change_quantity: inboundQty,
|
||||
});
|
||||
}
|
||||
|
||||
// ────────── 10. 写 wms_storage 库位库存(按收货明细库位 space_id 逐明细写入) ──────────
|
||||
// storage(wms_storage)变更笔数,供操作日志记录
|
||||
let storageWriteCount = 0;
|
||||
|
||||
for (const detail of pendingDetails) {
|
||||
const sid = Number(detail.system_product_id);
|
||||
const qty = Number(detail.actual_quantity) || 0;
|
||||
if (qty <= 0) continue;
|
||||
|
||||
// 收货明细有库位则按其库位入库,否则使用默认库位
|
||||
const spaceId = Number(detail.space_id) > 0 ? Number(detail.space_id) : 1;
|
||||
const batchNo = detail.batch_number || entryOrderCode;
|
||||
const productionDate = detail.production_date || null;
|
||||
const expiryDate = detail.expiry_date || null;
|
||||
const inventoryType = 'ZP';
|
||||
|
||||
// 查找 库位+SKU+批次+类型 已有的库存记录(悲观行锁)
|
||||
const sRecord = await ctx.model.Storage.findOne({
|
||||
where: {
|
||||
space_id: spaceId,
|
||||
stock_id: sid,
|
||||
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;
|
||||
let sBeforeLockedQty = 0;
|
||||
let sAfterLockedQty = 0;
|
||||
|
||||
if (sRecord) {
|
||||
if (Number(sRecord.stock_id) !== Number(sid)) {
|
||||
throw new Error(
|
||||
`直入库存归属冲突: storage_id=${sRecord.id} 当前 stock_id=${sRecord.stock_id}, ` +
|
||||
`本次入库 stock_id=${sid}, space_id=${spaceId}, batch_no=${batchNo}`
|
||||
);
|
||||
}
|
||||
sBeforeQty = Number(sRecord.quantity) || 0;
|
||||
sAfterQty = sBeforeQty + qty;
|
||||
sBeforeLockedQty = Number(sRecord.locked_quantity) || 0;
|
||||
sAfterLockedQty = sBeforeLockedQty;
|
||||
|
||||
await ctx.model.Storage.update({
|
||||
quantity: app.Sequelize.literal(`quantity + ${qty}`),
|
||||
available_quantity: app.Sequelize.literal(`available_quantity + ${qty}`),
|
||||
entry_order_id: entryOrderId,
|
||||
inbound_time: new Date(),
|
||||
inbound_order_no: entryOrderCode,
|
||||
production_date: productionDate,
|
||||
expiry_date: expiryDate,
|
||||
receiving_id: receivingId,
|
||||
updated_at: new Date(),
|
||||
}, {
|
||||
where: { id: sRecord.id },
|
||||
transaction,
|
||||
});
|
||||
sId = sRecord.id;
|
||||
} else {
|
||||
const createdStorage = await ctx.model.Storage.create({
|
||||
warehouse_id: warehouseId,
|
||||
store_id: storeId,
|
||||
space_id: spaceId,
|
||||
stock_id: sid,
|
||||
batch_no: batchNo,
|
||||
inventory_type: inventoryType,
|
||||
quantity: qty,
|
||||
locked_quantity: 0,
|
||||
available_quantity: qty,
|
||||
inbound_time: new Date(),
|
||||
inbound_order_no: entryOrderCode,
|
||||
receiving_id: receivingId,
|
||||
entry_order_id: entryOrderId,
|
||||
supplier_id: null,
|
||||
supplier_batch_no: null,
|
||||
production_date: productionDate,
|
||||
expiry_date: expiryDate,
|
||||
is_enabled: 1,
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
}, { transaction });
|
||||
sId = createdStorage.id;
|
||||
sAfterQty = qty;
|
||||
sAfterLockedQty = 0;
|
||||
}
|
||||
|
||||
storageWriteCount += 1;
|
||||
|
||||
// 库位维度流水(order_type 与现有 storage.entry.commit 链路一致:entry_order)
|
||||
await ctx.model.StorageTransaction.create({
|
||||
transaction_type: 'INBOUND',
|
||||
warehouse_id: warehouseId,
|
||||
store_id: storeId,
|
||||
stock_id: sid,
|
||||
storage_id: sId,
|
||||
space_id: spaceId,
|
||||
quantity: qty,
|
||||
before_quantity: sBeforeQty,
|
||||
after_quantity: sAfterQty,
|
||||
before_locked_quantity: sBeforeLockedQty,
|
||||
after_locked_quantity: sAfterLockedQty,
|
||||
batch_no: batchNo,
|
||||
inventory_type: inventoryType,
|
||||
order_type: 'entry_order',
|
||||
order_no: entryOrderCode,
|
||||
order_id: entryOrderId,
|
||||
operator_id: String(ctx.user?.employees_id || ctx.user?.id || ''),
|
||||
operator_name: ctx.user?.employees_name || '',
|
||||
remark: '收货单直入库存',
|
||||
created_at: new Date(),
|
||||
}, { transaction });
|
||||
|
||||
// 写入导入/PDA 入库记录(status=committed,与 storage.entry.commit 链路一致)。
|
||||
// 该表被 storage.receiving.scanDetail、storage.entry.getDirectInboundItems、
|
||||
// getReceivingDetailsForBind 的可用数量占用统计等读取,直入也必须写入以保持口径一致。
|
||||
await ctx.model.InboundDirectLog.create({
|
||||
warehouse_id: warehouseId,
|
||||
store_id: storeId,
|
||||
stock_id: sid,
|
||||
quantity: qty,
|
||||
inbound_type: 'import',
|
||||
entry_order_id: entryOrderId,
|
||||
entry_order_code: entryOrderCode,
|
||||
entry_item_id: entryItemIdByDetailId.get(detail.id) || 0,
|
||||
receiving_detail_id: detail.id,
|
||||
space_id: spaceId,
|
||||
status: 'committed',
|
||||
remark: '收货单直入库存',
|
||||
operator_id: ctx.user?.employees_id || ctx.user?.id || null,
|
||||
operator_name: ctx.user?.employees_name || '',
|
||||
}, { transaction });
|
||||
}
|
||||
|
||||
// ────────── 11. 回填收货单明细与主表入库状态 ──────────
|
||||
for (const detail of pendingDetails) {
|
||||
await ctx.model.ReceivingDetail.update({
|
||||
stored_quantity: Number(detail.actual_quantity) || 0,
|
||||
storage_status: 'all',
|
||||
updated_at: new Date(),
|
||||
}, {
|
||||
where: { id: detail.id },
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
|
||||
// 收货单主表 storage_status:统计全部明细状态
|
||||
const allStatusRows = await ctx.model.ReceivingDetail.findAll({
|
||||
where: { receiving_id: receivingId },
|
||||
attributes: [ 'storage_status' ],
|
||||
raw: true,
|
||||
transaction,
|
||||
});
|
||||
const allStored = allStatusRows.length > 0
|
||||
&& allStatusRows.every(d => d.storage_status === 'all');
|
||||
await ctx.model.Receiving.update({
|
||||
storage_status: allStored ? 'all' : 'partial',
|
||||
updated_at: new Date(),
|
||||
}, {
|
||||
where: { id: receivingId },
|
||||
transaction,
|
||||
});
|
||||
|
||||
// ────────── 12. 操作日志(entry_order / storage / stock 三种业务维度全量记录) ──────────
|
||||
try {
|
||||
const totalDirectQty = pendingDetails.reduce((sum, d) => sum + (Number(d.actual_quantity) || 0), 0);
|
||||
await ctx.service.systems.operationLog.createOperationLogs([
|
||||
{
|
||||
businessType: 'entry_order',
|
||||
businessId: entryOrderId,
|
||||
operationCode: 'ENTRY_ORDER_DIRECT_PUTAWAY',
|
||||
operator: {
|
||||
id: ctx.user?.employees_id || ctx.user?.id,
|
||||
name: ctx.user?.employees_name || '',
|
||||
},
|
||||
extras: {
|
||||
warehouseId,
|
||||
message: `收货单${receiving.receiving_number}直入库存完成,生成入库单${entryOrderCode},共 ${pendingDetails.length} 行明细、${totalDirectQty} 件`,
|
||||
},
|
||||
},
|
||||
{
|
||||
businessType: 'storage',
|
||||
businessId: entryOrderId,
|
||||
operationCode: 'STORAGE_DIRECT_PUTAWAY',
|
||||
operator: {
|
||||
id: ctx.user?.employees_id || ctx.user?.id,
|
||||
name: ctx.user?.employees_name || '',
|
||||
},
|
||||
extras: {
|
||||
warehouseId,
|
||||
message: `直入库存写入 wms_storage 库位库存 ${storageWriteCount} 笔(按收货明细库位),共 ${totalDirectQty} 件,入库单号 ${entryOrderCode}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
businessType: 'stock',
|
||||
businessId: entryOrderId,
|
||||
operationCode: 'STOCK_DIRECT_PUTAWAY',
|
||||
operator: {
|
||||
id: ctx.user?.employees_id || ctx.user?.id,
|
||||
name: ctx.user?.employees_name || '',
|
||||
},
|
||||
extras: {
|
||||
warehouseId,
|
||||
message: `直入库存更新 wms_stock_warehouse 总库存 ${stockChangeSummaries.length} 个 SKU,共 ${totalDirectQty} 件,变更明细: ${JSON.stringify(stockChangeSummaries)}`,
|
||||
},
|
||||
},
|
||||
], { transaction });
|
||||
} catch (logErr) {
|
||||
ctx.logger.warn('[inventory.directPutaway] 写入操作日志失败,不影响业务', logErr);
|
||||
}
|
||||
|
||||
// ────────── 13. 提交事务 ──────────
|
||||
await transaction.commit();
|
||||
|
||||
ctx.logger.info(`[inventory.directPutaway] 直入库存完成 entryOrderId=${entryOrderId} receivingId=${receivingId} lines=${pendingDetails.length}`);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: '直入库存成功',
|
||||
data: {
|
||||
entry_order_id: entryOrderId,
|
||||
entry_order_code: entryOrderCode,
|
||||
order_status: 'COMPLETED',
|
||||
receiving_id: receivingId,
|
||||
total_lines: pendingDetails.length,
|
||||
total_quantity: pendingDetails.reduce((sum, d) => sum + (Number(d.actual_quantity) || 0), 0),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
await transaction.rollback();
|
||||
ctx.logger.error(`[inventory.directPutaway] 直入库存异常 receivingId=${receivingId}: ${error.message}`, error.stack);
|
||||
return { code: 500, message: `系统异常: ${error.message}`, data: null };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出库存列表(异步任务内部调用,生成 Excel 并上传)。
|
||||
*
|
||||
|
||||
@ -0,0 +1,52 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 新增 storage.inventory.directPutaway(收货单直入库存)API 权限记录。
|
||||
*
|
||||
* 幂等设计:先查询 path + type='api' 是否已存在,已存在则跳过。
|
||||
*/
|
||||
|
||||
const TABLE = 'wms_permission';
|
||||
|
||||
module.exports.up = async queryInterface => {
|
||||
const permissions = [
|
||||
{ path: 'storage.inventory.directPutaway', meta_title: '收货单直入库存' },
|
||||
];
|
||||
|
||||
const paths = permissions.map(p => p.path);
|
||||
const placeholders = paths.map(() => '?').join(', ');
|
||||
const [ rows ] = await queryInterface.sequelize.query(
|
||||
`SELECT path FROM \`${TABLE}\` WHERE type = 'api' AND path IN (${placeholders})`,
|
||||
{ replacements: paths }
|
||||
);
|
||||
const existingPaths = new Set(rows.map(r => r.path));
|
||||
|
||||
const toInsert = permissions.filter(p => !existingPaths.has(p.path));
|
||||
|
||||
if (toInsert.length > 0) {
|
||||
const values = toInsert
|
||||
.map(
|
||||
p => `('${p.path.replace(/'/g, "\\'")}', '${p.path.replace(/'/g, "\\'")}', '${p.meta_title.replace(/'/g, "\\'")}', 'api', 1, NOW(), NOW())`
|
||||
)
|
||||
.join(',\n');
|
||||
|
||||
await queryInterface.sequelize.query(`
|
||||
INSERT INTO \`${TABLE}\` (path, name, meta_title, type, status, created_at, update_at)
|
||||
VALUES ${values}
|
||||
`);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[Migration] wms_permission API 权限新增:共 ${permissions.length} 条,` +
|
||||
`已存在 ${permissions.length - toInsert.length} 条,实际插入 ${toInsert.length} 条`
|
||||
);
|
||||
};
|
||||
|
||||
module.exports.down = async queryInterface => {
|
||||
const paths = [ 'storage.inventory.directPutaway' ];
|
||||
const placeholders = paths.map(() => '?').join(', ');
|
||||
await queryInterface.sequelize.query(
|
||||
`DELETE FROM \`${TABLE}\` WHERE type = 'api' AND path IN (${placeholders})`,
|
||||
{ replacements: paths }
|
||||
);
|
||||
};
|
||||
@ -6885,5 +6885,24 @@
|
||||
"代码变更:systemMethods/storage/inventory.js#list 新增 extractStockQueryVirtualFilter(从 where 提取并移除 stock_query);systems/storage/inventory.js#getInventoryList 新增 stockQuery 参数与 resolveStockQueryToStockIds;methods/system.json 同步 where 参数文档(export 注明暂不支持 stock_query)。"
|
||||
],
|
||||
"module": "storage"
|
||||
},
|
||||
{
|
||||
"version": "2026.08.24-storage.inventory.directPutaway",
|
||||
"date": "2026-08-24",
|
||||
"type": "added",
|
||||
"summary": "新增 storage.inventory.directPutaway 接口:收货单直入库存(一步完成),直接写 wms_storage 并生成入库单",
|
||||
"methods": [
|
||||
"storage.inventory.directPutaway"
|
||||
],
|
||||
"notes": [
|
||||
"新增收货单直入库存接口,与 storage.inventory.quickPutaway(转入库单,不写库存)并存,名称不交换保持兼容。",
|
||||
"核心流程:校验收货单(completed + is_review=true,未作废)→ 检查货主配置 policy_config.allow_receiving_direct_putaway=1 → 过滤未入库明细(storage_status!=all)并校验全部已匹配系统商品 → 事务内锁定收货单行防并发 → 创建入库单 SCRK(order_status=COMPLETED、sync_status=SUCCESS,一步完成)→ 入库单明细与收货明细一一对应(不合并,每行 count/received_quantity=actual_quantity)→ 写入 wms_entry_order_receiving 主表级绑定 → 按 stock_id 聚合悲观行锁更新 wms_stock_warehouse 总库存并写 wms_stock_transaction(order_type=SCRK,与现有链路一致)→ 按收货明细库位 space_id 逐明细写入/累加 wms_storage 并写 wms_storage_transaction(order_type=entry_order,与现有 storage.entry.commit 链路一致)→ 逐明细写入 wms_inbound_direct_log(entry_item_id/receiving_detail_id/space_id 齐全,status=committed、inbound_type=import,与 storage.entry.commit 链路口径一致,保证 scanDetail/可用数量占用统计等读取链路数据一致)→ 回填收货明细 storage_status=all/stored_quantity 与主表 storage_status(all/partial)→ 操作日志。",
|
||||
"日志全量写入:storage 变更记录(wms_storage_transaction,逐明细含 before/after 数量与锁定量)、stock 变更记录(wms_stock_transaction,按 SKU 聚合含 before/after 数量与锁定量)、stock 变更(wms_stock_warehouse 悲观行锁 increment quantity);操作日志(wms_operation_log)同时写三条:businessType=entry_order(ENTRY_ORDER_DIRECT_PUTAWAY)、businessType=storage(STORAGE_DIRECT_PUTAWAY,记录库位库存写入笔数与数量)、businessType=stock(STOCK_DIRECT_PUTAWAY,记录各 SKU 总量变更前后值摘要)。",
|
||||
"库位规则:明细 space_id 有效则按其库位入库,否则回退默认库位 1;批次取收货明细 batch_number,无批次则以入库单号作为批次号;生产/效期日期取收货明细 production_date/expiry_date。",
|
||||
"与 quickPutaway 的区别:directPutaway 一步直接写库存(入库单 COMPLETED),quickPutaway 仅转入库单(PENDING)不写库存,需再走 storage.entry.commit 才写库存。",
|
||||
"货主开关:policy_config 新增 allow_receiving_direct_putaway(默认未开启时 400 拒绝),与 allow_receiving_quick_putaway 相互独立;app/model/store_warehouse.js 的 POLICY_CONFIG_FIELDS 白名单已补充该字段(否则模型 getter sanitizeOnly 会过滤掉该键导致开关无法读取)。",
|
||||
"代码变更:app/service/systems/storage/inventory.js#directPutaway、app/service/systemMethods/storage/inventory.js#directPutaway(_noNeedAuth 新增 directPutaway)、methods/system.json 新增 method 文档、database/migrations/20260824 新增权限记录(path=storage.inventory.directPutaway)。"
|
||||
],
|
||||
"module": "storage"
|
||||
}
|
||||
]
|
||||
|
||||
@ -23566,6 +23566,107 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"method": "storage.inventory.directPutaway",
|
||||
"description": "收货单直入库存(一步完成):将已审核的收货单(completed + is_review=true)未入库明细直接写入 wms_storage 库位库存(按收货明细的库位 space_id 入库)并生成入库单(SCRK,一步置为 COMPLETED),入库单明细与收货明细一一对应(不合并,逐行保留库位/批次/效期),同时更新 wms_stock_warehouse 总库存并写入出入库流水(wms_stock_transaction / wms_storage_transaction),并写入 wms_inbound_direct_log(status=committed,与 storage.entry.commit 链路口径一致),回填收货单及其明细的入库状态。要求货主仓库签约配置 policy_config.allow_receiving_direct_putaway=1 开启直入库存功能。",
|
||||
"isNeedAuth": true,
|
||||
"isNeedLogin": true,
|
||||
"params": [
|
||||
{
|
||||
"name": "receiving_id",
|
||||
"type": "number",
|
||||
"required": true,
|
||||
"source": "body",
|
||||
"description": "收货单ID(必填)"
|
||||
}
|
||||
],
|
||||
"response": [
|
||||
{
|
||||
"name": "entry_order_id",
|
||||
"type": "number",
|
||||
"description": "生成的入库单ID"
|
||||
},
|
||||
{
|
||||
"name": "entry_order_code",
|
||||
"type": "string",
|
||||
"description": "入库单号(SCRK + 年月日 + 4位流水号)"
|
||||
},
|
||||
{
|
||||
"name": "order_status",
|
||||
"type": "string",
|
||||
"description": "入库单状态(COMPLETED,一步完成)"
|
||||
},
|
||||
{
|
||||
"name": "receiving_id",
|
||||
"type": "number",
|
||||
"description": "收货单ID"
|
||||
},
|
||||
{
|
||||
"name": "total_lines",
|
||||
"type": "number",
|
||||
"description": "入库单明细行数(与待入库收货明细一一对应)"
|
||||
},
|
||||
{
|
||||
"name": "total_quantity",
|
||||
"type": "number",
|
||||
"description": "入库总数量"
|
||||
}
|
||||
],
|
||||
"errors": [
|
||||
{
|
||||
"code": 400,
|
||||
"message": "receiving_id 必填"
|
||||
},
|
||||
{
|
||||
"code": 400,
|
||||
"message": "无法获取当前仓库信息"
|
||||
},
|
||||
{
|
||||
"code": 404,
|
||||
"message": "收货单不存在或无权访问"
|
||||
},
|
||||
{
|
||||
"code": 400,
|
||||
"message": "收货单已作废,不允许直入库存"
|
||||
},
|
||||
{
|
||||
"code": 400,
|
||||
"message": "收货单状态不允许直入库存,仅 completed 状态可操作"
|
||||
},
|
||||
{
|
||||
"code": 400,
|
||||
"message": "收货单未审核,不允许直入库存"
|
||||
},
|
||||
{
|
||||
"code": 400,
|
||||
"message": "货主仓库签约关系不存在"
|
||||
},
|
||||
{
|
||||
"code": 400,
|
||||
"message": "货主未开启收货单直入库存功能"
|
||||
},
|
||||
{
|
||||
"code": 400,
|
||||
"message": "收货单无明细,无法直入库存"
|
||||
},
|
||||
{
|
||||
"code": 400,
|
||||
"message": "收货单所有明细均已入库,禁止重复直入库存"
|
||||
},
|
||||
{
|
||||
"code": 400,
|
||||
"message": "以下商品未匹配系统商品,请先绑定"
|
||||
},
|
||||
{
|
||||
"code": 400,
|
||||
"message": "以下商品在当前仓库中不存在或已失效"
|
||||
},
|
||||
{
|
||||
"code": 500,
|
||||
"message": "系统异常"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"method": "storage.inventory.transferLocation",
|
||||
"description": "单商品单库位移库,将库存从一个库位移动到另一个库位,支持部分数量移库和全量移库",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user