transfer road item project from v2 to v3
This commit is contained in:
314
app/Http/Controllers/V3/RoadItemsProjectController.php
Normal file
314
app/Http/Controllers/V3/RoadItemsProjectController.php
Normal file
@@ -0,0 +1,314 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\V3;
|
||||
|
||||
use App\Exports\V2\RoadItems\SupervisorCartableExport;
|
||||
use App\Facades\DataTable\DataTableFacade;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\V3\RoadItemsProject\StoreRequest;
|
||||
use App\Http\Requests\V3\RoadItemsProject\VerifyBySupervisorRequest;
|
||||
use App\Http\Traits\ApiResponse;
|
||||
use App\Models\EdarateShahri;
|
||||
use App\Models\InfoItem;
|
||||
use App\Models\RoadItemsProject;
|
||||
use App\Services\NominatimService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
|
||||
class RoadItemsProjectController extends Controller
|
||||
{
|
||||
use ApiResponse;
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function supervisorIndex(Request $request): JsonResponse
|
||||
{
|
||||
$columns = array(
|
||||
'id', 'user_id', 'start_lat', 'start_lng', 'end_lat',
|
||||
'end_lng', 'project_distance', 'item', 'item_fa', 'sub_item_fa',
|
||||
'sub_item_data', 'created_at', 'updated_at', 'province_id', 'province_fa',
|
||||
'unit_fa', 'status', 'status_fa', 'supervisor_description', 'supervising_time',
|
||||
'supervisor_name', 'edarat_id', 'edarat_name', 'activity_date_time'
|
||||
);
|
||||
|
||||
$allowedFilters = $columns;
|
||||
$allowedSortings = $columns;
|
||||
|
||||
$user = auth()->user();
|
||||
$query = null;
|
||||
|
||||
if ($user->hasPermissionTo('show-road-item-supervise-cartable')) {
|
||||
$query = RoadItemsProject::query()->where('is_new', 1)->with('files');
|
||||
}
|
||||
elseif ($user->hasPermissionTo('show-road-item-supervise-cartable-province')) {
|
||||
if (is_null($user->province_id)) {
|
||||
return $this->errorResponse('استانی برای شما در سامانه ثبت نشده است!');
|
||||
}
|
||||
$query = RoadItemsProject::query()->where('is_new', 1)
|
||||
->with('files')->where('province_id', auth()->user()->province_id);
|
||||
}
|
||||
|
||||
$data = DataTableFacade::run(
|
||||
$query,
|
||||
$request,
|
||||
allowedFilters: $allowedFilters,
|
||||
allowedSortings: $allowedSortings);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public function verifyBySupervisor(VerifyBySupervisorRequest $request, RoadItemsProject $road_item): JsonResponse
|
||||
{
|
||||
$status_fa = $request->verify == 1 ? 'تایید شده' : 'عدم تایید';
|
||||
$user = auth()->user();
|
||||
|
||||
$road_item->update([
|
||||
'status' => $request->verify,
|
||||
'status_fa' => $status_fa,
|
||||
'supervisor_id' => $user->id,
|
||||
'supervisor_name' => $user->name,
|
||||
'supervisor_description' => $request->description,
|
||||
'supervising_time' => now(),
|
||||
]);
|
||||
|
||||
$user->addActivityComplete(1149);
|
||||
|
||||
return $this->successResponse();
|
||||
}
|
||||
|
||||
public function restore(RoadItemsProject $road_item): JsonResponse
|
||||
{
|
||||
if ($road_item->status == 0) {
|
||||
return response()->json([
|
||||
'message' => 'امکان بازگردانی وضعیت این فعالیت وجود ندارد!'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$road_item->update([
|
||||
'status' => 0,
|
||||
'status_fa' => 'در حال بررسی',
|
||||
'supervisor_id' => null,
|
||||
'supervisor_description' => null,
|
||||
'supervising_time' => null,
|
||||
'supervisor_name' => null,
|
||||
]);
|
||||
auth()->user()->addActivityComplete(1150);
|
||||
|
||||
return $this->successResponse();
|
||||
}
|
||||
|
||||
public function delete(RoadItemsProject $road_item): JsonResponse
|
||||
{
|
||||
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();
|
||||
}
|
||||
auth()->user()->addActivityComplete(1151);
|
||||
|
||||
$road_item->delete();
|
||||
|
||||
return $this->successResponse();
|
||||
}
|
||||
|
||||
|
||||
public function supervisorCartableReport(Request $request)
|
||||
{
|
||||
$name = 'گزارش از کارتابل ارزیابی فعالیت روزانه ' . verta()->now()->format('Y-m-d H:i') . '.xlsx';
|
||||
return Excel::download(new SupervisorCartableExport($request->id, $request->item, $request->status,
|
||||
$request->fromDate, $request->toDate, $request->province_id, $request->edarat_id), $name);
|
||||
}
|
||||
|
||||
public function operatorIndex(Request $request): JsonResponse
|
||||
{
|
||||
$columns = array(
|
||||
'id', 'user_id', 'start_lat', 'start_lng', 'end_lat',
|
||||
'end_lng', 'project_distance', 'item', 'item_fa', 'sub_item_fa',
|
||||
'sub_item_data', 'created_at', 'updated_at', 'province_id', 'province_fa',
|
||||
'unit_fa', 'status', 'status_fa', 'supervisor_description', 'supervising_time',
|
||||
'supervisor_name', 'edarat_id', 'edarat_name', 'activity_date_time'
|
||||
);
|
||||
|
||||
$allowedFilters = $columns;
|
||||
$allowedSortings = $columns;
|
||||
|
||||
$query = RoadItemsProject::with('files')
|
||||
->where('is_new', 1)
|
||||
->where('user_id', auth()->user()->id);
|
||||
|
||||
$data = DataTableFacade::run(
|
||||
$query,
|
||||
$request,
|
||||
allowedFilters: $allowedFilters,
|
||||
allowedSortings: $allowedSortings);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function store(StoreRequest $request, NominatimService $nominatimService): JsonResponse
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
$info_item = InfoItem::where('item', $request->item_id)
|
||||
->where('sub_item', $request->sub_item_id)
|
||||
->firstOrFail();
|
||||
|
||||
if ($info_item->needs_end_point && !$request->end_point) {
|
||||
throw ValidationException::withMessages([
|
||||
'end_point' => __('validation.required', ['attribute' => 'end_point']),
|
||||
]);
|
||||
}
|
||||
|
||||
if ((!$request->before_image || !$request->after_image) && $info_item->needs_image) {
|
||||
throw ValidationException::withMessages([
|
||||
'before_image' => __('validation.required', ['attribute' => 'before_image']),
|
||||
'after_image' => __('validation.required', ['attribute' => 'after_image']),
|
||||
]);
|
||||
}
|
||||
|
||||
$start_coordinates = explode(',', $request->start_point);
|
||||
$end_coordinates = $info_item->needs_end_point ? explode(',', $request->end_point) : null;
|
||||
|
||||
$attributes = [
|
||||
'start_lat' => $start_coordinates[0],
|
||||
'start_lng' => $start_coordinates[1],
|
||||
'end_lat' => $end_coordinates ? $end_coordinates[0] : null,
|
||||
'end_lng' => $end_coordinates ? $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' => $request->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($start_coordinates[0], $start_coordinates[1]),
|
||||
'end_way_id' => $end_coordinates ? $nominatimService->get_way_id_from_nominatim($end_coordinates[0], $end_coordinates[1]) : null,
|
||||
'unit_fa' => $info_item->sub_item_unit,
|
||||
'created_at_fa' => verta(\Carbon\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' => $request->activity_date,
|
||||
'activity_time' => $request->activity_time,
|
||||
];
|
||||
|
||||
$after_image = ($request->has('after_image') && $info_item->needs_image) ?
|
||||
$request->file('after_image') :
|
||||
null;
|
||||
|
||||
$before_image = ($request->has('before_image') && $info_item->needs_image) ?
|
||||
$request->file('before_image') :
|
||||
null;
|
||||
|
||||
$road_item = RoadItemsProject::store($user, $attributes, $before_image, $after_image, $request->observed_item_id);
|
||||
|
||||
if ($road_item == -1) {
|
||||
return response()->json([
|
||||
'message' => 'اقدام مربوطه قبلا رسیدگی شده است.'
|
||||
], 400);
|
||||
}
|
||||
auth()->user()->addActivityComplete(1154);
|
||||
|
||||
return $this->successResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, RoadItemsProject $road_item)
|
||||
{
|
||||
$info_item = InfoItem::where('item', $road_item->item)
|
||||
->where('sub_item', $road_item->sub_item)
|
||||
->firstOrFail();
|
||||
|
||||
$start_coordinates = explode(',', $request->start_point);
|
||||
$end_coordinates = null;
|
||||
|
||||
if ($info_item->needs_end_point) {
|
||||
if (!$request->end_point) {
|
||||
throw ValidationException::withMessages([
|
||||
'end_point' => __('validation.required', ['attribute' => 'end_point']),
|
||||
]);
|
||||
}
|
||||
|
||||
$end_coordinates = explode(',', $request->end_point);
|
||||
}
|
||||
|
||||
if ($info_item->needs_image) {
|
||||
if ($request->has('after_image')) {
|
||||
|
||||
Storage::delete('public/'. $road_item->files[0]->path);
|
||||
|
||||
$after = $request->file('after_image')->store('road_items_projects_new/after', 'public');
|
||||
$road_item->files[0]->update([
|
||||
'path' => $after
|
||||
]);
|
||||
// $road_item->files()->create(['path' => $after]);
|
||||
$urlAfter = "/var/www/rms/public/storage/{$after}";
|
||||
\App\Helpers\Compress::resize($urlAfter);
|
||||
}
|
||||
if ($request->has('before_image')) {
|
||||
Storage::delete('public/'. $road_item->files[1]->path);
|
||||
|
||||
$before = $request->file('before_image')->store('/road_items_projects_new/before', 'public');
|
||||
$road_item->files[1]->update([
|
||||
'path' => $before
|
||||
]);
|
||||
// $road_item->files()->create(['path' => $before]);
|
||||
$urlBefore = "/var/www/rms/public/storage/{$before}";
|
||||
\App\Helpers\Compress::resize($urlBefore);
|
||||
}
|
||||
}
|
||||
|
||||
$road_item->update([
|
||||
'start_lat' => $start_coordinates[0],
|
||||
'start_lng' => $start_coordinates[1],
|
||||
'end_lat' => $end_coordinates ? $end_coordinates[0] : null,
|
||||
'end_lng' => $end_coordinates ? $end_coordinates[1] : null,
|
||||
'sub_item_data' => $request->amount,
|
||||
'status' => 0,
|
||||
'status_fa' => 'در حال بررسی',
|
||||
]);
|
||||
|
||||
|
||||
auth()->user()->addActivityComplete(1155);
|
||||
|
||||
return $this->successResponse();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user