oms/backend/app/utils/stock.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

116 lines
4.2 KiB
JavaScript
Raw Permalink 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';
/**
* 商品系统库存(product_stocks)操作工具
* 库存按 item_type(存储类型,与 WMS itemType 对齐)分多行存储:
* ZC=正品 / CC=残次品,每行:可用 = 总数量 - 锁定数量
* - increase / decrease:增加/减少某类型总数量
* - lock / unlock / deduct:占用与扣减(默认 ZC 正品维度,残次品一般只增减小计不动锁定)
* 全部在事务内使用行锁(SELECT ... FOR UPDATE)保证并发安全
*/
async function getOrCreate(ctx, principalId, productId, itemType = 'ZC', transaction) {
let row = await ctx.model.ProductStock.findOne({
where: { principal_id: principalId, product_id: productId, item_type: itemType },
lock: true,
transaction,
});
if (!row) {
row = await ctx.model.ProductStock.create(
{
principal_id: principalId,
product_id: productId,
item_type: itemType,
quantity: 0,
lock_quantity: 0,
available: 0,
},
{ transaction },
);
}
return row;
}
function recompute(row) {
const qty = Number(row.quantity) || 0;
const lock = Number(row.lock_quantity) || 0;
row.available = qty - lock;
}
/** 增加某类型总数量 */
async function increase(ctx, principalId, productId, qty, transaction, itemType = 'ZC') {
const n = Number(qty) || 0;
if (n <= 0) return null;
const row = await getOrCreate(ctx, principalId, productId, itemType, transaction);
row.quantity = Number(row.quantity) + n;
recompute(row);
await row.save({ transaction });
return row;
}
/** 锁定某类型库存:可用不足时抛出「库存不足」(err.code = INSUFFICIENT_STOCK) */
async function lock(ctx, principalId, productId, qty, transaction, itemType = 'ZC') {
const n = Number(qty) || 0;
if (n <= 0) return null;
const row = await getOrCreate(ctx, principalId, productId, itemType, transaction);
const available = Number(row.quantity) - Number(row.lock_quantity);
if (available < n) {
const err = new Error('库存不足');
err.code = 'INSUFFICIENT_STOCK';
err.productId = productId;
err.available = available;
err.require = n;
throw err;
}
row.lock_quantity = Number(row.lock_quantity) + n;
recompute(row);
await row.save({ transaction });
return row;
}
/** 解锁某类型库存(出库单取消/恢复时回滚锁定) */
async function unlock(ctx, principalId, productId, qty, transaction, itemType = 'ZC') {
const n = Number(qty) || 0;
if (n <= 0) return null;
const row = await getOrCreate(ctx, principalId, productId, itemType, transaction);
row.lock_quantity = Math.max(0, Number(row.lock_quantity) - n);
recompute(row);
await row.save({ transaction });
return row;
}
/** 发货扣减:总数量与锁定各减 qty */
async function deduct(ctx, principalId, productId, qty, transaction, itemType = 'ZC') {
const n = Number(qty) || 0;
if (n <= 0) return null;
const row = await getOrCreate(ctx, principalId, productId, itemType, transaction);
row.quantity = Math.max(0, Number(row.quantity) - n);
row.lock_quantity = Math.max(0, Number(row.lock_quantity) - n);
recompute(row);
await row.save({ transaction });
return row;
}
/** 仅减少某类型总数量(库存同步覆盖用:系统多于外部,已直接完成的出库单,不动锁定) */
async function decrease(ctx, principalId, productId, qty, transaction, itemType = 'ZC') {
const n = Number(qty) || 0;
if (n <= 0) return null;
const row = await getOrCreate(ctx, principalId, productId, itemType, transaction);
row.quantity = Math.max(0, Number(row.quantity) - n);
recompute(row);
await row.save({ transaction });
return row;
}
/** 残次品:入库增加总数量(等价于 increase(..., 'CC')) */
async function increaseDefect(ctx, principalId, productId, qty, transaction) {
return increase(ctx, principalId, productId, qty, transaction, 'CC');
}
/** 残次品:仅减少总数量(等价于 decrease(..., 'CC')) */
async function decreaseDefect(ctx, principalId, productId, qty, transaction) {
return decrease(ctx, principalId, productId, qty, transaction, 'CC');
}
module.exports = { getOrCreate, increase, lock, unlock, deduct, decrease, increaseDefect, decreaseDefect };