oms/backend/app/lib/orderPushItems.js
liufumingren d4128c4eee fix(order): 修正手工单推送仓库的「未绑定系统商品」误判
- 推送前校验改为「能否解析到系统商品」:product_id / combo_product_id / 历史手工单 item_id(与商品编码一致)/ 无平台溯源时编码命中商品库
- 平台未绑定明细(带店铺商品 / 平台快照 / 平台商品ID)仍然拦截并提示先在「店铺商品」绑定
- 建单 / 改单写库时把历史手工单误存在 item_id 里的系统商品ID 迁到 product_id,并补 item_type / parse_status
- 推送报文 itemId 优先用仓库映射(warehouse_products.wms_item_id),避免把系统商品ID 当 WMS 商品ID 下发
- 手工单表单改用 product_id 记录所选系统商品(item_id 仅沿用历史数据)
- 新增 orderPushItems 与推送报文 itemId 单测

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-13 23:17:21 +08:00

83 lines
3.7 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';
/**
* 订单推送前的明细校验:明细能否落到「系统商品」。
*
* 背景:平台拉单的明细在绑定店铺商品前保留的是平台原样数据(平台商品ID / 平台编码 / 快照),
* 这类明细直接推给仓库会建出脏商品或报错,必须先在「店铺商品」绑定系统商品/组合商品;
* 手工建单 / 接口建单的明细则直接来自系统商品,没有店铺商品绑定,不能按绑定状态拦。
*
* 因此判断口径是「能否解析到系统商品」而不是「有没有 product_id」:
* ① 有 combo_product_id(组合拆分子行)→ 可推送
* ② product_id 命中系统商品 → 可推送
* ③ 历史手工单口径:item_id 存的是系统商品ID,且与行上的 item_code 一致 → 可推送
* ④ 没有平台溯源信息(shop_product_id / platform_snapshot / outer_item_id 都为空)时,
* item_code 命中系统商品编码 → 可推送
* 其余一律视为未转换(平台编码不能下发仓库)。
*/
/**
* 明细是否带平台溯源信息(平台拉单行的标记)
* @param {Object} item 明细行
* @return {Boolean} 是否来自平台
*/
function hasPlatformTrace(item) {
if (!item || typeof item !== 'object') return false;
return Boolean(item.shop_product_id || item.platform_snapshot || item.outer_item_id);
}
/**
* 找出无法落到系统商品的明细
* @param {Array<Object>} items 订单明细(order_status_items 行)
* @param {Array<Object>} products 本主体下的系统商品(按 id / item_code 命中即可,{ id, item_code })
* @return {Array<Object>} 无法解析的明细行(全部可推送时返回空数组)
*/
function findUnconvertibleItems(items, products) {
const productCodeById = new Map();
const productCodes = new Set();
for (const row of products || []) {
const id = Number(row && row.id);
const code = String(row && row.item_code == null ? '' : row.item_code).trim();
if (Number.isInteger(id) && id > 0) productCodeById.set(id, code);
if (code) productCodes.add(code);
}
return (Array.isArray(items) ? items : []).filter(item => {
if (!item || typeof item !== 'object') return false;
if (Number(item.combo_product_id) > 0) return false;
const productId = Number(item.product_id);
if (Number.isInteger(productId) && productId > 0 && productCodeById.has(productId)) return false;
const code = String(item.item_code == null ? '' : item.item_code).trim();
// 历史手工单:item_id 误存了系统商品ID(编码一致才认,避免把真正的 WMS 商品ID误判)
const legacyId = Number(item.item_id);
if (Number.isInteger(legacyId) && legacyId > 0 && code && productCodeById.get(legacyId) === code) return false;
if (code && productCodes.has(code) && !hasPlatformTrace(item)) return false;
return true;
});
}
/**
* 生成「未转换明细」的推送中止提示(最多列出 5 行)
* @param {Array<Object>} items 无法解析的明细行
* @return {String} 提示文案
*/
function unconvertibleMessage(items) {
const list = Array.isArray(items) ? items : [];
const detail = list
.slice(0, 5)
.map(item => `第 ${(item && item.order_line_no) || '?'} 行 ${(item && item.item_code) || '(无编码)'}`)
.join('、');
return `明细商品未绑定系统商品(${detail}${list.length > 5 ? ` 等 ${list.length} 行` : ''}),无法推送:` +
'平台订单请先在「店铺商品」把对应商品绑定到系统商品/组合商品(重新绑定会自动重建订单明细);' +
'手工单请选择系统商品(明细商品编码需与系统商品编码一致)';
}
module.exports = {
hasPlatformTrace,
findUnconvertibleItems,
unconvertibleMessage,
};