File: /var/www/vhost/disk-apps/dev-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');
}
}
}