From 9a6701288d22c2edc6a603013718a00096a66963 Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Wed, 27 Aug 2025 10:43:40 +0330 Subject: [PATCH] refactor --- .../Dashboard/Harim/HarimOfficeController.php | 8 +-- .../Harim/PanjareVahedController.php | 50 +++++++++++++++++-- .../Mission/ControlUnitController.php | 5 +- .../Mission/SendDataToFMSListener.php | 11 +++- app/Models/City.php | 4 +- .../2025_07_30_153041_create_harims_table.php | 2 + 6 files changed, 66 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/V3/Dashboard/Harim/HarimOfficeController.php b/app/Http/Controllers/V3/Dashboard/Harim/HarimOfficeController.php index 30bdc2ff..3f5c3587 100644 --- a/app/Http/Controllers/V3/Dashboard/Harim/HarimOfficeController.php +++ b/app/Http/Controllers/V3/Dashboard/Harim/HarimOfficeController.php @@ -86,7 +86,7 @@ class HarimOfficeController extends Controller 'state_id' => $state, 'state_name' => HarimStates::name($state), 'is_possible' => true, - 'need_road_access' => true, + 'need_access_road' => true, ]); }); return $this->successResponse(); @@ -112,7 +112,7 @@ class HarimOfficeController extends Controller 'state_id' => $state, 'state_name' => HarimStates::name($state), 'is_possible' => true, - 'need_road_access' => false, + 'need_access_road' => false, ]); }); return $this->successResponse(); @@ -135,7 +135,7 @@ class HarimOfficeController extends Controller $harim ->update([ 'state_id' => $state, 'state_name' => HarimStates::name($state), - 'is_file_good' => true, + 'access_road_allowed' => true, ]); }); return $this->successResponse(); @@ -158,7 +158,7 @@ class HarimOfficeController extends Controller $harim ->update([ 'state_id' => $state, 'state_name' => HarimStates::name($state), - 'is_file_good' => false, + 'access_road_allowed' => false, ]); }); return $this->successResponse(); diff --git a/app/Http/Controllers/V3/Dashboard/Harim/PanjareVahedController.php b/app/Http/Controllers/V3/Dashboard/Harim/PanjareVahedController.php index d19bac12..323960c5 100644 --- a/app/Http/Controllers/V3/Dashboard/Harim/PanjareVahedController.php +++ b/app/Http/Controllers/V3/Dashboard/Harim/PanjareVahedController.php @@ -2,22 +2,64 @@ namespace App\Http\Controllers\V3\Dashboard\Harim; +use App\Enums\HarimStates; use App\Http\Controllers\Controller; use App\Http\Traits\ApiResponse; +use App\Models\City; use App\Models\Harim; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Request; class PanjareVahedController extends Controller { use ApiResponse; - public function receiveNewRequests() + public function receiveNewRequests(Request $request) { - // find city and province by lat,lng - Harim::query()->create([ + try { + $city = City::query() + ->whereRaw("ST_Contains( + geometry, + ST_SRID(ST_GeomFromText('POINT($request->lng $request->lat)'), 4326))" + ) + ->firstOrFail(['id', 'name_fa', 'province_id']); + } + catch (ModelNotFoundException $exception){ + $city = City::query() + ->orderByRaw("ST_Distance( + geometry, + ST_SRID(ST_GeomFromText('POINT($request->lng $request->lat)'), 4326) + ) ASC" + ) + ->first(['id', 'name_fa', 'province_id']); + } + $state = HarimStates::BARESI_EDARE_SHAHRESTAN->value; + + Harim::query()->create([ + 'state_id' => $state, + 'state_name' => HarimStates::name($state), + 'panjare_vahed_id' => $request->id, + 'national_id' => $request->national_id, + 'phone_number' => $request->phone_number, + 'request_date' => $request->request_date, + 'province_id' => $city->province_id, + 'province_name' => $city->province()->first()->name_fa, + 'city_id' => $city->id, + 'city_name' => $city->name, + 'edareh_shahri_id' => $city->edarateShahri()->first()->id, + 'requested_organization' => $request->organization, + 'plan_group' => $request->plan_group, + 'plan_title' => $request->plan_title, + 'worksheet_id' => $request->worksheet_id, + 'response_options' => json_encode($request->response_options), + 'isic' => $request->isic, + 'primary_area' => $request->primary_area, ]); - return $this->successResponse(); + return $this->successResponse([ + 'city' => $city->edarateShahri()->first()->name_fa, + 'province' => $city->province()->first()->name_fa, + ]); } } diff --git a/app/Http/Controllers/V3/Dashboard/Mission/ControlUnitController.php b/app/Http/Controllers/V3/Dashboard/Mission/ControlUnitController.php index e76e92af..588ea09c 100644 --- a/app/Http/Controllers/V3/Dashboard/Mission/ControlUnitController.php +++ b/app/Http/Controllers/V3/Dashboard/Mission/ControlUnitController.php @@ -72,14 +72,12 @@ class ControlUnitController extends Controller public function noProcess(NoProcessRequest $request): JsonResponse { - DB::transaction(function () use ($request) { $user = auth()->user(); $zone= $request->zone; $type= MissionTypes::NO_PROCESS->value; $state = MissionStates::CREATED_NO_PROCESS->value; - $mission = Mission::query()->create([ 'user_id' => $user->id, 'username' => $user->username, @@ -107,6 +105,7 @@ class ControlUnitController extends Controller $mission->machines()->attach($request->machines); $mission->rahdaran()->attach($request->driver, ['is_driver' => true]); }); - return $this->successResponse(); + + return $this->successResponse(); } } diff --git a/app/Listeners/V3/Dashboard/Mission/SendDataToFMSListener.php b/app/Listeners/V3/Dashboard/Mission/SendDataToFMSListener.php index afc06944..500f1ed1 100644 --- a/app/Listeners/V3/Dashboard/Mission/SendDataToFMSListener.php +++ b/app/Listeners/V3/Dashboard/Mission/SendDataToFMSListener.php @@ -25,6 +25,15 @@ class SendDataToFMSListener implements ShouldQueue */ public function handle(SendDataToFMSEvent $event): void { - $this->getErrorRateService->run($event->mission); + $mission = $event->mission; + + $this->getErrorRateService->setInputParameters([ + 'area' => $mission->area, + 'start_date' => $mission->start_date, + 'end_date' => $mission->end_date, + 'machine_code' => $mission->machines()->first('cmms_machines.machine_code') + ]); + + $this->getErrorRateService->run(); } } diff --git a/app/Models/City.php b/app/Models/City.php index fa8cf2ef..088c475d 100644 --- a/app/Models/City.php +++ b/app/Models/City.php @@ -27,7 +27,7 @@ class City extends Model 'geometry', ]; - public function province() + public function province(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo('App\Models\Province'); } @@ -76,7 +76,7 @@ class City extends Model return $this->hasMany('App\Models\RoadConstructionRural'); } - public function edarateShahri() + public function edarateShahri(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany('App\Models\EdarateShahri', 'edarate_shahri_be_city')->withPivot('is_primary','id'); } diff --git a/database/migrations/2025_07_30_153041_create_harims_table.php b/database/migrations/2025_07_30_153041_create_harims_table.php index 3175ca76..67f85ddf 100644 --- a/database/migrations/2025_07_30_153041_create_harims_table.php +++ b/database/migrations/2025_07_30_153041_create_harims_table.php @@ -43,6 +43,8 @@ return new class extends Migration $table->string('payment_amount')->nullable(); $table->boolean('need_access_road')->nullable(); $table->boolean('access_road_allowed')->nullable(); + $table->string('lat')->nullable(); + $table->string('lng')->nullable(); $table->timestamps(); }); }