#7709 -supplierPackaging_ReportSource_2 #3107
|
@ -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
|
||||||
carlosap marked this conversation as resolved
|
|||||||
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
|
||||||
|
@ -94,8 +45,7 @@ BEGIN
|
||||||
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',
|
||||||
|
@ -115,11 +65,10 @@ BEGIN
|
||||||
AND NOT t.isDeleted
|
AND NOT t.isDeleted
|
||||||
GROUP BY s.itemFk
|
GROUP BY s.itemFk
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT vUser,
|
SELECT p.itemFk,
|
||||||
p.itemFk,
|
|
||||||
i.longName,
|
i.longName,
|
||||||
c.name,
|
c.name,
|
||||||
CONCAT('TP',tp.ticketFk),
|
CONCAT('TP',tp.ticketFk) entryFk,
|
||||||
DATE(t.shipped),
|
DATE(t.shipped),
|
||||||
-LEAST(tp.quantity,0) `in`,
|
-LEAST(tp.quantity,0) `in`,
|
||||||
GREATEST(tp.quantity,0) `out`,
|
GREATEST(tp.quantity,0) `out`,
|
||||||
|
@ -134,8 +83,7 @@ BEGIN
|
||||||
AND t.shipped >= vFromDated
|
AND t.shipped >= vFromDated
|
||||||
AND p.isPackageReturnable
|
AND p.isPackageReturnable
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT vUser,
|
SELECT p.itemFk,
|
||||||
p.itemFk,
|
|
||||||
i.longName,
|
i.longName,
|
||||||
c.name,
|
c.name,
|
||||||
'TP previous',
|
'TP previous',
|
||||||
|
@ -154,23 +102,76 @@ BEGIN
|
||||||
AND p.isPackageReturnable
|
AND p.isPackageReturnable
|
||||||
AND NOT t.isDeleted
|
AND NOT t.isDeleted
|
||||||
GROUP BY p.itemFk
|
GROUP BY p.itemFk
|
||||||
ORDER BY itemFk, landed, entryFk
|
ORDER BY itemFk , landed, entryFk
|
||||||
) sub
|
)sub
|
||||||
WHERE `out` OR `in`;
|
WHERE (`out` OR `in`);
|
||||||
|
|
||||||
SELECT supplierFk,
|
IF vUser IS NULL THEN
|
||||||
itemFk,
|
|
||||||
|
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,
|
longName,
|
||||||
supplier,
|
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,
|
||||||
|
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$$
|
||||||
|
|
Loading…
Reference in New Issue
hay una columna virtual para este valor, es sale
la columna total de sale el calculo que hace es el siguiente :
(
price
*quantity
* (100 -discount
) / 100)