From 3d948ef317f615dab2dc90223dba104ef623ba75 Mon Sep 17 00:00:00 2001 From: carlosjr Date: Tue, 9 Feb 2021 10:43:19 +0100 Subject: [PATCH] warning color added lines with clientType loses --- .../10281-valentineDay/00-item_getBalance.sql | 137 ++++++++++++++++++ db/changes/10281-valentineDay/delete.keep | 0 modules/item/back/methods/item/getBalance.js | 6 +- .../methods/item/specs/getBalance.spec.js | 33 +++++ modules/item/front/diary/index.html | 18 ++- 5 files changed, 185 insertions(+), 9 deletions(-) create mode 100644 db/changes/10281-valentineDay/00-item_getBalance.sql delete mode 100644 db/changes/10281-valentineDay/delete.keep create mode 100644 modules/item/back/methods/item/specs/getBalance.spec.js diff --git a/db/changes/10281-valentineDay/00-item_getBalance.sql b/db/changes/10281-valentineDay/00-item_getBalance.sql new file mode 100644 index 000000000..96c82cc01 --- /dev/null +++ b/db/changes/10281-valentineDay/00-item_getBalance.sql @@ -0,0 +1,137 @@ +DROP PROCEDURE `vn`.`item_getBalance`; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`item_getBalance`(IN vItemId INT, IN vWarehouse INT) +BEGIN + DECLARE vDateInventory DATETIME; + DECLARE vCurdate DATE DEFAULT CURDATE(); + DECLARE vDayEnd DATETIME DEFAULT util.dayEnd(vCurdate); + + SELECT inventoried INTO vDateInventory FROM config; + SET @a = 0; + SET @currentLineFk = 0; + SET @shipped = ''; + + SELECT DATE(@shipped:= shipped) shipped, + alertLevel, + stateName, + origin, + reference, + clientFk, + name, + `in`, + `out`, + @a := @a + IFNULL(`in`,0) - IFNULL(`out`,0) as balance, + @currentLineFk := IF (@shipped < CURDATE() + OR (@shipped = CURDATE() AND (isPicked OR alertLevel >= 2)), + lineFk,@currentLineFk) lastPreparedLineFk, + isTicket, + lineFk, + isPicked, + clientType + FROM + ( SELECT tr.landed AS shipped, + b.quantity AS `in`, + NULL AS `out`, + al.alertLevel AS alertLevel, + st.name AS stateName, + s.name AS name, + e.ref AS reference, + e.id AS origin, + s.id AS clientFk, + IF(al.alertLevel = 3, TRUE, FALSE) isPicked, + FALSE AS isTicket, + b.id lineFk, + NULL `order`, + NULL AS clientType + FROM buy b + JOIN entry e ON e.id = b.entryFk + JOIN travel tr ON tr.id = e.travelFk + JOIN supplier s ON s.id = e.supplierFk + JOIN alertLevel al ON al.alertLevel = + CASE + WHEN tr.shipped < CURDATE() THEN 3 + WHEN tr.shipped = CURDATE() AND tr.isReceived = TRUE THEN 3 + ELSE 0 + END + JOIN state st ON st.code = al.code + WHERE tr.landed >= vDateInventory + AND vWarehouse = tr.warehouseInFk + AND b.itemFk = vItemId + AND e.isInventory = FALSE + AND e.isRaid = FALSE + UNION ALL + + SELECT tr.shipped, + NULL as `in`, + b.quantity AS `out`, + al.alertLevel AS alertLevel, + st.name AS stateName, + s.name AS name, + e.ref AS reference, + e.id AS origin, + s.id AS clientFk, + IF(al.alertLevel = 3, TRUE, FALSE) isPicked, + FALSE AS isTicket, + b.id, + NULL `order`, + NULL AS clientType + FROM buy b + JOIN entry e ON e.id = b.entryFk + JOIN travel tr ON tr.id = e.travelFk + JOIN warehouse w ON w.id = tr.warehouseOutFk + JOIN supplier s ON s.id = e.supplierFk + JOIN alertLevel al ON al.alertLevel = + CASE + WHEN tr.shipped < CURDATE() THEN 3 + WHEN tr.shipped = CURDATE() AND tr.isReceived = TRUE THEN 3 + ELSE 0 + END + JOIN state st ON st.code = al.code + WHERE tr.shipped >= vDateInventory + AND vWarehouse =tr.warehouseOutFk + AND s.id <> 4 + AND b.itemFk = vItemId + AND e.isInventory = FALSE + AND w.isFeedStock = FALSE + AND e.isRaid = FALSE + UNION ALL + + SELECT DATE(t.shipped), + NULL as `in`, + s.quantity AS `out`, + al.alertLevel AS alertLevel, + st.name AS stateName, + t.nickname AS name, + t.refFk AS reference, + t.id AS origin, + t.clientFk, + stk.id AS isPicked, + TRUE AS isTicket, + s.id, + st.`order`, + ct.code AS clientType + FROM sale s + JOIN ticket t ON t.id = s.ticketFk + LEFT JOIN ticketState ts ON ts.ticket = t.id + LEFT JOIN state st ON st.code = ts.code + JOIN client c ON c.id = t.clientFk + JOIN clientType ct ON ct.id = c.clientTypeFk + JOIN alertLevel al ON al.alertLevel = + CASE + WHEN t.shipped < curdate() THEN 3 + WHEN t.shipped > util.dayEnd(curdate()) THEN 0 + ELSE IFNULL(ts.alertLevel, 0) + END + LEFT JOIN state stPrep ON stPrep.`code` = 'PREPARED' + LEFT JOIN saleTracking stk ON stk.saleFk = s.id AND stk.stateFk = stPrep.id + WHERE t.shipped >= vDateInventory + AND s.itemFk = vItemId + AND vWarehouse =t.warehouseFk + ORDER BY shipped, alertLevel DESC, isTicket, `order` DESC, isPicked DESC, `in` DESC, `out` DESC + ) AS itemDiary; + +END$$ +DELIMITER ; + diff --git a/db/changes/10281-valentineDay/delete.keep b/db/changes/10281-valentineDay/delete.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/modules/item/back/methods/item/getBalance.js b/modules/item/back/methods/item/getBalance.js index 26fc2a7dc..916757c46 100644 --- a/modules/item/back/methods/item/getBalance.js +++ b/modules/item/back/methods/item/getBalance.js @@ -20,8 +20,12 @@ module.exports = Self => { }); Self.getBalance = async filter => { - let where = filter.where; + const where = filter.where; let [diary] = await Self.rawSql(`CALL vn.item_getBalance(?, ?)`, [where.itemFk, where.warehouseFk]); + + for (const entry of diary) + if (entry.clientType === 'loses') entry.highlighted = true; + return diary; }; }; diff --git a/modules/item/back/methods/item/specs/getBalance.spec.js b/modules/item/back/methods/item/specs/getBalance.spec.js new file mode 100644 index 000000000..671b8c103 --- /dev/null +++ b/modules/item/back/methods/item/specs/getBalance.spec.js @@ -0,0 +1,33 @@ +const app = require('vn-loopback/server/server'); +const LoopBackContext = require('loopback-context'); + +describe('item getBalance()', () => { + it('should return the balance lines of a client type loses in which one has highlighted true', async() => { + const activeCtx = { + accessToken: {userId: 9}, + }; + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + + const losesClientId = 111; + const ticket = await app.models.Ticket.findById(7); + const originalClientId = ticket.clientFk; + await ticket.updateAttribute('clientFk', losesClientId); + + const filter = { + where: { + itemFk: 1, + warehouseFk: 1 + } + }; + const results = await app.models.Item.getBalance(filter); + + const result = results.find(element => element.clientType == 'loses'); + + expect(result.highlighted).toBe(true); + + // restores + await ticket.updateAttribute('clientFk', originalClientId); + }); +}); diff --git a/modules/item/front/diary/index.html b/modules/item/front/diary/index.html index 3fdc9a9e4..7c210a26a 100644 --- a/modules/item/front/diary/index.html +++ b/modules/item/front/diary/index.html @@ -64,14 +64,16 @@ {{::sale.stateName | dashIfEmpty}} {{::sale.reference | dashIfEmpty}} - - {{::sale.name | dashIfEmpty}} - - - {{::sale.name | dashIfEmpty}} + + + {{::sale.name | dashIfEmpty}} + + + {{::sale.name | dashIfEmpty}} + {{::sale.in | dashIfEmpty}}