feat: install phpstan and resolving reported errors in PHPStan

This commit is contained in:
amir-ys
2024-11-07 01:41:19 +03:30
parent b7efa6b24a
commit 865d565a91
27 changed files with 1023 additions and 642 deletions
+229 -228
View File
@@ -12,141 +12,142 @@ use App\TestResult;
use App\TestType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Packages\Messaging\src\Facades\MessageResponse;
class TestResultController extends Controller
{
public function index( Request $request )
public function index(Request $request)
{
$user = auth( 'api' )->user();
$user = auth('api')->user();
if ( $user->hasRole( 'Supervisor|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' ) {
} 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' ) ) {
if ($user->hasRole('User')) {
$user_name = $user->name;
} elseif ( $request->has( 'user_name' ) && $request->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 );
} );
$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 );
} );
$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' ,
'user.medical_center' => function ( $query ) {
$query->select( 'id' , 'name' , 'address' , 'phone_number' );
} ,
'test_type' => function ( $query ) {
$query->select( 'id' , 'name' );
} ] )
->orderByDesc( 'id' )->paginate( 10 );
})
->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',
'user.medical_center' => function ($query) {
$query->select('id', 'name', 'address', 'phone_number');
},
'test_type' => function ($query) {
$query->select('id', 'name');
}])
->orderByDesc('id')->paginate(10);
return TestResultResource::collection( $TestResult );
return TestResultResource::collection($TestResult);
}
public function store( StoreTestResultRequest $request )
public function store(StoreTestResultRequest $request)
{
try {
DB::transaction( function () use ( $request ) {
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' ,
'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 );
$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 = 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 ($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 ] );
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 ] );
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 ] );
$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 ];
foreach ($Input->children as $InputChild) {
$Value = $TestResultInfo[$InputChild->name . '_' . $InputChild->id];
if ( $InputChild[ 'type' ] == 'Checkbox' || $InputChild[ 'type' ] == 'Select' || $InputChild[ 'type' ] == 'Radiobutton' ) {
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 ] );
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 ] );
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 ] );
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id, 'input_id' => $InputChild->id, 'value' => $Value]);
}
}
@@ -155,285 +156,285 @@ class TestResultController extends Controller
}
///////////////////////////////////////////////////////////////////////////Inputs
MediaHelper::moveMediaTo( $TestResultObj );
MediaHelper::moveMediaTo($TestResultObj);
} );
} catch ( \Exception $e ) {
return response()->json( $e->getMessage() , 441 );
});
} catch (\Exception $e) {
return response()->json($e->getMessage(), 441);
}
return MessageResponse::success( null , trans( 'messages.create' ) );
return MessageResponse::success(null, trans('messages.create'));
}
public function show( $id )
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 );
$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 ) {
foreach ($TestResult->input_values as $value) {
$input = $value->input;
if ( $input->parent_id != null && $input->parent[ 'type' ] == 'Form' ) {
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();
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 ) {
foreach ($input_options as $input_option) {
$options[] = $input_option->option->title;
$input_values[ $input->name ] = $options;
$input_values[$input->name] = $options;
}
$children[ $input->parent->name ] = $input_values;
$TestResult[ 'children' ] = $children;
$children[$input->parent->name] = $input_values;
$TestResult['children'] = $children;
} else {
$children[ $input->parent->name ][ $input->name ] = $value->value;
$TestResult[ 'children' ] = $children;
$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();
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 ) {
foreach ($input_options as $input_option) {
$options[] = $input_option->option->title;
}
$values[ $input->name ] = $options;
$TestResult[ 'options' ] = $values;
$values[$input->name] = $options;
$TestResult['options'] = $values;
} else {
$inputs[ $input->name ] = $value->value;
$TestResult[ 'inputs' ] = $inputs;
$inputs[$input->name] = $value->value;
$TestResult['inputs'] = $inputs;
}
//-----------------------------------------------------------------------Anothertype
}
}
$TestResult[ 'device_label' ] = $TestResult->test_type->device_label;
$TestResult['device_label'] = $TestResult->test_type->device_label;
return TestResultResource::make( $TestResult );
return TestResultResource::make($TestResult);
}
public function edit( TestResult $TestResult )
public function edit(TestResult $TestResult)
{
foreach ( $TestResult->input_values as $key => $value ) {
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 ];
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;
$TestResult[$input->name . '_' . $input->id] = $input_options;
}
} else {
$TestResult[ $input->name . '_' . $input->id ] = $value->value;
$TestResult[$input->name . '_' . $input->id] = $value->value;
}
}
return TestResultResource::make( $TestResult );
return TestResultResource::make($TestResult);
}
public static function storage_path( $path )
public static function storage_path($path)
{
if ( starts_with( $path , 'media/' ) ) {
$path = substr( $path , 6 );
if (Str::startsWith($path, 'media/')) {
$path = substr($path, 6);
}
return $path;
}
public function update( StoreTestResultRequest $request , TestResult $TestResult )
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 ,
'cycle_number' => $request->cycle_number,
'date' => $request->date,
'test_type_id' => $request->test_type_id,
'result' => $request->result,
// 'verified' => 'NotVerified',
];
$TestResult->update( $TestResultData );
$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();
$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' ) {
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();
$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 ] );
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 ] );
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 ] );
$InputValueObj = InputValue::where('test_result_id', $TestResult->id)->where('input_id', $Input['id'])->update(['value' => $Value]);
}
} else {
foreach ( $Input->children as $InputChild ) {
foreach ($Input->children as $InputChild) {
$Value = $TestResultInfo[ $InputChild->name . '_' . $InputChild->id ];
$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();
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 ] );
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 ] );
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 ] );
$InputValueObj = InputValue::where('test_result_id', $TestResult->id)->where('input_id', $InputChild['id'])->update(['value' => $Value]);
}
}
}
}
/////////////////////////////////////////////////////////////////////Images
return MessageResponse::success( null , trans( 'messages.update' ) );
return MessageResponse::success(null, trans('messages.update'));
}
public function destroy( TestResult $TestResult )
public function destroy(TestResult $TestResult)
{
if ( ! auth( 'api' )->user()->can( 'delete' , $TestResult ) ) {
return MessageResponse::error( trans( 'messages.access_denied' ) );
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->input_values)) {
InputValue::where('test_result_id', $TestResult->id)->get()->each->delete();
}
$TestResult->delete();
return MessageResponse::success( null , trans( 'messages.success' ) );
return MessageResponse::success(null, trans('messages.success'));
}
public function verifyTestResult( TestResult $TestResult , $Value )
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' ) );
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 );
$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();
$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 ,
$TestType = TestType::select('name', 'id')->get()->toArray();
return MessageResponse::data([
'MedicalCenters' => $MedicalCenter,
'TestTypes' => $TestType
] );
]);
}
public function chart( Request $request )
public function chart(Request $request)
{
$user = auth( 'api' )->user();
if ( $user->hasRole( 'User' ) ) {
$user = auth('api')->user();
if ($user->hasRole('User')) {
$userId = $user->id;
} elseif ( $request->has( 'userId' ) && $request->userId ) {
} elseif ($request->has('userId') && $request->userId) {
$userId = $request->userId;
} else {
$userId = null;
}
if ( $user->hasRole( 'Admin' ) && $request->has( 'medicalCenterId' ) && $request->medicalCenterId ) {
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;
$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 ] ] );
return MessageResponse::data(['labels' => ['Number Of Accepted Test Results', 'Number Of Rejected Test Results'], 'data' => [$AcceptCount, $RejectCount]]);
}
public function showTestResult( $id )
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 );
$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 ) {
foreach ($TestResult->input_values as $value) {
$input = $value->input;
if ( $input->parent_id != null && $input->parent[ 'type' ] == 'Form' ) {
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();
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 ) {
foreach ($input_options as $input_option) {
$options[] = $input_option->option->title;
$input_values[ $input->name ] = $options;
$input_values[$input->name] = $options;
}
$children[ $input->parent->name ] = $input_values;
$TestResult[ 'children' ] = $children;
$children[$input->parent->name] = $input_values;
$TestResult['children'] = $children;
} else {
$children[ $input->parent->name ][ $input->name ] = $value->value;
$TestResult[ 'children' ] = $children;
$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();
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 ) {
foreach ($input_options as $input_option) {
$options[] = $input_option->option->title;
}
$values[ $input->name ] = $options;
$TestResult[ 'options' ] = $values;
$values[$input->name] = $options;
$TestResult['options'] = $values;
} else {
$inputs[ $input->name ] = $value->value;
$TestResult[ 'inputs' ] = $inputs;
$inputs[$input->name] = $value->value;
$TestResult['inputs'] = $inputs;
}
//-----------------------------------------------------------------------Anothertype
}
}
$TestResult[ 'device_label' ] = $TestResult->test_type->device_label;
return MessageResponse::data( $TestResult );
$TestResult['device_label'] = $TestResult->test_type->device_label;
return MessageResponse::data($TestResult);
}
}