Files
Verde-Web/app/Http/Controllers/Api/V1/Driver/TripDetourController.php

77 lines
2.6 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1\Driver;
use App\Http\Controllers\Controller;
use App\Models\Trip;
use App\Models\TripTimelineEvent;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use MatanYadaev\EloquentSpatial\Objects\Point;
class TripDetourController extends Controller
{
/**
* Log that the truck is full and heading to the dumpsite.
*/
public function start(Request $request, Trip $trip): JsonResponse
{
if ($trip->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.']);
}
}