Merge branch 'feature/ReportDataTables' into 'develop'

Feature/report data tables

See merge request witelgroup/rms_v2!101
This commit is contained in:
2025-04-22 05:39:25 +00:00
22 changed files with 180 additions and 233 deletions

View File

@@ -19,13 +19,13 @@ class CountryActivityReport implements FromView, WithEvents, ShouldAutoSize, Wit
public function view(): View public function view(): View
{ {
$data = $this->data; $data = $this->data;
$existedProvinces = array_keys($data['activities']); $existedProvinces = array_keys($data);
foreach (\App\Models\Province::all() as $value) { foreach (\App\Models\Province::all() as $value) {
$provinceId = $value->id; $provinceId = $value->id;
if (!array_key_exists($provinceId, $existedProvinces)) { if (!array_key_exists($provinceId, $existedProvinces)) {
$data['activities'][] = (object) [ $data[] = (object) [
'province_id' => $provinceId, 'province_id' => $provinceId,
'name_fa' => $value->name_fa, 'name_fa' => $value->name_fa,
'sum' => 0, 'sum' => 0,
@@ -37,9 +37,7 @@ class CountryActivityReport implements FromView, WithEvents, ShouldAutoSize, Wit
} }
return view('v3.Reports.SafetyAndPrivacy.CountryActivityReport', [ return view('v3.Reports.SafetyAndPrivacy.CountryActivityReport', [
'data' => $data['activities'], 'data' => $data,
'fromDate' => $data['fromDate'],
'toDate' => $data['toDate'],
]); ]);
} }

View File

@@ -21,14 +21,14 @@ class ProvinceActivityReport implements FromView, WithEvents, ShouldAutoSize, Wi
public function view(): View public function view(): View
{ {
$data = $this->data; $data = $this->data;
$existedCities = array_keys($data['activities']); $existedCities = array_keys($data);
$edarat = EdarateShahri::query()->where('province_id', '=', $this->request->province_id)->get(['id', 'name_fa']); $edarat = EdarateShahri::query()->where('province_id', '=', $this->request->province_id)->get(['id', 'name_fa']);
foreach ($edarat as $value) { foreach ($edarat as $value) {
$edareId = $value->id; $edareId = $value->id;
if (!array_key_exists($edareId, $existedCities)) { if (!array_key_exists($edareId, $existedCities)) {
$data['activities'][] = (object) [ $data[] = (object) [
'edare_shahri_id' => $edareId, 'edare_shahri_id' => $edareId,
'name_fa' => $value->name_fa, 'name_fa' => $value->name_fa,
'sum' => 0, 'sum' => 0,
@@ -40,9 +40,7 @@ class ProvinceActivityReport implements FromView, WithEvents, ShouldAutoSize, Wi
} }
return view('v3.Reports.SafetyAndPrivacy.ProvinceActivityReport', [ return view('v3.Reports.SafetyAndPrivacy.ProvinceActivityReport', [
'data' => $data['activities'], 'data' => $data,
'fromDate' => $data['fromDate'],
'toDate' => $data['toDate'],
]); ]);
} }

View File

@@ -28,7 +28,8 @@ class DataTable
array $allowedFilters = [], array $allowedFilters = [],
array $allowedRelations = [], array $allowedRelations = [],
array $allowedSortings = [], array $allowedSortings = [],
array $allowedSelects = [] array $allowedSelects = [],
array $allowedGroupBy = []
): array ): array
{ {
@@ -43,7 +44,8 @@ class DataTable
$sorting, $sorting,
$rels, $rels,
$allowedFilters, $allowedFilters,
$allowedSortings $allowedSortings,
$allowedGroupBy
); );
$query = $this->makeQueryFromModel($mixed); $query = $this->makeQueryFromModel($mixed);
@@ -52,7 +54,8 @@ class DataTable
->setAllowedFilters($allowedFilters) ->setAllowedFilters($allowedFilters)
->setAllowedRelations($allowedRelations) ->setAllowedRelations($allowedRelations)
->setAllowedSortings($allowedSortings) ->setAllowedSortings($allowedSortings)
->setAllowedSelects($allowedSelects); ->setAllowedSelects($allowedSelects)
->setAllowedGroupBy($allowedGroupBy);
return $dataTableService->getData(); return $dataTableService->getData();
} }

View File

@@ -104,4 +104,9 @@ class File
fclose($file); fclose($file);
return trim($output); return trim($output);
} }
public function deleteDirectory(string $path, string $disk = 'public'): void
{
Storage::disk($disk)->deleteDirectory($path);
}
} }

View File

