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