WMS_MAIN/test/app/service/systems/order_b2b_packing.test.js
liufumingren 22e95306b7 feat(order): B2B 分箱快照商品明细补仓库商品属性,与拣货明细同口径
- order.getB2bPackingSnapshot 商品明细新增 sku_property / stock_unit / store_id / owner_code / store_remark / stock
- 商品属性走 stockWarehouse.getStockDetails(stockIds, { warehouseId }) 带缓存链路,未命中时按 item_snapshot → order_items 列逐级兜底
- item_code / item_name 取值优先 stock 属性,与 pick.wavePickTaskDetail 的 picking_details 保持一致
- loadB2bOrderItems 查询列补 owner_code
- 同步 methods/system.json 与 methods/changes/system.json(走 doc:api:update)
- 补充单测:stock 属性挂载、快照兜底,7 passing

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-22 18:44:33 +08:00

355 lines
13 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';
const assert = require('assert');
const OrderB2bService = require('../../../../app/service/systems/order_b2b');
describe('OrderB2bService packing quantity validation', () => {
function createService({ orderItems, packedItems }) {
const service = Object.create(OrderB2bService.prototype);
service.ctx = {
model: {
OrderItemsFactory: {
getCurrentModel: async () => ({
findAll: async () => orderItems,
}),
},
OrderB2bItem: {
findAll: async () => packedItems,
},
},
};
return service;
}
it('accepts exact per-line quantities and matching carton count', async () => {
const service = createService({
orderItems: [
{ id: 11, item_code: 'SKU-A', item_name: '商品A', plan_qty: 3 },
{ id: 12, item_code: 'SKU-B', item_name: '商品B', plan_qty: 1 },
],
packedItems: [
{ order_item_id: 11, box_no: 'A01', quantity: 2 },
{ order_item_id: 11, box_no: 'A02', quantity: 1 },
{ order_item_id: 12, box_no: 'A02', quantity: 1 },
],
});
const result = await service.assertB2bPackingQuantityComplete({
warehouseId: 7,
orderId: 99,
sourceType: 'current',
totalBoxCount: 2,
transaction: { LOCK: { UPDATE: 'UPDATE' } },
});
assert.deepStrictEqual(result, { boxCount: 2 });
});
it('rejects underpacked product lines with per-item mismatch details', async () => {
const service = createService({
orderItems: [
{ id: 11, item_code: 'SKU-A', item_name: '商品A', plan_qty: 3 },
],
packedItems: [
{ order_item_id: 11, box_no: 'A01', quantity: 2 },
],
});
await assert.rejects(
service.assertB2bPackingQuantityComplete({
warehouseId: 7,
orderId: 99,
sourceType: 'current',
totalBoxCount: 1,
transaction: { LOCK: { UPDATE: 'UPDATE' } },
}),
error => {
assert.strictEqual(error.code, 'B2B_PREP_PACKING_INCOMPLETE');
assert.deepStrictEqual(error.data.item_mismatches, [
{
order_item_id: 11,
item_code: 'SKU-A',
item_name: '商品A',
expected_quantity: 3,
packed_quantity: 2,
},
]);
return true;
}
);
});
it('replaces all packing rows only with item lines from the target order', async () => {
const transaction = {
LOCK: { UPDATE: 'UPDATE' },
commit: async () => {},
rollback: async () => {},
};
const deleted = [];
const created = [];
const service = Object.create(OrderB2bService.prototype);
service.ctx = {
model: {
transaction: async () => transaction,
OrderItemsFactory: {
getCurrentModel: async () => ({
findAll: async () => [{ id: 11 }],
}),
},
OrderB2bItem: {
destroy: async options => deleted.push(options),
bulkCreate: async (rows, options) => created.push({ rows, options }),
},
OrderB2b: {
update: async () => {},
},
},
};
service.loadPrepTargets = async () => ({
rowMap: new Map([[ 99, { is_b2b: 'Y', status: 'pending' }]]),
b2bMap: new Map([[ 99, { prep_status: 'padding' }]]),
sourceTypeMap: new Map([[ 99, 'current' ]]),
});
service.assertPrepOrders = () => {};
service.writePrepOperationLogs = async () => {};
const result = await service.updateB2bPackingItems({
warehouseId: 7,
orderIds: [ 99 ],
items: [
{ order_item_id: 11, box_no: 'A01', quantity: 2 },
],
});
assert.deepStrictEqual(result, { order_id: 99, item_count: 1, box_count: 1 });
assert.deepStrictEqual(deleted[0].where, { warehouse_id: 7, order_id: 99 });
assert.strictEqual(created[0].rows[0].order_item_id, 11);
assert.strictEqual(created[0].rows[0].quantity, 2);
assert.strictEqual(created[0].options.transaction, transaction);
});
it('returns per-item pick locations from wms_picking merged by space', async () => {
const transaction = { commit: async () => {}, rollback: async () => {} };
const pickQueries = [];
const service = Object.create(OrderB2bService.prototype);
service.app = { Sequelize: { Op: { ne: Symbol('ne') } } };
service.ctx = {
model: {
transaction: async () => transaction,
OrderDeliveryFactory: {
getCurrentModel: async () => ({
findOne: async () => ({ id: 99, is_b2b: 'Y', status: 'pending', delivery_order_code: 'DO-99' }),
}),
},
OrderItemsFactory: {
getCurrentModel: async () => ({
findAll: async () => [
{ id: 11, item_code: 'SKU-A', item_name: '商品A', plan_qty: 3 },
{ id: 12, item_code: 'SKU-B', item_name: '商品B', plan_qty: 1 },
],
}),
},
OrderB2bItem: { findAll: async () => [{ order_item_id: 11, box_no: 'A01', quantity: 3 }] },
OrderB2b: { findOne: async () => ({ prep_status: 'padding', total_box_count: 1 }) },
Picking: {
findAll: async options => {
pickQueries.push(options);
return [
{ order_item_id: 11, space_id: 31, space_name: 'B-02-01', quantity: 2 },
{ order_item_id: 11, space_id: 30, space_name: 'A-01-01', quantity: 1 },
{ order_item_id: 11, space_id: 31, space_name: 'B-02-01', quantity: 1 },
{ order_item_id: 12, space_id: 30, space_name: 'A-01-01', quantity: 1 },
{ order_item_id: 12, space_id: null, space_name: '', quantity: 1 },
];
},
},
},
};
service.isB2bPackingQuantityCheckEnabled = async () => false;
const result = await service.getB2bPackingSnapshot({ warehouseId: 7, orderIds: [ 99 ] });
assert.strictEqual(pickQueries.length, 1);
assert.deepStrictEqual(pickQueries[0].where, {
warehouse_id: 7,
order_id: 99,
status: { [service.app.Sequelize.Op.ne]: 'cancel' },
});
assert.strictEqual(result.pick_location_count, 3);
assert.deepStrictEqual(result.items[0].pick_locations, [
{ space_id: 30, space_name: 'A-01-01', quantity: 1 },
{ space_id: 31, space_name: 'B-02-01', quantity: 3 },
]);
assert.deepStrictEqual(result.items[1].pick_locations, [
{ space_id: null, space_name: null, quantity: 1 },
{ space_id: 30, space_name: 'A-01-01', quantity: 1 },
]);
});
it('attaches stock attributes like picking details do', async () => {
const transaction = { commit: async () => {}, rollback: async () => {} };
const getStockDetailsCalls = [];
const service = Object.create(OrderB2bService.prototype);
service.app = { Sequelize: { Op: { ne: Symbol('ne'), in: Symbol('in') } } };
service.ctx = {
logger: { warn: () => {} },
service: {
systems: {
stockWarehouse: {
getStockDetails: async (stockIds, options) => {
getStockDetailsCalls.push({ stockIds, options });
return [
{
id: 501,
item_code: 'SKU-A',
item_name: '商品A(仓库资料)',
sku_property: '红色/L',
stock_unit: '件',
store_id: 3,
quantity: 100,
image_url: [ 'http://img/a.png' ],
barcodes: [{ id: 1, barcode: '690000000001' }],
},
{
id: 502,
item_code: 'SKU-B',
item_name: '商品B(仓库资料)',
sku_property: '',
stock_unit: '箱',
store_id: 3,
quantity: 8,
image_url: [],
barcodes: [],
},
];
},
},
},
},
model: {
transaction: async () => transaction,
OrderDeliveryFactory: {
getCurrentModel: async () => ({
findOne: async () => ({ id: 99, is_b2b: 'Y', status: 'pending', delivery_order_code: 'DO-99' }),
}),
},
OrderItemsFactory: {
getCurrentModel: async () => ({
findAll: async () => [
{ id: 11, stock_id: 501, item_code: 'SKU-A', item_name: '商品A', plan_qty: 2, owner_code: 'OW-1' },
{ id: 12, stock_id: 502, item_code: 'SKU-B', item_name: '商品B', plan_qty: 1, owner_code: '' },
],
}),
},
OrderB2bItem: { findAll: async () => [] },
OrderB2b: { findOne: async () => null },
Picking: { findAll: async () => [] },
StoreWarehouse: {
findAll: async () => [{ store_id: 3, store_remask: '货主备注X' }],
},
},
};
service.isB2bPackingQuantityCheckEnabled = async () => false;
const result = await service.getB2bPackingSnapshot({ warehouseId: 7, orderIds: [ 99 ] });
assert.strictEqual(getStockDetailsCalls.length, 1);
assert.deepStrictEqual(getStockDetailsCalls[0].stockIds, [ 501, 502 ]);
assert.deepStrictEqual(getStockDetailsCalls[0].options, { warehouseId: 7 });
const [ first, second ] = result.items;
assert.strictEqual(first.item_code, 'SKU-A');
assert.strictEqual(first.item_name, '商品A(仓库资料)');
assert.strictEqual(first.sku_property, '红色/L');
assert.strictEqual(first.stock_unit, '件');
assert.strictEqual(first.store_id, 3);
assert.strictEqual(first.owner_code, 'OW-1');
assert.strictEqual(first.store_remark, '货主备注X');
assert.strictEqual(first.stock.id, 501);
assert.deepStrictEqual(first.stock.barcodes, [{ id: 1, barcode: '690000000001' }]);
assert.deepStrictEqual(first.stock.image_url, [ 'http://img/a.png' ]);
assert.strictEqual(second.stock_unit, '箱');
assert.strictEqual(second.owner_code, '');
assert.strictEqual(second.store_remark, '货主备注X');
});
it('falls back to item_snapshot for stock attributes when stock detail is missing', async () => {
const transaction = { commit: async () => {}, rollback: async () => {} };
const service = Object.create(OrderB2bService.prototype);
service.app = { Sequelize: { Op: { ne: Symbol('ne'), in: Symbol('in') } } };
service.ctx = {
logger: { warn: () => {} },
service: { systems: { stockWarehouse: { getStockDetails: async () => [] } } },
model: {
transaction: async () => transaction,
OrderDeliveryFactory: {
getCurrentModel: async () => ({
findOne: async () => ({ id: 99, is_b2b: 'Y', status: 'pending', delivery_order_code: 'DO-99' }),
}),
},
OrderItemsFactory: {
getCurrentModel: async () => ({
findAll: async () => [{
id: 11,
stock_id: 501,
item_code: 'SKU-A',
item_name: '商品A',
plan_qty: 1,
item_snapshot: JSON.stringify({
item_code: 'SKU-A',
item_name: '商品A(快照)',
sku_property: '蓝色/M',
stock_unit: '个',
store_id: 5,
}),
}],
}),
},
OrderB2bItem: { findAll: async () => [] },
OrderB2b: { findOne: async () => null },
Picking: { findAll: async () => [] },
StoreWarehouse: { findAll: async () => [] },
},
};
service.isB2bPackingQuantityCheckEnabled = async () => false;
const result = await service.getB2bPackingSnapshot({ warehouseId: 7, orderIds: [ 99 ] });
assert.strictEqual(result.items[0].item_name, '商品A(快照)');
assert.strictEqual(result.items[0].sku_property, '蓝色/M');
assert.strictEqual(result.items[0].stock_unit, '个');
assert.strictEqual(result.items[0].store_id, 5);
assert.deepStrictEqual(result.items[0].stock.barcodes, []);
});
it('outputs empty pick locations when the order has no allocation rows', async () => {
const transaction = { commit: async () => {}, rollback: async () => {} };
const service = Object.create(OrderB2bService.prototype);
service.app = { Sequelize: { Op: { ne: Symbol('ne') } } };
service.ctx = {
model: {
transaction: async () => transaction,
OrderDeliveryFactory: {
getCurrentModel: async () => ({
findOne: async () => ({ id: 99, is_b2b: 'Y', status: 'pending', delivery_order_code: 'DO-99' }),
}),
},
OrderItemsFactory: {
getCurrentModel: async () => ({
findAll: async () => [{ id: 11, item_code: 'SKU-A', item_name: '商品A', plan_qty: 1 }],
}),
},
OrderB2bItem: { findAll: async () => [] },
OrderB2b: { findOne: async () => null },
Picking: { findAll: async () => [] },
},
};
service.isB2bPackingQuantityCheckEnabled = async () => false;
const result = await service.getB2bPackingSnapshot({ warehouseId: 7, orderIds: [ 99 ] });
assert.strictEqual(result.pick_location_count, 0);
assert.deepStrictEqual(result.items[0].pick_locations, []);
assert.strictEqual(result.items[0].packed_quantity, 0);
});
});