diff --git a/app/Http/Controllers/Backend/Transport/TrainCheckinController.php b/app/Http/Controllers/Backend/Transport/TrainCheckinController.php index 050a9902d..809504dab 100644 --- a/app/Http/Controllers/Backend/Transport/TrainCheckinController.php +++ b/app/Http/Controllers/Backend/Transport/TrainCheckinController.php @@ -32,6 +32,7 @@ use Exception; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use InvalidArgumentException; use JetBrains\PhpStorm\ArrayShape; @@ -142,10 +143,28 @@ private static function createTrainCheckin( ->where('arrival_planned', $arrival) ->first(); + // In some rare occasions, the departure time of the origin station has a different timezone + // than the first stopover. In this case, we need to find it by comparing the departure time + // in a localtime string format. + if (empty($firstStop)) { + $firstStops = $trip->stopovers->where('train_station_id', $origin->id); + + if ($firstStops->count() > 1) { + $firstStop = $firstStops->filter(function(TrainStopover $stopover) use ($departure) { + return $stopover->departure_planned->format('H:i') === $departure->format('H:i'); + })->first(); + } else { + $firstStop = $firstStops->first(); + } + } + + if (empty($firstStop) || empty($lastStop)) { Log::debug('TrainCheckin: No stop found for origin or destination (HafasTrip ' . $trip->trip_id . ')'); Log::debug('TrainCheckin: Origin-ID: ' . $origin->id . ', Departure: ' . $departure->toIso8601String()); - Log::debug('TrainCheckin: Destination-ID: ' . $destination->id . ', Arrival: ' . $arrival->toIso8601String()); + Log::debug( + 'TrainCheckin: Destination-ID: ' . $destination->id . ', Arrival: ' . $arrival->toIso8601String() + ); throw new StationNotOnTripException(); }