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