DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_getProblems`(
	vIsTodayRelative TINYINT(1)
)
BEGIN
/**
 * Calcula los problemas para un conjunto de tickets.
 *
 * @param vIsTodayRelative Indica si se calcula el disponible como si todo saliera hoy
 * @table tmp.ticket(ticketFk) Identificadores de los tickets a calcular
 * @return tmp.ticketProblems, tmp.saleProblems
 */
	CREATE OR REPLACE TEMPORARY TABLE tmp.sale (
		saleFk INT(11),
		PRIMARY KEY (saleFk)
	) ENGINE = MEMORY
		SELECT DISTINCT s.id saleFk
			FROM tmp.ticket tt
				JOIN ticket t ON t.id = tt.ticketFk
				JOIN sale s ON s.ticketFk = t.id
			GROUP BY s.id;

	CALL sale_getProblems(vIsTodayRelative);

	CREATE OR REPLACE TEMPORARY TABLE tmp.ticketProblems (
		ticketFk INT(11),
		isFreezed BOOL DEFAULT FALSE,
		risk DECIMAL(10,1) DEFAULT 0,
		hasRisk BOOL DEFAULT FALSE,
		hasHighRisk BOOL DEFAULT FALSE,
		hasTicketRequest BOOL DEFAULT FALSE,
		isTaxDataChecked BOOL DEFAULT FALSE,
		isTooLittle BOOL DEFAULT FALSE,
		isVip BOOL DEFAULT FALSE,
		hasItemShortage BOOL DEFAULT FALSE,
		hasItemDelay BOOL DEFAULT FALSE,
		hasItemLost BOOL DEFAULT FALSE,
		hasComponentLack BOOL DEFAULT FALSE,
		hasRounding BOOL DEFAULT FALSE,
		PRIMARY KEY (ticketFk)
	) ENGINE = MEMORY
	WITH hasItemShortage AS(
	SELECT s.ticketFk
		FROM tmp.saleProblems sp
			JOIN vn.sale s ON s.id = sp.saleFk
		WHERE sp.hasItemShortage
		GROUP BY s.ticketFk
	),hasItemLost AS(
	SELECT s.ticketFk
		FROM tmp.saleProblems sp
			JOIN vn.sale s ON s.id = sp.saleFk
		WHERE sp.hasItemLost
		GROUP BY s.ticketFk
	),hasRounding AS(
	SELECT s.ticketFk
		FROM tmp.saleProblems sp
			JOIN vn.sale s ON s.id = sp.saleFk
		WHERE sp.hasRounding
		GROUP BY s.ticketFk
	), hasItemDelay AS(
	SELECT s.ticketFk
		FROM tmp.saleProblems sp
			JOIN vn.sale s ON s.id = sp.saleFk
		WHERE sp.hasItemDelay
		GROUP BY s.ticketFk
	), hasComponentLack AS(
	SELECT s.ticketFk
		FROM tmp.saleProblems sp
			JOIN vn.sale s ON s.id = sp.saleFk
		WHERE sp.hasComponentLack
		GROUP BY s.ticketFk
	)SELECT tt.ticketFk,
			FIND_IN_SET('isFreezed', t.problem) > 0 isFreezed,
			t.risk,
			FIND_IN_SET('hasRisk', t.problem) > 0 hasRisk,
			FIND_IN_SET('hasHighRisk', t.problem) > 0 hasHighRisk,
			FIND_IN_SET('hasTicketRequest', t.problem) > 0 hasTicketRequest,
			FIND_IN_SET('isTaxDataChecked', t.problem) > 0 isTaxDataChecked,
			FIND_IN_SET('isTooLittle', t.problem) > 0
				AND util.VN_NOW() < (util.VN_CURDATE() +
					INTERVAL HOUR(zc.`hour`) HOUR) +
					INTERVAL MINUTE(zc.`hour`) MINUTE isTooLittle,
			c.businessTypeFk = 'VIP' isVip,
			NOT (his.ticketFk IS NULL) hasItemShortage,
			NOT (hid.ticketFk IS NULL) hasItemDelay,
			NOT (hil.ticketFk IS NULL) hasItemLost,
			NOT (hcl.ticketFk IS NULL) hasComponentLack,
			NOT (hr.ticketFk IS NULL) hasRounding,
			0 totalProblems
		FROM tmp.ticket tt
			JOIN vn.ticket t ON t.id = tt.ticketFk
			JOIN vn.client c ON c.id = t.clientFk
			LEFT JOIN hasItemShortage his ON his.ticketFk = t.id
			LEFT JOIN hasItemLost hil ON hil.ticketFk = t.id
			LEFT JOIN hasRounding hr ON hr.ticketFk = t.id
			LEFT JOIN hasItemDelay hid ON hid.ticketFk = t.id
			LEFT JOIN hasComponentLack hcl ON hcl.ticketFk = t.id
			LEFT JOIN vn.zoneClosure zc ON zc.zoneFk = t.zoneFk
				AND zc.dated = util.VN_CURDATE()
		GROUP BY t.id;

	UPDATE tmp.ticketProblems
		SET totalProblems = isFreezed + hasHighRisk + hasTicketRequest +
			isTaxDataChecked + hasComponentLack + hasItemDelay +
			isTooLittle + hasItemLost + hasRounding  + hasItemShortage + isVip;

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