oms/backend/app/lib/orderDeliveryItems.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

97 lines
4.5 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';
/**
* 发货单明细(order_delivery_items)公共字段口径与校验
*
* 对应奇门 `deliveryorder.create` 的 `request.orderLines.orderLine`,
* 字段口径对齐 WMS 明细落库结构(D:\wms\app\service\systems\delivery_order.js#addOrderLines):
* - plan_qty 必须 > 0(WMS:`数量必须大于0`)
* - actual_price 必填且不小于 0(手工创建发货单时单价由人工填写)
* - item_id / item_code 至少一个(WMS:itemId 主键查不到时降级按 itemCode)
* - order_line_no 缺省按行序号(WMS:`${lineIndex + 1}`)
* - inventory_type:OMS 落 ZP 正品 / CC 残次,推送 WMS 时映射 ZC / CC
*/
/** 库存类型:OMS 落 ZP 正品 / CC 残次(推送 WMS 时映射 ZC/CC) */
const INVENTORY_TYPES = [ 'ZP', 'CC' ];
/**
* 校验明细数组:plan_qty 必须 > 0、actual_price 必填、item_id/item_code 至少一个、行号不重复
* @param {Array} items 明细数组
* @return {{ok: boolean, message?: string}} 校验结果
*/
function validateItems(items) {
const seenLineNos = new Set();
const list = Array.isArray(items) ? items : [];
for (let i = 0; i < list.length; i++) {
const item = list[i] || {};
const planQty = Number(item.plan_qty == null ? 0 : item.plan_qty);
if (!(planQty > 0)) {
return { ok: false, message: `第${i + 1}行:plan_qty(计划数量)必须大于 0` };
}
const hasItemId = item.item_id != null && item.item_id !== '';
const hasItemCode = item.item_code != null && String(item.item_code).trim() !== '';
if (!hasItemId && !hasItemCode) {
return { ok: false, message: `第${i + 1}行:item_id 与 item_code 至少填写一个` };
}
const price = item.actual_price;
if (price == null || String(price).trim() === '') {
return { ok: false, message: `第${i + 1}行:actual_price(单价)为必填项` };
}
if (!(Number(price) >= 0)) {
return { ok: false, message: `第${i + 1}行:actual_price(单价)必须为不小于 0 的数字` };
}
const inventoryType = item.inventory_type == null ? '' : String(item.inventory_type).trim().toUpperCase();
if (inventoryType && !INVENTORY_TYPES.includes(inventoryType)) {
return { ok: false, message: `第${i + 1}行:inventory_type 仅支持 ${INVENTORY_TYPES.join(' / ')}` };
}
const lineNo = String(item.order_line_no == null || item.order_line_no === '' ? i + 1 : item.order_line_no);
if (seenLineNos.has(lineNo)) {
return { ok: false, message: `第${i + 1}行:order_line_no(${lineNo})重复` };
}
seenLineNos.add(lineNo);
}
return { ok: true };
}
/**
* 规范化单条明细为可直接入库的行数据
* @param {Object} raw 明细原始数据(请求体或明细实例)
* @param {Object} opts { principalId, orderDeliveryId, lineIndex }
* @return {Object} 行数据
*/
function buildItemRow(raw, opts = {}) {
const body = raw || {};
const inventoryType = String(body.inventory_type || '').trim().toUpperCase();
const lineIndex = Number(opts.lineIndex) || 0;
return {
principal_id: opts.principalId,
order_delivery_id: opts.orderDeliveryId,
order_line_no: String(body.order_line_no == null || body.order_line_no === '' ? lineIndex + 1 : body.order_line_no),
item_id: body.item_id == null || body.item_id === '' ? null : Number(body.item_id),
item_code: body.item_code == null ? null : String(body.item_code).trim(),
item_name: body.item_name == null ? null : String(body.item_name).trim(),
plan_qty: Number(body.plan_qty == null ? 0 : body.plan_qty),
actual_price: body.actual_price == null || body.actual_price === '' ? null : Number(body.actual_price),
retail_price: body.retail_price == null || body.retail_price === '' ? null : Number(body.retail_price),
ext_code: body.ext_code == null ? null : String(body.ext_code).trim(),
// 非 CC 一律落 ZP(正品),与出入库明细 inventory_type 口径一致
inventory_type: inventoryType === 'CC' ? 'CC' : 'ZP',
owner_code: body.owner_code == null ? null : String(body.owner_code).trim(),
source_order_code: body.source_order_code == null ? null : String(body.source_order_code).trim(),
sub_source_order_code: body.sub_source_order_code == null ? null : String(body.sub_source_order_code).trim(),
stock_id: body.stock_id == null || body.stock_id === '' ? null : Number(body.stock_id),
extend_props: body.extend_props == null || body.extend_props === '' ? null : body.extend_props,
};
}
module.exports = { INVENTORY_TYPES, validateItems, buildItemRow };