Files
streamline-emr/docker/streamline-src/Modules/WardManagement/Http/Controllers/TreatmentSheetController.php
T

301 lines
15 KiB
PHP

<?php
namespace Modules\WardManagement\Http\Controllers;
use Barryvdh\Snappy\Facades\SnappyPdf;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
use Modules\Cancer\Http\Controllers\CancerProtocolController;
use Modules\ClinicalData\Services\Drugs\DrugFormsService;
use Modules\ClinicalData\Services\Drugs\DrugRoutesService;
use Modules\ClinicalData\Services\Drugs\DrugsService;
use Modules\ClinicalData\Services\Drugs\DrugUnitsService;
use Modules\WardManagement\Services\TreatmentSheetService;
use Streamline\Models\OrderedCancerProtocols;
use Streamline\Models\Patient;
use Streamline\Models\TreatmentSheetDispensations;
use Streamline\Models\User;
use Streamline\Models\WardTreatment;
use Streamline\Models\WardTreatmentDispensation;
use Streamline\Services\UserService;
use Streamline\Services\WardManagement\WardTreatmentDispensationService;
class TreatmentSheetController extends Controller
{
public function __construct(
protected TreatmentSheetService $treatmentSheetService,
protected DrugsService $drugsService,
protected DrugRoutesService $drugRoutesService,
protected DrugUnitsService $drugUnitsService,
protected UserService $userService,
protected DrugFormsService $drugFormsService,
protected WardTreatmentDispensationService $dispensationService
) {
$this->middleware('auth');
}
public function viewTreatmentSheet(): View|RedirectResponse
{
$patient_id = session()->get('patient_id');
$episode_id = session()->get('episode_id');
$inpatient_info = DB::table('inpatient_info')->where('episode_id', $episode_id)->first();
if ($inpatient_info) {
$ward_id = $inpatient_info->ward_id;
} else {
return redirect('home');
}
$patient = Patient::find($patient_id);
$ordered_cancer_protocols = DB::table('ordered_cancer_protocols')
->whereNull('deleted_at')->where('episode_id', $episode_id)->get();
$drugs = $this->drugsService->pluckAvailableDrugs('name');
$drug_with_units = $this->drugsService->pluckAvailableDrugs('unit_id');
$drug_units = $this->drugUnitsService->pluckAvailableDrugUnits('name');
$drug_routes = $this->drugRoutesService->pluckAvailableDrugRoutes('name');
$drug_with_forms = $this->drugsService->pluckAvailableDrugs('form_id');
$drug_forms = $this->drugFormsService->pluckAvailableDrugForms('name');
$factors = CancerProtocolController::$factors;
$ward_prescriptions = WardTreatment::where('episode_id', $episode_id)->get();
$users_array = $this->userService->pluckUserFullName();
$users_array = ['' => '- select -'] + $users_array;
return view('ward_management::inpatient.treatment_sheet_view', compact('patient', 'episode_id', 'patient_id', 'inpatient_info',
'ordered_cancer_protocols', 'drugs', 'drug_with_units', 'drug_units', 'drug_routes', 'factors', 'ward_prescriptions', 'users_array',
'ward_id', 'drug_forms', 'drug_with_forms'));
}
public function saveTreatmentSheet(Request $request) {
$pre_chemo_given_on = $request->pre_chemo_given_on ?? [];
$pre_chemo_tracker_num = $request->pre_chemo_tracker_num ?? [];
$pre_chemo_given_by = $request->pre_chemo_given_by ?? [];
$pre_chemo_quantity_given = $request->pre_chemo_quantity_given ?? [];
$pre_chemo_order_id = $request->pre_chemo_order_id ?? [];
$chemo_given_on = $request->chemo_given_on ?? [];
$chemo_tracker_num = $request->chemo_tracker_num ?? [];
$chemo_given_by = $request->chemo_given_by ?? [];
$chemo_quantity_given = $request->chemo_quantity_given ?? [];
$chemo_order_id = $request->chemo_order_id ?? [];
$post_chemo_given_on = $request->post_chemo_given_on ?? [];
$post_chemo_tracker_num = $request->post_chemo_tracker_num ?? [];
$post_chemo_given_by = $request->post_chemo_given_by ?? [];
$post_chemo_quantity_given = $request->post_chemo_quantity_given ?? [];
$post_chemo_order_id = $request->post_chemo_order_id ?? [];
$patient_id = $request->patient_id;
$episode_id = $request->episode_id;
$ward_id = $request->ward_id;
$grouped_items = [];
$duration_counter = 0;
for ($x = 0; $x < count($pre_chemo_order_id); $x++) {
if (isset($pre_chemo_given_by[$x]) && isset($pre_chemo_given_on[$x]) && $pre_chemo_quantity_given[$x] > 0) {
$grouped_items[$pre_chemo_order_id[$x]]['pre_chemo'][$pre_chemo_tracker_num[$x]] = [
"pre_chemo_given_on" => $pre_chemo_given_on[$x],
"pre_chemo_given_by" => $pre_chemo_given_by[$x],
"pre_chemo_quantity_given" => $pre_chemo_quantity_given[$x]
];
}
}
for ($x = 0; $x < count($chemo_order_id); $x++) {
if (isset($chemo_given_by[$x]) && isset($chemo_given_on[$x]) && $chemo_quantity_given[$x] > 0) {
$grouped_items[$chemo_order_id[$x]]['chemo'][$chemo_tracker_num[$x]] = [
"chemo_given_on" => $chemo_given_on[$x],
"chemo_given_by" => $chemo_given_by[$x],
"chemo_quantity_given" => $chemo_quantity_given[$x]
];
}
}
for ($x = 0; $x < count($post_chemo_order_id); $x++) {
if (isset($post_chemo_given_by[$x]) && isset($post_chemo_given_on[$x]) && $post_chemo_quantity_given[$x] > 0) {
$grouped_items[$post_chemo_order_id[$x]]['post_chemo'][$post_chemo_tracker_num[$x]] = [
"post_chemo_given_on" => $post_chemo_given_on[$x],
"post_chemo_given_by" => $post_chemo_given_by[$x],
"post_chemo_quantity_given" => $post_chemo_quantity_given[$x]
];
}
}
foreach ($grouped_items as $key => $grouped_item) {
$ordered_protocol = OrderedCancerProtocols::find($key);
if ($ordered_protocol) {
$pre_chemo_drugs = json_decode($ordered_protocol->pre_chemo_drugs, true);
for($x = 0; $x < count($pre_chemo_drugs); $x++) {
for($i = 0; $i < count($pre_chemo_drugs[$x]['dose']); $i++) {
for($p = 0; $p < count($pre_chemo_drugs[$x]['dose'][$i]['duration']); $p++) {
if (isset($grouped_item['pre_chemo']) && isset($grouped_item['pre_chemo'][$duration_counter])) {
$pre_chemo_drugs[$x]['dose'][$i]['duration'][$p]['given_by'] = $grouped_item['pre_chemo'][$duration_counter]['pre_chemo_given_by'];
$pre_chemo_drugs[$x]['dose'][$i]['duration'][$p]['given_on'] = $grouped_item['pre_chemo'][$duration_counter]['pre_chemo_given_on'];
$pre_chemo_drugs[$x]['dose'][$i]['duration'][$p]['quantity_given'] = $grouped_item['pre_chemo'][$duration_counter]['pre_chemo_quantity_given'];
$this->dispensationService->saveDispensation($patient_id, $episode_id, $pre_chemo_drugs[$x]['drug_id'], $grouped_item['pre_chemo'][$duration_counter]['pre_chemo_quantity_given'], 0, $ward_id);
$ordered_protocol->pre_chemo_status = 1;
}
$duration_counter++;
}
}
}
$ordered_protocol->pre_chemo_drugs = json_encode($pre_chemo_drugs);
$chemo_drugs = json_decode($ordered_protocol->chemo_drugs, true);
for($x = 0; $x < count($chemo_drugs); $x++) {
for($i = 0; $i < count($chemo_drugs[$x]['dose']); $i++) {
for($p = 0; $p < count($chemo_drugs[$x]['dose'][$i]['duration']); $p++) {
if (isset($grouped_item['chemo']) && isset($grouped_item['chemo'][$duration_counter])) {
$chemo_drugs[$x]['dose'][$i]['duration'][$p]['given_by'] = $grouped_item['chemo'][$duration_counter]['chemo_given_by'];
$chemo_drugs[$x]['dose'][$i]['duration'][$p]['given_on'] = $grouped_item['chemo'][$duration_counter]['chemo_given_on'];
$chemo_drugs[$x]['dose'][$i]['duration'][$p]['quantity_given'] = $grouped_item['chemo'][$duration_counter]['chemo_quantity_given'];
$this->dispensationService->saveDispensation($patient_id, $episode_id, $chemo_drugs[$x]['drug_id'], $grouped_item['chemo'][$duration_counter]['chemo_quantity_given'], 0, $ward_id);
$ordered_protocol->chemo_status = 1;
}
$duration_counter++;
}
}
}
$ordered_protocol->chemo_drugs = json_encode($chemo_drugs);
$post_chemo_drugs = json_decode($ordered_protocol->post_chemo_drugs, true);
for($x = 0; $x < count($post_chemo_drugs); $x++) {
for($i = 0; $i < count($post_chemo_drugs[$x]['dose']); $i++) {
for($p = 0; $p < count($post_chemo_drugs[$x]['dose'][$i]['duration']); $p++) {
if (isset($grouped_item['post_chemo']) && isset($grouped_item['post_chemo'][$duration_counter])) {
$post_chemo_drugs[$x]['dose'][$i]['duration'][$p]['given_by'] = $grouped_item['post_chemo'][$duration_counter]['post_chemo_given_by'];
$post_chemo_drugs[$x]['dose'][$i]['duration'][$p]['given_on'] = $grouped_item['post_chemo'][$duration_counter]['post_chemo_given_on'];
$post_chemo_drugs[$x]['dose'][$i]['duration'][$p]['quantity_given'] = $grouped_item['post_chemo'][$duration_counter]['post_chemo_quantity_given'];
$this->dispensationService->saveDispensation($patient_id, $episode_id, $post_chemo_drugs[$x]['drug_id'], $grouped_item['post_chemo'][$duration_counter]['post_chemo_quantity_given'], 0, $ward_id);
$ordered_protocol->post_chemo_status = 1;
}
$duration_counter++;
}
}
}
$ordered_protocol->post_chemo_drugs = json_encode($post_chemo_drugs);
try {
$ordered_protocol->save();
} catch (QueryException $e) {
return 0;
}
}
}
// handle the rest of the treatments
$opd_given_by = $request->opd_given_by ?? [];
$opd_given_on = $request->opd_given_on ?? [];
$opd_order_number = $request->opd_order_number ?? [];
$opd_ward_treatment_id = $request->opd_ward_treatment_id ?? [];
$opd_drug_id = $request->opd_drug_id ?? [];
$opd_quantity_given = $request->opd_quantity_given ?? [];
for ($x = 0; $x < count($opd_given_by); $x++) {
if (isset($opd_given_by[$x]) && isset($opd_given_on[$x])) {
$dispensations = new TreatmentSheetDispensations();
$dispensations->patient_id = $patient_id;
$dispensations->episode_id = $episode_id;
$dispensations->ward_treatment_id = $opd_ward_treatment_id[$x];
$dispensations->order_number = $opd_order_number[$x];
$dispensations->drug_id = $opd_drug_id[$x];
$dispensations->quantity_given = $opd_quantity_given[$x];
$dispensations->given_by = $opd_given_by[$x];
$dispensations->given_on = $opd_given_on[$x];
$dispensations->created_by = Auth::id();
$dispensations->save();
$this->dispensationService->saveDispensation($patient_id, $episode_id, $opd_drug_id[$x], $opd_quantity_given[$x], 0, $ward_id);
}
}
if (session()->has('treatment_sheet_route')) {
$treatment_sheet_route = session()->get('treatment_sheet_route');
session()->forget('treatment_sheet_route');
return $treatment_sheet_route;
} else {
return "self";
}
}
public function printTreatmentSheet($episode_id)
{
$ordered_cancer_protocols = DB::table('ordered_cancer_protocols')
->whereNull('deleted_at')->where('episode_id', $episode_id)->get();
$ward_prescriptions = WardTreatment::where('episode_id', $episode_id)->get();
$drugs = $this->drugsService->pluckAvailableDrugs('name');
$drug_with_units = $this->drugsService->pluckAvailableDrugs('unit_id');
$drug_units = $this->drugUnitsService->pluckAvailableDrugUnits('name');
$drug_routes = $this->drugRoutesService->pluckAvailableDrugRoutes('name');
$factors = CancerProtocolController::$factors;
$inpatient_info = DB::table('inpatient_info')->where('episode_id', $episode_id)->first();
if ($inpatient_info) {
$patient_id = $inpatient_info->patient_id;
$ward_id = $inpatient_info->ward_id;
$admitted_on = $inpatient_info->admitted_on;
} else {
return redirect('home');
}
$patient = Patient::find($patient_id);
$users_array = $this->userService->pluckUserFullName();
$users_array = ['' => '- select -'] + $users_array;
$data = [
"patient" => $patient,
"users_array" => $users_array,
"ordered_cancer_protocols" => $ordered_cancer_protocols,
"ward_prescriptions" => $ward_prescriptions,
"factors" => $factors,
"drug_routes" => $drug_routes,
"drug_with_units" => $drug_with_units,
"drug_units" => $drug_units,
"drugs" => $drugs,
"ward_id" => $ward_id,
"admitted_on" => $admitted_on,
];
$pdf = SnappyPDF::loadView('ward_management::inpatient/print_treatment_sheet', $data)
->setOrientation('portrait')
->setOption('margin-bottom', 7)
->setOption('margin-top', 5)
->setOption('footer-html', '<i>Stre@mline</i>');
return $pdf->inline('Inpatient Bill' . date(" d-m-y h:ia") . '.pdf');
}
public function redirectBack(): RedirectResponse
{
$treatment_sheet_route = session()->get('treatment_sheet_route');
session()->forget('treatment_sheet_route');
if ($treatment_sheet_route === "ward_home") {
return redirect('/wards/select');
} else {
return redirect('/patient_episodes');
}
}
}