![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/clinic.corals.io/app/Http/Controllers/ |
<?php namespace App\Http\Controllers; use App\DataTables\AppointmentDataTable; use App\DataTables\DoctorAppointmentDataTable; use App\Http\Requests\CreateAppointmentRequest; use App\Http\Requests\CreateFrontAppointmentRequest; use App\Http\Requests\UpdateAppointmentRequest; use App\Models\Address; use App\Models\Appointment; use App\Models\Doctor; use App\Models\Notification; use App\Models\Patient; use App\Models\Service; use App\Models\Transaction; use App\Models\User; use App\Repositories\AppointmentRepository; use Carbon\Carbon; use Exception; use Flash; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\HigherOrderBuilderProxy; use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Routing\Redirector; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\DB; use Illuminate\Support\HigherOrderCollectionProxy; use Stripe\Exception\ApiErrorException; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; use Yajra\DataTables\Facades\DataTables; class AppointmentController extends AppBaseController { /** @var AppointmentRepository */ private $appointmentRepository; public function __construct(AppointmentRepository $appointmentRepo) { $this->appointmentRepository = $appointmentRepo; } /** * @param Request $request * * @throws Exception * * @return Application|Factory|View */ public function index(Request $request) { if ($request->ajax()) { return Datatables::of((new AppointmentDataTable())->get($request->only([ 'status', 'filter_date', 'payment_type', ])))->make(true); } $appointmentStatus = Appointment::ALL_STATUS; return view('appointments.index', compact('appointmentStatus')); } /** * Show the form for creating a new Appointment. * * @return Application|Factory|View */ public function create() { $data = $this->appointmentRepository->getData(); $data['status'] = Appointment::BOOKED_STATUS_ARRAY; $patient = Patient::where('user_id', getLogInUserId())->first(); return view('appointments.create', compact('data', 'patient')); } /** * @param CreateAppointmentRequest $request * * @throws ApiErrorException * * @return JsonResponse */ public function store(CreateAppointmentRequest $request) { $input = $request->all(); $appointment = $this->appointmentRepository->store($input); if ($input['payment_type'] == Appointment::STRIPE) { $result = $this->appointmentRepository->createSession($appointment); return $this->sendResponse($result, 'Session created successfully.'); } $url = route('appointments.index'); if (getLogInUser()->hasRole('patient')) { $url = route('patients.appointments.index'); } $data = [ 'url' => $url, 'payment_type' => $input['payment_type'], ]; return $this->sendResponse($data, 'Appointment created successfully.'); } /** * Display the specified Appointment. * * @param Appointment $appointment * @return Application|RedirectResponse|Redirector */ public function show(Appointment $appointment) { if (getLogInUser()->hasRole('doctor')) { $doctor = Appointment::whereId($appointment->id)->whereDoctorId(getLogInUser()->doctor->id); if (! $doctor->exists()) { return redirect()->back(); } } elseif (getLogInUser()->hasRole('patient')) { $patient = Appointment::whereId($appointment->id)->wherePatientId(getLogInUser()->patient->id); if (! $patient->exists()) { return redirect()->back(); } } $appointment = $this->appointmentRepository->showAppointment($appointment); if (empty($appointment)) { Flash::error('Appointment not found'); if (getLogInUser()->hasRole('patient')) { return redirect(route('patients.appointments.index')); } else { return redirect(route('admin.appointments.index')); } } if (getLogInUser()->hasRole('patient')) { return view('patient_appointments.show')->with('appointment', $appointment); } else { return view('appointments.show')->with('appointment', $appointment); } } /** * Remove the specified Appointment from storage. * * @param Appointment $appointment * * @return JsonResponse */ public function destroy(Appointment $appointment) { $appointment->delete(); return $this->sendSuccess('Appointment deleted successfully.'); } /** * @param Request $request * * @throws Exception * * @return Application|Factory|View */ public function doctorAppointment(Request $request) { if ($request->ajax()) { return DataTables::of((new DoctorAppointmentDataTable())->get($request->only([ 'status', 'filter_date', 'payment_type', ])))->make(true); } $appointmentStatus = Appointment::ALL_STATUS; return view('doctor_appointment.index', compact('appointmentStatus')); } /** * @param Request $request * * @return Application|Factory|View|JsonResponse */ public function doctorAppointmentCalendar(Request $request) { if ($request->ajax()) { $input = $request->all(); $data = $this->appointmentRepository->getAppointmentsData(); return $this->sendResponse($data, 'Doctor Appointment calendar data retrieved successfully.'); } return view('doctor_appointment.calendar'); } /** * @param Request $request * * * @return Application|Factory|View */ public function patientAppointmentCalendar(Request $request) { if ($request->ajax()) { $input = $request->all(); $data = $this->appointmentRepository->getPatientAppointmentsCalendar(); return $this->sendResponse($data, 'Patient Appointment calendar data retrieved successfully.'); } return view('appointments.patient-calendar'); } /** * @param Request $request * * @return Application|Factory|View */ public function appointmentCalendar(Request $request) { if ($request->ajax()) { $input = $request->all(); $data = $this->appointmentRepository->getCalendar(); return $this->sendResponse($data, 'Appointment calendar data retrieved successfully.'); } return view('appointments.calendar'); } /** * @param Appointment $appointment * * @return Application|Factory|View */ public function appointmentDetail(Appointment $appointment) { $appointment = $this->appointmentRepository->showDoctorAppointment($appointment); return view('doctor_appointment.show', compact('appointment')); } /** * @param Request $request * * @return mixed */ public function changeStatus(Request $request) { $input = $request->all(); $appointment = Appointment::findOrFail($input['appointmentId']); $appointment->update([ 'status' => $input['appointmentStatus'], ]); $fullTime = $appointment->from_time.''.$appointment->from_time_type.' - '.$appointment->to_time.''.$appointment->to_time_type.' '.' '.Carbon::parse($appointment->date)->format('jS M, Y'); $patient = Patient::whereId($appointment->patient_id)->with('user')->first(); $doctor = Doctor::whereId($appointment->doctor_id)->with('user')->first(); if ($input['appointmentStatus'] == Appointment::CHECK_OUT){ Notification::create([ 'title' => Notification::APPOINTMENT_CHECKOUT_PATIENT_MSG.' '.getLogInUser()->full_name, 'type' => Notification::CHECKOUT, 'user_id' => $patient->user_id, ]); Notification::create([ 'title' => $patient->user->full_name.'`s appointment check out by '.getLogInUser()->full_name.' at '.$fullTime, 'type' => Notification::CHECKOUT, 'user_id' => $doctor->user_id, ]); }elseif($input['appointmentStatus'] == Appointment::CANCELLED){ Notification::create([ 'title' => Notification::APPOINTMENT_CANCEL_PATIENT_MSG.' '.getLogInUser()->full_name, 'type' => Notification::CANCELED, 'user_id' => $patient->user_id, ]); Notification::create([ 'title' => $patient->user->full_name.'`s appointment cancelled by'.getLogInUser()->full_name.' at '.$fullTime, 'type' => Notification::CANCELED, 'user_id' => $doctor->user_id, ]); } return $this->sendSuccess('Status updated successfully.'); } /** * @param Request $request * * @return mixed */ public function cancelStatus(Request $request) { $appointment = Appointment::findOrFail($request['appointmentId']); $appointment->update([ 'status' => Appointment::CANCELLED, ]); $fullTime = $appointment->date.' '.$appointment->form_time.''.$appointment->form_time_type.' - '.$appointment->to_time.''.$appointment->to_time_type; $patient = Patient::whereId($appointment->patient_id)->with('user')->first(); $doctor = Doctor::whereId($appointment->doctor_id)->with('user')->first(); Notification::create([ 'title' => $patient->user->full_name.' '.Notification::APPOINTMENT_CANCEL_DOCTOR_MSG.' '.$fullTime, 'type' => Notification::CANCELED, 'user_id' => $doctor->user_id, ]); return $this->sendSuccess('Appointment Cancelled.'); } /** * @param CreateFrontAppointmentRequest $request * * @throws ApiErrorException * * @return JsonResponse */ public function frontAppointmentBook(CreateFrontAppointmentRequest $request) { $input = $request->all(); $appointment = $this->appointmentRepository->frontSideStore($input); if ($input['payment_type'] == Appointment::STRIPE) { $result = $this->appointmentRepository->createSession($appointment); return $this->sendResponse($result, 'Session created successfully.'); } $data = $input['payment_type']; return $this->sendResponse($data, 'Appointment Booked successfully'); } /** * @param Request $request * */ public function frontHomeAppointmentBook(Request $request) { $data = $request->all(); return redirect()->route('medicalAppointment')->with(['data'=>$data]); } /** * @param Request $request * * @throws Exception * * @return HigherOrderBuilderProxy|mixed|string */ public function getPatientName(Request $request) { $checkRecord = User::whereEmail($request->email)->whereType(User::PATIENT)->first(); if ($checkRecord != '') { return $this->sendResponse($checkRecord->full_name, 'Patient name retrieved successfully.'); } return false; } /** * @param Request $request * * @throws ApiErrorException * * @return Application|RedirectResponse|Redirector */ public function paymentSuccess(Request $request) { $sessionId = $request->get('session_id'); if (empty($sessionId)) { throw new UnprocessableEntityHttpException('session id is required'); } setStripeApiKey(); $sessionData = \Stripe\Checkout\Session::retrieve($sessionId); $appointment = Appointment::whereAppointmentUniqueId($sessionData->client_reference_id)->first(); $patientId = User::whereEmail($sessionData->customer_details->email)->pluck('id')->first(); $transaction = [ 'user_id' => $patientId, 'transaction_id' => $sessionData->id, 'appointment_id' => $sessionData->client_reference_id, 'amount' => intval($sessionData->amount_total / 100), 'type' => Appointment::STRIPE, 'meta' => $sessionData, ]; Transaction::create($transaction); $appointment->update(['payment_type' => Appointment::STRIPE]); Flash::success('Appointment created successfully and Payment is completed.'); $patient = Patient::whereUserId($patientId)->with('user')->first(); Notification::create([ 'title' => Notification::APPOINTMENT_PAYMENT_DONE_PATIENT_MSG, 'type' => Notification::PAYMENT_DONE, 'user_id' => $patient->user_id, ]); if (parse_url(url()->previous(), PHP_URL_PATH) == '/medical-appointment') { return redirect(route('medicalAppointment')); } if (! getLogInUser()) { return redirect(route('medical')); } if (getLogInUser()->hasRole('patient')) { return redirect(route('patients.appointments.index')); } return redirect(route('appointments.index')); } /** * @return Application|RedirectResponse|Redirector */ public function handleFailedPayment() { if (! getLogInUser()) { return redirect(route('medical')); } setStripeApiKey(); Flash::error('Your Payment is not completed'); if (getLogInUser()->hasRole('patient')) { return redirect(route('patients.appointments.index')); } return redirect(route('appointments.index')); } /** * @param Request $request * * @throws ApiErrorException * * @return mixed */ public function appointmentPayment(Request $request) { $appointmentId = $request['appointmentId']; $appointment = Appointment::whereId($appointmentId)->first(); $result = $this->appointmentRepository->createSession($appointment); return $this->sendResponse($result, 'Session created successfully.'); } /** * @param Request $request * * @return mixed */ public function changePaymentStatus(Request $request) { $input = $request->all(); $appointment = Appointment::findOrFail($input['appointmentId']); $appointment->update([ 'payment_type' => $input['paymentStatus'], 'payment_method' => $input['paymentMethod'], ]); $fullTime = $appointment->from_type.''.$appointment->from_time_type.' - '.$appointment->to_type.''.$appointment->to_time_type; $patient = Patient::whereId($appointment->patient_id)->with('user')->first(); $doctor = Patient::whereId($appointment->doctor_id)->with('user')->first(); Notification::create([ 'title' => getLogInUser()->full_name.' changed the payment status '.Appointment::PAYMENT_TYPE[Appointment::PENDING].' to '.Appointment::PAYMENT_TYPE[$appointment->payment_type].' for appointment '.$fullTime, 'type' => Notification::PAYMENT_DONE, 'user_id' => $patient->user_id, ]); return $this->sendSuccess('Payment status updated successfully.'); } /** * @param $patient_id * @param $appointment_unique_id * * @return RedirectResponse */ public function cancelAppointment($patient_id, $appointment_unique_id) { $uniqueId = Crypt::decryptString($appointment_unique_id); $appointment = Appointment::whereAppointmentUniqueId($uniqueId)->wherePatientId($patient_id)->first(); $appointment->update(['status'=>Appointment::CANCELLED]); return redirect(route('medical')); } /** * @param Doctor $doctor * */ public function doctorBookAppointment(Doctor $doctor) { $data = []; $data['doctor_id'] = $doctor['id']; return redirect()->route('medicalAppointment')->with(['data'=>$data]); } /** * @param Service $service * */ public function serviceBookAppointment(Service $service) { $data = []; $data['service'] = Service::whereStatus(Service::ACTIVE)->get()->pluck('name','id'); $data['service_id'] = $service['id']; return redirect()->route('medicalAppointment')->with(['data'=>$data]); } }