2023-06-06 09:42:35 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_canMerge`(vDated DATE, vScopeDays INT, vLitersMax INT, vLinesMax INT, vWarehouseFk INT)
|
2023-06-06 09:42:35 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Devuelve un listado de tickets susceptibles de fusionarse con otros tickets en el futuro
|
2024-01-15 11:31:03 +00:00
|
|
|
*
|
|
|
|
* @param vDated Fecha en cuestión
|
|
|
|
* @param vScopeDays Dias en el futuro a sondear
|
|
|
|
* @param vLitersMax Volumen máximo de los tickets a catapultar
|
|
|
|
* @param vLinesMax Número máximo de lineas de los tickets a catapultar
|
2023-06-06 09:42:35 +00:00
|
|
|
* @param vWarehouseFk Identificador de vn.warehouse
|
|
|
|
*/
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT sv.ticketFk,
|
2023-06-06 09:42:35 +00:00
|
|
|
GROUP_CONCAT(DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk) ipt,
|
2024-01-15 11:31:03 +00:00
|
|
|
CAST(SUM(litros) AS DECIMAL(10,0)) liters,
|
|
|
|
CAST(COUNT(*) AS DECIMAL(10,0)) `lines`,
|
2023-06-06 09:42:35 +00:00
|
|
|
st.name state,
|
2024-01-15 11:31:03 +00:00
|
|
|
sub2.id ticketFuture,
|
|
|
|
sub2.shipped,
|
|
|
|
sub2.iptd tfIpt,
|
|
|
|
sub2.state tfState
|
2023-06-06 09:42:35 +00:00
|
|
|
FROM vn.saleVolume sv
|
|
|
|
JOIN vn.sale s ON s.id = sv.saleFk
|
|
|
|
JOIN vn.item i ON i.id = s.itemFk
|
|
|
|
JOIN vn.ticket t ON t.id = sv.ticketFk
|
|
|
|
JOIN vn.address a ON a.id = t.addressFk
|
|
|
|
JOIN vn.province p ON p.id = a.provinceFk
|
|
|
|
JOIN vn.country c ON c.id = p.countryFk
|
|
|
|
JOIN vn.ticketState ts ON ts.ticketFk = t.id
|
|
|
|
JOIN vn.state st ON st.id = ts.stateFk
|
|
|
|
JOIN vn.alertLevel al ON al.id = ts.alertLevel
|
|
|
|
LEFT JOIN vn.ticketParking tp ON tp.ticketFk = t.id
|
|
|
|
LEFT JOIN (
|
|
|
|
SELECT *
|
|
|
|
FROM (
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT t.addressFk,
|
|
|
|
t.id,
|
|
|
|
t.shipped,
|
|
|
|
st.name state,
|
|
|
|
GROUP_CONCAT(DISTINCT i.itemPackingTypeFk
|
|
|
|
ORDER BY i.itemPackingTypeFk) iptd
|
2023-06-06 09:42:35 +00:00
|
|
|
FROM vn.ticket t
|
|
|
|
JOIN vn.ticketState ts ON ts.ticketFk = t.id
|
|
|
|
JOIN vn.state st ON st.id = ts.stateFk
|
|
|
|
JOIN vn.sale s ON s.ticketFk = t.id
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN vn.alertLevel al ON al.id = st.alertLevel
|
2023-06-06 09:42:35 +00:00
|
|
|
JOIN vn.item i ON i.id = s.itemFk
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE t.shipped BETWEEN TIMESTAMPADD(DAY, vScopeDays,vDated)
|
|
|
|
AND util.dayend(TIMESTAMPADD(DAY, vScopeDays,vDated))
|
2023-06-06 09:42:35 +00:00
|
|
|
AND t.warehouseFk = vWarehouseFk
|
2024-01-15 11:31:03 +00:00
|
|
|
AND al.code = 'FREE'
|
2023-06-06 09:42:35 +00:00
|
|
|
GROUP BY t.id
|
|
|
|
) sub
|
|
|
|
GROUP BY sub.addressFk
|
|
|
|
) sub2 ON sub2.addressFk = t.addressFk AND t.id != sub2.id
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE t.shipped BETWEEN vDated AND util.dayend(vDated)
|
2023-06-06 09:42:35 +00:00
|
|
|
AND t.warehouseFk = vWarehouseFk
|
|
|
|
AND al.code = 'FREE'
|
|
|
|
AND tp.ticketFk IS NULL
|
|
|
|
GROUP BY sv.ticketFk
|
2024-01-15 11:31:03 +00:00
|
|
|
HAVING liters <= vLitersMax
|
|
|
|
AND `lines` <= vLinesMax
|
|
|
|
AND ticketFuture;
|
2023-06-06 09:42:35 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|