440 lines
15 KiB
PHP
440 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Integracao;
|
|
|
|
use App\Exceptions\Integracao\Rira\CertificadoDigitalNaoEncontradoException;
|
|
use App\Models\Integracao\Rira\Parametrizacao;
|
|
use App\Services\Service;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
class RiraService extends Service
|
|
{
|
|
public static function getParametrizacao()
|
|
{
|
|
return Parametrizacao::select(['senha', 'identificador_solicitante', 'cns_solicitante'])->first();
|
|
}
|
|
|
|
public static function send($cliente, $headers, $params)
|
|
{
|
|
$url = (env('APP_ENV') === 'production'
|
|
? Str::replace('uf-', Str::lower($cliente->municipio->estado->uf) . '-', env('RIRA_PRODUCAO_URL_API_SERVICO'))
|
|
: env('RIRA_HOMOLOGACAO_URL_API_SERVICO')
|
|
) . '/fhir/r4/Bundle';
|
|
|
|
$params->tipo_documento = 'RA';
|
|
$params->status_documento = 'final';
|
|
$params->titulo_documento = 'Registro de Informações da Regulação Assistencial';
|
|
$params->modalidade_assistencial = '09';
|
|
$params->composition_full_url = 'urn:uuid:transient-0';
|
|
$params->appointment_full_url = 'urn:uuid:transient-1';
|
|
$params->service_request_full_url = 'urn:uuid:transient-2';
|
|
$params->condition_full_url = 'urn:uuid:transient-3';
|
|
|
|
$params->request = [
|
|
'resourceType' => 'Bundle',
|
|
'identifier' => [
|
|
'system' => "http://www.saude.gov.br/fhir/r4/NamingSystem/BRRNDS-{$params->identificador_solicitante}",
|
|
'value' => $params->identificador_documento
|
|
],
|
|
'type' => 'document',
|
|
'timestamp' => $params->data_hora_envio->toIso8601String(),
|
|
'meta' => [
|
|
'lastUpdated' => $params->data_hora_envio->toIso8601String()
|
|
],
|
|
'entry' => [
|
|
[
|
|
'fullUrl' => $params->composition_full_url,
|
|
'resource' => self::getFhirComposition($params),
|
|
],
|
|
[
|
|
'fullUrl' => $params->appointment_full_url,
|
|
'resource' => self::getFhirAppointment($params),
|
|
],
|
|
[
|
|
'fullUrl' => $params->service_request_full_url,
|
|
'resource' => self::getFhirServiceRequest($params),
|
|
],
|
|
[
|
|
'fullUrl' => $params->condition_full_url,
|
|
'resource' => self::getFhirCondition($params),
|
|
],
|
|
]
|
|
];
|
|
|
|
return Http::withHeaders($headers)->post($url, $params->request);
|
|
}
|
|
|
|
private static function getFhirComposition($params)
|
|
{
|
|
$detailEvent = [];
|
|
|
|
if ($params->status_composicao === 'pending') {
|
|
|
|
$detailEvent[] = ['reference' => $params->service_request_full_url];
|
|
}
|
|
else if (in_array($params->status_composicao, ['booked', 'attended', 'absence'])) {
|
|
|
|
$detailEvent[] = ['reference' => $params->appointment_full_url];
|
|
}
|
|
|
|
$detailEvent[] = [
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BREstabelecimentoSaude-1.0',
|
|
'value' => $params->cnes_responsavel_status_composicao
|
|
]
|
|
];
|
|
|
|
$data = [
|
|
'resourceType' => 'Composition',
|
|
'meta' => [
|
|
'lastUpdated' => $params->data_hora_envio->toIso8601String(),
|
|
'profile' => [
|
|
'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRRegulacaoAssistencial'
|
|
]
|
|
],
|
|
'status' => $params->status_documento,
|
|
'type' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRTipoDocumento',
|
|
'code' => $params->tipo_documento
|
|
]
|
|
]
|
|
],
|
|
'category' => [
|
|
[
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRModalidadeAssistencial',
|
|
'code' => $params->modalidade_assistencial
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'subject' => [
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRIndividuo-1.0',
|
|
'value' => $params->paciente_cns ?? $params->paciente_cpf
|
|
]
|
|
],
|
|
'date' => $params->data_hora_envio->format('Y-m-d'),
|
|
'author' => [
|
|
[
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BREstabelecimentoSaude-1.0',
|
|
'value' => $params->cnes_responsavel_status_composicao
|
|
]
|
|
]
|
|
],
|
|
'title' => $params->titulo_documento,
|
|
'event' => [
|
|
[
|
|
'code' => [
|
|
[
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRStatusRegulacaoAssistencial',
|
|
'code' => $params->status_composicao
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'period' => [
|
|
'start' => $params->data_solicitacao->toIso8601String(),
|
|
'end' => $params->data_solicitacao->toIso8601String()
|
|
],
|
|
'detail' => $detailEvent
|
|
],
|
|
],
|
|
'section' => [
|
|
[
|
|
'entry' => [
|
|
[
|
|
'reference' => $params->appointment_full_url
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
|
|
if (isset($params->ultimo_identificador_rnds)) {
|
|
|
|
$data['relatesTo'] = [
|
|
[
|
|
'code' => 'replaces',
|
|
'targetReference' => [
|
|
'reference' => "Composition/{$params->ultimo_identificador_rnds}"
|
|
]
|
|
]
|
|
];
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
private static function getFhirAppointment($params)
|
|
{
|
|
$data = [
|
|
'resourceType' => 'Appointment',
|
|
'meta' => [
|
|
'lastUpdated' => $params->data_hora_envio->toIso8601String(),
|
|
'profile' => [
|
|
'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRAgendamentoRegulacaoAssistencial'
|
|
]
|
|
],
|
|
'status' => $params->status_consulta,
|
|
'serviceCategory' => [
|
|
[
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRModalidadeAssistencial',
|
|
'code' => $params->modalidade_assistencial
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'serviceType' => [
|
|
[
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRTabelaSUS',
|
|
'code' => $params->codigo_procedimento
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'appointmentType' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://hl7.org/fhir/request-priority',
|
|
'code' => $params->prioridade
|
|
]
|
|
]
|
|
],
|
|
'reasonReference' => [
|
|
[
|
|
'reference' => $params->condition_full_url
|
|
]
|
|
],
|
|
'basedOn' => [
|
|
[
|
|
'reference' => $params->service_request_full_url
|
|
]
|
|
],
|
|
'participant' => [
|
|
[
|
|
'type' => [
|
|
[
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRTipoParticipante',
|
|
'code' => 'PCT'
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'actor' => [
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRIndividuo-1.0',
|
|
'value' => $params->paciente_cns ?? $params->paciente_cpf
|
|
]
|
|
],
|
|
'status' => $params->status_paciente_consulta,
|
|
]
|
|
]
|
|
];
|
|
|
|
if (isset($params->data_hora_agendada)) {
|
|
|
|
$data['start'] = $params->data_hora_agendada->toIso8601String();
|
|
$data['end'] = $params->data_hora_agendada->toIso8601String();
|
|
}
|
|
|
|
if (isset($params->cbo_prestador)) {
|
|
|
|
$data['specialty'] = [
|
|
[
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRCBO',
|
|
'code' => $params->cbo_prestador
|
|
]
|
|
]
|
|
]
|
|
];
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
private static function getFhirServiceRequest($params)
|
|
{
|
|
$data = [
|
|
'resourceType' => 'ServiceRequest',
|
|
'meta' => [
|
|
'lastUpdated' => $params->data_hora_envio->toIso8601String(),
|
|
'profile' => [
|
|
'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRRequisicaoRegulacaoAssistencial'
|
|
]
|
|
],
|
|
'status' => $params->status_requisicao,
|
|
'intent' => $params->intencao_requisicao,
|
|
'category' => [
|
|
[
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRModalidadeAssistencial',
|
|
'code' => $params->modalidade_assistencial
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'priority' => $params->prioridade,
|
|
'code' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRTabelaSUS',
|
|
'code' => $params->codigo_procedimento
|
|
]
|
|
]
|
|
],
|
|
'subject' => [
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRIndividuo-1.0',
|
|
'value' => $params->paciente_cns ?? $params->paciente_cpf
|
|
]
|
|
],
|
|
'authoredOn' => $params->data_solicitacao->format('Y-m-d'),
|
|
'requester' => [
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BREstabelecimentoSaude-1.0',
|
|
'value' => $params->cnes_solicitante
|
|
]
|
|
],
|
|
'reasonReference' => [
|
|
[
|
|
'reference' => $params->condition_full_url
|
|
]
|
|
]
|
|
];
|
|
|
|
if (isset($params->data_execucao)) {
|
|
|
|
$data['occurrenceDateTime'] = $params->data_execucao->format('Y-m-d');
|
|
}
|
|
|
|
if (isset($params->cbo_prestador)) {
|
|
|
|
$data['performerType'] = [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRCBO',
|
|
'code' => $params->cbo_prestador
|
|
]
|
|
]
|
|
];
|
|
}
|
|
|
|
if (isset($params->cnes_prestador)) {
|
|
|
|
$data['performer'] = [
|
|
[
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BREstabelecimentoSaude-1.0',
|
|
'value' => $params->cnes_prestador
|
|
]
|
|
]
|
|
];
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
private static function getFhirCondition($params)
|
|
{
|
|
$data = [
|
|
'resourceType' => 'Condition',
|
|
'meta' => [
|
|
'lastUpdated' => $params->data_hora_envio->toIso8601String(),
|
|
'profile' => [
|
|
'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRCID10Avaliado-1.0'
|
|
]
|
|
],
|
|
'clinicalStatus' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://terminology.hl7.org/CodeSystem/condition-clinical',
|
|
'code' => $params->status_condicao
|
|
]
|
|
]
|
|
],
|
|
'category' => [
|
|
[
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRCategoriaDiagnostico',
|
|
'code' => $params->categoria_condicao->codigo,
|
|
'display' => $params->categoria_condicao->exibicao
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'code' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRCID10',
|
|
'code' => $params->cid_codigo
|
|
]
|
|
]
|
|
],
|
|
'subject' => [
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRIndividuo-1.0',
|
|
'value' => $params->paciente_cns ?? $params->paciente_cpf
|
|
]
|
|
]
|
|
];
|
|
|
|
if (isset($params->hipotese_diagnostica_justificativa)) {
|
|
|
|
$data['note'] = [
|
|
[
|
|
'text' => $params->hipotese_diagnostica_justificativa
|
|
]
|
|
];
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public static function getToken($senha)
|
|
{
|
|
$path = self::getPath() . DIRECTORY_SEPARATOR . self::getFile();
|
|
$url = env('APP_ENV') === 'production' ? env('RIRA_PRODUCAO_URL_API_TOKEN') : env('RIRA_HOMOLOGACAO_URL_API_TOKEN');
|
|
|
|
if (Storage::disk('local')->exists($path)) {
|
|
return Http::withOptions(['cert' => [Storage::path($path), $senha]])->get($url)->json();
|
|
} else {
|
|
throw new CertificadoDigitalNaoEncontradoException();
|
|
}
|
|
}
|
|
|
|
public static function getPath()
|
|
{
|
|
return 'integracao'
|
|
. DIRECTORY_SEPARATOR
|
|
. 'rira'
|
|
. DIRECTORY_SEPARATOR
|
|
. 'certificado-digital'
|
|
. DIRECTORY_SEPARATOR
|
|
. app('config')->get('database.default');
|
|
}
|
|
|
|
public static function getFile()
|
|
{
|
|
return 'CERTIFICADO-DIGITAL.PEM';
|
|
}
|
|
}
|