oms/backend/app/utils/completeInbound.js
liufumingren ec81f8b3cd feat(backend): 入库单/出库单全流程与 WMS 对接
- 入库单、出库单控制器与模型(状态流转、审核、驳回换号、作废、删除约束)
- 明细共用 stock_items:应/已数量、行级库存类型 ZP/CC、明细锁定
- lib/wms:单据类型常量(orderTypes)、奇门/openapi 客户端(qimenClient)、签名(sign)
- 库存联动:utils/stock.js、utils/completeInbound.js(入库完成加库存,幂等)
- 驳回换号:utils/rejectBillNo.js;旧 orders 模块同步调整
- 相关迁移:入库单 WMS 字段/审核/推送结果、明细计划与实际数量、单据类型落 WMS 编码、出库库存扣减标记等
2026-09-13 02:23:44 +08:00

48 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const stock = require('./stock');
/**
* 完成入库:把「已推送(3)」的入库单置为「已完成(4)」,并按明细增加系统库存。
* - 幂等:已完成的单据直接返回成功(不重复加库存),供 WMS 回调重复推送时安全复用
* - 明细为空时拒绝,避免「空单据」走完流程却不产生任何库存
* - 行级库存类型:inventory_type=CC → product_stocks.item_type='CC',其余(ZP 等)归为正品 ZC
* 复用入口:① 控制器手工「完成」 ② WMS 回调 entryorder.confirm(外部回推驱动)
* @param {Egg.Context} ctx
* @param {Object} order 入库单 model 实例(需含 id / principal_id / status)
* @return {Promise<{ok: boolean, already?: boolean, code?: number, message?: string}>}
*/
module.exports = async function completeInbound(ctx, order) {
if (!order) return { ok: false, code: 404, message: '入库单不存在' };
// 幂等:已完成直接返回
if (order.status === 4) return { ok: true, already: true };
if (order.status !== 3) {
return { ok: false, code: 400, message: '只有「已推送」状态的单据可以完成入库' };
}
const items = await ctx.model.StockItem.findAll({
where: { bill_type: 1, bill_id: order.id, product_id: { [ctx.app.Sequelize.Op.ne]: null } },
attributes: [ 'product_id', 'quantity', 'inventory_type' ],
order: [[ 'product_id', 'ASC' ]],
});
if (!items.length) {
return { ok: false, code: 400, message: '单据无明细,无法完成入库' };
}
const transaction = await ctx.model.transaction();
try {
for (const it of items) {
const p = it.get ? it.get({ plain: true }) : it;
// 行级库存类型:CC=残次 -> product_stocks 的 item_type='CC',其余(ZP等)归为正品 ZC
const itemType = p.inventory_type === 'CC' ? 'CC' : 'ZC';
await stock.increase(ctx, order.principal_id, Number(p.product_id), Number(p.quantity) || 0, transaction, itemType);
}
await order.update({ status: 4 }, { transaction });
await transaction.commit();
return { ok: true };
} catch (err) {
await transaction.rollback();
throw err;
}
};