From 22f78dbb916179040abe755d962ab90874246d39 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Mon, 25 Mar 2024 07:54:51 +0100 Subject: [PATCH] refs #6964 feat:hasItemOlder --- .../methods/item-shelving/hasItemOlder.js | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/modules/item/back/methods/item-shelving/hasItemOlder.js b/modules/item/back/methods/item-shelving/hasItemOlder.js index fc4eb8d4f..e7609a4db 100644 --- a/modules/item/back/methods/item-shelving/hasItemOlder.js +++ b/modules/item/back/methods/item-shelving/hasItemOlder.js @@ -1,3 +1,4 @@ +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethod('hasItemOlder', { description: @@ -35,23 +36,24 @@ module.exports = Self => { }); Self.hasItemOlder = async(shelvingFkIn, parking, shelvingFkOut, itemFk, options) => { + if (!parking && !shelvingFkOut) throw new UserError('Missing data: parking or shelving'); const result = await Self.rawSql(` - SELECT COUNT(ish.id) - FROM vn.itemShelving ish - JOIN ( - SELECT ish.itemFk, created,shelvingFk, p.code - FROM vn.itemShelving ish - JOIN vn.shelving s ON ish.shelvingFk = s.code - LEFT JOIN vn.parking p ON p.id = s.parkingFk - WHERE ish.shelvingFk = ? + SELECT COUNT(ish.id) countItemOlder + FROM vn.itemShelving ish + JOIN ( + SELECT ish.itemFk, created,shelvingFk, p.code + FROM vn.itemShelving ish + JOIN vn.shelving s ON ish.shelvingFk = s.code + LEFT JOIN vn.parking p ON p.id = s.parkingFk + 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 + 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]); - return result[0]['COUNT(ish.id)'] > 0; + return result[0]['countItemOlder'] > 0; }; };