status !== Trip::STATUS_IN_PROGRESS) { return response()->json(['message' => 'Trip must be in progress to detour.'], 400); } $validated = $request->validate([ 'lat' => 'nullable|numeric', 'lng' => 'nullable|numeric', ]); $trip->timelineEvents()->create([ 'event_type' => TripTimelineEvent::TYPE_DETOUR_TO_DUMPSITE, 'event_at' => now(), 'recorded_by_user_id' => $request->user()->id, 'coordinates' => isset($validated['lat'], $validated['lng']) ? new Point((float) $validated['lat'], (float) $validated['lng'], 4326) : null, 'notes' => 'Truck full - Detouring to dumpsite', 'created_at' => now(), ]); $trip->update(['is_detouring' => true]); return response()->json(['message' => 'Detour to dumpsite logged.']); } /** * Log that the truck has unloaded and is resuming its route. */ public function resume(Request $request, Trip $trip): JsonResponse { if (!in_array($trip->status, [Trip::STATUS_IN_PROGRESS, Trip::STATUS_AT_DUMPSITE], true)) { return response()->json(['message' => 'Trip must be in progress or at dumpsite to resume.'], 400); } $validated = $request->validate([ 'lat' => 'nullable|numeric', 'lng' => 'nullable|numeric', ]); $trip->timelineEvents()->create([ 'event_type' => TripTimelineEvent::TYPE_RESUMED_FROM_DETOUR, 'event_at' => now(), 'recorded_by_user_id' => $request->user()->id, 'coordinates' => isset($validated['lat'], $validated['lng']) ? new Point((float) $validated['lat'], (float) $validated['lng'], 4326) : null, 'notes' => 'Finished unloading at dumpsite - Resuming route', 'created_at' => now(), ]); $trip->update([ 'is_detouring' => false, 'status' => Trip::STATUS_IN_PROGRESS, ]); return response()->json(['message' => 'Resuming route logged.']); } }