middleware('auth'); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $patient_id = session()->get('patient_id'); $patient_episodes = DB::table('patient_episodes') ->where(['patient_id' => $patient_id]) ->whereNull('deleted_at') ->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")->prepend('- select -', '');; $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'); //$special_clinics = DB::table('clinics')->where('slug', '!=', 'general')->pluck("name", "id")->prepend('GENERAL OPD', 'general_opd')->prepend('- select -', ''); $special_clinics = DB::table('clinics')->where('available', 1)->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', ''); $wards = DB::table('wards')->where('available', 1)->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', ''); $users_collection = DB::table('users')->orderBy("first_name","asc")->select("id")->get()->toArray(); $users_array = []; foreach ($users_collection as $value){ $user = User::find($value->id); if (!is_null($user)) { if ($user->hasRole('Doctors') || $user->hasRole('Doctor')) { $consultation_records = DB::table('services') ->join('staff_payment_configurations','staff_payment_configurations.item_id', '=', 'services.id') ->where('services.item_type', 'Consultation') ->where('staff_payment_configurations.user_id',$value->id) ->where('staff_payment_configurations.item_category',3) ->whereNull('staff_payment_configurations.deleted_at') ->orderBy('staff_payment_configurations.created_at','asc') ->select('services.*') ->get(); foreach ($consultation_records as $record) { $price_list_id = is_patient_category_attached_to_price_list($patient_id); if ($price_list_id) { $users_consultation_fee = get_price_list_category_price($price_list_id, 6, $record->id); } else{ $users_consultation_fee = $record->non_insured_price; } //concatnate the service_record_id with the users id to form the key for the array $users_array[$record->id."__".$value->id] = get_full_name($value->id, 'id', 'first_name', 'last_name', 'users')." (".$record->name." Fee: ".ugandan_shillings($users_consultation_fee).")"; } } } } $users_array = ['' => '- select -'] + $users_array; return view('patients::patient_episodes.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', 'special_clinics', 'wards', 'users_array')); } /** * Show the form for creating a new resource. * */ public function create() { // } /** * Store a newly created resource in storage. * */ public function store(Request $request) { // } /** * Display the specified resource. * */ public function show($id) { // } /** * Show the form for editing the specified resource. * */ public function edit($id) { // } public function update(Request $request, $id) { /* Update Claim Number */ $episode = PatientEpisode::find($id); $episode->claim_number = $request->claim_number; $episode->save(); if(session()->get('edit_claim_number') == 'patient_home'){ return redirect()->route('patient_episodes.index'); }else{ return redirect()->route('patient_finance.home'); } } /** * Remove the specified resource from storage. * */ public function destroy($id) { // } /** * Create new episode for a patient. * */ public function create_episode(Request $request) { $logged_in_user_id = Auth()->user()->id; $episode = new PatientEpisode; $episode->patient_id = $request->patient_id; $episode->created_by = $logged_in_user_id; $episode->updated_by = $logged_in_user_id; try { $episode->save(); flash("A new episode has been saved")->success(); // check where the function was called from and return there if (isset($request->is_from_finance)) { // return to finance home return redirect('/patient_finance/home'); } else { // return to normal patient home return redirect('/patient_episodes/'); } } catch (QueryException $e) { $errorCode = $e->errorInfo[1]; if ($errorCode == 1062) { //error code for duplicate entry to a unique field flash("This episode already exists!")->error(); return back()->withInput(); } } } /** * Setup patient session using id. * * @param int $id * @return \Illuminate\Http\Response */ public function set_patient_id($id) { session()->put('patient_id', $id); return redirect('/patient_episodes/'); } /** * Pathfinder for all patient episode routes. * * @param int $id * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function route_patient_episode(Request $request) { //set session for episode_id session()->put('episode_id', $request->episode_id); $is_patient_allowed_to_do_consultation = $this->is_patient_allowed_to_have_consultation(session()->get('patient_id'), $request->episode_id); switch ($request->submit) { case 'triage': return redirect('/triage/'); break; case 'triage_without_etat': return redirect('/triage/create_without_etat'); break; case 'investigations': return redirect('/investigations/investigations_review'); break; case 'consultation': if ($is_patient_allowed_to_do_consultation == false) { session()->put('consultation_not_paid', 'consultation_not_paid'); return redirect()->back()->withInput(); } return redirect('/consultation/route'); break; case 'consultation_with_notes': if ($is_patient_allowed_to_do_consultation == false) { session()->put('consultation_not_paid', 'consultation_not_paid'); return redirect()->back()->withInput(); } return redirect('/consultation/create_with_notes'); break; case 'procedure': return redirect('/order_procedures'); break; case 'sundries': return redirect('/order_sundries'); break; case 'create_anaesthetics': return redirect('anaesthetics/create'); break; case 'create_surgery': return redirect('theatre_surgery/create'); break; case 'anaesthetics_history': return redirect('anaesthetics/history'); break; case 'surgery_index': return redirect('theatre_surgery'); break; case 'inpatient-sheet-button': case 'maternity_summary': return redirect('in_patient_sheet'); break; case 'inpatient_billing': return redirect('inpatient_billing'); break; case 'patient_document': return redirect('patient_documents/create'); break; case 'death_report_btn': return redirect('reports/nira/death'); break; case 'eye_clinic': return redirect('/eye_clinic/'); break; case 'drug_refill': session()->forget('alter_episode_id'); session()->forget('alter_patient_id'); return redirect('drug_refill'); break; case 'services': return redirect('order_services'); break; case 'edit_claim_number': session()->put(['edit_claim_number' => 'patient_home']); return redirect('edit_claim_number'); break; case 'prescription': session()->forget('alter_episode_id'); session()->forget('alter_patient_id'); return redirect('/prescriptions/create/'); break; case 'record_all_items': return redirect('record_staff_service_performance'); break; case 'maternity_admission': return redirect('maternity_inpatient_sheet'); case 'delivery_record': return redirect('maternity_delivery_record'); case 'birth_report': return redirect('/reports/nira/birth'); case 'inpatient_attendant_pass': return redirect('inpatient_attendant_pass'); break; default: return redirect('/patient_episodes/'); } } public function edit_claim_number() { $episode_id = session()->get('episode_id'); $episode = PatientEpisode::find($episode_id); return view('patients::patient_episodes.edit_claim_number', compact('episode', 'episode_id')); } public function internal_clinic_transfer($id) { // get triage id $triage = Triage::where('episode_id', $id)->first(); $consultation = Consultation::where('episode_id', $id)->first(); $episode_details = PatientEpisode::find($id); if ($triage) { if ($consultation) { return $triage->id . "," . $triage->clinic_allocation . "," . get_name($triage->clinic_allocation, 'id', 'name', 'clinics') . ",". $consultation->consultation_done_by. ",". $triage->patient_id; } return $triage->id . "," . $triage->clinic_allocation . "," . get_name($triage->clinic_allocation, 'id', 'name', 'clinics'). ",". 0 . ",". $triage->patient_id; //no doctor was allocated so it is zero } elseif ($episode_details && !is_null($episode_details->clinic_id)) { if ($consultation) { return 0 . "," . $episode_details->clinic_id . "," . get_name($episode_details->clinic_id, 'id', 'name', 'clinics') . ",". $consultation->consultation_done_by. ",". $episode_details->patient_id; } return 0 . "," . $episode_details->clinic_id . "," . get_name($episode_details->clinic_id, 'id', 'name', 'clinics') . ",". 0 . ",". $episode_details->patient_id; } else { return 0; } } public function save_internal_clinic_transfer(Request $request) { $transfered_to_doctor = null; $transfered_from_doctor = null; $allocated_service_id = null; $patient_id = $request->patient_id; $episode_id = $request->episode_id; if ($request->transfer_from_doctor) { $old_services_id_with_user_id = $request->transfer_from_doctor; $old_services_id_with_user_id_array = explode("__", $old_services_id_with_user_id); // ["service_id", "user_id"] $old_allocated_service_id = $old_services_id_with_user_id_array[0]; $transfered_from_doctor = $old_services_id_with_user_id_array[1]; } if ($request->transfer_to_doctor) { $services_id_with_user_id = $request->transfer_to_doctor; $services_id_with_user_id_array = explode("__", $services_id_with_user_id); // ["service_id", "user_id"] $allocated_service_id = $services_id_with_user_id_array[0]; $transfered_to_doctor = $services_id_with_user_id_array[1]; } $transfer = new PatientClinicTransfers; $transfer->patient_id = $patient_id; $transfer->episode_id = $episode_id; $transfer->old_clinic = $request->old_clinic; $transfer->new_clinic = $request->new_clinic; $transfer->old_doctor_id = $transfered_from_doctor; $transfer->new_doctor_id = $transfered_to_doctor; $transfer->created_by = Auth()->user()->id; $episode = PatientEpisode::find($request->episode_id); $episode->clinic_id = $request->new_clinic; if ($request->triage_id != 0) { $triage = Triage::find($request->triage_id); $triage->clinic_allocation = $request->new_clinic; $triage->update(); } if ($request->transfer_to_doctor) { $consultation = Consultation::where(['patient_id'=> $patient_id, 'episode_id' => $episode_id])->first(); if ($consultation) { // first, check if there is a previously ordered service attached to current doctor $already_ordered_service = OrderedService::where('performed', 1) ->where('patient_id', $patient_id) ->where('episode_id', $episode_id) ->where('service_id', $consultation->consultation_service_id)->first(); if ($already_ordered_service) { // check whether this has been paid for if ($already_ordered_service->payment_status == 1) { return 2; } // otherwise, go on and delete and also remove the performance fee StaffPerformedService::where('id', $already_ordered_service->performed_id)->delete(); $already_ordered_service->delete(); } $consultation->consultation_done_by = $transfered_to_doctor; $consultation->consultation_service_id = $allocated_service_id; $consultation->update(); } else { $consultation_record = new Consultation; $consultation_record->patient_id = $patient_id; $consultation_record->episode_id = $episode_id; $consultation_record->consultation_done_by = $transfered_to_doctor; $consultation_record->consultation_service_id = $allocated_service_id; $consultation_record->created_by = auth()->user()->id; $consultation_record->save(); // create new ordered service record $service_performed_id = record_staff_that_has_performed_the_service($patient_id, $episode_id, 3, $allocated_service_id, 0, $request->transfer_to_doctor); $service_order = new OrderedService; $service_order->patient_id = $patient_id; $service_order->episode_id = $episode_id; $service_order->service_id = $allocated_service_id; $service_order->quantity = 1; $service_order->performed = 1; $service_order->performed_id = $service_performed_id; $service_order->created_by = auth()->user()->id; $service_order->updated_by = auth()->user()->id; $service_order->save(); $episode->consultation_id = $consultation->id; } } if ($transfer->save() && $episode->update()) { return 1; } else { return 0; } } public function select_patient_create_session_variables(Request $request) { if (isset($request->ward_id)) { session()->put(['ward_id' => $request->ward_id]); session()->put(['date' => $request->date]); } if (isset($request->is_appointment_complete) && $request->is_appointment_complete == 1) { $appointment = PatientAppointment::find($request->appointment_id); // reassign the appointment_fulfilled to 1 $appointment->appointment_fulfilled = 1; $appointment->updated_by = Auth()->user()->id; $appointment->save(); } } public function create_special_clinic_episode(Request $request) { $clinic_id = $request->special_clinic_id; $last_inserted_id = 0; $logged_in_user_id = auth()->user()->id; $episode = new PatientEpisode; $episode->patient_id = $request->patient_id; $episode->clinic_id = $clinic_id; $episode->created_by = $logged_in_user_id; $episode->updated_by = $logged_in_user_id; try { $episode->save(); $last_inserted_id = $episode->id; $clinic_name = get_name($clinic_id, "id", "name", "clinics"); session()->put(['episode_id' => $last_inserted_id]); flash("Patient has been allocated to ".$clinic_name)->success(); $clinic_slug = get_name($clinic_id, "id", "slug", "clinics"); if ($clinic_slug == "art"){ //return redirect("hiv_menu"); } if ($clinic_slug == "diabetes") { //return redirect('diabetes_clinic/clinic_registration'); } if ($clinic_slug == "ante_natal") { //return redirect('ante_natal_clinic_menu'); } if ($clinic_slug == "mental_health") { $is_mental_health_clinic = true; session()->put(['episode_id' => $last_inserted_id]); session()->put(['is_mental_health_clinic' => 1]); //return redirect('consultation/route'); } return redirect('/patient_episodes/'); } catch (QueryException $e) { $errorCode = $e->errorInfo[1]; if ($errorCode == 1062) { //error code for duplicate entry to a unique field flash("This episode already exists!")->error(); return back()->withInput(); } } } public function create_episode_with_ward(Request $request) { $last_inserted_id = 0; $logged_in_user_id = auth()->user()->id; $episode = new PatientEpisode; $episode->patient_id = $request->patient_id; $episode->created_by = $logged_in_user_id; $episode->updated_by = $logged_in_user_id; $ward_id = $request->admission_ward_id; $admitted_on = $request->ward_admission_date; //Carbon::createFromFormat('d/m/Y', $request->ward_admission_date)->toDateString(); $ward_name = get_name($ward_id, 'id', 'name', 'wards'); try { $episode->save(); $last_inserted_id = $episode->id; session()->put(['episode_id' => $last_inserted_id]); // Admit patient in ward $inpatient = new InpatientInfo; $inpatient->patient_id = $request->patient_id; $inpatient->episode_id = $last_inserted_id; $inpatient->admitted_on = $admitted_on; $inpatient->ward_id = $ward_id; $inpatient->created_by = auth()->user()->id; $inpatient->created_at = Carbon::now(); $inpatient->save(); flash("Patient admitted in ".$ward_name)->success(); return redirect('/patient_episodes/'); } catch (QueryException $e) { $errorCode = $e->errorInfo[1]; if ($errorCode == 1062) { //error code for duplicate entry to a unique field flash("This episode already exists!")->error(); return back()->withInput(); } } } public function admit_patient_with_episode(Request $request) { $ward_id = $request->admission_ward_id; $admitted_on = $request->ward_admission_date; $ward_name = get_name($ward_id, 'id', 'name', 'wards'); // Admit patient in ward $inpatient = new InpatientInfo; $inpatient->patient_id = $request->patient_id; $inpatient->episode_id = $request->episode_admission_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(); flash("The patient has been admitted in ".$ward_name)->success(); return redirect('/patient_episodes/'); } public function create_episode_with_doctor(Request $request) { $services_id_with_user_id = $request->allocated_services_id_with_doctor_id; $services_id_with_user_id_array = explode("__", $services_id_with_user_id); // ["service_id", "user_id"] $allocated_service_id = $services_id_with_user_id_array[0]; $allocated_user_id = $services_id_with_user_id_array[1]; $last_inserted_id = 0; $logged_in_user_id = auth()->user()->id; $episode = new PatientEpisode; $episode->patient_id = $request->patient_id; $episode->created_by = $logged_in_user_id; $episode->updated_by = $logged_in_user_id; try { $episode->save(); $last_inserted_id = $episode->id; $allocated_persons_name = get_full_name($allocated_user_id, "id", "first_name", "last_name", "users"); session()->put(['episode_id' => $last_inserted_id]); flash("Patient has been allocated to ".$allocated_persons_name)->success(); /* create a record in consultations table attached to allocated doctor */ $consultation_record = new Consultation; $consultation_record->patient_id = $episode->patient_id; $consultation_record->episode_id = $last_inserted_id; $consultation_record->consultation_done_by = $allocated_user_id; $consultation_record->consultation_service_id = $allocated_service_id; $consultation_record->created_by = $logged_in_user_id; $consultation_record->save(); // create new ordered service record $service_performed_id = record_staff_that_has_performed_the_service($episode->patient_id, $last_inserted_id, 3, $allocated_service_id, 0, $allocated_user_id); $service_order = new OrderedService; $service_order->patient_id = $episode->patient_id; $service_order->episode_id = $last_inserted_id; $service_order->service_id = $allocated_service_id; $service_order->quantity = 1; $service_order->performed = 1; $service_order->performed_id = $service_performed_id; $service_order->created_by = $logged_in_user_id; $service_order->updated_by = $logged_in_user_id; $service_order->save(); /********** update patient episodes table with the consultation_id ********/ $episode_record = PatientEpisode::find($last_inserted_id); if (!is_null($episode_record)) { $episode_record->consultation_id = $consultation_record->id; $episode_record->update(); } /**********************/ return redirect('/patient_episodes/'); } catch (QueryException $e) { flash("An error occurred!")->error(); return back()->withInput(); } } public function create_episode_with_doctor_and_clinic(Request $request) { $services_id_with_user_id = $request->allocated_services_id_with_doctor_id; $services_id_with_user_id_array = explode("__", $services_id_with_user_id); // ["service_id", "user_id"] $allocated_service_id = $services_id_with_user_id_array[0]; $allocated_user_id = $services_id_with_user_id_array[1]; $clinic_id = $request->special_clinic_id; $last_inserted_id = 0; $logged_in_user_id = auth()->user()->id; $episode = new PatientEpisode; $episode->patient_id = $request->patient_id; $episode->clinic_id = $clinic_id; $episode->created_by = $logged_in_user_id; $episode->updated_by = $logged_in_user_id; $episode->claim_number = $request->claim_number; try { $episode->save(); $last_inserted_id = $episode->id; $allocated_persons_name = get_full_name($allocated_user_id, "id", "first_name", "last_name", "users"); /* create a record in consultations table attached to allocated doctor */ $consultation_record = new Consultation; $consultation_record->patient_id = $episode->patient_id; $consultation_record->episode_id = $last_inserted_id; $consultation_record->consultation_done_by = $allocated_user_id; $consultation_record->consultation_service_id = $allocated_service_id; /* for now make consultation->created_by = allocated_person */ $consultation_record->created_by = $logged_in_user_id; /* discuss and see whether to change or not*/ $consultation_record->save(); // create new ordered service record $service_performed_id = record_staff_that_has_performed_the_service($episode->patient_id, $last_inserted_id, 3, $allocated_service_id, 0, $allocated_user_id); $service_order = new OrderedService; $service_order->patient_id = $episode->patient_id; $service_order->episode_id = $last_inserted_id; $service_order->service_id = $allocated_service_id; $service_order->quantity = 1; $service_order->performed = 1; $service_order->performed_id = $service_performed_id; $service_order->created_by = $logged_in_user_id; $service_order->updated_by = $logged_in_user_id; $service_order->save(); /********** update patient episodes table with the consultation_id ********/ $episode_record = PatientEpisode::find($last_inserted_id); if (!is_null($episode_record)) { $episode_record->consultation_id = $consultation_record->id; $episode_record->update(); } /**********************/ $clinic_slug = get_name($clinic_id, "id", "slug", "clinics"); if ($clinic_slug == "art"){ //return redirect("hiv_menu"); } if ($clinic_slug == "diabetes") { //return redirect('diabetes_clinic/clinic_registration'); } if ($clinic_slug == "ante_natal") { //return redirect('ante_natal_clinic_menu'); } if ($clinic_slug == "mental_health") { $is_mental_health_clinic = true; session()->put(['episode_id' => $last_inserted_id]); session()->put(['is_mental_health_clinic' => 1]); //return redirect('consultation/route'); } session()->put(['episode_id' => $last_inserted_id]); flash("Patient has been allocated to ".$allocated_persons_name)->success(); return redirect('/patient_episodes/'); } catch (QueryException $e) { $errorCode = $e->errorInfo[1]; if ($errorCode == 1062) { //error code for duplicate entry to a unique field flash("This episode already exists!")->error(); return back()->withInput(); } } } public function create_episode_with_lab_self_request(Request $request) { $logged_in_user_id = auth()->user()->id; $episode = new PatientEpisode; $episode->patient_id = $request->patient_id; $episode->episode_type = 1; //self lab request episode $episode->created_by = $logged_in_user_id; $episode->updated_by = $logged_in_user_id; try { $episode->save(); session()->put(['episode_id' => $episode->id]); flash("Episode has started")->success(); return redirect('/investigations/investigations_review'); } catch (QueryException $e) { $errorCode = $e->errorInfo[1]; if ($errorCode == 1062) { //error code for duplicate entry to a unique field flash("This episode already exists!")->error(); return back()->withInput(); } } } public function episode_summary($episode_id) { $patient_id = session()->get('patient_id'); $patient = DB::table('patients')->find(session()->get('patient_id')); $episode = PatientEpisode::find($episode_id); $hospital_information = HospitalInformation::find(1); $outcomes = DB::table('outcomes')->pluck('name', 'id'); $diagnoses = DB::table('diagnoses')->where('available', 1)->pluck("name", "id"); $investigations = DB::table('investigations')->pluck("name", "id"); $procedures = DB::table('procedures')->where('available', 1)->pluck("name", "id"); $sundries = Sundry::where('available', 1)->orderby('name', 'asc')->pluck('name', 'id')->prepend('- select -', ''); $services = Services::where('available', 1)->orderby('name', 'asc')->pluck('name', 'id'); $sundries_results = Sundry::select('id', 'name')->get(); /* Patient File : It should show Diagnosis, Prescriptions, Lab results, Procedures */ $consultation = Consultation::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->first(); $treatments = Treatment::where(["patient_id" => $patient_id, "episode_id" => $episode_id])->get(); //fetch treatment the way Douglas fetches it in printing the treatment receipt $ordered_procedures = OrderedProcedure::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->get(); $ordered_sundries = OrderedSundry::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->get(); $ordered_services = OrderedService::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->get(); $inpatient_infos = InpatientInfo::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->get(); $antenatal_data = DB::table('ante_natal_clinic_followups')->where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->first(); // prep the inpatient investigation arrays and counters $opd_investigations = []; // get all investigations ordered in this episode $ordered_investigations = OrderedInvestigation::where(['episode_id' => $episode_id])->where('inpatient', 0)->get(); // get all results $investigation_results_order_ids = InvestigationResults::pluck('order_id', 'id')->toArray(); //triage details $triage = null; if ($episode && $episode->triage_id) { $triage = \Streamline\Models\Triage::where(['id' => $episode->triage_id])->first(); } // get all symptoms $symptoms = DB::table('symptoms')->where('available', 1)->pluck("name", "id"); foreach ($ordered_investigations as $investigation) { $investigation_ids = explode(",", $investigation->investigation_id); $inpatient_status = explode(",", $investigation->for_inpatient); // check if results are available for this investigation if (in_array($investigation->id, $investigation_results_order_ids)) { // get the key if available $key = array_search($investigation->id, $investigation_results_order_ids); // get the results object $results = InvestigationResults::find($key); // exclude obstetric u/s results if ($results->result_type != "Ultrasound_Obstetric") { $investigation_results = explode(",", $results->value); $investigation_per_valid = explode(",", $results->per_investigation); $investigation_comments = explode(",", $results->comment); if ($results->all_authenticated == 1) { for ($i = 0; $i < count($inpatient_status); $i++) { if ($inpatient_status[$i] == 0) { if (isset($investigation_ids[$i]) && $investigation_ids[$i] != "") { $result = Investigation::where('id', $investigation_ids[$i])->first(); $opd_investigations['name'][] = $result->name; $opd_investigations['value'][] = $investigation_results[$i]; $opd_investigations['comment'][] = $investigation_comments[$i]; $opd_investigations['type'][] = $result->type; } } else { if (isset($investigation_ids[$i]) && $investigation_ids[$i] != "") { $result = Investigation::where('id', $investigation_ids[$i])->first(); $ward_investigations['name'][] = $result->name; $ward_investigations['value'][] = $investigation_results[$i]; $ward_investigations['comment'][] = $investigation_comments[$i]; $ward_investigations['type'][] = $result->type; } } } } else { for ($i = 0; $i < count($inpatient_status); $i++) { if ($investigation_per_valid[$i] == 1) { // this investigation is authenticated if ($inpatient_status[$i] == 0) { if (isset($investigation_ids[$i]) && $investigation_ids[$i] != "") { $result = Investigation::where('id', $investigation_ids[$i])->first(); $opd_investigations['name'][] = $result->name; $opd_investigations['value'][] = $investigation_results[$i]; $opd_investigations['comment'][] = $investigation_comments[$i]; $opd_investigations['type'][] = $result->type; } } else { if (isset($investigation_ids[$i]) && $investigation_ids[$i] != "") { $result = Investigation::where('id', $investigation_ids[$i])->first(); $ward_investigations['name'][] = $result->name; $ward_investigations['value'][] = $investigation_results[$i]; $ward_investigations['comment'][] = $investigation_comments[$i]; $ward_investigations['type'][] = $result->type; } } } else { if ($inpatient_status[$i] == 0) { if (isset($investigation_ids[$i]) && $investigation_ids[$i] != "") { $result = Investigation::where('id', $investigation_ids[$i])->first(); $opd_investigations['name'][] = $result->name; $opd_investigations['value'][] = "Pending"; $opd_investigations['comment'][] = "Pending"; } } else { if (isset($investigation_ids[$i]) && $investigation_ids[$i] != "") { $result = Investigation::where('id', $investigation_ids[$i])->first(); $ward_investigations['name'][] = $result->name; $ward_investigations['value'][] = "Pending"; $ward_investigations['comment'][] = "Pending"; } } } } } } } else { for ($i = 0; $i < count($inpatient_status); $i++) { if ($inpatient_status[$i] == 0) { if (isset($investigation_ids[$i]) && $investigation_ids[$i] != "") { $result = Investigation::where('id', $investigation_ids[$i])->first(); $opd_investigations['name'][] = $result->name; $opd_investigations['value'][] = "Pending"; $opd_investigations['comment'][] = "Pending"; } } else { if (isset($investigation_ids[$i]) && $investigation_ids[$i] != "") { $result = Investigation::where('id', $investigation_ids[$i])->first(); $ward_investigations['name'][] = $result->name; $ward_investigations['value'][] = "Pending"; $ward_investigations['comment'][] = "Pending"; } } } } } $data = [ 'hospitalInfo' => $hospital_information, 'patient' => $patient, 'episode' => $episode, 'consultation' => $consultation, 'treatments' => $treatments, 'ordered_procedures' => $ordered_procedures, 'diagnoses' => $diagnoses, 'ordered_investigations' => $ordered_investigations, 'investigations' => $investigations, 'procedures' => $procedures, 'opd_investigations' => $opd_investigations, 'inpatient_infos' => $inpatient_infos, 'sundries' => $sundries, 'ordered_sundries'=>$ordered_sundries, 'services' =>$services, 'ordered_services'=>$ordered_services, 'outcomes' => $outcomes, 'antenatal_data' => $antenatal_data, 'symptoms'=> $symptoms ]; $pdf = SnappyPDF::loadView('patients::patient_episodes/episode_summary', $data) ->setOrientation('portrait') ->setOption('margin-bottom', 7) ->setOption('margin-top', 5) ->setOption('footer-html', 'Printed On ' . date('d-M-Y h:ia') . ' By ' . auth()->user()->first_name . " " . auth()->user()->last_name . ''); $patientName = $patient->first_name . ' ' . $patient->last_name; return $pdf->inline($patientName . ' - Episode Summary' . date(" d-m-y h:ia") . '.pdf'); } public function get_assigned_doctor($episode_id) { $patient_id = session()->get('patient_id'); $consultation = Consultation::where(['patient_id'=> $patient_id, 'episode_id' => $episode_id])->first(); if ($consultation) { $doctor_id = $consultation->consultation_done_by; $doctor_name = get_full_name($doctor_id, "id", "first_name", "last_name", "users"); return $doctor_name; } return 0; } public function save_doctor_transfer(Request $request) { $patient_id = session()->get('patient_id'); $doctor_to_transfer_to = $request->dt_doctor_to; $episode_id = $request->episode_id; if ($doctor_to_transfer_to && $doctor_to_transfer_to != "remove_from_doctor") { $services_id_with_user_id = $doctor_to_transfer_to; $services_id_with_user_id_array = explode("__", $services_id_with_user_id); // ["service_id", "user_id"] $allocated_service_id = $services_id_with_user_id_array[0]; $transfer_to_doctor = $services_id_with_user_id_array[1]; $consultation = Consultation::where(['patient_id'=> $patient_id, 'episode_id' => $request->episode_id])->first(); if ($consultation) { // first, check if there is a previously ordered service attached to current doctor $already_ordered_service = OrderedService::where('performed', 1) ->where('patient_id', $patient_id) ->where('episode_id', $episode_id) ->where('service_id', $consultation->consultation_service_id)->first(); if ($already_ordered_service) { // check whether this has been paid for if ($already_ordered_service->payment_status == 1) { return 2; } // otherwise, go on and delete and also remove the performance fee StaffPerformedService::where('id', $already_ordered_service->performed_id)->delete(); $already_ordered_service->delete(); } $consultation->consultation_done_by = $transfer_to_doctor; $consultation->consultation_service_id = $allocated_service_id; } else { // create a consultation record with new assigned doctor $consultation = new Consultation; $consultation->patient_id = $patient_id; $consultation->episode_id = $episode_id; $consultation->consultation_done_by = $transfer_to_doctor; $consultation->consultation_service_id = $allocated_service_id; $consultation->created_by = auth()->user()->id; } if ($consultation->save()) { /********** update patient episodes table with the consultation_id ********/ $episode_record = PatientEpisode::find($episode_id); // create new ordered service record $service_performed_id = record_staff_that_has_performed_the_service($patient_id, $episode_id, 3, $allocated_service_id, 0, $transfer_to_doctor); $service_order = new OrderedService; $service_order->patient_id = $patient_id; $service_order->episode_id = $episode_id; $service_order->service_id = $allocated_service_id; $service_order->quantity = 1; $service_order->performed = 1; $service_order->performed_id = $service_performed_id; $service_order->created_by = auth()->user()->id; $service_order->updated_by = auth()->user()->id; $service_order->save(); $episode_record->consultation_id = $consultation->id; $episode_record->update(); return 1; } else { return 0; } } else { return 0; } } function start_appointment_with_clinic(Request $request) { $appointment_id = $request->selected_appointment_id; $selected_clinic_id = $request->special_clinic_id; $patient_id = $request->patient_id; $allocated_service_id = null; $allocated_user_id = null; $consultation_record = null; $services_id_with_user_id = $request->allocated_services_id_with_doctor_id; if ($services_id_with_user_id) { $services_id_with_user_id_array = explode("__", $services_id_with_user_id); // ["service_id", "user_id"] $allocated_service_id = $services_id_with_user_id_array[0]; $allocated_user_id = $services_id_with_user_id_array[1]; } $appointment = \Streamline\Models\PatientAppointment::find($appointment_id); // reassign the appointment_fulfilled to 1 $appointment->appointment_fulfilled = 1; $appointment->updated_by = Auth()->user()->id; if ($appointment->save()){ $episode = new PatientEpisode; $episode->clinic_id = $selected_clinic_id; $episode->patient_id = $patient_id; $episode->parent_episode_id = $appointment->episode_id == 0 ? null : $appointment->episode_id;//the original episode_id $episode->created_by = auth()->user()->id; $episode->updated_by = auth()->user()->id; $episode->save(); $last_inserted_id = $episode->id; if ($services_id_with_user_id) { $allocated_persons_name = get_full_name($allocated_user_id, "id","first_name","last_name","users"); /* create a record in consultations table attached to allocated doctor */ $consultation_record = new Consultation; $consultation_record->patient_id = $episode->patient_id; $consultation_record->episode_id = $last_inserted_id; $consultation_record->consultation_done_by = $allocated_user_id; $consultation_record->consultation_service_id = $allocated_service_id; /* for now make consultation->created_by = allocated_person */ $consultation_record->created_by = auth()->user()->id; /* discuss and see whether to change or not*/ $consultation_record->save(); } /********** update patient episodes table with the consultation_id ********/ $episode_record = PatientEpisode::find($last_inserted_id); if (!is_null($episode_record) && !is_null($consultation_record)) { $episode_record->consultation_id = $consultation_record->id; $episode_record->update(); } /**********************/ //put this patient_id into session session()->put('patient_id', $patient_id); try { flash("A new episode has been saved")->success(); //if the parent episode was an ANC visit then create a copy of previous anc registration and attach it to this episode $previous_anc_visit = \Streamline\Models\AnteNatalClinicFollowup::where(['patient_id' => $episode->patient_id, 'episode_id' => $episode->parent_episode_id])->first(); if ($previous_anc_visit) { create_anc_registration_record_from_previous_visit($patient_id, $episode->parent_episode_id, $episode->id); } // check where the function was called from and return there if (isset($request->is_from_finance)) { // return to finance home return redirect('/patient_finance/home'); } else { // return to normal patient home return redirect('/patient_episodes/'); } } catch (QueryException $e) { $errorCode = $e->errorInfo[1]; if ($errorCode == 1062) { //error code for duplicate entry to a unique field flash("This episode already exists!")->error(); return back()->withInput(); } } /*================= end of creation of new episode on starting an appointment =================*/ flash("Patient follow up completed successfully")->success(); return redirect('/patient_episodes/set_patient_id/' . $patient_id); } else { flash("An error occurred!")->error(); return back()->withInput(); } } function delete_episode($episode_id) { $episode = PatientEpisode::find($episode_id); // check if consultation record exists with that id $consultation = Consultation::where('episode_id', $episode_id) ->first(); if ($consultation) { $consultation->forceDelete(); } if ($episode->forceDelete()) { flash("Patient episode has been deleted.")->success(); return redirect('/patient_episodes/'); } } public function episode_merge_preview() { $patient_id = session()->get('patient_id'); $patient = Patient::find($patient_id); $episodes = PatientEpisode::where('patient_id', $patient_id)->orderBy('id','desc')->get(); $diagnoses = DB::table('diagnoses')->where('available', 1)->pluck("name", "id"); $clinics = DB::table('clinics')->pluck("name", "id")->prepend('- select -', ''); $special_clinics = DB::table('clinics')->where('available', 1)->whereNull('deleted_at')->pluck("name", "id")->prepend('- select -', ''); $wards = DB::table('wards')->where('available', 1)->pluck("name", "id")->prepend('- select -', ''); $users_collection = DB::table('users')->orderBy("first_name","asc")->select("id")->get()->toArray(); $users_array = []; foreach ($users_collection as $value){ $user = User::find($value->id); if (!is_null($user)) { if ($user->hasRole('Doctors') || $user->hasRole('Doctor')) { //use this join query instead of the commented out query to cater for execution speed (N+1) $consultation_records = DB::table('services') ->join('staff_payment_configurations','staff_payment_configurations.item_id', '=', 'services.id') ->where('services.item_type', 'Consultation') ->where('staff_payment_configurations.user_id',$value->id) ->where('staff_payment_configurations.item_category',3) ->whereNull('staff_payment_configurations.deleted_at') ->orderBy('staff_payment_configurations.created_at','asc') ->select('services.*') ->get(); foreach ($consultation_records as $record) { $price_list_id = is_patient_category_attached_to_price_list($patient_id); if ($price_list_id) { $users_consultation_fee = get_price_list_category_price($price_list_id, 6, $record->id); } else{ $users_consultation_fee = $record->non_insured_price; } //concatnate the service_record_id with the users id to form the key for the array $users_array[$record->id."__".$value->id] = get_full_name($value->id, 'id', 'first_name', 'last_name', 'users')." (".$record->name." Fee: ".ugandan_shillings($users_consultation_fee).")"; } } } } $users_array = ['' => '- select -'] + $users_array; return view('patients::patient_episodes.merge_episodes',compact('patient', 'episodes', 'diagnoses', 'clinics', 'special_clinics', 'wards', 'users_array')); } public function display_original_and_duplicate_episodes(Request $request) { $episode_id_one = $request->episode_id_one; $episode_id_two = $request->episode_id_two; $episode_one = PatientEpisode::find($episode_id_one); $html_text_one = ""; $html_text_one .= ""; $html_text_one .= "clinic_id."> " . get_name($episode_one->clinic_id, "id", "name", "clinics") . ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= ""; $html_text_one .= "
Select which episode to keep Episode started on ". streamline_date_time($episode_one->created_at)."
Primary Diagnosis " . get_name(get_name($episode_one->consultation_id, "id", "primary_diagnosis", "consultations"), "id", "name", "diagnoses") . "
Triage Comments " . get_name($episode_one->triage_id, "id", "comments", "triage") . "
Consultation Comments " . get_name($episode_one->consultation_id, "id", "comments", "consultations") . "
Examination Comments " . get_name($episode_one->consultation_id, "id", "clinic_examination_comments", "consultations") . "
Management Plan " . get_name($episode_one->consultation_id, "id", "investigation_and_management_plan_comments", "consultations") . "
History Comments " . get_name($episode_one->consultation_id, "id", "history_comments", "consultations") . "
"; $episode_two = PatientEpisode::find($episode_id_two); $html_text_two = ""; $html_text_two .= ""; $html_text_two .= "clinic_id."> " . get_name($episode_two->clinic_id, "id", "name", "clinics") . ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= ""; $html_text_two .= "
Episode started on ". streamline_date_time($episode_two->created_at)."
" . get_name(get_name($episode_two->consultation_id, "id", "primary_diagnosis", "consultations"), "id", "name", "diagnoses") . "
" . get_name($episode_two->triage_id, "id", "comments", "triage") . "
" . get_name($episode_two->consultation_id, "id", "comments", "consultations") . "
" . get_name($episode_two->consultation_id, "id", "clinic_examination_comments", "consultations") . "
" . get_name($episode_two->consultation_id, "id", "investigation_and_management_plan_comments", "consultations") . "
" . get_name($episode_two->consultation_id, "id", "history_comments", "consultations") . "
"; return json_encode([ "html_one" => $html_text_one, "html_two" => $html_text_two, ]); } public function complete_episodes_merge(Request $request) { $episode_id_one = (int)$request->original_id; $episode_id_two = (int)$request->duplicate_id; $episode_id_to_delete = 0; $episode_id_to_keep = 0; if ($request->has('all_duplicate_fields')) { $episode_id_to_delete = $request->original_id; $episode_id_to_keep = $request->duplicate_id; } if ($request->has('all_original_fields')) { $episode_id_to_delete = $request->duplicate_id; $episode_id_to_keep = $request->original_id; } //1.get the original and dupe episode ids //2.the the original record with all selected fields to keep //3.loop through the medical and finance tables updating the episode id. $delete_episode_record = PatientEpisode::find($episode_id_to_delete); $delete_episode_record->delete(); if ($delete_episode_record->update()) { //update the episode with new clinic $episode = PatientEpisode::find($episode_id_to_keep); $episode->clinic_id = $request->clinic_id; $episode->update(); //loop through triage $patient_triage = \Streamline\Models\Triage::where('episode_id', $episode_id_to_delete)->get(); foreach ($patient_triage as $triage) { $triage->episode_id = $episode_id_to_keep; $triage->clinic_allocation = $request->clinic_id; $triage->update(); } //loop thru consultations $patient_consultations = \Streamline\Models\Consultation::where('episode_id', $episode_id_to_delete)->get(); foreach ($patient_consultations as $consultation) { $consultation->episode_id = $episode_id_to_keep; $consultation->primary_diagnosis = ($request->primary_diagnosis == 'N/A') ? null : $request->primary_diagnosis; $consultation->update(); } $consultations = \Streamline\Models\Consultation::where('episode_id', $episode_id_to_keep)->get(); foreach ($consultations as $consultation) { $consultation->primary_diagnosis = ($request->primary_diagnosis == 'N/A') ? null : $request->primary_diagnosis; $consultation->clinic_examination_comments = $request->clinic_examination_comments; $consultation->history_comments = $request->history_comments; $consultation->comments = $request->consultation_comments; $consultation->update(); } //loop thru treatment $patient_treatments = \Streamline\Models\Treatment::where('episode_id', $episode_id_to_delete)->get(); foreach ($patient_treatments as $treatment) { $treatment->episode_id = $episode_id_to_keep; $treatment->update(); } //loop thru ordered invs $patient_ordered_invs = \Streamline\Models\OrderedInvestigation::where('episode_id', $episode_id_to_delete)->get(); foreach ($patient_ordered_invs as $ordered_invs) { $ordered_invs->episode_id = $episode_id_to_keep; $ordered_invs->update(); } //loop thru investigation results $investigation_results = \Streamline\Models\InvestigationResults::where('episode_id', $episode_id_to_delete)->get(); foreach ($investigation_results as $inv_results) { $inv_results->episode_id = $episode_id_to_keep; $inv_results->update(); } //loop thru ordered procedures $patient_ordered_procedures = \Streamline\Models\OrderedProcedure::where('episode_id', $episode_id_to_delete)->get(); foreach ($patient_ordered_procedures as $ordered_procedures) { $ordered_procedures->episode_id = $episode_id_to_keep; $ordered_procedures->update(); } //loop thru ordered sundries $patient_ordered_sundries = \Streamline\Models\OrderedSundry::where('episode_id', $episode_id_to_delete)->get(); foreach ($patient_ordered_sundries as $ordered_sundry) { $ordered_sundry->episode_id = $episode_id_to_keep; $ordered_sundry->update(); } //loop through ordered services $patient_ordered_services = \Streamline\Models\OrderedService::where('episode_id', $episode_id_to_delete)->get(); foreach ($patient_ordered_services as $ordered_services) { $ordered_services->episode_id = $episode_id_to_keep; $ordered_services->update(); } //loop through inpatient info $inpatient_info_records = \Streamline\Models\InpatientInfo::where('episode_id', $episode_id_to_delete)->get(); foreach ($inpatient_info_records as $inpatient_info) { $inpatient_info->episode_id = $episode_id_to_keep; $inpatient_info->update(); } //loop though inpatient bills $inpatient_bills = \Streamline\Models\InpatientBill::where('episode_id', $episode_id_to_delete)->get(); foreach ($inpatient_bills as $inpatient_bill) { $inpatient_bill->episode_id = $episode_id_to_keep; $inpatient_bill->update(); } //loop though ward bed stays $ward_bed_stay = \Streamline\Models\WardBedStay::where('episode_id', $episode_id_to_delete)->get(); foreach ($ward_bed_stay as $bed_stay) { $bed_stay->episode_id = $episode_id_to_keep; $bed_stay->update(); } //loop though ward consultation and service $ward_consultations_and_services = \Streamline\Models\WardConsultationsAndService::where('episode_id', $episode_id_to_delete)->get(); foreach ($ward_consultations_and_services as $ward_consultation) { $ward_consultation->episode_id = $episode_id_to_keep; $ward_consultation->update(); } //loop though ward extras $ward_extras = \Streamline\Models\WardExtra::where('episode_id', $episode_id_to_delete)->get(); foreach ($ward_extras as $ward_extra) { $ward_extra->episode_id = $episode_id_to_keep; $ward_extra->update(); } //loop though ward comments $ward_comments = \Streamline\Models\WardInpatientSheetComment::where('episode_id', $episode_id_to_delete)->get(); foreach ($ward_comments as $ward_comment) { $ward_comment->episode_id = $episode_id_to_keep; $ward_comment->update(); } //loop though ward investigation pricing $ward_investigation_pricing = \Streamline\Models\WardInvestigationPricing::where('episode_id', $episode_id_to_delete)->get(); foreach ($ward_investigation_pricing as $ward_inv_pricing) { $ward_inv_pricing->episode_id = $episode_id_to_keep; $ward_inv_pricing->update(); } //loop though ward procedures $ward_procedures = \Streamline\Models\WardProcedure::where('episode_id', $episode_id_to_delete)->get(); foreach ($ward_procedures as $ward_procedure) { $ward_procedure->episode_id = $episode_id_to_keep; $ward_procedure->update(); } //loop though ward sundries $ward_sundries = \Streamline\Models\WardSundryDispensation::where('episode_id', $episode_id_to_delete)->get(); foreach ($ward_sundries as $ward_sundry) { $ward_sundry->episode_id = $episode_id_to_keep; $ward_sundry->update(); } //loop though ward treatment $ward_treatments = \Streamline\Models\WardTreatment::where('episode_id', $episode_id_to_delete)->get(); foreach ($ward_treatments as $ward_treatment) { $ward_treatment->episode_id = $episode_id_to_keep; $ward_treatment->update(); } $ward_treatment_dispensations = \Streamline\Models\WardTreatmentDispensation::where('episode_id', $episode_id_to_delete)->get(); foreach ($ward_treatment_dispensations as $ward_treatment_dispensation) { $ward_treatment_dispensation->episode_id = $episode_id_to_keep; $ward_treatment_dispensation->update(); } //loop through investigation deposits $investigation_deposits = \Streamline\Models\InvestigationDeposit::where('episode_id', $episode_id_to_delete)->get(); foreach ($investigation_deposits as $inv_deposit) { $inv_deposit->episode_id = $episode_id_to_keep; $inv_deposit->update(); } //loop through treatment deposits $treatment_deposits = \Streamline\Models\TreatmentDeposits::where('episode_id', $episode_id_to_delete)->get(); foreach ($treatment_deposits as $treatment_deposit) { $treatment_deposit->episode_id = $episode_id_to_keep; $treatment_deposit->update(); } //loop through service deposits $service_deposits = \Streamline\Models\ServiceDeposit::where('episode_id', $episode_id_to_delete)->get(); foreach ($service_deposits as $service_deposit) { $service_deposit->episode_id = $episode_id_to_keep; $service_deposit->update(); } //loop through procedure deposits $procedure_deposits = \Streamline\Models\ProcedureDeposit::where('episode_id', $episode_id_to_delete)->get(); foreach ($procedure_deposits as $procedure_deposit) { $procedure_deposit->episode_id = $episode_id_to_keep; $procedure_deposit->update(); } //loop through sundries deposits $sundry_deposits = \Streamline\Models\SundryDeposit::where('episode_id', $episode_id_to_delete)->get(); foreach ($sundry_deposits as $sundry_deposit) { $sundry_deposit->episode_id = $episode_id_to_keep; $sundry_deposit->update(); } //loop through patient category invoices $patient_category_invoices = \Streamline\Models\PatientCategoryInvoice::where('episode_id', $episode_id_to_delete)->get(); foreach ($patient_category_invoices as $patient_invoice) { $patient_invoice->episode_id = $episode_id_to_keep; $patient_invoice->update(); } $staff_performed_services = StaffPerformedService::where('episode_id', $episode_id_to_delete)->get(); foreach ($staff_performed_services as $staff_performed_service) { $staff_performed_service->episode_id = $episode_id_to_keep; $staff_performed_service->update(); } $patient_clinic_transfers = PatientClinicTransfers::where('episode_id', $episode_id_to_delete)->get(); foreach ($patient_clinic_transfers as $patient_clinic_transfer) { $patient_clinic_transfer->episode_id = $episode_id_to_keep; $patient_clinic_transfer->update(); } $ordered_sundries = OrderedSundry::where('episode_id', $episode_id_to_delete)->get(); foreach ($ordered_sundries as $ordered_sundry) { $ordered_sundry->episode_id = $episode_id_to_keep; $ordered_sundry->update(); } $tables = ['ward_inpatient_sheet_nurse_comments','ward_inpatient_detailed_notes','ward_dispensing_per_charts','patient_documents','patient_one_off_discounts','triage_nutrition','triage_news','surgeries','sundries_deposits','smart_triage','patient_refunds','patient_account_consumptions','patient_appointments','phone_followup_patients','point_of_sale_records','procedure_deposits','cancel_patient_transactions','central_billing_deposits','chronic_patients','chi_deposits','debtors','debt_plan','dependants_consumptions','family_account_consumptions','inpatient_attendant_passes','inpatient_ward_discounts','inpatient_sheet_audits','maternity_delivery_records','maternity_inpatients','mental_health_consultation','ordered_investigations','ordered_procedures']; foreach ($tables as $table) { $episodes = DB::table($table)->where('episode_id', $episode_id_to_delete)->get(); foreach ($episodes as $episode) DB::table($table)->where('id', $episode->id)->update(['episode_id' => $episode_id_to_keep]); } $incoming_ward_charts = DB::table('incoming_ward_charts')->whereRaw('FIND_IN_SET(' . $episode_id_to_delete . ',episode_ids)')->get(); foreach ($incoming_ward_charts as $incoming_ward_chart) { $episode_ids = explode(',',$incoming_ward_chart->episode_ids); foreach ($episode_ids as $key => $episode_id) if( $episode_id == $episode_id_to_delete) $episode_ids[$key]=$episode_id_to_keep; DB::table('incoming_ward_charts')->where('id', $incoming_ward_chart->id)->update(['episode_ids' => implode(',', $episode_ids)]); } flash("Episodes have been merged")->success(); return redirect('patient_episodes'); } flash("Oops, episode merge has failed. Contact Stre@mline Support")->error(); return back()->withInput(); } public function check_clinical_consultation_payment(Request $request) { $service_deposit = \Streamline\Models\ServiceDeposit::where('episode_id', $request->episode_id)->first(); if ($service_deposit) { return "paid"; } return "unpaid"; } public function is_patient_allowed_to_have_consultation($patient_id, $episode_id) { $patient_details = Patient::withTrashed()->find($patient_id); $episode_details = PatientEpisode::withTrashed()->find($episode_id); $patient_episodes_payment_setting = $patient_details->episode_payments; $service_deposit = ServiceDeposit::where('episode_id', $episode_id)->first(); $patient_category_invoice = PatientCategoryInvoice::where(['episode_id' => $episode_id, 'tag_id' => 6])->first(); // check if the episode is a review or not if (is_null($episode_details->parent_episode_id)) { $is_perform_unpaid_consultations_enabled = is_perform_unpaid_consultations_enabled($patient_details->category_id); } else { $is_perform_unpaid_consultations_enabled = is_perform_unpaid_review_consultations_enabled($patient_details->category_id); } if (!$is_perform_unpaid_consultations_enabled) { if ($patient_episodes_payment_setting == 0) { if ($service_deposit || $patient_category_invoice) { return true; } return false; } elseif ($patient_episodes_payment_setting == 1) { return false; } } return true; } }