DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`raidUpdate`()
BEGIN
/**
 * Actualiza el travel de las entradas de redadas
 */
	UPDATE entry e
		JOIN entryVirtual ev ON ev.entryFk = e.id
		JOIN travel t ON t.id = e.travelFk
		JOIN (
			SELECT *
				FROM (
					SELECT id, landed, tt.warehouseInFk, tt.warehouseOutFk
						FROM travel t
							JOIN (
								SELECT t.warehouseInFk, t.warehouseOutFk
									FROM entryVirtual ev
										JOIN entry e ON e.id = ev.entryFk
										JOIN travel t ON t.id = e.travelFk
									GROUP BY t.warehouseInFk, t.warehouseOutFk
							) tt ON t.warehouseInFk = tt.warehouseInFk AND t.warehouseOutFk = tt.warehouseOutFk
						WHERE shipped > util.VN_CURDATE() AND isDelivered = FALSE
						ORDER BY t.landed
						LIMIT 10000000000000000000
				) t  
				GROUP BY warehouseInFk, warehouseOutFk
		) tt ON t.warehouseInFk = tt.warehouseInFk AND t.warehouseOutFk = tt.warehouseOutFk
		SET e.travelFk = t.id;

END$$
DELIMITER ;
