204 lines
9.6 KiB
PHP
204 lines
9.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Integracao\Erpsaude;
|
|
|
|
use App\Enums\Intranet\Sistema;
|
|
use App\Models\Integracao\Erpsaude\Atendimento as ErpsaudeAtendimento;
|
|
use App\Models\Integracao\Erpsaude\Exame as ErpsaudeExame;
|
|
use App\Models\Integracao\Erpsaude\Medicamento;
|
|
use App\Models\Integracao\Erpsaude\Paciente as ErpsaudePaciente;
|
|
use App\Models\Integracao\Erpsaude\Reavaliacao;
|
|
use App\Models\Integracao\Erpsaude\TipoAlta;
|
|
use App\Models\Integracao\Erpsaude\TipoAtendimento as ErpsaudeTipoAtendimento;
|
|
use App\Models\Integracao\Erpsaude\UnidadeAtendimento;
|
|
use App\Models\Integracao\Erpsaude\UnidadeEncaminhadora;
|
|
use App\Models\Intranet\Cliente;
|
|
use Carbon\Carbon;
|
|
use Exception;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class BuscarAtendimentosHospitalSantaCasaCommand extends Command
|
|
{
|
|
protected $signature = 'integracao:erpsaude-buscar-atendimentos-hospital-santa-casa';
|
|
protected $description = 'Comando para buscar os atendimentos realizados no hospital santa casa.';
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
|
|
try {
|
|
$cliente = Cliente::where('link', 'santarita')
|
|
->where('sistema_id', Sistema::CONECTASUS)
|
|
->firstOrFail();
|
|
|
|
Config::set('database.default', $cliente->link);
|
|
|
|
$token = env('ERPSAUDE_ACCESS_TOKEN');
|
|
$url = env('ERPSAUDE_ENDPOINT_URL') . "DadosPaciente";
|
|
|
|
$response = Http::withHeaders([
|
|
'Authorization' => "Bearer $token"
|
|
])->get($url);
|
|
|
|
$dados = $response->json();
|
|
|
|
if (!is_array($dados)) {
|
|
Log::channel('integracao-erpsaude')->error('A resposta da API não retornou um array válido', [
|
|
'cliente' => $cliente->link,
|
|
'origem' => 'BuscarAtendimentosHospitalSantaCasaCommand',
|
|
'dados' => $dados,
|
|
]);
|
|
return;
|
|
}
|
|
|
|
if ($response->successful()) {
|
|
foreach ($dados as $item) {
|
|
|
|
$cpf = isset($item['cpf']) ? substr(preg_replace('/\D/', '', $item['cpf']), 0, 11) : null;
|
|
$dataNascimento = Carbon::createFromFormat('d/m/Y', $item['dataNascimento'])->format('Y-m-d');
|
|
$cns = $item['cartaoNacionalSaude'];
|
|
$nome = $item['paciente'];
|
|
|
|
$paciente = null;
|
|
|
|
if ($cpf) {
|
|
$paciente = ErpsaudePaciente::where('cpf', $cpf)
|
|
->where('nome', $nome)
|
|
->where('data_nascimento', $dataNascimento)
|
|
->first();
|
|
}
|
|
|
|
if (!$paciente) {
|
|
$paciente = ErpsaudePaciente::where('cns', $cns)
|
|
->where('nome', $nome)
|
|
->where('data_nascimento', $dataNascimento)
|
|
->first();
|
|
}
|
|
|
|
if (!$paciente) {
|
|
$paciente = ErpsaudePaciente::create([
|
|
'nome' => $nome,
|
|
'cpf' => $cpf,
|
|
'cns' => $cns,
|
|
'data_nascimento' => $dataNascimento,
|
|
'sexo' => $item['sexo'],
|
|
'cep' => isset($item['cep']) ? preg_replace('/\D/', '', $item['cep']) : null,
|
|
]);
|
|
} else {
|
|
|
|
$paciente->update([
|
|
'nome' => $nome,
|
|
'cpf' => $cpf,
|
|
'cns' => $cns,
|
|
'data_nascimento' => $dataNascimento,
|
|
'sexo' => $item['sexo'],
|
|
'cep' => isset($item['cep']) ? preg_replace('/\D/', '', $item['cep']) : null,
|
|
]);
|
|
}
|
|
|
|
$tipoAlta = TipoAlta::where('codigo', $item['codTipoAlta'])->first();
|
|
$tipoAtendimento = ErpsaudeTipoAtendimento::where('codigo', (int) $item['codTipoAtendimento'])->first();
|
|
$unidadeEncaminhadora = UnidadeEncaminhadora::where('codigo', (int) $item['codUnEncaminhadora'])->first();
|
|
$unidadeAtendimento = UnidadeAtendimento::where('codigo', (int) $item['codUnAtendimento'])->first();
|
|
|
|
$atendimento = ErpsaudeAtendimento::create([
|
|
'paciente_id' => $paciente->id,
|
|
'registro_paciente' => $item['registroPaciente'],
|
|
'data' => Carbon::createFromFormat('d/m/Y', $item['data'])->format('Y-m-d'),
|
|
'hora' => $item['hora'],
|
|
'suspeita_maus_tratos' => $item['suspeitaMausTratos'],
|
|
'hipotese_diagnostica' => $item['hipoteseDiagnostica'],
|
|
'cid10' => $item['cid10'],
|
|
'descricao_procedimento' => $item['descricaoProcedimento'],
|
|
'descricao_exame_clinico' => $item['descricaoExameClinico'],
|
|
'conduta_medica' => $item['condutaMedica'],
|
|
'codigo_conta_prof' => $item['codigoContaProf'],
|
|
'profissional' => $item['profissional'],
|
|
'conselho' => $item['conselho'],
|
|
'uf_conselho' => $item['ufConselho'],
|
|
'convenio' => $item['convenio'],
|
|
'registro_ans' => $item['registroAns'],
|
|
'cod_convenio' => $item['codConvenio'],
|
|
'nome_setor' => $item['nomeSetor'],
|
|
'codigo_setor' => $item['codigoSetor'],
|
|
'cod_tipo_encaminhamento' => $item['codTipoEncaminhamento'],
|
|
'tipo_atendimento_id' => $tipoAtendimento ? $tipoAtendimento->id : null,
|
|
'cod_grupo_atendimento' => $item['codGrupoAtendimento'],
|
|
'unidade_encaminhadora_id' => $unidadeEncaminhadora ? $unidadeEncaminhadora->id : null,
|
|
'unidade_atendimento_id' => $unidadeAtendimento ? $unidadeAtendimento->id : null,
|
|
'data_atendimento' => Carbon::createFromFormat('d/m/Y', $item['dataAtendimento'])->format('Y-m-d'),
|
|
'data_alta' => !empty($item['dataAlta']) && preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $item['dataAlta'])
|
|
? Carbon::createFromFormat('d/m/Y', $item['dataAlta'])->format('Y-m-d')
|
|
: null,
|
|
'horario_atendimento' => $item['horarioAtendimento'],
|
|
'hora_alta' => isset($item['horaAlta']) && trim($item['horaAlta']) !== '' ? $item['horaAlta'] : null,
|
|
'tipo_alta_id' => $tipoAlta ? $tipoAlta->id : null,
|
|
'unidade_atendimento' => $item['unAtendimento'],
|
|
'cnes_un_atendimento' => $item['cnesUnAtendimento'],
|
|
]);
|
|
|
|
if (!empty($item['reavaliacoes'])) {
|
|
foreach ($item['reavaliacoes'] as $reavaliacao) {
|
|
|
|
Reavaliacao::create([
|
|
'atendimento_id' => $atendimento->id,
|
|
'reavaliacao' => $reavaliacao['reavalicao'],
|
|
'profissional' => $reavaliacao['profissional'],
|
|
'numero_conselho' => $reavaliacao['numConselho'],
|
|
'uf_conselho' => $reavaliacao['ufConselho'],
|
|
'data' => Carbon::createFromFormat('d/m/Y', $reavaliacao['dataReavaliacao'])->format('Y-m-d'),
|
|
'hora' => $reavaliacao['horaReavaliacao'],
|
|
]);
|
|
}
|
|
}
|
|
|
|
if (!empty($item['medicamentos'])) {
|
|
foreach ($item['medicamentos'] as $med) {
|
|
|
|
Medicamento::create([
|
|
'atendimento_id' => $atendimento->id,
|
|
'medicamento' => $med['medicamento'],
|
|
'quantidade' => str_replace(',', '.', $med['qtdePresc']),
|
|
'unidade_medida' => $med['uniMedPresc'],
|
|
'medicamento_id' => $med['idMedicamento'],
|
|
'tipo' => $med['tipo'],
|
|
]);
|
|
}
|
|
}
|
|
|
|
if (!empty($item['exames'])) {
|
|
foreach ($item['exames'] as $exame) {
|
|
|
|
ErpsaudeExame::create([
|
|
'atendimento_id' => $atendimento->id,
|
|
'exame' => $exame['descricao'] ?? '',
|
|
'tipo' => $exame['tipo'] ?? '',
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
Log::channel('integracao-erpsaude')->info('Comando executado com sucesso.', [
|
|
'cliente' => $cliente->link,
|
|
'origem' => 'BuscarAtendimentosHospitalSantaCasaCommand'
|
|
]);
|
|
}
|
|
} catch (Exception $error) {
|
|
Log::channel('integracao-erpsaude')->error('Erro ao executar o comando', [
|
|
'cliente' => $cliente->link ?? $cliente,
|
|
'origem' => 'BuscarAtendimentosHospitalSantaCasaCommand',
|
|
'erro' => $error->getMessage(),
|
|
]);
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|