DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_getTooLittleProblemItemCost`(
	vItemFk INT
)
BEGIN
/**
 * Update the problems when the ticket is too small or is no longer so, 
 * derived from changes in the itemCost table
 * 
 * @param vItemFk Id del item
 */
	CREATE OR REPLACE TEMPORARY TABLE tmp.ticket 
		(INDEX(ticketFk))
		ENGINE = MEMORY
		WITH tickets AS(
			SELECT t.id ticketFk
				FROM vn.ticket t
					JOIN vn.sale s ON s.ticketFk = t.id
				WHERE s.itemFk = vItemFk
					AND t.shipped >= util.midnight()
				GROUP BY t.id
		)SELECT ticketFk, ticket_isTooLittle(ticketFk) hasProblem
			FROM tickets;

	CALL ticket_setProblem('isTooLittle');

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