DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `hedera`.`myTicket_list`(vFrom DATE, vTo DATE)
BEGIN
/**
 * Returns the current user list of tickets between two dates reange.
 * Also returns the total price with tax of every ticket.
 *
 * @param vFrom From date, if %NULL current date minus 25 days
 * @param vTo To date, if %NULL current date
 * @select The tickets list
 */
	SET vFrom = IFNULL(vFrom, DATE_FORMAT(TIMESTAMPADD(MONTH, -3, util.VN_CURDATE()), '%Y-%m-01'));
	SET vTo = IFNULL(vTo, TIMESTAMPADD(YEAR, 1, util.VN_CURDATE()));

	DROP TEMPORARY TABLE IF EXISTS tmp.ticket;
	CREATE TEMPORARY TABLE tmp.ticket
		(KEY (ticketFk))
		ENGINE = MEMORY
		SELECT t.id ticketFk
			FROM myTicket t
			WHERE shipped BETWEEN TIMESTAMP(vFrom) AND TIMESTAMP(vTo, '23:59:59');
	
	CALL vn.ticketGetTotal(NULL);

	SELECT v.id, IFNULL(v.landed, v.shipped) landed,
			v.shipped, v.companyFk, v.nickname,
			a.city, ag.description agency, t.total
		FROM tmp.ticket i
			JOIN vn.ticket v ON v.id = i.ticketFk
			JOIN vn.address a ON a.id = v.addressFk
            JOIN vn.agencyMode ag ON ag.id = v.agencyModeFk
			JOIN tmp.ticketTotal t ON t.ticketFk = i.ticketFk
		ORDER BY IFNULL(v.landed, v.shipped) DESC, i.ticketFk DESC;

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