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/demo-telemedicina.teky.com.co/app/Http/Controllers/XMLController.php
<?php

namespace Telemedicina\Http\Controllers;

use Illuminate\Http\Request;
use Telemedicina\ClinicDocument;
use Telemedicina\Diagnostic;
use SoapBox\Formatter\Formatter;
use Carbon\Carbon;

class XMLController extends Controller
{

    /*
    * Se expone en formato XML para la integración con SAP.
    */
    public function sendHCJSON(Request $request){
        $hc = ClinicDocument::find($request->id);

        if(is_null($hc)){
            $message =  __('messages.xml_ctrl.not_found') . $request->id;
            $notFound = array('message' => $message, 'code' => 400);

            $logId = $this->registerLog($notFound['message'], 'Sync', 'ERROR', '');
            $notFound['logId'] = $logId;

            $formatter = Formatter::make(json_encode($notFound), Formatter::JSON);

            return response($formatter->toXML())
                ->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0")
                ->header('Content-Type', 'text/xml');
        }

        // Diagnosticos
        $diags = $hc->diagnostics;
        $hc['str_diagnostics_hcs'] = $diags;

        // FCFS
        $fcf = $hc->fcf;
        $hc['str_go_cardiac_frecuencies'] = $fcf;

        // Anexos
        $files = $hc->files;
        $hc['str_zhc_externa_att'] = $files;

        // ginecoobstetricia
        $ginecoobstetricia = $hc->ginecoobstetricia;
        $hc['str_ginecoobstetricias'] = $ginecoobstetricia;

        if($hc){
            return response()->json($hc);
        }else{
            $message = __('messages.xml_ctrl.not_found') . $request->id;
            $notFound = array('message' => $message, 'code' => 400);

            $logId = $this->registerLog($notFound['message'], 'Sync', 'ERROR', '');

            $notFound['logId'] = $logId;

            $formatter = Formatter::make(json_encode($notFound), Formatter::JSON);

            return response($formatter->toXML())
                ->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0")
                ->header('Content-Type', 'text/xml');
        }
    }

    /*
    * Se expone en formato XML para la integración con SAP.
    */
    public function sendHC(Request $request){
        $hc = ClinicDocument::find($request->id);

        if(is_null($hc)){
            $message = __('messages.xml_ctrl.not_found') . $request->id;
            $notFound = array('message' => $message, 'code' => 400);

            $logId = $this->registerLog($notFound['message'], 'Sync', 'ERROR', '');
            $notFound['logId'] = $logId;

            $formatter = Formatter::make(json_encode($notFound), Formatter::JSON);

            return response($formatter->toXML())
                ->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0")
                ->header('Content-Type', 'text/xml');
        }

        // Diagnosticos
        $diags = $hc->diagnostics;
        $hc['str_diagnostics_hcs'] = $diags;

        // FCFS
        $fcf = $hc->fcf;
        $hc['str_go_cardiac_frecuencies'] = $fcf;

        // Anexos
        $files = $hc->files;
        $hc['str_zhc_externa_att'] = $files;

        // ginecoobstetricia
        $ginecoobstetricia = $hc->ginecoobstetricia;
        $hc['str_ginecoobstetricias'] = $ginecoobstetricia;

        if($hc){
            $formatter = Formatter::make($hc->toJson(), Formatter::JSON);
            return response($formatter->toXml())
                ->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0")
                ->header('Content-Type', 'text/xml');
        }else{
            $message = __('messages.xml_ctrl.not_found') . $request->id;
            $notFound = array('message' => $message, 'code' => 400);

            $logId = $this->registerLog($notFound['message'], 'Sync', 'ERROR', '');

            $notFound['logId'] = $logId;

            $formatter = Formatter::make(json_encode($notFound), Formatter::JSON);

            return response($formatter->toXML())
                ->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0")
                ->header('Content-Type', 'text/xml');
        }
    }

    public function getPendingHC(Request $request){
        $hc = ClinicDocument::where('sync', false)
        ->where('state_id', 5)
        ->select('id', 'state_id', 'created_by', 'tipo_solicitud', 'tipo_especialidad')->orderBy('id')->get();

        if(sizeof($hc) > 0 ){
            $formatter = Formatter::make($hc, Formatter::JSON);
            return response($formatter->toXML())
                ->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0")
                ->header('Content-Type', 'text/xml');
        }else{
            $notFound = array('message' => __('messages.xml_ctrl.not_pending'), 'code' => 400);
            $formatter = Formatter::make(json_encode($notFound), Formatter::JSON);
            return response($formatter->toXML())
                ->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0")
                ->header('Content-Type', 'text/xml');
        }
    }

    public function handshakeSync(Request $request){
        try{
            $sapID = $request->sap_id;
            $hc = ClinicDocument::find($request->id);

            if(is_null($hc)){
                $notFound = array('message' => __('messages.xml_ctrl.not_id_found'), 'code' => 400);
                $formatter = Formatter::make(json_encode($notFound), Formatter::JSON);
                return response($formatter->toXML())
                    ->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0")
                    ->header('Content-Type', 'text/xml');
            }

            $hc->sap_id = $sapID;
            $hc->sync = true;
            $hc->date_sync = Carbon::now();
            $hc->save();

            $message = 'Sync ok. SAP ID:' . $request->sap_id .' ID:' . $request->id;
            $logId = $this->registerLog($message, 'Sync', 'ok', '');
            $notFound = array('message' => $message, 'code' => 200, 'logId' => $logId);

            $formatter = Formatter::make($hc->toJson(), Formatter::JSON);
            return response($formatter->toXML())
                ->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0")
                ->header('Content-Type', 'text/xml');
        }
        catch(Exception $e) {
            $message =  __('messages.xml_ctrl.error_external_id') . $request->sap_id .' ID:' . $request->id. '\n' . $e->getMessage();
            $logId = $this->registerLog($message, 'Sync', 'ERROR', '');
            $notFound = array('message' => $message, 'code' => 400, 'logId' => $logId);

            $formatter = Formatter::make(json_encode($notFound), Formatter::JSON);
            return response($formatter->toXML())
                ->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0")
                ->header('Content-Type', 'text/xml');
        }
    }
}