create new report
This commit is contained in:
103
app/Services/Cartables/Mission/ReportService.php
Normal file
103
app/Services/Cartables/Mission/ReportService.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cartables\Mission;
|
||||
|
||||
use App\Exceptions\ProhibitedAction;
|
||||
use App\Facades\DataTable\DataTableFacade;
|
||||
use App\Models\Mission;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ReportService
|
||||
{
|
||||
public function dataTable(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
throw_if(is_null($user->edarate_shahri_id), new ProhibitedAction('اداره ای برای شما در سامانه ثبت نشده است!'));
|
||||
|
||||
$query = Mission::query()->where('edare_shahri_id', '=', $user->edarate_shahri_id);
|
||||
|
||||
return DataTableFacade::run(
|
||||
$query,
|
||||
$request,
|
||||
allowedFilters: ['*'],
|
||||
allowedSortings: ['*'],
|
||||
);
|
||||
}
|
||||
|
||||
public function countryActivity(Request $request): array
|
||||
{
|
||||
$from = null;
|
||||
$to = null;
|
||||
|
||||
$filters = json_decode($request->filters ?? '[]', true);
|
||||
|
||||
foreach ($filters as $filter) {
|
||||
if (($filter['fn'] ?? null) === 'between' && ($filter['id'] ?? null) === 'created_at') {
|
||||
$from = ($filter['value'][0] ?? null) ? ($filter['value'][0] . ' 00:00:00') : null;
|
||||
$to = ($filter['value'][1] ?? null) ? ($filter['value'][1] . ' 23:59:59') : null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$from ??= '1970-01-01 00:00:00';
|
||||
$to ??= now()->endOfDay()->toDateTimeString();
|
||||
|
||||
$sql = "
|
||||
WITH r AS (
|
||||
SELECT
|
||||
p.id,
|
||||
p.name_fa AS province_name,
|
||||
|
||||
COUNT(DISTINCT CASE
|
||||
WHEN mv.type = 1
|
||||
AND mv.created_at BETWEEN :from AND :to
|
||||
THEN mv.id
|
||||
END) AS no_mission_count,
|
||||
|
||||
COUNT(DISTINCT CASE
|
||||
WHEN m.mission_duration != m.in_area_duration
|
||||
AND m.created_at BETWEEN :from AND :to
|
||||
THEN m.id
|
||||
END) AS out_of_area_count
|
||||
|
||||
FROM provinces p
|
||||
LEFT JOIN cmms_machines cm
|
||||
ON cm.province_id = p.id
|
||||
LEFT JOIN mission_violations mv
|
||||
ON TRIM(cm.machine_code) = TRIM(mv.machine_code)
|
||||
LEFT JOIN missions m
|
||||
ON m.province_id = p.id
|
||||
|
||||
GROUP BY p.id, p.name_fa
|
||||
)
|
||||
|
||||
SELECT
|
||||
'کل کشور' AS province_name,
|
||||
SUM(no_mission_count) AS no_mission_count,
|
||||
SUM(out_of_area_count) AS out_of_area_count
|
||||
FROM r
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
province_name, no_mission_count, out_of_area_count
|
||||
FROM r
|
||||
|
||||
ORDER BY
|
||||
CASE WHEN province_name = 'کل کشور' THEN 0 ELSE 1 END,
|
||||
province_name
|
||||
";
|
||||
|
||||
return DB::select($sql, [
|
||||
'from' => $from,
|
||||
'to' => $to,
|
||||
]);
|
||||
}
|
||||
|
||||
public function provinceActivity(Request $request, int $info_id): array
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user