CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `srt`.`routePalletized`
AS SELECT `t`.`routeFk` AS `routeFk`,
	COUNT(DISTINCT `e`.`id`) AS `expediciones`,
	sum(
		`es`.`description` <=> 'OUT'
		OR `esc`.`expeditionFk` IS NOT NULL
	) AS `paletizadas`
FROM (
		(
			(
				(
					(
						(
							`vn`.`ticket` `t`
							LEFT JOIN `vn`.`expedition` `e` ON(`e`.`ticketFk` = `t`.`id`)
						)
						LEFT JOIN `srt`.`expedition` `e2` ON(`e2`.`id` = `e`.`id`)
					)
					LEFT JOIN `srt`.`expeditionState` `es` ON(`es`.`id` = `e2`.`stateFk`)
				)
				LEFT JOIN `vn`.`expeditionScan` `esc` ON(`esc`.`expeditionFk` = `e`.`id`)
			)
			JOIN `vn`.`ticketCollection` `tc` ON(`tc`.`ticketFk` = `t`.`id`)
		)
		JOIN `vn`.`collection` `c` ON(`c`.`id` = `tc`.`collectionFk`)
	)
WHERE `t`.`shipped` BETWEEN `util`.`yesterday`() AND `util`.`tomorrow`()
	AND `c`.`itemPackingTypeFk` = 'H'
GROUP BY `t`.`routeFk`
