DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`sale_setProblemComponentLackByComponent`(
	vComponentFk INT
)
BEGIN
/**
 * Update the issues for sales lines that have or no longer have problems with components, verify 
 * whether all mandatory components are present or not resulting from changes in the table vn.component
 *
 * @param vComponentFk Id component
 */
	CREATE OR REPLACE TEMPORARY TABLE tmp.sale
		(INDEX(saleFk, isProblemCalcNeeded))
		ENGINE = MEMORY
		SELECT saleFk, 
				sale_hasComponentLack(saleFk) hasProblem, 
				(ticket_isProblemCalcNeeded(ticketFk) AND quantity > 0) isProblemCalcNeeded
			FROM (
				SELECT s.id saleFk, s.ticketFk, s.quantity
					FROM ticket t
						JOIN sale s ON s.ticketFk = t.id
						LEFT JOIN saleComponent sc ON sc.saleFk = s.id 
					WHERE t.shipped >= util.VN_CURDATE()
						 AND (vComponentFk IS NULL OR sc.componentFk = vComponentFk)
					GROUP BY s.id) sub;

	CALL sale_setProblem('hasComponentLack');

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