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

90 lines
3.0 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`collection_getTickets`(vParamFk INT)
BEGIN
/**
2024-04-11 10:16:00 +00:00
* Selecciona los tickets de una colección/ticket/sectorCollection
* @param vParamFk ticketFk/collectionFk/sectorCollection
* @return Retorna ticketFk, level, agencyName, warehouseFk, salesPersonFk, observation
*/
DECLARE vItemPackingTypeFk VARCHAR(1);
2024-04-11 10:16:00 +00:00
DECLARE vDated DATE;
-- Si los sacadores son los de pruebas, pinta los colores
2024-04-11 10:16:00 +00:00
SELECT itemPackingTypeFk
INTO vItemPackingTypeFk
FROM collection
WHERE id = vParamFk;
2024-04-11 10:16:00 +00:00
SET vDated = util.yesterday();
2024-04-11 10:16:00 +00:00
WITH observation AS (
SELECT tob.ticketFk, tob.description
2024-05-23 14:35:36 +00:00
FROM ticketObservation tob
JOIN ticketCollection tc ON tc.ticketFk = tob.ticketFk
LEFT JOIN observationType ot ON ot.id = tob.observationTypeFk
2024-04-11 10:16:00 +00:00
WHERE ot.`code` = 'itemPicker'
AND tc.collectionFk = vParamFk
)
SELECT t.id ticketFk,
2024-04-11 10:16:00 +00:00
IF(!(vItemPackingTypeFk <=> 'V'), cc.code, CONCAT(SUBSTRING('ABCDEFGH', tc.wagon, 1), '-', tc.`level`)) `level`,
am.name agencyName,
t.warehouseFk,
w.id salesPersonFk,
2024-04-11 10:16:00 +00:00
IFNULL(ob.description,'') observation,
cc.rgb
2024-05-23 14:35:36 +00:00
FROM ticket t
LEFT JOIN ticketCollection tc ON t.id = tc.ticketFk
LEFT JOIN collection c2 ON c2.id = tc.collectionFk
LEFT JOIN collectionColors cc
ON cc.wagon = tc.wagon
AND cc.shelve = tc.`level`
2024-04-11 10:16:00 +00:00
AND cc.trainFk = c2.trainFk
2024-05-23 14:35:36 +00:00
LEFT JOIN zone z ON z.id = t.zoneFk
LEFT JOIN agencyMode am ON am.id = z.agencyModeFk
LEFT JOIN client c ON c.id = t.clientFk
LEFT JOIN worker w ON w.id = c.salesPersonFk
2024-04-11 10:16:00 +00:00
LEFT JOIN observation ob ON ob.ticketFk = t.id
WHERE t.id = vParamFk
2024-04-11 10:16:00 +00:00
AND t.shipped >= vDated
UNION ALL
SELECT t.id ticketFk,
2024-04-11 10:16:00 +00:00
IF(NOT(vItemPackingTypeFk <=> 'V'), cc.code, CONCAT(SUBSTRING('ABCDEFGH', tc.wagon, 1), '-', tc.`level`)) `level`,
am.name agencyName,
t.warehouseFk,
2024-04-11 10:16:00 +00:00
w.id salesPersonFk,
ob.description,
IF(NOT (vItemPackingTypeFk <=> 'V'), cc.rgb, NULL) `rgb`
2024-05-23 14:35:36 +00:00
FROM ticket t
JOIN ticketCollection tc ON t.id = tc.ticketFk
LEFT JOIN collection c2 ON c2.id = tc.collectionFk
LEFT JOIN collectionColors cc
ON cc.wagon = tc.wagon
AND cc.shelve = tc.`level`
2024-04-11 10:16:00 +00:00
AND cc.trainFk = c2.trainFk
2024-05-23 14:35:36 +00:00
LEFT JOIN zone z ON z.id = t.zoneFk
LEFT JOIN agencyMode am ON am.id = z.agencyModeFk
LEFT JOIN client c ON c.id = t.clientFk
LEFT JOIN worker w ON w.id = c.salesPersonFk
2024-04-11 10:16:00 +00:00
LEFT JOIN observation ob ON ob.ticketFk = t.id
WHERE tc.collectionFk = vParamFk
UNION ALL
SELECT sg.ticketFk,
NULL `level`,
am.name agencyName,
t.warehouseFk,
c.salesPersonFk,
ob.description,
NULL `rgb`
2024-05-23 14:35:36 +00:00
FROM sectorCollection sc
JOIN sectorCollectionSaleGroup ss ON ss.sectorCollectionFk = sc.id
JOIN saleGroup sg ON sg.id = ss.saleGroupFk
JOIN ticket t ON t.id = sg.ticketFk
LEFT JOIN zone z ON z.id = t.zoneFk
LEFT JOIN agencyMode am ON am.id = z.agencyModeFk
2024-04-11 10:16:00 +00:00
LEFT JOIN observation ob ON ob.ticketFk = t.id
2024-05-23 14:35:36 +00:00
LEFT JOIN client c ON c.id = t.clientFk
2024-04-11 10:16:00 +00:00
WHERE sc.id = vParamFk
AND t.shipped >= vDated;
END$$
DELIMITER ;