salix/db/routines/vn/procedures/supplierPackaging_ReportSou...

83 lines
1.6 KiB
MySQL
Raw Normal View History

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,
entryFk,
landed,
`in`,
`out`,
warehouse,
buyingValue
FROM supplierPackaging
WHERE supplierFk = vSupplierFk
AND landed >= vFromDated
UNION ALL
SELECT vSupplierFk,
itemFk,
longName,
supplier,
'previous',
vFromDated,
SUM(`in`),
SUM(`out`),
NULL,
buyingValue
FROM supplierPackaging
WHERE supplierFk = vSupplierFk
AND landed < vFromDated
GROUP BY itemFk
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 ;