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/Appointment.php
<?php

namespace App;

use App\AppointmentSymptom;
use App\Enums\AppoitmentStatusEnum;
use App\HealthRecords;
use App\HealthRecordSymptoms;
use App\Presciption;
use App\Reason;
use App\ReferalDetails;
use App\SickLeaveCertificate;
use App\User;
use Config;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Storage;
use App\Casts\AppoitmentEnumCast;

/**
 * App\Appointment
 *
 * @property object $doctorDetails
 * @property object $patientDetails
 * @property object $waitingRooms
 * @property mixed $appointment_status
 */
class Appointment extends Model
{
    use SoftDeletes;

    protected $appends = ['consultation_type_str', 'appointment_request_detail', 'consultation_cost', 'price_detail', 'is_avail_insurance_claim_details', 'insurance_claim_status', 'referal_pdf'];

    protected $with = ['appointmentReason'];

    protected $enums = [
        'appointment_status' => AppoitmentStatusEnum::class.':nullable',
    ];

    // protected $casts = [
    //     'appointment_status' => AppoitmentEnumCast::class
    // ];

    public function getAppointmentRequestDetailAttribute()
    {
        if ($this->consultation_type == 2) {
            //Home Visit
            if ($this->home_visit_range != null) {
                /** @var object $decode */
                $decode = json_decode($this->home_visit_range);
                if (count($decode) != 0) {
                    $out = [];
                    for ($i = 0; $i < count($decode); $i++) {
                        $jData = Carbon::parse($decode[$i]->from)->format('H:i').' - '.Carbon::parse($decode[$i]->to)->format('H:i');
                        array_push($out, $jData);
                    }
                    $slots = $out;
                } else {
                    $slots = null;
                }
            } else {
                $slots = null;
            }

            $json['date'] = Carbon::parse($this->appointment_start_dt)->format('Y-m-d');
            $json['slots'] = $slots;
            /** @var array $channelList */
            $channelList = Config::get('channel_list');
            if (count($channelList['channels']) != 0) {
                $arr = $channelList['channels'];
                if ($this->channel != null) {
                    $keys = array_keys(array_column($arr, 'value'), $this->channel);
                    $channal_name = $arr[$keys[0]]['name'];
                } else {
                    $channal_name = null;
                }
            } else {
                $channal_name = null;
            }

            $json['channal_location'] = ['channel_id' => $this->channel, 'channel_name' => $channal_name, 'location' => $this->lat_long_address];

            return $json;
        } elseif ($this->consultation_type == 1 || $this->consultation_type == 3) {
            $json['date'] = Carbon::parse($this->appointment_start_dt)->format('Y-m-d');
            $json['slots'] = [Carbon::parse($this->appointment_start_dt)->format('H:i').' - '.Carbon::parse($this->appointment_end_dt)->format('H:i')];
            /** @var array $channelList */
            $channelList = Config::get('channel_list');
            if (count($channelList['channels']) != 0) {
                $arr = $channelList['channels'];
                if ($this->channel != null) {
                    $keys = array_keys(array_column($arr, 'value'), $this->channel);
                    $channal_name = $arr[$keys[0]]['name'];
                } else {
                    $channal_name = null;
                }
            } else {
                $channal_name = null;
            }
            $json['channal_location'] = ['channel_id' => $this->channel, 'channel_name' => $channal_name, 'location' => null];

            return $json;
        } else {
            return null;
        }
    }

