Files
streamline-emr/docker/streamline-src/Modules/Theatre/Http/Controllers/TheatreAnaestheticsController.php
T

881 lines
46 KiB
PHP
Executable File

<?php
namespace Modules\Theatre\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Streamline\Models\Alert;
use Streamline\Models\Allergy;
use Streamline\Models\Diagnosis;
use Streamline\Models\Patient;
use Streamline\Models\Surgery;
use Streamline\Models\User;
use Streamline\Models\AnaesthesiaAirway;
use Streamline\Models\Analgesic;
use Streamline\Models\Anaesthesia;
use Streamline\Models\InpatientInfo;
use Streamline\Models\OrderedProcedure;
use Illuminate\Support\Facades\DB;
use Barryvdh\Snappy\Facades\SnappyPdf;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\OrderedSundry;
use Streamline\Models\Treatment;
use Streamline\Models\VolatileLiquidAnaesthetics;
use Streamline\Models\WardExtra;
use Streamline\Models\WardSundryDispensation;
use Streamline\Models\WardTreatmentDispensation;
use Streamline\Services\WardManagement\WardTreatmentDispensationService;
class TheatreAnaestheticsController extends Controller {
public function __construct(protected WardTreatmentDispensationService $dispensationService)
{
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
//
}
/**
* 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);
//check if surgery data is available for this current episode
$surgery_done = Surgery::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->orderBy('id', 'desc')->first();
$documents = DB::table('patient_documents')->whereNull('deleted_at')->where('patient_id', $patient_id)->orderBy('date_taken', 'desc')->take(2)->get();
$categories = DB::table('patient_categories')->where('available', 1)->whereNull('deleted_at')->pluck("name", "id");
$drug_categories = DB::table('drug_categories')->whereNull('deleted_at')->orderBy('name', 'asc')->get();
//allergies and alerts
$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')->whereNull('deleted_at')->pluck('name', 'id');
//dropdown options used by the form
$iv_fluids = DB::table('iv_fluids')->whereNull('deleted_at')->orderBy("name", "asc")->pluck("name", "id")->prepend('- select -', '')->toArray();
$iv_fluids[0] = 'Nil';
$procedures = DB::table('procedures')->where('available', 1)->whereNull('deleted_at')->orderBy("name", "asc")->pluck("name", "id")->prepend('- select -', '');
$users = DB::table('users')->whereNull('deleted_at')->orderBy("first_name", "asc")->select("id")->get()->toArray();
$doctors = [];
foreach ($users as $value) {
$user = User::find($value->id);
if (!is_null($user)) {
if ($user->hasRole('Doctors') || $user->hasRole('Doctor')) {
$doctors[$value->id] = get_full_name($value->id, 'id', 'first_name', 'last_name', 'users');
}
}
}
// add --select--
$doctors = ['' => '- select -'] + $doctors;
$nurses = [];
foreach ($users as $value) {
$user = User::find($value->id);
if (!is_null($user)) {
if ($user->hasRole('Enrolled Nurse') || $user->hasRole('Nursing') || $user->hasRole('Nursing - Senior Ward Staff') || $user->hasRole('Specialist Nurses')) {
$nurses[$value->id] = get_full_name($value->id, 'id', 'first_name', 'last_name', 'users');
}
}
}
// add --select--
$nurses = ['' => '- select -'] + $nurses;
$anaesthesia_types = DB::table('procedures')->whereNull('deleted_at')->where('category_id', 3)->orderBy("name", "asc")->pluck("name", "id")->prepend('- select -', '');
$anaesthesia_inductions = DB::table('anaesthesia_inductions')->whereNull('deleted_at')->orderBy("name", "asc")->pluck("name", "id");
$anti_biotics = DB::table('drugs')->whereNull('deleted_at')->whereIn('category_id', [6, 9, 10, 11, 15, 29, 41, 94])->orderBy("name", "asc")->pluck("name", "id")->prepend('- select -', '')->toArray();
$anti_biotics[0] = 'Nil';
$airways = DB::table('anaesthesia_airways')->whereNull('deleted_at')->orderBy("name", "asc")->pluck("name", "id")->prepend('- select -', '');
$anaesthesia_etts = DB::table('anaesthesia_etts')->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '');
$muscle_relaxants = DB::table('muscle_relaxants')->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '')->toArray();
$muscle_relaxants[0] = 'Nil';
$analgesics = DB::table('analgesics')->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '')->toArray();
$analgesics[0] = 'Nil';
$anaesthetic_agents = DB::table('anaesthetic_agents')->whereNull('deleted_at')->pluck("name", "id");
$anaesthetic_techniques = DB::table('anaesthetic_techniques')->whereNull('deleted_at')->pluck("name", "id");
$needle_types = DB::table('needle_types')->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '');
$drugs = DB::table('drugs')->where('available', 1)->whereNull('deleted_at')->orderBy('name')->pluck('name', 'id')->prepend('- select -', '');
$units = DB::table('drug_units')->whereNull('deleted_at')->orderBy('name')->pluck('name', 'id')->prepend('- select -', '');
//inpatient_row = {} no idea where it is gotten from the old streamline
//$iv_fluid_ids = unserialize($inpatient_row["iv_fluids"]);
$iv_fluid_ids = []; //use the unserialized value after figuring where the old streamline gets them
//check if there exists an anaesthesia record of this episode already
$anaesthesia_records = Anaesthesia::where(['episode_id' => $episode_id])->get();
$sundries_array = DB::table('sundries')->whereNull('deleted_at')->orderBy("name","asc")->pluck("name", "id")->prepend('- select -', '');
$diagnoses = Diagnosis::where('available', 1)->orderby('name', 'asc')->pluck("name", "id")->prepend('- select -', '');
$diagnoses_all = Diagnosis::select('id', 'prompts', 'reference_areas', 'reference_names')->get();
$hmis_categories = DB::table('hmis_categories')->whereNull('deleted_at')->orderBy('title', 'asc')->pluck('title', 'id')->toArray();
$hmis_categories = ['' => '- select -'] + $hmis_categories;
$volatile_liquid_anaethetics = VolatileLiquidAnaesthetics::orderBy('name', 'asc')->pluck("name", "id");
return view('theatre::anaesthetics.create', compact('episode_id', 'patient_id', 'patient', 'documents', 'users', 'drugs', 'units', 'drug_categories', 'known_patient_alerts', 'known_patient_allergies', 'drug_categories_array', 'categories', 'procedures', 'anaesthesia_types', 'hmis_categories', 'anaesthesia_inductions', 'anti_biotics', 'airways', 'anaesthesia_etts', 'muscle_relaxants', 'analgesics', 'anaesthetic_agents', 'anaesthetic_techniques', 'needle_types', 'iv_fluids', 'iv_fluid_ids', 'surgery_done', 'anaesthesia_records', 'nurses', 'doctors', 'sundries_array', 'diagnoses', 'diagnoses_all', 'volatile_liquid_anaethetics'));
}
/**
* Store a newly created resource in storage.
*
*/
public function store(Request $request) : RedirectResponse
{
$patient_id = session()->get('patient_id');
$episode_id = session()->get('episode_id');
$theatre_ward_id = get_name("theatre", "slug", "id", "wards");
if (!is_integer($theatre_ward_id)) {
flash('Theatre ward not registered')->error();
return redirect()->back()->withInput();
}
$procedure = $request->procedure_id;
$anaesthetist = $request->anaesthetist;
$time_commenced = $request->anaesthetic_commence_time;
$checklist_confirmed_by = $request->checklist_confirmed_by;
$anaesthesia_type = $request->anaesthesia_type;
$induction = $request->induction ? implode(",", $request->induction) : null;
$anaesthesia_checklist = array(
"oxygen" => isset($request->oxygen) ? 1 : 0,
"diazepam" => isset($request->diazepam) ? 1 : 0,
"atropine" => isset($request->atropine) ? 1 : 0,
"ergometrine" => isset($request->ergometrine) ? 1 : 0,
"oxytocin" => isset($request->oxytocin) ? 1 : 0,
"misoprostol" => isset($request->misoprostol) ? 1 : 0,
"paracetamol" => isset($request->paracetamol) ? 1 : 0,
"hydralazine" => isset($request->hydralazine) ? 1 : 0,
"ephedrine" => isset($request->ephedrine) ? 1 : 0,
"adrenaline" => isset($request->adrenaline) ? 1 : 0,
"tranexamic_acid" => isset($request->tranexamic_acid) ? 1 : 0
);
$anaesthesia_checklist = serialize($anaesthesia_checklist);
$antibiotics_array = $request->antibiotics;
$antibiotics = implode(",", $antibiotics_array);
$airway = $request->airway;
$ett = $request->ett;
$agent_used = $request->agent_used ? implode(",", $request->agent_used) : null;
$technique = $request->technique ? implode(",", $request->technique) : null;
$monitoring_signs = array(
"continuos_sao2" => isset($request->continuos_sao2) ? 1 : 0,
"intermittent_sao2" => isset($request->intermittent_sao2) ? 1 : 0,
"full_vital_monitor" => isset($request->full_vital_monitor) ? 1 : 0,
"end_tidal_co2" => isset($request->end_tidal_co2) ? 1 : 0
);
$monitoring_signs = serialize($monitoring_signs);
$needle_type = $request->needle_type;
$volume_administered = $request->volume_administered;
$muscle_relaxant = $request->muscle_relaxant;
$analgesic = implode(",", $request->analgesic);
$volatile_liquid_anaesthetic_used = $request->volatile_liquid_anaesthetic_used;
$iv_fluids = array(
"iv_fluids" => isset($request->iv_fluid_id) ? array_filter($request->iv_fluid_id) : NULL,
"units" => isset($request->iv_fluid_units) ? array_filter($request->iv_fluid_units) : NULL
);
$iv_fluids = serialize($iv_fluids);
$blood_given = $request->blood_given;
$comments = $request->comments;
$surgery_complete = $request->surgery_complete;
$surgery_id = $request->surgery_id;
$inpatient_info = InpatientInfo::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->orderBy('created_at', 'desc')->first();
// Prepare the other drugs given and cost for storing
$drug_ids = $request->drugs;
if (count($drug_ids) > 0 && $drug_ids[0] != null): // Make sure some drugs have been selected
$doses = $request->dose;
$units = $request->units;
$drugs_given = [];
$drugs_cost = [];
$drug_quantity_used_array = $drug_frequency_array = $drug_purchased_elsewhere = $drug_eyey_array = $drug_order_comments_array = $drug_instructions_array = $drug_duration_array = [];
foreach ($drug_ids as $key => $value):
$drugs_given[] = [
'id' => $value,
'dose' => $doses[$key],
'unit' => $units[$key]
];
// Gettings the cost
// @TODO - Check if a patient is insured or not
$drug = DB::table('drugs')->whereNull('deleted_at')->select('non_insured_price', 'strength', 'pack')->find($value);
/*
* Formula for cost
* (Dose/DrugStrength)/DrugPack * UnitPrice
*/
if ($drug) {
$drugs_cost[] = (($doses[$key] / $drug->strength) / $drug->pack) * $drug->non_insured_price;
$drug_quantity_used = ($doses[$key] / $drug->strength) / $drug->pack;
reduce_ward_stock_with_consumed_quantity($value, $drug_quantity_used, 1, $theatre_ward_id, $patient_id, $episode_id, null);
$drug_quantity_used_array[] = $drug_quantity_used;
$drug_frequency_array[] = "";
$drug_purchased_elsewhere[] = "";
$drug_eyey_array[] = "";
$drug_order_comments_array[] = "";
$drug_instructions_array[] = "";
$drug_duration_array[] = "";
}
endforeach;
$drugs_given_serial = serialize($drugs_given);
$drugs_cost_serial = serialize($drugs_cost);
if ($inpatient_info){
// make treatment record for billing
for ($i=0; $i < count($drug_ids) ; $i++) {
if (isset($drug_quantity_used_array[$i]) && isset( $drug_ids[$i])) {
$ward_treatment_id = $this->dispensationService->saveDispensation((int)$patient_id, (int)$episode_id, $drug_ids[$i], (int)$drug_quantity_used_array[$i],
0, $request->ward_id ?? 0);
}
}
} else{
// bill drugs as opd treatment
$treatment = new Treatment;
$treatment->patient_id = $patient_id;
$treatment->episode_id = $episode_id;
$treatment->drugs = implode(',', $drug_ids);
$treatment->doses = implode(',', $doses);
$treatment->frequencies = implode(',', $drug_frequency_array);
$treatment->durations = implode(',', $drug_duration_array);
$treatment->quantities_dispensed = implode(',', $drug_quantity_used_array);
$treatment->instruction = implode(',', $drug_instructions_array);
$treatment->purchased_elsewhere = implode(',', $drug_purchased_elsewhere);
$treatment->dispense_status = 0;
$treatment->eye = implode(",", $drug_eyey_array);
$treatment->order_comments = "";
$treatment->created_by = auth()->user()->id;
$treatment->save();
//do this for sundries to be billed
$sundry_used_array = $request->sundries_used;
$sundry_quantity_array = $request->sundries_quantity_used;
for ($i=0; $i < count($sundry_used_array); $i++) {
if (!is_null($sundry_used_array[$i])) {
$ordered_sundry_objects = OrderedSundry::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->where('sundries_id',$sundry_used_array[$i])->get();
if (count($ordered_sundry_objects) > 0) {
foreach ($ordered_sundry_objects as $sundry_to_update) {
$sundry_to_update->quantity = $sundry_quantity_array[$i];
$sundry_to_update->payment_status = 0;
$sundry_to_update->update();
}
} else {
$ordered_sundry = new OrderedSundry;
$ordered_sundry->patient_id = $patient_id;
$ordered_sundry->episode_id = $episode_id;
$ordered_sundry->sundries_id = $sundry_used_array[$i];
$ordered_sundry->quantity = $sundry_quantity_array[$i];
$ordered_sundry->payment_status = 0;
$ordered_sundry->created_by = Auth::id();
$ordered_sundry->save();
}
}
}
}
else:
$drugs_given_serial = null;
$drugs_cost_serial = null;
endif;
if ($inpatient_info){
//do this for sundries orders billing
$ward_sundries_ids_array = $request->sundries_used;
$ward_sundries_quantities_array = $request->sundries_quantity_used;
if (is_array($ward_sundries_ids_array)) {
for ($j = 0; $j < count($ward_sundries_ids_array); $j++) {
// only save if the dispensed drug quantity is not null
if (!is_null($ward_sundries_quantities_array[$j])) {
$ward_sundry_dispensations = new WardSundryDispensation;
$ward_sundry_dispensations->patient_id = $patient_id;
$ward_sundry_dispensations->episode_id = $episode_id;
$ward_sundry_dispensations->sundry_id = $ward_sundries_ids_array[$j];
$ward_sundry_dispensations->quantity_given = $ward_sundries_quantities_array[$j];
$ward_sundry_dispensations->ward_id = $inpatient_info->ward_id;
$ward_sundry_dispensations->created_by = Auth::id();
$ward_sundry_dispensations->save();
}
}
}
$extra_items_array = $request->extras_used;
$extra_costs_array = $request->extras_cost;
$ward_extras_income_account = ChartOfAccount::where('slug', 'ward_extras_income_account')->first();
if (is_array($extra_items_array)) {
for ($k = 0; $k < count($extra_items_array); $k++) {
// only save if the dispensed drug quantity is not null
if (!is_null($extra_items_array[$k])) {
$ward_extra = new WardExtra;
$ward_extra->patient_id = $patient_id;
$ward_extra->episode_id = $episode_id;
$ward_extra->extra_item = $extra_items_array[$k];
$ward_extra->extra_cost = $extra_costs_array[$k];
$ward_extra->current_chart_of_account = !is_null($ward_extras_income_account) ? $ward_extras_income_account->id : 0;
$ward_extra->ward_id = $inpatient_info->ward_id;
$ward_extra->created_by = Auth::id();
$ward_extra->save();
}
}
}
}
$anaesthesia_record = New Anaesthesia;
//record into anaesthesia table
$anaesthesia_record->episode_id = $episode_id;
$anaesthesia_record->patient_id = $patient_id;
$anaesthesia_record->procedure_id = $procedure;
$anaesthesia_record->surgery_id = ($surgery_complete) ? $surgery_id : NULL;
$anaesthesia_record->anaesthetist = $anaesthetist;
$anaesthesia_record->time_commenced = $time_commenced;
$anaesthesia_record->primary_diagnosis = $request->primary_diagnosis;
$anaesthesia_record->other_diagnoses = serialize($request->other_diagnosis);
$anaesthesia_record->checklist_confirmed_by = $checklist_confirmed_by;
$anaesthesia_record->anaesthesia_type = $anaesthesia_type;
$anaesthesia_record->induction = $induction;
$anaesthesia_record->anaesthesia_checklist = $anaesthesia_checklist;
$anaesthesia_record->antibiotics = $antibiotics;
$anaesthesia_record->airway = $airway;
$anaesthesia_record->ETT = $ett;
$anaesthesia_record->agent_used = $agent_used;
$anaesthesia_record->technique = $technique;
$anaesthesia_record->monitoring_signs = $monitoring_signs;
$anaesthesia_record->needle_type = $needle_type;
$anaesthesia_record->volume_administered = $volume_administered;
$anaesthesia_record->muscle_relaxant = $muscle_relaxant;
$anaesthesia_record->analgesic = $analgesic;
$anaesthesia_record->volatile_liquid_anaethetics = $request->volatile_liquid_anaethetics;
$anaesthesia_record->volatile_liquid_anaesthetic_used = $volatile_liquid_anaesthetic_used;
$anaesthesia_record->iv_fluids = $iv_fluids;
$anaesthesia_record->blood_given = $blood_given;
$anaesthesia_record->comments = $comments;
$anaesthesia_record->other_drugs_given = $drugs_given_serial;
$anaesthesia_record->other_drugs_cost = $drugs_cost_serial;
$anaesthesia_record->mallampati_score = $request->mallampati_score;
$anaesthesia_record->asa_classification = $request->asa_classification;
$extras_array = $request->extras_used;
$extras_cost_array = $request->extras_cost;
$unserialized_extras_array = array(
"extras" => $extras_array,
"cost" => $extras_cost_array
);
$anaesthesia_record->extras_used = serialize($unserialized_extras_array);
$sundry_used_array = $request->sundries_used;
$sundry_quantity_array = $request->sundries_quantity_used;
$unserialized_sundries_array = array(
"sundries" => $sundry_used_array,
"quantity" => $sundry_quantity_array
);
for ($i=0; $i < count($sundry_used_array) ; $i++) {
reduce_ward_stock_with_consumed_quantity($sundry_used_array[$i], $sundry_quantity_array[$i], 2, $theatre_ward_id, $patient_id, $episode_id, null);
}
$anaesthesia_record->sundries_used = serialize($unserialized_sundries_array);
$anaesthesia_record->created_by = Auth::id();
$save_anaesthesia_record = $anaesthesia_record->save();
if ($save_anaesthesia_record) {
// save the procedure info in inpatient or OPD if surgery is not complete
/* i have commented out the if stmt coz i dt remember why we needed to check if surgery is done or not before saving the procedure in the rescpective table */
//if (!$surgery_complete){
$inpatient_info = InpatientInfo::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->orderBy('created_at', 'desc')->first();
if ($inpatient_info) {
//check_if_there_are_exisiting_procedures and add the new ones instead of over riding them
if (!is_null($inpatient_info->procedures) && $inpatient_info->procedures != '') {
$unserialized_procedures_array = unserialize($inpatient_info->procedures);
$unserialized_procedures_array[] = $anaesthesia_type;
$inpatient_info->procedures = serialize($unserialized_procedures_array);
} else {
$inpatient_info->procedures = serialize([$anaesthesia_type]);
}
// procedure_done_by
if(!is_null($inpatient_info->procedures_done_by) && $inpatient_info->procedures_done_by != ''){
$procedures_done_by_array = @unserialize($inpatient_info->procedures_done_by);
$procedures_done_by_array[] = $request->anaesthetist;
$inpatient_info->procedures_done_by = serialize($procedures_done_by_array);
} else {
$inpatient_info->procedures_done_by = serialize([$request->anaesthetist]);
}
// procedure_hospital_fee
if(!is_null($inpatient_info->procedure_hospital_fee) && $inpatient_info->procedure_hospital_fee != ''){
$procedures_hospital_fee_array = @unserialize($inpatient_info->procedure_hospital_fee);
$procedures_hospital_fee_array[] = get_name($anaesthesia_type, "id", "non_insured_price", "procedures");
$inpatient_info->procedure_hospital_fee = serialize($procedures_hospital_fee_array);
} else {
$inpatient_info->procedure_hospital_fee = serialize([0]);
}
// procedure_staff_fee
if(!is_null($inpatient_info->procedure_staff_fee) && $inpatient_info->procedure_staff_fee != ''){
$procedures_staff_fee_array = @unserialize($inpatient_info->procedure_staff_fee);
$procedures_staff_fee_array[] = 0;
$inpatient_info->procedure_staff_fee = serialize($procedures_staff_fee_array);
} else {
$inpatient_info->procedure_staff_fee = serialize([0]);
}
$inpatient_info->updated_by = Auth::id();
$inpatient_info->save();
} else {
$procedure_performed_id = record_staff_that_has_performed_the_service($patient_id, $episode_id, 1, $anaesthesia_type, 0, $request->anaesthetist);
$ordered_procedure = new OrderedProcedure;
$ordered_procedure->patient_id = $patient_id;
$ordered_procedure->episode_id = $episode_id;
$ordered_procedure->procedure_id = $anaesthesia_type;
$ordered_procedure->payment_status = 0;
$ordered_procedure->performed = 1;
$ordered_procedure->performed_id = $procedure_performed_id;
$ordered_procedure->created_by = Auth::id();
$ordered_procedure->save();
}
//}
// if surgery was completed, update the anaesthesia_id
if ($surgery_complete) {
// update anaesthesia table with surgery_id
$update = DB::table('surgeries')->where('id', $surgery_id)->update([
"anaesthesia_id" => $anaesthesia_record->id
]);
}
flash('Anaesthesia has been saved')->success();
return redirect('anaesthetics/history');
} else {
flash('There was an error')->error();
return redirect()->back()->withInput();
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
$anaesthesia_record = Anaesthesia::find($id);
$patient_id = $anaesthesia_record->patient_id;
$episode_id = $anaesthesia_record->episode_id;
$patient = Patient::find($patient_id);
$surgery_done = Surgery::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->orderBy('id', 'desc')->first();
$documents = DB::table('patient_documents')->whereNull('deleted_at')->where('patient_id', $patient_id)->orderBy('date_taken', 'desc')->take(2)->get();
$categories = DB::table('patient_categories')->where('available', 1)->whereNull('deleted_at')->pluck("name", "id");
$drug_categories = DB::table('drug_categories')->whereNull('deleted_at')->orderBy('name', 'asc')->get();
//allergies and alerts
$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')->whereNull('deleted_at')->pluck('name', 'id');
//$inpatient_info or $ordered_procedure
$ordered_procedure = OrderedProcedure::where(['episode_id' => $episode_id])->get();
$inpatient_info = InpatientInfo::where(['episode_id' => $episode_id])->get();
return view('theatre::anaesthetics.show', compact('episode_id', 'patient_id', 'patient', 'documents', 'drug_categories', 'known_patient_alerts', 'known_patient_allergies', 'drug_categories_array', 'categories', 'anaesthesia_record', 'ordered_procedure', 'inpatient_info'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
$anaesthesia_record = Anaesthesia::find($id);
$patient_id = $anaesthesia_record->patient_id;
$episode_id = $anaesthesia_record->episode_id;
$patient = Patient::find($patient_id);
//check if surgery data is available for this current episode
$surgery_done = Surgery::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->orderBy('id', 'desc')->first();
$documents = DB::table('patient_documents')->whereNull('deleted_at')->where('patient_id',$patient_id)->orderBy('date_taken','desc')->take(2)->get();
$categories = DB::table('patient_categories')->where('available', 1)->whereNull('deleted_at')->pluck("name", "id");
$drug_categories = DB::table('drug_categories')->whereNull('deleted_at')->orderBy('name','asc')->get();
//allergies and alerts
$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')->whereNull('deleted_at')->pluck('name', 'id');
//dropdown options used by the form
$iv_fluids = DB::table('iv_fluids')->whereNull('deleted_at')->orderBy("name","asc")->pluck("name", "id")->prepend('- select -', '');
$procedures = DB::table('procedures')->whereNull('deleted_at')->orderBy("name","asc")->pluck("name", "id")->prepend('- select -', '');
$users = DB::table('users')->orderBy("first_name","asc")->whereNull('deleted_at')->select("id")->get()->toArray();
$doctors = [];
$all_staff = [];
foreach ($users as $value){
$user = User::find($value->id);
if(!is_null($user)){
if ($user->hasRole('doctors') || $user->hasRole('doctor')) {
$doctors[$value->id] = get_full_name($value->id, 'id', 'first_name', 'last_name', 'users');
}
$all_staff[$value->id] = get_full_name($value->id, 'id', 'first_name', 'last_name', 'users');
}
}
// add --select--
$doctors = ['' => '- select -'] + $doctors;
$all_staff = ['' => '- select -'] + $all_staff;
$nurses = [];
foreach ($users as $value){
$user = User::find($value->id);
if(!is_null($user)){
if ($user->hasRole('Enrolled Nurse') || $user->hasRole('Nursing') || $user->hasRole('Nursing - Senior Ward Staff') || $user->hasRole('Specialist Nurses')) {
$nurses[$value->id] = get_full_name($value->id, 'id', 'first_name', 'last_name', 'users');
}
}
}
// add --select--
$nurses = ['' => '- select -'] + $nurses;
$anaesthesia_types = DB::table('procedures')->whereNull('deleted_at')->where('category_id',3)->whereNotNull('slug')->orderBy("name","asc")->pluck("name", "id")->prepend('- select -', '');
$anaesthesia_inductions = DB::table('anaesthesia_inductions')->whereNull('deleted_at')->orderBy("name","asc")->pluck("name", "id")->prepend('- select -', '');
$anti_biotics = DB::table('drugs')->whereNull('deleted_at')->whereIn('category_id', [6, 9, 10, 11, 15, 29, 41, 94])->orderBy("name", "asc")->pluck("name", "id")->prepend('- select -', '')->toArray();
$anti_biotics[0] = 'Nil';
$airways = DB::table('anaesthesia_airways')->whereNull('deleted_at')->orderBy("name","asc")->pluck("name", "id")->prepend('- select -', '');
$anaesthesia_etts = DB::table('anaesthesia_etts')->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '');
$muscle_relaxants = DB::table('muscle_relaxants')->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '');
$analgesics = DB::table('analgesics')->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '');
$anaesthetic_agents = DB::table('anaesthetic_agents')->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '');
$anaesthetic_techniques = DB::table('anaesthetic_techniques')->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '');
$needle_types = DB::table('needle_types')->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', '');
//inpatient_row = {} no idea where it is gotten from the old streamline
//$iv_fluid_ids = unserialize($inpatient_row["iv_fluids"]);
$iv_fluid_ids = [];
$users = User::orderBy('first_name')->select("id", "first_name", "last_name")->get();
$sundries_array = DB::table('sundries')->whereNull('deleted_at')->orderBy("name","asc")->pluck("name", "id")->prepend('- select -', '');
$drugs = DB::table('drugs')->where('available', 1)->whereNull('deleted_at')->orderBy('name')->pluck('name', 'id')->prepend('- select -', '');
$units = DB::table('drug_units')->whereNull('deleted_at')->orderBy('name')->pluck('name', 'id')->prepend('- select -', '');
$diagnoses = Diagnosis::where('available', 1)->orderBy('name', 'asc')->pluck("name", "id")->prepend('- select -', '');
$diagnoses_all = Diagnosis::select('id', 'prompts', 'reference_areas', 'reference_names')->get();
$volatile_liquid_anaethetics = VolatileLiquidAnaesthetics::orderBy('name', 'asc')->pluck("name", "id");
return view('theatre::anaesthetics.edit',compact('anaesthesia_record','episode_id','patient_id','patient','documents','users','drug_categories','known_patient_alerts','known_patient_allergies','drug_categories_array','categories','procedures','anaesthesia_types','anaesthesia_inductions','anti_biotics','airways','anaesthesia_etts','muscle_relaxants','analgesics','anaesthetic_agents','anaesthetic_techniques','needle_types','iv_fluids','iv_fluid_ids','surgery_done', 'nurses','doctors','all_staff','sundries_array','drugs','units', 'diagnoses', 'diagnoses_all', 'volatile_liquid_anaethetics'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
$anaesthesia_record = Anaesthesia::find($id);
$procedure = $request->procedure_id;
$anaesthetist = $request->anaesthetist;
$time_commenced = $request->anaesthetic_commence_time; //date("H:i", strtotime($row['time_d'])); anaesthetic_commence_time
$checklist_confirmed_by = $request->checklist_confirmed_by;
$anaesthesia_type = $request->anaesthesia_type;
$induction = $request->induction ? implode($request->induction) : null;
$anaesthesia_checklist = array(
"oxygen" => isset($request->oxygen) ? 1 : 0,
"diazepam" => isset($request->diazepam) ? 1 : 0,
"atropine" => isset($request->atropine) ? 1 : 0,
"ergometrine" => isset($request->ergometrine) ? 1 : 0,
"oxytocin" => isset($request->oxytocin) ? 1 : 0,
"misoprostol" => isset($request->misoprostol) ? 1 : 0,
"paracetamol" => isset($request->paracetamol) ? 1 : 0,
"hydralazine" => isset($request->hydralazine) ? 1 : 0,
"ephedrine" => isset($request->ephedrine) ? 1 : 0,
"adrenaline" => isset($request->adrenaline) ? 1 : 0,
"tranexamic_acid" => isset($request->tranexamic_acid) ? 1 : 0
);
$anaesthesia_checklist = serialize($anaesthesia_checklist);
$antibiotics_array = $request->antibiotics;
$antibiotics = implode(",", $antibiotics_array);
$airway = $request->airway;
$ett = $request->ett;
$agent_used = is_array($request->agent_used) ? implode(",", $request->agent_used) : null;
$technique = is_array($request->technique) ? implode(",", $request->technique) : null;
$monitoring_signs = array(
"continuos_sao2" => isset($request->continuos_sao2) ? 1 : 0,
"intermittent_sao2" => isset($request->intermittent_sao2) ? 1 : 0,
"full_vital_monitor" => isset($request->full_vital_monitor) ? 1 : 0,
"end_tidal_co2" => isset($request->end_tidal_co2) ? 1 : 0
);
$monitoring_signs = serialize($monitoring_signs);
$needle_type = $request->needle_type;
$volume_administered = $request->volume_administered;
$muscle_relaxant = $request->muscle_relaxant;
$analgesic = is_array($request->analgesic) ? implode(",", $request->analgesic) : null;
$volatile_liquid_anaesthetic_used = $request->volatile_liquid_anaesthetic_used;
$iv_fluids = array(
"iv_fluids" => isset($request->iv_fluid_id) ? array_filter($request->iv_fluid_id) : NULL,
"units" => isset($request->iv_fluid_units) ? array_filter($request->iv_fluid_units) : NULL
);
$iv_fluids = serialize($iv_fluids);
$blood_given = $request->blood_given;
$comments = $request->comments;
$surgery_complete = $request->surgery_complete;
$surgery_id = $request->surgery_id;
// Prepare the other drugs given and cost for storing
$drug_ids = $request->drugs;
if (count($drug_ids) > 0 && $drug_ids[0] != null): // Make sure some drugs have been selected
$doses = $request->dose;
$units = $request->units;
$drugs_given = [];
$drugs_cost = [];
foreach ($drug_ids as $key => $value):
$drugs_given[] = [
'id' => $value,
'dose' => $doses[$key],
'unit' => $units[$key]
];
// Gettings the cost
// @TODO - Check if a patient is insured or not
$drug = DB::table('drugs')->select('non_insured_price', 'strength', 'pack')->find($value);
/*
* Formula for cost
* (Dose/DrugStrength)/DrugPack * UnitPrice
*/
$drugs_cost[] = (($doses[$key] / $drug->strength) / $drug->pack) * $drug->non_insured_price;
endforeach;
$drugs_given_serial = serialize($drugs_given);
$drugs_cost_serial = serialize($drugs_cost);
else:
$drugs_given_serial = null;
$drugs_cost_serial = null;
endif;
//record into anaesthesia table
$anaesthesia_record->procedure_id = $procedure;
$anaesthesia_record->surgery_id = ($surgery_complete) ? $surgery_id : NULL;
$anaesthesia_record->anaesthetist = $anaesthetist;
$anaesthesia_record->time_commenced = $time_commenced;
$anaesthesia_record->checklist_confirmed_by = $checklist_confirmed_by;
$anaesthesia_record->anaesthesia_type = $anaesthesia_type;
$anaesthesia_record->induction = $induction;
$anaesthesia_record->anaesthesia_checklist = $anaesthesia_checklist;
$anaesthesia_record->antibiotics = $antibiotics;
$anaesthesia_record->airway = $airway;
$anaesthesia_record->ETT = $ett;
$anaesthesia_record->agent_used = $agent_used;
$anaesthesia_record->technique = $technique;
$anaesthesia_record->monitoring_signs = $monitoring_signs;
$anaesthesia_record->needle_type = $needle_type;
$anaesthesia_record->volume_administered = $volume_administered;
$anaesthesia_record->muscle_relaxant = $muscle_relaxant;
$anaesthesia_record->analgesic = $analgesic;
$anaesthesia_record->volatile_liquid_anaethetics = $request->volatile_liquid_anaethetics;
$anaesthesia_record->volatile_liquid_anaesthetic_used = $volatile_liquid_anaesthetic_used;
$anaesthesia_record->iv_fluids = $iv_fluids;
$anaesthesia_record->blood_given = $blood_given;
$anaesthesia_record->comments = $comments;
$anaesthesia_record->other_drugs_given = $drugs_given_serial;
$anaesthesia_record->other_drugs_cost = $drugs_cost_serial;
$anaesthesia_record->mallampati_score = $request->mallampati_score;
$anaesthesia_record->asa_classification = $request->asa_classification;
$extras_array = $request->extras_used;
$extras_cost_array = $request->extras_cost;
$unserialized_extras_array = array(
"extras" => $extras_array,
"cost" => $extras_cost_array
);
$anaesthesia_record->extras_used = serialize($unserialized_extras_array);
$sundry_used_array = $request->sundries_used;
$sundry_quantity_array = $request->sundries_quantity_used;
$unserialized_sundries_array = array(
"sundries" => $sundry_used_array,
"quantity" => $sundry_quantity_array
);
$anaesthesia_record->sundries_used = serialize($unserialized_sundries_array);
$anaesthesia_record->created_by = Auth::id();
$save_anaesthesia_record = $anaesthesia_record->update();
if ($save_anaesthesia_record) {
/* check if this episode exists in the inpatient_info then add/update anaesthesia_type into procedures column,else add anaesthesia_type into ordered_procedures */
flash('Anaesthesia has been updated')->success();
return redirect('anaesthetics/history');
} else {
flash('There was an error')->error();
return redirect()->back()->withInput();
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
//
}
/* get procedure slug */
public function get_procedure_slug(Request $request) {
$procedure_id = $request->procedure_id;
$slug = get_name($procedure_id, 'id', 'slug', 'procedures');
return $slug;
}
/* add a new doctor to the users table */
public function add_new_doctor_or_nurse(Request $request) {
$user = new User;
$user->first_name = $request->fname;
$user->last_name = $request->lname;
$user->username = $request->fname . $request->lname;
//$set_url = str_replace(url('/'), '', url()->current());
//if($set_url == '/add_new_doctor') add user category of doctor
//if($set_url == '/add_new_nurse') add user category of nurse
if ($user->save()) {
$last_inserted_id = $user->id;
return $last_inserted_id;
}
return 'error occurred';
}
/* add new airway */
public function add_new_airway(Request $request) {
$airway = new AnaesthesiaAirway;
$airway->name = $request->name;
$airway->created_by = Auth::id();
if ($airway->save()) {
$last_inserted_id = $airway->id;
return $last_inserted_id;
}
return 'error occurred';
}
/* add new analgesic */
public function add_new_analgesic(Request $request) {
$analgesic = new Analgesic;
$analgesic->name = $request->name;
$analgesic->created_by = Auth::id();
if ($analgesic->save()) {
$last_inserted_id = $analgesic->id;
return $last_inserted_id;
}
return 'error occurred';
}
public function history() {
$patient_id = session()->get('patient_id');
$episode_id = session()->get('episode_id');
$patient = Patient::find($patient_id);
$documents = DB::table('patient_documents')->whereNull('deleted_at')->where('patient_id', $patient_id)->orderBy('date_taken', 'desc')->take(2)->get();
$categories = DB::table('patient_categories')->where('available', 1)->whereNull('deleted_at')->pluck("name", "id");
$drug_categories = DB::table('drug_categories')->whereNull('deleted_at')->orderBy('name', 'asc')->get();
//allergies and alerts
$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')->whereNull('deleted_at')->pluck('name', 'id');
$users = DB::table('users')->whereNull('deleted_at')->orderBy("first_name", "asc")->pluck("first_name", "id")->prepend('- select -', '');
$anaesthesia_records = Anaesthesia::where(['episode_id' => $episode_id])->get();
return view('theatre::anaesthetics.history', compact('episode_id', 'patient_id', 'patient', 'documents', 'users', 'drug_categories', 'known_patient_alerts', 'known_patient_allergies', 'drug_categories_array', 'categories', 'anaesthesia_records'));
}
public function view_history($id) {
$anaesthesia = Anaesthesia::find($id);
$patient_id = $anaesthesia->patient_id;
$episode_id = $anaesthesia->episode_id;
$patient = Patient::find($patient_id);
$diagnoses = DB::table('diagnoses')->where('available', 1)->whereNull('deleted_at')->orderBy('name')->pluck("name", "id")->prepend('- select -', '');
return view('theatre::anaesthetics.view_history', compact('patient_id', 'episode_id', 'patient', 'anaesthesia', 'diagnoses', 'id'));
}
public function anaesthesia_print($anaesthesia_id)
{
$anaesthesia = Anaesthesia::withTrashed()->find($anaesthesia_id);
if (!$anaesthesia) {
return redirect()->back();
}
$patient_id = $anaesthesia->patient_id;
$episode_id = $anaesthesia->episode_id;
$patient = Patient::find($patient_id);
$hospitalInfo = DB::table('hospital_information')->find(1);
$diagnoses = DB::table('diagnoses')->where('available', 1)->whereNull('deleted_at')->orderBy('name')->pluck("name", "id")->prepend('- select -', '');
$data = [
'patient' => $patient,
'patient_id' => $patient_id,
'episode_id' => $episode_id,
'hospitalInfo' => $hospitalInfo,
'diagnoses' => $diagnoses,
'anaesthesia' => $anaesthesia
];
$pdf = SnappyPDF::loadView('theatre::anaesthetics/anaesthesia_print', $data)
->setOrientation('portrait')
->setOption('margin-bottom', 7)
->setOption('margin-top', 5)
->setOption('footer-html', '<i>Stre@mline</i>');
return $pdf->inline('Anaesthesia Details' . date(" d-m-y h:ia") . '.pdf');
}
public function remove_drug_from_inpatient_bill($record_id, $drug_id) {
$anaesthesia = Anaesthesia::find($record_id);
$other_drugs_bill = explode(",", $anaesthesia->other_drugs_bill);
$other_drugs_bill[] = $drug_id;
$anaesthesia->other_drugs_bill = implode(",", $other_drugs_bill);
$anaesthesia->save();
return redirect('/anaesthetics/view_history/' . $record_id);
}
public function add_drug_to_inpatient_bill($record_id, $drug_id) {
$anaesthesia = Anaesthesia::find($record_id);
$other_drugs_bill = explode(",", $anaesthesia->other_drugs_bill);
$key = array_search($drug_id, $other_drugs_bill);
if ($key !== false) {
unset($other_drugs_bill[$key]);
}
$anaesthesia->other_drugs_bill = implode(",", $other_drugs_bill);
$anaesthesia->save();
return redirect('/anaesthetics/view_history/' . $record_id);
}
}