DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`ticket_DelayTruck`(vWarehouserFk INT, vHour INT, vMinute INT)
BEGIN
	DECLARE done INT DEFAULT FALSE;
	DECLARE vTicketFk INT;
	DECLARE cur1 CURSOR FOR SELECT ticketFk FROM tTicket;

	DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

	CALL vn.productionControl(vWarehouserFk,0) ;

	DROP TEMPORARY TABLE IF EXISTS tTicket;
	CREATE TEMPORARY TABLE tTicket
		SELECT ticketFk
		FROM tmp.productionBuffer
			JOIN alertLevel al ON al.code = 'FREE'
		WHERE shipped = util.VN_CURDATE()
			AND problem LIKE '%I:%'
			AND (HH <= vHour OR HH = vHour AND mm < vMinute)
			AND alertLevel = al.id;

	OPEN cur1;

	read_loop: LOOP
		FETCH cur1 INTO vTicketFk;
		IF done THEN
			LEAVE read_loop;
		END IF;

		CALL vn.ticket_DelayTruckSplit(vTicketFk);
	END LOOP;

	CLOSE cur1;
	DROP TEMPORARY TABLE tTicket, tmp.productionBuffer;
END$$
DELIMITER ;
