From a7895ab0a45c56ce7a04e86258dd3ab2add482f6 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 4 Jul 2024 09:17:22 +0200 Subject: [PATCH 1/9] refs #6769 First commit --- db/routines/vn/procedures/item_getBalance.sql | 224 +++++++++++------- 1 file changed, 144 insertions(+), 80 deletions(-) diff --git a/db/routines/vn/procedures/item_getBalance.sql b/db/routines/vn/procedures/item_getBalance.sql index a4942af6f..561957452 100644 --- a/db/routines/vn/procedures/item_getBalance.sql +++ b/db/routines/vn/procedures/item_getBalance.sql @@ -2,25 +2,65 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getBalance`( vItemFk INT, vWarehouseFk INT, - vDate DATETIME + vDated DATETIME ) BEGIN /** - * @vItemFk item a buscar - * @vWarehouseFk almacen donde buscar - * @vDate Si la fecha es null, muestra el histórico desde el inventario. - * Si la fecha no es null, muestra histórico desde la fecha de vDate. + * Calcula el disponible. + * + * @vItemFk Id de artículo + * @vWarehouseFk Id de almacén + * @vDated Fecha + * Si la fecha es NULL, muestra el histórico desde el inventario. + * Si la fecha no es NULL, muestra histórico desde la fecha de vDated. */ DECLARE vDateInventory DATETIME; + DECLARE vLifeScope DATE; + DECLARE vWarehouseInventoryFk INT; + DECLARE vSupplierInventoryFk INT; - IF vDate IS NULL THEN - SELECT inventoried INTO vDateInventory - FROM config; - ELSE - SELECT mockUtcTime INTO vDateInventory - FROM util.config; + SELECT IF(vDated, uc.mockUtcTime, c.inventoried) INTO vDateInventory + FROM config c + JOIN util.config uc; + + SELECT COALESCE(vDated, vDateInventory) - INTERVAL MAX(life) DAY + INTO vLifeScope + FROM itemType; + + SELECT warehouseOutFk, supplierFk + INTO vWarehouseInventoryFk, vSupplierInventoryFk + FROM inventoryConfig; + + IF NOT vWarehouseInventoryFk OR NOT vSupplierInventoryFk THEN + CALL util.throw('Config variables are not set'); END IF; + -- Calcula el ultimo dia de vida para cada producto + CREATE OR REPLACE TEMPORARY TABLE tItemRange + (PRIMARY KEY (itemFk)) + ENGINE = MEMORY + SELECT i.id itemFk, + util.dayEnd(c.maxLanded + INTERVAL it.life DAY) ended, + it.life + FROM item i + LEFT JOIN ( + SELECT b.itemFk, MAX(t.landed) maxLanded + FROM buy b + JOIN entry e ON b.entryFk = e.id + JOIN travel t ON t.id = e.travelFk + JOIN warehouse w ON w.id = t.warehouseInFk + JOIN item i ON i.id = b.itemFk + JOIN itemType it ON it.id = i.typeFk + WHERE t.landed BETWEEN vLifeScope AND COALESCE(vDated, vDateInventory) + AND t.warehouseInFk = vWarehouseFk + AND t.warehouseOutFk <> vWarehouseInventoryFk + AND it.life + AND NOT e.isExcludedFromAvailable + GROUP BY b.itemFk + ) c ON i.id = c.itemFk + JOIN itemType it ON it.id = i.typeFk + HAVING ended >= COALESCE(vDated, vDateInventory) OR life IS NULL; + CREATE OR REPLACE TEMPORARY TABLE tItemDiary( shipped DATE, `in` INT(11), @@ -37,8 +77,9 @@ BEGIN `order` TINYINT(3) UNSIGNED, clientType VARCHAR(20), claimFk INT(10) UNSIGNED, - inventorySupplierFk INT(10) - ); + inventorySupplierFk INT(10), + orderFk INT(10) UNSIGNED + ) ENGINE = MEMORY; INSERT INTO tItemDiary WITH entriesIn AS ( @@ -57,7 +98,8 @@ BEGIN NULL `order`, NULL clientType, NULL claimFk, - ec.inventorySupplierFk + vSupplierInventoryFk inventorySupplierFk, + NULL orderFk FROM vn.buy b JOIN vn.entry e ON e.id = b.entryFk JOIN vn.travel tr ON tr.id = e.travelFk @@ -66,20 +108,20 @@ BEGIN OR (util.VN_CURDATE() AND tr.isReceived), 'DELIVERED', 'FREE') - JOIN vn.entryConfig ec WHERE tr.landed >= vDateInventory - AND vWarehouseFk = tr.warehouseInFk - AND (s.id <> ec.inventorySupplierFk OR vDate IS NULL) + AND tr.warehouseInFk = vWarehouseFk + AND (s.id <> vSupplierInventoryFk OR vDated IS NULL) AND b.itemFk = vItemFk AND NOT e.isExcludedFromAvailable AND NOT e.isRaid - ), entriesOut AS ( + ), + entriesOut AS ( SELECT tr.shipped, NULL, b.quantity, st.alertLevel, st.name stateName, - s.name , + s.name, e.invoiceNumber, e.id entryFk, s.id supplierFk, @@ -89,7 +131,8 @@ BEGIN NULL `order`, NULL clientType, NULL claimFk, - ec.inventorySupplierFk + vSupplierInventoryFk, + NULL orderFk FROM vn.buy b JOIN vn.entry e ON e.id = b.entryFk JOIN vn.travel tr ON tr.id = e.travelFk @@ -102,39 +145,41 @@ BEGIN JOIN vn.entryConfig ec WHERE tr.shipped >= vDateInventory AND vWarehouseFk = tr.warehouseOutFk - AND (s.id <> ec.inventorySupplierFk OR vDate IS NULL) + AND (s.id <> vSupplierInventoryFk OR vDated IS NULL) AND b.itemFk = vItemFk AND NOT e.isExcludedFromAvailable AND NOT w.isFeedStock AND NOT e.isRaid - ), sales AS ( - SELECT DATE(t.shipped) shipped, - s.quantity, - st2.alertLevel, - st2.name, - t.nickname, - t.refFk, - t.id ticketFk, - t.clientFk, - s.id saleFk, - st.`order`, - c.typeFk, - cb.claimFk - FROM vn.sale s - JOIN vn.ticket t ON t.id = s.ticketFk - LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id - LEFT JOIN vn.state st ON st.`code` = ts.`code` - JOIN vn.client c ON c.id = t.clientFk - JOIN vn.state st2 ON st2.`code` = IF(t.shipped < util.VN_CURDATE(), - 'DELIVERED', - IF (t.shipped > util.dayEnd(util.VN_CURDATE()), - 'FREE', - IFNULL(ts.code, 'FREE'))) - LEFT JOIN vn.claimBeginning cb ON s.id = cb.saleFk - WHERE t.shipped >= vDateInventory - AND s.itemFk = vItemFk - AND vWarehouseFk = t.warehouseFk - ),sale AS ( + ), + sales AS ( + WITH itemSales AS ( + SELECT DATE(t.shipped) shipped, + s.quantity, + st2.alertLevel, + st2.name, + t.nickname, + t.refFk, + t.id ticketFk, + t.clientFk, + s.id saleFk, + st.`order`, + c.typeFk, + cb.claimFk + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + LEFT JOIN vn.state st ON st.`code` = ts.`code` + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.state st2 ON st2.`code` = IF(t.shipped < util.VN_CURDATE(), + 'DELIVERED', + IF (t.shipped > util.dayEnd(util.VN_CURDATE()), + 'FREE', + IFNULL(ts.code, 'FREE'))) + LEFT JOIN vn.claimBeginning cb ON s.id = cb.saleFk + WHERE t.shipped >= vDateInventory + AND s.itemFk = vItemFk + AND vWarehouseFk = t.warehouseFk + ) SELECT s.shipped, NULL `in`, s.quantity, @@ -144,39 +189,55 @@ BEGIN s.refFk, s.ticketFk, s.clientFk, - IF(stk.saleFk, TRUE, NULL), + IF(stk.saleFk, TRUE, FALSE), TRUE, s.saleFk, s.`order`, s.typeFk, s.claimFk, - NULL - FROM sales s + NULL, + NULL orderFk + FROM itemSales s LEFT JOIN vn.state stPrep ON stPrep.`code` = 'PREPARED' LEFT JOIN vn.saleTracking stk ON stk.saleFk = s.saleFk AND stk.stateFk = stPrep.id GROUP BY s.saleFk - ) SELECT shipped, - `in`, - `out`, - alertLevel, - stateName, - `name`, - reference, - origin, - clientFk, - isPicked, - isTicket, - lineFk, - `order`, - clientType, - claimFk, - inventorySupplierFk - FROM entriesIn + ), + orders AS ( + SELECT r.shipment, + NULL 'in', + r.amount, + NULL alertLevel, + NULL stateName, + NULL, + NULL invoiceNumber, + NULL entryFk, + NULL supplierFk, + FALSE, + FALSE isTicket, + NULL buyFk, + NULL 'order', + c.typeFk, + NULL claimFk, + NULL inventorySupplierFk, + o.id + FROM hedera.orderRow r + JOIN hedera.`order` o ON o.id = r.orderFk + JOIN tItemRange ir ON ir.itemFk = r.itemFk + JOIN vn.client c ON c.id = o.customer_id + WHERE r.shipment >= vDateInventory + AND (ir.ended IS NULL OR r.shipment <= ir.ended) + AND r.warehouseFk = vWarehouseFk + AND NOT o.confirmed + AND r.itemFk = vItemFk + ) + SELECT * FROM entriesIn UNION ALL SELECT * FROM entriesOut UNION ALL - SELECT * FROM sale + SELECT * FROM sales + UNION ALL + SELECT * FROM orders ORDER BY shipped, (inventorySupplierFk = clientFk) DESC, alertLevel DESC, @@ -186,8 +247,7 @@ BEGIN `in` DESC, `out` DESC; - IF vDate IS NULL THEN - + IF vDated IS NULL THEN SET @a := 0; SET @currentLineFk := 0; SET @shipped := ''; @@ -211,16 +271,16 @@ BEGIN t.isPicked, t.clientType, t.claimFk, - t.`order` + t.`order`, + t.orderFk FROM tItemDiary t LEFT JOIN alertLevel a ON a.id = t.alertLevel; - ELSE SELECT IFNULL(SUM(IFNULL(`in`, 0)) - SUM(IFNULL(`out`, 0)), 0) INTO @a FROM tItemDiary - WHERE shipped < vDate; + WHERE shipped < vDated; - SELECT vDate shipped, + SELECT vDated shipped, 0 alertLevel, 0 stateName, 0 origin, @@ -236,7 +296,8 @@ BEGIN 0 isPicked, 0 clientType, 0 claimFk, - NULL `order` + NULL `order`, + 0 orderFk UNION ALL SELECT shipped, alertlevel, @@ -253,11 +314,14 @@ BEGIN isPicked, clientType, claimFk, - `order` + `order`, + orderFk FROM tItemDiary - WHERE shipped >= vDate; + WHERE shipped >= vDated; END IF; - DROP TEMPORARY TABLE tItemDiary; + DROP TEMPORARY TABLE + tItemDiary, + tItemRange; END$$ DELIMITER ; -- 2.40.1 From cf7de14de6e5c9babc89d70c20224aa96c665c3f Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 4 Jul 2024 11:34:55 +0200 Subject: [PATCH 2/9] refs #6769 Changes, with error no database selected --- db/routines/vn/procedures/item_getBalance.sql | 95 ++++++++----------- 1 file changed, 38 insertions(+), 57 deletions(-) diff --git a/db/routines/vn/procedures/item_getBalance.sql b/db/routines/vn/procedures/item_getBalance.sql index 561957452..dc4e16789 100644 --- a/db/routines/vn/procedures/item_getBalance.sql +++ b/db/routines/vn/procedures/item_getBalance.sql @@ -61,29 +61,11 @@ BEGIN JOIN itemType it ON it.id = i.typeFk HAVING ended >= COALESCE(vDated, vDateInventory) OR life IS NULL; - CREATE OR REPLACE TEMPORARY TABLE tItemDiary( - shipped DATE, - `in` INT(11), - `out` INT(11), - alertLevel INT(11), - stateName VARCHAR(20), - `name` VARCHAR(50), - reference VARCHAR(50), - origin INT(11), - clientFk INT(11), - isPicked INT(11), - isTicket TINYINT(1), - lineFk INT(11), - `order` TINYINT(3) UNSIGNED, - clientType VARCHAR(20), - claimFk INT(10) UNSIGNED, - inventorySupplierFk INT(10), - orderFk INT(10) UNSIGNED - ) ENGINE = MEMORY; - - INSERT INTO tItemDiary + CREATE OR REPLACE TEMPORARY TABLE tItemDiary + ENGINE = MEMORY WITH entriesIn AS ( - SELECT tr.landed shipped, + SELECT 'entry' `type`, + tr.landed shipped, b.quantity `in`, NULL `out`, st.alertLevel , @@ -98,13 +80,12 @@ BEGIN NULL `order`, NULL clientType, NULL claimFk, - vSupplierInventoryFk inventorySupplierFk, - NULL orderFk + vSupplierInventoryFk inventorySupplierFk FROM vn.buy b JOIN vn.entry e ON e.id = b.entryFk JOIN vn.travel tr ON tr.id = e.travelFk JOIN vn.supplier s ON s.id = e.supplierFk - JOIN vn.state st ON st.`code` = IF( tr.landed < util.VN_CURDATE() + JOIN vn.state st ON st.`code` = IF(tr.landed < util.VN_CURDATE() OR (util.VN_CURDATE() AND tr.isReceived), 'DELIVERED', 'FREE') @@ -116,7 +97,8 @@ BEGIN AND NOT e.isRaid ), entriesOut AS ( - SELECT tr.shipped, + SELECT 'entry', + tr.shipped, NULL, b.quantity, st.alertLevel, @@ -131,8 +113,7 @@ BEGIN NULL `order`, NULL clientType, NULL claimFk, - vSupplierInventoryFk, - NULL orderFk + vSupplierInventoryFk FROM vn.buy b JOIN vn.entry e ON e.id = b.entryFk JOIN vn.travel tr ON tr.id = e.travelFk @@ -180,7 +161,8 @@ BEGIN AND s.itemFk = vItemFk AND vWarehouseFk = t.warehouseFk ) - SELECT s.shipped, + SELECT 'ticket', + s.shipped, NULL `in`, s.quantity, s.alertLevel, @@ -195,8 +177,7 @@ BEGIN s.`order`, s.typeFk, s.claimFk, - NULL, - NULL orderFk + NULL FROM itemSales s LEFT JOIN vn.state stPrep ON stPrep.`code` = 'PREPARED' LEFT JOIN vn.saleTracking stk ON stk.saleFk = s.saleFk @@ -204,23 +185,23 @@ BEGIN GROUP BY s.saleFk ), orders AS ( - SELECT r.shipment, + SELECT 'order' `type`, + r.shipment, NULL 'in', r.amount, NULL alertLevel, NULL stateName, - NULL, + c.name, NULL invoiceNumber, - NULL entryFk, - NULL supplierFk, + o.id origin, + c.id, FALSE, FALSE isTicket, NULL buyFk, NULL 'order', c.typeFk, NULL claimFk, - NULL inventorySupplierFk, - o.id + NULL FROM hedera.orderRow r JOIN hedera.`order` o ON o.id = r.orderFk JOIN tItemRange ir ON ir.itemFk = r.itemFk @@ -252,7 +233,8 @@ BEGIN SET @currentLineFk := 0; SET @shipped := ''; - SELECT DATE(@shipped:= t.shipped) shipped, + SELECT t.type, + DATE(@shipped:= t.shipped) shipped, t.alertLevel, t.stateName, t.origin, @@ -271,8 +253,7 @@ BEGIN t.isPicked, t.clientType, t.claimFk, - t.`order`, - t.orderFk + t.`order` FROM tItemDiary t LEFT JOIN alertLevel a ON a.id = t.alertLevel; ELSE @@ -280,26 +261,27 @@ BEGIN FROM tItemDiary WHERE shipped < vDated; - SELECT vDated shipped, - 0 alertLevel, - 0 stateName, - 0 origin, - '' reference, - 0 clientFk, + SELECT NULL `type`, + vDated shipped, + NULL alertLevel, + NULL stateName, + NULL origin, + NULL reference, + NULL clientFk, 'Inventario calculado', @a invalue, NULL `out`, @a balance, - 0 lastPreparedLineFk, - 0 isTicket, - 0 lineFk, - 0 isPicked, - 0 clientType, - 0 claimFk, - NULL `order`, - 0 orderFk + NULL lastPreparedLineFk, + NULL isTicket, + NULL lineFk, + NULL isPicked, + NULL clientType, + NULL claimFk, + NULL `order` UNION ALL - SELECT shipped, + SELECT `type`, + shipped, alertlevel, stateName, origin, @@ -314,8 +296,7 @@ BEGIN isPicked, clientType, claimFk, - `order`, - orderFk + `order` FROM tItemDiary WHERE shipped >= vDated; END IF; -- 2.40.1 From 6f0196d656fb8ffcdcfb20bc4979f9a23bc8a963 Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 5 Jul 2024 07:06:40 +0200 Subject: [PATCH 3/9] refs #6769 Changes --- db/routines/vn/procedures/item_getBalance.sql | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/db/routines/vn/procedures/item_getBalance.sql b/db/routines/vn/procedures/item_getBalance.sql index dc4e16789..c2e0bc0c1 100644 --- a/db/routines/vn/procedures/item_getBalance.sql +++ b/db/routines/vn/procedures/item_getBalance.sql @@ -6,13 +6,11 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getBalance`( ) BEGIN /** - * Calcula el disponible. + * Calcula el balance de un artículo. * * @vItemFk Id de artículo * @vWarehouseFk Id de almacén - * @vDated Fecha - * Si la fecha es NULL, muestra el histórico desde el inventario. - * Si la fecha no es NULL, muestra histórico desde la fecha de vDated. + * @vDated Fecha a calcular, si es NULL muestra el histórico desde el inventario */ DECLARE vDateInventory DATETIME; DECLARE vLifeScope DATE; @@ -64,7 +62,8 @@ BEGIN CREATE OR REPLACE TEMPORARY TABLE tItemDiary ENGINE = MEMORY WITH entriesIn AS ( - SELECT 'entry' `type`, + SELECT 'entry' originType, + e.id origin, tr.landed shipped, b.quantity `in`, NULL `out`, @@ -72,8 +71,8 @@ BEGIN st.name stateName, s.name `name`, e.invoiceNumber reference, - e.id origin, - s.id clientFk, + 'supplier' entityType, + s.id entity, IF(st.`code` = 'DELIVERED', TRUE, FALSE) isPicked, FALSE isTicket, b.id lineFk, @@ -98,6 +97,7 @@ BEGIN ), entriesOut AS ( SELECT 'entry', + e.id originType, tr.shipped, NULL, b.quantity, @@ -105,8 +105,8 @@ BEGIN st.name stateName, s.name, e.invoiceNumber, - e.id entryFk, - s.id supplierFk, + 'supplier' entityType, + s.id entity, IF(st.`code` = 'DELIVERED' , TRUE, FALSE), FALSE isTicket, b.id, @@ -123,7 +123,6 @@ BEGIN OR (tr.shipped = util.VN_CURDATE() AND tr.isReceived), 'DELIVERED', 'FREE') - JOIN vn.entryConfig ec WHERE tr.shipped >= vDateInventory AND vWarehouseFk = tr.warehouseOutFk AND (s.id <> vSupplierInventoryFk OR vDated IS NULL) @@ -141,7 +140,8 @@ BEGIN t.nickname, t.refFk, t.id ticketFk, - t.clientFk, + 'client' entityType, + t.clientFk entity, s.id saleFk, st.`order`, c.typeFk, @@ -162,6 +162,7 @@ BEGIN AND vWarehouseFk = t.warehouseFk ) SELECT 'ticket', + s.ticketFk, s.shipped, NULL `in`, s.quantity, @@ -169,8 +170,8 @@ BEGIN s.name, s.nickname, s.refFk, - s.ticketFk, - s.clientFk, + s.entityType, + s.entity, IF(stk.saleFk, TRUE, FALSE), TRUE, s.saleFk, @@ -185,7 +186,8 @@ BEGIN GROUP BY s.saleFk ), orders AS ( - SELECT 'order' `type`, + SELECT 'order' originType, + o.id origin, r.shipment, NULL 'in', r.amount, @@ -193,7 +195,7 @@ BEGIN NULL stateName, c.name, NULL invoiceNumber, - o.id origin, + 'client' entityType, c.id, FALSE, FALSE isTicket, @@ -220,7 +222,7 @@ BEGIN UNION ALL SELECT * FROM orders ORDER BY shipped, - (inventorySupplierFk = clientFk) DESC, + (inventorySupplierFk = entity) DESC, alertLevel DESC, isTicket, `order` DESC, @@ -233,13 +235,14 @@ BEGIN SET @currentLineFk := 0; SET @shipped := ''; - SELECT t.type, + SELECT t.originType, + t.origin, DATE(@shipped:= t.shipped) shipped, t.alertLevel, t.stateName, - t.origin, t.reference, - t.clientFk, + t.entityType, + t.entity, t.name, t.`in` invalue, t.`out`, @@ -261,13 +264,14 @@ BEGIN FROM tItemDiary WHERE shipped < vDated; - SELECT NULL `type`, + SELECT NULL originType, + NULL origin, vDated shipped, NULL alertLevel, NULL stateName, - NULL origin, NULL reference, - NULL clientFk, + NULL entityType, + NULL entity, 'Inventario calculado', @a invalue, NULL `out`, @@ -280,13 +284,14 @@ BEGIN NULL claimFk, NULL `order` UNION ALL - SELECT `type`, + SELECT originType, + origin, shipped, alertlevel, stateName, - origin, - reference, - clientFk, + reference, + entityType, + entity, name, `in`, `out`, @a := @a + IFNULL(`in`, 0) - IFNULL(`out`, 0), -- 2.40.1 From 50c9ded103a66f7b667f6ec0f4da4ccfe71e5a86 Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 5 Jul 2024 08:38:40 +0200 Subject: [PATCH 4/9] refs #6769 Fix --- db/routines/vn/procedures/item_getBalance.sql | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/db/routines/vn/procedures/item_getBalance.sql b/db/routines/vn/procedures/item_getBalance.sql index c2e0bc0c1..a12397aa6 100644 --- a/db/routines/vn/procedures/item_getBalance.sql +++ b/db/routines/vn/procedures/item_getBalance.sql @@ -34,7 +34,8 @@ BEGIN END IF; -- Calcula el ultimo dia de vida para cada producto - CREATE OR REPLACE TEMPORARY TABLE tItemRange + -- Tiene tmp a propósito, porque si no falla al utilizarlo en el WITH + CREATE OR REPLACE TEMPORARY TABLE tmp.itemRange (PRIMARY KEY (itemFk)) ENGINE = MEMORY SELECT i.id itemFk, @@ -206,7 +207,7 @@ BEGIN NULL FROM hedera.orderRow r JOIN hedera.`order` o ON o.id = r.orderFk - JOIN tItemRange ir ON ir.itemFk = r.itemFk + JOIN tmp.itemRange ir ON ir.itemFk = r.itemFk JOIN vn.client c ON c.id = o.customer_id WHERE r.shipment >= vDateInventory AND (ir.ended IS NULL OR r.shipment <= ir.ended) @@ -308,6 +309,6 @@ BEGIN DROP TEMPORARY TABLE tItemDiary, - tItemRange; + tmp.itemRange; END$$ DELIMITER ; -- 2.40.1 From aa46d7a53659188933d6cc4a1307f6961f74f168 Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 5 Jul 2024 09:46:09 +0200 Subject: [PATCH 5/9] refs #6769 item_getBalance optimization --- db/routines/vn/procedures/item_getBalance.sql | 68 +++++++++++-------- .../11135-whiteErica/00-firstScript.sql | 1 + 2 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 db/versions/11135-whiteErica/00-firstScript.sql diff --git a/db/routines/vn/procedures/item_getBalance.sql b/db/routines/vn/procedures/item_getBalance.sql index a12397aa6..15be8c926 100644 --- a/db/routines/vn/procedures/item_getBalance.sql +++ b/db/routines/vn/procedures/item_getBalance.sql @@ -21,14 +21,14 @@ BEGIN FROM config c JOIN util.config uc; - SELECT COALESCE(vDated, vDateInventory) - INTERVAL MAX(life) DAY + SELECT vDateInventory - INTERVAL MAX(life) DAY INTO vLifeScope FROM itemType; SELECT warehouseOutFk, supplierFk INTO vWarehouseInventoryFk, vSupplierInventoryFk FROM inventoryConfig; - + IF NOT vWarehouseInventoryFk OR NOT vSupplierInventoryFk THEN CALL util.throw('Config variables are not set'); END IF; @@ -50,30 +50,32 @@ BEGIN JOIN warehouse w ON w.id = t.warehouseInFk JOIN item i ON i.id = b.itemFk JOIN itemType it ON it.id = i.typeFk - WHERE t.landed BETWEEN vLifeScope AND COALESCE(vDated, vDateInventory) + WHERE t.landed BETWEEN vLifeScope AND vDateInventory AND t.warehouseInFk = vWarehouseFk AND t.warehouseOutFk <> vWarehouseInventoryFk AND it.life + AND i.id = vItemFk AND NOT e.isExcludedFromAvailable GROUP BY b.itemFk ) c ON i.id = c.itemFk JOIN itemType it ON it.id = i.typeFk - HAVING ended >= COALESCE(vDated, vDateInventory) OR life IS NULL; + WHERE i.id = vItemFk + HAVING ended >= vDateInventory OR life IS NULL; CREATE OR REPLACE TEMPORARY TABLE tItemDiary ENGINE = MEMORY WITH entriesIn AS ( SELECT 'entry' originType, - e.id origin, + e.id originId, tr.landed shipped, b.quantity `in`, NULL `out`, st.alertLevel , st.name stateName, - s.name `name`, e.invoiceNumber reference, 'supplier' entityType, - s.id entity, + s.id entityId, + s.name entityName, IF(st.`code` = 'DELIVERED', TRUE, FALSE) isPicked, FALSE isTicket, b.id lineFk, @@ -104,10 +106,10 @@ BEGIN b.quantity, st.alertLevel, st.name stateName, - s.name, e.invoiceNumber, 'supplier' entityType, - s.id entity, + s.id entityId, + s.name, IF(st.`code` = 'DELIVERED' , TRUE, FALSE), FALSE isTicket, b.id, @@ -125,7 +127,7 @@ BEGIN 'DELIVERED', 'FREE') WHERE tr.shipped >= vDateInventory - AND vWarehouseFk = tr.warehouseOutFk + AND tr.warehouseOutFk = vWarehouseFk AND (s.id <> vSupplierInventoryFk OR vDated IS NULL) AND b.itemFk = vItemFk AND NOT e.isExcludedFromAvailable @@ -138,29 +140,30 @@ BEGIN s.quantity, st2.alertLevel, st2.name, - t.nickname, t.refFk, t.id ticketFk, 'client' entityType, - t.clientFk entity, + t.clientFk entityId, + t.nickname, s.id saleFk, st.`order`, c.typeFk, cb.claimFk FROM vn.sale s JOIN vn.ticket t ON t.id = s.ticketFk - LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id - LEFT JOIN vn.state st ON st.`code` = ts.`code` + LEFT JOIN vn.ticketLastState tls ON tls.ticketFk = t.id + LEFT JOIN vn.ticketTracking tt ON tt.id = tls.ticketTrackingFk + LEFT JOIN vn.state st ON st.id = tt.stateFk JOIN vn.client c ON c.id = t.clientFk JOIN vn.state st2 ON st2.`code` = IF(t.shipped < util.VN_CURDATE(), 'DELIVERED', IF (t.shipped > util.dayEnd(util.VN_CURDATE()), 'FREE', - IFNULL(ts.code, 'FREE'))) - LEFT JOIN vn.claimBeginning cb ON s.id = cb.saleFk + IFNULL(st.code, 'FREE'))) + LEFT JOIN vn.claimBeginning cb ON cb.saleFk = s.id WHERE t.shipped >= vDateInventory AND s.itemFk = vItemFk - AND vWarehouseFk = t.warehouseFk + AND t.warehouseFk = vWarehouseFk ) SELECT 'ticket', s.ticketFk, @@ -169,10 +172,10 @@ BEGIN s.quantity, s.alertLevel, s.name, - s.nickname, s.refFk, s.entityType, - s.entity, + s.entityId, + s.nickname, IF(stk.saleFk, TRUE, FALSE), TRUE, s.saleFk, @@ -188,16 +191,16 @@ BEGIN ), orders AS ( SELECT 'order' originType, - o.id origin, + o.id originId, r.shipment, NULL 'in', r.amount, NULL alertLevel, NULL stateName, - c.name, NULL invoiceNumber, 'client' entityType, c.id, + c.name, FALSE, FALSE isTicket, NULL buyFk, @@ -212,6 +215,10 @@ BEGIN WHERE r.shipment >= vDateInventory AND (ir.ended IS NULL OR r.shipment <= ir.ended) AND r.warehouseFk = vWarehouseFk + AND r.created >= ( + SELECT SUBTIME(util.VN_NOW(), reserveTime) + FROM hedera.orderConfig + ) AND NOT o.confirmed AND r.itemFk = vItemFk ) @@ -223,7 +230,7 @@ BEGIN UNION ALL SELECT * FROM orders ORDER BY shipped, - (inventorySupplierFk = entity) DESC, + (inventorySupplierFk = entityId) DESC, alertLevel DESC, isTicket, `order` DESC, @@ -237,14 +244,14 @@ BEGIN SET @shipped := ''; SELECT t.originType, - t.origin, + t.originId, DATE(@shipped:= t.shipped) shipped, t.alertLevel, t.stateName, t.reference, t.entityType, - t.entity, - t.name, + t.entityId, + t.entityName, t.`in` invalue, t.`out`, @a := @a + IFNULL(t.`in`, 0) - IFNULL(t.`out`, 0) balance, @@ -266,13 +273,13 @@ BEGIN WHERE shipped < vDated; SELECT NULL originType, - NULL origin, + NULL originId, vDated shipped, NULL alertLevel, NULL stateName, NULL reference, NULL entityType, - NULL entity, + NULL entityId, 'Inventario calculado', @a invalue, NULL `out`, @@ -286,14 +293,15 @@ BEGIN NULL `order` UNION ALL SELECT originType, - origin, + originId, shipped, alertlevel, stateName, reference, entityType, - entity, - name, `in`, + entityId, + entityName, + `in`, `out`, @a := @a + IFNULL(`in`, 0) - IFNULL(`out`, 0), 0, diff --git a/db/versions/11135-whiteErica/00-firstScript.sql b/db/versions/11135-whiteErica/00-firstScript.sql new file mode 100644 index 000000000..902608a3e --- /dev/null +++ b/db/versions/11135-whiteErica/00-firstScript.sql @@ -0,0 +1 @@ +CREATE INDEX travel_landed_IDX USING BTREE ON vn.travel (landed DESC,warehouseInFk,warehouseOutFk); -- 2.40.1 From dab386326f0d96e5b9ee2cb5a52e770ca5b425c0 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 11 Jul 2024 14:44:46 +0200 Subject: [PATCH 6/9] feat: refs #6769 Requested changes --- db/routines/vn/procedures/item_getBalance.sql | 35 +------------------ 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/db/routines/vn/procedures/item_getBalance.sql b/db/routines/vn/procedures/item_getBalance.sql index 15be8c926..6488676d1 100644 --- a/db/routines/vn/procedures/item_getBalance.sql +++ b/db/routines/vn/procedures/item_getBalance.sql @@ -33,35 +33,6 @@ BEGIN CALL util.throw('Config variables are not set'); END IF; - -- Calcula el ultimo dia de vida para cada producto - -- Tiene tmp a propósito, porque si no falla al utilizarlo en el WITH - CREATE OR REPLACE TEMPORARY TABLE tmp.itemRange - (PRIMARY KEY (itemFk)) - ENGINE = MEMORY - SELECT i.id itemFk, - util.dayEnd(c.maxLanded + INTERVAL it.life DAY) ended, - it.life - FROM item i - LEFT JOIN ( - SELECT b.itemFk, MAX(t.landed) maxLanded - FROM buy b - JOIN entry e ON b.entryFk = e.id - JOIN travel t ON t.id = e.travelFk - JOIN warehouse w ON w.id = t.warehouseInFk - JOIN item i ON i.id = b.itemFk - JOIN itemType it ON it.id = i.typeFk - WHERE t.landed BETWEEN vLifeScope AND vDateInventory - AND t.warehouseInFk = vWarehouseFk - AND t.warehouseOutFk <> vWarehouseInventoryFk - AND it.life - AND i.id = vItemFk - AND NOT e.isExcludedFromAvailable - GROUP BY b.itemFk - ) c ON i.id = c.itemFk - JOIN itemType it ON it.id = i.typeFk - WHERE i.id = vItemFk - HAVING ended >= vDateInventory OR life IS NULL; - CREATE OR REPLACE TEMPORARY TABLE tItemDiary ENGINE = MEMORY WITH entriesIn AS ( @@ -210,10 +181,8 @@ BEGIN NULL FROM hedera.orderRow r JOIN hedera.`order` o ON o.id = r.orderFk - JOIN tmp.itemRange ir ON ir.itemFk = r.itemFk JOIN vn.client c ON c.id = o.customer_id WHERE r.shipment >= vDateInventory - AND (ir.ended IS NULL OR r.shipment <= ir.ended) AND r.warehouseFk = vWarehouseFk AND r.created >= ( SELECT SUBTIME(util.VN_NOW(), reserveTime) @@ -315,8 +284,6 @@ BEGIN WHERE shipped >= vDated; END IF; - DROP TEMPORARY TABLE - tItemDiary, - tmp.itemRange; + DROP TEMPORARY TABLE tItemDiary; END$$ DELIMITER ; -- 2.40.1 From 28b6dc901fcd3c55e281258917a0d8d407cc85cb Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 12 Jul 2024 07:18:37 +0200 Subject: [PATCH 7/9] feat: refs #6769 Requested changes --- db/routines/vn/procedures/item_getBalance.sql | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/db/routines/vn/procedures/item_getBalance.sql b/db/routines/vn/procedures/item_getBalance.sql index 6488676d1..5c9007ae3 100644 --- a/db/routines/vn/procedures/item_getBalance.sql +++ b/db/routines/vn/procedures/item_getBalance.sql @@ -12,24 +12,16 @@ BEGIN * @vWarehouseFk Id de almacén * @vDated Fecha a calcular, si es NULL muestra el histórico desde el inventario */ - DECLARE vDateInventory DATETIME; - DECLARE vLifeScope DATE; - DECLARE vWarehouseInventoryFk INT; - DECLARE vSupplierInventoryFk INT; + DECLARE vDateInventory DATETIME + DEFAULT ( + SELECT IF(vDated, uc.mockUtcTime, c.inventoried) + FROM config c + JOIN util.config uc + ); + DECLARE vSupplierInventoryFk INT + DEFAULT (SELECT supplierFk FROM inventoryConfig); - SELECT IF(vDated, uc.mockUtcTime, c.inventoried) INTO vDateInventory - FROM config c - JOIN util.config uc; - - SELECT vDateInventory - INTERVAL MAX(life) DAY - INTO vLifeScope - FROM itemType; - - SELECT warehouseOutFk, supplierFk - INTO vWarehouseInventoryFk, vSupplierInventoryFk - FROM inventoryConfig; - - IF NOT vWarehouseInventoryFk OR NOT vSupplierInventoryFk THEN + IF NOT vSupplierInventoryFk OR NOT vDateInventory THEN CALL util.throw('Config variables are not set'); END IF; -- 2.40.1 From 31530d6d3da159646ff79c72b6683a67a36bc5ab Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 12 Jul 2024 07:22:22 +0200 Subject: [PATCH 8/9] feat: refs #6769 Fix tests --- modules/item/back/methods/item/specs/getBalance.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/item/back/methods/item/specs/getBalance.spec.js b/modules/item/back/methods/item/specs/getBalance.spec.js index bae0997bb..95de3cc50 100644 --- a/modules/item/back/methods/item/specs/getBalance.spec.js +++ b/modules/item/back/methods/item/specs/getBalance.spec.js @@ -61,7 +61,7 @@ describe('item getBalance()', () => { const secondItemBalance = await models.Item.getBalance(ctx, secondFilter, options); expect(firstItemBalance[9].claimFk).toEqual(null); - expect(secondItemBalance[7].claimFk).toEqual(2); + expect(secondItemBalance[7].claimFk).toEqual(1); await tx.rollback(); } catch (e) { -- 2.40.1 From 186cd4c7dd01d0105c556d9d04d5e3be2187912c Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 12 Jul 2024 09:29:33 +0200 Subject: [PATCH 9/9] refactor: refs #6769 Requested changes --- db/routines/vn/procedures/item_getBalance.sql | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/db/routines/vn/procedures/item_getBalance.sql b/db/routines/vn/procedures/item_getBalance.sql index 5c9007ae3..0de59b478 100644 --- a/db/routines/vn/procedures/item_getBalance.sql +++ b/db/routines/vn/procedures/item_getBalance.sql @@ -114,9 +114,8 @@ BEGIN cb.claimFk FROM vn.sale s JOIN vn.ticket t ON t.id = s.ticketFk - LEFT JOIN vn.ticketLastState tls ON tls.ticketFk = t.id - LEFT JOIN vn.ticketTracking tt ON tt.id = tls.ticketTrackingFk - LEFT JOIN vn.state st ON st.id = tt.stateFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + LEFT JOIN vn.state st ON st.code = ts.code JOIN vn.client c ON c.id = t.clientFk JOIN vn.state st2 ON st2.`code` = IF(t.shipped < util.VN_CURDATE(), 'DELIVERED', -- 2.40.1