This commit is contained in:
sajjad
2023-09-23 02:29:26 -07:00
parent 4f570bb7ef
commit cf58a63013
136 changed files with 29612 additions and 5125 deletions
+468 -480
View File
@@ -1,516 +1,504 @@
<?php
namespace App\Http\Controllers;
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;
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)
class TestResultController extends Controller
{
$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;
}
// 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;
// }
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;
}
public function index( Request $request )
{
$user = auth( 'api' )->user();
$TestResult = TestResult::when($request->has('verified'), function ($query) use ($request){
return $query->where('verified', $request->verified);
})
->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, function($query) use($request){
$device_label = $request->device_label ? $request->device_label : '';
if ($device_label && $device_label != 'undefined'){
$query->whereHas('input_values', function($query) use($request, $device_label){
$query->whereHas('input', function($query) use($request, $device_label){
$query->where('name', $device_label);
})->where('value', $request->device_id);
});
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;
}
else{
$query->where(function($query) use($request){
$query->where('id', $request->device_id);
});
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;
}
})
->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);
}
$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 );
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
}
return \MessageResponse::data( $TestResult );
}
$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;
}
public function create()
{
//
}
$FilesPath = [];
public function store( StoreTestResultRequest $request )
{
try {
$return_value = \DB::transaction( function () use ( $request ) {
if (count($TestResult->files)) {
foreach ($TestResult->files as $Key => $file) {
$FilesPath[] = $file;
}
$TestResult['filesPath'] = $FilesPath;
}
else {
$TestResult['filesPath'] = [];
}
$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();
return \MessageResponse::data($TestResult);
}
$Inputs = Input::with( 'options' , 'children' , 'children.options' )->whereNull( 'parent_id' )->where( 'test_type_id' , $request->test_type_id )->get();
public static function storage_path($path)
{
if (starts_with($path, 'media/')) {
$path = substr($path, 6);
}
return $path;
}
///////////////////////////////////////////////////////////////////////////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' ) {
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();
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 ] );
}
}
$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]);
} else {
$InputValueObj = InputValue::create( [ 'test_result_id' => $TestResultObj->id , 'input_id' => $Input->id , 'value' => $Value ] );
}
if ($InputChild['type'] == 'Radiobutton' && $Option->id == $Value) {
$InputValueObj = InputValue::create(['test_result_id' => $TestResult->id, 'input_id' => $InputChild->id, 'option_id' => $Option->id]);
} 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 ] );
}
}
}
}
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();
}
}
///////////////////////////////////////////////////////////////////////////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 );
}
}
$TestResult->delete();
return \MessageResponse::success(null, trans('messages.success'));
}
return \MessageResponse::success( null , trans( 'messages.create' ) );
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)
public function show( $id )
{
$query->where('id', $user->medical_center_id);
})->select('name', 'id')->get()->toArray();
$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;
$TestType = TestType::select('name', 'id')->get()->toArray();
return \MessageResponse::data([
'MedicalCenters' => $MedicalCenter,
'TestTypes' => $TestType
]);
}
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;
}
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;
}
$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;
}
$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;
$values[ $input->name ] = $options;
$TestResult[ 'options' ] = $values;
} else {
$inputs[ $input->name ] = $value->value;
$TestResult[ 'inputs' ] = $inputs;
}
//-----------------------------------------------------------------------Anothertype
}
}
$TestResult['filesPath'] = $FilesPath;
$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 );
}
// dd($TestResult->test_type()->device_label);
$TestResult['device_label'] = $TestResult->test_type->device_label;
return \MessageResponse::data($TestResult);
}
}