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

73 lines
2.5 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\Request;
use Illuminate\Http\JsonResponse;
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 \MatanYadaev\EloquentSpatial\Objects\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 ($trip->status !== Trip::STATUS_IN_PROGRESS) {
return response()->json(['message' => 'Trip must be in progress to resume detour.'], 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 \MatanYadaev\EloquentSpatial\Objects\Point((float)$validated['lat'], (float)$validated['lng'], 4326)
: null,
'notes' => 'Finished unloading at dumpsite - Resuming route',
'created_at' => now(),
]);
$trip->update(['is_detouring' => false]);
return response()->json(['message' => 'Resuming route logged.']);
}
}