mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
720 lines
32 KiB
PHP
Executable File
720 lines
32 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Pharmacy\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\Models\Alert;
|
|
use Streamline\Models\Allergy;
|
|
use Streamline\Models\ChronicPatient;
|
|
use Streamline\Models\Drug;
|
|
use Streamline\Models\DrugCategory;
|
|
use Streamline\Models\ItemBatchWatcher;
|
|
use Streamline\Models\MaritalStatus;
|
|
use Streamline\Models\Patient;
|
|
use Streamline\Models\PatientCategory;
|
|
use Streamline\Models\PatientDocument;
|
|
use Streamline\Models\PrescriptionError;
|
|
use Streamline\Models\Treatment;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Streamline\Models\PatientEpisode;
|
|
use Streamline\Models\TreatmentOpdProgressive;
|
|
use Streamline\Models\WardTreatment;
|
|
use Streamline\Models\DosageFrequency;
|
|
use Streamline\Services\ItemsStockService;
|
|
|
|
class PrescriptionsController extends Controller {
|
|
|
|
public function __construct(
|
|
protected ItemsStockService $itemsStockService
|
|
) {}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index() {
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function create() {
|
|
if (session()->has('alter_episode_id')) {
|
|
$patient_id = session()->get('alter_patient_id');
|
|
$episode_id = session()->get('alter_episode_id');
|
|
} else {
|
|
$patient_id = session()->get('patient_id');
|
|
$episode_id = session()->get('episode_id');
|
|
}
|
|
|
|
$patient = Patient::where('id', $patient_id)->first();
|
|
$drugs = Drug::where('available', 1)->orderby('name')->pluck('name', 'id')->toArray();
|
|
$dosage_frequencies = DB::table('dosage_frequencies')->orderBy('name')->pluck('name', 'id');
|
|
$treatment = Treatment::where(['patient_id' => $patient_id, 'episode_id' => $episode_id, 'payment_status' => 0, 'dispense_status' => 0, 'inpatient_bill_generated' => 0])->first();
|
|
$dispensed_drugs = Treatment::where(['patient_id' => $patient_id, 'episode_id' => $episode_id, 'dispense_status' => 1])->get();
|
|
$paid_drugs = Treatment::where(['patient_id' => $patient_id, 'episode_id' => $episode_id, 'payment_status' => 1])->get();
|
|
$dispensed_drugs = $dispensed_drugs->merge($paid_drugs);
|
|
|
|
if (session()->get("ward_prescription") == 1) {
|
|
$treatment = false;
|
|
}
|
|
|
|
if (session()->get("tta") == 1) {
|
|
//$treatment = false;
|
|
}
|
|
|
|
// collect chronic drug ids as array
|
|
$chronic_drugs_array = ChronicPatient::where('patient_id', $patient_id)->whereNotNull('drug_id')->get(['drug_id']);
|
|
|
|
$previous_treatments = Treatment::where('patient_id',$patient_id)->where('episode_id', '!=', $episode_id)->take(5)->get();
|
|
|
|
return view('pharmacy::prescriptions.create',compact('patient_id','episode_id','patient','drugs','treatment','chronic_drugs_array', 'dispensed_drugs', 'dosage_frequencies', 'previous_treatments'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request) {
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id) {
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id) {
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id) {
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id) {
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Add episode id to prep for prescriptions
|
|
*
|
|
* @param $id
|
|
*/
|
|
public function set_episode_id($id) {
|
|
session()->put(['episode_id' => $id]);
|
|
}
|
|
|
|
/**
|
|
* Get the factor of a drug frequency
|
|
*
|
|
* @param $frequency_id
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function get_factor($frequency_id) {
|
|
$sql = "select factor as alias from dosage_frequencies where id='$frequency_id'";
|
|
$results = DB::select($sql);
|
|
$result = isset($results[0]) ? $results[0]->alias : "";
|
|
return response()->json($result);
|
|
}
|
|
|
|
/**
|
|
* Checking if the patient is allergic to a medicine
|
|
*
|
|
* @param $patient_id
|
|
* @param $drug_category_id
|
|
* @return boolean
|
|
*/
|
|
public static function checkPatientAllergies($patient_id, $drug_category_id) {
|
|
$patient_allergies = Allergy::where('patient_id' , $patient_id)->select('names')->get();
|
|
|
|
$patient_allergies_array = [];
|
|
|
|
foreach ($patient_allergies as $allergy) {
|
|
$patient_allergies_array = array_merge($patient_allergies_array, explode(",", $allergy->names));
|
|
}
|
|
|
|
if (in_array($drug_category_id, $patient_allergies_array)){
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/* check if the quantity dispensed is more than the what is in store */
|
|
|
|
public function check_dispensed_drugs_quantity(Request $request) {
|
|
if (isset($request->treatment_id) && isset($request->purchased_elsewhere)) {
|
|
// update the purchased elsewhere column of the treatment
|
|
DB::table('treatments')->where('id', $request->treatment_id)->update([
|
|
'purchased_elsewhere' => implode(",", $request->purchased_elsewhere)
|
|
]);
|
|
}
|
|
|
|
$drugs_array = $request->drug_id_array;
|
|
|
|
// check if the request has purchased elsewhere
|
|
if (isset($request->purchased_elsewhere)) {
|
|
$purchased_elsewhere_array = $request->purchased_elsewhere;
|
|
} else {
|
|
// if not just set empty array with all 0s
|
|
$purchased_elsewhere_array = array_fill(0, count($drugs_array), 0);
|
|
}
|
|
|
|
$quantity_dispensed_array = $request->quantity_dispensed_array;
|
|
$flash_message = "";
|
|
$is_over_dispensed = false;
|
|
for ($i = 0; $i < count($drugs_array); $i++) {
|
|
// check if drug is to be bought from the hospice
|
|
if ($purchased_elsewhere_array[$i] == 0) {
|
|
$drug_details = DB::table('drugs')->where('id', $drugs_array[$i])->first();
|
|
|
|
//$pharmacy_stock = $drug_details->pharmacy_stock;
|
|
//$store_stock = $drug_details->store_stock;
|
|
$pharmacy_stock = ItemBatchWatcher::where(['item_type' => 1, 'item_id' => $drugs_array[$i]])->sum('pharmacy_stock');
|
|
$store_stock = ItemBatchWatcher::where(['item_type' => 1, 'item_id' => $drugs_array[$i]])->sum('store_stock');
|
|
|
|
if (stock_levels_to_consider() == 0) {
|
|
$total_stock = (($pharmacy_stock < 0) ? 0 : $pharmacy_stock) + (($store_stock < 0) ? 0 : $store_stock);
|
|
} else {
|
|
$total_stock = (($pharmacy_stock < 0) ? 0 : $pharmacy_stock);
|
|
}
|
|
|
|
if ($total_stock < $quantity_dispensed_array[$i]) {
|
|
$is_over_dispensed = true;
|
|
$flash_message .= $drug_details->name . " only has " . $total_stock . " dispensable unit(s) in stock \n\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($is_over_dispensed) {
|
|
$can_be_prescribed = is_prescribing_out_of_stock_drugs_allowed(session()->get("ward_prescription") == 1 || session()->get("tta") == 1);
|
|
$can_be_dispensed = can_dispense_out_of_stock_drugs();
|
|
$message = [($can_be_prescribed ? "1" : "2"), $flash_message, ($can_be_dispensed ? "1" : "2")];
|
|
} else {
|
|
$message = ["0", "", "0"];
|
|
}
|
|
|
|
return $message;
|
|
}
|
|
|
|
/**
|
|
* Create new prescriptions record for patient
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|string
|
|
*/
|
|
public function process_prescription(Request $request) {
|
|
|
|
if (session()->has('alter_episode_id')) {
|
|
$patient_id = session()->get('alter_patient_id');
|
|
$episode_id = session()->get('alter_episode_id');
|
|
} else {
|
|
$patient_id = session()->get('patient_id');
|
|
$episode_id = session()->get('episode_id');
|
|
}
|
|
|
|
if (is_null($patient_id) || is_null($episode_id)) {
|
|
flash('Invalid patient record selected')->success();
|
|
return redirect('/home');
|
|
}
|
|
|
|
//1.check for any chronic drug and save in chronic_patient if the record doesnt exist in the db
|
|
//2.check if the treatment has been done and insert or update accordingly
|
|
//3.update patient_episode table with treatment details
|
|
//4.log the insertion in whichever audit table is used
|
|
$validator = Validator::make($request->all(), [
|
|
'drug_id' => 'required',
|
|
'dose' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
return back()->withErrors($validator)->withInput();
|
|
} else {
|
|
|
|
/*
|
|
* check if its a ward prescription NOTE: Kindly leave this check at this position of this script else me and u :)
|
|
*/
|
|
if (session()->get("ward_prescription") == 1) {
|
|
$this->process_ward_prescription($request);
|
|
flash("Prescription has been saved")->success();
|
|
return redirect('in_patient_sheet');
|
|
}
|
|
$drugs_array = $request->drug_id;
|
|
$duration_array = $request->item_duration;
|
|
$time_duration = [];
|
|
$purchased_elsewhere_array = [];
|
|
$purchased_elsewhere = $request->purchased_elsewhere;
|
|
|
|
// check whether treatment for episode exists and is not paid
|
|
if (isset($request->treatment_id) && get_name($request->treatment_id, 'id', 'payment_status', 'treatments') == 0) {
|
|
$treatment = Treatment::find($request->treatment_id);
|
|
$treatment->updated_by = Auth::id();
|
|
} else {
|
|
$treatment = new Treatment;
|
|
$treatment->created_by = Auth::id();
|
|
}
|
|
|
|
for ($i = 0; $i < count($drugs_array); $i++) {
|
|
if (isset($purchased_elsewhere) && count($purchased_elsewhere) > 0 && in_array($drugs_array[$i], $purchased_elsewhere)) {
|
|
$purchased_elsewhere_array[] = 1;
|
|
} else {
|
|
$purchased_elsewhere_array[] = 0;
|
|
}
|
|
|
|
if (is_drug_chronic($drugs_array[$i])) {
|
|
register_chronic_patient($patient_id, $episode_id, $drugs_array[$i]);
|
|
}
|
|
$time_duration[] = $duration_array[$i] . " " . "Days";
|
|
}
|
|
|
|
$treatment->patient_id = $patient_id;
|
|
$treatment->episode_id = $episode_id;
|
|
$treatment->drugs = implode(',', $request->drug_id);
|
|
$treatment->doses = implode(',', $request->dose);
|
|
$treatment->frequencies = implode(',', $request->frequency);
|
|
$treatment->durations = implode(',', $time_duration);
|
|
$treatment->quantities_dispensed = implode(',', $request->item_dispense);
|
|
$treatment->instruction = implode(',', $request->item_instructions);
|
|
$treatment->purchased_elsewhere = implode(',', $purchased_elsewhere_array);
|
|
$treatment->dispense_status = 0;
|
|
$treatment->eye = implode(",", $request->eye);
|
|
$treatment->order_comments = $request->order_comments;
|
|
|
|
if (!session()->has('alter_episode_id') && $request->is_progressive_treatment_setup != 1) {
|
|
$treatment->confirmation_status = 0;
|
|
}
|
|
|
|
// get tta session if available
|
|
$tta = session()->get("tta");
|
|
$treatment->tta = isset($tta) ? 1 : 0;
|
|
// unset the session for tta
|
|
session()->forget('tta');
|
|
|
|
$treatment->save();
|
|
|
|
// save the progressive treatment if available
|
|
if ($request->is_progressive_treatment_setup == 1) {
|
|
if (isset($request->treatment_opd_progressive_id) && $request->treatment_opd_progressive_id != 0) {
|
|
$prog_treatment_opd = TreatmentOpdProgressive::find($request->treatment_opd_progressive_id);
|
|
} else {
|
|
$prog_treatment_opd = new TreatmentOpdProgressive();
|
|
}
|
|
|
|
$prog_treatment_opd->patient_id = $treatment->patient_id;
|
|
$prog_treatment_opd->episode_id = $treatment->episode_id;
|
|
$prog_treatment_opd->initial_treatment_id = $treatment->id;
|
|
$prog_treatment_opd->treatment_ids = $treatment->id;
|
|
$prog_treatment_opd->is_first_treatment_given = 1;
|
|
$prog_treatment_opd->drugs = $treatment->drugs;
|
|
$prog_treatment_opd->dose = $treatment->doses;
|
|
$prog_treatment_opd->drug_frequency = $treatment->frequencies;
|
|
$prog_treatment_opd->durations = $treatment->durations;
|
|
$prog_treatment_opd->quantities_to_dispense = $treatment->quantities_dispensed;
|
|
$prog_treatment_opd->instruction = $treatment->instruction;
|
|
$prog_treatment_opd->created_by = Auth::id();
|
|
$prog_treatment_opd->prescribed_by = $treatment->created_by;
|
|
$prog_treatment_opd->order_comments = $treatment->order_comments;
|
|
$prog_treatment_opd->save();
|
|
|
|
// create the first dose to dispense
|
|
$time_duration = [];
|
|
$first_dose_quantity_dispensed = [];
|
|
$first_dose_quantity = $request->first_dose_quantity;
|
|
$first_dose_duration = $request->first_dose_duration;
|
|
for ($i = 0; $i < count($drugs_array); $i++) {
|
|
$time_duration[] = ($first_dose_duration[$i] ?? 1) . " Days";
|
|
$first_dose_quantity_dispensed[] = $first_dose_quantity[$i] ?? 1;
|
|
}
|
|
|
|
$assigned_treatment = Treatment::find($treatment->id);
|
|
$assigned_treatment->durations = implode(',', $time_duration);
|
|
$assigned_treatment->quantities_dispensed = implode(',', $first_dose_quantity_dispensed);
|
|
$assigned_treatment->is_treatment_progressive = $prog_treatment_opd->id;
|
|
$assigned_treatment->save();
|
|
} elseif (isset($request->treatment_opd_progressive_id) && $request->treatment_opd_progressive_id != 0) {
|
|
// if the above is not set but this is set that means the progressive treatment has been removed so we should delete
|
|
$prog_treatment_opd = TreatmentOpdProgressive::find($request->treatment_opd_progressive_id);
|
|
$prog_treatment_opd->forceDelete();
|
|
|
|
$assigned_treatment = Treatment::find($treatment->id);
|
|
$assigned_treatment->is_treatment_progressive = 0;
|
|
$assigned_treatment->save();
|
|
}
|
|
|
|
if (session()->has('alter_episode_id')) {
|
|
$type_of_error = [];
|
|
$type_of_error[] = isset($request->wrong_dosage) ? "Wrong dosage" : "";
|
|
$type_of_error[] = isset($request->wrong_frequency) ? "Wrong frequency" : "";
|
|
$type_of_error[] = isset($request->inadequate_funds) ? "Inadequate Funds" : "";
|
|
$type_of_error[] = isset($request->other_error) ? "Other error" : "";
|
|
$type_of_error = implode(',', $type_of_error);
|
|
|
|
$severity_of_error = $request->severity_of_error;
|
|
|
|
// inserting the prescription errors
|
|
$prescription_error = new PrescriptionError;
|
|
$prescription_error->patient_id = $patient_id;
|
|
$prescription_error->treatment_id = $treatment->id;
|
|
$prescription_error->patient_dispensings_id = 0;
|
|
$prescription_error->type_of_error = $type_of_error;
|
|
$prescription_error->severity_of_error = $severity_of_error;
|
|
$prescription_error->prescriber = $treatment->created_by;
|
|
$prescription_error->pharmacist = Auth::id();
|
|
$prescription_error->created_by = Auth::id();
|
|
$prescription_error->previous_drugs = get_name($treatment->id, 'id', 'drugs', 'treatments');
|
|
$prescription_error->previous_doses = get_name($treatment->id, 'id', 'doses', 'treatments');
|
|
$prescription_error->previous_frequencies = get_name($treatment->id, 'id', 'frequencies', 'treatments');
|
|
$prescription_error->previous_durations = get_name($treatment->id, 'id', 'durations', 'treatments');
|
|
$prescription_error->previous_quantities_dispensed = get_name($treatment->id, 'id', 'quantities_dispensed', 'treatments');
|
|
$prescription_error->previous_instruction = get_name($treatment->id, 'id', 'instruction', 'treatments');
|
|
$prescription_error->comment = $request->comment;
|
|
$prescription_error->save();
|
|
|
|
if ($request->get('submit-btn') == "Alter and Confirm Prescription") {
|
|
$treatment->confirmation_status = 1;
|
|
}
|
|
|
|
$treatment->altered = $prescription_error->id;
|
|
$treatment->is_new_alter_model = 1;
|
|
|
|
$treatment->save();
|
|
session()->forget('alter_episode_id');
|
|
session()->forget('alter_patient_id');
|
|
|
|
flash("Treatment has been altered.")->success();
|
|
return redirect('/confirm_prescriptions');
|
|
}
|
|
|
|
// update the patient episode table with the id of the saved treatment
|
|
if ($treatment->tta == 0 && !isset($request->treatment_id)):
|
|
$treatment_id = Treatment::where(['patient_id' => $patient_id, 'episode_id' => $episode_id, 'drugs' => $treatment->drugs])->pluck('id')->first();
|
|
$episode = PatientEpisode::find($episode_id);
|
|
$episode->treatment_id = $treatment_id;
|
|
$episode->update();
|
|
endif;
|
|
|
|
// create an insurance claim for the ordered items
|
|
if ($request->patient_insurance_status == 1) {
|
|
generate_insurance_claim($treatment->id, 4);
|
|
}
|
|
|
|
// redirect to the consultation page if it wasn't a tta or to the in-patient sheet if it was a tta
|
|
flash("Prescription has been saved")->success();
|
|
|
|
if ($treatment->tta == 1) {
|
|
return redirect('in_patient_sheet');
|
|
} else {
|
|
if (session()->has('redirect_to_consultation')) {
|
|
$url = session()->get('redirect_to_consultation');
|
|
session()->forget('redirect_to_consultation');
|
|
return redirect($url);
|
|
} elseif (session()->has('anc_visit_redirect')) {
|
|
$url = session()->get('anc_visit_redirect');
|
|
session()->forget('anc_visit_redirect');
|
|
return redirect($url);
|
|
} else {
|
|
if(session()->has('previous_action') && session()->get('previous_action') == 'anc_consultation_page') {
|
|
session()->forget('previous_action');
|
|
return redirect("ante_natal_clinic/route");
|
|
}
|
|
else return redirect("/patient_episodes");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Print prescriptions list
|
|
*
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function print_treatment() {
|
|
$patient_id = session()->get("patient_id");
|
|
$episode_id = session()->get("episode_id");
|
|
$patient = Patient::where('id', $patient_id)->first();
|
|
$categories = PatientCategory::pluck("name", "id");
|
|
$marital_statuses = MaritalStatus::pluck("name", "id");
|
|
$drug_categories = DrugCategory::get();
|
|
$drugs = Drug::where('available', 1)->orderby('name')->pluck('name', 'id');
|
|
$allergies = Allergy::where(['patient_id' => $patient_id])->pluck('patient_id', 'names');
|
|
|
|
$treatment = Treatment::where(["patient_id" => $patient_id, "episode_id" => $episode_id])->first(); //maybe check id too
|
|
return view('pharmacy::prescriptions.print_treatment', compact('patient_id', 'episode_id', 'patient', 'categories', 'marital_statuses', 'drug_categories', 'drugs', 'allergies', 'treatment'));
|
|
}
|
|
|
|
/*
|
|
* process prescription that has been ordered from the inpatient sheet
|
|
*/
|
|
public function process_ward_prescription(Request $request) {
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'drug_id' => 'required',
|
|
'dose' => 'required',
|
|
]);
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
return back()->withErrors($validator)->withInput();
|
|
} else {
|
|
|
|
$drugs_array = $request->drug_id;
|
|
$duration_array = $request->item_duration;
|
|
$time_duration = [];
|
|
|
|
$patient_id = session()->get("patient_id");
|
|
$episode_id = session()->get("episode_id");
|
|
|
|
// check whether treatment for episode exists and is not paid
|
|
if (isset($request->treatment_id) && get_name($request->treatment_id, 'id', 'payment_status', 'ward_treatments') == 0) {
|
|
$ward_treatment = WardTreatment::find($request->treatment_id);
|
|
} else {
|
|
$ward_treatment = new WardTreatment;
|
|
}
|
|
|
|
for ($i = 0; $i < count($drugs_array); $i++) {
|
|
if (is_drug_chronic($drugs_array[$i])) {
|
|
register_chronic_patient($patient_id, $episode_id, $drugs_array[$i]);
|
|
}
|
|
|
|
$time_duration[] = $duration_array[$i] . " Days";
|
|
}
|
|
|
|
$ward_treatment->patient_id = $patient_id;
|
|
$ward_treatment->episode_id = $episode_id;
|
|
$ward_treatment->ward_id = session()->get("ward_id");
|
|
session()->forget('ward_id');
|
|
$ward_treatment->drugs = implode(',', $request->drug_id);
|
|
$ward_treatment->doses = implode(',', $request->dose);
|
|
$ward_treatment->frequencies = implode(',', $request->frequency);
|
|
$ward_treatment->durations = implode(',', $time_duration);
|
|
$ward_treatment->quantities_dispensed = implode(',', $request->item_dispense);
|
|
$ward_treatment->instruction = implode(',', $request->item_instructions);
|
|
$ward_treatment->eye = implode(',', $request->eye);
|
|
$ward_treatment->dispense_status = null;
|
|
|
|
// get tta session if available
|
|
$ward_prescription = session()->get("ward_prescription");
|
|
// unset the session for ward prescription
|
|
session()->forget('ward_prescription');
|
|
|
|
$ward_treatment->created_by = Auth::id();
|
|
|
|
// check whether treatment for episode exists and is not paid
|
|
if (isset($request->treatment_id) && get_name($request->treatment_id, 'id','payment_status','ward_treatments') == 0) {
|
|
$ward_treatment->update();
|
|
} else {
|
|
$ward_treatment->save();
|
|
}
|
|
|
|
// update the patient episode table with the id of the saved treatment
|
|
if (isset($request->treatment_id)) {
|
|
$ward_treatment_id = WardTreatment::where(['patient_id' => $patient_id, 'episode_id' => $episode_id, 'drugs' => $ward_treatment->drugs])->pluck('id')->first();
|
|
$episode = PatientEpisode::find($episode_id);
|
|
$episode->treatment_id = $ward_treatment_id;
|
|
$episode->update();
|
|
}
|
|
|
|
flash("Prescription has been saved")->success();
|
|
return redirect('in_patient_sheet');
|
|
}
|
|
}
|
|
|
|
public function drug_refill(Request $request)
|
|
{
|
|
$patient_id = session()->get('patient_id');
|
|
$episode_id = session()->get('episode_id');
|
|
|
|
$patient = Patient::find($patient_id);
|
|
|
|
$dispensed_drugs_from_parent_episode = Treatment::where(['patient_id' => $patient_id, 'episode_id' => $episode_id, 'dispense_status' => 1])->get();
|
|
if (count($dispensed_drugs_from_parent_episode) == 0) {
|
|
flash("No treatment was given during this episode so you can not do a drug refill")->error();
|
|
return redirect()->back();
|
|
}
|
|
|
|
/*==== create a new episode but attached to the original epsiode as appointment is started/activated ========*/
|
|
$episode = new PatientEpisode;
|
|
$episode->patient_id = $patient_id;
|
|
$episode->clinic_id = get_default_hospital_clinic();
|
|
$episode->parent_episode_id = $episode_id;//the original episode_id
|
|
$episode->created_by = Auth::id();
|
|
$episode->updated_by = Auth::id();
|
|
|
|
try {
|
|
$episode->save();
|
|
//put the newly created episode into session
|
|
session()->put('episode_id', $episode->id);
|
|
|
|
flash("A new refill episode has been created")->success();
|
|
|
|
// collect chronic drug ids as array
|
|
return redirect('create_drug_refill');
|
|
} catch (QueryException $e) {
|
|
flash("An error occurred")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function create_drug_refill()
|
|
{
|
|
$patient_id = session()->get('patient_id');
|
|
$episode_id = session()->get('episode_id');
|
|
|
|
$patient = Patient::where('id', $patient_id)->first();
|
|
$epsiode_details = PatientEpisode::find($episode_id);
|
|
$parent_episode_id = $epsiode_details->parent_episode_id;
|
|
|
|
$treatment_collection = Treatment::where(['patient_id' => $patient_id, 'episode_id' => $episode_id, 'payment_status' => 0, 'dispense_status' => 0, 'inpatient_bill_generated' => 0])->get();
|
|
$dispensed_drugs = Treatment::where(['patient_id' => $patient_id, 'episode_id' => $episode_id, 'dispense_status' => 1])->get();
|
|
$dispensed_drugs_from_parent_episode = Treatment::where(['patient_id' => $patient_id, 'episode_id' => $parent_episode_id, 'dispense_status' => 1])->get();
|
|
$treatment = count($treatment_collection) > 0 ? $treatment_collection->first() : false;
|
|
|
|
|
|
$categories = PatientCategory::pluck("name", "id");
|
|
$marital_statuses = MaritalStatus::pluck("name", "id");
|
|
$drug_categories = DrugCategory::get();
|
|
$drugs = Drug::where('available', 1)->orderby('name')->pluck('name', 'id');
|
|
$patient_prescriptions = null;
|
|
$chronic_drugs_array = ChronicPatient::where('patient_id', $patient_id)->whereNotNull('drug_id')->select('drug_id')->get()->toArray();
|
|
$chronic_results = Drug::whereIn('id', $chronic_drugs_array)->get();
|
|
$documents = PatientDocument::where('patient_id',$patient_id)->orderBy('created_at','desc')->take(2)->get();
|
|
$known_patient_allergies = Allergy::where('patient_id' , $patient_id)->orderBy('created_at','desc')->take(2)->get();
|
|
$known_patient_alerts = Alert::where('patient_id' , $patient_id)->orderBy('created_at','desc')->take(2)->get();
|
|
$drug_categories_array = DrugCategory::pluck('name', 'id');
|
|
$dosage_frequencies = DosageFrequency::orderBy('name')->select("id", "name")->get();
|
|
|
|
return view('pharmacy::prescriptions.drug_refill',compact('patient_id','episode_id','patient','categories','marital_statuses','drug_categories','patient_prescriptions','drugs','treatment','chronic_results','documents','known_patient_alerts','known_patient_allergies','drug_categories_array', 'dispensed_drugs','dispensed_drugs_from_parent_episode','parent_episode_id','dosage_frequencies'));
|
|
}
|
|
|
|
public function get_drug_details(Request $request) {
|
|
$drug_id = $request->drug_id;
|
|
$patient_id = $request->patient_id;
|
|
$patient_insurance_status = $request->patient_insurance_status ?? 0;
|
|
|
|
$drug = DB::table('drugs')->find($drug_id);
|
|
$patient_is_allergic = $this->checkPatientAllergies($patient_id, $drug->category_id);
|
|
$can_drug_be_prescribed = !$patient_is_allergic;
|
|
|
|
if (stock_levels_to_consider() == 0) {
|
|
$total_stock = $this->itemsStockService->getItemAllQuantityByTotal($drug_id, 1);
|
|
} else {
|
|
$total_stock = $this->itemsStockService->getItemPharmacyQuantity($drug_id, 1);
|
|
}
|
|
|
|
$drug_out_of_stock = $total_stock < 1;
|
|
|
|
if (!is_chi_enabled() && !is_drug_covered_by_pay_later_category(get_name($patient_id, 'id', 'category_id', 'patients'), $drug->id)) {
|
|
$covered_by_pat_cat = "Not Covered by " . get_name(get_name($patient_id, 'id', 'category_id', 'patients'), 'id', 'name', 'patient_categories');
|
|
} else {
|
|
$covered_by_pat_cat = "";
|
|
}
|
|
|
|
$drug_expiry_date = $this->itemsStockService->getItemLatestExpiryDate($drug_id, 1) ?? $drug->expiry_date;
|
|
if (Carbon::createFromFormat('Y-m-d', $drug_expiry_date)->isBefore(Carbon::tomorrow())) {
|
|
$drug_is_expired = 1;
|
|
} else {
|
|
$drug_is_expired = 0;
|
|
}
|
|
|
|
if (is_chi_enabled()) {
|
|
if (is_patient_item_covered($patient_id, $drug->id, 4)) {
|
|
$covered_by_chi = ' <br><span style="color: darkgreen"><b>Covered by CHI</b></span>';
|
|
} else {
|
|
$covered_by_chi = ' <br><span style="color: darkorange"><b>Not covered by CHI</b></span>';
|
|
}
|
|
} else {
|
|
$covered_by_chi = "";
|
|
}
|
|
|
|
if(is_numeric($patient_insurance_status) && $patient_insurance_status == 1 && $patient_id != 0 && is_numeric($patient_id)) {
|
|
$selling_price = get_item_insurance_co_payment($patient_id, $drug->id, 4, false, 0);
|
|
} else {
|
|
// check if the patient category is attached to a price list
|
|
$price_list_id = is_patient_category_attached_to_price_list($patient_id);
|
|
if ($price_list_id) {
|
|
$selling_price = get_price_list_category_price($price_list_id, 3, $drug->id);
|
|
} else {
|
|
$selling_price = get_name($drug->id, "id", "non_insured_price", "drugs");
|
|
}
|
|
}
|
|
|
|
if ($drug->prompt) {
|
|
$prompt = "<div style='padding: 3px 3px 3px 4px; color: #C85F6A;' class='well margin-none span12'>";
|
|
$prompt .= read_more($drug->prompt, 'prompt_short' . $drug->id, 'prompt_long' . $drug->id);
|
|
$prompt .= "<div id='prompt_long" . $drug->id . "' style='display: none;'>" . $drug->prompt;
|
|
$prompt .= "<br/><a class='read_more' style='color : #0099CC;' onclick= 'hide(\"prompt_long" . $drug->id . "\");show(\"prompt_short" . $drug->id . "\");'>Read Less</a></div></div>";
|
|
} else {
|
|
$prompt = 0;
|
|
}
|
|
|
|
$drug_references = "";
|
|
$reference_array = explode(",", $drug->reference_areas);
|
|
$reference_name = explode(",", $drug->reference_name);
|
|
for ($x = 0; $x < count($reference_array); $x++){
|
|
if(isset($reference_array[$x]) && isset($reference_name[$x])){
|
|
$drug_references = "<a style='font-weight:normal;' href='".$reference_array[$x]."' target='_blank'>".$reference_name[$x]."</a><br>";
|
|
}
|
|
}
|
|
|
|
return json_encode([
|
|
"pharmacy_comment" => ($drug->pharmacy_comment) ?: 0,
|
|
"drug_references" => $drug_references,
|
|
"is_covered" => 1,
|
|
"prompt" => $prompt,
|
|
"drug_form" => get_name($drug->form_id, 'id', 'name', 'drug_forms'),
|
|
"drug_unit" => get_name($drug->unit_id, 'id', 'name', 'drug_units'),
|
|
"can_drug_be_prescribed" => ($can_drug_be_prescribed) ? 1 : 0,
|
|
"patient_is_allergic" => ($patient_is_allergic) ? 1 : 0,
|
|
"drug_out_of_stock" => ($drug_out_of_stock) ? 1 : 0,
|
|
"selling_price" => $selling_price,
|
|
"pack" => $drug->pack,
|
|
"strength" => $drug->strength,
|
|
"covered_by_pat_cat" => $covered_by_pat_cat,
|
|
"covered_by_chi" => $covered_by_chi,
|
|
"drug_is_expired" => $drug_is_expired,
|
|
]);
|
|
}
|
|
|
|
}
|