    public function getConsultationTypeStrAttribute()
    {
        if ($this->consultation_type != null) {
            /** @var array $consultationTypes */
            $consultationTypes = Config::get('consultation_type');
            if (count($consultationTypes) != 0) {
                $keys = array_keys(array_column($consultationTypes, 'id'), $this->consultation_type);

                return $consultationTypes[$keys[0]]['name'];
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public function getForLog()
    {
        /** @var array $consultationTypeArr */
        $consultationTypeArr = Config::get('patients_consultation_types');
        $key = array_search($this->consultation_type, array_column($consultationTypeArr, 'id'));

        return [
            'appointment_id' => $this->id,
            'consultation_id' => $this->consultation_id,
            'patient_name' => $this->patientDetails->first_name.' '.$this->patientDetails->last_name,
            'reason' => (isset($this->appointmentReason)) ? $this->appointmentReason->reasons_text : '',
            'time_at' => $this->created_at,
            'consultation_type' => (isset($this->consultation_type)) ? $consultationTypeArr[$key]['name'] : '',
            'doctor_email' => $this->doctorDetails->email,
        ];
    }

    public function doctorDetails() : HasOne
    {
        return $this->hasOne(User::class, 'id', 'doctor_id')->withTrashed();
    }

    public function patientDetails() : HasOne
    {
        return $this->hasOne(User::class, 'id', 'patient_id')->withTrashed();
    }

    public function clinicDetails() : HasOne
    {
        return $this->hasOne(Clinic::class, 'id', 'clinic_appointment_id');
    }

    public function diagnosisDetails() : HasOne
    {
        return $this->hasOne(Diagnosis::class, 'appointment_id', 'id');
    }

    public function prescriptions() : HasMany
    {
        return $this->hasMany(Prescription::class, 'appointment_id', 'id');
    }

    public function paymentDetails() : HasOne
    {
        return $this->hasOne(Payment::class, 'appointment_id', 'id');
    }

    public function insuranceDetails() : HasOne
    {
        return $this->hasOne(InsuranceDetail::class, 'user_id', 'patient_id');
    }

    public function medicalHistory() : HasOne
    {
        return $this->hasOne(UserMedicalhistory::class, 'user_id', 'patient_id');
    }

    public function insuranceClaimDetails() : HasOne
    {
        return $this->hasOne(InsuranceClaimDetail::class, 'appointment_id', 'id');
    }

    public function getIsAvailInsuranceClaimDetailsAttribute()
    {
        $data = $this->insuranceClaimDetails()->where('appointment_id', $this->id)->count();

        return $data;
    }

    public function getInsuranceClaimStatusAttribute()
    {
        return isset($this->insuranceClaimDetails) ? $this->insuranceClaimDetails->claim_status : '';
    }

    public function appointmentSymptoms() : HasMany
    {
        return $this->hasMany(AppointmentSymptom::class, 'appointment_id', 'id');
    }

    public function appointmentReason() : HasOne
    {
        return $this->hasOne(AppointmentReason::class, 'appointment_id', 'id');
    }

    public function getCountryIdAttribute($value)
    {
        $arr = explode(',', $value);
        $out = [];
        for ($i = 0; $i < count($arr); $i++) {
            $json = (int) $arr[$i];
            array_push($out, $json);
        }

        return Language::select('id', 'name')->whereIn('id', $out)->get()->toArray();
    }

    public function getConsultationCostAttribute()
    {
        $consultation_type = $this->consultation_type;
        $userDetail = UserDetails::where('user_id', $this->doctor_id)->first();
        $currency = null;
        $amount = null;
        if ($consultation_type == 1) {
            $currency = $userDetail->schedule_appointment_currency_code;
            $amount = $userDetail->schedule_appointment_price;
        } elseif ($consultation_type == 2) {
            $currency = $userDetail->home_visit_currency_code;
            $amount = $userDetail->home_visit_price;
        } elseif ($consultation_type == 3) {
            $currency = $userDetail->on_demand_currency_code;
            $amount = $userDetail->on_demand_price;
        }

        return [
            'currency' =>$currency,
            'amount' =>$amount,
        ];
    }

    public function getPriceDetailAttribute()
    {
        $query = ConsultationPrice::select('currency_code', 'price')->where('user_id', $this->doctor_id)->where('type', $this->consultation_type)->first();

        return (! empty($query)) ? $query : [];
    }

    public function appointmentHealthRecord() : HasOne
    {
        return $this->hasOne(HealthRecords::class, 'appointment_id', 'id');
    }

    // public function appointmentHealthRecordSymptoms() : HasMany
    // {
    //     return $this->hasMany(HealthRecordSymptoms::class, 'appointment_id', 'id');
    // }

    public function getReferalPdfAttribute()
    {
        $fileName = 'referal_'.$this->id.'.pdf';
        $S3Library = new \App\Library\S3Library;
        $digimedFile = ($S3Library->s3Url($fileName, null, 'digimed/images/referal'));

        return $digimedFile;
    }

    public function waitingRooms() : HasOne
    {
        return $this->hasOne(WaitingRoom::class, 'appointment_id', 'id');
    }

    public function orderData()
    {
        return $this->hasMany(LabOrderData::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'patient_id')->withTrashed();
    }

    public function lab()
    {
        return $this->belongsTo(Lab::class, 'lab_id');
    }

    public function category()
    {
        return $this->hasOne(LabCategory::class, 'id', 'category_id');
    }

    public function order()
    {
        return $this->hasOne(LabOrder::class);
    }

}