352 lines
14 KiB
PHP
352 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\V3\Dashboard;
|
|
|
|
use App\Enums\AccidentStates;
|
|
use App\Exports\V3\AccidentReceipt\DataTableReport;
|
|
use App\Facades\DataTable\DataTableFacade;
|
|
use App\Facades\File\FileFacade;
|
|
use App\Facades\Sms\Sms;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\V3\AccidentReceipt\ConfirmPaymentInfoRequest;
|
|
use App\Http\Requests\V3\AccidentReceipt\StoreRequest;
|
|
use App\Http\Requests\V3\AccidentReceipt\UpdateRequest;
|
|
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\Cartables\AccidentReceiptTableService;
|
|
use App\Services\NominatimService;
|
|
use App\Services\PaymentService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
class AccidentReceiptController extends Controller
|
|
{
|
|
use ApiResponse;
|
|
|
|
public function index(Request $request, AccidentReceiptTableService $accidentReceiptTableService): JsonResponse
|
|
{
|
|
$data = $accidentReceiptTableService->dataTable($request);
|
|
|
|
auth()->user()->addActivityComplete(1122);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
public function excelReport(Request $request, AccidentReceiptTableService $accidentReceiptTableService): BinaryFileResponse
|
|
{
|
|
auth()->user()->addActivityComplete(1126);
|
|
$name = 'گزارش از خسارات وارده به ابنیه فنی و تاسیسات راه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
|
|
$data = $accidentReceiptTableService->dataTable($request);
|
|
return Excel::download(new DataTableReport($data), $name);
|
|
}
|
|
|
|
public function accidentDamage(Accident $accident): JsonResponse
|
|
{
|
|
return $this->successResponse($accident->damages()->get(['unit', 'amount', 'value']));
|
|
}
|
|
|
|
public function store(StoreRequest $request, NominatimService $nominatimService): JsonResponse
|
|
{
|
|
DB::transaction(function () use ($request, $nominatimService) {
|
|
|
|
$province = Province::query()->where('id', '=', $request->province_id)->first();
|
|
$city = City::query()->where('id', '=', $request->city_id)->first();
|
|
|
|
$accidentData = [
|
|
'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,
|
|
'police_file' => $request->report_base ? null : FileFacade::save($request->file('police_file'), 'receipts_files/'),
|
|
'police_serial' => $request->police_serial ?? null,
|
|
'police_file_date' => $request->police_file_date ?? null,
|
|
'damage_picture1' => FileFacade::save($request->file('damage_picture1'), 'receipts_files/'),
|
|
'damage_picture2' => FileFacade::save($request->file('damage_picture2'), 'receipts_files/'),
|
|
'status' => AccidentStates::BEDON_EGHDAM->value,
|
|
'status_fa' => AccidentStates::name(AccidentStates::BEDON_EGHDAM->value),
|
|
];
|
|
|
|
$sum = 0;
|
|
$damageData = [];
|
|
|
|
foreach ($request->items_damge as $key => $damage) {
|
|
$damage = Damage::query()->find($damage->id);
|
|
$damageData[$damage->id] = [
|
|
'value' => $damage->value,
|
|
'unit' => $damage->unit,
|
|
'amount' => $damage->amount,
|
|
];
|
|
$sum += $damage->amount;
|
|
}
|
|
|
|
$ojrate_nasb = $sum/100*20;
|
|
$sum += $ojrate_nasb;
|
|
|
|
$accidentData['ojrate_nasb'] = $ojrate_nasb;
|
|
$accidentData['sum'] = $sum;
|
|
|
|
$accident = Accident::query()->create($accidentData);
|
|
|
|
$accident->damages()->attach($damageData);
|
|
|
|
auth()->user()->addActivityComplete(1123);
|
|
});
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
public function show(Accident $accident): JsonResponse
|
|
{
|
|
return $this->successResponse($accident->load('damages'));
|
|
}
|
|
|
|
public function update(Accident $accident, UpdateRequest $request, NominatimService $nominatimService): JsonResponse
|
|
{
|
|
DB::transaction(function () use ($request, $accident, $nominatimService) {
|
|
|
|
$province = Province::query()->where('id', '=', $request->province_id)->first();
|
|
$city = City::query()->where('id', '=', $request->city_id)->first();
|
|
|
|
if ($request->report_base && $accident->police_file) {
|
|
FileFacade::delete($accident->police_file, true);
|
|
}
|
|
|
|
if (!$request->report_base) {
|
|
FileFacade::delete($accident->police_file, true);
|
|
$police_file = FileFacade::save($request->file('police_file'), 'receipts_files/');
|
|
}
|
|
|
|
if ($request->has('damage_picture1')) {
|
|
FileFacade::delete($accident->damage_picture1, true);
|
|
$damage_picture1 = FileFacade::save($request->file('damage_picture1'), 'receipts_files/');
|
|
}
|
|
if ($request->has('damage_picture2')) {
|
|
FileFacade::delete($accident->damage_picture2, true);
|
|
$damage_picture2 = FileFacade::save($request->file('damage_picture2'), 'receipts_files/');
|
|
}
|
|
|
|
$accidentData = [
|
|
'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,
|
|
'police_file' => $police_file ?? null,
|
|
'police_serial' => $request->police_serial ?? null,
|
|
'police_file_date' => $request->police_file_date ?? null,
|
|
'damage_picture1' => $damage_picture1 ?? null,
|
|
'damage_picture2' => $damage_picture2 ?? null,
|
|
];
|
|
|
|
$sum = 0;
|
|
$damageData = [];
|
|
|
|
foreach ($request->items_damge as $key => $item) {
|
|
$damage = Damage::query()->find($item->id);
|
|
$damageData[$damage->id] = [
|
|
'value' => $damage->value,
|
|
'unit' => $damage->unit,
|
|
'amount' => $damage->amount,
|
|
];
|
|
$sum += $item->amount;
|
|
}
|
|
|
|
$ojrate_nasb = $sum/100*20;
|
|
$sum += $ojrate_nasb;
|
|
|
|
$accidentData['ojrate_nasb'] = $ojrate_nasb;
|
|
$accidentData['sum'] = $sum;
|
|
|
|
$accident->update($accidentData);
|
|
|
|
$accident->damages()->sync($damageData);
|
|
|
|
auth()->user()->addActivityComplete(1124);
|
|
});
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
public function destroy(Accident $accident): JsonResponse
|
|
{
|
|
FileFacade::delete('storage/receipts_files/'.$accident->id);
|
|
|
|
DB::transaction(function () use ($accident) {
|
|
|
|
$accident->damages()->detach();
|
|
|
|
$accident->delete();
|
|
|
|
auth()->user()->addActivityComplete(1125);
|
|
});
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
public function generateInsuranceLetter(Accident $accident): JsonResponse
|
|
{
|
|
DB::transaction(function () use ($accident) {
|
|
$accident->update([
|
|
'status' => AccidentStates::SODOR_NAME_BIME_VA_DAGHI->value,
|
|
'status_fa' => AccidentStates::name(AccidentStates::SODOR_NAME_BIME_VA_DAGHI->value),
|
|
]);
|
|
|
|
auth()->user()->addActivityComplete(1126);
|
|
});
|
|
|
|
$account_data = Province::query()->where('id', $accident->province_id)->first();
|
|
|
|
return $this->successResponse([
|
|
$account_data,
|
|
$accident->load('damages')
|
|
]);
|
|
}
|
|
|
|
public function confirmPaymentInfo(ConfirmPaymentInfoRequest $request, Accident $accident): JsonResponse
|
|
{
|
|
DB::transaction(function () use ($request, $accident) {
|
|
if ($request->has('deposit_insurance_status')) {
|
|
if ($request->has('deposit_insurance_image')) {
|
|
$deposit_insurance_image = $request->file('deposit_insurance_image')->storeAs('receipts_files/'.$accident->id, 'deposit_insurance_image_'.$accident->id.'_'.time().'.'.$request->file('deposit_insurance_image')->extension(), 'public');
|
|
}
|
|
|
|
$deposit_insurance_amount = $request->deposit_insurance_amount;
|
|
} else {
|
|
$accident->deposit_insurance_image = null;
|
|
$accident->deposit_insurance_amount = null;
|
|
}
|
|
|
|
if ($request->has('deposit_daghi_status')) {
|
|
if ($request->has('deposit_daghi_image')) {
|
|
$deposit_daghi_image = $request->file('deposit_daghi_image')->storeAs('receipts_files/'.$accident->id, 'deposit_daghi_image_'.$accident->id.'_'.time().'.'.$request->file('deposit_daghi_image')->extension(), 'public');
|
|
}
|
|
|
|
$deposit_daghi_amount = $request->deposit_daghi_amount;
|
|
} else {
|
|
$accident->deposit_daghi_image = null;
|
|
$accident->deposit_daghi_amount = null;
|
|
}
|
|
|
|
$accident->update([
|
|
'deposit_insurance_image' => $deposit_insurance_image,
|
|
'deposit_insurance_amount' => $deposit_insurance_amount,
|
|
'deposit_daghi_image' => $deposit_daghi_image,
|
|
'deposit_daghi_amount' => $deposit_daghi_amount,
|
|
'status' => AccidentStates::SABT_FISH->value,
|
|
'status_fa' => AccidentStates::name(AccidentStates::SABT_FISH->value),
|
|
]);
|
|
|
|
auth()->user()->addActivityComplete(1132);
|
|
});
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
public function submitInvoice(Accident $accident, PaymentService $paymentService): JsonResponse
|
|
{
|
|
$final_amount = $accident->sum - ($accident->deposit_insurance_amount + $accident->deposit_daghi_amount);
|
|
|
|
if ($final_amount < 0) {
|
|
return $this->errorResponse('error');
|
|
}
|
|
|
|
$bill_code = $paymentService->invoiceBillApi($accident->driver_national_code, $final_amount);
|
|
|
|
DB::transaction(function () use ($bill_code, $final_amount, $accident) {
|
|
|
|
$accident->update([
|
|
'final_amount' => $final_amount,
|
|
'bill_code' => $bill_code,
|
|
'status' => AccidentStates::SODOR_FACTOR->value,
|
|
'status_fa' => AccidentStates::name(AccidentStates::SODOR_FACTOR->value),
|
|
]);
|
|
|
|
auth()->user()->addActivityComplete(1129);
|
|
});
|
|
|
|
$msg = "فاکتور با شناسه پرداخت \n" . explode("/", $accident->bill_code)[0]. "\n در سامانه سازمان راهداری ثبت شد. برای پرداخت از لینک زیر اقدام نمایید.\n\n".env("PAYMENT_LINK")."/#/pay/".explode("/", $accident->bill_code)[0]."/$final_amount";
|
|
|
|
Sms::sendSms($accident->driver_phone_number, $msg);
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
public function checkPaymentStatus(Accident $accident, PaymentService $paymentService): JsonResponse
|
|
{
|
|
DB::transaction(function () use ($accident, $paymentService) {
|
|
if ($accident->final_amount > 0) {
|
|
|
|
$response = json_decode($paymentService->callPaymentStatusBillApi(explode("/", $accident->bill_code)[0]));
|
|
|
|
if ($response->isPayed) {
|
|
$accident->update([
|
|
'status' => AccidentStates::PARDAKHT_FACTOR->value,
|
|
'status_fa' => AccidentStates::name(AccidentStates::PARDAKHT_FACTOR->value),
|
|
]);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$accident->update([
|
|
'status' => AccidentStates::PARDAKHT_FACTOR->value,
|
|
'status_fa' => AccidentStates::name(AccidentStates::PARDAKHT_FACTOR->value),
|
|
]);
|
|
}
|
|
|
|
auth()->user()->addActivityComplete(1130);
|
|
});
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
public function generatePoliceDocument(Request $request, Accident $accident): JsonResponse
|
|
{
|
|
DB::transaction(function () use ($request, $accident) {
|
|
|
|
$accident->update([
|
|
'status' => AccidentStates::SODOR_NAME_POLICE_RAH->value,
|
|
'status_fa' => AccidentStates::name(AccidentStates::SODOR_NAME_POLICE_RAH->value),
|
|
]);
|
|
|
|
auth()->user()->addActivityComplete(1131);
|
|
});
|
|
|
|
return $this->successResponse();
|
|
}
|
|
}
|