improve cods
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Enums\MissionStates;
|
||||
use App\Enums\MissionTypes;
|
||||
use App\Enums\MissionZones;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\V3\Dashboard\RoadPatrol\OperatorController;
|
||||
use App\Http\Requests\V3\Mission\RequestPortal\StoreRequest;
|
||||
use App\Http\Requests\V3\Mission\RequestPortal\UpdateRequest;
|
||||
use App\Http\Traits\ApiResponse;
|
||||
@@ -59,6 +60,12 @@ class RequestPortalController extends Controller
|
||||
]);
|
||||
|
||||
$mission->rahdaran()->attach($request->rahdaran);
|
||||
|
||||
if ($request->category_id == 2) {
|
||||
$rpc = new OperatorController();
|
||||
$rpc->store($mission);
|
||||
}
|
||||
|
||||
if ($request->category_id == 3){
|
||||
RoadObserved::query()
|
||||
->find($request->road_observed_id)
|
||||
|
||||
@@ -27,6 +27,7 @@ class TransportationUnitController extends Controller
|
||||
{
|
||||
DB::transaction(function () use ($request, $mission) {
|
||||
$mission->machines()->attach($request->machines);
|
||||
$mission->rahdaran()->attach($request->driver, ['is_driver'=>true]);
|
||||
|
||||
$mission->histories()->create([
|
||||
'user_id' => auth()->user()->id,
|
||||
|
||||
@@ -4,10 +4,14 @@ namespace App\Http\Controllers\V3\Dashboard;
|
||||
|
||||
use App\Facades\DataTable\DataTableFacade;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\V3\ObservedItem\StoreRequest;
|
||||
use App\Http\Traits\ApiResponse;
|
||||
use App\Models\InfoItem;
|
||||
use App\Models\ObservedItem;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ObservedItemController extends Controller
|
||||
{
|
||||
@@ -37,4 +41,59 @@ class ObservedItemController extends Controller
|
||||
|
||||
return response()->json($dataTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function store(StoreRequest $request): jsonResponse
|
||||
{
|
||||
foreach ($request->observed_items as $index => $item) {
|
||||
$info_item = InfoItem::query()->where('item', $item['item_id'])
|
||||
->where('sub_item', $item['sub_item_id'])
|
||||
->firstOrFail();
|
||||
|
||||
$start = explode(',', $item['start_point']);
|
||||
$priority = [
|
||||
1 => 'عادی',
|
||||
2 => 'فوری',
|
||||
3 => 'آنی',
|
||||
];
|
||||
|
||||
$data = [
|
||||
'road_patrol_id' => $request->road_patrol_id,
|
||||
'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' => $start[0],
|
||||
'start_lon' => $start[1],
|
||||
'priority' => $item['instant_action'] == 0 ? $item['priority'] : null,
|
||||
'priority_fa' => $item['instant_action'] ? $priority[$item['priority']] : null,
|
||||
];
|
||||
|
||||
if ($item['instant_action'] == 1) {
|
||||
$item_end_coordinates = null;
|
||||
|
||||
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']);
|
||||
|
||||
$data['end_lat'] = $item_end_coordinates[0];
|
||||
$data['end_lon'] = $item_end_coordinates[1];
|
||||
}
|
||||
}
|
||||
DB::transaction(function () use ($data) {
|
||||
ObservedItem::query()->create($data);
|
||||
});
|
||||
}
|
||||
|
||||
return $this->successResponse();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,26 +3,18 @@
|
||||
namespace App\Http\Controllers\V3\Dashboard\RoadPatrol;
|
||||
|
||||
|
||||
use App\Enums\MissionStates;
|
||||
use App\Exports\V3\RoadPatrols\OperatorCartableReport;
|
||||
use App\Facades\Sms\Sms;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\V3\RoadPatrol\DeleteRequest;
|
||||
use App\Http\Requests\V3\RoadPatrol\StoreRequest;
|
||||
use App\Http\Traits\ApiResponse;
|
||||
use App\Models\EdarateShahri;
|
||||
use App\Models\InfoItem;
|
||||
use App\Models\Mission;
|
||||
use App\Models\RoadItemsProject;
|
||||
use App\Models\RoadPatrol;
|
||||
use App\Services\Cartables\RoadPatrol\OperatorService;
|
||||
use App\Services\NominatimService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
@@ -43,159 +35,20 @@ class OperatorController extends Controller
|
||||
return Excel::download(new OperatorCartableReport($data['data']), $name);
|
||||
}
|
||||
|
||||
public function store(StoreRequest $request, NominatimService $nominatimService): JsonResponse
|
||||
public function store(Mission $mission): JsonResponse
|
||||
{
|
||||
$user = auth()->user();
|
||||
$road_patrol = RoadPatrol::query()->create([
|
||||
'status' => 0,
|
||||
'status_fa' => 'در حال بررسی',
|
||||
]);
|
||||
|
||||
$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->mission()->attach($mission->id);
|
||||
|
||||
$road_patrol = DB::transaction(function () use (
|
||||
$request, $edare_id,
|
||||
$edare_name, $nominatimService, $user
|
||||
) {
|
||||
$mission = Mission::query()->findOrFail($request->mission_id);
|
||||
|
||||
if ($mission->status !== MissionStates::START_MISSION->value) {
|
||||
throw ValidationException::withMessages([
|
||||
'mission_id' => 'ماموریت انتخاب شده هنوز شروع نشده است.',
|
||||
]);
|
||||
}
|
||||
|
||||
$road_patrol = RoadPatrol::query()->create([
|
||||
'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,
|
||||
'status' => 0,
|
||||
'status_fa' => 'در حال بررسی',
|
||||
'distance' => $request->distance,
|
||||
'start_time' => $request->start_time,
|
||||
'end_time' => $request->end_time,
|
||||
'description' => $request->description ?? null,
|
||||
'fuel_consumption' => $request->fuel_consumption,
|
||||
'vehicle_runtime' => $request->vehicle_runtime,
|
||||
'stop_points' => json_encode($request->stop_points),
|
||||
]);
|
||||
|
||||
$road_patrol->rahdaran()->attach($request->road_patrol_rahdaran_id);
|
||||
$road_patrol->cmmsMachines()->attach($request->road_patrol_machines_id);
|
||||
|
||||
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) {
|
||||
$priority = [
|
||||
1 => 'عادی',
|
||||
2 => 'فوری',
|
||||
3 => 'آنی',
|
||||
];
|
||||
$observed_item->priority = $item['priority'];
|
||||
$observed_item->priority_fa = $priority[$item['priority']];
|
||||
$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 $this->errorResponse('اقدام مربوطه قبلا رسیدگی شده است.');
|
||||
}
|
||||
|
||||
$road_item->rahdaran()->attach($item['road_item_rahdaran_id']);
|
||||
$road_item->cmmsMachines()->attach($item['road_item_machines_id']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
auth()->user()->addActivityComplete(1147);
|
||||
|
||||
Sms::sendSms($mission->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 $this->successResponse();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user