mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
763 lines
44 KiB
PHP
Executable File
763 lines
44 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Reports\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Carbon\Carbon;
|
|
use DateTime;
|
|
use Barryvdh\Snappy\Facades\SnappyPdf;
|
|
use Streamline\Models\HospitalInformation;
|
|
use Streamline\Models\HmisCategory;
|
|
use Streamline\Models\HmisCategoryOptions;
|
|
use Streamline\Models\Diagnosis;
|
|
use Streamline\Models\Consultation;
|
|
use Streamline\Models\Investigation;
|
|
use Streamline\Models\Treatment;
|
|
use Streamline\Models\OrderedInvestigation;
|
|
|
|
class HmisController105 extends Controller {
|
|
|
|
/**
|
|
* OPD Attendances, Referrals and Diagnosis Totals HMIS
|
|
*/
|
|
public function hmis_105(Request $request) {
|
|
|
|
$hospital_information = HospitalInformation::find(1);
|
|
|
|
if(isset($request->date_from)) {
|
|
$attendance_from = Carbon::createFromFormat('d-m-Y', $request->date_from)->format('Y-m-d');
|
|
// Add one day to make the report accurate
|
|
$attendance_to = Carbon::createFromFormat('d-m-Y', $request->date_to)->addDays(1)->format('Y-m-d');
|
|
|
|
$dateRange = $request->date_from . '/' . $request->date_to;
|
|
} else {
|
|
$attendance_from = Carbon::today()->subDays(6)->format('Y-m-d');
|
|
$attendance_to = Carbon::today()->addDays(1)->format('Y-m-d');
|
|
|
|
$dateRange = Carbon::today()->subDays(6)->format('d-m-Y') . '/' . Carbon::today()->addDays(1)->format('d-m-Y');
|
|
}
|
|
|
|
$patient_ids_male_0_28_days_new_attendance = [];
|
|
$patient_ids_male_0_28_days_re_attendance = [];
|
|
$patient_ids_female_0_28_days_new_attendance = [];
|
|
$patient_ids_female_0_28_days_re_attendance = [];
|
|
|
|
$patient_ids_male_29days_4yrs_new_attendance = [];
|
|
$patient_ids_male_29days_4yrs_re_attendance = [];
|
|
$patient_ids_female_29days_4yrs_new_attendance = [];
|
|
$patient_ids_female_29days_4yrs_re_attendance = [];
|
|
|
|
$patient_ids_male_5_9yrs_new_attendance = [];
|
|
$patient_ids_male_5_9yrs_re_attendance = [];
|
|
$patient_ids_female_5_9yrs_new_attendance = [];
|
|
$patient_ids_female_5_9yrs_re_attendance = [];
|
|
|
|
$patient_ids_male_10_19yrs_new_attendance = [];
|
|
$patient_ids_male_10_19yrs_re_attendance = [];
|
|
$patient_ids_female_10_19yrs_new_attendance = [];
|
|
$patient_ids_female_10_19yrs_re_attendance = [];
|
|
|
|
$patient_ids_male_20yrs_new_attendance = [];
|
|
$patient_ids_male_20yrs_re_attendance = [];
|
|
$patient_ids_female_20yrs_new_attendance = [];
|
|
$patient_ids_female_20yrs_re_attendance = [];
|
|
|
|
|
|
/*$triageRecords = DB::table('triage')->whereIn('new_attendance', [0, 1])->whereIn('re_attendance', [0, 1])
|
|
->whereBetween('created_at', [$attendance_from, $attendance_to])
|
|
->select('patient_id', 'new_attendance', 're_attendance')->get();*/
|
|
$triageRecords = DB::table('triage')->whereBetween('created_at', [$attendance_from, $attendance_to])
|
|
->where(function($q) {
|
|
$q->whereIn('new_attendance', [0, 1])
|
|
->orWhereIn('re_attendance', [0, 1]);
|
|
})->select('patient_id', 'new_attendance', 're_attendance')->get();
|
|
|
|
|
|
foreach ($triageRecords as $triage):
|
|
// Get patient and their age
|
|
$patient = DB::table('patients')->where('id', $triage->patient_id)->select('id', 'date_of_birth', 'gender', 'created_at')->first();
|
|
|
|
if ($patient && in_array($patient->gender, [1, 2])): // Check if patient has valid gender
|
|
$dateOfBirth = Carbon::parse($patient->date_of_birth);
|
|
//$dateCreated = Carbon::parse($patient->created_at);
|
|
$currentDateTimeStamp = Carbon::now();
|
|
|
|
$diffInDays = $dateOfBirth->diffInDays($currentDateTimeStamp);
|
|
|
|
|
|
switch ($diffInDays):
|
|
case ($diffInDays >= 0 && $diffInDays <= 29): // 0 - 28 days olds
|
|
if ($patient->gender == 1): // Males
|
|
|
|
if (!empty($triage->new_attendance)):
|
|
$patient_ids_male_0_28_days_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if (!empty($triage->re_attendance)):
|
|
$patient_ids_male_0_28_days_re_attendance[] = $patient->id;
|
|
endif;
|
|
elseif ($patient->gender == 2): // Females
|
|
|
|
if (!empty($triage->new_attendance)):
|
|
$patient_ids_female_0_28_days_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if (!empty($triage->re_attendance)):
|
|
$patient_ids_female_0_28_days_re_attendance[] = $patient->id;
|
|
endif;
|
|
endif;
|
|
break;
|
|
case ($diffInDays >= 28 && $diffInDays <= 1825): // 29 Days - 4 Years
|
|
if ($patient->gender == 1): // Males
|
|
|
|
if (!empty($triage->new_attendance)):
|
|
$patient_ids_male_29days_4yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if (!empty($triage->re_attendance)):
|
|
$patient_ids_male_29days_4yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
elseif ($patient->gender == 2): // Females
|
|
|
|
if (!empty($triage->new_attendance)):
|
|
$patient_ids_female_29days_4yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if (!empty($triage->re_attendance)):
|
|
$patient_ids_female_29days_4yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
endif;
|
|
break;
|
|
case ($diffInDays >= 1826 && $diffInDays <= 3650): // 5 - 9 Years
|
|
if ($patient->gender == 1): // Males
|
|
|
|
if (!empty($triage->new_attendance)):
|
|
$patient_ids_male_5_9yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if (!empty($triage->re_attendance)):
|
|
$patient_ids_male_5_9yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
elseif ($patient->gender == 2): // Females
|
|
|
|
if (!empty($triage->new_attendance)):
|
|
$patient_ids_female_5_9yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if (!empty($triage->re_attendance)):
|
|
$patient_ids_female_5_9yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
endif;
|
|
break;
|
|
case ($diffInDays >= 3651 && $diffInDays <= 7300): // 10 - 19 Years
|
|
if ($patient->gender == 1): // Males
|
|
|
|
if (!empty($triage->new_attendance)):
|
|
$patient_ids_male_10_19yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if (!empty($triage->re_attendance)):
|
|
$patient_ids_male_10_19yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
elseif ($patient->gender == 2): // Females
|
|
|
|
if (!empty($triage->new_attendance)):
|
|
$patient_ids_female_10_19yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if (!empty($triage->re_attendance)):
|
|
$patient_ids_female_10_19yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
endif;
|
|
break;
|
|
case ($diffInDays >= 7301): // 20 Years & Above
|
|
if ($patient->gender == 1): // Males
|
|
|
|
if (!empty($triage->new_attendance)):
|
|
$patient_ids_male_20yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if (!empty($triage->re_attendance)):
|
|
$patient_ids_male_20yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
elseif ($patient->gender == 2): // Females
|
|
|
|
if (!empty($triage->new_attendance)):
|
|
$patient_ids_female_20yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if (!empty($triage->re_attendance)):
|
|
$patient_ids_female_20yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
endif;
|
|
break;
|
|
default:
|
|
break;
|
|
endswitch;
|
|
endif;
|
|
endforeach;
|
|
|
|
$printing = false;
|
|
|
|
|
|
return view('reports::hmis_reports.105.index', compact('hospital_information', 'attendance_from', 'attendance_to',
|
|
'patient_ids_male_0_28_days_new_attendance', 'patient_ids_male_0_28_days_re_attendance',
|
|
'patient_ids_female_0_28_days_new_attendance', 'patient_ids_female_0_28_days_re_attendance',
|
|
'patient_ids_male_29days_4yrs_new_attendance', 'patient_ids_male_29days_4yrs_re_attendance',
|
|
'patient_ids_female_29days_4yrs_new_attendance', 'patient_ids_female_29days_4yrs_re_attendance',
|
|
'patient_ids_male_5_9yrs_new_attendance', 'patient_ids_male_5_9yrs_re_attendance',
|
|
'patient_ids_female_5_9yrs_new_attendance', 'patient_ids_female_5_9yrs_re_attendance',
|
|
'patient_ids_male_10_19yrs_new_attendance', 'patient_ids_male_10_19yrs_re_attendance',
|
|
'patient_ids_female_10_19yrs_new_attendance', 'patient_ids_female_10_19yrs_re_attendance',
|
|
'patient_ids_male_20yrs_new_attendance', 'patient_ids_male_20yrs_re_attendance', 'dateRange',
|
|
'patient_ids_female_20yrs_new_attendance', 'patient_ids_female_20yrs_re_attendance', 'printing'));
|
|
}
|
|
|
|
public function print_hmis_105(Request $request) {
|
|
|
|
$hospital_information = HospitalInformation::find(1);
|
|
//dd($request->all());
|
|
|
|
if(request()->segment(4)) {
|
|
$attendance_from = request()->segment(4);
|
|
// Add one day to make the report accurate
|
|
$attendance_to = request()->segment(5);
|
|
|
|
$dateRange = $request->date_from . '/' . $request->date_to;
|
|
} else {
|
|
$attendance_from = Carbon::today()->subDays(6)->format('Y-m-d');
|
|
$attendance_to = Carbon::today()->addDays(1)->format('Y-m-d');
|
|
|
|
$dateRange = Carbon::today()->subDays(6)->format('d-m-Y') . '/' . Carbon::today()->addDays(1)->format('d-m-Y');
|
|
}
|
|
|
|
$patient_ids_male_0_28_days_new_attendance = [];
|
|
$patient_ids_male_0_28_days_re_attendance = [];
|
|
$patient_ids_female_0_28_days_new_attendance = [];
|
|
$patient_ids_female_0_28_days_re_attendance = [];
|
|
|
|
$patient_ids_male_29days_4yrs_new_attendance = [];
|
|
$patient_ids_male_29days_4yrs_re_attendance = [];
|
|
$patient_ids_female_29days_4yrs_new_attendance = [];
|
|
$patient_ids_female_29days_4yrs_re_attendance = [];
|
|
|
|
$patient_ids_male_5_9yrs_new_attendance = [];
|
|
$patient_ids_male_5_9yrs_re_attendance = [];
|
|
$patient_ids_female_5_9yrs_new_attendance = [];
|
|
$patient_ids_female_5_9yrs_re_attendance = [];
|
|
|
|
$patient_ids_male_10_19yrs_new_attendance = [];
|
|
$patient_ids_male_10_19yrs_re_attendance = [];
|
|
$patient_ids_female_10_19yrs_new_attendance = [];
|
|
$patient_ids_female_10_19yrs_re_attendance = [];
|
|
|
|
$patient_ids_male_20yrs_new_attendance = [];
|
|
$patient_ids_male_20yrs_re_attendance = [];
|
|
$patient_ids_female_20yrs_new_attendance = [];
|
|
$patient_ids_female_20yrs_re_attendance = [];
|
|
|
|
if (!empty($attendance_from) && !empty($attendance_to)) {
|
|
|
|
$triageRecords = DB::table('triage')->whereIn('new_attendance', [0, 1])->whereIn('re_attendance', [0, 1])
|
|
->whereBetween('created_at', [$attendance_from, $attendance_to])
|
|
->select('patient_id', 'new_attendance', 're_attendance')->get();
|
|
|
|
foreach ($triageRecords as $triage):
|
|
// Get patient and their age
|
|
$patient = DB::table('patients')->where('id', $triage->patient_id)->select('id', 'date_of_birth', 'gender', 'created_at')->first();
|
|
|
|
if ($patient && in_array($patient->gender, [1, 2])): // Check if patient has valid gender
|
|
$dateOfBirth = Carbon::parse($patient->date_of_birth);
|
|
$dateCreated = Carbon::parse($patient->created_at);
|
|
|
|
$diffInDays = $dateOfBirth->diffInDays($dateCreated);
|
|
|
|
|
|
switch ($diffInDays):
|
|
case ($diffInDays >= 0 && $diffInDays <= 29): // 0 - 28 days olds
|
|
if ($patient->gender == 1): // Males
|
|
|
|
if ($triage->new_attendance):
|
|
$patient_ids_male_0_28_days_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if ($triage->re_attendance):
|
|
$patient_ids_male_0_28_days_re_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
elseif ($patient->gender == 2): // Females
|
|
|
|
if ($triage->new_attendance):
|
|
$patient_ids_female_0_28_days_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if ($triage->re_attendance):
|
|
$patient_ids_female_0_28_days_re_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
endif;
|
|
break;
|
|
case ($diffInDays >= 28 && $diffInDays <= 1825): // 29 Days - 4 Years
|
|
if ($patient->gender == 1): // Males
|
|
|
|
if ($triage->new_attendance):
|
|
$patient_ids_male_29days_4yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if ($triage->re_attendance):
|
|
$patient_ids_male_29days_4yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
elseif ($patient->gender == 2): // Females
|
|
|
|
if ($triage->new_attendance):
|
|
$patient_ids_female_29days_4yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if ($triage->re_attendance):
|
|
$patient_ids_female_29days_4yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
endif;
|
|
break;
|
|
case ($diffInDays >= 1826 && $diffInDays <= 3650): // 5 - 9 Years
|
|
if ($patient->gender == 1): // Males
|
|
|
|
if ($triage->new_attendance):
|
|
$patient_ids_male_5_9yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if ($triage->re_attendance):
|
|
$patient_ids_male_5_9yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
elseif ($patient->gender == 2): // Females
|
|
|
|
if ($triage->new_attendance):
|
|
$patient_ids_female_5_9yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if ($triage->re_attendance):
|
|
$patient_ids_female_5_9yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
endif;
|
|
break;
|
|
case ($diffInDays >= 3651 && $diffInDays <= 7300): // 10 - 19 Years
|
|
if ($patient->gender == 1): // Males
|
|
|
|
if ($triage->new_attendance):
|
|
$patient_ids_male_10_19yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if ($triage->re_attendance):
|
|
$patient_ids_male_10_19yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
elseif ($patient->gender == 2): // Females
|
|
|
|
if ($triage->new_attendance):
|
|
$patient_ids_female_10_19yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if ($triage->re_attendance):
|
|
$patient_ids_female_10_19yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
endif;
|
|
break;
|
|
case ($diffInDays >= 7301): // 20 Years & Above
|
|
if ($patient->gender == 1): // Males
|
|
|
|
if ($triage->new_attendance):
|
|
$patient_ids_male_20yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if ($triage->re_attendance):
|
|
$patient_ids_male_20yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
elseif ($patient->gender == 2): // Females
|
|
|
|
if ($triage->new_attendance):
|
|
$patient_ids_female_20yrs_new_attendance[] = $patient->id;
|
|
endif;
|
|
|
|
if ($triage->re_attendance):
|
|
$patient_ids_female_20yrs_re_attendance[] = $patient->id;
|
|
endif;
|
|
endif;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
endswitch;
|
|
|
|
endif;
|
|
endforeach;
|
|
}
|
|
|
|
$data = [
|
|
'hospital_information' => $hospital_information,
|
|
'attendance_from' => $attendance_from,
|
|
'attendance_to' => $attendance_to,
|
|
'patient_ids_male_0_28_days_new_attendance' => $patient_ids_male_0_28_days_new_attendance,
|
|
'patient_ids_male_0_28_days_re_attendance' => $patient_ids_male_0_28_days_re_attendance,
|
|
'patient_ids_female_0_28_days_new_attendance' => $patient_ids_female_0_28_days_new_attendance,
|
|
'patient_ids_female_0_28_days_re_attendance' => $patient_ids_female_0_28_days_re_attendance,
|
|
'patient_ids_male_29days_4yrs_new_attendance' => $patient_ids_male_29days_4yrs_new_attendance,
|
|
'patient_ids_male_29days_4yrs_re_attendance' => $patient_ids_male_29days_4yrs_re_attendance,
|
|
'patient_ids_female_29days_4yrs_new_attendance' => $patient_ids_female_29days_4yrs_new_attendance,
|
|
'patient_ids_female_29days_4yrs_re_attendance' => $patient_ids_female_29days_4yrs_re_attendance,
|
|
'patient_ids_male_5_9yrs_new_attendance' => $patient_ids_male_5_9yrs_new_attendance,
|
|
'patient_ids_male_5_9yrs_re_attendance' => $patient_ids_male_5_9yrs_re_attendance,
|
|
'patient_ids_female_5_9yrs_new_attendance' => $patient_ids_female_5_9yrs_new_attendance,
|
|
'patient_ids_female_5_9yrs_re_attendance' => $patient_ids_female_5_9yrs_re_attendance,
|
|
'patient_ids_male_10_19yrs_new_attendance' => $patient_ids_male_10_19yrs_new_attendance,
|
|
'patient_ids_male_10_19yrs_re_attendance' => $patient_ids_male_10_19yrs_re_attendance,
|
|
'patient_ids_female_10_19yrs_new_attendance' => $patient_ids_female_10_19yrs_new_attendance,
|
|
'patient_ids_female_10_19yrs_re_attendance' => $patient_ids_female_10_19yrs_re_attendance,
|
|
'patient_ids_male_20yrs_new_attendance' => $patient_ids_male_20yrs_new_attendance,
|
|
'patient_ids_male_20yrs_re_attendance' => $patient_ids_male_20yrs_re_attendance,
|
|
'patient_ids_female_20yrs_new_attendance' => $patient_ids_female_20yrs_new_attendance,
|
|
'patient_ids_female_20yrs_re_attendance' => $patient_ids_female_20yrs_re_attendance,
|
|
'printing' => false
|
|
];
|
|
|
|
$pdf = SnappyPDF::loadView('reports::hmis_reports/105/print', $data)
|
|
->setOrientation('landscape')
|
|
->setPaper('a4')
|
|
->setOption('margin-bottom', 10)
|
|
->setOption('footer-left', 'Stre@mline')
|
|
->setOption('footer-right', '[page] of [toPage]');
|
|
|
|
|
|
return $pdf->inline('HMIS 105 Report' . date(" d-m-y h:ia") . '.pdf');
|
|
}
|
|
|
|
public function opd_attendance_drill(Request $request) {
|
|
|
|
$patientIDs = unserialize($request->patients);
|
|
|
|
$patients = DB::table('patients')->whereIn('id', $patientIDs)->orderBy('last_name')->paginate(25);
|
|
$categories = DB::table('patient_categories')->pluck('name', 'id');
|
|
|
|
return view('reports::hmis_reports.105.opd-attendance-details', compact('patients', 'categories'));
|
|
}
|
|
|
|
public function opd_referral_drill(Request $request) {
|
|
$patientIDs = decrypt($request->patients);
|
|
|
|
$patients = DB::table('patients')->whereIn('id', $patientIDs)->orderBy('last_name')->paginate(25);
|
|
$categories = DB::table('patient_categories')->pluck('name', 'id');
|
|
|
|
return view('reports::hmis_reports.105.opd-referral-details', compact('patients', 'categories'));
|
|
}
|
|
|
|
public function opd_diagnosis_drill(Request $request) {
|
|
|
|
$patientIDs = decrypt($request->patients);
|
|
|
|
$patients = DB::table('patients')->whereIn('id', $patientIDs)->orderBy('last_name')->paginate(25);
|
|
$categories = DB::table('patient_categories')->pluck('name', 'id');
|
|
|
|
return view('reports::hmis_reports.105.opd-diagnosis-details', compact('patients', 'categories'));
|
|
}
|
|
|
|
/* form for displaying risky behaviour report */
|
|
|
|
public function risky_behaviours(Request $request) {
|
|
|
|
$hospital_information = HospitalInformation::find(1);
|
|
|
|
if (isset($request->date_from) && isset($request->date_to)) {
|
|
|
|
$attendance_from = Carbon::createFromFormat('d/m/Y', $request->date_from)->toDateString();
|
|
$attendance_to = Carbon::createFromFormat('d/m/Y', $request->date_to)->toDateString();
|
|
|
|
$search_button = $request->filterBtn;
|
|
} else {
|
|
$attendance_from = \Carbon\Carbon::today()->subDays(30)->format('Y-m-d');
|
|
$attendance_to = date("Y-m-d");
|
|
}
|
|
|
|
$alcohol_male_5to9 = [];
|
|
$alcohol_male_10to19 = [];
|
|
$alcohol_male_20plus = [];
|
|
|
|
$tobacco_male_5to9 = [];
|
|
$tobacco_male_10to19 = [];
|
|
$tobacco_male_20plus = [];
|
|
|
|
$alcohol_female_5to9 = [];
|
|
$alcohol_female_10to19 = [];
|
|
$alcohol_female_20plus = [];
|
|
|
|
$tobacco_female_5to9 = [];
|
|
$tobacco_female_10to19 = [];
|
|
$tobacco_female_20plus = [];
|
|
|
|
// $query_behavior = "SELECT t.patient_id as patient_id, t.id as triage_id, t.observations as observations, t.created_at as create_date,"
|
|
// . "d.date_of_birth as dob, d.gender as gender FROM triage t, patients d "
|
|
// . "WHERE (t.created_at BETWEEN '{$attendance_from}' AND '{$attendance_to}')"
|
|
// . " AND (patient_id = d.id) AND ((t.observations LIKE '%Alcohol use=High%') "
|
|
// . "OR (t.observations LIKE '%Alcohol use=Moderate%') "
|
|
// . "OR (t.observations LIKE '%Alcohol use=Low%') "
|
|
// . "OR (t.observations LIKE '%Tobacco use=High%') "
|
|
// . "OR (t.observations LIKE '%Tobacco use=Moderate%') "
|
|
// . "OR (t.observations LIKE '%Tobacco use=Low%')) ORDER BY patient_id DESC";
|
|
|
|
// $triage_results1 = DB::select($query_behavior);
|
|
$triage_results = DB::table('triage')
|
|
->leftJoin('patients', 'triage.patient_id', '=', 'patients.id')
|
|
->select('patient_id', 'date_of_birth as dob','gender','patient_id','observations','triage.id as triage_id','triage.created_at as create_date')
|
|
->whereBetween('triage.created_at', [$attendance_from, $attendance_to])
|
|
->orWhere([ ['observations', 'LIKE', '%Alcohol use=High%'],
|
|
['observations', 'LIKE', '%Alcohol use=Moderate%'],
|
|
['observations', 'LIKE', '%Alcohol use=Low%'],
|
|
['observations', 'LIKE', '%Tobacco use=High%'],
|
|
['observations', 'LIKE', '%Tobacco use=Moderate%'],
|
|
['observations', 'LIKE', '%Tobacco use=Low%']
|
|
])
|
|
->orderBy('patient_id', 'desc')
|
|
->get();
|
|
if (count($triage_results) > 0) {
|
|
foreach ($triage_results as $triage_result) {
|
|
|
|
$dob = new Carbon($triage_result->dob);
|
|
$age = $dob->diffInYears($triage_result->create_date);
|
|
|
|
$age = intval($age);
|
|
$sex = $triage_result->gender;
|
|
$observation_explode = explode(",", $triage_result->observations);
|
|
|
|
if ($age > 5 && $age < 10) {
|
|
if ($sex == 1 && (in_array('Alcohol use=High', $observation_explode) || in_array('Alcohol use=Moderate', $observation_explode) || in_array('Alcohol use=Low', $observation_explode))) {
|
|
array_push($alcohol_male_5to9, $triage_result->patient_id);
|
|
}
|
|
if ($sex == 1 && (in_array('Tobacco use=High', $observation_explode) || in_array('Tobacco use=Moderate', $observation_explode) || in_array('Tobacco use=Low', $observation_explode))) {
|
|
array_push($tobacco_male_5to9, $triage_result->patient_id);
|
|
}
|
|
if ($sex == 2 && (in_array('Alcohol use=High', $observation_explode) || in_array('Alcohol use=Moderate', $observation_explode) || in_array('Alcohol use=Low', $observation_explode))) {
|
|
array_push($alcohol_female_5to9, $triage_result->patient_id);
|
|
}
|
|
if ($sex == 2 && (in_array('Tobacco use=High', $observation_explode) || in_array('Tobacco use=Moderate', $observation_explode) || in_array('Tobacco use=Low', $observation_explode))) {
|
|
array_push($tobacco_female_5to9, $triage_result->patient_id);
|
|
}
|
|
} elseif ($age > 10 && $age < 20) {
|
|
if ($sex == 1 && (in_array('Alcohol use=High', $observation_explode) || in_array('Alcohol use=Moderate', $observation_explode) || in_array('Alcohol use=Low', $observation_explode))) {
|
|
array_push($alcohol_male_10to19, $triage_result->patient_id);
|
|
}
|
|
if ($sex == 1 && (in_array('Tobacco use=High', $observation_explode) || in_array('Tobacco use=Moderate', $observation_explode) || in_array('Tobacco use=Low', $observation_explode))) {
|
|
array_push($tobacco_male_10to19, $triage_result->patient_id);
|
|
}
|
|
if ($sex == 2 && (in_array('Alcohol use=High', $observation_explode) || in_array('Alcohol use=Moderate', $observation_explode) || in_array('Alcohol use=Low', $observation_explode))) {
|
|
array_push($alcohol_female_10to19, $triage_result->patient_id);
|
|
}
|
|
if ($sex == 2 && (in_array('Tobacco use=High', $observation_explode) || in_array('Tobacco use=Moderate', $observation_explode) || in_array('Tobacco use=Low', $observation_explode))) {
|
|
array_push($tobacco_female_10to19, $triage_result->patient_id);
|
|
}
|
|
} elseif ($age > 20) {
|
|
if ($sex == 1 && (in_array('Alcohol use=High', $observation_explode) || in_array('Alcohol use=Moderate', $observation_explode) || in_array('Alcohol use=Low', $observation_explode))) {
|
|
array_push($alcohol_male_20plus, $triage_result->patient_id);
|
|
}
|
|
if ($sex == 1 && (in_array('Tobacco use=High', $observation_explode) || in_array('Tobacco use=Moderate', $observation_explode) || in_array('Tobacco use=Low', $observation_explode))) {
|
|
array_push($tobacco_male_20plus, $triage_result->patient_id);
|
|
}
|
|
if ($sex == 2 && (in_array('Alcohol use=High', $observation_explode) || in_array('Alcohol use=Moderate', $observation_explode) || in_array('Alcohol use=Low', $observation_explode))) {
|
|
array_push($alcohol_female_20plus, $triage_result->patient_id);
|
|
}
|
|
if ($sex == 2 && (in_array('Tobacco use=High', $observation_explode) || in_array('Tobacco use=Moderate', $observation_explode) || in_array('Tobacco use=Low', $observation_explode))) {
|
|
array_push($tobacco_female_20plus, $triage_result->patient_id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return view('reports::hmis_reports.105.risky-behaviours', compact('hospital_information', 'attendance_from', 'attendance_to',
|
|
'triage_results', 'alcohol_male_10to19', 'alcohol_male_5to9', 'alcohol_male_20plus',
|
|
'tobacco_male_10to19', 'tobacco_male_5to9', 'tobacco_male_20plus', 'alcohol_female_10to19',
|
|
'alcohol_female_5to9', 'alcohol_female_20plus', 'tobacco_female_10to19',
|
|
'tobacco_female_5to9', 'tobacco_female_20plus'));
|
|
}
|
|
|
|
public function risky_behaviour_drill(Request $request) {
|
|
$behaviour = $request->behaviour;
|
|
$gender = $request->sex;
|
|
$age = $request->age;
|
|
$patient_id_array = unserialize($request->patient_ids);
|
|
$patients_results = DB::table('patients')->whereIn('id', $patient_id_array)->get();
|
|
return view('reports::hmis_reports.105.risky-behaviours-drill', compact('behaviour', 'gender', 'age', 'patients_results'));
|
|
}
|
|
|
|
public function monthly_outpatient_diagnoses(Request $request) {
|
|
request()->validate([
|
|
'date_from' => 'nullable|date|required_with:date_to',
|
|
'date_to' => 'bail|nullable|required_with:date_from|date|after:date_from',
|
|
]);
|
|
$start = !empty($request->date_from)? date('Y-m-d', strtotime($request->date_from)) : Carbon::now()->subMonth()->startOfMonth()->format('Y-m-d');
|
|
$end = !empty($request->date_to)? date('Y-m-d', strtotime($request->date_to)) : date("Y-m-t", strtotime($start));
|
|
$page = 'Outpatient Diagnoses';$printing = false;
|
|
$citizenship = isset($request->type)? $request->type :1;
|
|
$dates = $primary_diagnosis = $other_diagnoses = $table_options = $dependent_options = $diagnoses_data = [];
|
|
if(empty($request->date_from)){
|
|
$dates['last_month'] = date('F', strtotime($start));
|
|
$dates['last_year'] = date('Y', strtotime($start));
|
|
$dates['days'] = date('t', strtotime($start));
|
|
$dates['citizenship'] = $citizenship;
|
|
$dateRange = $start . '/' . $end;
|
|
$attendance_from = $start;
|
|
$attendance_to = $end;
|
|
}else {
|
|
$dates['start'] = date('d-M-Y', strtotime($start));
|
|
$dates['end'] = date('d-M-Y', strtotime($end));
|
|
$dates['days'] = intval((new DateTime($start))->diff(new DateTime($end))->format("%a"));
|
|
$dates['citizenship'] = $citizenship;
|
|
$attendance_from = $start;
|
|
$attendance_to = $end;
|
|
$dateRange = $attendance_from . '/' . $attendance_to;
|
|
}
|
|
$parent_categories = array_unique(HmisCategoryOptions::where('parent_option', '<>','')->pluck('parent_option')->toArray());
|
|
$episodes = Consultation::leftJoin('patients as p', 'consultations.patient_id','p.id')
|
|
->leftJoin('triage as tr', 'consultations.episode_id','tr.episode_id')
|
|
->select('consultations.id','consultations.patient_id', 'consultations.episode_id', 'gender', 'date_of_birth as dob', 'primary_diagnosis', 'other_diagnoses', 'rdt')
|
|
->whereBetween('consultations.created_at', [Carbon::parse($start)->startOfDay()->toDateTimeString(), Carbon::parse($end)->endOfDay()->toDateTimeString()])->where('citizenship', $citizenship)->get();
|
|
$primary_diagnosis = $ordered_investigations = $prescribed_treatments = $ordered_investigation_results = $others =$other_diagnoses = $patientIds = [];
|
|
foreach ($episodes as $episode) {
|
|
$primary_diagnosis[] = $episode->primary_diagnosis;
|
|
$others = (!empty($episode->other_diagnoses))? unserialize($episode->other_diagnoses):[];
|
|
if(!empty($others)) foreach($others as $other) if(!empty($other)) $other_diagnoses[] = $other;
|
|
$patients[] = ['id'=> $episode->id,'patient_id'=> $episode->patient_id, 'episode_id'=>$episode->episode_id, 'gender'=> $episode->gender, 'age'=> $episode->dob, 'primary_diagnosis'=> $episode->primary_diagnosis, 'other_diagnoses'=> $others, 'rdt' => $episode->rdt ];
|
|
}
|
|
$malaria_diagnoses = Diagnosis::where('outpatient_hmis_category',10096)->whereIn('outpatient_dependent_option', [400, 401, 399, 402])->get(['id','name', 'outpatient_hmis_category_option','outpatient_hmis_category','outpatient_dependent_option'])->first();
|
|
$all_diagnoses = array_unique(array_merge($primary_diagnosis,$other_diagnoses));
|
|
$diag = Diagnosis::whereNotNull(['outpatient_hmis_category','outpatient_hmis_category_option'])->whereIn('id', $all_diagnoses)->get(['id','name', 'outpatient_hmis_category_option','outpatient_hmis_category','outpatient_dependent_option']);
|
|
foreach($diag as $dia) {
|
|
$diagnosis = $dia->id;$other = $primary = $other = [];
|
|
if(!empty($patients)) {
|
|
$primary = array_filter($patients, function ($item) use ($diagnosis) {
|
|
return $item['primary_diagnosis'] == $diagnosis;
|
|
});
|
|
foreach ($patients as $patient) if(in_array($diagnosis, $patient['other_diagnoses'] )) $other[] =$patient;
|
|
}
|
|
$diagnoses_data[]=['id'=>$diagnosis,'name'=>$dia->name, 'hmis_category'=>$dia->outpatient_hmis_category, 'hmis_category_option'=>$dia->outpatient_hmis_category_option, 'dependent_option'=>$dia->outpatient_dependent_option, 'primary'=>$primary, 'other'=>$other];
|
|
}
|
|
|
|
$diagnosis_categories = HmisCategory::where('section_number', '1')->select('id','number', 'title')->get()->toArray();
|
|
foreach($diagnosis_categories as $diagnosis_category) {
|
|
$options=[];
|
|
$category_options = HmisCategoryOptions::where('hmis_category_id', $diagnosis_category['id'])->select('id','number', 'name')->get()->toArray();
|
|
foreach($category_options as $category_option) {
|
|
$dependent_options = [];
|
|
if(in_array($category_option['id'], $parent_categories)) $dependent_options = HmisCategoryOptions::where('parent_option', $category_option['id'])->select('id','number', 'name')->get()->toArray();
|
|
$options[]=['id'=>$category_option['id'], 'name'=>$category_option['name'], 'number'=>$category_option['number'], 'dependent_options'=>$dependent_options];
|
|
}
|
|
$table_options[] = ['category_id'=>$diagnosis_category['id'],'category_name'=>$diagnosis_category['title'],'category_number'=>$diagnosis_category['number'],'category_options'=>$options, 'dependent_options'=>$dependent_options];
|
|
}
|
|
$hospital_information = HospitalInformation::find(1);
|
|
$malaria_investigations =Investigation::whereIn('hmis_002_slug', [2,3])->pluck('id')->toArray();
|
|
$malaria_ids = '';
|
|
foreach($malaria_investigations as $key => $malaria_investigation) {
|
|
if(count($malaria_investigations) == 1) $malaria_ids .= 'FIND_IN_SET('.$malaria_investigation.',r.investigation_id)';
|
|
else {
|
|
if($key == 0) $malaria_ids .= '(FIND_IN_SET('.$malaria_investigation.',r.investigation_id)';
|
|
else $malaria_ids .= ' OR FIND_IN_SET('.$malaria_investigation.',r.investigation_id)';
|
|
if($key == array_key_last($malaria_investigations)) $malaria_ids .=')';
|
|
}
|
|
}
|
|
if(!empty($malaria_ids)){
|
|
$results = OrderedInvestigation::leftJoin('patients as p', 'patient_id','p.id')->leftJoin('investigation_results as r', 'order_id','ordered_investigations.id')
|
|
->leftJoin('consultations as c', 'c.episode_id','ordered_investigations.episode_id')->where([['ordered_investigations.inpatient',0], ['citizenship', $citizenship]])
|
|
->whereRaw($malaria_ids)
|
|
->whereBetween('ordered_investigations.created_at', [Carbon::parse($start)->startOfDay()->toDateTimeString(), Carbon::parse($end)->endOfDay()->toDateTimeString()])
|
|
->get(['ordered_investigations.id','p.id as patient_id', 'r.investigation_id','r.episode_id','gender','date_of_birth as age','value', 'r.id as result_id', 'primary_diagnosis', 'other_diagnoses', 'per_investigation']);
|
|
foreach($results as $result) {
|
|
$other_diagnoses = unserialize($result->other_diagnoses);
|
|
$ordered_investigations[] = ['id'=> $result->id,'patient_id'=> $result->patient_id, 'episode_id'=>$result->episode_id, 'gender'=> $result->gender, 'age'=> $result->age, "primary_diagnosis" =>$result->primary_diagnosis?? 'N/A', "other_diagnoses" => (!empty($other_diagnoses))? $other_diagnoses:[]];
|
|
$ids = explode(',', $result->investigation_id);
|
|
$values = explode(',', $result->value);
|
|
$authenticated = explode(',', $result->per_investigation);
|
|
foreach($malaria_investigations as $malaria_investigation){
|
|
$id = array_search($malaria_investigation, $ids);
|
|
if($id !== false) {
|
|
$inv_result = strtolower($values[$id]);
|
|
$result_authenticated = $authenticated[$id];
|
|
if((str_contains($inv_result, 'pos') || $inv_result =='+'|| $inv_result =='++'|| $inv_result =='+++') && !in_array($result->patient_id, $patientIds) && !empty($result_authenticated)) {
|
|
$ordered_investigation_results[] = ['id'=> $result->result_id,'patient_id'=> $result->patient_id, 'result'=>$inv_result ,'episode_id'=>$result->episode_id, 'gender'=> $result->gender, 'age'=> $result->age, "primary_diagnosis" => $result->primary_diagnosis?? 'N/A', "other_diagnoses" => (!empty($other_diagnoses))? $other_diagnoses:[]];
|
|
$patientIds[] = $result->patient_id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$anti_malarials = DB::table('drugs')->leftJoin('drug_categories as d', 'category_id','d.id')->where('d.anti_malarial', 1)->pluck('drugs.id')->toArray();
|
|
$patientIds = [];
|
|
$anti_malarial_ids = '';
|
|
foreach($anti_malarials as $key => $anti_malarial) {
|
|
if(count($anti_malarials) == 1) $anti_malarial_ids .= 'FIND_IN_SET('.$anti_malarial.',drugs)';
|
|
else {
|
|
if($key == 0) $anti_malarial_ids .= '(FIND_IN_SET('.$anti_malarial.',drugs)';
|
|
else $anti_malarial_ids .= ' OR FIND_IN_SET('.$anti_malarial.',drugs)';
|
|
if($key == array_key_last($anti_malarials)) $anti_malarial_ids .=')';
|
|
}
|
|
}
|
|
if(!empty($anti_malarial_ids)){
|
|
$treatments = Treatment::leftJoin('patients as p', 'patient_id','p.id') ->leftJoin('consultations as c', 'c.episode_id','treatments.episode_id')
|
|
->where([['citizenship', $citizenship]])->whereBetween('treatments.created_at', [Carbon::parse($start)->startOfDay()->toDateTimeString(), Carbon::parse($end)->endOfDay()->toDateTimeString()])
|
|
->whereRaw($anti_malarial_ids)
|
|
->groupBy('patient_id')
|
|
->get(['treatments.id','p.id as patient_id', DB::raw('GROUP_CONCAT(drugs) as drugs'),'c.episode_id','gender','date_of_birth as age', 'primary_diagnosis', 'other_diagnoses']);
|
|
foreach ($treatments as $treatment) {
|
|
$other_diagnoses = unserialize($treatment->other_diagnoses);
|
|
if(!in_array($treatment->patient_id, $patientIds)) {
|
|
$prescribed_treatments[] = ['id'=> $treatment->id,'patient_id'=> $treatment->patient_id, 'episode_id'=>$treatment->episode_id, 'gender'=> $treatment->gender, 'age'=> $treatment->age, "primary_diagnosis" => $treatment->primary_diagnosis, "other_diagnoses" => !empty($other_diagnoses)? $other_diagnoses:[], 'drugs'=> $treatment->drugs];
|
|
$patientIds[] = $treatment->patient_id;
|
|
}
|
|
}
|
|
}
|
|
return view('reports::hmis_reports.105.index', compact('hospital_information','dates','diagnoses_data','table_options','page', 'dateRange', 'attendance_from', 'attendance_to', 'printing', 'ordered_investigation_results', 'ordered_investigations', 'prescribed_treatments', 'malaria_diagnoses'));
|
|
}
|
|
|
|
public function print_hmis_105_information(Request $request){
|
|
$diagnoses_data =!empty($request->diagnoses_data)? unserialize($request->diagnoses_data):[];
|
|
$table_options = !empty($request->table_options)? unserialize($request->table_options):[];
|
|
$ordered_investigation_results = !empty($request->ordered_investigation_results)? unserialize($request->ordered_investigation_results):[];
|
|
$ordered_investigations = !empty($request->ordered_investigations)? unserialize($request->ordered_investigations):[];
|
|
$prescribed_treatments = !empty($request->prescribed_treatments)? unserialize($request->prescribed_treatments):[];
|
|
$dates = !empty($request->dates)? unserialize($request->dates):[];
|
|
$page = $request->page;
|
|
$pdf = SnappyPDF::loadView('reports::hmis_reports.105.print_hmis_105_information', compact('dates','page','table_options','diagnoses_data','ordered_investigations', 'ordered_investigation_results', 'prescribed_treatments'))
|
|
->setOrientation('portrait')
|
|
->setOption('margin-bottom', 7)
|
|
->setOption('margin-top', 5)
|
|
->setOption('footer-html', '<i>'. config('app.name') .' - Printed On ' . date('Y-m-d') . ' By ' . auth()->user()->first_name . " " . auth()->user()->last_name . '</i>');
|
|
return $pdf->inline(__('hmis_reports.hmis_108_report_census') . date(" d-m-y h:ia") . '.pdf');
|
|
|
|
}
|
|
|
|
public function hmis_data(){
|
|
try{
|
|
DB::transaction(function() {
|
|
$options = HmisCategoryOptions::leftJoin('hmis_categories AS a','a.id', 'hmis_category_options.hmis_category_id')
|
|
->select('a.id as outpatient_hmis_category','hmis_category_options.id','parent_option', 'hmis_category_options.number as number')
|
|
->where([['a.type', 0], ['a.section_number',1]])->orWhereNotNull('parent_option')
|
|
->get();
|
|
foreach ($options as $option) {
|
|
if(!empty($option->parent_option)) {
|
|
$outpatient_hmis_category = HmisCategoryOptions::find($option->parent_option);
|
|
Diagnosis::where('hmis_no_outpatient',$option->number)->update(['outpatient_hmis_category'=>$outpatient_hmis_category->hmis_category_id, 'outpatient_hmis_category_option'=>$option->parent_option, 'outpatient_dependent_option'=>$option->id]);
|
|
}
|
|
else Diagnosis::where('hmis_no_outpatient',$option->number)->update(['outpatient_hmis_category'=>$option->outpatient_hmis_category, 'outpatient_hmis_category_option'=>$option->id]);
|
|
}
|
|
});
|
|
|
|
}catch(\Exception $e){
|
|
flash('HMIS Outpatient Data has NOT been reconciled. Contact system admin. '.$e->getMessage())->error();
|
|
return redirect()->back()->withInput();
|
|
}
|
|
|
|
flash("HMIS Outpatient Data has been reconciled.")->success();
|
|
return redirect("/list/hmis-reports");
|
|
}
|
|
|
|
}
|