HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/dev-telemedicina.teky.com.co/app/Http/Controllers/RequestController.php
<?php

namespace Telemedicina\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

use Telemedicina\Specialty;
use Telemedicina\ClinicDocument;
use Telemedicina\AttentionType;
use Telemedicina\SpecialtyCenter;
use Telemedicina\ExternalApp;
use Telemedicina\User;
use Carbon\Carbon;
use DB;
use Session;
use Redirect;
use Config;
use Telemedicina\Form;
use Telemedicina\HeaderValue;
use Telemedicina\Event;

class RequestController extends Controller
{
    public function indexStep1(){

        $obj = SpecialtyCenter::groupBy('espes_id')
        ->where('cents_id', Auth::user()->centro->id)
        ->where('active',1)
        ->with('specialty')
        ->select('espes_id')->get();


        $specialties = Specialty::where('active', true)->get();
        $type_cares = AttentionType::where('active', true)->get();

        return view('clinic_document.step1')
        ->with('specialties', $specialties)
        ->with('type_cares', $type_cares)
        ->with('obj',$obj);
    }

    public function indexStep2(Request $request){
        $type_care_id = $request->type_care_id;

        $specialty = Specialty::where('id', $request->specialty_id)->first();
        $type_care = AttentionType::where('id', $request->type_care_id)->first();

        $dynamic_form = Form::where('specialty_id', $request->specialty_id)
                        ->with('fields')
                        ->with('specialty')
                        ->first();

        if($dynamic_form && $dynamic_form->visibilty == 'app'){
            $dynamic_form = [];
        }

        return view('clinic_document.document', compact('specialty', 'type_care', 'dynamic_form'));
    }

    public function store(Request $request){

    }

    public function filterSpecialty(Request $request){
        $obj = SpecialtyCenter::where('cents_id', Auth::user()->centro->id)
        ->where('active',1)
        ->where('espes_id',$request['id'])
        ->with('type_care')->get();

        $res[] = array('respuesta' => true, 'type_care' => $obj);
        $dres = json_encode($res);
        return json_decode($dres, true);

    }

    public function viewDocument(Request $request){
        $id = $request->id;
        $obj = ClinicDocument::where('id', $id)->first();

        $event = Event::where('document_id', $id)->first();
        $event_id = $event->id;

        if(!$obj){
            Session::flash('data', array('type' => 'error', 'message' => __('messages.request.not_found')));
            return Redirect::to('control_table');
        }

        $specialty = Specialty::where('id', $obj->tipo_especialidad)->first();
        $type_care = AttentionType::where('id', $obj->tipo_solicitud)->first();
        $type = 'view';

        $dynamic_form = Form::where('specialty_id', $obj->tipo_especialidad)
        ->with('fields')
        ->with('specialty')
        ->first();

        return view('clinic_document.document', compact('id', 'type', 'specialty', 'type_care', 'dynamic_form', 'event_id'));

    }

    public function editDocument(Request $request){
        $id = $request->id;
        $obj = ClinicDocument::where('id', $id)->first();

        $event = Event::where('document_id', $id)->first();
        $event_id = $event->id;

        // Si otro usuario entra a modificar el documento.
        if($obj->bloqueo_documento != null && $obj->bloqueo_documento != Auth::user()->id){
            $userLock = User::find($obj->bloqueo_documento);
            Session::flash('data', array('type' => 'error', 'message' => __('messages.request.locked_by') . $userLock->name . ' ' . $userLock->last_name));
            return Redirect::to('control_table');
        }

        if(($obj->state_id == 1 || $obj->state_id == 2) && (Auth::user()->rol->id == 3)){ // Si es médico y el estado es pendiente o solicitado.

            $obj->state_id = 3;
            $current = Carbon::now();
            $obj->fecha_en_tratamiento = $current;
            $obj->save();
        }

        $specialty = Specialty::where('id', $obj->tipo_especialidad)->first();
        $type_care = AttentionType::where('id', $obj->tipo_solicitud)->first();
        $type = 'edit';

        $dynamic_form = Form::where('specialty_id', $obj->tipo_especialidad)
                        ->with('fields')
                        ->with('specialty')
                        ->first();

        $obj->bloqueo_documento = Auth::user()->id;
        $obj->update();

        $obj = ClinicDocument::where('id', $id)
        ->with('state')
        ->with('specialty')
        ->first();


        if(!$obj){
            Session::flash('data', array('type' => 'error', 'message' => __('messages.request.not_found')));
            return Redirect::to('control_table');
        }

        if($obj->state->id < 5){

            return view('clinic_document.document', compact('id', 'type', 'specialty', 'type_care', 'dynamic_form', 'event_id'));

        }else{
            Session::flash('data', array('type' => 'error', 'message' => __('messages.request.not_modifiable')));
            return Redirect::to('control_table');
        }
    }

