mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
415 lines
17 KiB
PHP
415 lines
17 KiB
PHP
<?php
|
|
|
|
namespace Modules\Patients\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Streamline\Http\Controllers\SMSController;
|
|
use Streamline\Models\Clinic;
|
|
use Streamline\Models\PatientAppointment;
|
|
use Streamline\Models\PatientEpisode;
|
|
use Streamline\Models\User;
|
|
|
|
class PatientAppointmentsController
|
|
{
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function follow_up()
|
|
{
|
|
$follow_ups = PatientAppointment::where('is_confirmed', 1)->get();
|
|
|
|
$calender_dates = [];
|
|
$count = 0;
|
|
|
|
foreach ($follow_ups as $follow_up) {
|
|
$patient_name = get_full_name($follow_up->patient_id, 'id', 'first_name', 'last_name', 'patients');
|
|
$calender_dates[$count] = ["title" => $patient_name, "start" => $follow_up->appointment_date . "T" . $follow_up->appointment_time];
|
|
$count++;
|
|
}
|
|
|
|
$clinics = Clinic::orderBy('name')->pluck("name", "id")->toArray();
|
|
$clinics = ['0' => 'All Clinics'] + $clinics;
|
|
|
|
$users = User::orderBy('first_name')->select("id", "first_name", "last_name")->get();
|
|
|
|
$users_array = [];
|
|
$users_collection = DB::table('users')->orderBy("first_name", "asc")->select("id")->get()->toArray();
|
|
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)
|
|
->orderBy('staff_payment_configurations.created_at', 'asc')
|
|
->select('services.*')
|
|
->get();
|
|
|
|
foreach ($consultation_records as $record) {
|
|
$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') . " (Consultation Fee: " . ugandan_shillings($users_consultation_fee) . ")";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$users_array = ['' => '- select -'] + $users_array;
|
|
$special_clinics = DB::table('clinics')->where('available', 1)->whereNull('deleted_at')->orderBy('name', 'asc')->pluck("name", "id")->prepend('- select -', '');
|
|
|
|
return view('patients::patients.follow_up', compact('calender_dates', 'clinics', 'users', 'users_array', 'special_clinics'));
|
|
}
|
|
|
|
public function follow_up_fetch_patients(Request $request)
|
|
{
|
|
$selected_date = $request->selected_date;
|
|
|
|
if ($request->user_id != 0) {
|
|
$follow_ups = PatientAppointment::where(['appointment_date' => $selected_date])->where(['incharge_id' => $request->user_id])->get();
|
|
} else {
|
|
$follow_ups = PatientAppointment::where(['appointment_date' => $selected_date])->get();
|
|
}
|
|
|
|
$code = "";
|
|
|
|
if ($follow_ups->count() > 0) {
|
|
foreach ($follow_ups as $follow_up) {
|
|
|
|
if ($request->clinic_id != -1) {
|
|
if ($follow_up->clinic_allocation != $request->clinic_id) {
|
|
// skip current loop
|
|
continue;
|
|
}
|
|
}
|
|
|
|
$code .= "<tr>";
|
|
$code .= "<td>";
|
|
$code .= get_full_name($follow_up->patient_id, 'id', 'first_name', 'last_name', 'patients') . " (" . get_name($follow_up->patient_id, 'id', 'number', 'patients') . ")";
|
|
$code .= "</td>";
|
|
$code .= "<td>";
|
|
$code .= ($follow_up->clinic_allocation == 0) ? "No clinic specified" : get_name($follow_up->clinic_allocation, 'id', 'name', 'clinics');
|
|
$code .= "</td>";
|
|
$code .= "<td>";
|
|
$code .= ($follow_up->incharge_id == 0) ? "No incharge specified" : get_full_name($follow_up->incharge_id, 'id', 'first_name', 'last_name', 'users');
|
|
$code .= "</td>";
|
|
$code .= "<td>";
|
|
$code .= $follow_up->appointment_time;
|
|
$code .= "</td>";
|
|
$code .= "<td>";
|
|
$code .= "Phone Number: " . get_name($follow_up->patient_id, 'id', 'phone', 'patients') . " Email: " . get_name($follow_up->patient_id, 'id', 'email', 'patients');
|
|
$code .= "</td>";
|
|
$code .= "<td>";
|
|
$code .= is_null($follow_up->comments) ? 'N/A' : $follow_up->comments;
|
|
$code .= "</td>";
|
|
$code .= "<td>";
|
|
if ($follow_up->appointment_fulfilled == 1) {
|
|
$code .= "<label class='label label-success'>Appointment Completed</label>";
|
|
} else if ($follow_up->appointment_fulfilled == 2) {
|
|
$code .= "<label class='label label-danger'>Appointment Cancelled</label>";
|
|
} else {
|
|
$code .= "<a class='btn btn-success btn-rounded' onclick='displayAppointmentActions(" . $follow_up->patient_id . "," . $follow_up->id . ")'>Actions</a>";
|
|
}
|
|
$code .= "</td>";
|
|
$code .= "</tr>";
|
|
}
|
|
} else {
|
|
$code .= "<tr><td colspan='8'><h4 class='text-center'>No patient appointments available for this date</h4></td></tr>";
|
|
}
|
|
|
|
// check if the code is empty for when clinics selected are not available
|
|
if ($code == '') {
|
|
$code .= "<tr><td colspan='8'><h4 class='text-center'>No patient appointments available for this date</h4></td></tr>";
|
|
}
|
|
|
|
return $code;
|
|
}
|
|
|
|
public function create_appointment()
|
|
{
|
|
$clinics = Clinic::orderBy('name')->pluck("name", "id")->toArray();
|
|
$clinics = ['0' => "Don't assign clinic"] + $clinics;
|
|
|
|
$users = User::orderBy('first_name')->select("id", "first_name", "last_name")->get();
|
|
|
|
return view('patients::patients.create_appointment', compact('clinics', 'users'));
|
|
}
|
|
|
|
public function save_appointment(Request $request)
|
|
{
|
|
$appointment = new PatientAppointment();
|
|
|
|
if (is_null($request->in_charge)) {
|
|
$in_charge = 0;
|
|
} else {
|
|
$in_charge = $request->in_charge;
|
|
}
|
|
|
|
if (is_null($request->clinic_allocation)) {
|
|
$clinic_allocation = 0;
|
|
} else {
|
|
$clinic_allocation = $request->clinic_allocation;
|
|
}
|
|
|
|
$appointment->patient_id = $request->patient_id;
|
|
$appointment->incharge_id = $in_charge;
|
|
$appointment->clinic_allocation = $clinic_allocation;
|
|
$appointment->episode_id = 0;
|
|
$appointment->appointment_date = Carbon::parse($request->appointment_date)->format('Y-m-d');
|
|
$appointment->appointment_time = $request->appointment_time;
|
|
$appointment->comments = $request->comments;
|
|
$appointment->created_from = "Patient Appointments";
|
|
$appointment->created_by = Auth::user()->id;
|
|
$appointment->updated_by = Auth::user()->id;
|
|
|
|
if ($appointment->save()) {
|
|
flash("Patient appointment has been saved")->success();
|
|
return redirect("/patients/follow_up");
|
|
} else {
|
|
flash("An error occurred!")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function complete_appointment($id)
|
|
{
|
|
$appointment = PatientAppointment::find($id);
|
|
|
|
$patient_id = $appointment->patient_id;
|
|
|
|
// reassign the appointment_fulfilled to 1
|
|
$appointment->appointment_fulfilled = 1;
|
|
$appointment->updated_by = Auth::user()->id;
|
|
|
|
if ($appointment->save()) {
|
|
$episode = new PatientEpisode;
|
|
$episode->patient_id = $appointment->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;
|
|
//put this patient_id into session
|
|
session()->put('patient_id', $patient_id);
|
|
|
|
try {
|
|
$episode->save();
|
|
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);
|
|
//allocate to ANC clinic straight away
|
|
$anc_clinic_id = get_name('ante_natal', 'slug', 'id', 'clinics');
|
|
$episode->clinic_id = $anc_clinic_id;
|
|
$episode->update();
|
|
}
|
|
|
|
// 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) {
|
|
flash("An error occurred")->error();
|
|
return back()->withInput();
|
|
}
|
|
} else {
|
|
flash("An error occurred!")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function cancel_patient_appointment($id)
|
|
{
|
|
$appointment = PatientAppointment::find($id);
|
|
|
|
// reassign the appointment_fulfilled to 2, indicating cancelled
|
|
$appointment->appointment_fulfilled = 2;
|
|
$appointment->updated_by = Auth::user()->id;
|
|
|
|
if ($appointment->save()) {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public function reschedule_appointment($id)
|
|
{
|
|
$appointment = PatientAppointment::find($id);
|
|
$clinics = Clinic::orderBy('name')->pluck("name", "id")->toArray();
|
|
$clinics = ['0' => "Don't assign clinic"] + $clinics;
|
|
|
|
$users = User::orderBy('first_name')->select("id", "first_name", "last_name")->get();
|
|
|
|
return view('patients::patients.reschedule_appointment', compact('appointment', 'clinics', 'users'));
|
|
}
|
|
|
|
public function save_rescheduled_appointment(Request $request)
|
|
{
|
|
$appointment = PatientAppointment::find($request->appointment_id);
|
|
|
|
if (is_null($request->in_charge)) {
|
|
$in_charge = 0;
|
|
} else {
|
|
$in_charge = $request->in_charge;
|
|
}
|
|
|
|
if (is_null($request->clinic_allocation)) {
|
|
$clinic_allocation = 0;
|
|
} else {
|
|
$clinic_allocation = $request->clinic_allocation;
|
|
}
|
|
|
|
$appointment->incharge_id = $in_charge;
|
|
$appointment->clinic_allocation = $clinic_allocation;
|
|
$appointment->appointment_date = Carbon::parse($request->appointment_date)->format('Y-m-d');
|
|
$appointment->appointment_time = $request->appointment_time;
|
|
$appointment->comments = $request->comments;
|
|
$appointment->updated_by = Auth::user()->id;
|
|
|
|
if ($appointment->save()) {
|
|
flash("Patient appointment has been rescheduled successfully")->success();
|
|
return redirect("/patients/follow_up");
|
|
} else {
|
|
flash("An error occurred!")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function appointment_requests()
|
|
{
|
|
$appointments = PatientAppointment::where('is_confirmed', 0)
|
|
->whereNotIn('appointment_fulfilled', [2])
|
|
->where('appointment_date', '>', date('Y-m-d'))
|
|
->get();
|
|
|
|
return view('patients::patients.appointment_requests', compact('appointments'));
|
|
}
|
|
|
|
public function confirm_appointment($id)
|
|
{
|
|
$appointment = PatientAppointment::find($id);
|
|
$clinics = Clinic::orderBy('name')->pluck("name", "id")->toArray();
|
|
$clinics = ['0' => "Don't assign clinic"] + $clinics;
|
|
|
|
$users = User::orderBy('first_name')->select("id", "first_name", "last_name")->get();
|
|
|
|
return view('patients::patients.confirm_appointment', compact('appointment', 'clinics', 'users'));
|
|
}
|
|
|
|
public function save_confirmed_appointment(Request $request)
|
|
{
|
|
$appointment = PatientAppointment::find($request->appointment_id);
|
|
|
|
if (is_null($request->in_charge)) {
|
|
$in_charge = 0;
|
|
} else {
|
|
$in_charge = $request->in_charge;
|
|
}
|
|
|
|
if (is_null($request->clinic_allocation)) {
|
|
$clinic_allocation = 0;
|
|
} else {
|
|
$clinic_allocation = $request->clinic_allocation;
|
|
}
|
|
|
|
$appointment->incharge_id = $in_charge;
|
|
$appointment->clinic_allocation = $clinic_allocation;
|
|
$appointment->appointment_date = Carbon::parse($request->appointment_date)->format('Y-m-d');
|
|
$appointment->appointment_time = $request->appointment_time;
|
|
$appointment->comments = $request->comments;
|
|
$appointment->is_confirmed = 1;
|
|
$appointment->updated_by = auth()->id();
|
|
|
|
if ($appointment->save()) {
|
|
if (is_sms_enabled()) {
|
|
// send sms alert to the patient
|
|
$sms = "Your appointment at " . get_name(1, 'id', 'name', 'hospital_information') .
|
|
" has been confirmed for the " . streamline_date($appointment->appointment_date) . " at " . $appointment->appointment_time;
|
|
|
|
(new SMSController)->send_appointments_alert($appointment->patient_id, $sms);
|
|
}
|
|
|
|
flash("Patient appointment has been confirmed")->success();
|
|
return redirect("/patients/appointment_requests");
|
|
} else {
|
|
flash("An error occurred!")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function patient_appointments_report(Request $request)
|
|
{
|
|
$clinic_id = $request->clinic_id;
|
|
$search_by = $request->search_by;
|
|
$reg_date = $request->reg_date;
|
|
$start_date = $request->start_date;
|
|
$end_date = $request->end_date;
|
|
//dd($request->all());
|
|
$clinics = Clinic::where('available', 1)->pluck('name', 'id')->prepend(['' => 'All clinics']);
|
|
|
|
$today = Carbon::now()->toDateString();
|
|
|
|
$filters = [];
|
|
$clinic_name = Clinic::where('id', $clinic_id)->pluck('name')->first();
|
|
|
|
if ($search_by == 0) {
|
|
array_push($filters, ['appointment_date', '<', $today]);
|
|
} elseif ($search_by == 1) {
|
|
// custom date
|
|
$start_date_search = Carbon::parse($reg_date)->toDateString();
|
|
array_push($filters, ['appointment_date', '=', $start_date_search]);
|
|
} elseif ($search_by == 2) {
|
|
// custom date range
|
|
$start_date_search = Carbon::parse($start_date)->toDateString();
|
|
$end_date_search = Carbon::parse($end_date)->toDateString();
|
|
|
|
array_push($filters, ['appointment_date', '>', $start_date_search]);
|
|
array_push($filters, ['appointment_date', '<', $end_date_search]);
|
|
} else {
|
|
array_push($filters, ['appointment_date', '<', $today]);
|
|
}
|
|
|
|
if ($clinic_id == 0 || is_null($clinic_id)) {
|
|
if ($request->appointment_outcome != 'all') {
|
|
$patient_appointments = PatientAppointment::where($filters)->where(['appointment_fulfilled' => $request->appointment_outcome])->orderBy('appointment_date', 'desc')->paginate(500);
|
|
} else {
|
|
$patient_appointments = PatientAppointment::where($filters)->orderBy('appointment_date', 'desc')->paginate(500);
|
|
}
|
|
} else {
|
|
if ($request->appointment_outcome != 'all') {
|
|
$patient_appointments = PatientAppointment::where(['clinic_allocation' => $clinic_id])->where(['appointment_fulfilled' => $request->appointment_outcome])->where($filters)->orderBy('appointment_date', 'desc')->paginate(400);
|
|
} else {
|
|
$patient_appointments = PatientAppointment::where(['clinic_allocation' => $clinic_id])->where($filters)->orderBy('appointment_date', 'desc')->paginate(400);
|
|
}
|
|
}
|
|
|
|
return view('patients::patients.appointments_report', compact('clinics', 'patient_appointments'));
|
|
}
|
|
|
|
public function store_appointment_comment(Request $request)
|
|
{
|
|
$appointment_id = $request->set_appointment_id;
|
|
$appointment_comment = $request->appointment_comment;
|
|
|
|
$appointment = PatientAppointment::find($appointment_id);
|
|
$appointment->action_comment = $appointment_comment;
|
|
$appointment->action_comment_by = Auth::user()->id;
|
|
|
|
if ($appointment->save()) {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
} |