salix/db/routines/bs/procedures/saleGraphic.sql

40 lines
1.5 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`saleGraphic`(IN vItemFk INT, IN vTypeFk INT, IN vCategoryFk INT, IN vFromDate DATE,
IN vToDate DATE, IN vProducerFk INT)
BEGIN
DECLARE vFromDateLastYear DATE;
DECLARE vToDateLastYear DATE;
DECLARE vFromDateTwoYearsAgo DATE;
DECLARE vToDateTwoYearsAgo DATE;
SET vItemFk = IFNULL(vItemFk,0);
SET vTypeFk = IFNULL(vTypeFk, 0);
SET vCategoryFk = IFNULL(vCategoryFk,0);
SET vFromDate = IFNULL(vFromDate,util.VN_CURDATE());
SET vToDate = IFNULL(vToDate,util.VN_CURDATE());
SET vProducerFk = IFNULL(vProducerFk,0);
SET vFromDateLastYear = TIMESTAMPADD(YEAR,-1,vFromDate);
SET vToDateLastYear = TIMESTAMPADD(YEAR,-1,vToDate);
SET vFromDateTwoYearsAgo = TIMESTAMPADD(YEAR,-2,vFromDate);
SET vToDateTwoYearsAgo = TIMESTAMPADD(YEAR,-2,vToDate);
SELECT s.quantity, CAST(v.importe AS DECIMAL(10,0)) importe, v.fecha
FROM bs.ventas v
JOIN vn.sale s ON s.id = v.Id_Movimiento
JOIN vn.item i ON i.id = s.itemFk
JOIN vn.itemType it ON it.id = i.typeFk
JOIN vn.itemCategory ic ON ic.id = it.categoryFk
WHERE vItemFk IN (i.id, 0)
AND vTypeFk IN (i.typeFk, 0)
AND vCategoryFk IN (it.categoryFk, 0)
AND vProducerFk IN (i.producerFk, 0)
AND (v.fecha BETWEEN vFromDate AND vToDate
OR v.fecha BETWEEN vFromDateLastYear AND vToDateLastYear
OR v.fecha BETWEEN vFromDateTwoYearsAgo AND vToDateTwoYearsAgo
);
END$$
DELIMITER ;