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

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
use App\Appointment;
use App\Prescription;
use App\Diagnosis;
use App\PrescriptionDetails;
use App\PrescriptionRoutes;
use App\ReferalDetails;
use App\SickLeaveCertificate;
use App\HealthRecords;
use App\WebSetting;
use App\User;
use Notification;
use Storage;
use Config;
use PDF;
use App\DependantAccountHolder;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use App\Jobs\ConsultationCompletedJob;
use App\Jobs\ConsultationReferalJob;
use App\Jobs\ConsultationPrescriptionJob;
use Illuminate\Support\Facades\Mail;
use App\Mail\SendEmailNotification;
use App\Jobs\SendEmailNotificationJob;
use App\Enums\AppoitmentStatusEnum;
use App\GeneralHealthConditionPatient;
use App\Notifications\PrescriptionDoctorCompletedNotify;
use App\Notifications\DoctorConsultationCompletedNotify;

class AppointmentConsultationPdfCreate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $appointment_id;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($appointment_id)
    {
        $this->appointment_id = $appointment_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $appointment_id = $this->appointment_id;
        $appointment = Appointment::with(['patientDetails','doctorDetails'])->find($appointment_id);
        $diagnosis = Diagnosis::where("appointment_id", $appointment_id)->first();
        $pdf_password = substr(md5((string)rand()),0,8);
        $web_settings = WebSetting::find(1);
        $contact_email = $web_settings->contact_email;
        $website = $web_settings->website;

        if(Config::get('filesystems.default') == "s3") {
            $logo_image = Storage::temporaryUrl('digimed/images/assets/img/logo.svg', now()->addMinutes(30));
            $doctor_signature = Storage::temporaryUrl('digimed/images/user_signature/'.$appointment->doctor_id.'/'.$appointment->doctorDetails->signature, now()->addMinutes(30));
            $patient_image = $appointment->patientDetails->profile_image;
            $doctor_image = $appointment->doctorDetails->profile_image;
            $morning = Storage::temporaryUrl('digimed/images/assets/img/sun-icon.svg', now()->addMinutes(30));
            $afternoon = Storage::temporaryUrl('digimed/images/assets/img/sun.svg', now()->addMinutes(30));
            $evening = Storage::temporaryUrl('digimed/images/assets/img/moon-icon.svg', now()->addMinutes(30));
        } else {
            $logo_image = Storage::disk('public')->url('images/logo.png');
            $doctor_signature = Storage::url('images/user_signature/'.$appointment->doctor_id.'/'.$appointment->doctorDetails->signature);
            $patient_image = Storage::url('images/profile-images/'.$appointment->patientDetails->profile_image);
            $doctor_image = Storage::url('images/profile-images/'.$appointment->doctorDetails->profile_image);
            $morning = Storage::url('img/sun-icon.svg');
            $afternoon = Storage::url('img/sun.svg');
            $evening = Storage::url('img/moon-icon.svg');
        }
        $patient_id = $appointment->patient_id;
        $IfDependantQuery = User::with('roles')->find($patient_id);
        $roleGet = $IfDependantQuery->roles->pluck('name');
        if($roleGet[0] == "dependant") {
            $DependantAccountHolder = DependantAccountHolder::with('user')->where('dependant_id', $IfDependantQuery->id)->first();
            $emailRecipient = $DependantAccountHolder['user']['email'];
        } else {
            $emailRecipient = $appointment->patientDetails->email;
        }
        $health_records = HealthRecords::with(['templateSymptoms'])->where('appointment_id', $appointment_id)->first();
        if(!empty($health_records)){
            $health_record_pdf = PDF::loadView('pdf.health_record_pdf', compact('logo_image','appointment','diagnosis','health_records','doctor_signature','patient_image','doctor_image','contact_email','website','emailRecipient'));
            $health_record_pdf->SetProtection(array('print'), '', $pdf_password);

            if(Config::get('filesystems.default') == "s3") {
                Storage::disk('s3')->put('digimed/images/health_records/health_record_'.$appointment_id.'.pdf', $health_record_pdf->output());
            }
            else{
                Storage::disk('public')->put('digimed/images/health_records/health_record_'.$appointment_id.'.pdf', $health_record_pdf->output());
            }
            HealthRecords::where('appointment_id', $appointment_id)->update(['pdf' => 'health_record_'.$appointment_id.'.pdf']);
        }

        $prescriptions = Prescription::with(['prescriptionRouteList'])->where('appointment_id',$appointment_id)->get()->toArray();
        if(!empty($prescriptions)){
            foreach($prescriptions as $key => $value){
                /** @var array $value */
                $obj = ["PrescriptionCode"=>$value['prescription_code'],"AppointmentID"=>$value['appointment_id'],'prescription_id'=>$value['id']];
                $qrcode = QrCode::format('png')->size(110)->generate(json_encode($obj));

                if(Config::get('filesystems.default') == "s3") {
                    Storage::disk('s3')->put('digimed/images/prescription/'.$appointment_id.'/'.$value['prescription_code'].'.png', $qrcode);
                }
                else{
                    Storage::disk('public')->put('digimed/images/prescription/'.$appointment_id.'/'.$value['prescription_code'].'.png', $qrcode);
                }
                Prescription::where('id', $value['id'])->update(['qrcode' => $value['prescription_code'].'.png']);
            }
            $prescriptions = Prescription::with(['prescriptionDetails'])->where('appointment_id',$appointment_id)->get()->toArray();

            $prescription_pdf = PDF::loadView('pdf.prescription_pdf', compact('logo_image','appointment','patient_image','doctor_image','doctor_signature','prescriptions','morning','afternoon','evening','contact_email','website','emailRecipient'));
            $prescription_pdf->SetProtection(array('print'), '', $pdf_password);

            if(Config::get('filesystems.default') == "s3") {
                Storage::disk('s3')->put('digimed/images/prescription/prescription_'.$appointment_id.'.pdf', $prescription_pdf->output());
            }
            else{
                Storage::disk('public')->put('digimed/images/prescription/prescription_'.$appointment_id.'.pdf', $prescription_pdf->output());
            }
            Prescription::where('appointment_id', $appointment_id)->update(['pdf' => 'prescription_'.$appointment_id.'.pdf']);
        }
        $medical_notes = SickLeaveCertificate::where("appointment_id", $appointment_id)->first();
        if(!empty($medical_notes)){
            $medical_note = $medical_notes->medical_note;
            $medical_note_pdf = PDF::loadView('pdf.medical_note_pdf', compact('logo_image','appointment','doctor_signature','patient_image','doctor_image','medical_note','contact_email','website','emailRecipient'));
            $medical_note_pdf->SetProtection(array('print'), '', $pdf_password);

            if(Config::get('filesystems.default') == "s3") {
                Storage::disk('s3')->put('digimed/images/medical_notes/medical_note_'.$appointment_id.'.pdf', $medical_note_pdf->output());
            }
            else{
                Storage::disk('public')->put('digimed/images/medical_notes/medical_note_'.$appointment_id.'.pdf', $medical_note_pdf->output());
            }
            SickLeaveCertificate::where('appointment_id', $appointment_id)->update(['pdf' => 'medical_note_'.$appointment_id.'.pdf']);
        }

        //referal pdf
        $referal = ReferalDetails::with(['referalExaminations','referalSpecificExaminations','referalRequestFor'])->where("appointment_id", $appointment_id)->first();
        if(!empty($referal)){
            $referal_pdf = PDF::loadView('pdf.referal_pdf', compact('logo_image','appointment','doctor_signature','patient_image','doctor_image','referal','diagnosis','contact_email','website','emailRecipient'));
            $referal_pdf->SetProtection(array('print'), '', $pdf_password);
            if(Config::get('filesystems.default') == "s3") {
                Storage::disk('s3')->put('digimed/images/referal/referal_'.$appointment_id.'.pdf', $referal_pdf->output());
            }
            else{
                Storage::disk('public')->put('digimed/images/referal/referal_'.$appointment_id.'.pdf', $referal_pdf->output());
            }
            ReferalDetails::where('appointment_id', $appointment_id)->update(['pdf' => 'referal_'.$appointment_id.'.pdf']);
        }
        // print_r($referal->toArray());exit;
        $mailBody = [
            'email' => $emailRecipient,
            'body' => [
                'mail_title' => 'Consultation Completed',
                'consultation_id' => $appointment->consultation_id,
                'patient_name' => $appointment->patientDetails->first_name." ".$appointment->patientDetails->last_name,
                'report_link' => Config::get('custom.login_link')."/patient/after-consultation/".$appointment_id,
            ],
            'subject' => 'Consultation Completed'
        ];
        dispatch(new ConsultationCompletedJob($mailBody));
        // Mail::to($emailRecipient)->send(new ConsultationCompleted($mailBody));

        $referal = ReferalDetails::where("appointment_id", $appointment_id)->first();
        if($referal->is_referal == 1)
        {
            $file_name = $referal->getRawOriginal('pdf');
            $mailBody = [
                'email' => $emailRecipient,
                'body' => [
                    'mail_title' => 'Consultation Referral',
                    'consultation_id' => $appointment->consultation_id,
                    'patient_name' => $appointment->patientDetails->first_name." ".$appointment->patientDetails->last_name,
                ],
                'subject' => 'Consultation Referral',
                // 'pdf_link' => $referal->pdf,
                'pdf_link' => 'digimed/images/referal/referal_'.$appointment_id.'.pdf'
            ];
            dispatch(new ConsultationReferalJob($mailBody));
            // Mail::to($emailRecipient)->send(new ConsultationReferal($mailBody));
        }
        $prescription = Prescription::where("appointment_id",$appointment_id)->get();
        if(count($prescription) != 0){
            $attachments = [];
            foreach($prescription as $key => $value){
                $attachments[] = 'digimed/images/prescription/prescription_'.$appointment_id.'.pdf';
            }
            $mailBody = [
                'email' => $emailRecipient,
                'body' => [
                    'mail_title' => 'Consultation Prescription',
                    'consultation_id' => $appointment->consultation_id,
                    'patient_name' => $appointment->patientDetails->first_name." ".$appointment->patientDetails->last_name,
                ],
                'subject' => 'Consultation Prescription details',
                'pdf_link' => $attachments
            ];
            dispatch(new ConsultationPrescriptionJob($mailBody));
            $mailBody = [
                'email' => $emailRecipient,
                'body' => [
                    'content' => 'Appointment ('.($appointment->consultation_id).') Prescription was completed by the doctor.'
                ],
                'subject' => 'Appointment Prescription Completed by the doctor',
            ];
            // Mail::to($email)->send(new SendEmailNotification($mailBody));
            dispatch(new SendEmailNotificationJob($mailBody));
        }

        $doctor_id = $appointment->doctor_id;
        $patient_id = $appointment->patient_id;
        //general_health_condition_patients

        // $current_medications_str = GeneralHealthConditionPatient::where('doctor_id', $doctor_id)
        // ->where('patient_id', $patient_id)
        // ->where('type', 'Allergy')
        // ->value('description');

        $patient_notify = [];
        $doctor_notify = [];
        if(Config::get('pusher.fcm_enabled'))
        {
            $fcmArr = [];
            $notifications = \App\User::with(['notificationSettings'])->find($patient_id);
            if(!empty($notifications->notificationSettings)){
                $enable_push_notification = $notifications->notificationSettings->reminders_push;
                if($enable_push_notification == 1)
                {
                    if(count($prescription) != 0){
                        $fcmJson = ['user_array' => [$patient_id], 'title' => 'Prescription Completed by the Doctor', 'message' => 'Your appointment '.$appointment->consultation_id.' Prescription Completed'];
                        array_push($fcmArr, $fcmJson);
                        $patient_notify['type'] = __('digimed_validation.notification.prescription_completed_by_doctor.type');
                        $patient_notify['message'] = __('digimed_validation.notification.prescription_completed_by_doctor.message', ['consultation_id' => $appointment->consultation_id]);
                        $patient_notify['patient_id'] = User::find($patient_id)->basicProfile();
                        $patient_notify['doctor_id'] = User::find($doctor_id)->basicProfile();
                        $patient_notify['data'] = $appointment->toArray();
                        $patient_notify['mail'] = [];
                        $patient_notify['fcm'] = $fcmArr;
                        // $patient_notify['pusher'] = [$patient_id];
                        Notification::send(User::find($appointment->patient_id), new PrescriptionDoctorCompletedNotify($patient_notify));
                    }
                }
            }
            $fcmArr = [];
            $notifications = \App\User::with(['notificationSettings'])->find($doctor_id);
            if(!empty($notifications->notificationSettings)){
                $enable_push_notification = $notifications->notificationSettings->reminders_push;
                if($enable_push_notification == 1)
                {
                    $fcmJson = ['user_array' => [$doctor_id], 'title' => 'Appointment Consultation Completed', 'message' => 'Your appointment '.$appointment->consultation_id.' consultation was completed.'];
                    array_push($fcmArr, $fcmJson);
                    $doctor_notify['type'] = __('digimed_validation.notification.health_record_complete.type');
                    $doctor_notify['message'] = __('digimed_validation.notification.health_record_complete.message', ['consultation_id' => $appointment->consultation_id]);
                    $doctor_notify['patient_id'] = User::find($patient_id)->basicProfile();
                    $doctor_notify['doctor_id'] = User::find($doctor_id)->basicProfile();
                    $doctor_notify['data'] = $appointment->toArray();
                    $doctor_notify['mail'] = [];
                    $doctor_notify['fcm'] = $fcmArr;
                    // $doctor_notify['pusher'] = [];
                    Notification::send(User::find($doctor_id), new DoctorConsultationCompletedNotify($doctor_notify));
                }
            }

        }
    }
}