allerta-vvf/backend/app/Http/Controllers/StatsController.php

40 lines
1.2 KiB
PHP
Raw Normal View History

2023-12-31 15:30:39 +01:00
<?php
namespace App\Http\Controllers;
use App\Models\Service;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
class StatsController extends Controller
{
/**
2024-02-24 00:52:25 +01:00
* Get all services with all data, for usage in statistics page
2023-12-31 15:30:39 +01:00
*/
public function services(Request $request)
{
2024-01-07 18:43:52 +01:00
if(!$request->user()->hasPermission("services-read")) abort(401);
2023-12-31 15:30:39 +01:00
$query = Service::select('id','code','chief_id','type_id','place_id','notes','start','end','added_by_id','created_at')
->with('place')
->with('drivers:id')
->with('crew:id')
->orderBy('start', 'desc');
if($request->has('from')) {
try {
$from = Carbon::parse($request->input('from'));
$query->whereDate('start', '>=', $from->toDateString());
} catch (\Carbon\Exceptions\InvalidFormatException $e) { }
}
if($request->has('to')) {
try {
$to = Carbon::parse($request->input('to'));
$query->whereDate('start', '<=', $to->toDateString());
} catch (\Carbon\Exceptions\InvalidFormatException $e) { }
}
return response()->json(
$query->get()
);
}
}