'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 } }, ); };