商品增加「不用发货」标记

- products 新增 no_need_ship(默认 0),商品编辑页可开关,列表展示「免发货」标签
- 命中该商品的订单明细:审核不锁库存、发货不扣库存、推送 WMS 时不进入报文
- 整单明细都是这类商品时:审核报错提示、推送按永久失败转异常
- 补单测(推送明细过滤)与迁移

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-24 01:07:24 +08:00
parent ef5fffe277
commit 2b2ea066ea
8 changed files with 195 additions and 9 deletions

View File

@ -496,6 +496,7 @@ module.exports = {
is_shelf_life_mgmt: { type: 'string', description: '是否保质期管理:Y/N' }, is_shelf_life_mgmt: { type: 'string', description: '是否保质期管理:Y/N' },
is_batch_mgmt: { type: 'string', description: '是否批次管理:Y/N' }, is_batch_mgmt: { type: 'string', description: '是否批次管理:Y/N' },
is_order_unique_gift: { type: 'integer', description: '是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否,默认 0' }, is_order_unique_gift: { type: 'integer', description: '是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否,默认 0' },
no_need_ship: { type: 'integer', description: '?????1 ????/??????? WMS?0 ???? 0' },
barcode: { type: 'string', description: '条码' }, barcode: { type: 'string', description: '条码' },
brand_name: { type: 'string', description: '品牌名称' }, brand_name: { type: 'string', description: '品牌名称' },
image: { type: 'string', description: '图片URL(主图,取 images 首张)' }, image: { type: 'string', description: '图片URL(主图,取 images 首张)' },
@ -555,6 +556,7 @@ module.exports = {
is_shelf_life_mgmt: { type: 'string', description: '是否保质期管理:Y/N,默认N' }, is_shelf_life_mgmt: { type: 'string', description: '是否保质期管理:Y/N,默认N' },
is_batch_mgmt: { type: 'string', description: '是否批次管理:Y/N,默认N' }, is_batch_mgmt: { type: 'string', description: '是否批次管理:Y/N,默认N' },
is_order_unique_gift: { type: 'integer', description: '是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否,默认 0' }, is_order_unique_gift: { type: 'integer', description: '是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否,默认 0' },
no_need_ship: { type: 'integer', description: '?????1 ????/??????? WMS?0 ???? 0' },
tag_price: { type: 'number', description: '标价' }, tag_price: { type: 'number', description: '标价' },
retail_price: { type: 'number', required: true, description: '零售价' }, retail_price: { type: 'number', required: true, description: '零售价' },
cost_price: { type: 'number', description: '成本价' }, cost_price: { type: 'number', description: '成本价' },
@ -587,6 +589,7 @@ module.exports = {
is_shelf_life_mgmt: { type: 'string', description: '是否保质期管理:Y/N' }, is_shelf_life_mgmt: { type: 'string', description: '是否保质期管理:Y/N' },
is_batch_mgmt: { type: 'string', description: '是否批次管理:Y/N' }, is_batch_mgmt: { type: 'string', description: '是否批次管理:Y/N' },
is_order_unique_gift: { type: 'integer', description: '是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否,默认 0' }, is_order_unique_gift: { type: 'integer', description: '是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否,默认 0' },
no_need_ship: { type: 'integer', description: '?????1 ????/??????? WMS?0 ???? 0' },
tag_price: { type: 'number', description: '标价' }, tag_price: { type: 'number', description: '标价' },
retail_price: { type: 'number', description: '零售价' }, retail_price: { type: 'number', description: '零售价' },
cost_price: { type: 'number', description: '成本价' }, cost_price: { type: 'number', description: '成本价' },

View File

@ -37,6 +37,12 @@ module.exports = app => {
is_shelf_life_mgmt: { type: STRING(1), defaultValue: 'N', comment: '是否保质期管理' }, is_shelf_life_mgmt: { type: STRING(1), defaultValue: 'N', comment: '是否保质期管理' },
is_batch_mgmt: { type: STRING(1), defaultValue: 'N', comment: '是否批次管理' }, is_batch_mgmt: { type: STRING(1), defaultValue: 'N', comment: '是否批次管理' },
is_order_unique_gift: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否' }, is_order_unique_gift: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否' },
no_need_ship: {
type: TINYINT,
allowNull: false,
defaultValue: 0,
comment: '不用发货:1 是(不锁/不扣库存、不推 WMS) 0 否',
},
barcode: { type: STRING(100), comment: '条码' }, barcode: { type: STRING(100), comment: '条码' },
image: { type: STRING(255), comment: '图片(主图,取 images 首张)' }, image: { type: STRING(255), comment: '图片(主图,取 images 首张)' },
images: { type: JSON, comment: '图片组(URL 数组,首张为主图)' }, images: { type: JSON, comment: '图片组(URL 数组,首张为主图)' },

View File

@ -98,6 +98,49 @@ class OrderWmsService extends Service {
return { byProduct, byItemCode, productById }; return { byProduct, byItemCode, productById };
} }
/**
* 过滤掉「不用发货」商品(虚拟商品)的明细行
*
* 这类商品(products.no_need_ship=1)不进入 WMS 报文,也不在仓库侧产生作业。
* @param {Object} order 订单(需含 principal_id)
* @param {Array} items 明细(order_status_items)
* @return {Promise<Array>} 需要推给 WMS 的明细
*/
async filterShippableItems(order, items) {
const list = Array.isArray(items) ? items : [];
if (!list.length) return [];
const Op = this.app.Sequelize.Op;
const ids = [ ...new Set(list.map(item => Number(item.product_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 } });
if (!or.length) return list;
const products = await this.ctx.model.Product.findAll({
where: { principal_id: order.principal_id, [Op.or]: or },
attributes: [ 'id', 'item_code', 'no_need_ship' ],
raw: true,
});
if (!products.length) return list;
const virtualIds = new Set();
const virtualCodes = new Set();
for (const product of products) {
if (Number(product.no_need_ship) !== 1) continue;
virtualIds.add(Number(product.id));
if (product.item_code) virtualCodes.add(String(product.item_code));
}
if (!virtualIds.size && !virtualCodes.size) return list;
return list.filter(item => {
if (item.product_id != null && virtualIds.has(Number(item.product_id))) return false;
if (item.item_code && virtualCodes.has(String(item.item_code))) return false;
return true;
});
}
/** /**
* 推送前校验明细是否已落到「系统商品」 * 推送前校验明细是否已落到「系统商品」
* *
@ -325,7 +368,17 @@ class OrderWmsService extends Service {
} }
const client = new QimenWmsClient(api, this.app); const client = new QimenWmsClient(api, this.app);
const items = itemRows.map(item => item.get({ plain: true })); const allItems = itemRows.map(item => item.get({ plain: true }));
// 「不用发货」商品(虚拟商品)不推 WMS;若整单都是这类商品,没有可推的明细
const items = await this.filterShippableItems(order, allItems);
if (!items.length) {
const message = '订单明细全部为「不用发货」商品,无需推送仓库:请取消该单,或改为手工发货';
await order.update({
wms_push_result: { ...base, success: false, failureKind: 'virtual_items', message },
});
ctx.logger.warn(`[order-wms] ${order.order_no} 推送中止:${message}`);
return { ok: false, retry: false, fatal: true, message, attempts };
}
// 明细必须能落到系统商品(平台订单需先绑定店铺商品;手工单按商品库核对),否则推给仓库的会是平台编码 // 明细必须能落到系统商品(平台订单需先绑定店铺商品;手工单按商品库核对),否则推给仓库的会是平台编码
const itemCheck = await this.validateConvertedItems(order, items); const itemCheck = await this.validateConvertedItems(order, items);

View File

@ -38,7 +38,7 @@ const DEFAULT_REMARKS = {
* @param {Object} ctx egg context * @param {Object} ctx egg context
* @param {Object} order 订单(状态表行,需含 id / principal_id) * @param {Object} order 订单(状态表行,需含 id / principal_id)
* @param {Object} [transaction] Sequelize 事务 * @param {Object} [transaction] Sequelize 事务
* @return {Promise<Array<Object>>} [{ product_id, item_code, item_name, plan_qty, inventory_type }] * @return {Promise<Array<Object>>} [{ product_id, item_code, item_name, plan_qty, inventory_type, no_need_ship }]
*/ */
async function loadOrderLines(ctx, order, transaction) { async function loadOrderLines(ctx, order, transaction) {
const Op = ctx.app.Sequelize.Op; const Op = ctx.app.Sequelize.Op;
@ -51,24 +51,33 @@ async function loadOrderLines(ctx, order, transaction) {
if (!items.length) return []; if (!items.length) return [];
const codes = [ ...new Set(items.map(i => i.item_code).filter(v => v != null && String(v).trim() !== '')) ]; const codes = [ ...new Set(items.map(i => i.item_code).filter(v => v != null && String(v).trim() !== '')) ];
const products = codes.length const ids = [ ...new Set(items.map(i => Number(i.product_id)).filter(id => Number.isInteger(id) && id > 0)) ];
const or = [];
if (codes.length) or.push({ item_code: { [Op.in]: codes } });
if (ids.length) or.push({ id: { [Op.in]: ids } });
// 同时按编码与已回填的商品ID查,才能拿到「不用发货」标记(虚拟商品不参与锁/扣库存)
const products = or.length
? await ctx.model.Product.findAll({ ? await ctx.model.Product.findAll({
where: { principal_id: order.principal_id, item_code: { [Op.in]: codes } }, where: { principal_id: order.principal_id, [Op.or]: or },
attributes: [ 'id', 'item_code', 'item_name' ], attributes: [ 'id', 'item_code', 'item_name', 'no_need_ship' ],
transaction, transaction,
}) })
: []; : [];
const byCode = new Map(products.map(p => [ p.item_code, p ])); const byCode = new Map(products.filter(p => p.item_code).map(p => [ p.item_code, p ]));
const byId = new Map(products.map(p => [ Number(p.id), p ]));
return items.map(it => { return items.map(it => {
const row = it.get ? it.get({ plain: true }) : it; const row = it.get ? it.get({ plain: true }) : it;
const product = row.item_code ? byCode.get(row.item_code) : null; const product = (row.item_code ? byCode.get(row.item_code) : null)
|| (row.product_id != null ? byId.get(Number(row.product_id)) : null)
|| null;
return { return {
product_id: product ? Number(product.id) : (row.product_id != null ? Number(row.product_id) : null), product_id: product ? Number(product.id) : (row.product_id != null ? Number(row.product_id) : null),
item_code: row.item_code, item_code: row.item_code,
item_name: row.item_name || (product && product.item_name) || null, item_name: row.item_name || (product && product.item_name) || null,
plan_qty: Number(row.plan_qty) || 0, plan_qty: Number(row.plan_qty) || 0,
inventory_type: row.inventory_type, inventory_type: row.inventory_type,
no_need_ship: !!(product && Number(product.no_need_ship) === 1),
}; };
}); });
} }
@ -98,10 +107,25 @@ async function execute(ctx, order, action, options = {}) {
const transaction = options.transaction || await ctx.model.transaction(); const transaction = options.transaction || await ctx.model.transaction();
try { try {
const lines = await loadOrderLines(ctx, order, transaction); const lines = await loadOrderLines(ctx, order, transaction);
const hit = lines.filter(l => l.product_id && l.plan_qty > 0); // 「不用发货」商品是虚拟商品:不锁、不扣库存(明细仍保留在订单里,只是不参与库存动作)
const shippable = lines.filter(l => !l.no_need_ship);
const hit = shippable.filter(l => l.product_id && l.plan_qty > 0);
// 仅把「未匹配到系统商品」的明细计入 skippedLines(数量为空/0 的明细直接忽略,属数据异常) // 仅把「未匹配到系统商品」的明细计入 skippedLines(数量为空/0 的明细直接忽略,属数据异常)
const skippedLines = lines.filter(l => !l.product_id).map(l => l.item_code || '#?'); const skippedLines = shippable.filter(l => !l.product_id).map(l => l.item_code || '#?');
if (!hit.length) { if (!hit.length) {
const virtualOnly = shippable.length === 0 && lines.length > 0;
if (virtualOnly) {
if (action === 'lock') {
throw new Error('订单明细全部为「不用发货」商品,无需锁定库存:请取消该单,或改为手工发货');
}
// 释放/扣减时没有可操作明细:只清锁定标记,避免卡住状态流转
await ctx.model[orderModelName(order.status)].update(
{ stock_locked: 0 },
{ where: { id: order.id, principal_id: order.principal_id }, transaction }
);
if (ownTransaction) await transaction.commit();
return { action, affected: 0, skipped: true, skippedLines };
}
throw new Error('订单明细未匹配到系统商品(按 item_code 关联 products),无法进行库存操作'); throw new Error('订单明细未匹配到系统商品(按 item_code 关联 products),无法进行库存操作');
} }

View File

@ -0,0 +1,23 @@
'use strict';
/**
* 商品增加「不用发货」标记。
*
* 1 表示该商品是虚拟商品(虚拟发货、服务、赠品等):订单命中后
* - 审核时不再锁定库存、发货确认也不扣减库存;
* - 推送 WMS 时该明细行不进入报文。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('products', 'no_need_ship', {
type: Sequelize.TINYINT,
allowNull: false,
defaultValue: 0,
comment: '不用发货:1 是(不锁/不扣库存、不推 WMS) 0 否',
});
},
async down(queryInterface) {
await queryInterface.removeColumn('products', 'no_need_ship');
},
};

View File

@ -85,3 +85,52 @@ describe('test/app/service/orderWms.test.js', () => {
assert(secondFinished); assert(secondFinished);
}); });
}); });
describe('test/app/service/orderWms 不用发货商品过滤', () => {
function createFilterService(products) {
const service = Object.create(OrderWmsService.prototype);
service.app = {
Sequelize: {
Op: { in: Symbol('in'), or: Symbol('or') },
},
};
service.ctx = {
model: {
Product: {
async findAll() {
return products;
},
},
},
};
return service;
}
it('按商品ID或商品编码命中「不用发货」时排除该明细', async () => {
const service = createFilterService([
{ id: 11, item_code: 'VIRTUAL-1', no_need_ship: 1 },
{ id: 12, item_code: 'REAL-1', no_need_ship: 0 },
]);
const result = await service.filterShippableItems({ principal_id: 5 }, [
{ id: 1, product_id: 11, item_code: 'VIRTUAL-1' },
{ id: 2, product_id: 12, item_code: 'REAL-1' },
{ id: 3, product_id: null, item_code: 'VIRTUAL-1' },
]);
assert.deepStrictEqual(result.map(item => item.id), [ 2 ]);
});
it('没有「不用发货」商品时原样返回', async () => {
const service = createFilterService([{ id: 12, item_code: 'REAL-1', no_need_ship: 0 }]);
const items = [{ id: 1, product_id: 12, item_code: 'REAL-1' }];
const result = await service.filterShippableItems({ principal_id: 5 }, items);
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0], items[0]);
});
it('商品库查不到时不做过滤(交给后续校验报错)', async () => {
const service = createFilterService([]);
const items = [{ id: 1, product_id: 99, item_code: 'UNKNOWN' }];
const result = await service.filterShippableItems({ principal_id: 5 }, items);
assert.strictEqual(result.length, 1);
});
});

View File

@ -39,6 +39,8 @@ export interface ProductItem {
is_batch_mgmt: string is_batch_mgmt: string
/** 是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否 */ /** 是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否 */
is_order_unique_gift: number is_order_unique_gift: number
/** 不用发货:1 是(不锁/不扣库存、不推 WMS)0 否 */
no_need_ship: number
tag_price: number | string | null tag_price: number | string | null
retail_price: number | string retail_price: number | string
cost_price: number | string | null cost_price: number | string | null
@ -100,6 +102,8 @@ export interface ProductPayload {
is_batch_mgmt?: string is_batch_mgmt?: string
/** 是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否 */ /** 是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否 */
is_order_unique_gift?: number is_order_unique_gift?: number
/** 不用发货:1 是(不锁/不扣库存、不推 WMS)0 否 */
no_need_ship?: number
tag_price?: number | null tag_price?: number | null
retail_price: number retail_price: number
cost_price?: number | null cost_price?: number | null

View File

@ -220,6 +220,7 @@ const form = reactive({
is_shelf_life_mgmt: 'N', is_shelf_life_mgmt: 'N',
is_batch_mgmt: 'N', is_batch_mgmt: 'N',
is_order_unique_gift: 0, is_order_unique_gift: 0,
no_need_ship: 0,
tag_price: null as number | null, tag_price: null as number | null,
retail_price: 0, retail_price: 0,
cost_price: null as number | null, cost_price: null as number | null,
@ -253,6 +254,7 @@ const EMPTY_FORM = {
is_shelf_life_mgmt: 'N', is_shelf_life_mgmt: 'N',
is_batch_mgmt: 'N', is_batch_mgmt: 'N',
is_order_unique_gift: 0, is_order_unique_gift: 0,
no_need_ship: 0,
tag_price: null, tag_price: null,
retail_price: 0, retail_price: 0,
cost_price: null, cost_price: null,
@ -304,6 +306,7 @@ function openEdit(row: ProductItem) {
is_shelf_life_mgmt: row.is_shelf_life_mgmt || 'N', is_shelf_life_mgmt: row.is_shelf_life_mgmt || 'N',
is_batch_mgmt: row.is_batch_mgmt || 'N', is_batch_mgmt: row.is_batch_mgmt || 'N',
is_order_unique_gift: Number(row.is_order_unique_gift) ? 1 : 0, is_order_unique_gift: Number(row.is_order_unique_gift) ? 1 : 0,
no_need_ship: Number(row.no_need_ship) ? 1 : 0,
tag_price: toNumberOrNull(row.tag_price), tag_price: toNumberOrNull(row.tag_price),
retail_price: Number(row.retail_price) || 0, retail_price: Number(row.retail_price) || 0,
cost_price: toNumberOrNull(row.cost_price), cost_price: toNumberOrNull(row.cost_price),
@ -627,6 +630,12 @@ onMounted(async () => {
<span v-else class="muted">-</span> <span v-else class="muted">-</span>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column label="不用发货" width="100" align="center">
<template #default="{ row }">
<el-tag v-if="Number(row.no_need_ship) === 1" type="info" size="small">免发货</el-tag>
<span v-else class="muted">-</span>
</template>
</el-table-column>
<el-table-column label="零售价" width="100" align="right"> <el-table-column label="零售价" width="100" align="right">
<template #default="{ row }">{{ row.retail_price }}</template> <template #default="{ row }">{{ row.retail_price }}</template>
</el-table-column> </el-table-column>
@ -1013,6 +1022,21 @@ onMounted(async () => {
</el-tooltip> </el-tooltip>
</div> </div>
</div> </div>
<div class="task-panel__field task-panel__field--control">
<span class="task-panel__field-label">不用发货</span>
<div class="task-panel__field-form task-panel__field-form--row">
<el-form-item prop="no_need_ship">
<el-switch v-model="form.no_need_ship" :active-value="1" :inactive-value="0" />
</el-form-item>
<el-tooltip
content="开启后:订单命中该商品时审核不锁定库存、发货不扣减库存,且推送 WMS 时该明细不进入报文(虚拟商品/服务类)"
placement="top"
>
<el-icon class="field-hint"><QuestionFilled /></el-icon>
</el-tooltip>
</div>
</div>
</div> </div>
<div class="task-panel__section">图片</div> <div class="task-panel__section">图片</div>