salix/db/routines/vn2008/procedures/Rutas_Graph.sql

47 lines
2.6 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`Rutas_Graph`()
BEGIN
DECLARE datSTART DATE DEFAULT TIMESTAMPADD(DAY,-3,util.VN_CURDATE());
DECLARE datEND DATE;
SET datEND = util.dayEnd(timestampadd(DAY,3,util.VN_CURDATE()));
CALL vn.buyUltimate(NULL, datSTART);
SELECT CAST(SUM(Cantidad * vn.buy_getUnitVolume(buy_id)) / 1000000 AS DECIMAL(5,1)) AS m3, Agencia, w.name AS Almacen, DATE(R.Fecha) AS Fecha_Ruta
FROM vn2008.Rutas R
JOIN vn2008.Agencias a ON a.Id_Agencia = R.Id_Agencia
JOIN vn2008.Tickets t USING(Id_Ruta)
JOIN vn2008.warehouse w ON w.id = t.warehouse_id
JOIN vn2008.Movimientos m USING(Id_Ticket)
JOIN cache.last_buy b ON m.Id_Article = b.item_id AND t.warehouse_id = b.warehouse_id
WHERE R.Fecha BETWEEN datSTART AND datEND
GROUP BY Agencia, Almacen, Fecha_Ruta
UNION ALL
SELECT CAST(SUM(Cantidad * vn.buy_getUnitVolume(buy_id)) / 1000000 AS decimal(5,1)) AS m3 ,Agencia, w.name AS Almacen, timestampadd(DAY,1,t.Fecha) AS Fecha
FROM vn2008.Tickets t
JOIN warehouse w ON w.id = warehouse_id
JOIN Agencias a USING(Id_Agencia)
JOIN vn2008.Movimientos m USING(Id_Ticket)
JOIN cache.last_buy b ON m.Id_Article = b.item_id AND t.warehouse_id = b.warehouse_id
LEFT JOIN warehouse_pickup wp ON wp.agency_id = t.Id_Agencia and wp.warehouse_id = t.warehouse_id
JOIN warehouse_pickup is_rec ON is_rec.agency_id = t.Id_Agencia
WHERE t.Fecha BETWEEN TIMESTAMPADD(DAY,-1,datSTART) AND TIMESTAMPADD(DAY,-1,datEND)
AND wp.agency_id IS NULL
GROUP BY Agencia, Almacen, timestampadd(DAY,1,t.Fecha)
-- JGF 2016-05-05
UNION ALL
SELECT CAST(SUM(Cantidad * vn.buy_getUnitVolume(Id_Compra)) / 1000000 AS decimal(5,1)) AS m3 ,a.Agencia, w.name AS Almacen, timestampadd(DAY,1,t.shipment) AS Fecha
FROM Compres c
JOIN Entradas e ON e.Id_Entrada = c.Id_Entrada
JOIN travel t ON t.id = e.travel_id
JOIN Agencias a ON a.Id_Agencia = t.agency_id
JOIN warehouse w ON w.id = t.warehouse_id_out
JOIN warehouse_pickup wp ON wp.warehouse_id = t.warehouse_id_out
WHERE t.shipment BETWEEN TIMESTAMPADD(DAY,-1,datSTART) AND TIMESTAMPADD(DAY,-1,datEND)
GROUP BY a.Agencia, Almacen, Fecha;
END$$
DELIMITER ;