HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1014-azure #14~24.04.1-Ubuntu SMP Fri Oct 3 20:52:11 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/ghayatcom/ghayatcom-api/app/Library/AppointmentLibrary.php
<?php

namespace App\Library;

use App\Appointment;
use App\DependantAccountHolder;
use App\DoctorUnavailable;
use App\Enums\AppoitmentStatusEnum;
use App\Http\Resources\AppointmentCollection;
use App\User;
use Illuminate\Support\Carbon;

class AppointmentLibrary
{
    public function list($action, $auth_id, $type='')
    {
        $date = '';
        $list = new Appointment;
        $current = Carbon::now()->format('Y-m-d H:i:s');
        $current_date = Carbon::now()->format('Y-m-d');
        if ($action == 'doctor') {
            $list = $list->where('doctor_id', $auth_id)->whereIn('appointment_status', [AppoitmentStatusEnum::ACCEPTED(), AppoitmentStatusEnum::COMPLETED(), AppoitmentStatusEnum::CANCELLED(), AppoitmentStatusEnum::EXPIRED(), AppoitmentStatusEnum::REJECTED()]);
        } elseif ($action == 'patient') {
            $dependantQ = DependantAccountHolder::where('user_id', $auth_id)->where('status', 1)->get();
            $dependantArr = [$auth_id];
            for ($i = 0; $i < count($dependantQ); $i++) {
                array_push($dependantArr, $dependantQ[$i]->dependant_id);
            }
            $list = $list->whereIn('patient_id', $dependantArr)->whereIn('appointment_status', [AppoitmentStatusEnum::NEW(), AppoitmentStatusEnum::ACCEPTED(), AppoitmentStatusEnum::COMPLETED(), AppoitmentStatusEnum::CANCELLED(), AppoitmentStatusEnum::EXPIRED(), AppoitmentStatusEnum::REJECTED()]);
        } elseif ($action == 'request') {
            $list = $list->where('doctor_id', $auth_id)->whereIn('appointment_status', [AppoitmentStatusEnum::NEW()]);
        }

        if ($action == 'doctor' || $action == 'patient' || $action == 'request') {
            $dataList = $list->get();

            $appId = [];
            foreach ($dataList as $dlist) {
                if ($type == 'upcoming') {
                    if ($dlist->consultation_type == 2) {
                        if (strpos($dlist->appointment_end_dt, '00:00:00') !== false) {
                            if (Carbon::parse($dlist->appointment_end_dt)->format('Y-m-d') >= $current_date) {
                                $appId[] = $dlist->id;
                            }
                        } else {
                            if (Carbon::parse($dlist->appointment_end_dt)->format('Y-m-d H:i:s') >= $current) {
                                $appId[] = $dlist->id;
                            }
                        }
                    } else {
                        if (Carbon::parse($dlist->appointment_end_dt)->format('Y-m-d H:i:s') >= $current) {
                            $appId[] = $dlist->id;
                        }
                    }
                } elseif ($type == 'past') {
                    if ($dlist->consultation_type == 2) {
                        //$dlist->where('consultation_type', 2)
                        if (strpos($dlist->appointment_end_dt, '00:00:00') !== false) {
                            if (Carbon::parse($dlist->appointment_end_dt)->format('Y-m-d') < $current_date) {
                                $appId[] = $dlist->id;
                            }
                        } else {
                            if (Carbon::parse($dlist->appointment_end_dt)->format('Y-m-d H:i:s') < $current) {
                                $appId[] = $dlist->id;
                            }
                        }
                    } else {
                        if (Carbon::parse($dlist->appointment_end_dt)->format('Y-m-d H:i:s') < $current) {
                            $appId[] = $dlist->id;
                        }
                    }
                } else {
                    $appId[] = $dlist->id;
                }
            }
            $list = Appointment::whereIn('id', $appId);
            /*if(!empty($date)) {
                $date = Carbon::parse($date)->format('Y-m-d');
                $list = $list->whereDate('appointment_end_dt',$date);
            } else {
                $list = Appointment::whereIn('id',$appId);
                // $list = $list->orderBy(array_search($sort_by, $sort),$orderBy);
            }*/

            return $list;
        }
    }

    public function detail($appointment_id)
    {
        $query = Appointment::find($appointment_id);

        return $query;
    }

