oms/backend/app/utils/recalcOrderDelivery.js
liufumingren 11e7b26ced feat(backend): 发货单模块与 WMS 定时推送队列
- 发货单主表/明细表(order_deliveries / order_delivery_items)控制器与模型
- 表头金额与明细派生字段重算(utils/recalcOrderDelivery.js)
- 状态流转:审核/驳回/取消/恢复/作废、批量审核、批量切换仓库
- 审核入队 + 定时推送:service/orderDeliveryPush.js(乐观锁 CAS 防重、失败重试上限 3 次、业务失败退回待审核)+ schedule/orderDeliveryPush.js
- 收件人 AI 智能识别(service/ai.js,DeepSeek)
- 相关迁移:建表、站内字段、明细表、订单类型默认值、快递字典、菜单种子、历史数据搬迁
2026-09-13 02:23:47 +08:00

38 lines
1.3 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';
/**
* 重算发货单头的商品总数、商品种类数与商品总金额(明细增删改后回写)
* 商品总数 = 所有明细 plan_qty 之和;商品种类数 = 不重复商品数;
* 商品总金额 = Σ(plan_qty × actual_price)(实际单价,即明细成交金额合计)
* 种类判定优先级:item_id(WMS 内部商品ID)> item_code > 明细主键(无法识别商品时按行计)
* @param {Egg.Context} ctx egg context(含 model)
* @param {number} orderDeliveryId 发货单头ID
*/
module.exports = async function recalcOrderDelivery(ctx, orderDeliveryId) {
const items = await ctx.model.OrderDeliveryItem.findAll({
where: { order_delivery_id: orderDeliveryId },
attributes: [ 'id', 'item_id', 'item_code', 'plan_qty', 'actual_price' ],
});
let itemTotal = 0;
let itemAmount = 0;
const kinds = new Set();
for (const it of items) {
const qty = Number(it.plan_qty || 0);
itemTotal += qty;
itemAmount += qty * Number(it.actual_price || 0);
if (it.item_id) kinds.add(`w${it.item_id}`);
else if (it.item_code) kinds.add(`c${it.item_code}`);
else kinds.add(`i${it.id}`);
}
await ctx.model.OrderDelivery.update(
{
item_total: itemTotal,
item_kind: kinds.size,
item_amount: Number(itemAmount.toFixed(2)),
},
{ where: { id: orderDeliveryId } }
);
};