352 lines
14 KiB
PHP
352 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Integracao;
|
|
|
|
use App\Enums\Integracao\Ria\CondicaoMaternal;
|
|
use App\Enums\Integracao\Ria\Estrategia;
|
|
use App\Enums\Integracao\Ria\GrupoAtendimento;
|
|
use App\Enums\Integracao\Ria\LocalAplicacao;
|
|
use App\Enums\Integracao\Ria\ViaAdministracao;
|
|
use App\Exceptions\Integracao\Ria\CertificadoDigitalNaoEncontradoException;
|
|
use App\Models\Integracao\Ria\Parametrizacao;
|
|
use App\Services\Service;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
class RiaService extends Service
|
|
{
|
|
public static function getParametrizacao()
|
|
{
|
|
return Parametrizacao::select(['senha', 'identificador_solicitante', 'cns_solicitante'])
|
|
->first();
|
|
}
|
|
|
|
public static function sendDocumentoImunologicoRotina($headers, $params, $uf)
|
|
{
|
|
$url = 'https://' . (env('APP_ENV') === 'production' ? Str::lower($uf) . '-' . env('RIA_PRODUCAO_URL_API_SERVICO') : env('RIA_HOMOLOGACAO_URL_API_SERVICO')) . '/fhir/r4/Bundle';
|
|
|
|
$fhirComposition = self::getFhirComposition($params);
|
|
$fhirImunization = self::getFhirImunization($params);
|
|
$fhirConditiion = self::getFhirConditiion($params);
|
|
|
|
$entry = [];
|
|
|
|
if (!is_null($fhirComposition)) {
|
|
$entry[] = $fhirComposition;
|
|
}
|
|
|
|
if (!is_null($fhirImunization)) {
|
|
$entry[] = $fhirImunization;
|
|
}
|
|
|
|
if (!is_null($fhirConditiion)) {
|
|
$entry[] = $fhirConditiion;
|
|
}
|
|
|
|
$payload = [
|
|
'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(),
|
|
'entry' => $entry,
|
|
];
|
|
|
|
return Http::withHeaders($headers)->post($url, $payload);
|
|
}
|
|
|
|
public static function getToken($senha)
|
|
{
|
|
$path = self::getPath() . DIRECTORY_SEPARATOR . self::getFile();
|
|
|
|
$url = 'https://' . (env('APP_ENV') === 'production' ? env('RIA_PRODUCAO_URL_API_TOKEN') : env('RIA_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 . 'ria' . DIRECTORY_SEPARATOR . 'certificado-digital' . DIRECTORY_SEPARATOR . app('config')->get('database.default');
|
|
}
|
|
|
|
public static function getFile()
|
|
{
|
|
return 'CERTIFICADO-DIGITAL.PEM';
|
|
}
|
|
|
|
private static function getFhirComposition($params)
|
|
{
|
|
$data = [
|
|
'fullUrl' => $params->identificador_composicao,
|
|
'resource' => [
|
|
'resourceType' => 'Composition',
|
|
'id' => (string) $params->identificador_composicao,
|
|
'meta' => [
|
|
'profile' => [
|
|
'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRRegistroImunobiologicoAdministradoRotina-2.0',
|
|
],
|
|
],
|
|
'status' => 'final',
|
|
'type' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRTipoDocumento',
|
|
'code' => 'RIA',
|
|
]
|
|
]
|
|
],
|
|
'subject' => [
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRIndividuo-1.0',
|
|
'value' => (string) $params->paciente_cns_ou_cpf,
|
|
],
|
|
],
|
|
'date' => $params->data_hora_envio->toIso8601String(),
|
|
'author' => [
|
|
[
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BREstabelecimentoSaude-1.0',
|
|
'value' => (string) $params->unidade_cnes,
|
|
],
|
|
],
|
|
],
|
|
'title' => 'Registro de Imunobiologico Administrado na Rotina',
|
|
'section' => [
|
|
[
|
|
'entry' => [
|
|
[
|
|
'reference' => 'Immunization/' . $params->identificador_imunizacao,
|
|
],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
];
|
|
|
|
if (isset($params->is_substituicao) && $params->is_substituicao) {
|
|
|
|
$data['resource']['relatesTo'] = [
|
|
[
|
|
'code' => 'replaces',
|
|
'targetReference' => [
|
|
'reference' => 'Composition/' . $params->identificador_rnds
|
|
]
|
|
]
|
|
];
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
private static function getFhirImunization($params)
|
|
{
|
|
$imunization = [
|
|
'fullUrl' => $params->identificador_imunizacao,
|
|
'resource' => [
|
|
'resourceType' => 'Immunization',
|
|
'id' => (string) $params->identificador_imunizacao,
|
|
'meta' => [
|
|
'profile' => [
|
|
'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRImunobiologicoAdministrado-3.0',
|
|
],
|
|
],
|
|
'extension' => [
|
|
[
|
|
'url' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRGrupoAtendimento',
|
|
'valueCodeableConcept' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRGrupoAtendimento',
|
|
'code' => (string) ($params->grupo_atendimento_codigo ?? GrupoAtendimento::OUTROS),
|
|
],
|
|
],
|
|
],
|
|
],
|
|
[
|
|
'url' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRCondicaoMaternal',
|
|
'valueCodeableConcept' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRCondicaoMaternal',
|
|
'code' => (string) ($params->gestante ? CondicaoMaternal::GESTANTE : ($params->puerpera ? CondicaoMaternal::PUERPERA : CondicaoMaternal::NAO_SE_APLICA)),
|
|
],
|
|
],
|
|
],
|
|
],
|
|
[
|
|
'url' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRContatoHanseniase',
|
|
'valueBoolean' => (Boolean) $params->comunicante_hanseniase,
|
|
],
|
|
],
|
|
'status' => 'completed',
|
|
'vaccineCode' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRImunobiologico',
|
|
'code' => (string) $params->vacina_codigo,
|
|
],
|
|
],
|
|
],
|
|
'patient' => [
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRIndividuo-1.0',
|
|
'value' => (string) $params->paciente_cns_ou_cpf,
|
|
],
|
|
],
|
|
'occurrenceDateTime' => $params->data,
|
|
'primarySource' => true,
|
|
'site' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRLocalAplicacao',
|
|
'code' => (string) ($params->local_aplicacao_codigo ?? LocalAplicacao::OUTRO),
|
|
],
|
|
],
|
|
],
|
|
'route' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRViaAdministracao',
|
|
'code' => (string) ($params->via_administracao_codigo ?? ViaAdministracao::SEM_REGISTRO_NO_SISTEMA_DE_INFORMACAO_DE_ORIGEM),
|
|
],
|
|
],
|
|
],
|
|
'performer' => [
|
|
[
|
|
array_filter([
|
|
'function' => $params->estrategia_codigo == 2 ? [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRCBO',
|
|
'code' => $params->cbo ?? null,
|
|
]
|
|
]
|
|
] : null,
|
|
'actor' => [
|
|
'reference' => 'Practitioner/' . $params->aplicador_cns_ou_cpf,
|
|
],
|
|
])
|
|
],
|
|
],
|
|
'protocolApplied' => [
|
|
[
|
|
'extension' => [
|
|
[
|
|
'url' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BREstrategiaVacinacao-1.0',
|
|
'valueCodeableConcept' => [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BREstrategiaVacinacao',
|
|
'code' => (string) $params->estrategia_codigo
|
|
],
|
|
],
|
|
],
|
|
],
|
|
[
|
|
'url' => 'http://www.saude.gov.br/fhir/r4/StructureDefinition/BREstrategiaVacinacaoPesquisa-1.0',
|
|
'extension' => [
|
|
[
|
|
'url' => 'numeroProtocoloEstudoANVISA',
|
|
'valueString' => 'GU1AMT9CYR',
|
|
],
|
|
[
|
|
'url' => 'numeroVersaoProtocoloEstudo',
|
|
'valueString' => 'F3D1U354V6',
|
|
],
|
|
[
|
|
'url' => 'numeroRegistroVacinaAnvisa',
|
|
'valueString' => 'WWZZJJWIBF',
|
|
],
|
|
],
|
|
],
|
|
],
|
|
'doseNumberString' => (string) $params->dose_codigo,
|
|
],
|
|
],
|
|
],
|
|
];
|
|
|
|
if ($params->estrategia_codigo === Estrategia::ESPECIAL) {
|
|
$imunization['resource']['reasonReference'] = [
|
|
[
|
|
'reference' => 'Condition/' . $params->identificador_condicao,
|
|
],
|
|
];
|
|
}
|
|
|
|
if (isset($params->registro_aplicacao_id)) {
|
|
$imunization['resource']['lotNumber'] = (string) $params->lote;
|
|
$imunization['resource']['expirationDate'] = $params->data_validade_lote;
|
|
|
|
$imunization['resource']['manufacturer'] = [
|
|
'identifier' => [
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRFabricantePNI',
|
|
'value' => (string) $params->produtor_codigo,
|
|
],
|
|
];
|
|
}
|
|
|
|
if (isset($params->registro_atualizacao_id)) {
|
|
$imunization['resource']['reportOrigin'] = [
|
|
'coding' => [
|
|
[
|
|
'system' => 'http://www.saude.gov.br/fhir/r4/CodeSystem/BRRegistroOrigem',
|
|
'code' => '01',
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
return $imunization;
|
|
}
|
|
|
|
private static function getFhirConditiion($params)
|
|
{
|
|
$condition = null;
|
|
|
|
if ($params->estrategia_codigo === Estrategia::ESPECIAL) {
|
|
$condition = [
|
|
'fullUrl' => $params->identificador_condicao,
|
|
'resource' => [
|
|
'resourceType' => 'Condition',
|
|
'id' => (string) $params->identificador_condicao,
|
|
'meta' => [
|
|
'lastUpdated' => '2024-03-30T14:59:51.283-03:00',
|
|
'profile' => [
|
|
'http://www.saude.gov.br/fhir/r4/StructureDefinition/BRCID10Avaliado-1.0',
|
|
],
|
|
],
|
|
'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' => (string) $params->paciente_cns_ou_cpf,
|
|
],
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
return $condition;
|
|
}
|
|
|
|
public static function destroy($headers, $resourceUrl)
|
|
{
|
|
return Http::withHeaders($headers)->delete($resourceUrl);
|
|
}
|
|
}
|