133 lines
4.4 KiB
PHP
133 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cartables\Mission\Report;
|
|
|
|
use App\Facades\DataTable\DataTableFacade;
|
|
use App\Models\CMMSMachine;
|
|
use App\Models\Mission;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ReportMachineService
|
|
{
|
|
public function dataTableMachinesActivity(Request $request, int $machine_id): array
|
|
{
|
|
return DataTableFacade::run(
|
|
Mission::query()->where('machine_id', '=', $machine_id),
|
|
$request,
|
|
allowedFilters: ['*'],
|
|
allowedSortings: ['*'],
|
|
allowedSelects: [
|
|
'id', 'province_id', 'province_name', 'city_id', 'city_name',
|
|
'end_km', 'km', 'driver_name', 'state_id', 'station_name'
|
|
]
|
|
);
|
|
}
|
|
public function machinesActivity(Request $request): array
|
|
{
|
|
$query = Mission::query()
|
|
->join('cmms_machines as cm', 'cm.id', '=', 'missions.machine_id')
|
|
->where('state_id', '=', 4)
|
|
->when($request->province_id, function ($q) use ($request) {
|
|
$q->where('missions.province_id', $request->province_id);
|
|
})
|
|
->selectRaw(
|
|
'cm.car_name,
|
|
missions.machine_code,
|
|
missions.machine_id,
|
|
COUNT(*) as missions,
|
|
SUM(missions.end_km - missions.km) as func'
|
|
)
|
|
->groupBy('missions.machine_id')
|
|
->orderByRaw('COUNT(*) DESC');
|
|
|
|
$fields = ['machine_code','machine_id','cm.car_name','missions', 'func'];
|
|
return DataTableFacade::run(
|
|
$query,
|
|
$request,
|
|
allowedFilters: $fields,
|
|
allowedSortings: $fields,
|
|
);
|
|
}
|
|
|
|
public function dataTableMachineTypesActivity(Request $request,int $car_type): array
|
|
{
|
|
$machineIds = CMMSMachine::query()->where('car_type', '=', $request->query('car_type'))->pluck('machine_id');
|
|
return DataTableFacade::run(
|
|
Mission::query()->whereIn('machine_id', $machineIds),
|
|
$request,
|
|
allowedFilters: ['*'],
|
|
allowedSortings: ['*'],
|
|
allowedSelects: [
|
|
'id', 'province_id', 'province_name', 'city_id', 'city_name',
|
|
'end_km', 'km', 'driver_name', 'state_name', 'station_name'
|
|
]
|
|
);
|
|
}
|
|
|
|
public function machineTypesActivity(Request $request): array
|
|
{
|
|
$query = Mission::query()
|
|
->join('cmms_machines as cm', 'cm.id', '=', 'missions.machine_id')
|
|
->where('missions.state_id','=', 4)
|
|
->when($request->province_id, function ($q) use ($request) {
|
|
$q->where('missions.province_id', $request->province_id);
|
|
})
|
|
->selectRaw(
|
|
'cm.car_type,
|
|
COUNT(*) as missions,
|
|
SUM(missions.end_km - missions.km) as func'
|
|
)
|
|
->groupBy('cm.car_type')
|
|
->orderByRaw('COUNT(*) DESC');
|
|
|
|
$fields = ['car_type','missions', 'func'];
|
|
|
|
return DataTableFacade::run(
|
|
$query,
|
|
$request,
|
|
allowedFilters: $fields,
|
|
allowedSortings: $fields,
|
|
);
|
|
}
|
|
|
|
public function dataTableMachineDriversActivity(Request $request,int $driver_id): array
|
|
{
|
|
return DataTableFacade::run(
|
|
Mission::query()->where('driver_id', '=', $driver_id),
|
|
$request,
|
|
allowedFilters: ['*'],
|
|
allowedSortings: ['*'],
|
|
allowedSelects: [
|
|
'id', 'province_id', 'province_name', 'city_id', 'city_name',
|
|
'end_km', 'km', 'driver_name', 'state_id', 'station_name'
|
|
]
|
|
);
|
|
}
|
|
|
|
public function machineDriversActivity(Request $request): array
|
|
{
|
|
$query = Mission::query()
|
|
->selectRaw(
|
|
'driver_id,
|
|
driver_name,
|
|
COUNT(*) as missions,
|
|
SUM(end_km - km) as func'
|
|
)
|
|
->where('state_id', '=', 4)
|
|
->when($request->province_id, function ($query) use ($request) {
|
|
$query->where('province_id', '=', $request->province_id);
|
|
})
|
|
->groupBy('driver_id')
|
|
->orderByRaw('COUNT(*) DESC');
|
|
|
|
$fields = ['driver_id','driver_name','missions', 'func'];
|
|
|
|
return DataTableFacade::run(
|
|
$query,
|
|
$request,
|
|
allowedFilters: $fields,
|
|
allowedSortings: $fields,
|
|
);
|
|
}
|
|
}
|