97 lines
3.0 KiB
PHP
97 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\Tms\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Tms\Http\Controllers\Concerns\AuthorizesTms;
|
|
use Modules\Tms\Services\TmsTrackingService;
|
|
use Modules\Tms\Utils\TmsUtil;
|
|
|
|
class TrackingController extends Controller
|
|
{
|
|
use AuthorizesTms;
|
|
|
|
public function __construct(
|
|
protected TmsUtil $tmsUtil,
|
|
protected TmsTrackingService $trackingService
|
|
) {}
|
|
|
|
public function index()
|
|
{
|
|
$this->authorizeTmsAccess('tms.tracking.view');
|
|
|
|
return view('tms::tracking.index');
|
|
}
|
|
|
|
public function liveData(Request $request)
|
|
{
|
|
$this->authorizeTmsAccess('tms.tracking.view');
|
|
$business_id = $this->tmsUtil->getBusinessId();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'drivers' => $this->trackingService->getLiveDrivers($business_id),
|
|
]);
|
|
}
|
|
|
|
public function shipmentMap($shipment_id)
|
|
{
|
|
$this->authorizeTmsAccess('tms.tracking.view');
|
|
$business_id = $this->tmsUtil->getBusinessId();
|
|
|
|
$track = $this->trackingService->getShipmentTrack($business_id, (int) $shipment_id);
|
|
|
|
return view('tms::tracking.shipment', compact('track'));
|
|
}
|
|
|
|
public function shipmentTrackData($shipment_id)
|
|
{
|
|
$this->authorizeTmsAccess('tms.tracking.view');
|
|
$business_id = $this->tmsUtil->getBusinessId();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $this->trackingService->getShipmentTrack($business_id, (int) $shipment_id),
|
|
]);
|
|
}
|
|
|
|
public function storeLocation(Request $request)
|
|
{
|
|
$this->authorizeTmsAccess('tms.tracking.report');
|
|
|
|
$business_id = $this->tmsUtil->getBusinessId();
|
|
|
|
$data = $request->validate([
|
|
'latitude' => 'required|numeric|between:-90,90',
|
|
'longitude' => 'required|numeric|between:-180,180',
|
|
'driver_id' => 'nullable|exists:tms_drivers,id',
|
|
'shipment_id' => 'nullable|exists:tms_shipments,id',
|
|
'vehicle_id' => 'nullable|exists:tms_vehicles,id',
|
|
'accuracy' => 'nullable|numeric',
|
|
'speed' => 'nullable|numeric',
|
|
'heading' => 'nullable|numeric',
|
|
]);
|
|
|
|
$driver_id = $data['driver_id'] ?? null;
|
|
if (! $driver_id) {
|
|
$driver = $this->trackingService->resolveDriverForUser($business_id, (int) auth()->id());
|
|
$driver_id = $driver?->id;
|
|
}
|
|
|
|
$this->trackingService->recordLocation(
|
|
$business_id,
|
|
(float) $data['latitude'],
|
|
(float) $data['longitude'],
|
|
$driver_id,
|
|
$data['shipment_id'] ?? null,
|
|
$data['vehicle_id'] ?? null,
|
|
isset($data['accuracy']) ? (float) $data['accuracy'] : null,
|
|
isset($data['speed']) ? (float) $data['speed'] : null,
|
|
isset($data['heading']) ? (float) $data['heading'] : null
|
|
);
|
|
|
|
return response()->json(['success' => true, 'msg' => __('tms::lang.location_recorded')]);
|
|
}
|
|
}
|