Files
streamline-emr/docker/streamline-src/Modules/Investigations/Http/Controllers/InvestigationSpecialisedVariableController.php

251 lines
9.6 KiB
PHP
Executable File

<?php
namespace Modules\Investigations\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Streamline\Models\Investigation;
use Streamline\Models\InvestigationNormalRange;
use Streamline\Models\InvestigationSpecialisedVariable;
use Illuminate\Http\Request;
use Streamline\Models\UnitOfMeasure;
use Illuminate\Database\QueryException;
class InvestigationSpecialisedVariableController extends Controller
{
/**create
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$investigation_variables = InvestigationSpecialisedVariable::orderBy('name', 'asc')->get();
$investigations = Investigation::get();
$units = UnitOfMeasure::get();
return view('investigations::investigation_specialised_variables.index', compact('investigation_variables', 'investigations', 'units'));
}
/**
* Show the Investigation Variable for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
$investigations = Investigation::get();
$units = UnitOfMeasure::get();
return view('investigations::investigation_specialised_variables.create', compact('investigations', 'units'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
request()->validate([
'name' => 'required'
]);
$logged_in_user_id = Auth::user()->id;
$investigation_specialised_variable = new InvestigationSpecialisedVariable;
$investigation_specialised_variable->name = $request->name;
$investigation_specialised_variable->investigation_id = $request->investigation;
$investigation_specialised_variable->units = $request->unit;
$investigation_specialised_variable->normal_ranges = $request->normal_ranges;
$investigation_specialised_variable->range_type = $request->range_type;
$investigation_specialised_variable->created_by = $logged_in_user_id;
$investigation_specialised_variable->updated_by = $logged_in_user_id;
$investigation = Investigation::find($request->investigation);
$investigation->type = 1;
try {
$investigation_specialised_variable->save();
$investigation->save();
if ($request->range_type == 1) {
$age_id = $request->age_id;
$dynamic_range_male = $request->dynamic_range_male;
$dynamic_range_female = $request->dynamic_range_female;
for ($x = 0; $x < count($age_id); $x++){
$new_range = new InvestigationNormalRange;
$new_range->male_range = $dynamic_range_male[$x];
$new_range->female_range = $dynamic_range_female[$x];
$new_range->age_group_id = $age_id[$x];
$new_range->test_id = $investigation_specialised_variable->id;
$new_range->is_specialised_variable = 1;
$new_range->created_by = $logged_in_user_id;
$new_range->save();
}
}
flash($request->name . "Investigation Specialised Variable has been saved")->success();
return redirect("/investigation_variables/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
/**
* 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) {
$investigations = Investigation::get();
$units = UnitOfMeasure::get();
$investigation_specialised_variable = InvestigationSpecialisedVariable::where(['id' => $id])->first();
if (!$investigation_specialised_variable) {
flash()->error("Drug Investigation Variable not found");
return redirect('/investigation_variables/');
} else {
return view('investigations::investigation_specialised_variables.edit', compact('investigation_specialised_variable', 'investigations', 'units'));
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
request()->validate([
'name' => 'required'
]);
$logged_in_user_id = Auth::user()->id;
$investigation_specialised_variable = InvestigationSpecialisedVariable::find($id);
$investigation_specialised_variable->name = $request->name;
$investigation_specialised_variable->investigation_id = $request->investigation;
$investigation_specialised_variable->units = $request->unit;
$investigation_specialised_variable->normal_ranges = $request->normal_ranges;
$investigation_specialised_variable->range_type = $request->range_type;
$investigation_specialised_variable->created_by = $logged_in_user_id;
$investigation_specialised_variable->updated_by = $logged_in_user_id;
$investigation = Investigation::find($request->investigation);
$investigation->type = 1;
try {
$investigation_specialised_variable->save();
$investigation->save();
if ($request->range_type == 1) {
$age_id = $request->age_id;
$dynamic_range_male = $request->dynamic_range_male;
$dynamic_range_female = $request->dynamic_range_female;
for ($x = 0; $x < count($age_id); $x++){
$dynamic_normal_range = DB::table('investigations_normal_ranges')
->where('age_group_id', $age_id[$x])
->where('test_id', $investigation_specialised_variable->id)
->where('is_specialised_variable', 1)
->first();
if (!$dynamic_normal_range) {
$dynamic_normal_range = new InvestigationNormalRange;
$dynamic_normal_range->created_by = $logged_in_user_id;
} else {
$dynamic_normal_range = InvestigationNormalRange::find($dynamic_normal_range->id);
$dynamic_normal_range->updated_by = $logged_in_user_id;
}
$dynamic_normal_range->male_range = $dynamic_range_male[$x];
$dynamic_normal_range->female_range = $dynamic_range_female[$x];
$dynamic_normal_range->age_group_id = $age_id[$x];
$dynamic_normal_range->test_id = $investigation_specialised_variable->id;
$dynamic_normal_range->is_specialised_variable = 1;
$dynamic_normal_range->save();
}
}
flash($request->name . "Investigation Specialised Variable has been saved")->success();
return redirect("/investigation_variables/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$investigation_specialised_variable = InvestigationSpecialisedVariable::find($id);
$investigation_id = $investigation_specialised_variable->investigation_id;
if ($investigation_specialised_variable->delete()){
$variables = InvestigationSpecialisedVariable::where(['investigation_id' => $investigation_id])->count();
if ($variables < 1) {
$investigation = Investigation::find($investigation_id);
$investigation->type = 0;
$investigation->save();
}
flash("Investigation Variable has been deleted.")->success();
return redirect('/investigation_variables/');
}
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Http\Response
*/
public function inactive() {
$investigation_variables = InvestigationSpecialisedVariable::onlyTrashed()
->orderBy('name', 'asc')
->paginate(50);
if (empty($investigation_variables)) {
flash()->error("There is no inactive Drug Routes");
return redirect('/investigation_variables/');
} else {
return view('investigations::investigation_specialised_variables.inactive', compact('investigation_variables'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id) {
$investigation_specialised_variable = InvestigationSpecialisedVariable::withTrashed()->find($id);
$investigation = Investigation::find($investigation_specialised_variable->investigation_id);
$investigation->type = 1;
if ($investigation_specialised_variable->restore()){
$investigation->save();
flash("Investigation Variable has been activated.")->success();
return redirect('/investigation_variables/');
}
}
}