From d4128c4eeef93896b970690f0cd1dea513748d57 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Fri, 13 Nov 2026 23:17:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(order):=20=E4=BF=AE=E6=AD=A3=E6=89=8B?= =?UTF-8?q?=E5=B7=A5=E5=8D=95=E6=8E=A8=E9=80=81=E4=BB=93=E5=BA=93=E7=9A=84?= =?UTF-8?q?=E3=80=8C=E6=9C=AA=E7=BB=91=E5=AE=9A=E7=B3=BB=E7=BB=9F=E5=95=86?= =?UTF-8?q?=E5=93=81=E3=80=8D=E8=AF=AF=E5=88=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 推送前校验改为「能否解析到系统商品」: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> --- backend/app/contract/index.js | 4 +- backend/app/lib/orderPushItems.js | 82 +++++++++++++++++++ backend/app/lib/wms/qimenClient.js | 7 +- backend/app/service/orderStatus.js | 82 ++++++++++++++++++- backend/app/service/orderWms.js | 57 ++++++++----- backend/test/app/lib/orderPushItems.test.js | 73 +++++++++++++++++ .../test/app/lib/qimenDeliveryItemId.test.js | 53 ++++++++++++ frontend/src/views/Order/OrderFormDialog.vue | 23 ++++-- 8 files changed, 347 insertions(+), 34 deletions(-) create mode 100644 backend/app/lib/orderPushItems.js create mode 100644 backend/test/app/lib/orderPushItems.test.js create mode 100644 backend/test/app/lib/qimenDeliveryItemId.test.js diff --git a/backend/app/contract/index.js b/backend/app/contract/index.js index b1dcb68..bf1a4ff 100644 --- a/backend/app/contract/index.js +++ b/backend/app/contract/index.js @@ -2639,7 +2639,7 @@ module.exports = { id: { type: 'integer', description: '明细ID' }, order_id: { type: 'integer', description: '订单ID(= order_index.id)' }, order_line_no: { type: 'string', description: '行号(缺省按行序号)' }, - item_id: { type: 'integer', description: 'WMS 内部商品ID' }, + item_id: { type: 'integer', description: 'WMS 内部商品ID(历史手工单曾存系统商品ID,写库时会按商品编码核对后迁到 product_id)' }, item_code: { type: 'string', description: '商品编码' }, item_name: { type: 'string', description: '商品名称' }, plan_qty: { type: 'number', description: '计划数量(必须大于 0)' }, @@ -2651,7 +2651,7 @@ module.exports = { sub_source_order_code: { type: 'string', description: '子来源订单编码' }, stock_id: { type: 'integer', description: 'WMS 仓库库存ID' }, extend_props: { type: 'object', description: '扩展属性' }, - product_id: { type: 'integer', description: '系统商品ID' }, + product_id: { type: 'integer', description: '系统商品ID(手工单选中的系统商品;平台订单为绑定店铺商品后的系统商品ID)' }, sku_code: { type: 'string', description: '商家编码' }, spec: { type: 'string', description: '规格' }, image: { type: 'string', description: '图片' }, diff --git a/backend/app/lib/orderPushItems.js b/backend/app/lib/orderPushItems.js new file mode 100644 index 0000000..6923f5a --- /dev/null +++ b/backend/app/lib/orderPushItems.js @@ -0,0 +1,82 @@ +'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} items 订单明细(order_status_items 行) + * @param {Array} products 本主体下的系统商品(按 id / item_code 命中即可,{ id, item_code }) + * @return {Array} 无法解析的明细行(全部可推送时返回空数组) + */ +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} 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, +}; diff --git a/backend/app/lib/wms/qimenClient.js b/backend/app/lib/wms/qimenClient.js index 021fdf5..8a60115 100644 --- a/backend/app/lib/wms/qimenClient.js +++ b/backend/app/lib/wms/qimenClient.js @@ -647,7 +647,12 @@ class QimenWmsClient { // itemId 优先取明细自带的 WMS 商品ID,其次按商品编码/OMS 商品ID 回查 warehouse_products.wms_item_id const mappedItemId = (i.item_code && wmsItemIdByItemCode[i.item_code]) || (productId != null ? wmsItemIdByProduct[productId] : undefined); - const itemId = i.item_id != null && i.item_id !== '' ? Number(i.item_id) : mappedItemId; + // itemId 优先用仓库映射(warehouse_products.wms_item_id),再看明细自带值; + // 历史手工单曾把「系统商品ID」误存进 item_id,若该 ID 能在系统商品表里查到(productById), + // 就不能当 WMS 商品ID 下发,否则 WMS 可能按 itemId 命中其它商品(仍会下发 itemCode 供降级匹配) + const storedItemId = i.item_id != null && i.item_id !== '' ? Number(i.item_id) : null; + const storedIsProductId = storedItemId != null && productById[storedItemId] != null; + const itemId = mappedItemId != null ? Number(mappedItemId) : (storedIsProductId ? null : storedItemId); const line = { orderLineNo: String(i.order_line_no == null || i.order_line_no === '' ? idx + 1 : i.order_line_no), diff --git a/backend/app/service/orderStatus.js b/backend/app/service/orderStatus.js index b83d3aa..5b66c8c 100644 --- a/backend/app/service/orderStatus.js +++ b/backend/app/service/orderStatus.js @@ -21,6 +21,7 @@ const { PLATFORM_PRESETS, platformNameOf, resolvePlatform } = require('../lib/pl const { validateReceiver } = require('../lib/orderReceiver'); const { validateItems } = require('../lib/orderDeliveryItems'); const { findOrderIdsBySnCodes, listOrderSnCodes } = require('../lib/orderSnCodes'); +const { hasPlatformTrace } = require('../lib/orderPushItems'); /** 外部发货同步状态文案(与 service/externalSendSync.js 的 SEND_STATUS 对齐) */ const EXTERNAL_SEND_STATUS_LABELS = { @@ -876,8 +877,8 @@ class OrderStatusService extends Service { async _assertItemsOwner(items, subsidiaryId, principalId) { const { ctx } = this; const Op = ctx.app.Sequelize.Op; - // 明细既有新建订单的 item_id,也有历史排单口径的 product_id,两者都按商品校验 - const pickProductId = it => Number(it && (it.item_id != null && it.item_id !== '' ? it.item_id : it.product_id)); + // 明细既有新建订单(product_id)的口径,也有历史排单口径;优先按系统商品ID 校验 + const pickProductId = it => Number(it && (it.product_id != null && it.product_id !== '' ? it.product_id : it.item_id)); const productIds = [ ...new Set(items.map(pickProductId).filter(id => Number.isInteger(id) && id > 0)) ]; const comboIds = [ ...new Set(items.map(it => Number(it && it.combo_product_id)).filter(id => Number.isInteger(id) && id > 0)) ]; if (!productIds.length && !comboIds.length) return; @@ -988,9 +989,86 @@ class OrderStatusService extends Service { if (!Number(row.parse_status)) row.parse_status = Number(source.parse_status) || 0; return row; }); + await this._normalizeItemProductLinks(rows, principalId, transaction); return ctx.model.OrderStatusItem.bulkCreate(rows, { transaction }); } + /** + * 写库前补齐明细的「系统商品」关联(手工单 / 接口建单口径) + * + * 历史手工建单把**系统商品ID**存进了 item_id(该列本义是 WMS 商品ID),导致推送仓前校验 + * 与报文构造都要额外兼容。这里在写库时统一纠正: + * - item_id 命中系统商品、且与行上的 item_code 一致 → 迁到 product_id,item_id 置空 + * - 无平台溯源信息(shop_product_id / platform_snapshot / outer_item_id 均为空)的明细, + * item_code 唯一命中系统商品 → 补 product_id + * - 补齐后补 item_type=1 / parse_status=1(视为已解析的系统商品) + * 平台明细(带平台溯源)不做按编码兜底,避免绕过「店铺商品绑定」。 + * @param {Array} rows 待写入的明细行(原地修改) + * @param {Number} principalId 主体ID + * @param {Object} [transaction] 事务 + * @return {Promise} 无 + */ + async _normalizeItemProductLinks(rows, principalId, transaction) { + const { ctx } = this; + const Op = ctx.app.Sequelize.Op; + const list = Array.isArray(rows) ? rows : []; + const pending = list.filter(row => !(Number(row.product_id) > 0) && !(Number(row.combo_product_id) > 0)); + const stray = list.filter(row => Number(row.item_id) > 0); + if (!pending.length && !stray.length) return; + + const ids = [ ...new Set(stray.map(row => Number(row.item_id)).filter(id => Number.isInteger(id) && id > 0)) ]; + const codes = [ ...new Set(list.map(row => String(row.item_code == null ? '' : row.item_code).trim()).filter(Boolean)) ]; + if (!ids.length && !codes.length) return; + + const or = []; + if (ids.length) or.push({ id: { [Op.in]: ids } }); + if (codes.length) or.push({ item_code: { [Op.in]: codes } }); + const products = await ctx.model.Product.findAll({ + where: { principal_id: principalId, [Op.or]: or }, + attributes: [ 'id', 'item_code' ], + raw: true, + transaction, + }); + if (!products.length) return; + + const codeById = new Map(); + const productsByCode = new Map(); + for (const product of products) { + const id = Number(product.id); + const code = String(product.item_code == null ? '' : product.item_code).trim(); + if (Number.isInteger(id) && id > 0) codeById.set(id, code); + if (!code) continue; + if (!productsByCode.has(code)) productsByCode.set(code, []); + productsByCode.get(code).push(id); + } + + const isLegacyProductId = row => { + const legacyId = Number(row.item_id); + const code = String(row.item_code == null ? '' : row.item_code).trim(); + return Number.isInteger(legacyId) && legacyId > 0 && code && codeById.get(legacyId) === code; + }; + + for (const row of pending) { + const code = String(row.item_code == null ? '' : row.item_code).trim(); + if (isLegacyProductId(row)) { + row.product_id = Number(row.item_id); + row.item_id = null; + } else if (code && !hasPlatformTrace(row)) { + const matched = productsByCode.get(code) || []; + if (matched.length === 1) row.product_id = matched[0]; + } + if (Number(row.product_id) > 0) { + if (!Number(row.item_type)) row.item_type = 1; + if (!Number(row.parse_status)) row.parse_status = 1; + } + } + + // 已绑定系统商品的明细:item_id 若是历史误存的系统商品ID(与商品编码一致),置空避免当 WMS 商品ID 下发 + for (const row of list) { + if (Number(row.product_id) > 0 && isLegacyProductId(row)) row.item_id = null; + } + } + /** * 组装明细行(字段口径对齐 order_status_items:发货单明细 + 平台明细的超集) * @param {Object} raw 明细原始数据 diff --git a/backend/app/service/orderWms.js b/backend/app/service/orderWms.js index 41584c2..d6e2c9b 100644 --- a/backend/app/service/orderWms.js +++ b/backend/app/service/orderWms.js @@ -5,6 +5,7 @@ const QimenWmsClient = require('../lib/wms/qimenClient'); const { ORDER_STATUS } = require('../lib/orderStatus'); const { releaseOrderStock } = require('../utils/orderStock'); const { getShopBasic, getAccountWithPlatform } = require('../lib/externalPrintConfig'); +const { findUnconvertibleItems, unconvertibleMessage } = require('../lib/orderPushItems'); /** 单条单据最多推送尝试次数(无响应/超时才算一次可重试的失败;对方明确响应失败不重试) */ const PUSH_MAX_ATTEMPTS = 3; @@ -98,24 +99,40 @@ class OrderWmsService extends Service { } /** - * 推送前校验明细是否已完成「店铺商品 → 系统商品」转换 + * 推送前校验明细是否已落到「系统商品」 * - * 外部打单订单的明细在拉单时可能是未绑定状态(保留了平台商品编码), - * 这类明细不能推给仓库:平台编码在 WMS 侧不存在,会建出脏商品或报错。 + * 判据不是「有没有 product_id」而是「能否解析到系统商品」(见 app/lib/orderPushItems.js): + * - 平台拉单的明细必须完成「店铺商品 → 系统商品」绑定,否则推给仓库的会是平台编码; + * - 手工单 / 接口建单的明细则直接选系统商品(历史数据把系统商品ID 存在 item_id), + * 按主体商品库核对 ID / 编码即可,不能因为缺少店铺商品绑定而拦下。 + * @param {Object} order 订单(需含 principal_id) * @param {Array} items 明细(order_status_items,已展开为最小单位) - * @return {string} 不通过原因,全部通过返回空串 + * @return {Promise<{ok: Boolean, message: String}>} 校验结果(不通过时 message 为原因) */ - validateConvertedItems(items) { + async validateConvertedItems(order, items) { const list = Array.isArray(items) ? items : []; - if (!list.length) return ''; - const invalid = list.filter(item => !(Number(item.product_id) > 0) && !(Number(item.combo_product_id) > 0)); - if (!invalid.length) return ''; - const detail = invalid - .slice(0, 5) - .map(item => `第 ${item.order_line_no || '?'} 行 ${item.item_code || '(无编码)'}`) - .join('、'); - return `明细商品未绑定系统商品(${detail}${invalid.length > 5 ? ` 等 ${invalid.length} 行` : ''}),无法推送:` + - '请先在「店铺商品」把对应商品绑定到系统商品/组合商品(重新绑定会自动重建订单明细)'; + if (!list.length) return { ok: true, message: '' }; + + const Op = this.app.Sequelize.Op; + const ids = [ ...new Set( + [ ...list.map(item => Number(item.product_id)), ...list.map(item => Number(item.item_id)) ] + .filter(id => Number.isInteger(id) && id > 0) + ) ]; + const codes = [ ...new Set(list.map(item => String(item.item_code == null ? '' : item.item_code).trim()).filter(Boolean)) ]; + const or = []; + if (ids.length) or.push({ id: { [Op.in]: ids } }); + if (codes.length) or.push({ item_code: { [Op.in]: codes } }); + + const products = or.length + ? await this.ctx.model.Product.findAll({ + where: { principal_id: order.principal_id, [Op.or]: or }, + attributes: [ 'id', 'item_code' ], + raw: true, + }) + : []; + + const invalid = findUnconvertibleItems(list, products); + return invalid.length ? { ok: false, message: unconvertibleMessage(invalid) } : { ok: true, message: '' }; } /** @@ -310,14 +327,14 @@ class OrderWmsService extends Service { const client = new QimenWmsClient(api, this.app); const items = itemRows.map(item => item.get({ plain: true })); - // 明细必须已完成「店铺商品 → 系统商品」转换,否则推给仓库的会是平台商品编码 - const unconverted = this.validateConvertedItems(items); - if (unconverted) { + // 明细必须能落到系统商品(平台订单需先绑定店铺商品;手工单按商品库核对),否则推给仓库的会是平台编码 + const itemCheck = await this.validateConvertedItems(order, items); + if (!itemCheck.ok) { await order.update({ - wms_push_result: { ...base, success: false, failureKind: 'unbound_items', message: unconverted }, + wms_push_result: { ...base, success: false, failureKind: 'unbound_items', message: itemCheck.message }, }); - ctx.logger.warn(`[order-wms] ${order.order_no} 推送中止:${unconverted}`); - return { ok: false, retry: false, fatal: true, message: unconverted, attempts }; + ctx.logger.warn(`[order-wms] ${order.order_no} 推送中止:${itemCheck.message}`); + return { ok: false, retry: false, fatal: true, message: itemCheck.message, attempts }; } const wmsItemIds = await this.resolveWmsItemIds(order, items); diff --git a/backend/test/app/lib/orderPushItems.test.js b/backend/test/app/lib/orderPushItems.test.js new file mode 100644 index 0000000..bafebdf --- /dev/null +++ b/backend/test/app/lib/orderPushItems.test.js @@ -0,0 +1,73 @@ +'use strict'; + +const { assert } = require('egg-mock/bootstrap'); +const { findUnconvertibleItems, unconvertibleMessage, hasPlatformTrace } = require('../../../app/lib/orderPushItems'); + +const PRODUCTS = [ + { id: 4, item_code: 'P260911231515227608' }, + { id: 24, item_code: 'P261105011902808526' }, + { id: 30, item_code: 'tbcs111' }, +]; + +describe('test/app/lib/orderPushItems.test.js', () => { + it('已绑定系统商品 / 组合拆分子行可推送', () => { + const items = [ + { order_line_no: '1', product_id: 4, item_code: 'P260911231515227608' }, + { order_line_no: '2', product_id: 24, combo_product_id: 9, item_code: 'P261105011902808526' }, + ]; + assert(findUnconvertibleItems(items, PRODUCTS).length === 0); + }); + + it('平台未绑定明细(带平台溯源、平台编码)不可推送', () => { + const items = [ + { order_line_no: '1', shop_product_id: 1466, item_code: 'wf-C05速干定型喷雾420*3+排骨梳*1', parse_status: 2 }, + ]; + const invalid = findUnconvertibleItems(items, PRODUCTS); + assert(invalid.length === 1); + const message = unconvertibleMessage(invalid); + assert(message.includes('第 1 行')); + assert(message.includes('店铺商品')); + }); + + it('历史手工单:item_id 存系统商品ID 且编码一致 → 可推送', () => { + const items = [{ order_line_no: '1', item_id: 24, item_code: 'P261105011902808526' }]; + assert(findUnconvertibleItems(items, PRODUCTS).length === 0); + }); + + it('item_id 不能凭商品ID 放行,编码必须命中系统商品', () => { + const wrong = [{ order_line_no: '1', item_id: 4, item_code: 'NOT-EXIST' }]; + assert(findUnconvertibleItems(wrong, PRODUCTS).length === 1); + const byCode = [{ order_line_no: '1', item_id: 4, item_code: 'P261105011902808526' }]; + assert(findUnconvertibleItems(byCode, PRODUCTS).length === 0); + }); + + it('手工单只有商品编码且命中系统商品 → 可推送', () => { + const items = [{ order_line_no: '1', item_code: 'tbcs111' }]; + assert(findUnconvertibleItems(items, PRODUCTS).length === 0); + }); + + it('带平台溯源的明细即使编码命中系统商品也不放行(必须先绑定店铺商品)', () => { + const items = [ + { order_line_no: '1', item_code: 'tbcs111', shop_product_id: 1382 }, + { order_line_no: '2', item_code: 'tbcs111', platform_snapshot: { item_code: 'tbcs111' } }, + { order_line_no: '3', item_code: 'tbcs111', outer_item_id: '10000202451115' }, + ]; + const invalid = findUnconvertibleItems(items, PRODUCTS); + assert(invalid.length === 3); + }); + + it('product_id 不属于本主体商品库时按编码兜底(带平台溯源的仍拦下)', () => { + const items = [{ order_line_no: '1', product_id: 999, item_code: 'P260911231515227608' }]; + assert(findUnconvertibleItems(items, PRODUCTS).length === 0); + const platform = [{ order_line_no: '1', product_id: 999, item_code: 'P260911231515227608', shop_product_id: 1 }]; + assert(findUnconvertibleItems(platform, PRODUCTS).length === 1); + }); + + it('hasPlatformTrace 只看平台溯源字段', () => { + assert(hasPlatformTrace({ shop_product_id: 1 })); + assert(hasPlatformTrace({ platform_snapshot: {} })); + assert(hasPlatformTrace({ outer_item_id: 'x' })); + assert(!hasPlatformTrace({ item_code: 'x' })); + assert(!hasPlatformTrace(null)); + }); +}); diff --git a/backend/test/app/lib/qimenDeliveryItemId.test.js b/backend/test/app/lib/qimenDeliveryItemId.test.js new file mode 100644 index 0000000..05af34e --- /dev/null +++ b/backend/test/app/lib/qimenDeliveryItemId.test.js @@ -0,0 +1,53 @@ +'use strict'; + +const { assert } = require('egg-mock/bootstrap'); +const QimenWmsClient = require('../../../app/lib/wms/qimenClient'); + +/** 构造一个不触网的客户端(只测报文构造) */ +function buildClient() { + return new QimenWmsClient({ base_url: 'http://127.0.0.1:1', app_key: 'k', app_secret: 's' }); +} + +describe('test/app/lib/qimenDeliveryItemId.test.js', () => { + it('历史手工单的 item_id(系统商品ID)不会作为 WMS itemId 下发', () => { + const client = buildClient(); + const payload = client._buildDeliveryOrder( + { delivery_order_code: 'FH_473', order_type: 'JYCK' }, + [{ order_line_no: '1', item_id: 24, item_code: 'P261105011902808526', plan_qty: 1, actual_price: 1 }], + { + productById: { 24: { item_code: 'P261105011902808526', item_name: '1' } }, + wmsItemIdByItemCode: { P261105011902808526: 1001163 }, + wmsItemIdByProduct: {}, + } + ); + const line = payload.orderLines.orderLine[0]; + assert(line.itemCode === 'P261105011902808526'); + assert(line.itemId === 1001163); + }); + + it('item_id 是真正的 WMS 商品ID(商品库无此ID)时仍按明细下发', () => { + const client = buildClient(); + const payload = client._buildDeliveryOrder( + { delivery_order_code: 'FH_1', order_type: 'JYCK' }, + [{ order_line_no: '1', item_id: 777001, item_code: 'P-NO-LINK', plan_qty: 1, actual_price: 1 }], + { productById: {}, wmsItemIdByItemCode: {}, wmsItemIdByProduct: {} } + ); + const line = payload.orderLines.orderLine[0]; + assert(line.itemId === 777001); + }); + + it('已绑定系统商品时优先用仓库映射的 WMS 商品ID', () => { + const client = buildClient(); + const payload = client._buildDeliveryOrder( + { delivery_order_code: 'FH_1', order_type: 'JYCK' }, + [{ order_line_no: '1', product_id: 24, item_id: 999, item_code: 'P261105011902808526', plan_qty: 1, actual_price: 1 }], + { + productById: { 24: { item_code: 'P261105011902808526', item_name: '1' } }, + wmsItemIdByItemCode: {}, + wmsItemIdByProduct: { 24: 1001163 }, + } + ); + const line = payload.orderLines.orderLine[0]; + assert(line.itemId === 1001163); + }); +}); diff --git a/frontend/src/views/Order/OrderFormDialog.vue b/frontend/src/views/Order/OrderFormDialog.vue index 4659009..75a97ec 100644 --- a/frontend/src/views/Order/OrderFormDialog.vue +++ b/frontend/src/views/Order/OrderFormDialog.vue @@ -100,10 +100,11 @@ const rules: FormRules = { logistics_code: [ { required: true, message: '请选择物流公司(推送仓库时随单下发 logisticsCode)', trigger: 'change' } ], } -/** 明细行默认值 */ +/** 明细行默认值(product_id 是选中的系统商品;item_id 仅历史手工单数据沿用) */ function emptyLine(): OrderItemLine { return { order_line_no: '', + product_id: null, item_id: null, item_code: '', item_name: '', @@ -127,7 +128,7 @@ function fillItems(items: OrderItemLine[] | undefined) { /** 手工建单可选店铺:排除外部打单平台店铺(type=2,其订单只能由平台拉取) */ const selectableShops = computed(() => (props.filters.shops || []).filter(shop => Number(shop.type ?? 1) !== 2)) -/** item_id → 商品所属货主;用于切换店铺后校验已选明细是否同货主 */ +/** 选中的系统商品ID → 商品所属货主;用于切换店铺后校验已选明细是否同货主 */ const itemOwnerMap = ref>({}) /** 默认店铺:优先父级入口指定的店铺,否则取可选店铺列表第一个 */ @@ -413,10 +414,10 @@ watch(shopSubsidiaryId, sid => { // 已选商品若属于其它货主,直接清空(后端同样会拦截,避免提交后才报错) let cleared = 0 for (const line of form.items) { - if (!line.item_id) continue - const owner = itemOwnerMap.value[line.item_id] + if (!line.product_id) continue + const owner = itemOwnerMap.value[line.product_id] if (owner === undefined || owner === null || Number(owner) === sid) continue - line.item_id = null + line.product_id = null line.item_code = '' line.item_name = '' cleared += 1 @@ -438,7 +439,8 @@ function removeLine(index: number) { function onLineProductSelect(index: number, product: ProductSearchItem) { const line = form.items[index] if (!line) return - line.item_id = product.id + line.product_id = product.id + line.item_id = null line.item_code = product.item_code || '' line.item_name = product.item_name || '' // 记录商品所属货主,切换店铺时用来校验 / 清空不一致的明细 @@ -459,7 +461,7 @@ const itemsAmount = computed(() => /** 明细是否有实际内容(空行不计):有明细时商品总金额由明细派生,不允许手改 */ const hasRealItems = computed(() => - form.items.some(line => String(line.item_code || '').trim() || Number(line.item_id || 0) > 0), + form.items.some(line => String(line.item_code || '').trim() || Number(line.product_id || line.item_id || 0) > 0), ) /** 商品总金额(两位小数) */ @@ -476,9 +478,12 @@ watch( /** 提交前清理:空行剔除、行号补全、空串转 undefined */ function buildPayload(): OrderStatusPayload { const items = form.items - .filter(line => (line.item_code && String(line.item_code).trim()) || (line.item_id && line.item_id > 0)) + .filter(line => (line.item_code && String(line.item_code).trim()) || Number(line.product_id || line.item_id || 0) > 0) .map((line, index) => ({ order_line_no: String(line.order_line_no || index + 1), + // 选中的系统商品:product_id 是推送/取号/统计统一口径; + // item_id 仅历史手工单数据沿用(后端写库时会按商品编码核对后迁到 product_id) + product_id: line.product_id ?? null, item_id: line.item_id ?? null, item_code: line.item_code ? String(line.item_code).trim() : null, item_name: line.item_name ? String(line.item_name).trim() : null, @@ -868,7 +873,7 @@ async function submit() {