255 lines
10 KiB
PHP
255 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\V3\Dashboard;
|
|
|
|
use App\Facades\DataTable\DataTableFacade;
|
|
use App\Facades\Sms\Sms;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Traits\ApiResponse;
|
|
use App\Models\Accident;
|
|
use App\Models\City;
|
|
use App\Models\DailyAccidentSettings;
|
|
use App\Models\Damage;
|
|
use App\Models\Province;
|
|
use App\Services\NominatimService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ReceiptController extends Controller
|
|
{
|
|
use ApiResponse;
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$allowedFilters = ['*'];
|
|
$allowedSortings = ['*'];
|
|
|
|
$user = auth()->user();
|
|
$query = null;
|
|
|
|
if ($user->hasPermissionTo('show-receipt')) {
|
|
$query = Accident::query();
|
|
} else {
|
|
if (is_null($user->province_id)) {
|
|
return $this->errorResponse('استانی برای شما در سامانه ثبت نشده است!');
|
|
}
|
|
|
|
$query = Accident::query()->where('province_id', '=', $user->province_id);
|
|
}
|
|
|
|
$data = DataTableFacade::run(
|
|
$query,
|
|
$request,
|
|
allowedFilters: $allowedFilters,
|
|
allowedSortings: $allowedSortings
|
|
);
|
|
|
|
|
|
$user->addActivityComplete(1122);
|
|
|
|
return $this->successResponse($data);
|
|
}
|
|
|
|
public function store(Request $request, NominatimService $nominatimService)
|
|
{
|
|
DB::transaction(function () use ($request, $nominatimService) {
|
|
|
|
$province = Province::query()->where('id', '=', $request->province_id)->first();
|
|
$city = City::query()->where('id', '=', $request->city_id)->first();
|
|
|
|
$accident = [
|
|
'province_id' => $province->id,
|
|
'province_fa' => $province->name_fa,
|
|
'city_id' => $city->id,
|
|
'city_fa' => $city->name_fa,
|
|
'axis_name' => $request->axis_name,
|
|
'driver_name' => $request->driver_name,
|
|
'plaque' => $request->plaque,
|
|
'driver_national_code' => $request->driver_national_code,
|
|
'driver_phone_number' => $request->driver_phone_number,
|
|
'user_id' => auth()->user()->id,
|
|
'lat' => $request->lat,
|
|
'lng' => $request->lng,
|
|
'accident_type' => $request->accident_type,
|
|
'accident_date' => $request->accident_date,
|
|
'accident_time' => $request->accident_time,
|
|
'accident_type_fa' => DailyAccidentSettings::query()->where('type', 'accident_type')->where('name', $request->accident_type)->first()->value,
|
|
'way_id' => $nominatimService->get_way_id_from_nominatim($request->lat, $request->lng),
|
|
'report_base' => $request->report_base != null ? 1 : null,
|
|
'police_file' => '',
|
|
];
|
|
|
|
if ($request->report_base) {
|
|
$accident['report_base'] = 1;
|
|
} else {
|
|
$accident['police_file'] = $request->file('police_file')->storeAs('receipts_files/'.$accident->id, 'police_file_'.$accident->id.'_'.time().'.'.$request->file('police_file')->extension(), 'public');
|
|
$accident['police_serial'] = $request->police_serial;
|
|
$accident['police_file_date'] = $request->police_file_date;
|
|
$accident['report_base'] = 0;
|
|
}
|
|
|
|
if ($request->has('damage_picture1')) {
|
|
$new_accident->damage_picture1 = $request->file('damage_picture1')->storeAs('receipts_files/'.$new_accident->id, 'damage_picture1_'.$new_accident->id.'_'.time().'.'.$request->file('damage_picture1')->extension(), 'public');
|
|
}
|
|
|
|
if ($request->has('damage_picture2')) {
|
|
$new_accident->damage_picture2 = $request->file('damage_picture2')->storeAs('receipts_files/'.$new_accident->id, 'damage_picture2_'.$new_accident->id.'_'.time().'.'.$request->file('damage_picture2')->extension(), 'public');
|
|
}
|
|
|
|
$new_accident->save();
|
|
$sum = 0;
|
|
foreach (json_decode($request->items_damge) as $key => $item) {
|
|
$damage = Damage::find($item->id);
|
|
$new_accident->damages()->attach($item->id, [
|
|
'value' => $item->value,
|
|
'unit' => $damage->unit,
|
|
'amount' => $item->amount,
|
|
]);
|
|
$sum += $item->amount;
|
|
}
|
|
|
|
$ojrate_nasb = $sum/100*20;
|
|
$new_accident->ojrate_nasb = $ojrate_nasb;
|
|
|
|
$sum += $ojrate_nasb;
|
|
$new_accident->sum = $sum;
|
|
$new_accident->withoutAction();
|
|
$new_accident->save();
|
|
$msg = "درخواست شما بابت پرداخت خسارت وارده به ابنیه فنی و تاسیسات راه با کد یکتا ".$new_accident->id." ثبت شد.\n".verta()->now()->format('Y/m/d H:i') ;
|
|
Sms::sendSms($request->phone_number, $msg);
|
|
auth()->user()->addActivityComplete(1123);
|
|
});
|
|
}
|
|
|
|
public function show(Accident $accident): JsonResponse
|
|
{
|
|
return $this->successResponse($accident->load('damages'));
|
|
}
|
|
|
|
public function update(Accident $accident, Request $request, NominatimService $nominatimService)
|
|
{
|
|
DB::transaction(function () use ($request, $accident, $nominatimService) {
|
|
$accident->province_fa = Province::where('id', $request->province_id)->first()->name_fa;
|
|
$accident->province_id = $request->province_id;
|
|
|
|
$accident->city_fa = City::where('id', $request->city_id)->first()->name_fa;
|
|
$accident->city_id = $request->city_id;
|
|
|
|
$accident->axis_name = $request->axis_name;
|
|
$accident->driver_name = $request->driver_name;
|
|
$accident->plaque = $request->plaque;
|
|
$accident->driver_national_code = $request->national_code;
|
|
$accident->driver_phone_number = $request->phone_number;
|
|
|
|
$accident->lat = $request->lat;
|
|
$accident->lng = $request->lng;
|
|
|
|
$accident->accident_type = $request->accident_type;
|
|
$accident->accident_type_fa = DailyAccidentSettings::where('type', 'accident_type')->where('name', $request->accident_type)->first()->value;
|
|
$accident->accident_date = $request->accident_date;
|
|
$accident->accident_time = $request->accident_time;
|
|
|
|
$accident->way_id = $nominatimService->get_way_id_from_nominatim($request->lat, $request->lng);
|
|
|
|
$accident->save();
|
|
|
|
if ($request->report_base) {
|
|
\File::delete('storage/'.$accident->police_file);
|
|
$accident->police_file = null;
|
|
$accident->police_serial = null;
|
|
$accident->report_base = 1;
|
|
} else {
|
|
if ($accident->report_base) {
|
|
if ($request->has('police_file')) {
|
|
\File::delete('storage/'.$accident->police_file);
|
|
$accident->police_file = $request->file('police_file')->storeAs('receipts_files/'.$accident->id, 'police_file_'.$accident->id.'_'.time().'.'.$request->file('police_file')->extension(), 'public');
|
|
} else {
|
|
abort(401, 'please send the file');
|
|
}
|
|
} else {
|
|
if ($request->has('police_file')) {
|
|
\File::delete('storage/'.$accident->police_file);
|
|
$accident->police_file = $request->file('police_file')->storeAs('receipts_files/'.$accident->id, 'police_file_'.$accident->id.'_'.time().'.'.$request->file('police_file')->extension(), 'public');
|
|
}
|
|
}
|
|
|
|
$accident->police_file_date = $request->police_file_date;
|
|
$accident->police_serial = $request->police_serial;
|
|
$accident->report_base = 0;
|
|
}
|
|
|
|
$accident->save();
|
|
|
|
if ($request->has('damage_picture1')) {
|
|
\File::delete('storage/'.$accident->damage_picture1);
|
|
$accident->damage_picture1 = $request->file('damage_picture1')->storeAs('receipts_files/'.$accident->id, 'damage_picture1_'.$accident->id.'_'.time().'.'.$request->file('damage_picture1')->extension(), 'public');
|
|
} elseif ($request->has('damage_picture1_is_delete')) {
|
|
\File::delete('storage/'.$accident->damage_picture1);
|
|
$accident->damage_picture1 = null;
|
|
}
|
|
|
|
if ($request->has('damage_picture2')) {
|
|
\File::delete('storage/'.$accident->damage_picture2);
|
|
$accident->damage_picture2 = $request->file('damage_picture2')->storeAs('receipts_files/'.$accident->id, 'damage_picture2_'.$accident->id.'_'.time().'.'.$request->file('damage_picture2')->extension(), 'public');
|
|
} elseif ($request->has('damage_picture2_is_delete')) {
|
|
\File::delete('storage/'.$accident->damage_picture2);
|
|
$accident->damage_picture2 = null;
|
|
}
|
|
|
|
$accident->save();
|
|
$sum = 0;
|
|
$accident->damages()->detach();
|
|
foreach (json_decode($request->items_damge) as $key => $item) {
|
|
$damage = Damage::find($item->id);
|
|
$accident->damages()->attach($item->id, [
|
|
'value' => $item->value,
|
|
'unit' => $damage->unit,
|
|
'amount' => $item->amount,
|
|
]);
|
|
$sum += $item->amount;
|
|
}
|
|
|
|
|
|
$ojrate_nasb = $sum/100*20;
|
|
$accident->ojrate_nasb = $ojrate_nasb;
|
|
|
|
$sum += $ojrate_nasb;
|
|
$accident->sum = $sum;
|
|
|
|
$accident->save();
|
|
|
|
auth()->user()->addActivityComplete(1124);
|
|
});
|
|
}
|
|
|
|
public function delete(Accident $accident)
|
|
{
|
|
\File::delete('storage/receipts_files/'.$accident->id);
|
|
|
|
$accident->damages()->detach();
|
|
$accident->delete();
|
|
auth()->user()->addActivityComplete(1125);
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
public function sendToInsurance(Request $request, Accident $accident)
|
|
{
|
|
DB::transaction(function () use ($request, $accident) {
|
|
$accident->sendToInsurance();
|
|
$accident->save();
|
|
|
|
auth()->user()->addActivityComplete(1126);
|
|
});
|
|
|
|
$account_data = Province::query()->where('id', $accident->province_id)->first();
|
|
|
|
return $this->successResponse([
|
|
$account_data,
|
|
$accident->load('damages')
|
|
]);
|
|
}
|
|
}
|