@@ -202,7 +202,7 @@ class AccidentReceiptController extends Controller
public function destroy(Accident $accident): JsonResponse public function destroy(Accident $accident): JsonResponse
{ {
FileFacade::delete('storage/receipts_files/'.$accident->id); FileFacade::deleteDirectory('receipts_files/'.$accident->id);
DB::transaction(function () use ($accident) { DB::transaction(function () use ($accident) {
@@ -240,9 +240,9 @@ class AccidentReceiptController extends Controller
DB::transaction(function () use ($request, $accident) { DB::transaction(function () use ($request, $accident) {
$accident->update([ $accident->update([
'deposit_insurance_image' => $request->deposit_insurance_image != null ? FileFacade::save($request->deposit_insurance_image, "receipts_files/{$accident->id}/deposit_insurance/") : null, 'deposit_insurance_image' => $request->has('deposit_insurance_image') ? FileFacade::save($request->deposit_insurance_image, "receipts_files/{$accident->id}/deposit_insurance") : null,
'deposit_insurance_amount' => $request->deposit_insurance_amount ?? 0, 'deposit_insurance_amount' => $request->deposit_insurance_amount ?? 0,
'deposit_daghi_image' => $request->deposit_insurance_image != null ? FileFacade::save($request->deposit_daghi_image, "receipts_files/{$accident->id}/deposit_daghi/") : null, 'deposit_daghi_image' => $request->has('deposit_daghi_image') ? FileFacade::save($request->deposit_daghi_image, "receipts_files/{$accident->id}/deposit_daghi") : null,
'deposit_daghi_amount' => $request->deposit_daghi_amount ?? 0, 'deposit_daghi_amount' => $request->deposit_daghi_amount ?? 0,
'status' => AccidentStates::SABT_FISH->value, 'status' => AccidentStates::SABT_FISH->value,
'status_fa' => AccidentStates::name(AccidentStates::SABT_FISH->value), 'status_fa' => AccidentStates::name(AccidentStates::SABT_FISH->value),

View File

@@ -22,7 +22,7 @@ class SafetyAndPrivacyReportController extends Controller
{ {
$data = $safetyAndPrivacyTableService->countryActivity($request); $data = $safetyAndPrivacyTableService->countryActivity($request);
return $this->successResponse([ return $this->successResponse([
'activities' => $data['activities'], 'activities' => $data,
'provinces' => Province::all(['id', 'name_fa']) 'provinces' => Province::all(['id', 'name_fa'])
]); ]);
} }
@@ -31,7 +31,7 @@ class SafetyAndPrivacyReportController extends Controller
{ {
$data = $safetyAndPrivacyTableService->provinceActivity($request); $data = $safetyAndPrivacyTableService->provinceActivity($request);
return $this->successResponse([ return $this->successResponse([
'activities' => $data['activities'], 'activities' => $data,
'edarateShahri' => EdarateShahri::query()->where('province_id', '=', $request->province_id)->get(['id', 'name_fa']), 'edarateShahri' => EdarateShahri::query()->where('province_id', '=', $request->province_id)->get(['id', 'name_fa']),
]); ]);
} }

View File

@@ -242,7 +242,7 @@ class RoadMaintenanceStationController extends Controller
public function images(RahdariPoint $rahdariPoint): JsonResponse public function images(RahdariPoint $rahdariPoint): JsonResponse
{ {
return $this->successResponse($rahdariPoint->files()->get(['path'])); return $this->successResponse($rahdariPoint->files()->orderBy('id')->get(['path']));
} }
public function map(Request $request): JsonResponse public function map(Request $request): JsonResponse

View File

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\V3\Dashboard;
use App\Exports\V3\RoadObservation\ComplaintsReport; use App\Exports\V3\RoadObservation\ComplaintsReport;
use App\Exports\V3\RoadObservation\OperatorCartableReport; use App\Exports\V3\RoadObservation\OperatorCartableReport;
use App\Exports\V3\RoadObservation\SupervisorCartableReport; use App\Exports\V3\RoadObservation\SupervisorCartableReport;
use App\Facades\DataTable\DataTableFacade;
use App\Facades\File\FileFacade; use App\Facades\File\FileFacade;
use App\Facades\Sms\Sms; use App\Facades\Sms\Sms;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
@@ -24,6 +25,7 @@ use Hekmatinasser\Verta\Verta;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Services\Cartables\RoadObservationTableService; use App\Services\Cartables\RoadObservationTableService;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use SoapFault; use SoapFault;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
@@ -117,8 +119,8 @@ class RoadObservationController extends Controller
$roadObserved->files()->where('path', 'like', '%_image_before%')->delete(); $roadObserved->files()->where('path', 'like', '%_image_before%')->delete();
$roadObserved->files()->where('path', 'like', '%_image_after%')->delete(); $roadObserved->files()->where('path', 'like', '%_image_after%')->delete();
$image_before = FileFacade::save($request->image_before, "road_observeds/{$roadObserved->id}/"); $image_before = FileFacade::save($request->image_before, "road_observeds/{$roadObserved->id}");
$image_after = FileFacade::save($request->image_after, "road_observeds/{$roadObserved->id}/"); $image_after = FileFacade::save($request->image_after, "road_observeds/{$roadObserved->id}");
$files_path[0]['path'] = $image_before; $files_path[0]['path'] = $image_before;
$files_path[1]['path'] = $image_after; $files_path[1]['path'] = $image_after;
@@ -135,20 +137,15 @@ class RoadObservationController extends Controller
$roadObservedHistoryData['new_files'] = $files; $roadObservedHistoryData['new_files'] = $files;
} }
$roadObserved->problemHistories()->create($roadObservedHistoryData); DB::transaction(function () use ($roadObserved, $roadObservedHistoryData, $roadObservedData, $request, $nikarayanService) {
$roadObserved->update($roadObservedData); $roadObserved->problemHistories()->create($roadObservedHistoryData);
$roadObserved->update($roadObservedData);
auth()->user()->addActivityComplete(1142); auth()->user()->addActivityComplete(1142);
try {
$nikarayanService->updateRoadObservedInfoByNikarayan($request, $roadObserved, json_encode($roadObservedHistoryData['new_files'])); $nikarayanService->updateRoadObservedInfoByNikarayan($request, $roadObserved, json_encode($roadObservedHistoryData['new_files']));
}
catch (SoapFault $e) {
$msg = "error in send to nikarayan at" . date("Y-m-d H:i:s") . "\n"; });
$msg .= $e->getMessage();
Sms::sendSms(env('RMS_CTO_PHONE_NUMBER'), $msg);
}
return $this->successResponse(); return $this->successResponse();
} }
@@ -182,7 +179,7 @@ class RoadObservationController extends Controller
if ($roadObserved->image_before) { if ($roadObserved->image_before) {
FileFacade::delete($roadObserved->image_before, true); FileFacade::delete($roadObserved->image_before, true);
} }
$image_before = FileFacade::save($request->image_before, "road_observeds/{$roadObserved->id}/"); $image_before = FileFacade::save($request->image_before, "road_observeds/{$roadObserved->id}");
$roadObservedData['image_before'] = $image_before; $roadObservedData['image_before'] = $image_before;
$files_path[0]['path'] = $image_before; $files_path[0]['path'] = $image_before;
} }
@@ -191,7 +188,7 @@ class RoadObservationController extends Controller
if ($roadObserved->image_after) { if ($roadObserved->image_after) {
FileFacade::delete($roadObserved->image_after, true); FileFacade::delete($roadObserved->image_after, true);
} }
$image_after = FileFacade::save($request->image_after, "road_observeds/{$roadObserved->id}/"); $image_after = FileFacade::save($request->image_after, "road_observeds/{$roadObserved->id}");
$roadObservedData['image_after'] = $image_after; $roadObservedData['image_after'] = $image_after;
$files_path[1]['path'] = $image_after; $files_path[1]['path'] = $image_after;
} }
@@ -206,20 +203,14 @@ class RoadObservationController extends Controller
$roadObservedHistoryData['new_files'] = $files; $roadObservedHistoryData['new_files'] = $files;
} }
$roadObserved->problemHistories()->create($roadObservedHistoryData); DB::transaction(function () use ($roadObserved, $roadObservedHistoryData, $roadObservedData, $request, $nikarayanService) {
$roadObserved->update($roadObservedData); $roadObserved->problemHistories()->create($roadObservedHistoryData);
$roadObserved->update($roadObservedData);
auth()->user()->addActivityComplete(1142); auth()->user()->addActivityComplete(1142);
try {
$nikarayanService->updateRoadObservedInfoByNikarayan($request, $roadObserved, json_encode($roadObservedHistoryData['new_files'])); $nikarayanService->updateRoadObservedInfoByNikarayan($request, $roadObserved, json_encode($roadObservedHistoryData['new_files']));
} });
catch (SoapFault $e) {
$msg = "error in send to nikarayan at" . date("Y-m-d H:i:s") . "\n";
$msg .= $e->getMessage();
Sms::sendSms(env('RMS_CTO_PHONE_NUMBER'), $msg);
}
return $this->successResponse(); return $this->successResponse();
} }
@@ -305,4 +296,15 @@ class RoadObservationController extends Controller
return $this->successResponse(); return $this->successResponse();
} }
public function map(Request $request): JsonResponse
{
return response()->json(DataTableFacade::run(
RoadObserved::query(),
$request,
allowedFilters: ['*'],
allowedSortings: ['*'],
allowedSelects: ['id', 'lat', 'lng', 'status']
));
}
} }

