CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn`.`expeditionRoute_freeTickets` AS
SELECT
    `t`.`routeFk` AS `routeFk`,
    `tss`.`ticketFk` AS `ticket`,
    `s`.`name` AS `code`,
    `w`.`name` AS `almacen`,
    `tss`.`updated` AS `updated`,
    `p`.`code` AS `parkingCode`
	FROM `vn`.`ticketState` `tss`
		JOIN `vn`.`ticket` `t` ON `t`.`id` = `tss`.`ticketFk`
		JOIN `vn`.`warehouse` `w` ON `w`.`id` = `t`.`warehouseFk`
		JOIN `vn`.`state` `s` ON `s`.`id` = `tss`.`state`
		LEFT JOIN `vn`.`ticketParking` `tp` ON `tp`.`ticketFk` = `t`.`id`
		LEFT JOIN `vn`.`parking` `p` ON `p`.`id` = `tp`.`parkingFk`
	WHERE IFNULL(`t`.`packages`, 0) = 0;

CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn`.`ticketState`
AS SELECT `tt`.`created` AS `updated`,
	`tt`.`stateFk` AS `stateFk`,
	`tt`.`userFk` AS `userFk`,
	`tls`.`ticketFk` AS `ticketFk`,
	`s`.`id` AS `state`,
	`s`.`order` AS `productionOrder`,
	`s`.`alertLevel` AS `alertLevel`,
	`s`.`code` AS `code`,
	`s`.`isPreviousPreparable` AS `isPreviousPreparable`,
	`s`.`isPicked` AS `isPicked`
FROM (
		(
			`vn`.`ticketLastState` `tls`
			JOIN `vn`.`ticketTracking` `tt` ON(`tt`.`id` = `tls`.`ticketTrackingFk`)
		)
		JOIN `vn`.`state` `s` ON(`s`.`id` = `tt`.`stateFk`)
	);

CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn`.`ticketStateToday`
AS
SELECT
    `ts`.`ticketFk` AS `ticket`,
    `ts`.`state` AS `state`,
    `ts`.`productionOrder` AS `productionOrder`,
    `ts`.`alertLevel` AS `alertLevel`,
    `ts`.`userFk` AS `worker`,
    `ts`.`code` AS `code`,
    `ts`.`updated` AS `updated`,
    `ts`.`isPicked` AS `isPicked`
FROM
    `vn`.`ticketState` `ts`
    JOIN `vn`.`ticket` `t` ON `t`.`id` = `ts`.`ticketFk`
WHERE
    `t`.`shipped` BETWEEN `util`.`VN_CURDATE`() AND `util`.`VN_CURDATE`() + INTERVAL 1 DAY;

