WMS_MAIN/test/app/service/systems/order_b2b_packing.test.js
liufumingren 59bc65b443 feat(order): B2B 加急优先、分箱明细与取货储位,作业链路收口
- 列表:B2B 待处理/配送列表固定加急优先排序(wms_order_b2b.is_urgent='Y' 置顶),
  作业明细筛选口径不变
- 备货/提货:新增 setB2bUrgent/cancelB2bUrgent、startB2bPrep/cancelB2bPrep/finishB2bPrep、
  setB2bPickupPlate、pickupB2b、updateB2bOrderImages;提货即出库完成
  (写 outbound_status=fin / step=4 / delivery_time 并迁移到配送表),车牌与提货照片
  随单落库
- 分箱:新增 wms_order_b2b_items 与 order.updateB2bPackingItems、order.getB2bPackingSnapshot;
  快照按商品行返回 wms_picking 取货储位(items[].pick_locations / pick_location_count),
  列表不再携带储位明细
- 齐套校验:仓库 subject_config 新增 b2b_packing_quantity_check,备货完成时逐商品校验
  分箱数量合计等于 plan_qty 且总箱数与箱号数一致
- 拦截:B2B 订单不参与波次抓取、快捷复核批量发货、反审回待审核与克隆,命中即整批驳回
- 明细:order.pendingOrder.detail / order.deliveryOrder.detail 为 B2B 订单附加副表作业明细
  b2b、备货照片 images 与提货照片 pickup_images
- 迁移:wms_order_b2b 新增 is_urgent 与备货作业字段,新建 wms_order_b2b_items
- 文档:methods/system.json 与 methods/changes/system.json 同步至 0.3.375

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

219 lines
7.7 KiB
JavaScript

'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('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);
});
});