View File

@@ -24,16 +24,23 @@ use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Throwable;
class SafetyAndPrivacyController extends Controller class SafetyAndPrivacyController extends Controller
{ {
use ApiResponse; use ApiResponse;
/**
* @throws Throwable
*/
public function index(Request $request, SafetyAndPrivacyTableService $safetyAndPrivacyTableService): JsonResponse public function index(Request $request, SafetyAndPrivacyTableService $safetyAndPrivacyTableService): JsonResponse
{ {
return response()->json($safetyAndPrivacyTableService->datatable($request)); return response()->json($safetyAndPrivacyTableService->datatable($request));
} }
/**
* @throws Throwable
*/
public function excelReport(Request $request, SafetyAndPrivacyTableService $safetyAndPrivacyTableService): BinaryFileResponse public function excelReport(Request $request, SafetyAndPrivacyTableService $safetyAndPrivacyTableService): BinaryFileResponse
{ {
$name = 'گزارش از نگهداری حریم راه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx'; $name = 'گزارش از نگهداری حریم راه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';

View File

@@ -144,56 +144,44 @@ class NotificationController extends Controller
'supervise_cnt' => 0 'supervise_cnt' => 0
]; ];
$conditions = [];
$complaintConditions = [
['rms_status', '=', 0],
['edarate_shahri_id', '!=', null]
];
if ($user->hasPermissionTo('show-fast-react')) { if ($user->hasPermissionTo('show-fast-react')) {
$conditions[] = ['status', '=', 2]; $road_observations['supervise_cnt'] = RoadObserved::query()->where([
['rms_status', '<>', 0],
['status', '=', 0]
])->count();
$road_observations['complaint_cnt'] = RoadObserved::query()->where('rms_status', '=', 0)->count();
} }
elseif ($user->hasPermissionTo('show-fast-react-province')) { elseif ($user->hasPermissionTo('show-fast-react-province')) {
throw_if(is_null($user->province_id), new ProhibitedAction('استانی برای شما در سامانه ثبت نشده است!')); throw_if(is_null($user->province_id), new ProhibitedAction('استانی برای شما در سامانه ثبت نشده است!'));
$conditions = [
['status', '=', 2], $road_observations['supervise_cnt'] = RoadObserved::query()->where([
['rms_province_id', '=', $user->province_id] ['rms_status', '<>', 0],
]; ['status', '=', 0],
$complaintConditions[] = ['province_id', '=', $user->province_id]; ['province_id', '=', $user->province_id]
])->count();
$road_observations['complaint_cnt'] = RoadObserved::query()->where([
['rms_status', '=', 0],
['province_id', '=', $user->province_id]
])->count();
} }
elseif ($user->hasPermissionTo('show-fast-react-edarate-shahri')) { elseif ($user->hasPermissionTo('show-fast-react-edarate-shahri')) {
throw_if(is_null($user->edarate_shahri_id), new ProhibitedAction('اداره ای برای شما در سامانه ثبت نشده است!')); throw_if(is_null($user->edarate_shahri_id), new ProhibitedAction('اداره ای برای شما در سامانه ثبت نشده است!'));
$conditions = [
$road_observations['operation_cnt'] = RoadObserved::query()->where([
['rms_status', '<>', 0],
['status', '=', 2], ['status', '=', 2],
['edarate_shahri_id', '=', $user->edarate_shahri_id] ['edarate_shahri_id', '=', $user->edarate_shahri_id]
]; ])->count();
$complaintConditions[] = ['edarate_shahri_id', '=', $user->edarate_shahri_id];
$road_observations['complaint_cnt'] = RoadObserved::query()->where([
['rms_status', '=', 0],
['edarate_shahri_id', '=', $user->edarate_shahri_id]
])->count();
} }
if (!empty($conditions)) { $road_observations['total'] += $road_observations['operation_cnt'] + $road_observations['complaint_cnt'] + $road_observations['supervise_cnt'];
$road_observations['operation_cnt'] = RoadObserved::query()->where($conditions)->count();
$road_observations['complaint_cnt'] = RoadObserved::query()->where($complaintConditions)->count();
$road_observations['total'] += $road_observations['operation_cnt'] + $road_observations['complaint_cnt'];
}
$superviseConditions = [
['status', '=', 0]
];
if ($user->hasPermissionTo('supervise-fast-react')) {
$road_observations['supervise_cnt'] = RoadObserved::query()->where($superviseConditions)->count();
$road_observations['complaint_cnt'] = RoadObserved::query()->where($complaintConditions)->count();
$road_observations['total'] += $road_observations['supervise_cnt'] + $road_observations['complaint_cnt'];
}
elseif ($user->hasPermissionTo('supervise-fast-react-province')) {
throw_if(is_null($user->province_id), new ProhibitedAction('استانی برای شما در سامانه ثبت نشده است!'));
$superviseConditions[] = ['province_id', '=', $user->province_id];
$complaintConditions[] = ['province_id', '=', $user->province_id];
$road_observations['supervise_cnt'] = RoadObserved::query()->where($superviseConditions)->count();
$road_observations['complaint_cnt'] = RoadObserved::query()->where($complaintConditions)->count();
$road_observations['total'] += $road_observations['supervise_cnt'] + $road_observations['complaint_cnt'];
}
return $road_observations; return $road_observations;
} }

