This commit is contained in:
229
api/app/Services/Ambulatorio/HistoricoAtendimentoService.php
Normal file
229
api/app/Services/Ambulatorio/HistoricoAtendimentoService.php
Normal file
@@ -0,0 +1,229 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ambulatorio;
|
||||
|
||||
use App\Enums\Cadastro\SujeitoAtencao\Subclassificacao;
|
||||
use App\Models\Cadastro\Parametrizacao\Parametrizacao;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
||||
use App\Models\Integracao\Erpsaude\Atendimento as ErpsaudeAtendimento;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use App\Utils\MaskUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class HistoricoAtendimentoService extends Service
|
||||
{
|
||||
public const IDADE_ANOS_INICIAL_IDOSO = 60;
|
||||
public const IDADE_ANOS_FINAL_CRIANCA = 9;
|
||||
public const IDADE_ANOS_FINAL_CRIANCA_ALEITAMENTO = 1;
|
||||
public const IDADE_ANOS_FINAL_ADOLESCENTE = 18;
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
|
||||
$parametrizacao = Parametrizacao::first();
|
||||
$quantidadeDias = $parametrizacao->cadastro_paciente_desatualizado_quantidade_dias ?? 0;
|
||||
$provisorioBloqueado = $parametrizacao->cadastro_paciente_provisorio_bloqueado ?? false;
|
||||
$provisorioQuantidadeDias = $parametrizacao->cadastro_paciente_provisorio_quantidade_dias ?? 0;
|
||||
|
||||
$pacientes = DB::table('cadastro_paciente as paciente')
|
||||
->join(
|
||||
'cadastro_paciente_classificacao as classificacao',
|
||||
'classificacao.id',
|
||||
'paciente.classificacao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_cidadao as cidadao',
|
||||
'cidadao.id',
|
||||
'paciente.cidadao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_domicilio as domicilio',
|
||||
'domicilio.id',
|
||||
'cidadao.domicilio_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_endereco as endereco',
|
||||
'endereco.id',
|
||||
'domicilio.endereco_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_logradouro as logradouro',
|
||||
'logradouro.id',
|
||||
'endereco.logradouro_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'intranet.cadastro_municipio as municipio',
|
||||
'municipio.id',
|
||||
'logradouro.municipio_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'intranet.cadastro_estado as estado',
|
||||
'estado.id',
|
||||
'municipio.estado_id'
|
||||
)
|
||||
->whereNull('paciente.deleted_at')
|
||||
->whereNull('paciente.sequencial_desconhecido_em')
|
||||
->select(DB::raw("
|
||||
paciente.id,
|
||||
paciente.classificacao_id,
|
||||
cidadao.nome,
|
||||
cidadao.nome_social,
|
||||
cidadao.preferencia_nome_id,
|
||||
DATE_FORMAT(cidadao.data_nascimento, '%d/%m/%Y') as data_nascimento,
|
||||
cidadao.nome_mae,
|
||||
LPAD(paciente.prontuario, 7, '0') as prontuario,
|
||||
CONCAT(
|
||||
SUBSTR(cidadao.cpf, 1, 3),
|
||||
'.',
|
||||
SUBSTR(cidadao.cpf, 4, 3),
|
||||
'.',
|
||||
SUBSTR(cidadao.cpf, 7, 3),
|
||||
'-',
|
||||
SUBSTR(cidadao.cpf, 10, 2)
|
||||
) as cpf,
|
||||
CONCAT(
|
||||
SUBSTR(cidadao.cns, 1, 3),
|
||||
' ',
|
||||
SUBSTR(cidadao.cns, 4, 4),
|
||||
' ',
|
||||
SUBSTR(cidadao.cns, 8, 4),
|
||||
' ',
|
||||
SUBSTR(cidadao.cns, 12, 4)
|
||||
) as cns,
|
||||
classificacao.descricao as classificacao_descricao,
|
||||
IF(paciente.validade_fim IS NULL,
|
||||
'INDETERMINADO',
|
||||
DATE_FORMAT(paciente.validade_fim, '%d/%m/%Y')
|
||||
) as validade_fim,
|
||||
CASE
|
||||
WHEN paciente.updated_at IS NULL THEN 1
|
||||
WHEN " . ($quantidadeDias > 0 ? "DATE_ADD(paciente.updated_at, INTERVAL $quantidadeDias DAY) < NOW()" : "false") . "
|
||||
THEN DATEDIFF(NOW(), DATE_ADD(paciente.updated_at, INTERVAL $quantidadeDias DAY))
|
||||
ELSE 0
|
||||
END as dias_cadastro_desatualizado
|
||||
"));
|
||||
|
||||
if (!DataUtil::isStringTrue(request()->with_paciente_bloqueado)) {
|
||||
|
||||
$pacientes->where(function ($query) {
|
||||
|
||||
$query
|
||||
->where('paciente.bloqueado', false)
|
||||
->orWhereNull('paciente.bloqueado');
|
||||
});
|
||||
}
|
||||
|
||||
if (!DataUtil::isStringTrue(request()->with_paciente_provisorio) && $provisorioBloqueado) {
|
||||
|
||||
$pacientes->where(function ($query) use ($provisorioQuantidadeDias) {
|
||||
$query->where('paciente.subclassificacao_id', '!=', Subclassificacao::TEMPORARIO)
|
||||
->orWhere(function ($query) use ($provisorioQuantidadeDias) {
|
||||
$query->where('paciente.subclassificacao_id', Subclassificacao::TEMPORARIO)
|
||||
->where(function ($query) use ($provisorioQuantidadeDias) {
|
||||
|
||||
$query->where(function ($query) {
|
||||
$query->whereNull('paciente.validade_inicio')
|
||||
->orWhereDate('paciente.validade_inicio', '<=', Carbon::now()->format('Y-m-d'));
|
||||
});
|
||||
|
||||
$query->where(function ($query) use ($provisorioQuantidadeDias) {
|
||||
$query->whereNull('paciente.validade_fim')
|
||||
->orWhere(function ($query) use ($provisorioQuantidadeDias) {
|
||||
if (!is_null($provisorioQuantidadeDias) && $provisorioQuantidadeDias > 0) {
|
||||
$query->whereDate('paciente.validade_fim', '>=', Carbon::now()->format('Y-m-d'))
|
||||
->orWhereDate('paciente.validade_fim', '>=', Carbon::now()->subDays($provisorioQuantidadeDias)->format('Y-m-d'));
|
||||
} else {
|
||||
$query->whereDate('paciente.validade_fim', '>=', Carbon::now()->format('Y-m-d'));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
->orWhereNull('paciente.subclassificacao_id');
|
||||
});
|
||||
}
|
||||
|
||||
return $pacientes;
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
$paciente = Paciente::findOrFail($id);
|
||||
|
||||
return [
|
||||
'form' => self::getFormData($paciente),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFormData($paciente)
|
||||
{
|
||||
|
||||
$exibeHistoricoErpsaude = app('config')->get('database.default') == 'santarita' ? true : false;
|
||||
|
||||
return [
|
||||
'paciente' => self::getPacienteData($paciente),
|
||||
'historico' => AtendimentoService::getHistorico($paciente->id),
|
||||
'historico_erpsaude' => $exibeHistoricoErpsaude ? AtendimentoService::getHistoricoErpsaude($paciente) : null,
|
||||
'exibe_historico_erpsaude' => $exibeHistoricoErpsaude,
|
||||
];
|
||||
}
|
||||
|
||||
private static function getPacienteData($paciente)
|
||||
{
|
||||
$municipioResidencia = $paciente->cidadao->domicilio->endereco->logradouro->municipio ?? null;
|
||||
|
||||
return [
|
||||
'id' => $paciente->id,
|
||||
'nome' => $paciente->cidadao->nome,
|
||||
'nome_social' => $paciente->cidadao->nome_social,
|
||||
'preferencia_nome_id' => $paciente->cidadao->preferencia_nome_id,
|
||||
'sexo_id' => $paciente->cidadao->sexo_id,
|
||||
'sexo_descricao' => $paciente->cidadao->sexo->descricao ?? null,
|
||||
'sexo_identificador' => $paciente->cidadao->sexo->identificador ?? null,
|
||||
'data_nascimento' => is_null($paciente->cidadao->data_nascimento)
|
||||
? null
|
||||
: (new Carbon($paciente->cidadao->data_nascimento))->format('d/m/Y'),
|
||||
'idade_em_dias' => is_null($paciente->cidadao->data_nascimento)
|
||||
? null
|
||||
: Carbon::now()->diffInDays($paciente->cidadao->data_nascimento),
|
||||
'idade_em_meses' => is_null($paciente->cidadao->data_nascimento)
|
||||
? null
|
||||
: ceil(Carbon::now()->diffInDays($paciente->cidadao->data_nascimento) / 31),
|
||||
'idade_anos' => is_null($paciente->cidadao->data_nascimento)
|
||||
? null
|
||||
: $paciente->cidadao->idade['anos'],
|
||||
'idade_meses' => is_null($paciente->cidadao->data_nascimento)
|
||||
? null
|
||||
: $paciente->cidadao->idade['meses'],
|
||||
'is_idoso' => is_null($paciente->cidadao->data_nascimento)
|
||||
? false
|
||||
: $paciente->cidadao->idade['anos'] >= self::IDADE_ANOS_INICIAL_IDOSO,
|
||||
'is_crianca' => is_null($paciente->cidadao->data_nascimento)
|
||||
? false
|
||||
: $paciente->cidadao->idade['anos'] <= self::IDADE_ANOS_FINAL_CRIANCA,
|
||||
'is_crianca_aleitamento' => is_null($paciente->cidadao->data_nascimento)
|
||||
? false
|
||||
: $paciente->cidadao->idade['anos'] <= self::IDADE_ANOS_FINAL_CRIANCA_ALEITAMENTO,
|
||||
'is_adolescente' => is_null($paciente->cidadao->data_nascimento)
|
||||
? false
|
||||
: $paciente->cidadao->idade['anos'] > self::IDADE_ANOS_FINAL_CRIANCA
|
||||
&& $paciente->cidadao->idade['anos'] <= self::IDADE_ANOS_FINAL_ADOLESCENTE,
|
||||
'nome_mae' => $paciente->cidadao->nome_mae,
|
||||
'prontuario' => $paciente->prontuario,
|
||||
'cpf' => MaskUtil::getCpfMasked($paciente->cidadao->cpf),
|
||||
'cns' => MaskUtil::getCnsMasked($paciente->cidadao->cns),
|
||||
'municipio_uf' => is_null($municipioResidencia)
|
||||
? null
|
||||
: "{$municipioResidencia->nome} / {$municipioResidencia->estado->uf}",
|
||||
'telefone_id' => $paciente->cidadao->telefone_principal->id ?? null,
|
||||
'telefone_numero' => MaskUtil::getPhoneMasked($paciente->cidadao->telefone_principal->numero ?? null),
|
||||
'telefone_autoriza_sms' => $paciente->cidadao->telefone_principal->autoriza_sms ?? false,
|
||||
'unidade_referencia' => $paciente->unidade->nome_fantasia ?? null,
|
||||
'cep' => $paciente->cidadao->domicilio->endereco->logradouro->cep ?? null,
|
||||
'ibge' => $paciente->cidadao->domicilio->endereco->logradouro->municipio->ibge ?? null,
|
||||
'raca_cor_id' => $paciente->cidadao->racaCorId ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user