updated streamline-setup v2

This commit is contained in:
2025-01-15 08:53:49 -08:00
committed by alec.turner
parent a2ce9248f0
commit 4b569f81b0
20228 changed files with 2932048 additions and 63204 deletions
@@ -0,0 +1,54 @@
<?php
namespace Modules\Patients\Http\Controllers;
use Illuminate\Http\Request;
use Streamline\Models\Alert;
use Streamline\Models\Patient;
class AlertsController extends Controller {
public function __construct() {
$this->middleware('auth');
}
/* Add a new patient alert i.e used by the alerts modal in the header */
public function store_patient_alerts(Request $request) {
$alert = new Alert;
$alert->patient_id = $request->alert_patient_id;
$alert->alerts = $request->patient_alerts;
$alert->created_by = auth()->user()->id;
$alert->save();
return $alert;
}
public function view_alerts() {
$patient_id = session()->get('patient_id');
$patient = Patient::find($patient_id);
$alerts = Alert::where('patient_id', $patient_id)->get();
return view('patients::alerts.view_alerts', compact('alerts', 'patient'));
}
public function edit_alert($id) {
$alert = Alert::where(['id' => $id])->first();
return view('patients::alerts.edit_alert', compact('alert'));
}
public function save_edit_alert(Request $request) {
$alert = Alert::find($request->id);
$alert->alerts = $request->name;
$alert->save();
flash("Alert has been saved")->success();
return redirect('/alerts/view_alerts/');
}
public function delete_alert($id) {
$alert = Alert::find($id);
$alert->delete();
flash("Alert has been deleted.")->success();
return redirect('/alerts/view_alerts/');
}
}
@@ -0,0 +1,127 @@
<?php
namespace Modules\Patients\Http\Controllers;
use Illuminate\Http\Request;
use Streamline\Models\Allergy;
use Streamline\Models\Patient;
use Streamline\Models\DrugCategory;
use Streamline\Models\PatientDocument;
use Streamline\Models\Alert;
use Illuminate\Support\Facades\DB;
class AllergiesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$patient_id = session()->get('patient_id');
$patient = Patient::find($patient_id);
$categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id");
$marital_statuses = DB::table('marital_statuses')->pluck("name", "id");
$diagnoses = DB::table('diagnoses')->where('available', 1)->pluck("name", "id");
$clinics = DB::table('clinics')->pluck("name", "id");
$relationships = DB::table('family_relations')->pluck('name', 'id');
$occupations = DB::table('occupations')->pluck('name', 'id');
$patient_categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id');
$districts = DB::table('districts')->pluck('name', 'id');
$counties = DB::table('counties')->pluck('name', 'id');
$subcounties = DB::table('subcounties')->pluck('name', 'id');
$parishes = DB::table('parishes')->pluck('name', 'id');
$villages = DB::table('villages')->pluck('name', 'id');
$drug_categories = DrugCategory::orderBy('name', 'asc')->get();
$documents = PatientDocument::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->take(2)->get();
$known_patient_alerts = Alert::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->take(2)->get();
$known_patient_allergies = Allergy::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->get();
$drug_categories_array = DB::table('drug_categories')->pluck('name', 'id');
$drug_categories = DrugCategory::orderBy('name', 'asc')->get();
return view('patients::allergies.index',compact('patient','known_patient_allergies','drug_categories_array','categories','drug_categories','relationships', 'occupations', 'patient_categories', 'diagnoses', 'categories', 'marital_statuses', 'districts', 'counties', 'subcounties', 'parishes', 'villages', 'drug_categories', 'documents', 'known_patient_alerts'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
/*
* Add allergic drugs to a particular patient
*/
public function store_patient_allergies(Request $request)
{
$patient_id = $request->allergy_patient_id;
$patient_allergies_array = $request->patient_allergies;
$allergies_string = implode(',', $patient_allergies_array);
$existing_allergy = Allergy::where(['patient_id' => $patient_id])->first();
$allergy = is_null($existing_allergy) ? new Allergy : $existing_allergy;
$allergy->patient_id = $patient_id;
$allergy->names = $allergies_string;
$save_allergy = is_null($existing_allergy) ? $allergy->save() : $allergy->update(); /* if new patient allergy then insert else update the db table*/
return $request->all();
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,13 @@
<?php
namespace Modules\Patients\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
@@ -0,0 +1,254 @@
<?php
namespace Modules\Patients\Http\Controllers;
use Illuminate\Http\Request;
use Streamline\Models\TriageNutrition;
class NutritionController extends Controller {
public function get_nutrition_status(Request $request): string {
$age_diff_months = round($request->age_diff_months);
$weight = $request->weight;
$height = $request->height * 100;
$bmi = $request->bmi;
$gender = $request->gender;
$patient_id = $request->patient_id;
$episode_id = $request->episode_id;
$muac = $request->muac;
$oedema = $request->oedema;
$reference_age = NULL;
$reference_bmi = NULL;
$reference_height = NULL;
$reference_weight = NULL;
$texts_array = ["Normal" => 1, "Risk of overweight" => 2, "MAM" => 3,
"SAM without oedema" => 4, "SAM with oedema" => 5];
if (!is_null($request->weight) && !is_null($request->height) && !is_null($request->bmi)) {
if ($age_diff_months > 5 && $age_diff_months < 60) {
if ($gender == 2){
$data = file(base_path('public/uploads/nutrition_csv_lookup/female_6_60.csv'));
} else {
$data = file(base_path('public/uploads/nutrition_csv_lookup/male_6_60.csv'));
}
$formatted_data = [];
$lengths = [];
foreach($data as $item) {
$split_item = explode(',', $item);
$lengths[] = intval($split_item[0]);
$formatted_data[intval($split_item[0])] = [intval($split_item[1]), intval($split_item[2]), intval($split_item[3]), intval($split_item[4]), intval(str_replace(["\r", "\n"], "", $split_item[5]))];
}
// check if height is out of bounds
if ($height < $lengths[0] || $height > $lengths[count($lengths) - 1]) {
$text = "Nutrition Status Not Available";
$reason = "Height is out of range";
$text_color = "red";
return $text . "&&&&" . $reason . "&&&&" . $text_color;
}
$reference_height = get_closest_element_in_array($lengths, $height);
$reference_weight = get_closest_element_in_array($formatted_data[$reference_height], $weight);
$weight_key = array_search($reference_weight, $formatted_data[$reference_height]);
if ($weight_key == 0) {
$text = "SAM without oedema";
$reason = "Weight for height < -3 SD";
$text_color = "red";
} elseif ($weight_key == 1) {
$text = "MAM";
$reason = "Weight for height between -3 SD and -2 SD";
$text_color = "darkorange";
} elseif ($weight_key == 2 || $weight_key == 3) {
$text = "Normal";
$reason = "Weight for height between -2 SD and 2 SD";
$text_color = "black";
} else {
$text = "Risk of overweight";
$reason = "Weight for height > 2 SD";
$text_color = "black";
}
} elseif ($age_diff_months > 59 && $age_diff_months < 228) {
if ($age_diff_months < 120) {
if ($gender == 2){
$data = file(base_path('public/uploads/nutrition_csv_lookup/female_60_120.csv'));
} else {
$data = file(base_path('public/uploads/nutrition_csv_lookup/male_60_120.csv'));
}
} else {
if ($gender == 2){
$data = file(base_path('public/uploads/nutrition_csv_lookup/female_120_228.csv'));
} else {
$data = file(base_path('public/uploads/nutrition_csv_lookup/male_120_228.csv'));
}
}
$formatted_data = [];
$ages_in_months = [];
foreach($data as $item) {
$split_item = explode(',', $item);
$ages_in_months[] = intval($split_item[0]);
$formatted_data[intval($split_item[0])] = [intval($split_item[1]), intval($split_item[2]), intval($split_item[3]), intval($split_item[4]), intval(str_replace(["\r", "\n"], "", $split_item[5]))];
}
$reference_age = get_closest_element_in_array($ages_in_months, $age_diff_months);
$reference_bmi = get_closest_element_in_array($formatted_data[$reference_age], $bmi);
$bmi_key = array_search($reference_bmi, $formatted_data[$reference_age]);
if ($bmi_key == 0) {
$text = "SAM without oedema";
$reason = "BMI < -3 SD";
$text_color = "red";
} elseif ($bmi_key == 1) {
$text = "MAM";
$reason = "BMI between -3 SD and -2 SD";
$text_color = "darkorange";
} elseif ($bmi_key == 2 || $bmi_key == 3) {
$text = "Normal";
$reason = "BMI between -2 SD and 1 SD";
$text_color = "black";
} else {
$text = "Risk of overweight";
$reason = "BMI > 1 SD";
$text_color = "black";
}
} elseif ($age_diff_months > 227 && $age_diff_months < 1200) {
if ($bmi < 16) {
$text = "SAM without oedema";
$reason = "BMI < 16";
$text_color = "red";
} elseif ($bmi > 15.9 && $bmi < 17) {
$text = "MAM";
$reason = "BMI between 16 and 17";
$text_color = "darkorange";
} elseif ($bmi > 16.9 && $bmi < 25) {
$text = "Normal";
$reason = "BMI between 17 and 25";
$text_color = "black";
} else {
$text = "Risk of overweight";
$reason = "BMI ≥ 25";
$text_color = "black";
}
} else {
return "0";
}
} else {
$text = "Normal";
$reason = "Normal";
$text_color = 'black';
}
// check for oedema status
if ($oedema == 1) {
$oedema = "Yes";
$oedema_text = "SAM with oedema";
$oedema_reason = "Oedema of both feet";
$oedema_text_color = "red";
} else {
$oedema = "No";
$oedema_text = "Normal";
$oedema_reason = "";
$oedema_text_color = "black";
}
// check if oedema has a higher category
if (($texts_array[$oedema_text] > $texts_array[$text])) {
$text = $oedema_text;
$reason = $oedema_reason;
$text_color = $oedema_text_color;
}
if ($age_diff_months < 1.59 && $muac < 11.0) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 11.0";
$muac_text_color = 'red';
} else if(between($age_diff_months, 1.6, 5) && $muac < 11.5) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 11.5";
$muac_text_color = 'red';
} else if(between($age_diff_months, 6, 59) && between($muac, 11.5, 12.4)) {
$muac_text = "MAM";
$muac_reason = "MUAC between 11.5 and 12.4";
$muac_text_color = 'darkorange';
} else if(between($age_diff_months, 6, 59) && $muac < 11.5) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 11.5";
$muac_text_color = 'red';
} else if(between($age_diff_months, 60, 119) && between($muac, 13.5, 14.4)) {
$muac_text = "MAM";
$muac_reason = "MUAC between 13.5 and 14.4";
$muac_text_color = 'darkorange';
} else if(between($age_diff_months, 60, 119) && $muac < 13.5) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 13.5";
$muac_text_color = 'red';
} else if(between($age_diff_months, 120, 179) && between($muac, 16.0, 18.4)) {
$muac_text = "MAM";
$muac_reason = "MUAC between 16.0 and 18.4";
$muac_text_color = 'darkorange';
} else if(between($age_diff_months, 120, 179) && $muac < 16.0) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 16.0";
$muac_text_color = 'red';
} else if(between($age_diff_months, 180, 215) && between($muac, 18.5, 20.9)) {
$muac_text = "MAM";
$muac_reason = "MUAC between 18.5 and 21.0";
$muac_text_color = 'darkorange';
} else if(between($age_diff_months, 180, 215) && $muac < 18.5) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 18.5";
$muac_text_color = 'red';
} else if($age_diff_months > 215 && between($muac, 19.0, 21.9)) {
$muac_text = "MAM";
$muac_reason = "MUAC between 19.0 and 21.9";
$muac_text_color = 'darkorange';
} else if($age_diff_months > 215 && $muac < 19.0) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 19.0";
$muac_text_color = 'red';
} else {
$muac_text = "Normal";
$muac_reason = "Normal";
$muac_text_color = 'black';
}
if (($texts_array[$muac_text] > $texts_array[$text])) {
$text = $muac_text;
$reason = $muac_reason;
$text_color = $muac_text_color;
}
// save data to the nutrition table
$nutrition = TriageNutrition::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->first();
if (!$nutrition) {
$nutrition = new TriageNutrition();
}
$nutrition->patient_id = $patient_id;
$nutrition->episode_id = $episode_id;
$nutrition->age_diff_months = $age_diff_months;
$nutrition->weight = $weight;
$nutrition->height = $height;
$nutrition->bmi = $bmi;
$nutrition->muac = $muac;
$nutrition->oedema = $oedema;
$nutrition->gender = $gender;
$nutrition->reference_height = $reference_height;
$nutrition->reference_weight = $reference_weight;
$nutrition->text = $text;
$nutrition->reason = $reason;
$nutrition->reference_age = $reference_age;
$nutrition->reference_bmi = $reference_bmi;
$nutrition->updated_at = now();
$nutrition->save();
return $text . "&&&&" . $reason . "&&&&" . $text_color;
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,243 @@
<?php
namespace Modules\Patients\Http\Controllers;
use Illuminate\Http\Request;
use Streamline\Models\Alert;
use Streamline\Models\Allergy;
use Streamline\Models\DrugCategory;
use Streamline\Models\PatientDocument;
use Streamline\Models\Patient;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class PatientDocumentController extends Controller {
public function __construct() {
$this->middleware('auth');
}
public function index() {
$patient_id = session()->get('patient_id');
$patient_episodes = \Illuminate\Support\Facades\DB::table('patient_episodes')
->where(['patient_id' => $patient_id])
->orderBy('id', 'desc')
->get();
$patient = Patient::where(['id' => $patient_id])->first();
$categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id");
$marital_statuses = DB::table('marital_statuses')->pluck("name", "id");
$diagnoses = DB::table('diagnoses')->where('available', 1)->pluck("name", "id");
$clinics = DB::table('clinics')->pluck("name", "id");
$relationships = DB::table('family_relations')->pluck('name', 'id');
$occupations = DB::table('occupations')->pluck('name', 'id');
$patient_categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id');
$districts = DB::table('districts')->pluck('name', 'id');
$counties = DB::table('counties')->pluck('name', 'id');
$subcounties = DB::table('subcounties')->pluck('name', 'id');
$parishes = DB::table('parishes')->pluck('name', 'id');
$villages = DB::table('villages')->pluck('name', 'id');
$drug_categories = DrugCategory::orderBy('name', 'asc')->get();
$documents = PatientDocument::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->get();
$known_patient_allergies = Allergy::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->take(2)->get();
$known_patient_alerts = Alert::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->take(2)->get();
$drug_categories_array = DB::table('drug_categories')->pluck('name', 'id');
return view('patients::patient_documents.index', compact('patient', 'clinics', 'relationships', 'occupations', 'patient_categories', 'diagnoses', 'patient_episodes', 'categories', 'marital_statuses', 'districts', 'counties', 'subcounties', 'parishes', 'villages', 'drug_categories', 'documents', 'known_patient_allergies', 'known_patient_alerts', 'drug_categories_array'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
$patient_id = session()->get('patient_id');
$episode_id = session()->get('episode_id');
$patient = Patient::find($patient_id);
$categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id");
$drug_categories = DB::table('drug_categories')->get();
$marital_statuses = DB::table('marital_statuses')->pluck("name", "id");
$diagnoses = DB::table('diagnoses')->where('available', 1)->pluck("name", "id");
$clinics = DB::table('clinics')->pluck("name", "id");
$relationships = DB::table('family_relations')->pluck('name', 'id');
$occupations = DB::table('occupations')->pluck('name', 'id');
$patient_categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id');
$districts = DB::table('districts')->pluck('name', 'id');
$counties = DB::table('counties')->pluck('name', 'id');
$subcounties = DB::table('subcounties')->pluck('name', 'id');
$parishes = DB::table('parishes')->pluck('name', 'id');
$villages = DB::table('villages')->pluck('name', 'id');
$documents = PatientDocument::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->get();
$drug_categories = DB::table('drug_categories')->orderBy('name', 'asc')->get();
//allergies and alerts
$known_patient_allergies = \Streamline\Models\Allergy::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->take(2)->get();
$known_patient_alerts = \Streamline\Models\Alert::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->take(2)->get();
$drug_categories_array = DB::table('drug_categories')->pluck('name', 'id');
return view('patients::patient_documents.create', compact('patient', 'clinics', 'relationships', 'occupations', 'patient_categories', 'diagnoses', 'categories', 'marital_statuses', 'districts', 'counties', 'subcounties', 'parishes', 'villages', 'drug_categories', 'episode_id', 'documents', 'drug_categories_array', 'drug_categories', 'known_patient_allergies', 'known_patient_alerts'));
}
/**
* Store a newly created resource in storage.
*
*/
public function store(Request $request) {
request()->validate([
'documenttitle' => 'required'
]);
$patient_document = new PatientDocument;
$patient_document->patient_id = session()->get('patient_id');
$patient_document->episode_id = session()->get('episode_id');
$patient_document->title = $request->documenttitle;
$patient_document->description = $request->description;
if ($request->file('document')->isValid()) {
$file = $request->file('document');
$store = public_path() . '/uploads/patient-documents/';
$file_name = $file->getClientOriginalName();
$file->move($store, $file_name);
$patient_document->path = public_path() . '/uploads/patient-documents/' . $file_name;
}
$patient_document->date_taken = \Carbon\Carbon::createFromFormat('d/m/Y', $request->documentdate)->toDateString();
$patient_document->created_by = auth()->user()->id;
$patient_document->úpdated_by = auth()->user()->id;
$patient_document->save();
flash("document has been added.")->success();
// redirect to consultation or patient_episode page depending on where the user is from
if (session()->has('redirect_to_consultation')) {
$url = session()->get('redirect_to_consultation');
session()->forget('redirect_to_consultation');
return redirect($url);
} else {
return redirect("/patient_episodes");
}
}
public function modal_store(Request $request) {
$validator = Validator::make($request->all(), [
'documenttitle' => 'required',
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$patient_document = new PatientDocument;
$patient_document->patient_id = session()->get('patient_id');
$patient_document->episode_id = session()->get('episode_id');
$patient_document->title = $request->documenttitle;
$patient_document->description = $request->description;
if ($request->file('document')->isValid()) {
$file = $request->file('document');
$store = public_path() . '/uploads/patient-documents/';
$file_name = $file->getClientOriginalName();
$file->move($store, $file_name);
$patient_document->path = public_path() . '/uploads/patient-documents/' . $file_name;
}
$patient_document->date_taken = \Carbon\Carbon::createFromFormat('d/m/Y', $request->documentdate)->toDateString();
$patient_document->created_by = auth()->user()->id;
$patient_document->úpdated_by = auth()->user()->id;
$patient_document->save();
flash("document has been added.")->success();
return redirect('/consultation/route');
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
$document = PatientDocument::find($id);
if (substr($document->path, 0, 25) === "../uploads/patient_upload") {
$document_path_cleaned = str_replace("../uploads/patient_uploads", "/var/www/html/uploads/patient-documents", $document->path);
} else {
$document_path_cleaned = $document->path;
}
$ext = pathinfo($document->path, PATHINFO_EXTENSION);
if ($ext == "pdf") {
return response()->file($document_path_cleaned, ['Content-Type' => 'application/pdf']);
} elseif ($ext == 'doc') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'application/msword'
]);
} elseif ($ext == 'docx') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
]);
} elseif ($ext == 'xls') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'application/vnd.ms-excel'
]);
} elseif ($ext == 'xlsx') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
]);
} elseif ($ext == 'txt') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'application/octet-stream'
]);
} elseif ($ext == 'png' || $ext == 'jpg' || $ext == 'JPG' || $ext == 'jpeg') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'image/jpeg'
]);
}
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$document = PatientDocument::find($id);
if ($document->delete()):
flash("document has been deleted.")->error();
return redirect('/patient_documents/');
endif;
}
/*
* put the episode id in the session before creating the document
*/
public function set_episode_id($id) {
session()->put(['episode_id' => $id]);
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,231 @@
<?php
namespace Modules\Patients\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Streamline\Models\Clinic;
use Streamline\Models\PatientEpisode;
use Streamline\Models\InpatientInfo;
use Illuminate\Database\QueryException;
use Streamline\Models\Consultation;
class PatientFlowMonitoringController extends Controller {
public function __construct() {
$this->middleware('auth');
$this->middleware('permission:patient-flow-monitoring');
}
public function index(Request $request){
$clinic_id = $request->clinic_id;
$search_by = $request->search_by;
$reg_date = $request->reg_date;
$start_date = $request->start_date;
$end_date = $request->end_date;
$order_by = $request->order_by ?? 1;
if (!isset($clinic_id) && !isset($search_by)){
$clinic_id = session()->get('clinic_id');
$search_by = session()->get('search_by');
$reg_date = session()->get('reg_date');
$start_date = session()->get('start_date');
$end_date = session()->get('end_date');
$order_by = session()->get('order_by');
} else {
session()->put('clinic_id', $clinic_id);
session()->put('search_by', $search_by);
session()->put('reg_date', $reg_date);
session()->put('start_date', $start_date);
session()->put('end_date', $end_date);
session()->put('order_by', $order_by);
}
if($search_by === 0){
// last 24 hours
$start_date_search = Carbon::yesterday()->startOfDay()->toDateTimeString();
$end_date_search = Carbon::yesterday()->endOfDay()->toDateTimeString();
$date_search = "Yesterday";
} elseif($search_by == 1){
// custom date
$start_date_search = Carbon::parse($reg_date)->startOfDay()->toDateTimeString();
$end_date_search = Carbon::parse($reg_date)->endOfDay()->toDateTimeString();
$date_search = streamline_date($start_date_search);
} elseif($search_by == 2){
// custom date range
$start_date_search = Carbon::parse($start_date)->startOfDay()->toDateTimeString();
$end_date_search = Carbon::parse($end_date)->endOfDay()->toDateTimeString();
$date_search = streamline_date($start_date_search) . " to " . streamline_date($end_date_search);
} else {
// Today
$start_date_search = Carbon::today()->startOfDay()->toDateTimeString();
$end_date_search = Carbon::today()->endOfDay()->toDateTimeString();
$date_search = "Today";
}
switch (get_select_clinic_order_type()) {
case 0:
if ($order_by == 0) {
$order_by_text = "patient_episodes.id";
} else {
$order_by_text = "triage.severe_grade desc, patient_episodes.id";
}
break;
case 1:
if ($order_by == 0) {
$order_by_text = "patient_episodes.id desc";
} else {
$order_by_text = "triage.severe_grade desc, patient_episodes.id desc";
}
break;
case 2:
default:
if ($order_by == 0) {
$order_by_text = "consultations.completed, patient_episodes.id";
} else {
$order_by_text = "consultations.completed, triage.severe_grade desc, patient_episodes.id";
}
break;
}
if($clinic_id == 0){
$patient_episodes = DB::table('patient_episodes')
->whereNull('patient_episodes.deleted_at')
->leftJoin('consultations', 'patient_episodes.id', '=', 'consultations.episode_id')
->leftJoin('ante_natal_clinic_followups as a', 'patient_episodes.id', '=', 'a.episode_id')
->leftJoin('triage', 'patient_episodes.id', '=', 'triage.episode_id')
->select('patient_episodes.*', 'consultations.primary_diagnosis','consultations.outcome_id','consultations.completed','consultations.created_by as consultation_created_by','consultations.updated_by as consultation_updated_by','consultations.consultation_done_by', 'a.primary_diagnosis as antenatal_primary_diagnosis','a.outcome_id as antenatal_outcome_id','a.completion_status as antenatal_completed','a.created_by as antenatal_created_by','a.updated_by as antenatal_updated_by', 'triage.id as episode_triage_id', 'triage.severe_grade', 'triage.clinic_allocation')
->whereBetween('patient_episodes.created_at', [$start_date_search, $end_date_search])
->orderByRaw($order_by_text)
->paginate(200);
$clinic_name = "OPD";
} else {
$patient_episodes = DB::table('patient_episodes')
->whereNull('patient_episodes.deleted_at')
->leftJoin('consultations', 'patient_episodes.id', '=', 'consultations.episode_id')
->leftJoin('ante_natal_clinic_followups as a', 'patient_episodes.id', '=', 'a.episode_id')
->leftJoin('triage', 'patient_episodes.id', '=', 'triage.episode_id')
->where(['patient_episodes.clinic_id' => $clinic_id])
->whereBetween('patient_episodes.created_at', [$start_date_search, $end_date_search])
->orderByRaw($order_by_text)
->select('patient_episodes.*', 'consultations.primary_diagnosis','consultations.outcome_id','consultations.completed','consultations.created_by as consultation_created_by','consultations.updated_by as consultation_updated_by','consultations.consultation_done_by', 'a.primary_diagnosis as antenatal_primary_diagnosis','a.outcome_id as antenatal_outcome_id','a.completion_status as antenatal_completed','a.created_by as antenatal_created_by','a.updated_by as antenatal_updated_by','triage.id as episode_triage_id', 'triage.severe_grade', 'triage.clinic_allocation')
->paginate(200);
$clinic_name = get_name($clinic_id, 'id', 'name', 'clinics');
}
$patient_categories = DB::table("patient_categories")->whereNull('deleted_at')->pluck("name", "id");
$clinics = DB::table("clinics")->whereNull("deleted_at")->orderBy("name")->pluck("name", "id")->toArray();
$clinics = [0 => 'OPD'] + $clinics;
$clinics = ['' => '- select -'] + $clinics;
$diagnoses = DB::table('diagnoses')->where('available', 1)->whereNull('deleted_at')->pluck("name", "id")->toArray();
$wards = DB::table('wards')->where('available', 1)->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '');
return view('patients::patient_flow_monitoring.index', compact('patient_episodes', 'clinics', 'patient_categories','clinic_name','search_by', 'date_search', 'wards', 'diagnoses'));
}
public function patient_route($episode_id, $route){
$episode = PatientEpisode::find($episode_id);
$patient_id = $episode->patient_id;
// set up session
session()->put(['patient_id' => $patient_id]);
session()->put(['episode_id' => $episode_id]);
if($route == 'triage'){
$url = '/triage';
session()->put('triage_without_etat', 0);
} elseif ($route == 'consultation'){
session()->put('consultation_with_notes', 0);
$url = '/consultation/route';
} elseif ($route == 'create_anaesthetics'){
$url = '/anaesthetics/create';
} elseif ($route == 'create_surgery'){
$url = '/theatre_surgery/create';
} elseif ($route == 'anaesthetics_history'){
$url = '/anaesthetics/history';
} elseif ($route == 'surgery_index'){
$url = '/theatre_surgery';
} elseif ($route == 'treatment') {
$url = '/prescriptions/create';
} elseif ($route == 'anc_registration_button'){
$url = '/ante_natal_clinic/create';
} elseif ($route == 'anc_followup_button'){
$url = '/ante_natal_clinic_follow_up/create';
} elseif ($route == 'investigation') {
$url = '/investigations/investigations_review';
} elseif ($route == 'triage_without_etat') {
session()->put('triage_without_etat', 1);
$url = '/triage';
} elseif ($route == 'consultation_with_notes') {
session()->put('consultation_with_notes', 1);
$url = '/consultation/route';
} elseif ($route == 'view_patient_history') {
$url = '/patient_episodes/';
} elseif ($route == 'main_exam') {
$url = '/eye_clinic/main_exam_route';
} elseif ($route == 'base_refraction_exam') {
$url = '/eye_clinic/base_exam_refraction';
}
return response()->json($url);
}
public function inpatient_admission(Request $request)
{
$episode = PatientEpisode::find($request->admission_episode_id);
$patient_id = $episode->patient_id;
$ward_id = $request->admission_ward_id;
$admitted_on = $request->ward_admission_date;
try {
$episode_id = $episode->id;
session()->put(['episode_id' => $episode_id]);
session()->put(['patient_id' => $patient_id]);
$consultation = Consultation::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->first();
if ($consultation) {
$consultation->outcome_id = get_name("Admitted", "name", "id", "outcomes");
$consultation->ward_id = $ward_id;
$consultation->admitted_on = $admitted_on;
$consultation->save();
}
// Admit patient in ward
$existing_inpatient = InpatientInfo::where(['episode_id' => $episode_id])->first();
if ($existing_inpatient) {
$ward_id = $existing_inpatient->ward_id;
$ward_name = get_name($ward_id, "id", "name", "wards");
flash("Patient ".get_name($patient_id, "id", "number", "patients")." already admitted for in admitted in ".$ward_name. ". You can use the ward transfer option incase you want to transfer to another ward")->error();
} else {
$inpatient = is_null($existing_inpatient) ? new InpatientInfo : $existing_inpatient;
$inpatient = new InpatientInfo;
$inpatient->patient_id = $patient_id;
$inpatient->episode_id = $episode_id;
$inpatient->admitted_on = $admitted_on;
$inpatient->ward_id = $ward_id;
$inpatient->created_by = auth()->user()->id;
$inpatient->created_at = Carbon::now();
$inpatient->save();
$ward_name = get_name($ward_id, "id", "name", "wards");
flash("Patient ".get_name($patient_id, "id", "number", "patients")." admitted in ".$ward_name)->success();
}
return redirect('/patient_episodes/');
} catch (QueryException $e) {
flash("This episode already exists!")->error();
return back()->withInput();
}
}
}
@@ -0,0 +1,451 @@
<?php
namespace Modules\Patients\Http\Controllers;
use Barryvdh\Snappy\Facades\SnappyPdf;
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Streamline\Models\Drug;
use Streamline\Models\EyeGlasses;
use Streamline\Models\HospitalInformation;
use Streamline\Models\OrderedEyeGlasses;
use Streamline\Models\OrderedService;
use Streamline\Models\OrderedSundry;
use Streamline\Models\Patient;
use Streamline\Models\PatientEpisode;
use Streamline\Models\PointOfSaleRecord;
use Streamline\Models\ReferralHospital;
use Streamline\Models\Sundry;
use Streamline\Models\Services;
use Streamline\Models\Treatment;
class PointOfSaleController extends Controller {
public function index(Request $request){
$search_text = "";
switch ($request->search_date_by){
case 'yesterday':
$end_date = Carbon::yesterday()->endOfDay();
$start_date = Carbon::yesterday()->startOfDay();
$search_text .= "Yesterday ";
break;
case 'custom_date':
$end_date = Carbon::parse($request->start_date)->endOfDay();
$start_date = Carbon::parse($request->start_date)->startOfDay();
$search_text .= "From: " . streamline_date($start_date) . " ";
break;
case 'custom_date_range':
$end_date = Carbon::parse($request->end_date)->endOfDay();
$start_date = Carbon::parse($request->start_date)->startOfDay();
$search_text .= "From: " . streamline_date($start_date) . " to " . streamline_date($end_date) . " ";
break;
case 'today':
default:
$end_date = Carbon::today()->endOfDay();
$start_date = Carbon::today()->startOfDay();
$search_text .= "Today ";
break;
}
$records = PointOfSaleRecord::join('patients', 'point_of_sale_records.patient_id', '=', 'patients.id')
->whereBetween('point_of_sale_records.created_at', [$start_date, $end_date])
->limit(500)->get(['point_of_sale_records.*', 'patients.first_name', 'patients.last_name', 'patients.number']);
return view('patients::point_of_sale.index', compact('records', 'search_text'));
}
public function order_items(){
$drugs = Drug::get();
$sundries = Sundry::where('available', 1)->get();
$services = Services::where('available', 1)->get();
$eye_glasses = EyeGlasses::get();
$referral_hospitals = ReferralHospital::orderBy('name')->get();
return view('patients::point_of_sale.order_items', compact('drugs', 'eye_glasses', 'sundries', 'referral_hospitals', 'services'));
}
public function confirm_items(Request $request){
$pre_ordered_eye_glasses = [];
$manual_patient_prescriptions = [];
$automatic_patient_prescriptions = [];
$pre_ordered_sundries = [];
$pre_ordered_services = [];
if($request->patient_id) {
$patient_id = $request->patient_id;
$patient = Patient::find($patient_id);
// double check if for existing patient_id
if($patient){
$patient_number = Patient::where('id', $patient_id)->pluck('number')->first();
$episode_id = PatientEpisode::where('patient_id',$patient_id)->whereDate('created_at', Carbon::today()->toDateString())->pluck('id')->first();
if(!$episode_id){
$episode = new PatientEpisode;
$episode->patient_id = $patient_id;
$episode->paid_over = "pos";
$episode->created_by = Auth::user()->id;
$episode->updated_by = Auth::user()->id;
$episode->save();
flash('A new episode for patient with patient number ' . $patient_number . ' has been initiated.');
$episode_id = $episode->id;
}
} else {
flash('Patient not found')->error();
redirect('point_of_sale');
}
} else {
$patient = new Patient;
$patient->first_name = $request->first_name;
$patient->last_name = $request->last_name;
$patient->phone = $request->phone_number ?? "";
$patient->referred_from = $request->referral_hospital ?? 1;
$patient->category_id = 1;
$patient->created_by = Auth::user()->id;
$patient->gender = $request->gender ?? 2;
if (is_null($request->date_of_birth)) {
$age_in_years = $request->age_in_years ?? 18;
$calculated_dob = \Carbon\Carbon::now()->subYears($age_in_years);
$calculated_date_of_birth = $calculated_dob->toDateString();
$patient->date_of_birth = $calculated_date_of_birth;
} else {
$patient->date_of_birth = Carbon::createFromFormat('d/m/Y', $request->date_of_birth)->toDateString();
}
if ($patient->save()):
$prefix = DB::table('hospital_information')->where('id', 1)->value('patient_number_abbr');
$patient_id = $patient->id;
$new_id = quadLimit($patient_id);
$patient_number = $prefix . "-" . $new_id;
DB::table('patients')->where('id', $new_id)->update(['number' => $patient_number]); // Updating the patient number
else:
flash("There was an error")->error();
return back()->withInput();
endif;
$episode = new PatientEpisode;
$episode->patient_id = $patient_id;
$episode->clinic_id = get_default_hospital_clinic();
$episode->paid_over = "pos";
$episode->created_by = Auth::user()->id;
$episode->updated_by = Auth::user()->id;
try {
$episode->save();
$episode_id = $episode->id;
flash('Patient with patient number ' . $patient_number . ' has been successfully registered.')->success();
} catch (QueryException $e) {
flash("This episode already exists!")->error();
return back()->withInput();
}
}
if ($request->selected_eye_glasses) {
$pre_ordered_eye_glasses = EyeGlasses::whereIn('id', $request->selected_eye_glasses)->get();
}
if ($request->selected_drugs) {
if($request->manual_drug_select == 1){
$manual_patient_prescriptions = Drug::whereIn('id', $request->selected_drugs)->get();
}else if($request->automatic_drug_select == 1){
$automatic_patient_prescriptions = Drug::whereIn('id', $request->selected_drugs)->get();
}
}
if ($request->selected_sundries) {
$pre_ordered_sundries = Sundry::whereIn('id', $request->selected_sundries)->get();
}
if ($request->selected_services) {
$pre_ordered_services = Services::whereIn('id', $request->selected_services)->get();
}
$allergies = DB::table('allergies')->where(['patient_id' => $patient_id])->pluck('patient_id', 'names');
return view('patients::point_of_sale.confirm_items', compact('patient_id', 'episode_id', 'pre_ordered_eye_glasses',
'manual_patient_prescriptions', 'automatic_patient_prescriptions', 'allergies', 'patient', 'pre_ordered_sundries', 'pre_ordered_services'));
}
public function confirm_pricing(Request $request){
if($request->treatment_item){
$treatment = new Treatment;
$treatment->patient_id = $request->patient_id;
$treatment->episode_id = $request->episode_id;
$treatment->drugs = implode(',', $request->treatment_item);
$drugs_array = $request->treatment_item;
$duration_array = $request->duration;
$time_array = $request->time;
$time_duration = [];
$doses = $request->dose ?? [];
$frequencies = $request->frequency ?? [];
$dose_array = [];
$frequencies_array = [];
$instructions_array = [];
for ($i = 0; $i < count($drugs_array); $i++) {
if (is_drug_chronic($drugs_array[$i])) {
register_chronic_patient($request->patient_id, $request->episode_id, $drugs_array[$i]);
}
if (isset($duration_array[$i]) && isset($time_array[$i])) {
$time_duration[] = $duration_array[$i] . " " . $time_array[$i];
} else {
$time_duration[] = "1 Days";
}
if (isset($doses[$i])) {
$dose_array[] = $doses[$i];
} else {
$dose_array[] = "1";
}
if (isset($frequencies[$i])) {
$frequencies_array[] = $frequencies[$i];
} else {
$frequencies_array[] = "2";
}
$instructions_array[] = "";
}
$treatment->doses = implode(',', $dose_array);
$treatment->frequencies = implode(',', $frequencies_array);
$treatment->instruction = implode(',', $instructions_array);
$treatment->durations = implode(',', $time_duration);
$treatment->quantities_dispensed = implode(',', $request->treatment_quantity);
$treatment->dispense_status = 0;
$treatment->created_by = Auth::id();
$treatment->save();
$request->treatment_id = DB::table('treatments')->where('episode_id', $request->episode_id)->where('patient_id', $request->patient_id)->latest()->pluck('id')->first();
}
$eye_glass_ids = $request->eye_glass_item;
$eye_glass_quantity = $request->eye_glass_quantity;
if($request->eye_glass_item){
for ($i=0; $i < count($eye_glass_ids) ; $i++) {
$new_ordered_eye_glasses = new OrderedEyeGlasses;
$new_ordered_eye_glasses->patient_id = $request->patient_id;
$new_ordered_eye_glasses->episode_id = $request->episode_id;
$new_ordered_eye_glasses->eye_glasses_id = $eye_glass_ids[$i];
$new_ordered_eye_glasses->quantity = $eye_glass_quantity[$i];
$new_ordered_eye_glasses->payment_status = 0; //0 by default to mean not paid
$new_ordered_eye_glasses->created_by = auth()->user()->id;
$new_ordered_eye_glasses->save();
}
}
if($request->pos_sundry_ids){
$new_ordered_sundries = new OrderedSundry;
$new_ordered_sundries->patient_id = $request->patient_id;
$new_ordered_sundries->episode_id = $request->episode_id;
$new_ordered_sundries->sundries_id = implode(",", $request->pos_sundry_ids);
$new_ordered_sundries->quantity = implode(",", $request->sundry_quantity);
$new_ordered_sundries->created_by = auth()->user()->id;
$new_ordered_sundries->save();
}
if ($request->service_id && $request->service_id[0] != null) {
$new_ordered_service = new OrderedService;
$new_ordered_service->patient_id = $request->patient_id;
$new_ordered_service->episode_id = $request->episode_id;
$new_ordered_service->service_id = implode(",", $request->service_id);
$new_ordered_service->quantity = implode(",", $request->quantity);
$new_ordered_service->performed = 0;
$new_ordered_service->performed_id = 0;
$new_ordered_service->created_by = auth()->user()->id;
$new_ordered_service->save();
}
$request->ordered_sundry_ids = OrderedSundry::where('episode_id', $request->episode_id)->where('patient_id', $request->patient_id)
->whereDate('created_at', Carbon::today()->toDateString())->pluck('id')->toArray();
$treatment_item = $treatment_quantity = $treatment_subtotal = [];
// save for treatment
if($request->treatment_id){
$treatment_item = $request->treatment_item;
$treatment_quantity = $request->treatment_quantity;
$treatment_subtotal = $request->treatment_subtotal;
}
$eye_glasses_prices_array = $eye_glasses_quantity_array = $eye_glasses_ids_array = [];
// save for eye_glasses arrays
if($request->eye_glass_item){
$eye_glasses_prices_array = $request->eye_glass_amount;
$eye_glasses_quantity_array = $request->eye_glass_quantity;
$eye_glasses_ids_array = $request->eye_glass_item;
}
$sundry_item = $sundry_quantity = $sundry_subtotal = [];
// save for sundries array
if($request->pos_sundry_ids){
$sundry_item = $request->pos_sundry_ids;
$sundry_quantity = $request->sundry_quantity;
$sundry_subtotal = $request->sundry_subtotal;
}
// save for service arrays
$service_ids_array = [];
$service_prices_array = [];
$service_quantity_array = [];
if($request->service_id){
$service_prices_array = $request->service_item_subtotal;
$service_ids_array = $request->service_id;
$service_quantity_array = $request->quantity;
}
$pos_record = new PointOfSaleRecord();
$pos_record->patient_id = $request->patient_id;
$pos_record->episode_id = $request->episode_id;
$pos_record->treatments = count($treatment_item) > 0 ? json_encode([
"ids" => $treatment_item, "quantity" => $treatment_quantity,
"subtotal" => $treatment_subtotal
]) : NULL;
$pos_record->eye_glasses = count($eye_glasses_ids_array) > 0 ? json_encode([
"ids" => $eye_glasses_ids_array, "quantity" => $eye_glasses_quantity_array,
"subtotal" => $eye_glasses_prices_array
]) : NULL;
$pos_record->sundries = count($sundry_item) > 0 ? json_encode([
"ids" => $sundry_item, "quantity" => $sundry_quantity,
"subtotal" => $sundry_subtotal
]) : NULL;
$pos_record->services = count($service_ids_array) > 0 ? json_encode([
"ids" => $service_ids_array, "quantity" => $service_quantity_array,
"subtotal" => $service_prices_array
]) : NULL;
$pos_record->created_by = Auth::id();
$pos_record->save();
return redirect('point_of_sale/print/' . $pos_record->id);
}
public function add_referral(Request $request) {
$logged_in_user_id = Auth::user()->id;
$referral_hospital = new ReferralHospital;
$referral_hospital->name = $request->name;
$referral_hospital->created_by = $logged_in_user_id;
$referral_hospital->updated_by = $logged_in_user_id;
if ($referral_hospital->save()) {
//insert successful
return $referral_hospital->id;
} else {
return 0;
}
}
public function get_patient(Request $request){
$patient = Patient::where('id', $request->patient_id)->first();
return $patient;
}
public function print($id) {
$record = PointOfSaleRecord::find($id);
if ($record) {
if (is_cashier_receipt_type_print_html()) {
$hospital_information = HospitalInformation::first();
$patient = Patient::find($record->patient_id);
$receipt_date = $record->created_at;
$receipt_reprint_date = date('Y-m-d h:i:s');
$treatments_array = json_decode($record->treatments, true);
$sundries_array = json_decode($record->sundries, true);
$eye_glasses_array = json_decode($record->eye_glasses, true);
$services_array = json_decode($record->services, true);
$treatment_item = $treatments_array ? $treatments_array["ids"] : [];
$treatment_quantity = $treatments_array ? $treatments_array["quantity"] : [];
$treatment_subtotal = $treatments_array ? $treatments_array["subtotal"] : [];
$eye_glasses_ids_array = $eye_glasses_array ? $eye_glasses_array["ids"] : [];
$eye_glasses_quantity_array = $eye_glasses_array ? $eye_glasses_array["quantity"] : [];
$eye_glasses_prices_array = $eye_glasses_array ? $eye_glasses_array["subtotal"] : [];
$sundry_item = $sundries_array ? $sundries_array["ids"] : [];
$sundry_quantity = $sundries_array ? $sundries_array["quantity"] : [];
$sundry_subtotal = $sundries_array ? $sundries_array["subtotal"] : [];
$service_ids_array = $services_array ? $services_array["ids"] : [];
$service_quantity_array = $services_array ? $services_array["quantity"] : [];
$service_prices_array = $services_array ? $services_array["subtotal"] : [];
return view('patients::point_of_sale.receipt', compact('treatment_item', 'treatment_quantity', 'treatment_subtotal',
'eye_glasses_prices_array', 'eye_glasses_quantity_array', 'eye_glasses_ids_array', 'hospital_information', 'patient', 'receipt_date',
'sundry_item','sundry_quantity','sundry_subtotal', 'service_ids_array', 'service_prices_array', 'service_quantity_array', 'receipt_reprint_date'));
} else {
// set up the redirect link for html
session()->put('print_pos_pdf', 1);
session()->put('print_pos_pdf_id', $id);
return redirect('/point_of_sale');
}
} else {
return redirect('/point_of_sale');
}
}
public function print_pos_pdf() {
$id = session()->get("print_pos_pdf_id");
// add check for when the people try to reload the page
if (!$id) {
return redirect('/point_of_sale');
}
// lest i forget Thy love for me
session()->forget('print_pos_pdf');
session()->forget('print_pos_pdf_id');
$record = PointOfSaleRecord::find($id);
if ($record) {
$hospital_information = HospitalInformation::first();
$patient = Patient::find($record->patient_id);
$receipt_date = $record->created_at;
$receipt_reprint_date = date('Y-m-d h:i:s');
$treatments_array = json_decode($record->treatments, true);
$sundries_array = json_decode($record->sundries, true);
$eye_glasses_array = json_decode($record->eye_glasses, true);
$services_array = json_decode($record->services, true);
$treatment_item = $treatments_array ? $treatments_array["ids"] : [];
$treatment_quantity = $treatments_array ? $treatments_array["quantity"] : [];
$treatment_subtotal = $treatments_array ? $treatments_array["subtotal"] : [];
$eye_glasses_ids_array = $eye_glasses_array ? $eye_glasses_array["ids"] : [];
$eye_glasses_quantity_array = $eye_glasses_array ? $eye_glasses_array["quantity"] : [];
$eye_glasses_prices_array = $eye_glasses_array ? $eye_glasses_array["subtotal"] : [];
$sundry_item = $sundries_array ? $sundries_array["ids"] : [];
$sundry_quantity = $sundries_array ? $sundries_array["quantity"] : [];
$sundry_subtotal = $sundries_array ? $sundries_array["subtotal"] : [];
$service_ids_array = $services_array ? $services_array["ids"] : [];
$service_quantity_array = $services_array ? $services_array["quantity"] : [];
$service_prices_array = $services_array ? $services_array["subtotal"] : [];
$data = [
"patient" => $patient, "receipt_date" => $receipt_date, "receipt_reprint_date" => $receipt_reprint_date, "hospital_information" => $hospital_information,
"treatment_item" => $treatment_item, "treatment_quantity" => $treatment_quantity, "treatment_subtotal" => $treatment_subtotal,
"eye_glasses_ids_array" => $eye_glasses_ids_array, "eye_glasses_quantity_array" => $eye_glasses_quantity_array, "eye_glasses_prices_array" => $eye_glasses_prices_array,
"sundry_item" => $sundry_item, "sundry_quantity" => $sundry_quantity, "sundry_subtotal" => $sundry_subtotal,
"service_ids_array" => $service_ids_array, "service_quantity_array" => $service_quantity_array, "service_prices_array" => $service_prices_array,
];
$pdf = SnappyPDF::loadView('patients::point_of_sale.print_pos_pdf', $data)
->setOrientation('portrait')
->setPaper('a4')
->setOption('margin-bottom', 5)
->setOption('margin-top', 5)
->setOption('footer-html', '<i>&copy; ' . date('Y') . ' Stre@mline</i>');
return $pdf->inline('Patient Receipt' . date(" d-m-y h:ia") . '.pdf');
} else {
return redirect('/point_of_sale');
}
}
}
@@ -0,0 +1,309 @@
<?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'));
}
}
File diff suppressed because it is too large Load Diff