From dd616551aff95e9846824e36fcbaee14788b6cfe Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Tue, 24 Dec 2024 11:55:20 +0330 Subject: [PATCH] create fms service --- .env.example | 6 +- .../V3/RoadPatrolProjectController.php | 409 ++++++++++++++++++ .../V3/RoadPatrolProject/DeleteRequest.php | 28 ++ .../V3/RoadPatrolProject/StoreRequest.php | 55 +++ .../FMS/GetVehicleActivityService.php | 70 +++ config/fms_web_services.php | 9 + config/logging.php | 6 + routes/v3.php | 38 ++ 8 files changed, 620 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/V3/RoadPatrolProjectController.php create mode 100644 app/Http/Requests/V3/RoadPatrolProject/DeleteRequest.php create mode 100644 app/Http/Requests/V3/RoadPatrolProject/StoreRequest.php create mode 100644 app/Services/FMS/GetVehicleActivityService.php create mode 100644 config/fms_web_services.php diff --git a/.env.example b/.env.example index dcd498a5..87bbe2f7 100644 --- a/.env.example +++ b/.env.example @@ -61,4 +61,8 @@ IS_DEVELOPMENT_ENV= CMMS_MACHINE_INFO_URL= CMMS_MACHINE_INFO_PASSWORD= -CMMS_MACHINE_INFO_VIEW_NAME= \ No newline at end of file +CMMS_MACHINE_INFO_VIEW_NAME= + +FMS_VEHICLE_ACTIVITY_URL= +FMS_VEHICLE_ACTIVITY_PASSWORD= +FMS_VEHICLE_ACTIVITY_USERNAME= \ No newline at end of file diff --git a/app/Http/Controllers/V3/RoadPatrolProjectController.php b/app/Http/Controllers/V3/RoadPatrolProjectController.php new file mode 100644 index 00000000..21b505db --- /dev/null +++ b/app/Http/Controllers/V3/RoadPatrolProjectController.php @@ -0,0 +1,409 @@ +user(); + $query = null; + + if ($user->hasPermissionTo('show-road-patrol-supervise-cartable')) { + $query = RoadPatrol::query(); + } + elseif ($user->hasPermissionTo('show-road-patrol-supervise-cartable-province')) { + if (is_null($user->province_id)) { + return $this->errorResponse('استانی برای شما در سامانه ثبت نشده است!'); + } + $query = RoadPatrol::query()->where('province_id', '=', $user->province_id); + } + + $data = DataTableFacade::run( + $query, + $request, + allowedFilters: $allowedFilters, + allowedSortings: $allowedSortings); + + foreach ($data['data'] as $road_patrol) { + if (Gate::allows('gate-supervise-road-item', $road_patrol)) { + $road_patrol['can_supervise'] = 1; + } else { + $road_patrol['can_supervise'] = 0; + } + } + + return response()->json($data); + } + + public function delete(DeleteRequest $request, RoadPatrol $roadPatrol): JsonResponse + { + if ($request->type == 1) { + DB::transaction(function () use ($roadPatrol) { + $roadPatrol->observedItems()->delete(); + $roadPatrol->delete(); + }); + } else { + DB::transaction(function () use ($roadPatrol) { + if ($roadPatrol->observedItems) { + foreach ($roadPatrol->observedItems as $index => $observed_item) { + if ($observed_item->roadItemProject) { + $road_item = $observed_item->roadItemProject; + + if ($road_item->files()->exists()) { + Storage::delete('public/'. $road_item->files[0]->path); + Storage::delete('public/'. $road_item->files[1]->path); + $road_item->files()->delete(); + } + + $road_item->delete(); + } + + $observed_item->delete(); + } + } + auth()->user()->addActivityComplete(1144); + + $roadPatrol->delete(); + }); + } + + return $this->successResponse(); + } + + public function supervisorCartableReport(Request $request): BinaryFileResponse + { + $name = 'گزارش از کارتابل ارزیابی گشت راهداری ' . verta()->now()->format('Y-m-d H:i') . '.xlsx'; + return Excel::download( + new SupervisorCartableExport($request->id, $request->fromDate, + $request->toDate, $request->province_id, $request->edare_id + ), $name + ); + } + + public function operatorIndex(Request $request): JsonResponse + { + $columns = array( + 'id', 'start_lat', 'start_lon', 'end_lat', 'end_lon', 'officer_plaque', + 'officer_phone_number', 'officer_name', 'supervisor_description', 'supervising_time', + 'supervisor_name', 'status', 'status_fa', 'distance', 'start_time', 'end_time' + ); + + $allowedFilters = $columns; + $allowedSortings = $columns; + + $query = RoadPatrol::query()->where('operator_id', '=', auth()->user()->id); + + $data = DataTableFacade::run( + $query, + $request, + allowedFilters: $allowedFilters, + allowedSortings: $allowedSortings); + + return response()->json($data); + } + + public function store(StoreRequest $request, NominatimService $nominatimService) + { + $user = auth()->user(); + + if ($user->edarate_ostani_id || is_null($user->city_id)) { + return response()->json([ + 'message' => 'امکان ثبت فعالیت برای ادارات استانی وجود ندارد!' + ], 400); + } + + if (is_null($user->edarate_shahri_id)) { + return response()->json([ + 'message' => 'اداره شهری برای شما در سامانه ثبت نشده است!' + ], 400); + } + + $start_coordinates = explode(',', $request->start_point); + $end_coordinates = explode(',', $request->end_point); + + $edare_id = $user->edarate_shahri_id ?? $user->edarate_ostani_id; + $edare_name = $user->edarate_shahri_id ? $user->edarate_shahri_name : $user->edarate_ostani_name; + + $road_patrol = DB::transaction(function () use ($request, $start_coordinates, + $end_coordinates, $edare_id, + $edare_name, $nominatimService, $user) { + + $road_patrol = RoadPatrol::query()->create([ + 'start_lat' => $start_coordinates[0], + 'start_lon' => $start_coordinates[1], + 'end_lat' => $end_coordinates[0], + 'end_lon' => $end_coordinates[1], + 'officer_name' => $request->officer_name, + 'officer_phone_number' => $request->officer_phone_number, + 'officer_plaque' => $request->officer_plaque, + 'operator_id' => $user->id, + 'operator_name' => $user->name, + 'province_id' => $user->province_id, + 'province_fa' => $user->province_fa, + 'city_id' => $user->city_id, + 'city_fa' => $user->city_fa, + 'edare_id' => $edare_id, + 'edare_name' => $edare_name, + 'start_way_id' => $nominatimService->get_way_id_from_nominatim($start_coordinates[0], $start_coordinates[1]), + 'end_way_id' => $nominatimService->get_way_id_from_nominatim($end_coordinates[0], $end_coordinates[1]), + 'status' => 0, + 'status_fa' => 'در حال بررسی', + 'distance' => $request->distance, + 'start_time' => $request->start_time, + 'end_time' => $request->end_time, + 'description' => $request->description ?? null, + ]); + + if ($request->observed_items) { + foreach ($request->observed_items as $index => $item) { + $info_item = InfoItem::where('item', $item['item_id']) + ->where('sub_item', $item['sub_item_id']) + ->firstOrFail(); + + $item_start_coordinates = explode(',', $item['start_point']); + + $observed_item = $road_patrol->observedItems()->create([ + 'item_id' => $info_item->item, + 'item_name' => $info_item->item_str, + 'sub_item_id' => $info_item->sub_item, + 'sub_item_name' => $info_item->sub_item_str, + 'local_name' => $item['local_name'], + 'description' => $item['description'] ?? null, + 'start_lat' => $item_start_coordinates[0], + 'start_lon' => $item_start_coordinates[1], + ]); + + $item_end_coordinates = null; + if ($item['instant_action'] == 0) { + $observed_item->priority = $item['priority']; + $observed_item->priority_fa = $item['priority_fa']; + $observed_item->save(); + } elseif ($item['instant_action'] == 1) { + if ($info_item->needs_end_point) { + if (!isset($item['end_point'])) { + throw ValidationException::withMessages([ + "observed_items.$index.end_point" => __('validation.required', ['attribute' => 'end_point']), + ]); + } + + $item_end_coordinates = explode(',', $item['end_point']); + } + + $before_image = null; + $after_image = null; + if ($info_item->needs_image) { + if (!isset($item['before_image'])) { + throw ValidationException::withMessages([ + "observed_items.$index.before_image" => __('validation.required', ['attribute' => 'before_image']), + ]); + } + if (!isset($item['after_image'])) { + throw ValidationException::withMessages([ + "observed_items.$index.after_image" => __('validation.required', ['attribute' => 'after_image']), + ]); + } + + $before_image = $item['before_image']; + $after_image = $item['after_image']; + } + + $start_time = Carbon::createFromFormat('Y-m-d H:i', $request->start_time); + $end_time = Carbon::createFromFormat('Y-m-d H:i', $request->end_time); + $activity_date = $start_time->average($end_time); + + $attributes = [ + 'start_lat' => $item_start_coordinates[0], + 'start_lng' => $item_start_coordinates[1], + 'end_lat' => $item_end_coordinates ? $item_end_coordinates[0] : null, + 'end_lng' => $item_end_coordinates ? $item_end_coordinates[1] : null, + 'item' => $info_item->item, + 'item_fa' => $info_item->item_str, + 'sub_item' => $info_item->sub_item, + 'sub_item_fa' => $info_item->sub_item_str, + 'sub_item_data' => $item['amount'], + 'province_id' => $user->province_id, + 'province_fa' => $user->province_fa, + 'city_id' => $user->city_id, + 'city_fa' => $user->city_fa, + 'user_name' => $user->name, + 'start_way_id' => $nominatimService->get_way_id_from_nominatim($item_start_coordinates[0], $item_start_coordinates[1]), + 'end_way_id' => $item_end_coordinates ? $nominatimService->get_way_id_from_nominatim($item_end_coordinates[0], $item_end_coordinates[1]) : null, + 'unit_fa' => $info_item->sub_item_unit, + 'created_at_fa' => verta(Carbon::now())->format('Y-m-d H:i:s'), + 'info_id' => $info_item->id, + 'status' => 0, + 'status_fa' => 'در حال بررسی', + 'edarat_id' => $user->edarate_shahri_id, + 'edarat_type' => EdarateShahri::class, + 'edarat_name' => $user->edarate_shahri_name, + 'activity_date' => $activity_date->format('Y-m-d'), + 'activity_time' => $activity_date->format('H:i:s'), + ]; + + $road_item = RoadItemsProject::store($user, $attributes, $before_image, $after_image, $observed_item->id); + + if ($road_item === -1) { + return response()->json([ + 'message' => 'اقدام مربوطه قبلا رسیدگی شده است.' + ], 400); + } + } + } + } + + auth()->user()->addActivityComplete(1147); + + Sms::sendSms($request->officer_phone_number, + "گشت راهداری با کد یکتای $road_patrol->id در تاریخ ".verta(\Carbon\Carbon::now())->format('Y-m-d H:i:s')." با شماره شما در سامانه RMS ثبت شد. \n لینک دریافت گزارش : \n https://rms.rmto.ir/v2/road_patrols/operator/report/$road_patrol->id" + ); + return $road_patrol; + }); + + return response()->json([ + 'road_patrol_id' => $road_patrol->id, + 'message' => 'succussful' + ]); + } + + public function getReport(RoadPatrol $roadPatrol): JsonResponse + { + $data = [ + 'serial_no' => $roadPatrol->id, + 'province_fa' => $roadPatrol->province_fa, + 'edare_name' => $roadPatrol->edare_name, + 'start_time' => $roadPatrol->start_time, + 'end_time' => $roadPatrol->end_time, + 'officer_name' => $roadPatrol->officer_name, + 'officer_plaque' => $roadPatrol->officer_plaque, + 'officer_phone_number' => $roadPatrol->officer_phone_number, + 'created_at' => $roadPatrol->created_at + ]; + + $observed_items = array(); + if($roadPatrol->observedItems) { + foreach ($roadPatrol->observedItems as $index => $observed_item) { + $observed_items[] = [ + 'item' => $observed_item->item_name, + 'sub_item' => $observed_item->sub_item_name, + 'local_name' => $observed_item->local_name, + 'description' => $observed_item->description, + 'priority_fa' => $observed_item->priority_fa, + ]; + } + } + + $data['observed_items'] = $observed_items; + return $this->successResponse($data); + } + + public function getAllDataToMap(Request $request): JsonResponse + { + $date_from = $request->date_from ? $request->date_from .' 00:00:00' : Date('Y-m-d') .' 00:00:00'; + $date_to = $request->date_to ? $request->date_to.' 23:59:59' : (new \DateTime('tomorrow'))->format('Y-m-d').' 23:59:59'; + + $data = RoadPatrol::query()->when($request->province_id, function ($query, $province) { + return $query->where('province_id', '=', $province); + }) + ->when($request->edare_id, function ($query, $edare_id) { + return $query->where('edare_id', '=', $edare_id); + }) + ->whereBetween('created_at', [ + $date_from, + $date_to, + ]) + ->select('id', 'start_lat', 'start_lon') + ->get(); + + return $this->successResponse($data); + } + + public function map(Request $request): JsonResponse + { + $province = $request->province_id; + $edare_shahri = $request->edare_shahri_id; + $status = $request->status; + $rms_status = $request->rms_status; + + if ($request->date_from && $request->date_to) { + $date_from = $request->date_from.' 00:00:00'; + $date_to = $request->date_to.' 23:59:59'; + } else { + $date_from = Date('Y-m-d'); + $date_to = new \DateTime('tomorrow'); + $date_to = $date_to->format('Y-m-d'); + } + + $projects = RoadObserved::query()->select(['id', 'lng', 'lat','status']) + ->whereBetween('created_at', [$date_from, $date_to]) + ->when($province, function ($query) use ($province) { + return $query->where('province_id', '=', $province); + }) + ->when($rms_status, function ($query) use ($rms_status) { + if ($rms_status == 2) { + return $query->where('rms_status', '<>', 0); + } else { + return $query->where('rms_status', '=', 0); + } + }) + ->when($edare_shahri, function ($query) use ($edare_shahri) { + return $query->where('edarate_shahri_id', '=', $edare_shahri); + }) + ->when($status, function ($query) use ($status) { + return $query->where('status', '=', $status); + }) + ->get(); + return $this->successResponse($projects); + } + + public function show(RoadPatrol $roadPatrol): JsonResponse + { + return $this->successResponse($roadPatrol); + } + + public function operatorCartableReport(Request $request): BinaryFileResponse + { + $name = 'گزارش از کارتابل عملیات گشت راهداری ' . verta()->now()->format('Y-m-d H:i') . '.xlsx'; + return Excel::download( + new OperatorCartableExport($request->id, $request->status, $request->officer_name, $request->fromDate, $request->toDate), + $name + ); + } +} diff --git a/app/Http/Requests/V3/RoadPatrolProject/DeleteRequest.php b/app/Http/Requests/V3/RoadPatrolProject/DeleteRequest.php new file mode 100644 index 00000000..b6527892 --- /dev/null +++ b/app/Http/Requests/V3/RoadPatrolProject/DeleteRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + 'type' => 'required|in:1,2' // {1 = > partial , 2 => complete} + ]; + } +} diff --git a/app/Http/Requests/V3/RoadPatrolProject/StoreRequest.php b/app/Http/Requests/V3/RoadPatrolProject/StoreRequest.php new file mode 100644 index 00000000..9dbd916a --- /dev/null +++ b/app/Http/Requests/V3/RoadPatrolProject/StoreRequest.php @@ -0,0 +1,55 @@ +|string> + */ + public function rules(): array + { + return [ + 'start_point' => 'required', + 'end_point' => 'required', + 'distance' => 'numeric', + 'officer_name' => 'required', + 'officer_plaque' => 'required', + 'officer_phone_number' => 'required|digits:11', + 'start_time' => 'required|date_format:Y-m-d H:i', + 'end_time' => 'required|date_format:Y-m-d H:i|after:start_time', + + 'observed_items' => 'array', + 'observed_items.*.item_id' => 'required|integer', + 'observed_items.*.sub_item_id' => 'required|integer', + 'observed_items.*.local_name' => 'required', + 'observed_items.*.description' => '', + 'observed_items.*.instant_action' => 'required|in:0,1', + + // road item projects fields + 'observed_items.*.start_point' => 'required', + 'observed_items.*.end_point' => '', + 'observed_items.*.amount' => 'numeric|required_if:instant_action,1', + 'observed_items.*.before_image' => 'image|max:4096', + 'observed_items.*.after_image' => 'image|max:4096', + 'observed_items.*.priority' => 'integer|in:1,2,3|required_if:instant_action,0', + 'observed_items.*.priority_fa' => 'string|required_if:instant_action,0', + 'description' => 'string|required_with:observed_items', + + 'start_time' => 'ساعت شروع گشت', + 'end_time' => 'ساعت پایان گشت', + ]; + } +} diff --git a/app/Services/FMS/GetVehicleActivityService.php b/app/Services/FMS/GetVehicleActivityService.php new file mode 100644 index 00000000..94f124eb --- /dev/null +++ b/app/Services/FMS/GetVehicleActivityService.php @@ -0,0 +1,70 @@ +url = config('fms_web_services.Vehicle_Activity.url'); + $this->password = config('fms_web_services.Vehicle_Activity.password'); + $this->username = config('fms_web_services.Vehicle_Activity.username'); + $this->channelName = 'fms_vehicle_activity'; + } + + public function setInputParameters(array $inputs): void + { + $this->inputParameters = $inputs; + } + + /** + * @throws Exception + */ + public function run(): array + { + try { + return $this->sendRequest(); + } + catch (Exception $e) { + Log::channel($this->channelName) + ->error(get_class($this), + [ + 'message' => $e->getMessage(), + ] + ); + + throw $e; + } + } + + public function sendRequest(): array + { + $inputData = $this->makeInputParameters(); + + return Http::withBody($inputData) + ->throw() + ->withoutVerifying() + ->post($this->url) + ->json(); + } + + private function makeInputParameters(): string + { + $inputData = $this->inputParameters + array( + "username" => $this->username, + "password" => $this->password, + ); + + return json_encode($inputData); + } +} diff --git a/config/fms_web_services.php b/config/fms_web_services.php new file mode 100644 index 00000000..fe06bba3 --- /dev/null +++ b/config/fms_web_services.php @@ -0,0 +1,9 @@ + [ + 'url' => env('FMS_VEHICLE_ACTIVITY_URL'), + 'password' => env('FMS_VEHICLE_ACTIVITY_PASSWORD'), + 'username' => env('FMS_VEHICLE_ACTIVITY_USERNAME'), + ], +]; \ No newline at end of file diff --git a/config/logging.php b/config/logging.php index 2ac89fca..4dd0cf49 100644 --- a/config/logging.php +++ b/config/logging.php @@ -105,6 +105,12 @@ return [ 'path' => storage_path('logs/cmms/machines_info_list_service.log'), 'level' => 'info', ], + + 'fms_vehicle_activity' => [ + 'driver' => 'single', + 'path' => storage_path('logs/fms/vehicle_activity_service.log'), + 'level' => 'info', + ], ], ]; diff --git a/routes/v3.php b/routes/v3.php index dc7f7d9f..46e28e75 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -9,6 +9,7 @@ use App\Http\Controllers\V3\Harim\DivarkeshiController; use App\Http\Controllers\V3\ProfileController; use App\Http\Controllers\V3\RahdaranController; use App\Http\Controllers\V3\RoadItemsProjectController; +use App\Http\Controllers\V3\RoadPatrolProjectController; use Illuminate\Support\Facades\Route; Route::prefix('harim')->name('harim')->group(function () { @@ -117,6 +118,43 @@ Route::prefix('road_items') ->name('operatorCartableReport'); }); +Route::prefix('road_patrols') + ->name('road_patrols.') + ->controller(RoadPatrolProjectController::class) + ->group(function () { + + Route::get('/supervisor_index', 'supervisorIndex') + ->middleware(['permission:show-road-patrol-supervise-cartable|show-road-patrol-supervise-cartable-province']) + ->name('supervisorIndex'); + + Route::post('/delete/{roadPatrol}', 'delete') + ->middleware('permission:partial-delete-road-patrol|complete-delete-road-patrol') + ->name('delete'); + + Route::get('/supervisor_report', 'supervisorCartableReport') + ->name('supervisorCartableReport'); + + Route::get('/operator_index', 'operatorIndex') + ->middleware('permission:add-road-patrol') + ->name('operatorIndex'); + + Route::post('/store', 'store') + ->middleware('permission:add-road-patrol') + ->name('store'); + + Route::get('/operator_report', 'operatorCartableReport') + ->name('operatorCartableReport'); + + Route::get('/operator/report/{roadPatrol}', 'getReport') + ->name('getReport'); + + Route::get('/operator/map_data', 'getAllDataToMap') + ->name('getAllDataToMap'); + + Route::get('/detail/{roadPatrol}', 'show') + ->name('show'); + }); + Route::prefix('cmms_machines') ->name('cmms_machines.') ->controller(CMMSMachinesController::class)