    public function endRequest(Request $request){
        if($request->rol == 4){
            $obj = ClinicDocument::where('id', $request->id)->first();

            if($obj->state_id != 4){
                return response()->json(["status"=> 500, "message" => __('messages.request.treatment_not_end')])->setStatusCode(500);
            }

            $obj->state_id = 5;
            $current = Carbon::now();
            $obj->fecha_concluido = $current;
            $obj->save();

            try{
                // Notify user.
                $external_request = ExternalApp::select('id')->where('clinic_document_id', $request->id)->first();

                $tags = array(
                    ["field" => "tag", "key" => "user_id", "relation" => "=", "value" => $obj->user_id],
                    ["field" => "tag", "key" => "app_name", "relation" => "=", "value" => Config::get('app.name')]
                );

                $data = array(
                    "type" => "request_detail",
                    "id" => $external_request->id
                );

                $message = __('messages.request.notification_text_status');
                OneSignalController::sendToTags($message, $tags, null, $data, null, null, 'U');
            }
            catch(\Exception $e){}

            return response()->json(["status"=> 200, "message" => __('messages.request.end_consultation')]);
        }else{
            return response()->json(["status"=> 500, "message" => __('messages.request.nurse_type')])->setStatusCode(500);
        }
    }

    public function endTreatment(Request $request){
        if($request->rol == 3 || $request->rol == 6){
            $obj = ClinicDocument::where('id', $request->id)->first();
            $obj->state_id = 4;
            $current = Carbon::now();
            $obj->fecha_fin_tratamiento = $current;
            $obj->medico_especialista = $request->medico;
            $obj->medico_especialista_doc = $request->medico_especialista_doc;
            $obj->save();

            $event = Event::where('id', $request->event_id)->first();
            $event->event_state = 5;
            $event->update();

            try{
                // Notify user.
                $external_request = ExternalApp::select('id')->where('clinic_document_id', $request->id)->first();

                $tags = array(
                    ["field" => "tag", "key" => "user_id", "relation" => "=", "value" => $obj->user_id],
                    ["field" => "tag", "key" => "app_name", "relation" => "=", "value" => Config::get('app.name')]
                );

                $data = array(
                    "type" => "request_detail",
                    "id" => $external_request->id
                );

                $message = __('messages.request.notification_text_status');
                OneSignalController::sendToTags($message, $tags, null, $data, null, null, 'U');
            }
            catch(\Exception $e){}

            return response()->json(["status"=> 200, "message" =>  __('messages.request.end_treatment_by') . $request->medico]);
        }else{
            return response()->json(["status"=> 500, "message" => __('messages.request.user_not_medic')])->setStatusCode(500);
        }
    }

    public function createProposalDocument($id, $event_id){
        $hv = HeaderValue::where('id', $id)
            ->with('header_value_details')
            ->with('form')
            ->with('user')
            ->first();
            
        $specialty = Specialty::where('id', $hv->form->specialty_id)->first();
        $type_care = AttentionType::where('id', 1)->first();

        $type = 'propose';
        $a = 'app';

        $dynamic_form = Form::where('specialty_id', $hv->form->specialty_id)
                        ->with('fields')
                        ->with('specialty')
                        ->first();

        $u = User::where('id', Auth::user()->id)->with('centro')->first();

        $header_values = base64_encode( json_encode($hv) );
        $u = base64_encode( json_encode($u) );  

        return view('clinic_document.document', compact('id', 'type', 'specialty', 'type_care', 'dynamic_form', 'header_values', 'u', 'a', 'event_id'));
    }

    public function createProposalDocumentPortal($user_id, $speciality_id, $event_id){

        // $hv = HeaderValue::where('id', $id)
        //     ->with('header_value_details')
        //     ->with('form')
        //     ->with('user')
        //     ->first();
            
        $specialty = Specialty::where('id', $speciality_id)->first();
        $type_care = AttentionType::where('id', 1)->first();

        $type = 'propose';
        $a = 'not_app';

        // $dynamic_form = Form::where('specialty_id', $hv->form->specialty_id)
        //                 ->with('fields')
        //                 ->with('specialty')
        //                 ->first();

        $dynamic_form = null;

        $u = User::where('id', $user_id)->with('centro','city')->first();
        $u_document = $u->document;

        // $header_values = base64_encode( json_encode($hv) );
        $header_values = null;
        $u = base64_encode( json_encode($u) );  

        return view('clinic_document.document', compact('user_id', 'type', 'specialty', 'type_care', 'dynamic_form', 'header_values', 'u', 'a','u_document', 'event_id'));
    }

