#6776 makeMulti & itemShelvin_add refactor #1970
|
@ -19,6 +19,10 @@ BEGIN
|
|||
DECLARE vItemFk INT;
|
||||
|
||||
SELECT barcodeToItem(vBarcode) INTO vItemFk;
|
||||
|
||||
SET vPacking = COALESCE(vPacking, GREATEST(vn.itemPacking(vBarcode,vWarehouseFk), 1));
|
||||
|
||||
SET vQuantity = vQuantity * vPacking;
|
||||
|
||||
IF (SELECT COUNT(*) FROM shelving WHERE code = vShelvingFk COLLATE utf8_unicode_ci) = 0 THEN
|
||||
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
const {models} = require('vn-loopback/server/server');
|
||||
const LoopBackContext = require('loopback-context');
|
||||
|
||||
describe('ItemShelving upsertItem()', () => {
|
||||
const warehouseFk = 1;
|
||||
let ctx;
|
||||
let options;
|
||||
let tx;
|
||||
|
||||
beforeEach(async() => {
|
||||
ctx = {
|
||||
req: {
|
||||
accessToken: {userId: 9},
|
||||
headers: {origin: 'http://localhost'}
|
||||
},
|
||||
args: {}
|
||||
};
|
||||
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||||
active: ctx.req
|
||||
});
|
||||
|
||||
options = {transaction: tx};
|
||||
tx = await models.ItemShelving.beginTransaction({});
|
||||
options.transaction = tx;
|
||||
});
|
||||
|
||||
afterEach(async() => {
|
||||
await tx.rollback();
|
||||
});
|
||||
|
||||
it('should add two new records', async() => {
|
||||
const shelvingFk = 'ZPP';
|
||||
const items = [1, 1, 1, 2];
|
||||
|
||||
await models.ItemShelving.upsertItem(ctx, shelvingFk, items, warehouseFk, options);
|
||||
const itemShelvings = await models.ItemShelving.find({where: {shelvingFk}}, options);
|
||||
|
||||
expect(itemShelvings.length).toEqual(2);
|
||||
});
|
||||
|
||||
it('should update the visible items', async() => {
|
||||
const shelvingFk = 'GVC';
|
||||
const items = [2, 2];
|
||||
const {visible: itemsBefore} = await models.ItemShelving.findOne({
|
||||
where: {shelvingFk, itemFk: items[0]}
|
||||
}, options);
|
||||
await models.ItemShelving.upsertItem(ctx, shelvingFk, items, warehouseFk, options);
|
||||
const {visible: itemsAfter} = await models.ItemShelving.findOne({
|
||||
where: {shelvingFk, itemFk: items[0]}
|
||||
}, options);
|
||||
|
||||
expect(itemsAfter).toEqual(itemsBefore + 2);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,64 @@
|
|||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('upsertItem', {
|
||||
description: 'Add a record or update it if it already exists.',
|
||||
accessType: 'WRITE',
|
||||
accepts: [{
|
||||
arg: 'shelvingFk',
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
arg: 'items',
|
||||
type: ['number'],
|
||||
required: true,
|
||||
description: 'array of item foreign keys'
|
||||
},
|
||||
{
|
||||
arg: 'warehouseFk',
|
||||
type: 'number',
|
||||
required: true
|
||||
}],
|
||||
|
||||
http: {
|
||||
path: `/upsertItem`,
|
||||
verb: 'POST'
|
||||
}
|
||||
});
|
||||
|
||||
Self.upsertItem = async(ctx, shelvingFk, items, warehouseFk, options) => {
|
||||
const myOptions = {userId: ctx.req.accessToken.userId};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
const discardItems = new Set();
|
||||
const itemCounts = items.reduce((acc, item) => {
|
||||
acc[item] = (acc[item] || 0) + 1;
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
try {
|
||||
for (let item of items) {
|
||||
if (!discardItems.has(item)) {
|
||||
let quantity = itemCounts[item];
|
||||
discardItems.add(item);
|
||||
|
||||
await Self.rawSql('CALL vn.itemShelving_add(?, ?, ?, NULL, NULL, NULL, ?)',
|
||||
[shelvingFk, item, quantity, warehouseFk], myOptions
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (tx) await tx.commit();
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
|
@ -1,4 +1,5 @@
|
|||
module.exports = Self => {
|
||||
require('../methods/item-shelving/deleteItemShelvings')(Self);
|
||||
require('../methods/item-shelving/upsertItem')(Self);
|
||||
require('../methods/item-shelving/getInventory')(Self);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue