mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-10 23:41:04 +03:30
493 lines
22 KiB
PHP
493 lines
22 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 Intervention\Image\ImageManagerStatic as Image;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class TestResultController extends Controller
|
|
{
|
|
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) {
|
|
$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::with('test_type', 'user', 'user.medical_center')
|
|
->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){
|
|
$query->where('device_id', 'LIKE', '%'.$request->device_id.'%');
|
|
})
|
|
->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);
|
|
});
|
|
})
|
|
->paginate(10);
|
|
|
|
return \MessageResponse::data($TestResult);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function store(StoreTestResultRequest $request)
|
|
{
|
|
// dd($request->result);
|
|
try {
|
|
$return_value = \DB::transaction(function () use ($request) {
|
|
|
|
$TestResult = [
|
|
'user_id' => auth('api')->user()->id,
|
|
'cycle_number' => $request->cycle_number,
|
|
'device_id' => $request->device_id,
|
|
'date' => $request->date,
|
|
'test_type_id' => $request->test_type_id,
|
|
'result' => $request->result,
|
|
'verified' => 'NotVerified',
|
|
];
|
|
$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
|
|
if ($request->imgs != null) {
|
|
|
|
foreach ($request->imgs as $key => $img) {
|
|
$Image = $img;
|
|
$InterventionImage = Image::make($Image);
|
|
$Mime = explode('/', $InterventionImage->mime());
|
|
|
|
$ImageName = time().$key.'.'.$Mime[1];
|
|
$Path = 'uploads'.'/TestResults'.'/'.$TestResultObj->id .'/'.$ImageName;
|
|
|
|
Storage::disk('public')->makeDirectory('TestResults'.'/'.$TestResultObj->id);
|
|
$InterventionImage->save(public_path($Path));
|
|
|
|
$GalleryObj = Gallery::create(['user_id' => auth('api')->user()->id, 'path' => $Path]);
|
|
$Uploadable = Uploadable::create(['uploadable_id' => $TestResultObj->id, 'uploadable_type' => 'App\TestResult', 'gallery_id' => $GalleryObj->id, 'image_role' => 'TitleImage']);
|
|
}
|
|
}
|
|
///////////////////////////////////////////////////////////////////////////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
|
|
}
|
|
}
|
|
|
|
$ImagesPath = [];
|
|
if (count($TestResult->TitleImage)) {
|
|
foreach ($TestResult->TitleImage as $Key => $TitleImage) {
|
|
$ImagesPath[] = $TitleImage->path;
|
|
}
|
|
$TestResult['imagesPath'] = $ImagesPath;
|
|
}
|
|
// dd($TestResult->test_type()->device_label);
|
|
$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;
|
|
}
|
|
}
|
|
|
|
$ImagesPath = [];
|
|
if (count($TestResult->TitleImage)) {
|
|
foreach ($TestResult->TitleImage as $Key => $TitleImage) {
|
|
$ImagesPath[] = $TitleImage->path;
|
|
}
|
|
$TestResult['imagesPath'] = $ImagesPath;
|
|
}
|
|
else {
|
|
$TestResult['imagesPath'] = [];
|
|
}
|
|
|
|
return \MessageResponse::data($TestResult);
|
|
}
|
|
|
|
public static function storage_path($path)
|
|
{
|
|
if (starts_with($path, 'uploads/')) {
|
|
$path = substr($path, 8);
|
|
}
|
|
return $path;
|
|
}
|
|
|
|
public function update(StoreTestResultRequest $request, TestResult $TestResult)
|
|
{
|
|
$TestResultData = [
|
|
// 'autoclave_id' => $request->autoclave_id,
|
|
'cycle_number' => $request->cycle_number,
|
|
'device_id' => $request->device_id,
|
|
'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
|
|
$images = '';
|
|
$imageExist = [];
|
|
$imagesDeleted = [];
|
|
if (count($TestResult->TitleImage))
|
|
{
|
|
$images = $TestResult->TitleImage;
|
|
foreach ($images as $key => $image) {
|
|
|
|
$imageExist[] = '/'.$image->path;
|
|
if (!in_array('/'.$image->path, $request->imgs)) {
|
|
$imagesDeleted[] = $image->path;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if ($request->imgs != null) {
|
|
$request_imgs = array_diff($request->imgs, $imageExist);
|
|
|
|
foreach ($request_imgs as $key => $img) {//
|
|
|
|
if (strrpos($img, "base64") !== false) {
|
|
|
|
$Image = $img;
|
|
$InterventionImage = Image::make($Image);
|
|
$Mime = explode('/', $InterventionImage->mime());
|
|
|
|
$ImageName = time().$key.'.'.$Mime[1];
|
|
$Path = 'uploads'.'/TestResults'.'/'.$TestResult->id .'/'.$ImageName;
|
|
|
|
Storage::disk('public')->makeDirectory('TestResults'.'/'.$TestResult->id);
|
|
$InterventionImage->save(public_path($Path));
|
|
|
|
//--------------------------------------------------------
|
|
$GalleryObj = Gallery::create(['user_id' => auth('api')->user()->id, 'path' => $Path]);
|
|
$Uploadable = Uploadable::create(['uploadable_id' => $TestResult->id, 'uploadable_type' => 'App\TestResult', 'gallery_id' => $GalleryObj->id, 'image_role' => 'TitleImage']);
|
|
//--------------------------------------------------------
|
|
}
|
|
}
|
|
|
|
if (count($imagesDeleted)) {
|
|
foreach ($imagesDeleted as $key => $imageDeleted) {
|
|
$Gallery = Gallery::where('path', $imageDeleted)->first();
|
|
if ($Gallery) {
|
|
Storage::disk('public')->delete(static::storage_path($imageDeleted));
|
|
Uploadable::where('gallery_id', $Gallery->id)->where('uploadable_id', $TestResult->id)->where('uploadable_type', get_class($TestResult))->delete();
|
|
$Gallery->delete();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
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->TitleImage)){
|
|
foreach ($TestResult->TitleImage as $image) {
|
|
Storage::disk('public')->delete(static::storage_path($image->path));
|
|
Uploadable::where('gallery_id', $image->id)->where('uploadable_id', $TestResult->id)->where('uploadable_type', get_class($TestResult))->delete();
|
|
Gallery::where('id', $image->id)->delete();
|
|
}
|
|
}
|
|
$TestResult->delete();
|
|
return \MessageResponse::success(null, trans('messages.success'));
|
|
}
|
|
|
|
public function verifyTestResult(TestResult $TestResult)
|
|
{
|
|
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 ($TestResult->verified == 'Verified') {
|
|
$TestResult->verified = 'NotVerified';
|
|
} else {
|
|
$TestResult->verified = 'Verified';
|
|
}
|
|
$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)
|
|
{
|
|
return view('ShowTestResult', compact('id'));
|
|
}
|
|
}
|