From 531f3b0a68ace87dc514a1350971b647c39633b5 Mon Sep 17 00:00:00 2001
From: Daniel Friesel <derf@finalrewind.org>
Date: Tue, 19 Mar 2019 21:23:06 +0100
Subject: show cancelled trains in history

---
 index.pl                  | 33 +++++++++++++++++++++++++++------
 templates/history.html.ep | 45 +++++++++++++++++++++++++++++++++++----------
 templates/journey.html.ep | 21 ++++++++++++++++++---
 3 files changed, 80 insertions(+), 19 deletions(-)

diff --git a/index.pl b/index.pl
index 0f5622b..8cd45dd 100755
--- a/index.pl
+++ b/index.pl
@@ -808,6 +808,11 @@ helper 'get_user_travels' => sub {
 	else {
 		$query->execute($uid);
 	}
+	my @match_actions = ( $action_type{checkout}, $action_type{checkin} );
+	if ( $opt{cancelled} ) {
+		@match_actions
+		  = ( $action_type{cancelled_to}, $action_type{cancelled_from} );
+	}
 
 	my @travels;
 	my $prev_action = 0;
@@ -823,7 +828,9 @@ helper 'get_user_travels' => sub {
 		$raw_route    = decode( 'UTF-8', $raw_route );
 		$raw_messages = decode( 'UTF-8', $raw_messages );
 
-		if ( $action == $action_type{checkout} ) {
+		if ( $action == $match_actions[0]
+			or ( $opt{checkout_epoch} and $raw_ts == $opt{checkout_epoch} ) )
+		{
 			push(
 				@travels,
 				{
@@ -843,8 +850,13 @@ helper 'get_user_travels' => sub {
 				}
 			);
 		}
-		elsif ( $action == $action_type{checkin}
-			and $prev_action == $action_type{checkout} )
+		elsif (
+			(
+				    $action == $match_actions[1]
+				and $prev_action == $match_actions[0]
+			)
+			or ( $opt{checkin_epoch} and $raw_ts == $opt{checkin_epoch} )
+		  )
 		{
 			my $ref = $travels[-1];
 			$ref->{from_name}       = $name;
@@ -866,6 +878,11 @@ helper 'get_user_travels' => sub {
 				}
 				$ref->{messages} = [ reverse @parsed_messages ];
 			}
+			if (    $opt{checkin_epoch}
+				and $action == $action_type{cancelled_from} )
+			{
+				$ref->{cancelled} = 1;
+			}
 		}
 		$prev_action = $action;
 	}
@@ -1442,17 +1459,21 @@ get '/account' => sub {
 
 get '/history' => sub {
 	my ($self) = @_;
+	my $cancelled = $self->param('cancelled') ? 1 : 0;
 
 	$self->respond_to(
-		json => { json     => [ $self->get_user_travels ] },
-		any  => { template => 'history' }
+		json =>
+		  { json => [ $self->get_user_travels( cancelled => $cancelled ) ] },
+		any => { template => 'history' }
 	);
 };
 
 get '/history.json' => sub {
 	my ($self) = @_;
+	my $cancelled = $self->param('cancelled') ? 1 : 0;
 
-	$self->render( json => [ $self->get_user_travels ] );
+	$self->render(
+		json => [ $self->get_user_travels( cancelled => $cancelled ) ] );
 };
 
 get '/journey/:id' => sub {
diff --git a/templates/history.html.ep b/templates/history.html.ep
index d79b515..c2e6f88 100644
--- a/templates/history.html.ep
+++ b/templates/history.html.ep
@@ -1,11 +1,25 @@
+% if (param('cancelled')) {
+<h1>Ausgefallene Fahrten</h1>
+<div class="row">
+	<div class="col s12">
+		<ul>
+			<li><a href="/history.json?cancelled=1">JSON-Export</a> (Das Datenlayout ist noch nicht final)</li>
+			<li><a href="/history">Reguläre Zugfahrten</a></li>
+		</ul>
+	</div>
+</div>
+% }
+% else {
 <h1>Bisherige Fahrten</h1>
 <div class="row">
 	<div class="col s12">
 		<ul>
 			<li><a href="/history.json">JSON-Export</a> (Das Datenlayout ist noch nicht final)</li>
+			<li><a href="/history?cancelled=1">Ausgefallene Züge</a></li>
 		</ul>
 	</div>
 </div>
+% }
 <div class="row">
 	<div class="col s12">
 		<table class="striped">
@@ -19,25 +33,36 @@
 				</tr>
 			</thead>
 			<tbody>
-				% for my $travel (get_user_travels()) {
+				% for my $travel (get_user_travels(cancelled => (param('cancelled') ? 1 : 0))) {
 					% if ($travel->{completed}) {
 						% my $detail_link = '/journey/' . current_user()->{id} . '-' . $travel->{checkin}->epoch . '-' . $travel->{checkout}->epoch;
 						<tr>
 							<td><%= $travel->{sched_departure}->strftime('%d.%m.%Y') %></td>
 							<td><a href="<%= $detail_link %>"><%= $travel->{type} %> <%= $travel->{line} // '' %> <%= $travel->{no} %></a></td>
 							<td><a href="<%= $detail_link %>"><%= $travel->{from_name} %> → <%= $travel->{to_name} %></a></td>
-							<td><%= $travel->{rt_departure}->strftime('%H:%M') %>
-							% if ($travel->{sched_departure} != $travel->{rt_departure}) {
-								(+<%= ($travel->{rt_departure}->epoch - $travel->{sched_departure}->epoch) / 60 %>)
+							<td>
+							% if (param('cancelled')) {
+								%= $travel->{sched_departure}->strftime('%H:%M')
+							% }
+							% else {
+								<%= $travel->{rt_departure}->strftime('%H:%M') %>
+								% if ($travel->{sched_departure} != $travel->{rt_departure}) {
+									(+<%= ($travel->{rt_departure}->epoch - $travel->{sched_departure}->epoch) / 60 %>)
+								% }
 							% }
 							</td>
 							<td>
-							% if ($travel->{rt_arrival}->epoch == 0 and $travel->{sched_arrival}->epoch == 0) {
-								<i class="material-icons">timer_off</i>
-							% } else {
-								%= $travel->{rt_arrival}->strftime('%H:%M');
-								% if ($travel->{sched_arrival} != $travel->{rt_arrival}) {
-									(+<%= ($travel->{rt_arrival}->epoch - $travel->{sched_arrival}->epoch) / 60 %>)
+							% if (param('cancelled') and $travel->{sched_arrival}->epoch != 0) {
+								%= $travel->{sched_arrival}->strftime('%H:%M')
+							% }
+							% else {
+								% if ($travel->{rt_arrival}->epoch == 0 and $travel->{sched_arrival}->epoch == 0) {
+									<i class="material-icons">timer_off</i>
+								% } else {
+									%= $travel->{rt_arrival}->strftime('%H:%M');
+									% if ($travel->{sched_arrival} != $travel->{rt_arrival}) {
+										(+<%= ($travel->{rt_arrival}->epoch - $travel->{sched_arrival}->epoch) / 60 %>)
+									% }
 								% }
 							% }
 							</td>
diff --git a/templates/journey.html.ep b/templates/journey.html.ep
index 6c49538..abd9ba8 100644
--- a/templates/journey.html.ep
+++ b/templates/journey.html.ep
@@ -14,7 +14,12 @@
 	<div class="row">
 		<div class="col s12">
 			<p>
-				Fahrt von
+				% if ($journey->{cancelled}) {
+					Ausgefallene Fahrt von
+				% }
+				% else {
+					Fahrt von
+				% }
 				<b><%= $journey->{from_name} %></b>
 				nach
 				<b><%= $journey->{to_name} %></b>
@@ -34,7 +39,11 @@
 				<tr>
 					<th scope="row">Abfahrt</th>
 					<td>
-						% if ($journey->{rt_departure} != $journey->{sched_departure}) {
+						% if ($journey->{cancelled}) {
+							<i class="material-icons">cancel</i>
+							(Plan: <%= $journey->{sched_departure}->strftime('%H:%M'); %>)
+						% }
+						% elsif ($journey->{rt_departure} != $journey->{sched_departure}) {
 							%= $journey->{rt_departure}->strftime('%H:%M');
 							(+<%= ($journey->{rt_departure}->epoch - $journey->{sched_departure}->epoch) / 60 %>,
 							Plan: <%= $journey->{sched_departure}->strftime('%H:%M'); %>)
@@ -47,7 +56,13 @@
 				<tr>
 					<th scope="row">Ankunft</th>
 					<td>
-						% if ($journey->{rt_arrival}->epoch == 0 and $journey->{sched_arrival}->epoch == 0) {
+						% if ($journey->{cancelled}) {
+							<i class="material-icons">cancel</i>
+							% if ($journey->{sched_arrival}->epoch != 0) {
+								(Plan: <%= $journey->{sched_arrival}->strftime('%H:%M'); %>)
+							% }
+						% }
+						% elsif ($journey->{rt_arrival}->epoch == 0 and $journey->{sched_arrival}->epoch == 0) {
 							<i class="material-icons">timer_off</i>
 						% }
 						% elsif ($journey->{rt_arrival} != $journey->{sched_arrival}) {
-- 
cgit v1.2.3