DROP PROCEDURE IF EXISTS `vn`.`ticket_getProblems`;

DELIMITER $$
$$
CREATE
    DEFINER = root@`%` PROCEDURE `vn`.`ticket_getProblems`(IN vIsTodayRelative TINYINT(1))
BEGIN
    CALL sale_getProblems(vIsTodayRelative);

    DROP TEMPORARY TABLE IF EXISTS tmp.ticket_problems;
    CREATE TEMPORARY TABLE tmp.ticket_problems 
    (INDEX (ticketFk))
    ENGINE = MEMORY
    SELECT 
        ticketFk,
        MAX(p.isFreezed)            AS isFreezed,
        MAX(p.risk)                 AS risk,
        MAX(p.hasTicketRequest)     AS hasTicketRequest,
        MIN(p.isAvailable)          AS isAvailable,
        MAX(p.itemShortage)         AS itemShortage,
        MIN(p.isTaxDataChecked)     AS isTaxDataChecked,
        MAX(p.hasComponentLack)     AS hasComponentLack
     FROM tmp.sale_problems p
        GROUP BY ticketFk;

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