DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`preOrdenarRuta`(IN vRutaId INT)
BEGIN
/* Usa los valores del ultimo año para adivinar el orden de los tickets en la ruta
 * vRutaId id ruta
 * DEPRECATED use vn.routeGressPriority
*/

	UPDATE vn.ticket mt
		JOIN (
			SELECT tt.addressFk Id_Consigna, round(ifnull(avg(t.priority),0),0) as Prioridad
				FROM vn.ticket t
					JOIN vn.ticket tt on tt.addressFk = t.addressFk
				WHERE t.shipped > TIMESTAMPADD(YEAR,-1,util.VN_CURDATE())
					AND tt.routeFk = vRutaId
				GROUP BY t.addressFk
			) sub ON sub.Id_Consigna = mt.addressFk
	SET mt.priority = sub.Prioridad
	WHERE mt.routeFk = vRutaId;

END$$
DELIMITER ;
