- 列表: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>
360 lines
12 KiB
JavaScript
360 lines
12 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const { Op } = require('sequelize');
|
|
|
|
const WaveService = require('../../../../../app/service/systemMethods/order/wave');
|
|
|
|
/**
|
|
* 创建 singleFetchByIds 轻量测试上下文。
|
|
*
|
|
* @param {Object} options 初始化参数。
|
|
* @param {Array<Object>} options.orders 订单数据。
|
|
* @param {number} [options.storeId=100000] 仓库ID。
|
|
* @return {{ service: WaveService, state: Object }} 测试上下文。
|
|
*/
|
|
function createHarness({ orders = [], storeId = 100000 } = {}) {
|
|
const orderStore = orders.map(item => ({ ...item }));
|
|
const state = {
|
|
committed: false,
|
|
rolledBack: false,
|
|
createWaveCalls: [],
|
|
orderLogItems: [],
|
|
};
|
|
|
|
const transaction = {
|
|
LOCK: {
|
|
UPDATE: 'UPDATE',
|
|
},
|
|
async commit() {
|
|
state.committed = true;
|
|
},
|
|
async rollback() {
|
|
state.rolledBack = true;
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {},
|
|
Sequelize: {
|
|
literal(value) {
|
|
return value;
|
|
},
|
|
},
|
|
},
|
|
user: {
|
|
store_id: storeId,
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
model: {
|
|
async transaction() {
|
|
return transaction;
|
|
},
|
|
async query() {
|
|
return [[], { affectedRows: 0 }];
|
|
},
|
|
},
|
|
};
|
|
|
|
const service = new WaveService(ctx);
|
|
service.getGroupWavePrefix = async () => 'B';
|
|
service.getSingleFetchByIdsWavePrefix = async () => 'A';
|
|
service.getMinGroupOrderFetchQuantity = async () => 3;
|
|
service.createWaveBatchReady = async params => {
|
|
state.createWaveCalls.push({ ...params });
|
|
const wavePrefix = String(params && params.wavePrefix || 'X');
|
|
return {
|
|
waveId: 9001,
|
|
waveNo: `${wavePrefix}202605060001`,
|
|
};
|
|
};
|
|
service.batchCreateOrderOperationLogs = async logItems => {
|
|
state.orderLogItems = Array.isArray(logItems) ? logItems : [];
|
|
return state.orderLogItems.length;
|
|
};
|
|
|
|
service.getOrderModel = async () => ({
|
|
async findAll(options = {}) {
|
|
const where = options.where || {};
|
|
const idIn = where.id && where.id[Op.in];
|
|
const filterIds = Array.isArray(idIn) ? idIn : [];
|
|
const waveEq = Number(where.wave_id || 0);
|
|
const whereOr = Array.isArray(where[Op.or]) ? where[Op.or] : [];
|
|
|
|
const matchGroupCandidates = row => {
|
|
if (waveEq !== 0 || whereOr.length === 0) {
|
|
return false;
|
|
}
|
|
|
|
return whereOr.some(clause => {
|
|
if (!clause || typeof clause !== 'object') {
|
|
return false;
|
|
}
|
|
|
|
if (clause.items_hash && clause.items_hash[Op.in]) {
|
|
return clause.items_hash[Op.in].includes(String(row.items_hash || ''));
|
|
}
|
|
|
|
if (clause.items && clause.items[Op.in]) {
|
|
return clause.items[Op.in].includes(String(row.items || ''));
|
|
}
|
|
|
|
return false;
|
|
});
|
|
};
|
|
|
|
if (waveEq > 0) {
|
|
return orderStore
|
|
.filter(row => filterIds.includes(Number(row.id || 0)))
|
|
.filter(row => Number(row.wave_id || 0) === waveEq)
|
|
.map(row => ({ id: row.id }));
|
|
}
|
|
|
|
if (whereOr.length > 0) {
|
|
return orderStore
|
|
.filter(row => Number(row.wave_id || 0) === 0)
|
|
.filter(row => [ 1, 2, 3 ].includes(Number(row.step || 0)))
|
|
.filter(row => String(row.status || '') !== 'cancel')
|
|
.filter(row => String(row.outbound_status || '') !== 'padding')
|
|
.filter(row => matchGroupCandidates(row))
|
|
.map(row => ({
|
|
id: row.id,
|
|
items_hash: row.items_hash,
|
|
items: row.items,
|
|
logistics_name: row.logistics_name,
|
|
}));
|
|
}
|
|
|
|
return orderStore
|
|
.filter(row => filterIds.includes(Number(row.id || 0)))
|
|
.map(row => ({
|
|
id: row.id,
|
|
wave_id: row.wave_id,
|
|
wave_no: row.wave_no,
|
|
step: row.step,
|
|
status: row.status,
|
|
outbound_status: row.outbound_status,
|
|
items_hash: row.items_hash,
|
|
items: row.items,
|
|
logistics_name: row.logistics_name,
|
|
intercept_status: row.intercept_status,
|
|
lock_status: row.lock_status,
|
|
is_b2b: row.is_b2b,
|
|
has_sn_item: row.has_sn_item,
|
|
}));
|
|
},
|
|
|
|
async update(values = {}, options = {}) {
|
|
const where = options.where || {};
|
|
const idIn = where.id && where.id[Op.in];
|
|
const ids = Array.isArray(idIn) ? idIn : [];
|
|
const waveIdEq = Number(where.wave_id || 0);
|
|
let affected = 0;
|
|
|
|
for (const row of orderStore) {
|
|
if (!ids.includes(Number(row.id || 0))) {
|
|
continue;
|
|
}
|
|
|
|
if (Number(row.wave_id || 0) !== waveIdEq) {
|
|
continue;
|
|
}
|
|
|
|
row.wave_id = Number(values.wave_id || 0);
|
|
row.wave_no = String(values.wave_no || '');
|
|
affected += 1;
|
|
}
|
|
|
|
return [ affected ];
|
|
},
|
|
});
|
|
|
|
return {
|
|
service,
|
|
state,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systemMethods/order/wave.singleFetchByIds.test.js', () => {
|
|
it('should exclude orders already in wave and fetch remaining orders into one single wave', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 1, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none' },
|
|
{ id: 2, wave_id: 88, wave_no: 'W88', step: 1, status: 'new', outbound_status: 'none' },
|
|
{ id: 3, wave_id: 0, wave_no: '', step: 2, status: 'cancel', outbound_status: 'none' },
|
|
{ id: 4, wave_id: 0, wave_no: '', step: 9, status: 'new', outbound_status: 'none' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 1, 2, 3, 4, 5 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.wave_id, 9001);
|
|
assert.equal(result.wave_no, 'A202605060001');
|
|
assert.deepEqual(result.fetched_order_ids, [ 1, 3, 4 ]);
|
|
assert.equal(result.fetched_order_count, 3);
|
|
assert.deepEqual(result.excluded_existing_wave_order_ids, [ 2 ]);
|
|
assert.deepEqual(result.not_found_order_ids, [ 5 ]);
|
|
|
|
assert.equal(state.createWaveCalls.length, 1);
|
|
assert.equal(state.createWaveCalls[0].wavePrefix, 'A');
|
|
assert.equal(state.createWaveCalls[0].waveType, 2);
|
|
assert.equal(state.createWaveCalls[0].distinguishExpress, false);
|
|
assert.equal(state.createWaveCalls[0].logisticsName, '');
|
|
assert.equal(state.orderLogItems.length, 3);
|
|
assert.equal(state.orderLogItems[0].operationCode, 'ORDER_WAVE_ASSIGN');
|
|
assert.equal(state.committed, true);
|
|
assert.equal(state.rolledBack, false);
|
|
});
|
|
|
|
it('should return empty fetch result when no order can be fetched', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 10, wave_id: 77, wave_no: 'W77', step: 1, status: 'new', outbound_status: 'none' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 10 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.wave_id, 0);
|
|
assert.equal(result.fetched_order_count, 0);
|
|
assert.deepEqual(result.excluded_existing_wave_order_ids, [ 10 ]);
|
|
assert.equal(state.createWaveCalls.length, 0);
|
|
assert.equal(state.orderLogItems.length, 0);
|
|
assert.equal(state.committed, true);
|
|
assert.equal(state.rolledBack, false);
|
|
});
|
|
|
|
it('should fetch same-structure orders when they are not in any wave', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 101, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', items_hash: 'H-A', items: 'I-A', logistics_name: 'ZTO' },
|
|
{ id: 102, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', items_hash: 'H-A', items: 'I-A', logistics_name: 'YTO' },
|
|
{ id: 103, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', items_hash: 'H-A', items: 'I-A', logistics_name: 'STO' },
|
|
{ id: 104, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', items_hash: 'H-B', items: 'I-B', logistics_name: 'ZTO' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 101, 104 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.wave_id, 9001);
|
|
assert.deepEqual(result.fetched_order_ids, [ 101, 104 ]);
|
|
assert.deepEqual(result.skipped_order_ids, []);
|
|
assert.equal(state.createWaveCalls.length, 1);
|
|
assert.equal(state.createWaveCalls[0].wavePrefix, 'A');
|
|
assert.equal(state.createWaveCalls[0].waveType, 2);
|
|
assert.equal(state.createWaveCalls[0].distinguishExpress, false);
|
|
assert.equal(state.committed, true);
|
|
assert.equal(state.rolledBack, false);
|
|
});
|
|
|
|
it('should support order_ids alias for delete-by-ids scene', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 201, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none' },
|
|
{ id: 202, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ order_ids: [ 201, 202 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.deepEqual(result.ids, [ 201, 202 ]);
|
|
assert.deepEqual(result.fetched_order_ids, [ 201, 202 ]);
|
|
assert.equal(result.fetched_order_count, 2);
|
|
assert.equal(state.createWaveCalls.length, 1);
|
|
assert.equal(state.committed, true);
|
|
assert.equal(state.rolledBack, false);
|
|
});
|
|
|
|
it('should support orderIds alias for delete-by-ids scene', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 301, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ orderIds: [ 301 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.deepEqual(result.ids, [ 301 ]);
|
|
assert.deepEqual(result.fetched_order_ids, [ 301 ]);
|
|
assert.equal(result.fetched_order_count, 1);
|
|
assert.equal(state.createWaveCalls.length, 1);
|
|
assert.equal(state.committed, true);
|
|
assert.equal(state.rolledBack, false);
|
|
});
|
|
|
|
it('should allow SN orders to be fetched by order ids', async () => {
|
|
const { service } = createHarness({
|
|
orders: [
|
|
{ id: 401, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', has_sn_item: 'Y' },
|
|
{ id: 402, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', has_sn_item: 'N' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 401, 402 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.deepEqual(result.fetched_order_ids, [ 401, 402 ]);
|
|
assert.equal(result.fetched_order_count, 2);
|
|
assert.deepEqual(result.excluded_sn_order_ids, []);
|
|
assert.deepEqual(result.skipped_order_ids, []);
|
|
});
|
|
|
|
it('should still fetch SN orders while excluding intercepted orders', async () => {
|
|
const { service } = createHarness({
|
|
orders: [
|
|
{ id: 501, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', has_sn_item: 'Y' },
|
|
{ id: 503, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', intercept_status: 'padding' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 501, 503 ] });
|
|
|
|
assert.deepEqual(result.fetched_order_ids, [ 501 ]);
|
|
assert.deepEqual(result.excluded_intercepted_order_ids, [ 503 ]);
|
|
assert.deepEqual(result.excluded_sn_order_ids, []);
|
|
});
|
|
|
|
it('should reject the whole request when any B2B order is included', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 601, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none' },
|
|
{ id: 602, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', is_b2b: 'Y' },
|
|
{ id: 603, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', is_b2b: 'N' },
|
|
],
|
|
});
|
|
|
|
await assert.rejects(
|
|
async () => await service.singleFetchByIds({ ids: [ 601, 602, 603 ] }),
|
|
/B2B 订单不支持抓取散单(订单\[602\]),操作不生效/
|
|
);
|
|
|
|
// 整批驳回:不建波次、不写订单日志,事务回滚
|
|
assert.equal(state.createWaveCalls.length, 0);
|
|
assert.equal(state.orderLogItems.length, 0);
|
|
assert.equal(state.committed, false);
|
|
assert.equal(state.rolledBack, true);
|
|
});
|
|
|
|
it('should treat null is_b2b as non B2B order', async () => {
|
|
const { service } = createHarness({
|
|
orders: [
|
|
{ id: 701, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', is_b2b: null },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 701 ] });
|
|
|
|
assert.deepEqual(result.fetched_order_ids, [ 701 ]);
|
|
});
|
|
});
|