View File

@@ -13,6 +13,11 @@ class RahdariPointHistory extends Model
*/ */
protected $fillable = [ protected $fillable = [
'user_id', 'user_id',
'previous_province_id',
'previous_province_name',
'previous_city_id',
'previous_city_name',
'previous_name',
'previous_status', 'previous_status',
'previous_type', 'previous_type',
'previous_team_num', 'previous_team_num',

View File

@@ -108,14 +108,20 @@ class RoadObserved extends Model
protected function imageAfter(): Attribute protected function imageAfter(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn($value) => $value == null ? null : Storage::disk('public')->url($value) get: fn($value) => $value == null ? null :
(filter_var($value, FILTER_VALIDATE_URL)
? $value
: Storage::disk('public')->url($value))
); );
} }
protected function imageBefore(): Attribute protected function imageBefore(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn($value) => $value == null ? null : Storage::disk('public')->url($value) get: fn($value) => $value == null ? null :
(filter_var($value, FILTER_VALIDATE_URL)
? $value
: Storage::disk('public')->url($value))
); );
} }
} }

View File

@@ -53,7 +53,7 @@ class RoadItemTableService
$allowedSortings = ['*']; $allowedSortings = ['*'];
$query = RoadItemsProject::query() $query = RoadItemsProject::query()
->select(['id', 'supervisor_description', 'item', 'item_fa', 'sub_item', 'sub_item_fa', 'sub_item_data', 'unit_fa', 'start_lat', 'start_lng', ->select(['id', 'province_fa', 'edarat_name', 'supervisor_description', 'item', 'item_fa', 'sub_item', 'sub_item_fa', 'sub_item_data', 'unit_fa', 'start_lat', 'start_lng',
'end_lat', 'end_lng', 'activity_date_time', 'created_at', 'status_fa', 'status']) 'end_lat', 'end_lng', 'activity_date_time', 'created_at', 'status_fa', 'status'])
->where('is_new', 1) ->where('is_new', 1)
->where('user_id', auth()->user()->id) ->where('user_id', auth()->user()->id)