    const REQUEST_ENUM = [
        1 => 'NEW',
        2 => 'ACCEPTED',
        3 => 'COMPLETED',
        4 => 'CANCELLED',
        5 => 'EXPIRED',
        6 => 'REJECTED',
    ];

    /**
     * @param  int|string $req
     * @return string
     */
    public function requestEnum($req): string
    {
        return self::REQUEST_ENUM[$req];
    }

    public function doctorUnavailability($consultation_type, $home_visit_dt, $appointment_start_dt, $doctor_id)
    {
        if ($consultation_type == 2) {
            $checkUnavailableDate = '';
            /** @var string|array $data_home_visit_dt */
            $data_home_visit_dt = $home_visit_dt;
            $hv = json_decode(str_replace('\\', '', $data_home_visit_dt), true);
            /**
             * @var array $hv
             */
            if (count($hv) != 0) {
                /**
                 * @var object $hv[0]['date']
                 */
                $checkUnavailableDate = date('Y-m-d', strtotime($hv[0]['date']));
            }
        } else {
            /**
             * @var string $appointment_start_dt
             */
            $checkUnavailableDate = date('Y-m-d', strtotime($appointment_start_dt));
        }

        $count = DoctorUnavailable::where('unavailables_at', $checkUnavailableDate)->where('user_id', $doctor_id)->count();

        return $count;
    }

    public function appointmentPatientConflict($consultType, $appointmentStartDt, $homeVisitDt, $patientIdCheck)
    {
        if ($consultType == 1 || $consultType == 3) {
            $datePreCheck = $appointmentStartDt;
            $checkExistsAppointment = Appointment::whereIn('consultation_type', [1, 3])
            ->where('appointment_start_dt', '>', $datePreCheck)
            ->where('appointment_end_dt', '<', $datePreCheck)
            ->where('patient_id', $patientIdCheck)
            ->whereIn('appointment_status', [AppoitmentStatusEnum::NEW(), AppoitmentStatusEnum::ACCEPTED()])
            ->count();
            if ($checkExistsAppointment > 0) {
                return true;
            } else {
                return false;
            }
        } elseif ($consultType == 2) {
            $hv = json_decode(str_replace('\\', '', $homeVisitDt), true);
            /** @var array $hv */
            $rangeInner = $hv[1]['range'];
            $homeVisitSamePatient = Appointment::where('consultation_type', 2)->where('patient_id', $patientIdCheck)->whereIn('appointment_status', [AppoitmentStatusEnum::NEW(), AppoitmentStatusEnum::ACCEPTED()])->get();

            $conflictArr = [];
            for ($case = 0; $case < count($homeVisitSamePatient); $case++) {
                if ($homeVisitSamePatient[$case]->appointment_status == AppoitmentStatusEnum::NEW()) {
                    $dt_dv_date = $hv[0]['date'];
                    /**
                     * @var string|null $dt_dv_date
                     */
                    if (date('Y-m-d', strtotime($homeVisitSamePatient[$case]->appointment_start_dt)) == date('Y-m-d', strtotime($dt_dv_date))) {
                        $decodeCaseTiming = json_decode($homeVisitSamePatient[$case]->home_visit_range);

                        $conflictCase = [];
                        /**
                         * @var array $decodeCaseTiming
                         */
                        for ($caseSecond = 0; $caseSecond < count($decodeCaseTiming); $caseSecond++) {
                            /** @var object $decodeCaseTiming */
                            $caseFrom = date('H:i:s', strtotime($decodeCaseTiming[$caseSecond]->from));
                            $caseTo = date('H:i:s', strtotime($decodeCaseTiming[$caseSecond]->to));

                            $conflictRange = [];
                            for ($rangeCase = 0; $rangeCase < count($rangeInner); $rangeCase++) {
                                $fromCatch = $toCatch = '';
                                $dt_range_from = $rangeInner[$rangeCase]['from'];
                                $dt_range_to = $rangeInner[$rangeCase]['to'];
                                /**
                                 * @var string|null $dt_range_from
                                 * @var string|null $dt_range_to
                                 */
                                $reqFrom = date('H:i:s', strtotime($dt_range_from));
                                $reqTo = date('H:i:s', strtotime($dt_range_to));
                                // echo $reqFrom.' > '.$caseFrom.' && '.$reqFrom.' < '.$caseTo;exit;
                                if (strtotime($reqFrom) >= strtotime($caseFrom) && strtotime($reqFrom) < strtotime($caseTo)) {
                                    $fromCatch = 1;
                                }
                                if (strtotime($reqTo) > strtotime($caseFrom) && strtotime($reqTo) < strtotime($caseTo)) {
                                    $toCatch = 2;
                                }
                                if ($fromCatch != '' || $toCatch != '') {
                                    $conflictRange[] = 1;
                                }
                            }
                            if (count($conflictRange) > 0) {
                                $conflictCase[] = 1;
                            }
                        }

                        if (count($conflictCase) > 0) {
                            $conflictArr[] = 1;
                        }
                    }
                } elseif ($homeVisitSamePatient[$case]->appointment_status == AppoitmentStatusEnum::ACCEPTED()) {
                    $dt_hv_date = $hv[0]['date'];
                    /**
                     *  @var string|null $dt_hv_date
                     */
                    if (date('Y-m-d', strtotime($homeVisitSamePatient[$case]->appointment_start_dt)) == date('Y-m-d', strtotime($dt_hv_date))) {
                        $caseFrom = date('H:i:s', strtotime($homeVisitSamePatient[$case]->appointment_start_dt));
                        $caseTo = date('H:i:s', strtotime($homeVisitSamePatient[$case]->appointment_end_dt));

                        $conflictRange = [];
                        for ($rangeCase = 0; $rangeCase < count($rangeInner); $rangeCase++) {
                            $fromCatch = $toCatch = '';
                            $dt_range_from = $rangeInner[$rangeCase]['from'];
                            $dt_range_to = $rangeInner[$rangeCase]['to'];
                            /**
                             * @var string|null $dt_range_from
                             * @var string|null $dt_range_to
                             */
                            $reqFrom = date('H:i:s', strtotime($dt_range_from));
                            $reqTo = date('H:i:s', strtotime($dt_range_to));

                            if (strtotime($reqFrom) >= strtotime($caseFrom) && strtotime($reqFrom) < strtotime($caseTo)) {
                                $fromCatch = 1;
                            }
                            if (strtotime($reqTo) > strtotime($caseFrom) && strtotime($reqTo) < strtotime($caseTo)) {
                                $toCatch = 2;
                            }
                            if ($fromCatch != '' || $toCatch != '') {
                                $conflictRange[] = 1;
                            }
                        }

                        if (count($conflictRange) > 0) {
                            $conflictArr[] = 1;
                        }
                    }
                }
            }
            if (count($conflictArr) > 0) {
                return true;
            } else {
                return false;
            }
        }
    }

