diff options
author | Daniel Friesel <daniel.friesel@uos.de> | 2019-09-18 18:19:54 +0200 |
---|---|---|
committer | Daniel Friesel <daniel.friesel@uos.de> | 2019-09-18 18:19:54 +0200 |
commit | 8e6ee729e0a444f4aaf01d0535862c290bdde311 (patch) | |
tree | 44605668aba8c1ea4ca7ef30df7e8becdb03c491 /lib | |
parent | aac8e2a0e1b870e9c40cecd15fd3d2fdfd484869 (diff) |
passengerrights: improve support for cancelled trains
To do: also do so in form generation
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Travelynx/Controller/Passengerrights.pm | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/lib/Travelynx/Controller/Passengerrights.pm b/lib/Travelynx/Controller/Passengerrights.pm index be1ab19..2e6de96 100644 --- a/lib/Travelynx/Controller/Passengerrights.pm +++ b/lib/Travelynx/Controller/Passengerrights.pm @@ -14,7 +14,6 @@ sub mark_if_missed_connection { = ( $next_journey->{rt_departure}->epoch - $journey->{rt_arrival}->epoch ) / 60; - # Assumption: $next_journey is a missed connection (i.e., if $journey had # been on time it would have been an earlier train) # * the wait time between arrival and departure is less than 70 minutes @@ -48,11 +47,42 @@ sub mark_if_missed_connection { return 0; } +sub mark_substitute_connection { + my ( $self, $journey ) = @_; + + my @substitute_candidates = reverse $self->get_user_travels( + after => $journey->{sched_departure}->clone->subtract( hours => 1 ), + before => $journey->{sched_departure}->clone->add( hours => 12 ), + ); + + my ( $first_substitute, $last_substitute ); + + for my $substitute_candidate (@substitute_candidates) { + if ( not $first_substitute + and $substitute_candidate->{from_name} eq $journey->{from_name} ) + { + $first_substitute = $substitute_candidate; + } + if ( not $last_substitute + and $substitute_candidate->{to_name} eq $journey->{to_name} ) + { + $last_substitute = $substitute_candidate; + last; + } + } + + if ( $first_substitute and $last_substitute ) { + $journey->{has_substitute} = 1; + $journey->{from_substitute} = $first_substitute; + $journey->{to_substitute} = $last_substitute; + } +} + sub list_candidates { my ($self) = @_; my $now = DateTime->now( time_zone => 'Europe/Berlin' ); - my $range_start = $now->clone->subtract( months => 12 ); + my $range_start = $now->clone->subtract( months => 6 ); my @journeys = $self->get_user_travels( after => $range_start, @@ -78,14 +108,27 @@ sub list_candidates { @journeys = grep { $_->{delay} >= 60 or $_->{connection_missed} } @journeys; - push( - @journeys, - map { $_->{cancelled} = 1; $_ } $self->get_user_travels( - after => $range_start, - before => $now, - cancelled => 1 - ) + my @cancelled = $self->get_user_travels( + after => $range_start, + before => $now, + cancelled => 1 ); + for my $journey (@cancelled) { + + if ( not $journey->{sched_arrival}->epoch ) { + next; + } + + $journey->{cancelled} = 1; + $self->mark_substitute_connection($journey); + + if ( not $journey->{has_substitute} + or $journey->{to_substitute}->{rt_arrival}->epoch + - $journey->{sched_arrival}->epoch >= 3600 ) + { + push( @journeys, $journey ); + } + } @journeys = sort { $b->{sched_departure}->epoch <=> $a->{sched_departure}->epoch } |