DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`ticket_isProblemCalcNeeded`(
	vSelf INT
)
	RETURNS BOOL
	DETERMINISTIC
BEGIN
/**
 * Check if the ticket requires to update column vn.ticket.problem
 * 
 * @param vSelf Id ticket
 * @return BOOL
 */
	DECLARE vIsProblemCalcNeeded BOOL;

	SELECT COUNT(*) INTO vIsProblemCalcNeeded
		FROM ticket t
			JOIN client c ON c.id = t.clientFk
			JOIN agencyMode am ON am.id = t.agencyModeFk
			JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk
		WHERE t.id = vSelf
			AND dm.code IN ('AGENCY','DELIVERY','PICKUP')
			AND c.typeFk = 'normal';

	RETURN vIsProblemCalcNeeded;
END$$
DELIMITER ;