CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `srt`.`bufferStock`
AS SELECT `e`.`id` AS `expeditionFk`,
	`e`.`bufferFk` AS `bufferFk`,
	`e`.`position` AS `position`,
	`srt`.`dayMinute`(IFNULL(`et`.`eta`, `z`.`hour`)) AS `dayMinute`,
	IFNULL(
		`et`.`eta`,
		`util`.`VN_CURDATE`() + INTERVAL `srt`.`dayMinute`(`z`.`hour`) + 120 MINUTE
	) AS `eta`,
	`ve`.`ticketFk` AS `ticketFk`,
	IFNULL(`t`.`routeFk`, `t`.`agencyModeFk`) AS `routeFk`,
	`z`.`name` AS `zonaTicket`,
	`et`.`description` AS `truck`,
	`es`.`description` AS `expeditionState`,
	`b`.`hasWorkerWaiting` AS `hasWorkerWaiting`,
	`b`.`isActive` AS `isActive`,
	IF(
		`et`.`id` IS NULL,
		`c`.`bufferDefault`,
		`et`.`bufferFk`
	) AS `bufferTruck`,
	`bt`.`typeName` AS `typeName`,
	`rm`.`bufferFk` AS `routeBuffer`
FROM (
		(
			(
				(
					(
						(
							(
								(
									(
										`srt`.`expedition` `e`
										LEFT JOIN `vn`.`expedition` `ve` ON(`ve`.`id` = `e`.`id`)
									)
									JOIN `srt`.`expeditionState` `es` ON(`es`.`id` = `e`.`stateFk`)
								)
								JOIN `srt`.`buffer` `b` ON(`b`.`id` = `e`.`bufferFk`)
							)
							LEFT JOIN `vn`.`ticket` `t` ON(`t`.`id` = `ve`.`ticketFk`)
						)
						LEFT JOIN `vn`.`zone` `z` ON(`z`.`id` = `t`.`zoneFk`)
					)
					LEFT JOIN `vn`.`routesMonitor` `rm` ON(`t`.`routeFk` = `rm`.`routeFk`)
				)
				LEFT JOIN `vn`.`expeditionTruck` `et` ON(`rm`.`expeditionTruckFk` = `et`.`id`)
			)
			JOIN `srt`.`config` `c`
		)
		JOIN `srt`.`bufferType` `bt` ON(`bt`.`id` = `b`.`typeFk`)
	)
WHERE `bt`.`typeName` <> 'DISABLED'
