383 lines
9.6 KiB
JavaScript
383 lines
9.6 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const StorageInventoryService = require('../../../../app/service/systems/storage/inventory');
|
|
|
|
/**
|
|
* 创建移库测试支架。
|
|
* @return {{service: StorageInventoryService, calls: Object}} 测试支架
|
|
*/
|
|
function createTransferHarness() {
|
|
const calls = {
|
|
storageTransactions: [],
|
|
storageUpdates: [],
|
|
locationSpaceUpdates: [],
|
|
commitCount: 0,
|
|
rollbackCount: 0,
|
|
};
|
|
|
|
const transaction = {
|
|
LOCK: {
|
|
UPDATE: 'UPDATE',
|
|
},
|
|
async commit() {
|
|
calls.commitCount += 1;
|
|
},
|
|
async rollback() {
|
|
calls.rollbackCount += 1;
|
|
},
|
|
};
|
|
|
|
const srcRecord = {
|
|
id: 11,
|
|
warehouse_id: 100000,
|
|
store_id: 100001,
|
|
space_id: 21,
|
|
stock_id: 301,
|
|
batch_no: 'BATCH-TRANSFER-001',
|
|
inventory_type: 'ZP',
|
|
quantity: 10,
|
|
locked_quantity: 0,
|
|
available_quantity: 10,
|
|
is_enabled: 1,
|
|
delete_at: null,
|
|
};
|
|
|
|
const model = {
|
|
async transaction() {
|
|
return transaction;
|
|
},
|
|
StockWarehouse: {
|
|
async findOne() {
|
|
return {
|
|
id: 301,
|
|
quantity: 100,
|
|
locked_quantity: 12,
|
|
};
|
|
},
|
|
},
|
|
Storage: {
|
|
async findOne(args) {
|
|
if (typeof args.where.id === 'number') {
|
|
return srcRecord;
|
|
}
|
|
return null;
|
|
},
|
|
async update(values, options) {
|
|
calls.storageUpdates.push({ values, options });
|
|
},
|
|
async count() {
|
|
return 0;
|
|
},
|
|
},
|
|
LocationSpace: {
|
|
async findOne() {
|
|
return {
|
|
id: 22,
|
|
warehouse_id: 100000,
|
|
is_enabled: true,
|
|
space_status: 'AVAILABLE',
|
|
space_code: 'A-22',
|
|
};
|
|
},
|
|
async update(values, options) {
|
|
calls.locationSpaceUpdates.push({ values, options });
|
|
},
|
|
},
|
|
StorageTransaction: {
|
|
async create(payload, options) {
|
|
calls.storageTransactions.push({ payload, options });
|
|
return { id: calls.storageTransactions.length, ...payload };
|
|
},
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
model,
|
|
app: {
|
|
Sequelize: {
|
|
Op: {
|
|
ne: '$ne',
|
|
},
|
|
literal(value) {
|
|
return { literal: value };
|
|
},
|
|
},
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
user: {
|
|
employees_id: 'EMP-001',
|
|
employees_name: '测试操作员',
|
|
},
|
|
service: {
|
|
systems: {
|
|
operationLog: {
|
|
async createOperationLog() {
|
|
return null;
|
|
},
|
|
},
|
|
notice: {
|
|
async send() {
|
|
return null;
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
return {
|
|
service: new StorageInventoryService(ctx),
|
|
calls,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 创建按商品移库测试支架。
|
|
* @return {{service: StorageInventoryService, calls: Object}} 测试支架
|
|
*/
|
|
function createTransferByProductHarness() {
|
|
const calls = {
|
|
storageTransactions: [],
|
|
storageUpdates: [],
|
|
storageCreates: [],
|
|
locationSpaceUpdates: [],
|
|
commitCount: 0,
|
|
rollbackCount: 0,
|
|
};
|
|
|
|
const transaction = {
|
|
LOCK: {
|
|
UPDATE: 'UPDATE',
|
|
},
|
|
async commit() {
|
|
calls.commitCount += 1;
|
|
},
|
|
async rollback() {
|
|
calls.rollbackCount += 1;
|
|
},
|
|
};
|
|
|
|
const srcRecords = [
|
|
{
|
|
id: 101,
|
|
warehouse_id: 100000,
|
|
store_id: 100001,
|
|
space_id: 21,
|
|
stock_id: 301,
|
|
batch_no: null,
|
|
inventory_type: 'ZP',
|
|
quantity: 2,
|
|
locked_quantity: 0,
|
|
available_quantity: 2,
|
|
is_enabled: 1,
|
|
delete_at: null,
|
|
},
|
|
{
|
|
id: 102,
|
|
warehouse_id: 100000,
|
|
store_id: 100001,
|
|
space_id: 21,
|
|
stock_id: 301,
|
|
batch_no: null,
|
|
inventory_type: 'ZP',
|
|
quantity: 4,
|
|
locked_quantity: 0,
|
|
available_quantity: 4,
|
|
is_enabled: 1,
|
|
delete_at: null,
|
|
},
|
|
];
|
|
|
|
const model = {
|
|
async transaction() {
|
|
return transaction;
|
|
},
|
|
StockWarehouse: {
|
|
async findOne() {
|
|
return {
|
|
id: 301,
|
|
is_batch_mgmt: 'N',
|
|
quantity: 50,
|
|
locked_quantity: 7,
|
|
};
|
|
},
|
|
},
|
|
Storage: {
|
|
async findAll() {
|
|
return srcRecords;
|
|
},
|
|
async findOne() {
|
|
return null;
|
|
},
|
|
async update(values, options) {
|
|
calls.storageUpdates.push({ values, options });
|
|
},
|
|
async create(payload) {
|
|
const created = {
|
|
id: 900 + calls.storageCreates.length + 1,
|
|
...payload,
|
|
};
|
|
calls.storageCreates.push(created);
|
|
return created;
|
|
},
|
|
async count(options) {
|
|
if (options.where.space_id === 22) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
},
|
|
},
|
|
LocationSpace: {
|
|
async findOne() {
|
|
return {
|
|
id: 22,
|
|
warehouse_id: 100000,
|
|
is_enabled: true,
|
|
space_status: 'AVAILABLE',
|
|
space_code: 'A-22',
|
|
};
|
|
},
|
|
async update(values, options) {
|
|
calls.locationSpaceUpdates.push({ values, options });
|
|
},
|
|
},
|
|
StorageTransaction: {
|
|
async create(payload, options) {
|
|
calls.storageTransactions.push({ payload, options });
|
|
return { id: calls.storageTransactions.length, ...payload };
|
|
},
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
model,
|
|
app: {
|
|
Sequelize: {
|
|
Op: {
|
|
ne: '$ne',
|
|
},
|
|
literal(value) {
|
|
return { literal: value };
|
|
},
|
|
},
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
user: {
|
|
employees_id: 'EMP-001',
|
|
employees_name: '测试操作员',
|
|
},
|
|
service: {
|
|
systems: {
|
|
operationLog: {
|
|
async createOperationLog() {
|
|
return null;
|
|
},
|
|
},
|
|
notice: {
|
|
async send() {
|
|
return null;
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
return {
|
|
service: new StorageInventoryService(ctx),
|
|
calls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/storage.inventory.transfer.test.js', () => {
|
|
it('should create TRANSFER_OUT and TRANSFER_IN transactions in transferLocation', async () => {
|
|
const { service, calls } = createTransferHarness();
|
|
|
|
const result = await service.transferLocation({
|
|
storage_id: 11,
|
|
target_space_id: 22,
|
|
quantity: 10,
|
|
});
|
|
|
|
assert.equal(result.code, 200);
|
|
assert.equal(calls.commitCount, 1);
|
|
assert.equal(calls.rollbackCount, 0);
|
|
assert.equal(calls.storageTransactions.length, 2);
|
|
|
|
const transferOut = calls.storageTransactions[0];
|
|
const transferIn = calls.storageTransactions[1];
|
|
|
|
assert.equal(transferOut.payload.transaction_type, 'TRANSFER_OUT');
|
|
assert.equal(transferOut.payload.storage_id, 11);
|
|
assert.equal(transferOut.payload.space_id, 21);
|
|
assert.equal(transferOut.payload.quantity, 10);
|
|
assert.equal(transferOut.payload.before_quantity, 100);
|
|
assert.equal(transferOut.payload.after_quantity, 100);
|
|
assert.equal(transferOut.payload.before_locked_quantity, 12);
|
|
assert.equal(transferOut.payload.after_locked_quantity, 12);
|
|
assert.equal(transferOut.payload.order_id, null);
|
|
assert.equal(transferOut.payload.order_no, null);
|
|
assert.equal(transferOut.payload.order_type, null);
|
|
assert.equal(transferOut.payload.remark, '移库出');
|
|
|
|
assert.equal(transferIn.payload.transaction_type, 'TRANSFER_IN');
|
|
assert.equal(transferIn.payload.storage_id, 11);
|
|
assert.equal(transferIn.payload.space_id, 22);
|
|
assert.equal(transferIn.payload.quantity, 10);
|
|
assert.equal(transferIn.payload.before_quantity, 100);
|
|
assert.equal(transferIn.payload.after_quantity, 100);
|
|
assert.equal(transferIn.payload.before_locked_quantity, 12);
|
|
assert.equal(transferIn.payload.after_locked_quantity, 12);
|
|
assert.equal(transferIn.payload.order_id, null);
|
|
assert.equal(transferIn.payload.order_no, null);
|
|
assert.equal(transferIn.payload.order_type, null);
|
|
assert.equal(transferIn.payload.remark, '移库入');
|
|
});
|
|
|
|
it('should create TRANSFER_OUT and TRANSFER_IN transactions in transferByProduct for each toTransfer', async () => {
|
|
const { service, calls } = createTransferByProductHarness();
|
|
|
|
const result = await service.transferByProduct({
|
|
space_id: 21,
|
|
stock_id: 301,
|
|
target_space_id: 22,
|
|
quantity: 5,
|
|
});
|
|
|
|
assert.equal(result.code, 200);
|
|
assert.equal(calls.commitCount, 1);
|
|
assert.equal(calls.rollbackCount, 0);
|
|
assert.equal(calls.storageTransactions.length, 4);
|
|
|
|
const transferOutTransactions = calls.storageTransactions.filter(item => item.payload.transaction_type === 'TRANSFER_OUT');
|
|
const transferInTransactions = calls.storageTransactions.filter(item => item.payload.transaction_type === 'TRANSFER_IN');
|
|
|
|
assert.equal(transferOutTransactions.length, 2);
|
|
assert.equal(transferInTransactions.length, 2);
|
|
|
|
const outTotalQuantity = transferOutTransactions.reduce((sum, item) => sum + Number(item.payload.quantity || 0), 0);
|
|
const inTotalQuantity = transferInTransactions.reduce((sum, item) => sum + Number(item.payload.quantity || 0), 0);
|
|
assert.equal(outTotalQuantity, 5);
|
|
assert.equal(inTotalQuantity, 5);
|
|
|
|
for (const tx of calls.storageTransactions) {
|
|
assert.equal(tx.payload.before_quantity, tx.payload.after_quantity);
|
|
assert.equal(tx.payload.before_locked_quantity, tx.payload.after_locked_quantity);
|
|
assert.equal(tx.payload.before_quantity, 50);
|
|
assert.equal(tx.payload.after_quantity, 50);
|
|
assert.equal(tx.payload.before_locked_quantity, 7);
|
|
assert.equal(tx.payload.after_locked_quantity, 7);
|
|
assert.equal(tx.payload.order_id, null);
|
|
assert.equal(tx.payload.order_no, null);
|
|
assert.equal(tx.payload.order_type, null);
|
|
}
|
|
});
|
|
});
|