mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
314 lines
14 KiB
PHP
Executable File
314 lines
14 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Investigations\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Streamline\Models\InvestigationResults;
|
|
use Streamline\Models\InvestigationSpecialisedResult;
|
|
use Streamline\Models\LabInstrument;
|
|
use Streamline\Models\LabMachineRestart;
|
|
use Streamline\Models\LabMachineResult;
|
|
use Streamline\Models\OrderedInvestigation;
|
|
use Streamline\Models\TrackReceipt;
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class LabMachinesController extends Controller {
|
|
public function receive_results_mindray(Request $request) {
|
|
$test_id = $request->test_id; // the investigation order id
|
|
$arr_parameters = json_decode($request->parameters);
|
|
$results_time = $request->time;
|
|
|
|
$results_arr = [];
|
|
$flag_arr = [];
|
|
$test_code_arr = [];
|
|
|
|
foreach ($arr_parameters as $parameter) {
|
|
$results_arr[] = $parameter[1];
|
|
$flag_arr[] = $parameter[2];
|
|
$test_code_arr[] = $parameter[0];
|
|
}
|
|
|
|
$results = new LabMachineResult();
|
|
$results->sample_id = $test_id;
|
|
$results->results = implode(",", $results_arr);
|
|
$results->abnormal_flag = implode(",", $flag_arr);
|
|
$results->test_code = implode(",", $test_code_arr);
|
|
$results->results_time = $results_time;
|
|
$results->save();
|
|
}
|
|
|
|
public function receive_results_mispa(Request $request) {
|
|
$test_id = $request->SID ?? 0;
|
|
$patient_id = $request->PID ?? 0;
|
|
$machine_id = $request->machine_id ?? 0;
|
|
$arr_parameters = json_decode($request->results);
|
|
|
|
$results_arr = [];
|
|
$flag_arr = [];
|
|
$test_code_arr = [];
|
|
|
|
foreach ($arr_parameters as $parameter) {
|
|
$test_code_arr[] = $parameter[0];
|
|
$results_arr[] = str_replace(' ', '', $parameter[1]);
|
|
$flag_arr[] = strtoupper($parameter[2]);
|
|
}
|
|
|
|
$results = new LabMachineResult();
|
|
$results->sample_id = $test_id;
|
|
$results->results = implode(",", $results_arr);
|
|
$results->abnormal_flag = implode(",", $flag_arr);
|
|
$results->test_code = implode(",", $test_code_arr);
|
|
$results->machine_id = $machine_id;
|
|
$results->save();
|
|
}
|
|
|
|
public function import_results($sample_id) {
|
|
$lab_machine_result = LabMachineResult::where('sample_id', $sample_id)->orderBy('id', 'desc')->first();
|
|
|
|
$results = explode(",", $lab_machine_result->results);
|
|
$flag = explode(",", $lab_machine_result->abnormal_flag);
|
|
$test_code = explode(",", $lab_machine_result->test_code);
|
|
$machine_id = $lab_machine_result->machine_id;
|
|
$test_ids = [];
|
|
$test_flag = [];
|
|
$test_results = [];
|
|
|
|
for ($i = 0; $i < count($test_code); $i++) {
|
|
$filters = [];
|
|
|
|
if (is_integer($machine_id)) {
|
|
$filters[] = ['machine_name', '=', $machine_id];
|
|
}
|
|
|
|
$test_id = DB::table("investigation_test_codes")
|
|
->where("test_code", $test_code[$i])
|
|
->where($filters)->first()?->investigation_id;
|
|
|
|
if (is_numeric($test_id)) {
|
|
$test_flag[] = ($flag[$i] == "N") ? "" : $flag[$i];
|
|
$test_ids[] = $test_id;
|
|
$test_results[] = $results[$i];
|
|
}
|
|
}
|
|
|
|
if (count($test_ids) > 0) {
|
|
$correct_ids_array = [];
|
|
$correct_values_array = [];
|
|
$correct_comments_array = [];
|
|
$parent_investigation_id = get_name($test_ids[0], 'id', 'investigation_id', 'investigation_specialised_variables');
|
|
|
|
$specialized_variables = DB::table('investigation_specialised_variables')->whereNull('deleted_at')->where('investigation_id', $parent_investigation_id)->orderBy('ranking','asc')->get();
|
|
|
|
foreach ($specialized_variables as $variable) {
|
|
$key = array_search($variable->id, $test_ids);
|
|
|
|
if (is_integer($key)) {
|
|
$correct_ids_array[] = $test_ids[$key];
|
|
$correct_values_array[] = $test_results[$key];
|
|
$correct_comments_array[] = $test_flag[$key];
|
|
}
|
|
}
|
|
|
|
$this->map_machine_results_to_patient($correct_ids_array, $sample_id, $correct_values_array, $correct_comments_array);
|
|
flash("Results have been imported successfully")->success();
|
|
} else {
|
|
flash("Results not imported because the test codes are missing")->error();
|
|
}
|
|
|
|
return redirect('/investigations/view_lab_results_details/' . $sample_id);
|
|
}
|
|
|
|
public function map_machine_results_to_patient($test_ids, $sample_id, $results, $comments) {
|
|
// get investigation order details
|
|
$investigation_order = OrderedInvestigation::find($sample_id);
|
|
$parent_investigation_id = get_name($test_ids[0], 'id', 'investigation_id', 'investigation_specialised_variables');
|
|
|
|
if ($investigation_order){
|
|
// investigation ids
|
|
$investigation_ids = explode(",", $investigation_order->investigation_id);
|
|
|
|
// check if investigations results are available
|
|
$investigation_result = InvestigationResults::where('order_id', $sample_id)->first();
|
|
|
|
if ($investigation_result){
|
|
// edit the results
|
|
$investigation_result->updated_by = Auth::id();
|
|
|
|
$values_array = explode(",", $investigation_result->value);
|
|
$comments_array = explode(",", $investigation_result->comment);
|
|
|
|
$specialised_results_id = $values_array[array_search($parent_investigation_id, $investigation_ids)];
|
|
|
|
if (InvestigationSpecialisedResult::find($specialised_results_id)) {
|
|
$investigation_specialised_results = InvestigationSpecialisedResult::find($specialised_results_id);
|
|
} else {
|
|
$investigation_specialised_results = new InvestigationSpecialisedResult();
|
|
$investigation_specialised_results->created_by = Auth::id();
|
|
}
|
|
|
|
$investigation_specialised_results->specialised_variable_id = implode(',', $test_ids);
|
|
$investigation_specialised_results->value = implode(',', $results);
|
|
$investigation_specialised_results->comment = implode(',', $comments);
|
|
$investigation_specialised_results->updated_by = Auth::id();
|
|
$investigation_specialised_results->save();
|
|
|
|
$values_array[array_search($parent_investigation_id, $investigation_ids)] = $investigation_specialised_results->id;
|
|
$comments_array[array_search($parent_investigation_id, $investigation_ids)] = "-";
|
|
|
|
$investigation_result->value = implode(",", $values_array);
|
|
$investigation_result->comment = implode(",", $comments_array);
|
|
|
|
$investigation_result->save();
|
|
} else {
|
|
// add new results to the table
|
|
$order = new InvestigationResults;
|
|
|
|
$order->per_investigation = implode(",", array_fill(0, count($investigation_ids), '0'));
|
|
$order->all_authenticated = 0;
|
|
$order->order_id = $sample_id;
|
|
$order->patient_id = $investigation_order->patient_id;
|
|
$order->episode_id = $investigation_order->episode_id;
|
|
$order->created_by = Auth::id();
|
|
$order->inpatient = 0;
|
|
$order->result_type = $investigation_order->order_type;
|
|
|
|
$values_array = array_fill(0, count($investigation_ids), '');
|
|
$comments_array = array_fill(0, count($investigation_ids), '');
|
|
|
|
$investigation_specialised_results = new InvestigationSpecialisedResult();
|
|
|
|
$investigation_specialised_results->specialised_variable_id = implode(',', $test_ids);
|
|
$investigation_specialised_results->value = implode(',', $results);
|
|
$investigation_specialised_results->comment = implode(',', $comments);
|
|
$investigation_specialised_results->created_by = Auth::id();
|
|
$investigation_specialised_results->updated_by = Auth::id();
|
|
$investigation_specialised_results->save();
|
|
|
|
$values_array[array_search($parent_investigation_id, $investigation_ids)] = $investigation_specialised_results->id;
|
|
$comments_array[array_search($parent_investigation_id, $investigation_ids)] = "-";
|
|
|
|
$order->investigation_id = $investigation_order->investigation_id;
|
|
$order->value = implode(",", $values_array);
|
|
$order->comment = implode(",", $comments_array);
|
|
|
|
// save the order
|
|
$order->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
public function view_lab_machine_results(Request $request) {
|
|
$search_text = "";
|
|
$filters = [];
|
|
|
|
// if patient id is active, get all records regardless of date; else search by date
|
|
if (isset($request->lab_number) && $request->lab_number != '') {
|
|
$filters[] = ['sample_id', '=', $request->lab_number];
|
|
$search_text .= "Lab Number: " . $request->lab_number;
|
|
} else {
|
|
switch ($request->search_date_by){
|
|
case 'yesterday':
|
|
$end_date = Carbon::yesterday()->endOfDay();
|
|
$start_date = Carbon::yesterday()->startOfDay();
|
|
$search_text .= "Yesterday ";
|
|
break;
|
|
case 'custom_date':
|
|
$end_date = Carbon::parse($request->start_date)->endOfDay();
|
|
$start_date = Carbon::parse($request->start_date)->startOfDay();
|
|
$search_text .= "From: " . streamline_date($start_date) . " ";
|
|
break;
|
|
case 'custom_date_range':
|
|
$end_date = Carbon::parse($request->end_date)->endOfDay();
|
|
$start_date = Carbon::parse($request->start_date)->startOfDay();
|
|
$search_text .= "From: " . streamline_date($start_date) . " to " . streamline_date($end_date) . " ";
|
|
break;
|
|
case 'today':
|
|
default:
|
|
$end_date = Carbon::today()->endOfDay();
|
|
$start_date = Carbon::today()->startOfDay();
|
|
$search_text .= "Today ";
|
|
break;
|
|
}
|
|
|
|
$filters[] = ['created_at', '>', $start_date];
|
|
$filters[] = ['created_at', '<', $end_date];
|
|
}
|
|
|
|
$results = LabMachineResult::where($filters)->get();
|
|
$lab_machines = LabInstrument::pluck("name", "id")->prepend('- select -', '');
|
|
|
|
return view('investigations::lab_instruments.view_lab_machine_results', compact('results', 'search_text', 'lab_machines'));
|
|
}
|
|
|
|
public function restart_python_script(Request $request) {
|
|
$lab_instrument = LabInstrument::find($request->lab_instrument);
|
|
|
|
if(!$lab_instrument || is_null($lab_instrument->restart_lis_path)) {
|
|
flash("Could not find the lab instrument selected")->error();
|
|
return back()->withInput();
|
|
}
|
|
|
|
// 'public/uploads/lab_machine_scripts/kisiizi/restart_lis.py'
|
|
$process = new Process(['python3', base_path($lab_instrument->restart_lis_path)]);
|
|
$process->run();
|
|
$process->wait();
|
|
|
|
if (!$process->isSuccessful()) {
|
|
throw new ProcessFailedException($process);
|
|
}
|
|
|
|
$lab_machine_restart = new LabMachineRestart();
|
|
|
|
if (strpos($process->getOutput(), '1') !== false) {
|
|
flash("Lab integration has been restarted successfully")->success();
|
|
$lab_machine_restart->result = $process->getOutput();
|
|
} else {
|
|
flash("An error occurred while establishing a connection. Please contact the Stre@mline team")->error();
|
|
$lab_machine_restart->result = $process->getErrorOutput();
|
|
}
|
|
|
|
$lab_machine_restart->created_by = Auth::id();
|
|
$lab_machine_restart->save();
|
|
|
|
return redirect('lab_machines/view_lab_machine_results');
|
|
}
|
|
|
|
public function preview_lab_machine_results($id) {
|
|
$result = LabMachineResult::find($id);
|
|
|
|
$html_text = "<table class='table table-bordered table-striped'>";
|
|
|
|
$test_code_array = explode(",", $result->test_code);
|
|
$flag_array = explode(",", $result->abnormal_flag);
|
|
$results_array = explode(",", $result->results);
|
|
|
|
$html_text .= "<tr>";
|
|
$html_text .= "<th style='color: black' class='text-center'> Test Code </th>";
|
|
$html_text .= "<th style='color: black' class='text-center'> Specialised Variable </th>";
|
|
$html_text .= "<th style='color: black' class='text-center'> Result </th>";
|
|
$html_text .= "<th style='color: black' class='text-center'> Flag </th>";
|
|
$html_text .= "</tr>";
|
|
|
|
for ($i = 0; $i < count($test_code_array); $i++) {
|
|
$specialised_variable_id = get_name($test_code_array[$i], 'test_code', 'investigation_id', 'investigation_test_codes');
|
|
$specialised_variable_name = get_name($specialised_variable_id, 'id', 'name', 'investigation_specialised_variables');
|
|
|
|
$html_text .= "<tr>";
|
|
$html_text .= "<td class='text-center'>" . $test_code_array[$i] . "</td>";
|
|
$html_text .= "<td class='text-center'>" . $specialised_variable_name . "</td>";
|
|
$html_text .= "<td class='text-center'>" . $results_array[$i] . "</td>";
|
|
$html_text .= "<td class='text-center'>" . $flag_array[$i] . "</td>";
|
|
$html_text .= "</tr>";
|
|
}
|
|
|
|
$html_text .= "</table>";
|
|
|
|
return json_encode(["html" => $html_text]);
|
|
}
|
|
}
|