Files
streamline-emr/docker/_streamline-src/Modules/Patients/Http/Controllers/PostDischargeRiskController.php
T

310 lines
15 KiB
PHP

<?php
namespace Modules\Patients\Http\Controllers;
use Barryvdh\Snappy\Facades\SnappyPdf;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Streamline\Models\HospitalInformation;
use Streamline\Models\VhtContact;
class PostDischargeRiskController extends Controller {
public function view_scores(Request $request) {
if (isset($request->start_date)){
$start_date = Carbon::parse($request->start_date)->startOfDay();
$end_date = Carbon::parse($request->end_date)->endOfDay();
} else {
$start_date = Carbon::now()->startOfDay();
$end_date = Carbon::now()->endOfDay();
}
$risk_scores = DB::table('discharge_mortality_risk')
->whereNotNull('post_discharge_mortality_risk')
->whereNotNull('inpatient_id')
->where('child_with_proven_infection', 1)
->whereBetween('created_at', [$start_date, $end_date])
->get(['id', 'patient_id', 'gender', 'date_of_birth', 'post_discharge_mortality_risk',
'updated_at', 'is_vht_alerted', 'inpatient_id', 'child_with_proven_infection']);
$search_text = "From " . streamline_date($start_date) . " to " . streamline_date($end_date);
return view('patients::post_discharge_risk.view_scores', compact('risk_scores', 'search_text'));
}
public function vht_discharge_forms(Request $request) {
//
}
public function print_vht_discharge_forms($discharge_risk_score_id) {
$hospital_info = HospitalInformation::find(1);
$discharge_risk_score = DB::table('discharge_mortality_risk')
->where('id', $discharge_risk_score_id)
->first();
$patient_id = $discharge_risk_score->patient_id;
if ($discharge_risk_score->inpatient_id) {
$admission_date = streamline_date(get_name($discharge_risk_score->inpatient_id, 'id', 'admitted_on', 'inpatient_info'));
$discharge_date = streamline_date(get_name($discharge_risk_score->inpatient_id, 'id', 'discharged_on', 'inpatient_info'));
$diagnosis_id = get_name($discharge_risk_score->inpatient_id, 'id', 'primary_diagnosis', 'inpatient_info');
$discharge_diagnosis = get_name($diagnosis_id, 'id', 'name', 'diagnoses');
$main_symptom = explode(",", get_name($discharge_risk_score->episode_id, 'episode_id', 'symptoms', 'triage'))[0] ?? "";
} else {
$admission_date = "";
$discharge_date = "";
$discharge_diagnosis = "";
$main_symptom = "";
}
// get the dates for when the follow ups where scheduled
$scheduled_followups = DB::table('phone_followup_patients')
->where('discharge_mortality_risk_id', $discharge_risk_score_id)
->get();
$vht_name = "";
$vht_contact = "";
if (count($scheduled_followups) > 0) {
$first_followup_date = streamline_date($scheduled_followups[0]->follow_up_date);
$second_followup_date = streamline_date($scheduled_followups[1]->follow_up_date);
$third_followup_date = streamline_date($scheduled_followups[2]->follow_up_date);
$vht_name = get_name($scheduled_followups[2]->vht_id, 'id', 'name', 'vht_contacts');
$vht_contact = get_name($scheduled_followups[2]->vht_id, 'id', 'contact', 'vht_contacts');
} else {
$first_followup_date = "";
$second_followup_date = "";
$third_followup_date = "";
}
$data = [
'hospital_info' => $hospital_info,
'admission_date' => $admission_date,
'discharge_date' => $discharge_date,
'patient_id' => $patient_id,
'discharge_risk_score' => $discharge_risk_score,
'first_followup_date' => $first_followup_date,
'second_followup_date' => $second_followup_date,
'third_followup_date' => $third_followup_date,
'vht_name' => $vht_name,
'vht_contact' => $vht_contact,
'discharge_diagnosis' => $discharge_diagnosis,
'main_symptom' => $main_symptom,
];
$pdf = SnappyPDF::loadView('patients::post_discharge_risk/print_vht_discharge_forms', $data)
->setPaper('a4')
->setOption('margin-bottom', 10)
->setOption('footer-html', '<i>Stre@mline</i>');
return $pdf->inline('VHT Discharge Form' . date(" d-m-y h:ia") . '.pdf');
}
public function assign_vht($discharge_id) {
//$vhts = DB::table('vht_contacts')->get();
$post_discharge = DB::table('discharge_mortality_risk')->find($discharge_id);
$patient = DB::table('patients')->find($post_discharge->patient_id);
return view('patients::post_discharge_risk.assign_vht', compact('patient', 'discharge_id'));
}
public function search_vht_by_name_village(Request $request) {
$data = [];
if ($request->has('q')) {
$search = $request->q;
$data = DB::table('vht_contacts')->select("id", "parish_name", "village_name", "name")
->orWhere('village_name', 'LIKE', "%$search%")
->orWhere('name', 'LIKE', "%$search%")
->get();
}
return response()->json($data);
}
public function get_info_about_vht($vht_id) {
$data = DB::table('vht_contacts')->find($vht_id);
return $data->name . " (" . $data->contact . ")" . "&&" . $data->facility . "&&" . $data->parish_name . "&&" . $data->village_name;
}
public function save_assign_vht(Request $request) {
$discharge_id = $request->discharge_id;
// get the discharge info
$post_discharge = DB::table('discharge_mortality_risk')->find($request->discharge_id);
$patient_id = $post_discharge->patient_id;
$inpatient_id = get_name($request->discharge_id, 'id', 'inpatient_id', 'discharge_mortality_risk');
$inpatient_info = DB::table('inpatient_info')
->where('id', $inpatient_id)
->first();
// vht contact info
$vht = DB::table('vht_contacts')->find($request->vht_id);
$discharge_date = new Carbon(get_patient_discharge_date($inpatient_id));
$first_followup_date = $discharge_date->copy()->addDays(2);
$second_followup_date = $discharge_date->copy()->addDays(7);
$third_followup_date = $discharge_date->copy()->addDays(14);
if ($vht) {
// send the message
DB::table('phone_followup_patients')
->insert(['patient_id' => $patient_id, 'created_at' => date('Y-m-d H:i:s'), 'vht_id' => $vht->id,
'episode_id' => $inpatient_info->episode_id, 'inpatient_id' => $inpatient_info->id, 'discharge_mortality_risk_id' => $discharge_id,
'follow_up_date' => $first_followup_date, 'discharge_date' => get_patient_discharge_date($inpatient_id)]);
DB::table('phone_followup_patients')
->insert(['patient_id' => $patient_id, 'created_at' => date('Y-m-d H:i:s'), 'vht_id' => $vht->id,
'episode_id' => $inpatient_info->episode_id, 'inpatient_id' => $inpatient_info->id, 'discharge_mortality_risk_id' => $discharge_id,
'follow_up_date' => $second_followup_date, 'discharge_date' => get_patient_discharge_date($inpatient_id)]);
DB::table('phone_followup_patients')
->insert(['patient_id' => $patient_id, 'created_at' => date('Y-m-d H:i:s'), 'vht_id' => $vht->id,
'episode_id' => $inpatient_info->episode_id, 'inpatient_id' => $inpatient_info->id, 'discharge_mortality_risk_id' => $discharge_id,
'follow_up_date' => $third_followup_date, 'discharge_date' => get_patient_discharge_date($inpatient_id)]);
$patient = DB::table('patients')->where('id', $patient_id)->first();
$message = "Dear VHT,\nA child from your area was discharged today. Please complete 3 follow-up visits to assess recovery.\n\nDetails:\n";
$message .= "Child: " . $patient->first_name . " " . $patient->last_name . ",";
$message .= " " . ($patient->gender == 1) ? "Boy" : "Girl" . ",";
$message .= " " . get_patients_age($patient->date_of_birth) . " years,";
$message .= " " . get_name($patient->village_id, 'id', 'name', 'villages') . "\n";
$message .= "From: Kisiizi Hospital\n";
if ($patient->parent_id) {
$message .= "Parent: " . get_full_name($patient->parent_id, 'id', 'first_name', 'last_name', 'patients') . ", " . get_name($patient->parent_id, 'id', 'phone', 'patients') . "\n";
} elseif ($patient->hospital_contact) {
$message .= "Parent: " . $patient->hospital_contact_name . ", " . $patient->hospital_contact . "\n";
} elseif ($patient->next_of_kin) {
$message .= "Parent: " . $patient->next_of_kin . ", " . $patient->phone_of_next_of_kin . "\n";
}
$number = "+256" . $vht->contact;
$message .= "Follow-ups: " . $first_followup_date->format('D j M') . ", " . $second_followup_date->format('D j M') . ", " . $third_followup_date->format('D j M') . "";
send_sms($number, $message);
DB::table('discharge_mortality_risk')
->where('id', $discharge_id)
->update(['is_vht_alerted' => 1]);
send_data_to_redcap($discharge_id);
flash("VHT has been assigned and an SMS message has been sent to them")->success();
return redirect('/post_discharge_risk/view_scores');
} else {
flash("No VHT was found for the patient")->error();
return redirect('/post_discharge_risk/view_scores');
}
}
public function retry_sending_message($discharge_risk_score_id) {
$discharge_id = $discharge_risk_score_id;
// get the discharge info
$post_discharge = DB::table('discharge_mortality_risk')->find($discharge_id);
$patient_id = $post_discharge->patient_id;
$inpatient_id = get_name($discharge_id, 'id', 'inpatient_id', 'discharge_mortality_risk');
$inpatient_info = DB::table('inpatient_info')
->where('id', $inpatient_id)
->first();
$vht = DB::table('vht_contacts')
->where('village', get_name($patient_id, 'id', 'village_id', 'patients'))
->orWhere('village', get_name(get_name($patient_id, 'id', 'village_id', 'patients'), 'id', 'name', 'villages'))
->first();
$discharge_date = new Carbon(get_patient_discharge_date($inpatient_id));
$first_followup_date = $discharge_date->copy()->addDays(2);
$second_followup_date = $discharge_date->copy()->addDays(7);
$third_followup_date = $discharge_date->copy()->addDays(14);
if ($vht) {
// send the message
DB::table('phone_followup_patients')
->insert(['patient_id' => $patient_id, 'created_at' => date('Y-m-d H:i:s'), 'vht_id' => $vht->id,
'episode_id' => $inpatient_info->episode_id, 'inpatient_id' => $inpatient_info->id, 'discharge_mortality_risk_id' => $discharge_id,
'follow_up_date' => $first_followup_date, 'discharge_date' => get_patient_discharge_date($inpatient_id)]);
DB::table('phone_followup_patients')
->insert(['patient_id' => $patient_id, 'created_at' => date('Y-m-d H:i:s'), 'vht_id' => $vht->id,
'episode_id' => $inpatient_info->episode_id, 'inpatient_id' => $inpatient_info->id, 'discharge_mortality_risk_id' => $discharge_id,
'follow_up_date' => $second_followup_date, 'discharge_date' => get_patient_discharge_date($inpatient_id)]);
DB::table('phone_followup_patients')
->insert(['patient_id' => $patient_id, 'created_at' => date('Y-m-d H:i:s'), 'vht_id' => $vht->id,
'episode_id' => $inpatient_info->episode_id, 'inpatient_id' => $inpatient_info->id, 'discharge_mortality_risk_id' => $discharge_id,
'follow_up_date' => $third_followup_date, 'discharge_date' => get_patient_discharge_date($inpatient_id)]);
$patient = DB::table('patients')->where('id', $patient_id)->first();
$message = "Dear VHT,\nA child from your area was discharged today. Please complete 3 follow-up visits to assess recovery.\n\nDetails:\n";
$message .= "Child: " . $patient->first_name . " " . $patient->last_name . ",";
$message .= " " . ($patient->gender == 1) ? "Boy" : "Girl" . ",";
$message .= " " . get_patients_age($patient->date_of_birth) . " years,";
$message .= " " . get_name($patient->village_id, 'id', 'name', 'villages') . "\n";
$message .= "From: Kisiizi Hospital\n";
if ($patient->parent_id) {
$message .= "Parent: " . get_full_name($patient->parent_id, 'id', 'first_name', 'last_name', 'patients') . ", " . get_name($patient->parent_id, 'id', 'phone', 'patients') . "\n";
} elseif ($patient->hospital_contact) {
$message .= "Parent: " . $patient->hospital_contact_name . ", " . $patient->hospital_contact . "\n";
} elseif ($patient->next_of_kin) {
$message .= "Parent: " . $patient->next_of_kin . ", " . $patient->phone_of_next_of_kin . "\n";
}
$number = "+256" . $vht->contact;
$message .= "Follow-ups: " . $first_followup_date->format('D j M') . ", " . $second_followup_date->format('D j M') . ", " . $third_followup_date->format('D j M') . "";
send_sms($number, $message);
DB::table('discharge_mortality_risk')
->where('id', $discharge_id)
->update(['is_vht_alerted' => 1]);
send_data_to_redcap($discharge_risk_score_id);
return 1;
} else {
return 0;
}
}
public function view_follow_up_patients(Request $request) {
if (isset($request->start_date)){
$start_date = Carbon::parse($request->start_date)->startOfDay();
$end_date = Carbon::parse($request->end_date)->endOfDay();
} else {
$start_date = Carbon::now()->startOfDay();
$end_date = Carbon::now()->endOfDay();
}
$risk_scores = DB::table('discharge_mortality_risk')
->whereNotNull('post_discharge_mortality_risk')
->whereNotNull('inpatient_id')
->where('child_with_proven_infection', 1)
->whereBetween('created_at', [$start_date, $end_date])
->get();
$search_text = "From " . streamline_date($start_date) . " to " . streamline_date($end_date);
return view('patients::post_discharge_risk.view_follow_up_patients', compact('risk_scores', 'search_text'));
}
}