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 `workerFk`,
	`tls`.`ticketFk` AS `ticketFk`,
	`s`.`id` AS `state`,
	`s`.`order` AS `productionOrder`,
	`s`.`alertLevel` AS `alertLevel`,
	`s`.`code` AS `code`,
	`tls`.`ticketFk` AS `ticket`,
	`tt`.`userFk` AS `worker`,
	`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 `vn2008`.`v_inter`
AS SELECT `tt`.`id` AS `inter_id`,
	`tt`.`stateFk` AS `state_id`,
	`tt`.`notes` AS `nota`,
	`tt`.`created` AS `odbc_date`,
	`tt`.`ticketFk` AS `Id_Ticket`,
	`tt`.`userFk` AS `Id_Trabajador`,
	`tt`.`supervisorFk` AS `Id_supervisor`
FROM `vn`.`ticketTracking` `tt`;

CREATE OR REPLACE
ALGORITHM = UNDEFINED VIEW `ticketStateToday` AS
SELECT
    `ts`.`ticket` AS `ticket`,
    `ts`.`state` AS `state`,
    `ts`.`productionOrder` AS `productionOrder`,
    `ts`.`alertLevel` AS `alertLevel`,
    `ts`.`worker` AS `worker`,
    `ts`.`code` AS `code`,
    `ts`.`updated` AS `updated`,
    `ts`.`isPicked` AS `isPicked`
FROM
    (`ticketState` `ts`
JOIN `ticket` `t` ON
    (`t`.`id` = `ts`.`ticket`))
WHERE
    `t`.`shipped` BETWEEN `util`.`VN_CURDATE`() AND `MIDNIGHT`(`util`.`VN_CURDATE`());
