CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn`.`routesControl`
AS SELECT `t`.`routeFk` AS `routeFk`,
	COUNT(`e`.`id`) AS `expeditions`,
	COUNT(`es`.`id`) AS `scanned`,
	COUNT(DISTINCT `es`.`palletFk`) AS `pallets`,
	max(`es`.`scanned`) AS `lastScanned`,
	max(`rs`.`description`) AS `description`,
	max(`rs`.`eta`) AS `eta`
FROM (
		(
			(
				(
					`vn`.`ticket` `t`
					JOIN `vn`.`expedition` `e` ON(`t`.`id` = `e`.`ticketFk`)
				)
				LEFT JOIN `vn`.`expeditionScan` `es` ON(`es`.`expeditionFk` = `e`.`id`)
			)
			LEFT JOIN `vn`.`expeditionPallet` `ep` ON(`ep`.`id` = `es`.`palletFk`)
		)
		LEFT JOIN `vn`.`roadmapStop` `rs` ON(`rs`.`id` = `ep`.`truckFk`)
	)
WHERE `t`.`shipped` >= `util`.`VN_CURDATE`()
	AND `t`.`routeFk` <> 0
GROUP BY `t`.`routeFk`
ORDER BY max(`rs`.`eta`)
