307 lines
8.3 KiB
JavaScript
307 lines
8.3 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const StockTransactionService = require('../../../../app/service/systems/stock_transaction');
|
|
|
|
function createServiceHarness() {
|
|
const calls = {
|
|
createArgs: null,
|
|
orderFindOneArgs: null,
|
|
};
|
|
|
|
const state = {
|
|
orderRow: {
|
|
id: 500001,
|
|
source_order_codes: 'SRC-ORDER-001,SRC-ORDER-002',
|
|
wave_no: 'WAVE-001',
|
|
},
|
|
};
|
|
|
|
const model = {
|
|
StockTransaction: {
|
|
async create(payload, options) {
|
|
calls.createArgs = {
|
|
payload,
|
|
options,
|
|
};
|
|
return {
|
|
id: 1,
|
|
...payload,
|
|
};
|
|
},
|
|
},
|
|
OrderDelivery: {
|
|
async findOne(query) {
|
|
calls.orderFindOneArgs = query;
|
|
return state.orderRow;
|
|
},
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
model,
|
|
app: {
|
|
model,
|
|
},
|
|
};
|
|
|
|
const service = new StockTransactionService(ctx);
|
|
|
|
return {
|
|
service,
|
|
calls,
|
|
state,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/stock_transaction.create.test.js', () => {
|
|
it('should throw error when transaction is missing', async () => {
|
|
const { service } = createServiceHarness();
|
|
|
|
await assert.rejects(async () => {
|
|
await service.create({
|
|
transaction_type: 'INBOUND',
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
stock_id: 200001,
|
|
quantity: 12,
|
|
});
|
|
}, /stockTransaction\.create.*transaction/);
|
|
});
|
|
|
|
it('should create stock transaction with snapshot fields', async () => {
|
|
const { service, calls } = createServiceHarness();
|
|
const transaction = {
|
|
id: 'tx-1',
|
|
};
|
|
|
|
const result = await service.create({
|
|
transaction_type: 'INBOUND',
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
stock_id: 200001,
|
|
storage_id: 300001,
|
|
space_id: 400001,
|
|
quantity: 12,
|
|
batch_no: 'BATCH-001',
|
|
inventory_type: 'ZP',
|
|
order_type: 'SCRK',
|
|
order_no: 'EO-001',
|
|
order_id: 500001,
|
|
receiving_id: 600001,
|
|
entry_order_id: 700001,
|
|
operator_id: 'op-1',
|
|
operator_name: '测试人员',
|
|
remark: '测试流水',
|
|
before_quantity: 88,
|
|
after_quantity: 100,
|
|
before_locked_quantity: 5,
|
|
after_locked_quantity: 5,
|
|
created_at: new Date('2026-04-09T08:00:00Z'),
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
assert.equal(result.id, 1);
|
|
assert.ok(calls.createArgs, '应调用 StockTransaction.create');
|
|
assert.equal(calls.createArgs.options.transaction, transaction);
|
|
assert.equal(calls.createArgs.payload.before_quantity, 88);
|
|
assert.equal(calls.createArgs.payload.after_quantity, 100);
|
|
assert.equal(calls.createArgs.payload.before_locked_quantity, 5);
|
|
assert.equal(calls.createArgs.payload.after_locked_quantity, 5);
|
|
assert.equal(calls.createArgs.payload.quantity, 12);
|
|
assert.equal(calls.createArgs.payload.transaction_type, 'INBOUND');
|
|
});
|
|
|
|
it('should force inbound order_id to null when upstream passes order_id', async () => {
|
|
const { service, calls } = createServiceHarness();
|
|
const transaction = {
|
|
id: 'tx-inbound-null-order-id',
|
|
};
|
|
|
|
await service.create({
|
|
transaction_type: 'INBOUND',
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
stock_id: 200001,
|
|
quantity: 5,
|
|
order_type: 'SCRK',
|
|
order_no: 'EO-999999',
|
|
order_id: 999999,
|
|
remark: '入库测试',
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
assert.equal(calls.createArgs.payload.order_id, null);
|
|
assert.equal(calls.createArgs.payload.order_no, 'EO-999999');
|
|
assert.equal(calls.createArgs.payload.order_type, 'SCRK');
|
|
});
|
|
|
|
it('should normalize outbound order fields by order id and source_order_codes', async () => {
|
|
const { service, calls } = createServiceHarness();
|
|
const transaction = {
|
|
id: 'tx-2',
|
|
};
|
|
|
|
await service.create({
|
|
transaction_type: 'OUTBOUND',
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
stock_id: 200001,
|
|
quantity: 3,
|
|
order_type: 'STOCK_STORE_OUTBOUND',
|
|
order_id: 500001,
|
|
order_no: 'DO-500001',
|
|
remark: '订单出库',
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
assert.ok(calls.orderFindOneArgs, '应查询订单来源单号用于出库流水兜底');
|
|
assert.equal(calls.orderFindOneArgs.where.id, 500001);
|
|
assert.equal(calls.orderFindOneArgs.transaction, transaction);
|
|
assert.equal(calls.createArgs.payload.order_id, 500001);
|
|
assert.equal(calls.createArgs.payload.order_no, 'SRC-ORDER-001,SRC-ORDER-002');
|
|
assert.equal(calls.createArgs.payload.order_type, 'ORDER');
|
|
assert.equal(calls.createArgs.payload.batch_no, 'WAVE-001');
|
|
});
|
|
|
|
it('should keep non-inbound order_id unchanged for outbound transaction', async () => {
|
|
const { service, calls } = createServiceHarness();
|
|
const transaction = {
|
|
id: 'tx-outbound-keep-order-id',
|
|
};
|
|
|
|
await service.create({
|
|
transaction_type: 'OUTBOUND',
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
stock_id: 200001,
|
|
quantity: 3,
|
|
order_type: 'STOCK_STORE_OUTBOUND',
|
|
order_id: 500001,
|
|
source_order_codes: 'SRC-ORDER-001,SRC-ORDER-002',
|
|
remark: '订单出库',
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
assert.equal(calls.createArgs.payload.order_id, 500001);
|
|
assert.equal(calls.createArgs.payload.order_type, 'ORDER');
|
|
});
|
|
|
|
it('should overwrite outbound batch_no with wave_no when order has wave_no', async () => {
|
|
const { service, calls, state } = createServiceHarness();
|
|
const transaction = {
|
|
id: 'tx-2-1',
|
|
};
|
|
|
|
state.orderRow = {
|
|
id: 500001,
|
|
source_order_codes: 'SRC-ORDER-009',
|
|
wave_no: 'WAVE-009',
|
|
};
|
|
|
|
await service.create({
|
|
transaction_type: 'OUTBOUND',
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
stock_id: 200001,
|
|
quantity: 3,
|
|
order_type: 'ANY_UPSTREAM_TYPE',
|
|
order_id: 500001,
|
|
source_order_codes: 'SRC-ORDER-009',
|
|
batch_no: 'UPSTREAM-BATCH-009',
|
|
remark: '订单出库',
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
assert.equal(calls.createArgs.payload.order_type, 'ORDER');
|
|
assert.equal(calls.createArgs.payload.order_id, 500001);
|
|
assert.equal(calls.createArgs.payload.order_no, 'SRC-ORDER-009');
|
|
assert.equal(calls.createArgs.payload.batch_no, 'WAVE-009');
|
|
});
|
|
|
|
it('should set outbound batch_no to null when order wave_no is empty', async () => {
|
|
const { service, calls, state } = createServiceHarness();
|
|
const transaction = {
|
|
id: 'tx-2-1-null',
|
|
};
|
|
|
|
state.orderRow = {
|
|
id: 500001,
|
|
source_order_codes: 'SRC-ORDER-010',
|
|
wave_no: '',
|
|
};
|
|
|
|
await service.create({
|
|
transaction_type: 'OUTBOUND',
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
stock_id: 200001,
|
|
quantity: 3,
|
|
order_type: 'ORDER',
|
|
order_id: 500001,
|
|
source_order_codes: 'SRC-ORDER-010',
|
|
batch_no: 'UPSTREAM-BATCH-010',
|
|
remark: '订单出库',
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
assert.equal(calls.createArgs.payload.order_type, 'ORDER');
|
|
assert.equal(calls.createArgs.payload.batch_no, null);
|
|
});
|
|
|
|
it('should keep non-order outbound order_type unchanged', async () => {
|
|
const { service, calls } = createServiceHarness();
|
|
const transaction = {
|
|
id: 'tx-2-2',
|
|
};
|
|
|
|
await service.create({
|
|
transaction_type: 'OUTBOUND',
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
stock_id: 200001,
|
|
quantity: 2,
|
|
order_type: 'STOCK_MOVE',
|
|
batch_no: 'MOVE-BATCH-001',
|
|
order_id: null,
|
|
order_no: 'MOVE-001',
|
|
remark: '移库出库',
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
assert.equal(calls.createArgs.payload.order_type, 'STOCK_MOVE');
|
|
assert.equal(calls.createArgs.payload.order_no, 'MOVE-001');
|
|
assert.equal(calls.createArgs.payload.batch_no, 'MOVE-BATCH-001');
|
|
assert.equal(calls.orderFindOneArgs, null);
|
|
});
|
|
|
|
it('should reject outbound transaction when order_id is invalid', async () => {
|
|
const { service } = createServiceHarness();
|
|
const transaction = {
|
|
id: 'tx-3',
|
|
};
|
|
|
|
await assert.rejects(async () => {
|
|
await service.create({
|
|
transaction_type: 'OUTBOUND',
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
stock_id: 200001,
|
|
quantity: 1,
|
|
order_type: 'STOCK_STORE_OUTBOUND',
|
|
order_id: null,
|
|
remark: '订单出库',
|
|
}, {
|
|
transaction,
|
|
});
|
|
}, /order_id 必须为订单主键 id/);
|
|
});
|
|
});
|