View File

@@ -2,17 +2,22 @@
namespace App\Services\Cartables; namespace App\Services\Cartables;
use App\Exceptions\ProhibitedAction;
use App\Facades\DataTable\DataTableFacade; use App\Facades\DataTable\DataTableFacade;
use App\Http\Traits\ApiResponse; use App\Http\Traits\ApiResponse;
use App\Models\Province; use App\Models\Province;
use App\Models\SafetyAndPrivacy; use App\Models\SafetyAndPrivacy;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Throwable;
class SafetyAndPrivacyTableService class SafetyAndPrivacyTableService
{ {
use ApiResponse; use ApiResponse;
/**
* @throws Throwable
*/
public function dataTable(Request $request, $loadRelations = false) public function dataTable(Request $request, $loadRelations = false)
{ {
$user = auth()->user(); $user = auth()->user();
@@ -27,16 +32,12 @@ class SafetyAndPrivacyTableService
$query = SafetyAndPrivacy::query()->select($fields); $query = SafetyAndPrivacy::query()->select($fields);
} }
elseif ($user->hasPermissionTo('show-safety-and-privacy-operator-cartable-province')) { elseif ($user->hasPermissionTo('show-safety-and-privacy-operator-cartable-province')) {
if (is_null($user->province_id)) { throw_if(is_null($user->province_id), new ProhibitedAction('استانی برای شما در سامانه ثبت نشده است!'));
return $this->errorResponse('استانی برای شما در سامانه ثبت نشده است!');
}
$query = SafetyAndPrivacy::query()->where('province_id', '=', $user->province_id)->select($fields); $query = SafetyAndPrivacy::query()->where('province_id', '=', $user->province_id)->select($fields);
} }
elseif ($user->hasPermissionTo('show-safety-and-privacy-operator-cartable-edarate-shahri')) { elseif ($user->hasPermissionTo('show-safety-and-privacy-operator-cartable-edarate-shahri')) {
if (is_null($user->edarate_shahri_id)) { throw_if(is_null($user->edarate_shahri_id), new ProhibitedAction('اداره ای برای شما در سامانه ثبت نشده است!'));
return $this->errorResponse('اداره ای برای شما در سامانه ثبت نشده است!');
}
$query = SafetyAndPrivacy::query()->where('edare_shahri_id', '=', $user->edarate_shahri_id); $query = SafetyAndPrivacy::query()->where('edare_shahri_id', '=', $user->edarate_shahri_id);
} }
@@ -51,74 +52,45 @@ class SafetyAndPrivacyTableService
public function countryActivity(Request $request): array public function countryActivity(Request $request): array
{ {
$fromDate = $request->from_date ? $request->from_date . ' 00:00:00' : now()->startOfDay()->toDateTimeString();
$toDate = $request->date_to ? $request->date_to . ' 23:59:59' : now()->endOfDay()->toDateTimeString();
$axisTypeId = $request->axis_type_id;
$activities = []; $activities = [];
$countryQuery = SafetyAndPrivacy::query()->selectRaw('COUNT(*) as qty,info_id, step');
$country = SafetyAndPrivacy::query() $country = DataTableFacade::run(
->selectRaw('COUNT(*) as qty,info_id, step') $countryQuery,
->whereBetween('created_at', [$fromDate, $toDate]) $request,
->when($axisTypeId, function ($query) use ($axisTypeId) { allowedFilters: ['*'],
$query->where('axis_type_id', '=', $axisTypeId); allowedSortings: ['*'],
}) allowedGroupBy: ['info_id', 'step']
->groupBy('info_id', 'step') );
->get();
// $country = DB::select(
// "SELECT COUNT(*) as qty,info_id, step
// FROM safety_and_privacy where created_at BETWEEN '".$fromDate."' and '".$toDate."'
// GROUP BY info_id,step"
// );
$countryTotalSum = 0; $countryTotalSum = 0;
$countryData = [ $countryData = [
'name_fa' => 'کشوری', 'name_fa' => 'کشوری',
'province_id' => -1, 'province_id' => -1,
'sum' => 0, 'sum' => 0,
'89' => [ '89' => ['1' => 0, '2' => 0, '3' => 0,],
'1' => 0, '90' => ['1' => 0, '2' => 0, '3' => 0,],
'2' => 0, '91' => ['1' => 0, '2' => 0, '3' => 0,],
'3' => 0,
],
'90' => [
'1' => 0,
'2' => 0,
'3' => 0,
],
'91' => [
'1' => 0,
'2' => 0,
'3' => 0,
],
]; ];
foreach ($country as $key => $value) { foreach ($country['data'] as $key => $value) {
$countryTotalSum += $value->qty; $countryTotalSum += $value->qty;
$countryData[$value->info_id][$value->step] = $value->qty; $countryData[$value->info_id][$value->step] = $value->qty;
$countryData["sum"] = $countryTotalSum; $countryData["sum"] = $countryTotalSum;
} }
$activities[] = (object) $countryData; $activities[] = (object) $countryData;
$provinceQuery = SafetyAndPrivacy::query()->selectRaw('province_id, province_fa, COUNT(*) as qty,info_id, step');
$provinces = DataTableFacade::run(
$provinceQuery,
$request,
allowedFilters: ['*'],
allowedSortings: ['*'],
allowedGroupBy: ['info_id', 'step', 'province_id', 'province_fa']
);
$provinces = SafetyAndPrivacy::query() foreach ($provinces['data'] as $value)
->selectRaw('province_id, province_fa, COUNT(*) as qty,info_id, step') {
->whereBetween('created_at', [$fromDate, $toDate])
->when($axisTypeId, function ($query) use ($axisTypeId) {
$query->where('axis_type_id', '=', $axisTypeId);
})
->groupBy('info_id', 'step', 'province_id', 'province_fa')
->get();
// $provinces = DB::select(
// "SELECT province_id, province_fa, COUNT(*) as qty,info_id, step
// FROM safety_and_privacy where created_at BETWEEN '".$fromDate."' and '".$toDate."'
// GROUP BY info_id,step,province_id,province_fa"
// );
foreach ($provinces as $value) {
$provinceId = $value->province_id; $provinceId = $value->province_id;
// Check if the province already exists in the activities array
if (!isset($activities[$provinceId])) { if (!isset($activities[$provinceId])) {
$activities[$provinceId] = (object) [ $activities[$provinceId] = (object) [
'province_id' => $provinceId, 'province_id' => $provinceId,
@@ -129,92 +101,53 @@ class SafetyAndPrivacyTableService
'91' => ['1' => 0, '2' => 0, '3' => 0], '91' => ['1' => 0, '2' => 0, '3' => 0],
]; ];
} }
$activities[$provinceId]->{$value->info_id}[$value->step] = $value->qty; $activities[$provinceId]->{$value->info_id}[$value->step] = $value->qty;
$activities[$provinceId]->sum += $value->qty; $activities[$provinceId]->sum += $value->qty;
} }
$activities = array_values($activities); return array_values($activities);
return [
'activities' => $activities,
'fromDate' => $fromDate,
'toDate' => $toDate,
];
} }
public function provinceActivity(Request $request): array public function provinceActivity(Request $request): array
{ {
$fromDate = $request->from_date ? $request->from_date . ' 00:00:00' : now()->startOfDay()->toDateTimeString();
$toDate = $request->date_to ? $request->date_to . ' 23:59:59' : now()->endOfDay()->toDateTimeString();
$provinceId = $request->province_id;
$axisTypeId = $request->axis_type_id;
$activities = []; $activities = [];
$provinceQuery = SafetyAndPrivacy::query()->selectRaw('COUNT(*) as qty,info_id, step');
$province = SafetyAndPrivacy::query() $province = DataTableFacade::run(
->selectRaw('COUNT(*) as qty,info_id, step') $provinceQuery,
->where('province_id', '=', $provinceId) $request,
->whereBetween('created_at', [$fromDate, $toDate]) allowedFilters: ['*'],
->when($axisTypeId, function ($query) use ($axisTypeId) { allowedSortings: ['*'],
$query->where('axis_type_id', '=', $axisTypeId); allowedGroupBy: ['info_id', 'step']
}) );
->groupBy('info_id', 'step')
->get();
// $province = DB::select('
// SELECT COUNT(*) as qty,info_id, step
// FROM safety_and_privacy where province_id ='.$provinceId.' and created_at BETWEEN "'.$fromDate.'" and "'.$toDate.'"
// GROUP BY info_id,step
// ');
$provinceTotalSum = 0; $provinceTotalSum = 0;
$provinceData = [ $provinceData = [
'name_fa' => 'استانی', 'name_fa' => 'استانی',
'edare_shahri_id' => -1, 'edare_shahri_id' => -1,
'sum' => 0, 'sum' => 0,
'89' => [ '89' => ['1' => 0, '2' => 0, '3' => 0,],
'1' => 0, '90' => ['1' => 0, '2' => 0, '3' => 0,],
'2' => 0, '91' => ['1' => 0, '2' => 0, '3' => 0,],
'3' => 0,
],
'90' => [
'1' => 0,
'2' => 0,
'3' => 0,
],
'91' => [
'1' => 0,
'2' => 0,
'3' => 0,
],
]; ];
foreach ($province as $key => $value) { foreach ($province['data'] as $key => $value) {
$provinceTotalSum += $value->qty; $provinceTotalSum += $value->qty;
$provinceData["sum"] = $provinceTotalSum; $provinceData["sum"] = $provinceTotalSum;
$provinceData[$value->info_id][$value->step] = $value->qty; $provinceData[$value->info_id][$value->step] = $value->qty;
} }
$activities[] = (object) $provinceData; $activities[] = (object) $provinceData;
$cities = SafetyAndPrivacy::query() $citiesQuery = SafetyAndPrivacy::query()->selectRaw('edare_shahri_id, COUNT(*) as qty,info_id, step');
->selectRaw('edare_shahri_id, COUNT(*) as qty,info_id, step') $cities = DataTableFacade::run(
->where('province_id', '=', $provinceId) $citiesQuery,
->whereBetween('created_at', [$fromDate, $toDate]) $request,
->when($axisTypeId, function ($query) use ($axisTypeId) { allowedFilters: ['*'],
$query->where('axis_type_id', '=', $axisTypeId); allowedSortings: ['*'],
}) allowedGroupBy: ['info_id', 'step', 'edare_shahri_id']
->groupBy('info_id', 'step', 'edare_shahri_id') );
->get();
// $cities = DB::select(' foreach ($cities['data'] as $value)
// SELECT edare_shahri_id, COUNT(*) as qty,info_id, step {
// FROM safety_and_privacy where province_id ='.$provinceId.' and created_at BETWEEN "'.$fromDate.'" and "'.$toDate.'"
// GROUP BY info_id,step,edare_shahri_id
// ');
foreach ($cities as $value) {
$cityId = $value->edare_shahri_id; $cityId = $value->edare_shahri_id;
// Initialize data for each unique city
if (!isset($activities[$cityId])) { if (!isset($activities[$cityId])) {
$activities[$cityId] = (object) [ $activities[$cityId] = (object) [
'edare_shahri_id' => $cityId, 'edare_shahri_id' => $cityId,
@@ -229,12 +162,6 @@ class SafetyAndPrivacyTableService
$activities[$cityId]->sum += $value->qty; $activities[$cityId]->sum += $value->qty;
} }
$activities = array_values($activities); return array_values($activities);
return [
'activities' => $activities,
'fromDate' => $fromDate,
'toDate' => $toDate,
];
} }
} }

View File

@@ -22,6 +22,7 @@ class DataTableInput
private array $rels, private array $rels,
private array $allowedFilters, private array $allowedFilters,
private array $allowedSortings, private array $allowedSortings,
private ?array $GroupBy,
) )
{ {
} }
@@ -74,4 +75,9 @@ class DataTableInput
{ {
return $this->rels; return $this->rels;
} }
public function getGroupBy(): ?array
{
return $this->GroupBy;
}
} }

