Files
cssd-back/app/Http/Controllers/TestResultController.php
T
2023-09-23 02:29:26 -07:00

505 lines
27 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\StoreTestResultRequest;
use App\TestResult;
use App\InputValue;
use App\Input;
use App\MedicalCenter;
use App\TestType;
use App\Gallery;
use App\Uploadable;
use App\Media;
use Intervention\Image\ImageManagerStatic as Image;
use Intervention\Image\ImageManager;
use Illuminate\Support\Facades\Storage;
class TestResultController extends Controller
{
// private function filterByInputs($inputArray){
// $searchArray = ['Patient ID' => 1111, 'Autoclave ID' => 2222];
//
// $TestResults = TestResult::query();
//
// foreach ($inputArray as $key => $value) {
// $TestResults .= $TestResults->whereHas('input_values', function($query) use($value, $key){
// $query->whereHas('input', function($query) use($key){
// $query->where('name', $key);
// })->where('value', $value);
// });
// }
// return $query;
// }
public function index( Request $request )
{
$user = auth( 'api' )->user();
if ( $user->hasRole( 'Supervisor|User' ) ) {
$medical_center_id = $user->medical_center_id;
} elseif ( $request->has( 'medicalCenter_id' ) && $request->medicalCenter_id != '' && $request->medicalCenter_id != 'undefined' ) {
$medical_center_id = $request->medicalCenter_id;
} else {
$medical_center_id = null;
}
if ( $user->hasRole( 'User' ) ) {
$user_name = $user->name;
} elseif ( $request->has( 'user_name' ) && $request->user_name ) {
$user_name = $request->user_name;
} else {
$user_name = null;
}
$TestResult = TestResult::when( $request->has( 'verified' ) , function ( $query ) use ( $request ) {
return $query->where( 'verified' , $request->verified )->orWhere( 'verified' , "None" );
} )
->when( ( $request->has( 'fromPicker' ) && $request->fromPicker ) || ( $request->has( 'upToPicker' ) && $request->upToPicker ) , function ( $query ) use ( $request ) {
$from = ( $request->fromPicker ) ? date( $request->fromPicker . ' 00:00:00' ) : date( '2010-01-01' . ' 00:00:00' );
$upTo = ( $request->upToPicker ) ? date( $request->upToPicker . ' 23:59:59' ) : date( 'Y-m-d' . ' 23:59:59' );
return $query->whereBetween( 'created_at' , [ $from , $upTo ] );
} )
->when( $request->has( 'testType_id' ) && $request->testType_id , function ( $query ) use ( $request ) {
$query->where( 'test_type_id' , $request->testType_id );
} )
->when( ($request->has( 'device_id' ) && $request->device_id) && ($request->has( 'device_label' ) && $request->device_label && $request->device_label != "undefined") , function ( $query ) use ( $request ) {
if ( $request->device_label != 'undefined' ) {
$query->whereHas( 'input_values' , function ( $query ) use ( $request ) {
$query->whereHas( 'input' , function ( $query ) use ( $request ) {
$query->where( 'name' , $request->device_label );
} )->where( 'value' , $request->device_id );
} );
} else {
$query->where( function ( $query ) use ( $request ) {
$query->where( 'id' , $request->device_id );
} );
}
} )
->when( ($request->has( 'device_id' ) && $request->device_id) && !($request->has( 'device_label' ) && $request->device_label && $request->device_label != "undefined") , function ( $query ) use ( $request ) {
$query->where( function ( $query ) use ( $request ) {
$query->where( 'id' , $request->device_id );
} );
} )
->when( ($request->has( 'device_label' ) && $request->device_label && $request->device_label != "undefined") && !($request->has( 'device_id' ) && $request->device_id), function ( $query ) use ( $request ) {
$query->whereHas( 'input_values' , function ( $query ) use ( $request ) {
$query->whereHas( 'input' , function ( $query ) use ( $request ) {
$query->where( 'name' , $request->device_label );
} );
} );
} )
->when( $user_name , function ( $query ) use ( $user_name ) {
$query->whereHas( 'user' , function ( $query ) use ( $user_name ) {
$query->where( 'name' , 'LIKE' , '%' . $user_name . '%' );
} );
} )
->when( $medical_center_id , function ( $query ) use ( $medical_center_id ) {
$query->whereHas( 'user' , function ( $query ) use ( $medical_center_id ) {
$query->where( 'medical_center_id' , $medical_center_id );
} );
} )
->with( [ 'user' => function ( $query ) {
$query->select( 'id' , 'name' , 'medical_center_id' );
} ,
'user.medical_center' => function ( $query ) {
$query->select( 'id' , 'name' );
} ,
'test_type' => function ( $query ) {
$query->select( 'id' , 'name' );
} ] )
->orderByDesc( 'id' )->paginate( 10 );
return \MessageResponse::data( $TestResult );
}
public function create()
{
//
}
public function store( StoreTestResultRequest $request )
{
try {
$return_value = \DB::transaction( function () use ( $request ) {
$TestResult = [
'user_id' => auth( 'api' )->user()->id ,
'cycle_number' => $request->cycle_number ,
'date' => $request->date ,
'test_type_id' => $request->test_type_id ,
'result' => $request->result ,
'verified' => 'None' ,
];
$TestResultObj = TestResult::create( $TestResult );
$TestResultInfo = $request->all();
$Inputs = Input::with( 'options' , 'children' , 'children.options' )->whereNull( 'parent_id' )->where( 'test_type_id' , $request->test_type_id )->get();
///////////////////////////////////////////////////////////////////////////Inputs
foreach ( $Inputs as $Input ) {
if ( $Input[ 'type' ] != 'Form' ) {
$Value = $TestResultInfo[ $Input->name . '_' . $Input->id ];
if ( $Input[ 'type' ] == 'Checkbox' || $Input[ 'type' ] == 'Select' || $Input[ 'type' ] == 'Radiobutton' ) {
foreach ( $Input->options as $Option ) {//--2
if ( is_array( $Value ) && in_array( $Option->id , $Value ) ) {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResultObj->id , 'input_id' => $Input->id , 'option_id' => $Option->id ] );
}
if ( $Input[ 'type' ] == 'Radiobutton' && $Option->id == $Value ) {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResultObj->id , 'input_id' => $Input->id , 'option_id' => $Option->id ] );
}
}
} else {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResultObj->id , 'input_id' => $Input->id , 'value' => $Value ] );
}
} else {
foreach ( $Input->children as $InputChild ) {
$Value = $TestResultInfo[ $InputChild->name . '_' . $InputChild->id ];
if ( $InputChild[ 'type' ] == 'Checkbox' || $InputChild[ 'type' ] == 'Select' || $InputChild[ 'type' ] == 'Radiobutton' ) {
foreach ( $InputChild->options as $Option ) {//--2
if ( is_array( $Value ) && in_array( $Option->id , $Value ) ) {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResultObj->id , 'input_id' => $InputChild->id , 'option_id' => $Option->id ] );
}
if ( $InputChild[ 'type' ] == 'Radiobutton' && $Option->id == $Value ) {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResultObj->id , 'input_id' => $InputChild->id , 'option_id' => $Option->id ] );
}
}
} else {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResultObj->id , 'input_id' => $InputChild->id , 'value' => $Value ] );
}
}
}
}
///////////////////////////////////////////////////////////////////////////Inputs
///////////////////////////////////////////////////////////////////////////Images
$Images = Media::whereNull( 'model_id' )->where( 'batch_id' , $request->file_batch_id )->get();
if ( $Images ) {
foreach ( $Images as $Image ) {
$NewPath = 'media' . '/TestResults' . '/' . $TestResultObj->id . '/' . $Image->file_name;
Storage::disk( 'public' )->makeDirectory( 'TestResults' . '/' . $TestResultObj->id );
if ( \File::exists( public_path( $Image->path ) ) ) {
\File::move( public_path( $Image->path ) , public_path( $NewPath ) );
}
$Image->model_id = $TestResultObj->id;
$Image->path = $NewPath;
$Image->save();
}
}
///////////////////////////////////////////////////////////////////////////Images
} );
} catch ( \Exception $e ) {
return response()->json( $e->getMessage() , 441 );
}
return \MessageResponse::success( null , trans( 'messages.create' ) );
}
public function show( $id )
{
$TestResult = TestResult::with( 'input_values' , 'user.medical_center' , 'test_type' )->select( 'result' , 'verified' , 'device_id' , 'user_id' , 'id' , 'test_type_id' , 'created_at' )->findOrFail( $id );
$options = [];
foreach ( $TestResult->input_values as $value ) {
$input = $value->input;
if ( $input->parent_id != null && $input->parent[ 'type' ] == 'Form' ) {
//----------------------------------------------------------------------FormType
if ( $input->type == 'Checkbox' || $input->type == 'Select' || $input[ 'type' ] == 'Radiobutton' ) {
$input_options = InputValue::with( 'option' )->where( 'test_result_id' , $TestResult->id )->where( 'input_id' , $input->id )->get();
$options = [];
foreach ( $input_options as $input_option ) {
$options[] = $input_option->option->title;
$input_values[ $input->name ] = $options;
}
$children[ $input->parent->name ] = $input_values;
$TestResult[ 'children' ] = $children;
} else {
$children[ $input->parent->name ][ $input->name ] = $value->value;
$TestResult[ 'children' ] = $children;
}
//-----------------------------------------------------------------------FormType
} else {
//-----------------------------------------------------------------------Anothertype
if ( $input->type == 'Checkbox' || $input->type == 'Select' || $input[ 'type' ] == 'Radiobutton' ) {
$input_options = InputValue::with( 'option' )->where( 'test_result_id' , $TestResult->id )->where( 'input_id' , $input->id )->get();
$options = [];
foreach ( $input_options as $input_option ) {
$options[] = $input_option->option->title;
}
$values[ $input->name ] = $options;
$TestResult[ 'options' ] = $values;
} else {
$inputs[ $input->name ] = $value->value;
$TestResult[ 'inputs' ] = $inputs;
}
//-----------------------------------------------------------------------Anothertype
}
}
$FilesPath = [];
if ( count( $TestResult->files ) ) {
foreach ( $TestResult->files as $Key => $File ) {
$FilesPath[] = $File->path;
}
$TestResult[ 'filesPath' ] = $FilesPath;
}
$TestResult[ 'device_label' ] = $TestResult->test_type->device_label;
return \MessageResponse::data( $TestResult );
}
public function edit( TestResult $TestResult )
{
foreach ( $TestResult->input_values as $key => $value ) {
$input = $value->input;
if ( $input->type == 'Checkbox' || $input->type == 'Select' || $input[ 'type' ] == 'Radiobutton' ) {
$input_options = InputValue::where( 'test_result_id' , $TestResult->id )->where( 'input_id' , $input->id )->get()->pluck( 'option_id' )->toArray();
if ( $input[ 'type' ] == 'Radiobutton' ) {
$TestResult[ $input->name . '_' . $input->id ] = $input_options[ 0 ];
} else {
$TestResult[ $input->name . '_' . $input->id ] = $input_options;
}
} else {
$TestResult[ $input->name . '_' . $input->id ] = $value->value;
}
}
$FilesPath = [];
if ( count( $TestResult->files ) ) {
foreach ( $TestResult->files as $Key => $file ) {
$FilesPath[] = $file;
}
$TestResult[ 'filesPath' ] = $FilesPath;
} else {
$TestResult[ 'filesPath' ] = [];
}
return \MessageResponse::data( $TestResult );
}
public static function storage_path( $path )
{
if ( starts_with( $path , 'media/' ) ) {
$path = substr( $path , 6 );
}
return $path;
}
public function update( StoreTestResultRequest $request , TestResult $TestResult )
{
$TestResultData = [
'cycle_number' => $request->cycle_number ,
'date' => $request->date ,
'test_type_id' => $request->test_type_id ,
'result' => $request->result ,
// 'verified' => 'NotVerified',
];
$TestResult->update( $TestResultData );
$TestResultInfo = $request->all();
$Inputs = Input::with( 'options' , 'children' , 'children.options' )->whereNull( 'parent_id' )->where( 'test_type_id' , $request->test_type_id )->get();
foreach ( $Inputs as $Input ) {
if ( $Input[ 'type' ] != 'Form' ) {
$Value = $TestResultInfo[ $Input->name . '_' . $Input->id ];
if ( $Input[ 'type' ] == 'Checkbox' || $Input[ 'type' ] == 'Select' || $Input[ 'type' ] == 'Radiobutton' ) {
$InputOptions = InputValue::where( 'test_result_id' , $TestResult->id )->where( 'input_id' , $Input->id )->get()->each->delete();
foreach ( $Input->options as $Option ) {//--2
if ( is_array( $Value ) && in_array( $Option->id , $Value ) ) {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResult->id , 'input_id' => $Input->id , 'option_id' => $Option->id ] );
}
if ( $Input[ 'type' ] == 'Radiobutton' && $Option->id == $Value ) {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResult->id , 'input_id' => $Input->id , 'option_id' => $Option->id ] );
}
}
} else {
$InputValueObj = InputValue::where( 'test_result_id' , $TestResult->id )->where( 'input_id' , $Input[ 'id' ] )->update( [ 'value' => $Value ] );
}
} else {
foreach ( $Input->children as $InputChild ) {
$Value = $TestResultInfo[ $InputChild->name . '_' . $InputChild->id ];
if ( $InputChild[ 'type' ] == 'Checkbox' || $InputChild[ 'type' ] == 'Select' || $InputChild[ 'type' ] == 'Radiobutton' ) {
$InputOptions = InputValue::where( 'test_result_id' , $TestResult->id )->where( 'input_id' , $InputChild->id )->get()->each->delete();
foreach ( $InputChild->options as $Option ) {//--2
if ( is_array( $Value ) && in_array( $Option->id , $Value ) ) {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResult->id , 'input_id' => $InputChild->id , 'option_id' => $Option->id ] );
}
if ( $InputChild[ 'type' ] == 'Radiobutton' && $Option->id == $Value ) {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResult->id , 'input_id' => $InputChild->id , 'option_id' => $Option->id ] );
}
}
} else {
$InputValueObj = InputValue::where( 'test_result_id' , $TestResult->id )->where( 'input_id' , $InputChild[ 'id' ] )->update( [ 'value' => $Value ] );
}
}
}
}
/////////////////////////////////////////////////////////////////////Images
return \MessageResponse::success( null , trans( 'messages.update' ) );
}
public function destroy( TestResult $TestResult )
{
if ( ! auth( 'api' )->user()->can( 'delete' , $TestResult ) ) {
return \MessageResponse::error( trans( 'messages.access_denied' ) );
}
if ( count( $TestResult->input_values ) ) {
InputValue::where( 'test_result_id' , $TestResult->id )->get()->each->delete();
}
if ( count( $TestResult->files ) ) {
foreach ( $TestResult->files as $file ) {
Storage::disk( 'public' )->delete( static::storage_path( $file->path ) );
Media::where( 'id' , $file->id )->delete();
}
}
$TestResult->delete();
return \MessageResponse::success( null , trans( 'messages.success' ) );
}
public function verifyTestResult( TestResult $TestResult , $Value )
{
if ( auth( 'api' )->user()->cant( 'verifyTestResult' , $TestResult ) ) {
return \MessageResponse::error( trans( 'You dont have permission or this test result belongs to another medical center' ) );
}
$TestResult->verified = $Value;
$TestResult->save();
$TestResult = $TestResult->load( 'user.medical_center' );
return \MessageResponse::success( trans( 'messages.success' ) , null , $TestResult );
}
public function listFilterItems()
{
$user = auth( 'api' )->user();
$MedicalCenter = MedicalCenter::when( ! $user->hasRole( 'Admin' ) , function ( $query ) use ( $user ) {
$query->where( 'id' , $user->medical_center_id );
} )->select( 'name' , 'id' )->get()->toArray();
$TestType = TestType::select( 'name' , 'id' )->get()->toArray();
return \MessageResponse::data( [
'MedicalCenters' => $MedicalCenter ,
'TestTypes' => $TestType
] );
}
public function chart( Request $request )
{
$user = auth( 'api' )->user();
if ( $user->hasRole( 'User' ) ) {
$userId = $user->id;
} elseif ( $request->has( 'userId' ) && $request->userId ) {
$userId = $request->userId;
} else {
$userId = null;
}
if ( $user->hasRole( 'Admin' ) && $request->has( 'medicalCenterId' ) && $request->medicalCenterId ) {
$medicalCenterId = $request->medicalCenterId;
} else {
$medicalCenterId = $user->medical_center_id;
}
$TestResults = TestResult::
when( $userId , function ( $query ) use ( $userId ) {
$query->where( 'user_id' , $userId );
} )
->when( $medicalCenterId , function ( $query ) use ( $medicalCenterId ) {
$query->whereHas( 'user' , function ( $query ) use ( $medicalCenterId ) {
$query->where( 'medical_center_id' , $medicalCenterId );
} );
} )
->when( $request->has( 'testTypeId' ) && $request->testTypeId , function ( $query ) use ( $request ) {
$query->where( 'test_type_id' , $request->testTypeId );
} )
->when( $request->has( 'result' ) && $request->result , function ( $query ) use ( $request ) {
$query->where( 'result' , $request->result );
} )
->when( ( $request->has( 'fromPicker' ) && $request->fromPicker ) || ( $request->has( 'upToPicker' ) && $request->upToPicker ) , function ( $query ) use ( $request ) {
$from = ( $request->fromPicker ) ? date( $request->fromPicker . ' 00:00:00' ) : date( '2010-01-01' . ' 00:00:00' );
$upTo = ( $request->upToPicker ) ? date( $request->upToPicker . ' 23:59:59' ) : date( 'Y-m-d' . ' 23:59:59' );
return $query->whereBetween( 'created_at' , [ $from , $upTo ] );
} )
->when( $request->has( 'verify' ) && $request->verify , function ( $query ) use ( $request ) {
$query->where( 'verified' , $request->verify );
} )->select( 'result' )->get()->groupBy( 'result' );
$AcceptCount = ( isset( $TestResults[ 'Accept' ] ) ) ? $TestResults[ 'Accept' ]->count() : 0;
$RejectCount = ( isset( $TestResults[ 'Reject' ] ) ) ? $TestResults[ 'Reject' ]->count() : 0;
return \MessageResponse::data( [ 'labels' => [ 'Number Of Accepted Test Results' , 'Number Of Rejected Test Results' ] , 'data' => [ $AcceptCount , $RejectCount ] ] );
}
//--------------------------------------------------------------------------Show
public function showTestResult( $id )
{
$TestResult = TestResult::with( 'input_values' , 'user.medical_center' , 'test_type' )->select( 'result' , 'verified' , 'device_id' , 'user_id' , 'id' , 'test_type_id' , 'created_at' )->findOrFail( $id );
$options = [];
foreach ( $TestResult->input_values as $value ) {
$input = $value->input;
if ( $input->parent_id != null && $input->parent[ 'type' ] == 'Form' ) {
//----------------------------------------------------------------------FormType
if ( $input->type == 'Checkbox' || $input->type == 'Select' || $input[ 'type' ] == 'Radiobutton' ) {
$input_options = InputValue::with( 'option' )->where( 'test_result_id' , $TestResult->id )->where( 'input_id' , $input->id )->get();
$options = [];
foreach ( $input_options as $input_option ) {
$options[] = $input_option->option->title;
$input_values[ $input->name ] = $options;
}
$children[ $input->parent->name ] = $input_values;
$TestResult[ 'children' ] = $children;
} else {
$children[ $input->parent->name ][ $input->name ] = $value->value;
$TestResult[ 'children' ] = $children;
}
//-----------------------------------------------------------------------FormType
} else {
//-----------------------------------------------------------------------Anothertype
if ( $input->type == 'Checkbox' || $input->type == 'Select' || $input[ 'type' ] == 'Radiobutton' ) {
$input_options = InputValue::with( 'option' )->where( 'test_result_id' , $TestResult->id )->where( 'input_id' , $input->id )->get();
$options = [];
foreach ( $input_options as $input_option ) {
$options[] = $input_option->option->title;
}
$values[ $input->name ] = $options;
$TestResult[ 'options' ] = $values;
} else {
$inputs[ $input->name ] = $value->value;
$TestResult[ 'inputs' ] = $inputs;
}
//-----------------------------------------------------------------------Anothertype
}
}
$FilesPath = [];
if ( count( $TestResult->files ) ) {
foreach ( $TestResult->files as $Key => $File ) {
$FilesPath[] = $File->path;
}
$TestResult[ 'filesPath' ] = $FilesPath;
}
// dd($TestResult->test_type()->device_label);
$TestResult[ 'device_label' ] = $TestResult->test_type->device_label;
return \MessageResponse::data( $TestResult );
}
}