    public function checkDoctorAvailability($consultType, $appointmentStartDt, $doctorId, $variableLibrary, $time)
    {
        if ($consultType == 1 || $consultType == 3) {
            $request_appointment_start_dt = Carbon::make($appointmentStartDt);
            $var_hours = $variableLibrary->int($time[0]);
            $var_mints = $variableLibrary->int($time[1]);
            $var_secs = $variableLibrary->int($time[2]);
            $end_time_check = Carbon::parse($request_appointment_start_dt)->addHours($var_hours)->addMinutes($var_mints)->addSeconds($var_secs);

            $checkDoctorAvailableC1 = Appointment::where('appointment_start_dt', '>', $appointmentStartDt)->where('appointment_end_dt', '<', $appointmentStartDt)
            ->where('doctor_id', $doctorId)->whereIn('appointment_status', [AppoitmentStatusEnum::NEW(), AppoitmentStatusEnum::ACCEPTED()])->count();
            if ($checkDoctorAvailableC1 > 0) {
                return true;
            }

            $checkDoctorAvailableC2 = Appointment::where('appointment_start_dt', '>', $end_time_check)
            ->where('appointment_end_dt', '<', $end_time_check)
            ->where('doctor_id', $doctorId)->whereIn('appointment_status', [AppoitmentStatusEnum::NEW(), AppoitmentStatusEnum::ACCEPTED()])->count();
            if ($checkDoctorAvailableC2 > 0) {
                return true;
            }

            return false;
        }
    }
}