feat: refs #7709 supplierPackaging_ReportSource
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Robert Ferrús 2024-10-14 09:38:19 +02:00
parent 85cf301e48
commit a99fed2690
1 changed files with 128 additions and 127 deletions

View File

@ -12,79 +12,30 @@ BEGIN
*/ */
DECLARE vUser INT; DECLARE vUser INT;
SET @vBalance = 0;
SET @vItemFk = NULL;
SELECT id INTO vUser SELECT id INTO vUser
FROM supplier FROM supplier
WHERE nif = vNif; 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 CREATE OR REPLACE TEMPORARY TABLE tSupplierPackaging
SELECT vUser supplierFk, SELECT itemFk,
itemFk,
longName, longName,
supplier, `name`,
entryFk, entryFk,
landed, landed,
`in`, `in`,
`out`, `out`,
sref, CAST(sref AS CHAR) sref,
buyingValue, buyingValue
IF (
NOT (@vItemFk <=> sub.itemFk),
@vBalance := (`in` - `out`),
@vBalance := (`in` - `out` + @vBalance)
) balance,
@vItemFk := sub.itemFk previousItemFk
FROM ( FROM (
SELECT vUser, SELECT s.itemFk,
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,
i.longName, i.longName,
c.name, c.name,
CONCAT('T',s.ticketFk), CONCAT('T',s.ticketFk) entryFk,
DATE(t.shipped), DATE(t.shipped) landed,
-LEAST(s.quantity,0) `in`, -LEAST(s.quantity,0) `in`,
GREATEST(s.quantity,0) `out`, GREATEST(s.quantity,0) `out`,
t.cmrFk, t.cmrFk sref,
s.price * (100 - s.discount) / 100 s.price * (100 - s.discount) / 100 buyingValue
FROM sale s FROM sale s
JOIN item i ON i.id = s.itemFk JOIN item i ON i.id = s.itemFk
JOIN packaging p ON p.itemFk = i.id JOIN packaging p ON p.itemFk = i.id
@ -93,85 +44,135 @@ BEGIN
WHERE c.fi = vNif WHERE c.fi = vNif
AND t.shipped >= vFromDated AND t.shipped >= vFromDated
AND p.isPackageReturnable AND p.isPackageReturnable
UNION ALL UNION ALL
SELECT vUser, SELECT s.itemFk,
s.itemFk, i.longName,
i.longName, c.name,
c.name, 'T previous',
'T previous', vFromDated,
vFromDated, SUM(-LEAST(s.quantity,0)) `in`,
SUM(-LEAST(s.quantity,0)) `in`, SUM(GREATEST(s.quantity,0)) `out`,
SUM(GREATEST(s.quantity,0)) `out`, NULL,
NULL, AVG(s.price * (100 - s.discount) / 100)
AVG(s.price * (100 - s.discount) / 100) FROM sale s
FROM sale s JOIN item i ON i.id = s.itemFk
JOIN item i ON i.id = s.itemFk JOIN packaging p ON p.itemFk = i.id
JOIN packaging p ON p.itemFk = i.id JOIN ticket t ON t.id = s.ticketFk
JOIN ticket t ON t.id = s.ticketFk JOIN client c ON c.id = t.clientFk
JOIN client c ON c.id = t.clientFk WHERE c.fi = vNif
WHERE c.fi = vNif AND t.shipped < vFromDated
AND t.shipped < vFromDated AND p.isPackageReturnable
AND p.isPackageReturnable AND NOT t.isDeleted
AND NOT t.isDeleted GROUP BY s.itemFk
GROUP BY s.itemFk UNION ALL
UNION ALL SELECT p.itemFk,
SELECT vUser, i.longName,
p.itemFk, c.name,
i.longName, CONCAT('TP',tp.ticketFk) entryFk,
c.name, DATE(t.shipped),
CONCAT('TP',tp.ticketFk), -LEAST(tp.quantity,0) `in`,
DATE(t.shipped), GREATEST(tp.quantity,0) `out`,
-LEAST(tp.quantity,0) `in`, t.cmrFk,
GREATEST(tp.quantity,0) `out`, 0
t.cmrFk, FROM ticketPackaging tp
0 JOIN packaging p ON p.id = tp.packagingFk
FROM ticketPackaging tp JOIN item i ON i.id = p.itemFk
JOIN packaging p ON p.id = tp.packagingFk JOIN ticket t ON t.id = tp.ticketFk
JOIN item i ON i.id = p.itemFk JOIN client c ON c.id = t.clientFk
JOIN ticket t ON t.id = tp.ticketFk WHERE c.fi = vNif
JOIN client c ON c.id = t.clientFk AND t.shipped >= vFromDated
WHERE c.fi = vNif AND p.isPackageReturnable
AND t.shipped >= vFromDated UNION ALL
AND p.isPackageReturnable SELECT p.itemFk,
UNION ALL i.longName,
SELECT vUser, c.name,
p.itemFk, 'TP previous',
i.longName, vFromDated,
c.name, SUM(-LEAST(tp.quantity,0)) `in`,
'TP previous', SUM(GREATEST(tp.quantity,0)) `out`,
vFromDated, NULL,
SUM(-LEAST(tp.quantity,0)) `in`, 0
SUM(GREATEST(tp.quantity,0)) `out`, FROM ticketPackaging tp
NULL, JOIN packaging p ON p.id = tp.packagingFk
0 JOIN item i ON i.id = p.itemFk
FROM ticketPackaging tp JOIN ticket t ON t.id = tp.ticketFk
JOIN packaging p ON p.id = tp.packagingFk JOIN client c ON c.id = t.clientFk
JOIN item i ON i.id = p.itemFk WHERE c.fi = vNif
JOIN ticket t ON t.id = tp.ticketFk AND t.shipped < vFromDated
JOIN client c ON c.id = t.clientFk AND p.isPackageReturnable
WHERE c.fi = vNif AND NOT t.isDeleted
AND t.shipped < vFromDated GROUP BY p.itemFk
AND p.isPackageReturnable ORDER BY itemFk , landed, entryFk
AND NOT t.isDeleted )sub
GROUP BY p.itemFk WHERE (`out` OR `in`);
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, itemFk,
longName, longName,
supplier, name,
entryFk, entryFk,
landed, landed,
CAST(`in` AS DECIMAL(10,0)) `in`, CAST(`in` AS DECIMAL(10,0)) `in`,
CAST(`out` AS DECIMAL(10,0)) `out`, CAST(`out` AS DECIMAL(10,0)) `out`,
sref, sref,
buyingValue, 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 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; DROP TEMPORARY TABLE tSupplierPackaging;
END$$ END$$
DELIMITER ; DELIMITER ;