diff --git a/db/routines/vn/procedures/itemShelving_add.sql b/db/routines/vn/procedures/itemShelving_add.sql index 02762fa0b..2a4676b50 100644 --- a/db/routines/vn/procedures/itemShelving_add.sql +++ b/db/routines/vn/procedures/itemShelving_add.sql @@ -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 diff --git a/modules/item/back/methods/item-shelving/specs/addListByItem.spec.js b/modules/item/back/methods/item-shelving/specs/addListByItem.spec.js new file mode 100644 index 000000000..e31ff2e61 --- /dev/null +++ b/modules/item/back/methods/item-shelving/specs/addListByItem.spec.js @@ -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); + }); +}); diff --git a/modules/item/back/methods/item-shelving/upsertItem.js b/modules/item/back/methods/item-shelving/upsertItem.js new file mode 100644 index 000000000..49c2f1b0d --- /dev/null +++ b/modules/item/back/methods/item-shelving/upsertItem.js @@ -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; + } + }; +}; diff --git a/modules/item/back/models/item-shelving.js b/modules/item/back/models/item-shelving.js index 98ff18931..c031d8271 100644 --- a/modules/item/back/models/item-shelving.js +++ b/modules/item/back/models/item-shelving.js @@ -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); };