improved proc
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Pako Natek 2024-05-20 08:21:54 +02:00
parent 9d98ae4a03
commit 9a5acedd38
1 changed files with 85 additions and 3 deletions

View File

@ -35,7 +35,7 @@ BEGIN
itemFk,
longName,
supplier,
entryFk,
CONCAT('E',entryFk) entryFk,
landed,
`in`,
`out`,
@ -49,16 +49,98 @@ BEGIN
itemFk,
longName,
supplier,
'previous',
'E previous',
vFromDated,
SUM(`in`),
SUM(`out`),
NULL,
buyingValue
AVG(buyingValue)
FROM supplierPackaging
WHERE supplierFk = vSupplierFk
AND landed < vFromDated
GROUP BY itemFk
UNION ALL
SELECT vSupplierFk,
s.itemFk,
i.longName,
c.name,
CONCAT('T',s.ticketFk),
DATE(t.shipped),
-LEAST(s.quantity,0) `in`,
GREATEST(s.quantity,0) `out`,
t.warehouseFk,
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
JOIN supplier su ON su.nif = c.fi
WHERE su.id = vSupplierFk
AND t.shipped >= vFromDated
AND p.isPackageReturnable
UNION ALL
SELECT vSupplierFk,
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
JOIN supplier su ON su.nif = c.fi
WHERE su.id = vSupplierFk
AND t.shipped < vFromDated
AND p.isPackageReturnable
GROUP BY s.itemFk
UNION ALL
SELECT vSupplierFk,
p.itemFk,
i.longName,
c.name,
CONCAT('TP',tp.ticketFk),
DATE(t.shipped),
-LEAST(tp.quantity,0) `in`,
GREATEST(tp.quantity,0) `out`,
t.warehouseFk,
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
JOIN supplier su ON su.nif = c.fi
WHERE su.id = vSupplierFk
AND t.shipped >= vFromDated
AND p.isPackageReturnable
UNION ALL
SELECT vSupplierFk,
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
JOIN supplier su ON su.nif = c.fi
WHERE su.id = vSupplierFk
AND t.shipped >= vFromDated
AND p.isPackageReturnable
GROUP BY p.itemFk
ORDER BY itemFk, landed, entryFk
) sub
WHERE `out` OR `in`;