2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`supplierPackaging_ReportSource`(
|
|
|
|
vFromDated DATE,
|
|
|
|
vSupplierFk INT
|
|
|
|
)
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Selecciona los embalajes de un proveedor a partir de una fecha
|
|
|
|
*
|
|
|
|
* @param vFromDated Fecha de la que partir
|
|
|
|
* @param vSupplierFk Id del proveedor
|
|
|
|
*/
|
|
|
|
SET @vBalance = 0;
|
|
|
|
SET @vItemFk = NULL;
|
|
|
|
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tSupplierPackaging
|
|
|
|
SELECT supplierFk,
|
|
|
|
itemFk,
|
|
|
|
longName,
|
|
|
|
supplier,
|
|
|
|
entryFk,
|
|
|
|
landed,
|
|
|
|
`in`,
|
|
|
|
`out`,
|
|
|
|
warehouse,
|
|
|
|
buyingValue,
|
|
|
|
IF (
|
|
|
|
NOT (@vItemFk <=> sub.itemFk),
|
|
|
|
@vBalance := (`in` - `out`),
|
|
|
|
@vBalance := (`in` - `out` + @vBalance)
|
|
|
|
) balance,
|
|
|
|
@vItemFk := sub.itemFk previousItemFk
|
|
|
|
FROM (
|
|
|
|
SELECT supplierFk,
|
|
|
|
itemFk,
|
|
|
|
longName,
|
|
|
|
supplier,
|
2024-05-20 06:21:54 +00:00
|
|
|
CONCAT('E',entryFk) entryFk,
|
2024-01-15 11:31:03 +00:00
|
|
|
landed,
|
|
|
|
`in`,
|
|
|
|
`out`,
|
|
|
|
warehouse,
|
|
|
|
buyingValue
|
|
|
|
FROM supplierPackaging
|
|
|
|
WHERE supplierFk = vSupplierFk
|
|
|
|
AND landed >= vFromDated
|
|
|
|
UNION ALL
|
|
|
|
SELECT vSupplierFk,
|
|
|
|
itemFk,
|
|
|
|
longName,
|
|
|
|
supplier,
|
2024-05-20 06:21:54 +00:00
|
|
|
'E previous',
|
2024-01-15 11:31:03 +00:00
|
|
|
vFromDated,
|
|
|
|
SUM(`in`),
|
|
|
|
SUM(`out`),
|
|
|
|
NULL,
|
2024-05-20 06:21:54 +00:00
|
|
|
AVG(buyingValue)
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM supplierPackaging
|
|
|
|
WHERE supplierFk = vSupplierFk
|
|
|
|
AND landed < vFromDated
|
|
|
|
GROUP BY itemFk
|
2024-05-20 06:21:54 +00:00
|
|
|
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
|
2024-01-15 11:31:03 +00:00
|
|
|
ORDER BY itemFk, landed, entryFk
|
|
|
|
) sub
|
|
|
|
WHERE `out` OR `in`;
|
|
|
|
|
|
|
|
SELECT supplierFk,
|
|
|
|
itemFk,
|
|
|
|
longName,
|
|
|
|
supplier,
|
|
|
|
entryFk,
|
|
|
|
landed,
|
|
|
|
`in`,
|
|
|
|
`out`,
|
|
|
|
warehouse,
|
|
|
|
buyingValue,
|
|
|
|
balance
|
|
|
|
FROM tSupplierPackaging
|
|
|
|
WHERE NOT (NOT balance AND landed < CURDATE() - INTERVAL 1 YEAR);
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tSupplierPackaging;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|