DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`ticket_setProblemTooLittleItemCost`(
	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, NULL ALL items
 */
	CREATE OR REPLACE TEMPORARY TABLE tmp.ticket 
		(INDEX(ticketFk, isProblemCalcNeeded))
		ENGINE = MEMORY
		WITH tickets AS(
			SELECT t.id ticketFk
				FROM vn.ticket t
					LEFT JOIN vn.sale s ON s.ticketFk = t.id
				WHERE t.shipped >= util.VN_CURDATE()
					AND (s.itemFk = vItemFk OR vItemFk IS NULL)
				GROUP BY t.id
		)SELECT ticketFk, 
				ticket_isTooLittle(ticketFk) hasProblem, 
				ticket_isProblemCalcNeeded(ticketFk) isProblemCalcNeeded
			FROM tickets;

	CALL ticket_setProblem('isTooLittle');

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