From f1dfd515e81784a6f68bb62073d91bf7521e1872 Mon Sep 17 00:00:00 2001 From: robert Date: Mon, 29 Jul 2024 13:26:52 +0200 Subject: [PATCH 1/5] feat: refs #7709 supplierPackaging_ReportSource --- .../supplierPackaging_ReportSource.sql | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql index 63285203b..1656b40de 100644 --- a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql +++ b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql @@ -1,7 +1,7 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`supplierPackaging_ReportSource`( vFromDated DATE, - vSupplierFk INT + vNif VARCHAR(50) ) BEGIN /** @@ -10,11 +10,23 @@ BEGIN * @param vFromDated Starting date * @param vSupplierFk Supplier ID */ + DECLARE vUser INT; + SET @vBalance = 0; SET @vItemFk = NULL; + SELECT id INTO vUser + FROM supplier + WHERE nif = vNif; + + IF vUser IS NULL THEN + SELECT id INTO vUser + FROM client + WHERE fi = vNif; + END IF; + CREATE OR REPLACE TEMPORARY TABLE tSupplierPackaging - SELECT supplierFk, + SELECT vUser supplierFk, itemFk, longName, supplier, @@ -31,7 +43,7 @@ BEGIN ) balance, @vItemFk := sub.itemFk previousItemFk FROM ( - SELECT sp.supplierFk, + SELECT vUser, sp.itemFk, sp.longName, sp.supplier, @@ -43,10 +55,11 @@ BEGIN sp.buyingValue FROM supplierPackaging sp JOIN entry e ON e.id = sp.entryFk - WHERE sp.supplierFk = vSupplierFk + JOIN supplier s ON s.id = sp.supplierFk + WHERE s.nif = vNif AND sp.landed >= vFromDated UNION ALL - SELECT vSupplierFk, + SELECT vUser, itemFk, longName, supplier, @@ -57,11 +70,12 @@ BEGIN NULL, AVG(buyingValue) FROM supplierPackaging - WHERE supplierFk = vSupplierFk + JOIN supplier s ON s.id = sp.supplierFk + WHERE s.nif = vNif AND landed < vFromDated GROUP BY itemFk UNION ALL - SELECT vSupplierFk, + SELECT vUser, s.itemFk, i.longName, c.name, @@ -76,12 +90,11 @@ BEGIN JOIN packaging p ON p.itemFk = i.id JOIN ticket t ON t.id = s.ticketFk JOIN client c ON c.id = t.clientFk - JOIN supplier su ON su.nif = c.fi - WHERE su.id = vSupplierFk + WHERE c.fi = vNif AND t.shipped >= vFromDated AND p.isPackageReturnable UNION ALL - SELECT vSupplierFk, + SELECT vUser, s.itemFk, i.longName, c.name, @@ -96,14 +109,13 @@ BEGIN JOIN packaging p ON p.itemFk = i.id JOIN ticket t ON t.id = s.ticketFk JOIN client c ON c.id = t.clientFk - JOIN supplier su ON su.nif = c.fi - WHERE su.id = vSupplierFk + WHERE c.fi = vNif AND t.shipped < vFromDated AND p.isPackageReturnable AND NOT t.isDeleted GROUP BY s.itemFk UNION ALL - SELECT vSupplierFk, + SELECT vUser, p.itemFk, i.longName, c.name, @@ -118,12 +130,11 @@ BEGIN JOIN item i ON i.id = p.itemFk JOIN ticket t ON t.id = tp.ticketFk JOIN client c ON c.id = t.clientFk - JOIN supplier su ON su.nif = c.fi - WHERE su.id = vSupplierFk + WHERE c.fi = vNif AND t.shipped >= vFromDated AND p.isPackageReturnable UNION ALL - SELECT vSupplierFk, + SELECT vUser, p.itemFk, i.longName, c.name, @@ -138,8 +149,7 @@ BEGIN JOIN item i ON i.id = p.itemFk JOIN ticket t ON t.id = tp.ticketFk JOIN client c ON c.id = t.clientFk - JOIN supplier su ON su.nif = c.fi - WHERE su.id = vSupplierFk + WHERE c.fi = vNif AND t.shipped < vFromDated AND p.isPackageReturnable AND NOT t.isDeleted -- 2.40.1 From 55fe9f2f8649de968e68ac7eab3eaf9cc7b03353 Mon Sep 17 00:00:00 2001 From: robert Date: Mon, 9 Sep 2024 08:16:04 +0200 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20refs=20#7709=20a=C3=B1adir=20alias?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/routines/vn/procedures/supplierPackaging_ReportSource.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql index a20905552..ab05e6aa0 100644 --- a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql +++ b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql @@ -69,7 +69,7 @@ BEGIN SUM(`out`), NULL, AVG(buyingValue) - FROM supplierPackaging + FROM supplierPackaging sp JOIN supplier s ON s.id = sp.supplierFk WHERE s.nif = vNif AND landed < vFromDated -- 2.40.1 From a99fed26901b986090e9659caa5711c9fc6d7ca5 Mon Sep 17 00:00:00 2001 From: robert Date: Mon, 14 Oct 2024 09:38:19 +0200 Subject: [PATCH 3/5] feat: refs #7709 supplierPackaging_ReportSource --- .../supplierPackaging_ReportSource.sql | 255 +++++++++--------- 1 file changed, 128 insertions(+), 127 deletions(-) diff --git a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql index ab05e6aa0..19b03ad3e 100644 --- a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql +++ b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql @@ -12,79 +12,30 @@ BEGIN */ DECLARE vUser INT; - SET @vBalance = 0; - SET @vItemFk = NULL; - SELECT id INTO vUser FROM supplier WHERE nif = vNif; - IF vUser IS NULL THEN - SELECT id INTO vUser - FROM client - WHERE fi = vNif; - END IF; - CREATE OR REPLACE TEMPORARY TABLE tSupplierPackaging - SELECT vUser supplierFk, - itemFk, + SELECT itemFk, longName, - supplier, + `name`, entryFk, landed, `in`, `out`, - sref, - buyingValue, - IF ( - NOT (@vItemFk <=> sub.itemFk), - @vBalance := (`in` - `out`), - @vBalance := (`in` - `out` + @vBalance) - ) balance, - @vItemFk := sub.itemFk previousItemFk + CAST(sref AS CHAR) sref, + buyingValue FROM ( - SELECT vUser, - sp.itemFk, - sp.longName, - sp.supplier, - CONCAT('E',sp.entryFk) entryFk, - sp.landed, - sp.`in`, - sp.`out`, - e.invoiceNumber sref, - sp.buyingValue - FROM supplierPackaging sp - JOIN entry e ON e.id = sp.entryFk - JOIN supplier s ON s.id = sp.supplierFk - WHERE s.nif = vNif - AND sp.landed >= vFromDated - UNION ALL - SELECT vUser, - itemFk, - longName, - supplier, - 'E previous', - vFromDated, - SUM(`in`), - SUM(`out`), - NULL, - AVG(buyingValue) - FROM supplierPackaging sp - JOIN supplier s ON s.id = sp.supplierFk - WHERE s.nif = vNif - AND landed < vFromDated - GROUP BY itemFk - UNION ALL - SELECT vUser, - s.itemFk, + SELECT s.itemFk, i.longName, c.name, - CONCAT('T',s.ticketFk), - DATE(t.shipped), + CONCAT('T',s.ticketFk) entryFk, + DATE(t.shipped) landed, -LEAST(s.quantity,0) `in`, GREATEST(s.quantity,0) `out`, - t.cmrFk, - s.price * (100 - s.discount) / 100 + t.cmrFk sref, + s.price * (100 - s.discount) / 100 buyingValue FROM sale s JOIN item i ON i.id = s.itemFk JOIN packaging p ON p.itemFk = i.id @@ -93,85 +44,135 @@ BEGIN WHERE c.fi = vNif AND t.shipped >= vFromDated AND p.isPackageReturnable - UNION ALL - SELECT vUser, - s.itemFk, - i.longName, - c.name, - 'T previous', - vFromDated, - SUM(-LEAST(s.quantity,0)) `in`, - SUM(GREATEST(s.quantity,0)) `out`, - NULL, - AVG(s.price * (100 - s.discount) / 100) - FROM sale s - JOIN item i ON i.id = s.itemFk - JOIN packaging p ON p.itemFk = i.id - JOIN ticket t ON t.id = s.ticketFk - JOIN client c ON c.id = t.clientFk - WHERE c.fi = vNif - AND t.shipped < vFromDated - AND p.isPackageReturnable - AND NOT t.isDeleted - GROUP BY s.itemFk - UNION ALL - SELECT vUser, - p.itemFk, - i.longName, - c.name, - CONCAT('TP',tp.ticketFk), - DATE(t.shipped), - -LEAST(tp.quantity,0) `in`, - GREATEST(tp.quantity,0) `out`, - t.cmrFk, - 0 - FROM ticketPackaging tp - JOIN packaging p ON p.id = tp.packagingFk - JOIN item i ON i.id = p.itemFk - JOIN ticket t ON t.id = tp.ticketFk - JOIN client c ON c.id = t.clientFk - WHERE c.fi = vNif - AND t.shipped >= vFromDated - AND p.isPackageReturnable - UNION ALL - SELECT vUser, - p.itemFk, - i.longName, - c.name, - 'TP previous', - vFromDated, - SUM(-LEAST(tp.quantity,0)) `in`, - SUM(GREATEST(tp.quantity,0)) `out`, - NULL, - 0 - FROM ticketPackaging tp - JOIN packaging p ON p.id = tp.packagingFk - JOIN item i ON i.id = p.itemFk - JOIN ticket t ON t.id = tp.ticketFk - JOIN client c ON c.id = t.clientFk - WHERE c.fi = vNif - AND t.shipped < vFromDated - AND p.isPackageReturnable - AND NOT t.isDeleted - GROUP BY p.itemFk - ORDER BY itemFk, landed, entryFk - ) sub - WHERE `out` OR `in`; + UNION ALL + SELECT s.itemFk, + i.longName, + c.name, + 'T previous', + vFromDated, + SUM(-LEAST(s.quantity,0)) `in`, + SUM(GREATEST(s.quantity,0)) `out`, + NULL, + AVG(s.price * (100 - s.discount) / 100) + FROM sale s + JOIN item i ON i.id = s.itemFk + JOIN packaging p ON p.itemFk = i.id + JOIN ticket t ON t.id = s.ticketFk + JOIN client c ON c.id = t.clientFk + WHERE c.fi = vNif + AND t.shipped < vFromDated + AND p.isPackageReturnable + AND NOT t.isDeleted + GROUP BY s.itemFk + UNION ALL + SELECT p.itemFk, + i.longName, + c.name, + CONCAT('TP',tp.ticketFk) entryFk, + DATE(t.shipped), + -LEAST(tp.quantity,0) `in`, + GREATEST(tp.quantity,0) `out`, + t.cmrFk, + 0 + FROM ticketPackaging tp + JOIN packaging p ON p.id = tp.packagingFk + JOIN item i ON i.id = p.itemFk + JOIN ticket t ON t.id = tp.ticketFk + JOIN client c ON c.id = t.clientFk + WHERE c.fi = vNif + AND t.shipped >= vFromDated + AND p.isPackageReturnable + UNION ALL + SELECT p.itemFk, + i.longName, + c.name, + 'TP previous', + vFromDated, + SUM(-LEAST(tp.quantity,0)) `in`, + SUM(GREATEST(tp.quantity,0)) `out`, + NULL, + 0 + FROM ticketPackaging tp + JOIN packaging p ON p.id = tp.packagingFk + JOIN item i ON i.id = p.itemFk + JOIN ticket t ON t.id = tp.ticketFk + JOIN client c ON c.id = t.clientFk + WHERE c.fi = vNif + AND t.shipped < vFromDated + AND p.isPackageReturnable + AND NOT t.isDeleted + GROUP BY p.itemFk + ORDER BY itemFk , landed, entryFk + )sub + WHERE (`out` OR `in`); - SELECT supplierFk, + IF vUser IS NULL THEN + + SELECT id INTO vUser + FROM client + WHERE fi = vNif; + + INSERT INTO tSupplierPackaging + SELECT itemFk, + longName, + `name`, + entryFk, + landed, + `in`, + `out`, + sref, + buyingValue + FROM ( + SELECT sp.itemFk, + sp.longName, + sp.supplier name, + CONCAT('E',sp.entryFk) entryFk, + sp.landed, + sp.`in`, + sp.`out`, + e.invoiceNumber sref, + sp.buyingValue + FROM supplierPackaging sp + JOIN entry e ON e.id = sp.entryFk + JOIN supplier s ON s.id = sp.supplierFk + WHERE s.nif = vNif + AND sp.landed >= vFromDated + UNION ALL + SELECT itemFk, + longName, + supplier, + 'E previous', + vFromDated, + IF(SUM(`in`) + SUM(-`out`)>0, SUM(`in`) + SUM(-`out`), 0) `in`, + -IF(SUM(`in`) + SUM(-`out`)<0, SUM(`in`) + SUM(-`out`), 0) `out`, + NULL, + AVG(buyingValue) + FROM supplierPackaging sp + JOIN supplier s ON s.id = sp.supplierFk + WHERE s.nif = vNif + AND landed < vFromDated + GROUP BY itemFk + ORDER BY itemFk , landed, entryFk + ) sub + WHERE (`out` OR `in`); + + END IF; + + SELECT vUser, itemFk, longName, - supplier, + name, entryFk, landed, CAST(`in` AS DECIMAL(10,0)) `in`, CAST(`out` AS DECIMAL(10,0)) `out`, sref, buyingValue, - balance + CAST(SUM(IF(`in`=0, -`out`, `in`)) OVER (PARTITION BY itemFk ORDER BY itemFk , landed, entryFk) AS DECIMAL(10,2)) balance FROM tSupplierPackaging - WHERE NOT (NOT balance AND landed < CURDATE() - INTERVAL 1 YEAR); + WHERE NOT (landed < CURDATE() - INTERVAL 1 YEAR) + ORDER BY itemFk, landed, entryFk; DROP TEMPORARY TABLE tSupplierPackaging; END$$ -DELIMITER ; +DELIMITER ; \ No newline at end of file -- 2.40.1 From 980b7aa2cc65cd8460bc32eea692a8bc4abafc3b Mon Sep 17 00:00:00 2001 From: robert Date: Fri, 18 Oct 2024 07:49:13 +0200 Subject: [PATCH 4/5] feat: refs #7709 cambios solicitados --- .../supplierPackaging_ReportSource.sql | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql index 19b03ad3e..aa50a2d81 100644 --- a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql +++ b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql @@ -5,14 +5,14 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`supplierPackaging_Rep ) BEGIN /** -* Create a report with packaging balance -* -* @param vFromDated Starting date -* @param vSupplierFk Supplier ID -*/ - DECLARE vUser INT; + * Create a report with packaging balance + * + * @param vFromDated Starting date + * @param vSupplierFk Supplier ID + */ + DECLARE vPerson INT; - SELECT id INTO vUser + SELECT id INTO vPerson FROM supplier WHERE nif = vNif; @@ -104,11 +104,11 @@ BEGIN GROUP BY p.itemFk ORDER BY itemFk , landed, entryFk )sub - WHERE (`out` OR `in`); + WHERE `out` OR `in`; - IF vUser IS NULL THEN + IF vPerson IS NULL THEN - SELECT id INTO vUser + SELECT id INTO vPerson FROM client WHERE fi = vNif; @@ -154,11 +154,11 @@ BEGIN GROUP BY itemFk ORDER BY itemFk , landed, entryFk ) sub - WHERE (`out` OR `in`); + WHERE `out` OR `in`; END IF; - SELECT vUser, + SELECT vPerson, itemFk, longName, name, @@ -170,7 +170,7 @@ BEGIN buyingValue, CAST(SUM(IF(`in`=0, -`out`, `in`)) OVER (PARTITION BY itemFk ORDER BY itemFk , landed, entryFk) AS DECIMAL(10,2)) balance FROM tSupplierPackaging - WHERE NOT (landed < CURDATE() - INTERVAL 1 YEAR) + WHERE NOT (landed < util.VN_CURDATE() - INTERVAL 1 YEAR) ORDER BY itemFk, landed, entryFk; DROP TEMPORARY TABLE tSupplierPackaging; -- 2.40.1 From e4cd290027f7fe1f0f4d0ee9dca720612212e76a Mon Sep 17 00:00:00 2001 From: robert Date: Fri, 18 Oct 2024 10:34:53 +0200 Subject: [PATCH 5/5] feat: refs #7709 modifcar vPerson por vEntityId --- .../vn/procedures/supplierPackaging_ReportSource.sql | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql index aa50a2d81..04d0e0f29 100644 --- a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql +++ b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql @@ -10,9 +10,9 @@ BEGIN * @param vFromDated Starting date * @param vSupplierFk Supplier ID */ - DECLARE vPerson INT; + DECLARE vEntityId INT; - SELECT id INTO vPerson + SELECT id INTO vEntityId FROM supplier WHERE nif = vNif; @@ -106,9 +106,9 @@ BEGIN )sub WHERE `out` OR `in`; - IF vPerson IS NULL THEN + IF vEntityId IS NULL THEN - SELECT id INTO vPerson + SELECT id INTO vEntityId FROM client WHERE fi = vNif; @@ -158,7 +158,7 @@ BEGIN END IF; - SELECT vPerson, + SELECT vEntityId, itemFk, longName, name, -- 2.40.1