diff --git a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql index f9d43f925..7bba46e7e 100644 --- a/db/routines/vn/procedures/supplierPackaging_ReportSource.sql +++ b/db/routines/vn/procedures/supplierPackaging_ReportSource.sql @@ -10,9 +10,15 @@ BEGIN * @param vFromDated Fecha de la que partir * @param vSupplierFk Id del proveedor */ + DECLARE vClientfk INTEGER; SET @vBalance = 0; SET @vItemFk = NULL; + SELECT c.id INTO vClientFk + FROM supplier s + JOIN client c ON c.fi = s.nif + WHERE s.id = vSupplierFk; + CREATE OR REPLACE TEMPORARY TABLE tSupplierPackaging SELECT supplierFk, itemFk, @@ -45,6 +51,46 @@ BEGIN WHERE supplierFk = vSupplierFk AND landed >= vFromDated UNION ALL + SELECT vSupplierFk, + p.itemFk, + i.name, + c.name, + t.id, + t.landed, + 0, + SUM(tp.quantity), + 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.id = vClientFk + AND t.landed >= vFromDated + AND tp.quantity > 0 + AND p.itemFk + UNION ALL + SELECT vSupplierFk, + p.itemFk, + i.name, + c.name, + t.id, + t.landed, + SUM(tp.quantity), + 0, + 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.id = vClientFk + AND t.landed >= vFromDated + AND tp.quantity < 0 + AND p.itemFk + UNION ALL SELECT vSupplierFk, itemFk, longName, @@ -59,7 +105,8 @@ BEGIN WHERE supplierFk = vSupplierFk AND landed < vFromDated GROUP BY itemFk - ORDER BY itemFk, landed, entryFk + ORDER BY itemFk, landed, entryFk DESC + LIMIT 10000000000000000000 ) sub WHERE `out` OR `in`; diff --git a/db/routines/vn/procedures/ticketPackaging_add.sql b/db/routines/vn/procedures/ticketPackaging_add.sql index d669b95f5..a274a68ab 100644 --- a/db/routines/vn/procedures/ticketPackaging_add.sql +++ b/db/routines/vn/procedures/ticketPackaging_add.sql @@ -64,16 +64,15 @@ BEGIN SET vGraceDate = vGraceDate -INTERVAL 1 MONTH; END IF; - DROP TEMPORARY TABLE IF EXISTS tmp.packagingToInvoice; - CREATE TEMPORARY TABLE tmp.packagingToInvoice + CREATE OR REPLACE TEMPORARY TABLE tmp.packagingToInvoice (INDEX (clientFk)) ENGINE = MEMORY SELECT p.itemFk, tp.packagingFk, tp.quantity, - tp.ticketFk, p.price, - t.clientFk + t.clientFk, + NULL FROM ticketPackaging tp JOIN packaging p ON p.id = tp.packagingFk JOIN ticket t ON t.id = tp.ticketFk @@ -83,7 +82,25 @@ BEGIN AND t.shipped BETWEEN vDateStart AND vDateEnd AND (tp.quantity < 0 OR (tp.quantity > 0 AND t.shipped < vGraceDate)) AND tp.quantity - AND p.itemFk; + AND p.itemFk + UNION ALL + (SELECT i.id, + p.id, + SUM(`in`) - SUM(`out`) quantity, + p.price, + c.id, + landed + FROM supplierPackaging sp + JOIN supplier s ON s.id = sp.supplierFk + JOIN client c ON c.fi = s.nif + JOIN item i ON i.id = sp.itemFk + JOIN packaging p ON p.itemFk = i.id + WHERE (vClientFk IS NULL OR c.id = vClientFk) + AND landed BETWEEN vDateStart AND vDateEnd + AND p.itemFk + GROUP BY c.id, i.id, landed + HAVING (quantity < 0 OR (quantity > 0 AND landed < vGraceDate)) + AND quantity); OPEN vCursor; l: LOOP