View File

@@ -13,6 +13,7 @@ class DataTableService
protected array $allowedRelations; protected array $allowedRelations;
protected array $allowedSortings; protected array $allowedSortings;
protected array $allowedSelects; protected array $allowedSelects;
protected array $allowedGroupBy;
private int $totalRowCount; private int $totalRowCount;
public function __construct( public function __construct(
@@ -46,6 +47,12 @@ class DataTableService
return $this; return $this;
} }
public function setAllowedGroupBy(array $allowedGroupBy): DataTableService
{
$this->allowedGroupBy = $allowedGroupBy;
return $this;
}
/** /**
* Handle 'getData' operations * Handle 'getData' operations
* @return array * @return array
@@ -73,6 +80,7 @@ class DataTableService
$query = $this->applySelect($query, $this->allowedSelects); $query = $this->applySelect($query, $this->allowedSelects);
$query = $this->includeRelationsInQuery($query, $this->allowedRelations); $query = $this->includeRelationsInQuery($query, $this->allowedRelations);
$query = $this->applyGroupBy($query, $this->allowedGroupBy);
$this->totalRowCount = $query->count(); $this->totalRowCount = $query->count();
@@ -88,7 +96,7 @@ class DataTableService
foreach ($sorting as $sort){ foreach ($sorting as $sort){
$query = (new ApplySort($query, $sort))->apply(); $query = (new ApplySort($query, $sort))->apply();
} }
return $query; return $query;
} }
@@ -101,6 +109,11 @@ class DataTableService
return $query; return $query;
} }
protected function applyGroupBy(Builder $query, array $groupBy): Builder
{
return empty($groupBy) ? $query : $query->groupBy($groupBy);
}
protected function includeRelationsInQuery(Builder $query, array $rels): Builder protected function includeRelationsInQuery(Builder $query, array $rels): Builder
{ {
if (!empty($rels)) { if (!empty($rels)) {

View File

@@ -97,7 +97,7 @@
<td style="border: 1px solid black;text-align: center;"> <td style="border: 1px solid black;text-align: center;">
{{ $item['sub_item_data'] ? ($item['unit_fa'] ? $item['sub_item_data'] . '(' . $item['unit_fa'] . ')' : $item['sub_item_data'] . '(-)') : '-' }} {{ $item['sub_item_data'] ? ($item['unit_fa'] ? $item['sub_item_data'] . '(' . $item['unit_fa'] . ')' : $item['sub_item_data'] . '(-)') : '-' }}
</td> </td>
<td style="border: 1px solid black;text-align: center;">{{ $item['cmms_machines'] != null ? $item['cmms_machines']->pluck('machine_code')->implode(', ') : '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['cmmsMachines'] != null ? $item['cmmsMachines']->pluck('machine_code')->implode(', ') : '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['rahdaran'] != null ? $item['rahdaran']->pluck('code')->implode(', ') : '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['rahdaran'] != null ? $item['rahdaran']->pluck('code')->implode(', ') : '-' }}</td>
<td style="border: 1px solid black;text-align: center;"> <td style="border: 1px solid black;text-align: center;">
{{ isset($item['start_lat']) && isset($item['start_lng']) ? $item['start_lat'] . ' ' . $item['start_lng'] : '-' }} {{ isset($item['start_lat']) && isset($item['start_lng']) ? $item['start_lat'] . ' ' . $item['start_lng'] : '-' }}
@@ -114,5 +114,4 @@
</tbody> </tbody>
</table> </table>
</body> </body>
</html> </html>

View File

@@ -97,7 +97,7 @@
<td style="border: 1px solid black;text-align: center;"> <td style="border: 1px solid black;text-align: center;">
{{ $item['sub_item_data'] ? ($item['unit_fa'] ? $item['sub_item_data'] . '(' . $item['unit_fa'] . ')' : $item['sub_item_data'] . '(-)') : '-' }} {{ $item['sub_item_data'] ? ($item['unit_fa'] ? $item['sub_item_data'] . '(' . $item['unit_fa'] . ')' : $item['sub_item_data'] . '(-)') : '-' }}
</td> </td>
<td style="border: 1px solid black;text-align: center;">{{ $item['cmms_machines'] != null ? $item['cmms_machines']->pluck('machine_code')->implode(', ') : '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['cmmsMachines'] != null ? $item['cmmsMachines']->pluck('machine_code')->implode(', ') : '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['rahdaran'] != null ? $item['rahdaran']->pluck('code')->implode(', ') : '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['rahdaran'] != null ? $item['rahdaran']->pluck('code')->implode(', ') : '-' }}</td>
<td style="border: 1px solid black;text-align: center;"> <td style="border: 1px solid black;text-align: center;">
{{ isset($item['start_lat']) && isset($item['start_lng']) ? $item['start_lat'] . ' ' . $item['start_lng'] : '-' }} {{ isset($item['start_lat']) && isset($item['start_lng']) ? $item['start_lat'] . ' ' . $item['start_lng'] : '-' }}

View File

@@ -74,7 +74,7 @@
<td style="border: 1px solid black;text-align: center;">{{ $item['id'] ?? '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['id'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['province_fa'] ?? '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['province_fa'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['edare_name'] ?? '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['edare_name'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['cmms_machines'] != null ? $item['cmms_machines']->pluck('machine_code')->implode(', ') : '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['cmmsMachines'] != null ? $item['cmmsMachines']->pluck('machine_code')->implode(', ') : '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['rahdaran'] != null ? $item['rahdaran']->pluck('code')->implode(', ') : '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['rahdaran'] != null ? $item['rahdaran']->pluck('code')->implode(', ') : '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['start_time'] ? verta($item['start_time'])->format('Y/n/j H:i:s') : '-' }} <td style="border: 1px solid black;text-align: center;">{{ $item['start_time'] ? verta($item['start_time'])->format('Y/n/j H:i:s') : '-' }}
<td style="border: 1px solid black;text-align: center;">{{ $item['end_time'] ? verta($item['end_time'])->format('Y/n/j H:i:s') : '-' }} <td style="border: 1px solid black;text-align: center;">{{ $item['end_time'] ? verta($item['end_time'])->format('Y/n/j H:i:s') : '-' }}

View File

@@ -33,7 +33,7 @@
</tr> </tr>
<tr> <tr>
<th colspan="9" <th colspan="9"
style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;font-size: 13;"> style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;font-size: 13px;">
گزارش کارتابل ارزیابی گشت راهداری گزارش کارتابل ارزیابی گشت راهداری
</th> </th>
</tr> </tr>
@@ -74,7 +74,7 @@
<td style="border: 1px solid black;text-align: center;">{{ $item['id'] ?? '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['id'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['province_fa'] ?? '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['province_fa'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['edare_name'] ?? '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['edare_name'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['cmms_machines'] != null ? $item['cmms_machines']->pluck('machine_code')->implode(', ') : '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['cmmsMachines'] != null ? $item['cmmsMachines']->pluck('machine_code')->implode(', ') : '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['rahdaran'] != null ? $item['rahdaran']->pluck('code')->implode(', ') : '-' }}</td> <td style="border: 1px solid black;text-align: center;">{{ $item['rahdaran'] != null ? $item['rahdaran']->pluck('code')->implode(', ') : '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['start_time'] ? verta($item['start_time'])->format('Y/n/j H:i:s') : '-' }} <td style="border: 1px solid black;text-align: center;">{{ $item['start_time'] ? verta($item['start_time'])->format('Y/n/j H:i:s') : '-' }}
<td style="border: 1px solid black;text-align: center;">{{ $item['end_time'] ? verta($item['end_time'])->format('Y/n/j H:i:s') : '-' }} <td style="border: 1px solid black;text-align: center;">{{ $item['end_time'] ? verta($item['end_time'])->format('Y/n/j H:i:s') : '-' }}

View File

@@ -28,12 +28,7 @@
<tr> <tr>
<th colspan="11" <th colspan="11"
style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;font-size: 13px;"> style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;font-size: 13px;">
گزارش نگهداری حریم راه از تاریخ گزارش نگهداری حریم راه
{{ verta($fromDate)->format('Y/n/j') }}
تا
تاریخ
{{ verta($toDate)->format('Y/n/j') }}
</th> </th>
</tr> </tr>

View File

@@ -27,13 +27,8 @@
</tr> </tr>
<tr> <tr>
<th colspan="11" <th colspan="11"
style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;font-size: 13;"> style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;font-size: 13px;">
گزارش نگهداری حریم راه از تاریخ گزارش نگهداری حریم راه
{{ verta($fromDate)->format('Y/n/j') }}
تا
تاریخ
{{ verta($toDate)->format('Y/n/j') }}
</th> </th>
</tr> </tr>