    public function changeState(Request $request, $id){
        $obj = ClinicDocument::where('id', $id)->first();
        $obj->state_id = $request->modal_state_id;
        $obj->text_change_state = $request->text_state;
        $obj->save();

        try{
            // Notify user.
            $external_request = ExternalApp::select('id')->where('clinic_document_id', $id)->first();

            $tags = array(
                ["field" => "tag", "key" => "user_id", "relation" => "=", "value" => $obj->user_id],
                ["field" => "tag", "key" => "app_name", "relation" => "=", "value" => Config::get('app.name')]
            );

            $data = array(
                "type" => "request_detail",
                "id" => $external_request->id
            );

            $message = __('messages.request.notification_text_status');
            OneSignalController::sendToTags($message, $tags, null, $data, null, null, 'U');
        }
        catch(\Exception $e){}
        return response()->json(["status"=> 200, "message" =>  __('messages.request.change_state'). $obj->id]);
    }

    public function setCancel(Request $request, $id){
        $obj = ClinicDocument::where('id', $id)->first();
        $obj->state_id = 6;
        $current = Carbon::now();
        $obj->fecha_cancelado = $current;
        $obj->template_response_id = $request->template_response_id;
        $obj->text_cancel = $request->text_cancel;
        $obj->save();

        return response()->json(["status"=> 200, "message" => __('messages.request.cancel_document') . $obj->id]);
    }



    public function viewCancel(Request $request){
        $obj = ClinicDocument::where('id', $request->id)->select('id', 'text_cancel', 'template_response_id')->first();

        return response()->json($obj);
    }

    public function autocomplete(Request $request)
    {
        $term = $request->input('term');
        $results = array();
        $term= strtoupper($term);

        $queries = DB::table('diagnostics')
        ->where('id', 'LIKE', '%' . $term . '%')
        ->take(15)
        ->get();

        foreach ($queries as $query) {
            $results[] = ['value' => $query->id,
            'id' => $query->description,
            'description' => $query->description,
        ];
    }
    return json_encode($results);
}

public function autocompleteDescription(Request $request)
{
    $term = $request->input('term');
    $term= str_replace('*','%',$term);
    $results = array();
    $term= strtoupper($term);

    $queries = DB::table('diagnostics')
    ->where('description', 'LIKE',  $term )
    ->take(15)
    ->get();

    foreach ($queries as $query) {

        $results[] = ['id' => $query->id,
        'value' => $query->description,
        'codigo' => $query->id,
    ];
}
return json_encode($results);
}

public function autocompletePaciente(Request $request)
{
    $term = $request->input('term');
    $results = array();

    $queries = User::where('document', 'LIKE', $term . '%')
                ->where('rol_id', 9)
                ->where('active', true)
                ->with('city')
                ->with('insurance')
                ->take(10)
                ->get();

    foreach ($queries as $query) {

        $results[] = [
            'id' => $query->id,
            'value' => $query->document,
            'nombre1' => $query->name,
            'nombre2' => $query->name,
            'apellido1' => $query->last_name,
            'apellido2' => $query->last_name,
            'direccion' => $query->address,
            'tipo_documento' =>$query->identification_type_id,
            'country_id' => isset($query->city) ? $query->city->department->country->id : null,
            'department_id' =>  isset($query->city) ?  $query->city->department->id : null,
            'city_id' => isset($query->city) ? $query->city->id : null,
            'sexo' => $query->sex,
            'fecha_nacimiento' => $query->dob,
            'telefono' => $query->phone,
            'aseguradora' => $query->insurance_id,
            'email' => $query->email,
            'rate' => $query->insurance ? $query->insurance->rate : 0
         ];
    }

    return json_encode($results);
}

  public function dataRequest(Request $request){
    return ClinicDocument::where('id', $request->id)
        ->with('ginecoobstetricia')
        ->with('files')
        ->with('fcf')
        ->with('diagnostics_all')
        ->with('specialty')
        ->with('state')
        ->with('type_care')
        ->with('insurance')
        ->with('department')
        ->with('city')
        ->first();
  }

  public function unlockDocument($id){
    $obj = ClinicDocument::where('id', $id)->first();

    if( $obj->bloqueo_documento == Auth::user()->id || Auth::user()->rol->id == 1){ // Si es el usuario o un admin.
        $obj->bloqueo_documento = null;
        $obj->save();

        $message = __('messages.request.document_lock');
        $message = str_replace('<id>', $obj->id, $message);

        $data = [
            "r"=>true,
            "status" => "200",
            "id" => $obj->id,
            "message" => $message
        ];
      }else{
        $message = __('messages.request.user_not_valid');
        $message = str_replace('<id>', $obj->id, $message);

        $data = [
            "r"=>false,
            "status" => "200",
            "id" => $obj->id,
            "message" => $message
        ];

      }
    return response()->json($data);

}

}