CREATE DEFINER=`root`@`%` PROCEDURE `ticket_closeByRoute`( vRouteFk INT)
BEGIN
/**
 * Inserta los tickets de la ruta en la tabla temporal
 * para ser cerrados.
 *
 * @param vWarehouseFk Almacén a cerrar
 * @param vRouteFk Ruta a cerrar
 * @param vDate Fecha del cierre
 */

	DROP TEMPORARY TABLE IF EXISTS tmp.ticketClosure;

	CREATE TEMPORARY TABLE tmp.ticketClosure ENGINE = MEMORY (
		SELECT 
			t.id AS ticketFk
		FROM expedition e
			JOIN ticket t ON t.id = e.ticketFk
			JOIN ticketState ts ON ts.ticketFk = t.id
		    JOIN alertLevel al ON al.alertLevel = ts.alertLevel
		WHERE al.code = 'PACKED'
			AND t.routeFk = vRouteFk
			AND t.refFk IS NULL
		GROUP BY e.ticketFk);
        
	CALL ticket_close();
    
    DROP TEMPORARY TABLE tmp.ticketClosure;
END