oms/backend/app/utils/recalcStock.js
liufumingren cbd16752a5 feat(backend): 商品、店铺、组合商品、仓库与库存模块
- 商品/商品图片、店铺、店铺商品、组合商品及明细
- 仓库、仓库商品(WMS itemId 映射)、仓库接口配置与字段定义(warehouseApi/warehouseApiField)
- 库存:stock_items、product_stocks(ZC/CC 双维度)与库存重算工具(utils/recalcStock.js)
- 库存单据与对接日志:stock orders、api_outbound_logs
- 相关迁移:shops/shop_products/stock_orders/warehouses/warehouse_apis/warehouse_products/product_stocks/files-images 等表与字段
2026-09-13 02:23:39 +08:00

28 lines
1003 B
JavaScript

'use strict';
/**
* 重算出入库单据头的商品总数与商品种类数,并在明细增删改后回写
* 商品总数 = 所有明细 quantity 之和;商品种类数 = 不重复商品数(同一商品计为一种)
* @param {Egg.Context} ctx
* @param {number} billType 1入库 2出库
* @param {number} billId 单据头ID
*/
module.exports = async function recalcStock(ctx, billType, billId) {
const items = await ctx.model.StockItem.findAll({
where: { bill_type: billType, bill_id: billId },
attributes: [ 'id', 'product_id', 'quantity' ],
});
let itemTotal = 0;
const kinds = new Set();
for (const it of items) {
itemTotal = Number(itemTotal) + Number(it.quantity || 0);
if (it.product_id) kinds.add(`p${it.product_id}`);
else kinds.add(`i${it.id}`);
}
const model = billType === 1 ? ctx.model.InboundOrder : ctx.model.OutboundOrder;
await model.update(
{ item_total: itemTotal, item_kind: kinds.size },
{ where: { id: billId } },
);
};