DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`camiones`(vWarehouse INT, vDate DATE)
BEGIN
	SELECT Temperatura
		,ROUND(SUM(Etiquetas * volume)) AS cm3 
		,ROUND(SUM(IF(scanned, Etiquetas, 0) * volume)) AS cm3s
		,ROUND(SUM(Vida * volume)) AS cm3e 
		FROM (
			SELECT t.Temperatura, c.Etiquetas, b.scanned, c.Vida,
					IF(cu.Volumen > 0, cu.Volumen, cu.x * cu.y * IF(cu.z > 0, cu.z, a.Medida + 10)) volume
				FROM Compres c
					LEFT JOIN buy_edi b ON b.id = c.buy_edi_id
					JOIN Articles a ON a.Id_Article = c.Id_Article
					JOIN Tipos t ON t.tipo_id = a.tipo_id
					JOIN Entradas e ON e.Id_Entrada = c.Id_Entrada
					JOIN travel tr ON tr.id = e.travel_id
					JOIN Cubos cu ON cu.Id_Cubo = c.Id_Cubo
				WHERE tr.warehouse_id = vWarehouse
					AND tr.landing = vDate
		) sub
		GROUP BY Temperatura;
END$$
DELIMITER ;
