#6776 makeMulti & itemShelvin_add refactor #1970

Merged
jorgep merged 11 commits from 6776-makeMulti into dev 2024-02-19 12:23:45 +00:00
4 changed files with 124 additions and 0 deletions

View File

@ -20,6 +20,10 @@ BEGIN
SELECT barcodeToItem(vBarcode) INTO vItemFk; 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 IF (SELECT COUNT(*) FROM shelving WHERE code = vShelvingFk COLLATE utf8_unicode_ci) = 0 THEN
INSERT IGNORE INTO parking(code) VALUES(vShelvingFk); INSERT IGNORE INTO parking(code) VALUES(vShelvingFk);

View File

@ -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);
});
});

View File

@ -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;
}
};
};

View File

@ -1,4 +1,5 @@
module.exports = Self => { module.exports = Self => {
require('../methods/item-shelving/deleteItemShelvings')(Self); require('../methods/item-shelving/deleteItemShelvings')(Self);
require('../methods/item-shelving/upsertItem')(Self);
require('../methods/item-shelving/getInventory')(Self); require('../methods/item-shelving/getInventory')(Self);
}; };