DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_setProblemFreeze`(
	vClientFk INT 
)
BEGIN
/**
 * Update the problem of tickets whose client is frozen or unfrozen
 * 
 * @param vClientFk Id Cliente, if NULL all clients
 */
	CREATE OR REPLACE TEMPORARY TABLE tmp.ticket 
		(INDEX(ticketFk))
		ENGINE = MEMORY
		SELECT t.id ticketFk, FALSE hasProblem
			FROM ticket t 
			WHERE t.shipped >= util.midnight()
				AND (vClientFk IS NULL OR t.clientFk = vClientFk);

	UPDATE tmp.ticket t
			JOIN ticket ti ON ti.id = t.ticketFk
			JOIN client c ON c.id = ti.clientFk
		SET t.hasProblem = TRUE
		WHERE c.isFreezed;
	
	CALL ticket_setProblem('hasTicketRequest');

	DROP TEMPORARY TABLE tmp.ticket;
END$$
DELIMITER ;