diff --git a/db/routines/vn/procedures/item_getSimilar.sql b/db/routines/vn/procedures/item_getSimilar.sql index a61898756..f79bed375 100644 --- a/db/routines/vn/procedures/item_getSimilar.sql +++ b/db/routines/vn/procedures/item_getSimilar.sql @@ -60,7 +60,11 @@ BEGIN (i.value8 <=> its.value8) match8, a.available, IFNULL(ip.counter, 0) `counter`, - IF(b.groupingMode = 1, b.grouping, b.packing) minQuantity, + CASE + WHEN b.groupingMode = 1 THEN b.grouping + WHEN b.groupingMode = 2 THEN b.packing + ELSE 1 + END AS minQuantity, iss.visible located FROM vn.item i JOIN cache.available a ON a.item_id = i.id diff --git a/db/versions/10967-wheatLilium/00-firstScript.sql b/db/versions/10967-wheatLilium/00-firstScript.sql new file mode 100644 index 000000000..40cfe45bd --- /dev/null +++ b/db/versions/10967-wheatLilium/00-firstScript.sql @@ -0,0 +1,4 @@ +-- Place your SQL code here + +INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId) + VALUES('ItemShelving', 'hasItemOlder', 'READ', 'ALLOW', 'ROLE', 'production'); diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 00e3ae50f..f374b14dc 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -352,4 +352,4 @@ "The line could not be marked": "La linea no puede ser marcada", "This password can only be changed by the user themselves": "Esta contraseƱa solo puede ser modificada por el propio usuario", "They're not your subordinate": "No es tu subordinado/a." -} +} \ No newline at end of file diff --git a/modules/item/back/methods/item-shelving/hasItemOlder.js b/modules/item/back/methods/item-shelving/hasItemOlder.js new file mode 100644 index 000000000..ee4cdc829 --- /dev/null +++ b/modules/item/back/methods/item-shelving/hasItemOlder.js @@ -0,0 +1,63 @@ +const UserError = require('vn-loopback/util/user-error'); +module.exports = Self => { + Self.remoteMethod('hasItemOlder', { + description: + 'Get boolean if any or specific item of the shelving has older created in another shelving or parking', + accessType: 'READ', + accepts: [{ + arg: 'shelvingFkIn', + type: 'string', + required: true, + description: 'Shelving code' + }, + { + arg: 'parking', + type: 'string', + description: 'Parking code' + }, + { + arg: 'shelvingFkOut', + type: 'string', + description: 'Shelving code' + }, + { + arg: 'itemFk', + type: 'integer', + description: 'Item id' + }], + returns: { + type: 'boolean', + root: true + }, + http: { + path: `/hasItemOlder`, + verb: 'GET' + } + }); + + Self.hasItemOlder = async(shelvingFkIn, parking, shelvingFkOut, itemFk, options) => { + if (!parking && !shelvingFkOut) throw new UserError('Missing data: parking or shelving'); + + const myOptions = {}; + if (typeof options == 'object') + Object.assign(myOptions, options); + + const result = await Self.rawSql(` + SELECT COUNT(ish.id) countItemOlder + FROM vn.itemShelving ish + JOIN ( + SELECT ish.itemFk, created,shelvingFk + FROM vn.itemShelving ish + JOIN vn.shelving s ON ish.shelvingFk = s.code + WHERE ish.shelvingFk = ? + )sub ON sub.itemFK = ish.itemFk + JOIN vn.shelving s ON s.code = ish.shelvingFk + JOIN vn.parking p ON p.id = s.parkingFk + WHERE sub.created > ish.created + AND (p.code <> ? OR ? IS NULL) + AND (ish.shelvingFk <> ? OR ? IS NULL) + AND (ish.itemFk <> ? OR ? IS NULL)`, + [shelvingFkIn, parking, parking, shelvingFkOut, shelvingFkOut, itemFk, itemFk], myOptions); + return result[0]['countItemOlder'] > 0; + }; +}; diff --git a/modules/item/back/methods/item-shelving/specs/hasItemOlder.spec.js b/modules/item/back/methods/item-shelving/specs/hasItemOlder.spec.js new file mode 100644 index 000000000..abffead53 --- /dev/null +++ b/modules/item/back/methods/item-shelving/specs/hasItemOlder.spec.js @@ -0,0 +1,45 @@ + +const {models} = require('vn-loopback/server/server'); + +describe('itemShelving hasOlder()', () => { + it('should return false because there are not older items', async() => { + const shelvingFkIn = 'GVC'; + const shelvingFkOut = 'HEJ'; + const result = await models.ItemShelving.hasItemOlder(shelvingFkIn, null, shelvingFkOut); + + expect(result).toBe(false); + }); + + it('should return false because there are not older items in parking', async() => { + const shelvingFkIn = 'HEJ'; + const parking = '700-01'; + const result = await models.ItemShelving.hasItemOlder(shelvingFkIn, parking); + + expect(result).toBe(false); + }); + + it('should return true because there is an older item', async() => { + const shelvingFkIn = 'UXN'; + const shelvingFkOut = 'PCC'; + const parking = 'A-01-1'; + const itemFk = 1; + + const tx = await models.ItemShelving.beginTransaction({}); + const myOptions = {transaction: tx}; + const filter = {where: {shelvingFk: shelvingFkOut} + }; + try { + const itemShelvingBefore = await models.ItemShelving.findOne(filter, myOptions); + await itemShelvingBefore.updateAttributes({ + itemFk: itemFk + }, myOptions); + const result = await models.ItemShelving.hasItemOlder(shelvingFkIn, parking, null, null, myOptions); + + expect(result).toBe(true); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/item/back/models/item-shelving.js b/modules/item/back/models/item-shelving.js index 53e57dcee..d48ee10d5 100644 --- a/modules/item/back/models/item-shelving.js +++ b/modules/item/back/models/item-shelving.js @@ -4,4 +4,5 @@ module.exports = Self => { require('../methods/item-shelving/getInventory')(Self); require('../methods/item-shelving/getAlternative')(Self); require('../methods/item-shelving/updateFromSale')(Self); + require('../methods/item-shelving/hasItemOlder')(Self); };