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

120 lines
3.1 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`travelDetail`()
BEGIN
/**
* Returns the details of the travels that have a departure warehouse in Ecuador or Colombia.
*/
DECLARE vDateFrom DATE DEFAULT util.VN_CURDATE() - INTERVAL 12 WEEK;
SELECT IFNULL(CONCAT(" ",Entrada),travel) travelAndEntry,
travel,
Entrada,
IsTravel,
Agencia,
ref,
shipment,
OrigenCajas,
landing,
Destino,
Etiquetas,
Notas_Eva,
kg,
loadedKg,
volumeKg,
loadPriority,
invoiceAmount,
Carguera,
reference
FROM
(SELECT TRUE IsTravel,
tr.id travel,
NULL Entrada,
ag.Agencia,
tr.ref,
tr.shipment,
wo.name OrigenCajas,
tr.landing,
w.name Destino,
SUM(c.Etiquetas) Etiquetas,
NULL Notas_Eva,
tr.kg,
CAST(SUM(c.weight * c.Etiquetas) AS INT) loadedkg,
CAST(
SUM(vc.aerealVolumetricDensity *
c.Etiquetas *
IF(cb.Volumen,
cb.Volumen,
cb.X * cb.Y * cb.Z
) / 1000000
) AS INT
) volumeKg,
NULL loadPriority,
NULL invoiceAmount,
pc.Alias Carguera,
NULL reference
FROM travel tr
LEFT JOIN Proveedores pc ON pc.Id_Proveedor = tr.cargoSupplierFk
LEFT JOIN Entradas e ON e.travel_id = tr.id
LEFT JOIN Compres c ON c.Id_Entrada = e.Id_Entrada
LEFT JOIN Cubos cb ON cb.Id_Cubo = c.Id_Cubo
LEFT JOIN Articles a ON a.Id_Article = c.Id_Article
LEFT JOIN Tipos tp ON tp.tipo_id = a.tipo_id
JOIN warehouse w ON w.id = tr.warehouse_id
JOIN warehouse wo ON wo.id = tr.warehouse_id_out
JOIN Agencias ag ON ag.Id_Agencia = tr.agency_id
JOIN vn.volumeConfig vc
WHERE tr.landing >= vDateFrom
AND (wo.name="Colombia" OR wo.name="Ecuador")
GROUP BY tr.id
UNION ALL
SELECT 0 IsTravel,
e.travel_id travel,
e.Id_Entrada,
p.Proveedor,
e.Referencia,
tr.shipment,
wo.name OrigenCajas,
tr.landing,
w.name Destino,
SUM(Etiquetas) Etiquetas,
e.Notas_Eva,
NULL kg,
CAST(SUM(c.weight * c.Etiquetas) AS INT) loadedkg,
CAST(
SUM(vc.aerealVolumetricDensity *
c.Etiquetas *
IF(cb.Volumen,
cb.Volumen,
cb.X * cb.Y * cb.Z
) / 1000000
) AS INT
) volumeKg,
loadPriority,
e.invoiceAmount,
pc.Alias carguera,
e.reference
FROM Entradas e
JOIN Compres c ON c.Id_Entrada = e.Id_Entrada
JOIN Cubos cb ON cb.Id_Cubo = c.Id_Cubo
JOIN Articles a ON a.Id_Article = c.Id_Article
JOIN Tipos tp ON tp.tipo_id = a.tipo_id
JOIN Proveedores p ON p.Id_Proveedor = e.Id_Proveedor
JOIN travel tr ON tr.id = e.travel_id
LEFT JOIN Proveedores pc ON pc.Id_Proveedor = tr.cargoSupplierFk
JOIN warehouse w ON w.id = tr.warehouse_id
JOIN warehouse wo ON wo.id = tr.warehouse_id_out
JOIN vn.volumeConfig vc
WHERE tr.landing >= vDateFrom
AND (wo.name="Colombia" OR wo.name="Ecuador")
GROUP BY e.Id_Entrada
) sub
ORDER BY landing ASC,
shipment ASC,
travel,
IsTravel DESC,
(loadPriority > 0) DESC,
loadPriority,
Agencia,
Notas_Eva;
END$$
DELIMITER ;