salix/db/routines/vn/procedures/expedition_getFromRoute.sql

46 lines
1.5 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`expedition_getFromRoute`(vRouteFk INT)
BEGIN
/**
* Obtiene las expediciones a partir de una ruta
* @param vRouteFk Id de la ruta
*/
SELECT e.id,
e.ticketFk,
t.routeFk,
ts.alertLevel,
a.street,
a.city,
a.postalCode,
t.addressFk,
a.nickname,
sub2.itemPackingTypeConcat,
2024-06-07 07:18:52 +00:00
est.code,
sub5.isScanned
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = e.ticketFk
JOIN address a ON t.addressFk = a.id
JOIN route r ON r.id = t.routeFk
LEFT JOIN warehouse w ON t.warehouseFk = w.id
LEFT JOIN (SELECT sub.ticketFk,
CONCAT('(', GROUP_CONCAT(DISTINCT sub.itemPackingTypeFk ORDER BY sub.items DESC SEPARATOR ','), ') ') itemPackingTypeConcat
FROM (SELECT s.ticketFk , i.itemPackingTypeFk, COUNT(*) items
FROM ticket t
JOIN sale s ON s.ticketFk = t.id
JOIN item i ON i.id = s.itemFk
WHERE t.routeFk = vRouteFk
GROUP BY t.id,i.itemPackingTypeFk)sub
GROUP BY sub.ticketFk
) sub2 ON sub2.ticketFk = t.id
LEFT JOIN expeditionStateType est ON est.id = e.stateTypeFk
2024-06-07 07:18:52 +00:00
LEFT JOIN (SELECT es.expeditionFk, isScanned
FROM expeditionState es
JOIN (SELECT expeditionFk, MAX(id) maxId
FROM expeditionState es
GROUP BY expeditionFk)sub4 ON sub4.maxId = es.id)sub5 ON sub5.expeditionFk = e.id
WHERE t.routeFk = vRouteFk AND e.freightItemFk <> FALSE
ORDER BY r.created, t.priority DESC;
END$$
DELIMITER ;