439 lines
17 KiB
PHP
439 lines
17 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Sigtap;
|
|
|
|
use App\Enums\Sigtap\Registro;
|
|
use App\Models\Sigtap\Competencia;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ProcedimentoService
|
|
{
|
|
private $competenciaId;
|
|
private $cbo;
|
|
private $ocupacaoId;
|
|
private $cidId;
|
|
private $registro;
|
|
private $paciente;
|
|
private $where;
|
|
private $whereNot;
|
|
private $onlyWithCbo;
|
|
private $onlyWithCid;
|
|
private $onlyWithoutCid;
|
|
private $likeCodigo;
|
|
private $atendimentoOdontologico;
|
|
private $atendimentoDomiciliar;
|
|
private $onlyServicosEnfermagem;
|
|
private $searchByApelido;
|
|
private $instrumentoRegistro;
|
|
private $withAtributosComplementares;
|
|
private $procedimentoPrincipalCodigo;
|
|
|
|
public function __construct(array $params = [])
|
|
{
|
|
$this->competenciaId = $params['competencia_id'] ?? $this->getUltimaCompetencia()->id;
|
|
$this->cbo = $params['cbo'] ?? null;
|
|
$this->ocupacaoId = $params['ocupacao_id'] ?? null;
|
|
$this->cidId = $params['cid_id'] ?? null;
|
|
$this->registro = $params['registro'] ?? null;
|
|
$this->paciente = $params['paciente'] ?? null;
|
|
$this->where = $params['where'] ?? null;
|
|
$this->whereNot = $params['where_not'] ?? null;
|
|
$this->onlyWithCbo = isset($params['only_with_cbo']) ? $params['only_with_cbo'] : false;
|
|
$this->onlyWithCid = isset($params['only_with_cid']) ? $params['only_with_cid'] : false;
|
|
$this->onlyWithoutCid = isset($params['only_without_cid']) ? $params['only_without_cid'] : false;
|
|
$this->atendimentoOdontologico = $params['atendimento_odontologico'] ?? null;
|
|
$this->atendimentoDomiciliar = $params['atendimento_domiciliar'] ?? null;
|
|
$this->likeCodigo = $params['like_codigo'] ?? null;
|
|
|
|
$this->onlyServicosEnfermagem = isset($params['only_servicos_enfermagem'])
|
|
? $params['only_servicos_enfermagem'] : false;
|
|
|
|
$this->searchByApelido = isset($params['search_by_apelido']) ? $params['search_by_apelido'] : false;
|
|
$this->instrumentoRegistro = $params['instrumento_registro'] ?? null;
|
|
$this->withAtributosComplementares = $params['with_atributos_complementares'] ?? false;
|
|
$this->procedimentoPrincipalCodigo = $params['procedimento_principal_codigo'] ?? null;
|
|
}
|
|
|
|
public function getUltimaCompetencia()
|
|
{
|
|
return Competencia::orderBy('valor', 'desc')->first();
|
|
}
|
|
|
|
public function getProcedimentos() : Collection
|
|
{
|
|
return $this->getProcedimentosBuilder()->get();
|
|
}
|
|
|
|
public function getProcedimentosBuilder()
|
|
{
|
|
$procedimentos = $this->searchByApelido ? $this->getSelectWithParametrizacao() : $this->getSelectAllProcedimentos();
|
|
|
|
if (!is_null($this->competenciaId)) {
|
|
|
|
$procedimentos->where('procedimento.competencia_id', $this->competenciaId);
|
|
}
|
|
|
|
if (!is_null($this->instrumentoRegistro)) {
|
|
|
|
$procedimentos->whereExists(function($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_registro')
|
|
->join('sigtap.registro', 'registro.id', 'procedimento_registro.registro_id')
|
|
->whereIn('registro.codigo', $this->instrumentoRegistro)
|
|
->whereColumn('procedimento_registro.procedimento_id', 'procedimento.id');
|
|
});
|
|
}
|
|
|
|
if (!is_null($this->cbo)) {
|
|
|
|
$procedimentos->where(function ($procedimentos) {
|
|
|
|
$procedimentos->whereExists(function($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_ocupacao')
|
|
->whereColumn('sigtap.procedimento_ocupacao.procedimento_id', 'procedimento.id')
|
|
->join('sigtap.ocupacao', 'sigtap.ocupacao.id', 'sigtap.procedimento_ocupacao.ocupacao_id');
|
|
|
|
if (is_array($this->cbo)) {
|
|
|
|
$query->whereIn('sigtap.ocupacao.codigo', $this->cbo);
|
|
}
|
|
else {
|
|
|
|
$query->where('sigtap.ocupacao.codigo', $this->cbo);
|
|
}
|
|
});
|
|
|
|
if (!$this->onlyWithCbo) {
|
|
|
|
$procedimentos->orWhereNotExists(function($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_ocupacao')
|
|
->whereColumn('sigtap.procedimento_ocupacao.procedimento_id', 'procedimento.id');
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
if (!is_null($this->ocupacaoId)) {
|
|
$procedimentos->where(function ($query) {
|
|
$query->whereExists(function($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_ocupacao')
|
|
->join('sigtap.ocupacao', 'ocupacao.id', 'procedimento_ocupacao.ocupacao_id')
|
|
->whereColumn('procedimento_ocupacao.procedimento_id', 'procedimento.id')
|
|
->where('ocupacao.id', $this->ocupacaoId);
|
|
})->orWhereNotExists(function($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_ocupacao')
|
|
->whereColumn('sigtap.procedimento_ocupacao.procedimento_id', 'sigtap.procedimento.id');
|
|
})->orWhereNotExists(function($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_ocupacao')
|
|
->whereColumn('procedimento_ocupacao.procedimento_id', 'procedimento.id');
|
|
});
|
|
});
|
|
}
|
|
|
|
if (!is_null($this->registro)) {
|
|
|
|
$registros = [];
|
|
|
|
switch ($this->registro) {
|
|
|
|
case 'aih': {
|
|
|
|
$registros = [
|
|
Registro::AIH_PROC_ESPECIAL,
|
|
Registro::AIH_PROC_PRINCIPAL,
|
|
Registro::AIH_PROC_SECUNDARIO
|
|
];
|
|
|
|
break;
|
|
}
|
|
case 'raas': {
|
|
|
|
$registros = [
|
|
Registro::RAAS_ATENCAO_DOMICILIAR,
|
|
Registro::RAAS_ATENCAO_PSICOSSOCIAL,
|
|
];
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!empty($registros)) {
|
|
|
|
$procedimentos->where(function ($query) use ($registros) {
|
|
$query->whereExists(function($query) use ($registros) {
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_registro')
|
|
->join('sigtap.registro', 'sigtap.registro.id', 'sigtap.procedimento_registro.registro_id')
|
|
->whereColumn('sigtap.procedimento_registro.procedimento_id', 'sigtap.procedimento.id')
|
|
->whereIn('sigtap.registro.codigo', $registros);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
if ($this->onlyWithoutCid) {
|
|
|
|
$procedimentos->whereNotExists(function($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_cid')
|
|
->whereColumn('sigtap.procedimento_cid.procedimento_id', 'sigtap.procedimento.id');
|
|
});
|
|
}
|
|
else if (!is_null($this->cidId)) {
|
|
|
|
$procedimentos->where(function ($query) {
|
|
|
|
$query->whereExists(function($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_cid')
|
|
->whereColumn('sigtap.procedimento_cid.procedimento_id', 'sigtap.procedimento.id')
|
|
->whereIn('sigtap.procedimento_cid.cid_id', [$this->cidId]);
|
|
});
|
|
|
|
if (!$this->onlyWithCid) {
|
|
|
|
$query->orWhereNotExists(function($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('sigtap.procedimento_cid')
|
|
->whereColumn('sigtap.procedimento_cid.procedimento_id', 'sigtap.procedimento.id');
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
if (!is_null($this->paciente)) {
|
|
|
|
$procedimentos->where(function ($procedimentos) {
|
|
$procedimentos
|
|
->where('procedimento.sexo', $this->paciente->cidadao->sexo->identificador)
|
|
->orWhere('procedimento.sexo', 'I')
|
|
->orWhere('procedimento.sexo', 'N');
|
|
});
|
|
|
|
if (!$this->paciente->is_desconhecido) {
|
|
|
|
$procedimentos->where(function ($procedimentos) {
|
|
|
|
$meses = $this->paciente->cidadao->idade['anos'] * 12;
|
|
|
|
$procedimentos
|
|
->where(function ($query) use ($meses) {
|
|
|
|
$query->where('procedimento.idade_minima', '<=', $meses)->orWhere('procedimento.idade_minima', 9999);
|
|
})
|
|
->where(function ($query) use ($meses) {
|
|
|
|
$query->where('procedimento.idade_maxima', '>=', $meses)->orWhere('procedimento.idade_maxima', 9999);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
if (!is_null($this->whereNot) && is_array($this->whereNot)) {
|
|
|
|
$whereNotCodigos = $this->whereNot['codigo'] ?? [];
|
|
|
|
$procedimentos->whereNotIn('procedimento.codigo', $whereNotCodigos);
|
|
}
|
|
|
|
if (!is_null($this->atendimentoOdontologico)) {
|
|
|
|
$whereCodigos = $this->where['codigo'] ?? [];
|
|
|
|
$procedimentos->where(function($query) use($whereCodigos) {
|
|
$query->where('procedimento.codigo', 'like', '01%')
|
|
->orWhere('procedimento.codigo', 'like', '02%')
|
|
->orWhere('procedimento.codigo', 'like', '03%')
|
|
->orWhere('procedimento.codigo', 'like', '04%')
|
|
->orWhere('procedimento.codigo', 'like', '09%')
|
|
->orWhereIn('procedimento.codigo', $whereCodigos);
|
|
});
|
|
}
|
|
|
|
if (!is_null($this->atendimentoDomiciliar)) {
|
|
|
|
$whereCodigos = $this->where['codigo'] ?? [];
|
|
|
|
$procedimentos->where(function($query) use($whereCodigos) {
|
|
$query->where('procedimento.codigo', 'like', '01%')
|
|
->orWhere('procedimento.codigo', 'like', '02%')
|
|
->orWhere('procedimento.codigo', 'like', '03%')
|
|
->orWhere('procedimento.codigo', 'like', '04%');
|
|
});
|
|
}
|
|
|
|
if (!is_null($this->likeCodigo)) {
|
|
|
|
if (is_array($this->likeCodigo)) {
|
|
|
|
$procedimentos->where(function ($query) {
|
|
|
|
foreach ($this->likeCodigo as $likeCodigo) {
|
|
|
|
$query->orWhere('procedimento.codigo', 'like', $likeCodigo);
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
|
|
$procedimentos->where('procedimento.codigo', 'like', $this->likeCodigo);
|
|
}
|
|
}
|
|
|
|
if ($this->onlyServicosEnfermagem) {
|
|
|
|
$procedimentos
|
|
->whereExists(function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_parametrizacao_servico_enfermagem')
|
|
->whereColumn(
|
|
'pronto_atendimento_parametrizacao_servico_enfermagem.procedimento_codigo',
|
|
'procedimento.codigo'
|
|
)
|
|
->whereNull('deleted_at');
|
|
});
|
|
}
|
|
|
|
if (!is_null($this->procedimentoPrincipalCodigo)) {
|
|
|
|
$procedimentos->whereExists(function ($query) {
|
|
|
|
$query->selectRaw(1)
|
|
->from('sigtap.procedimento_compativel')
|
|
->join(
|
|
'sigtap.procedimento_registro as procedimento_registro_compativel',
|
|
'procedimento_registro_compativel.id',
|
|
'sigtap.procedimento_compativel.procedimento_registro_compativel_id'
|
|
)
|
|
->join(
|
|
'sigtap.procedimento_registro as procedimento_registro_principal',
|
|
'procedimento_registro_principal.id',
|
|
'sigtap.procedimento_compativel.procedimento_registro_principal_id'
|
|
)
|
|
->join(
|
|
'sigtap.procedimento as procedimento_principal',
|
|
'procedimento_principal.id',
|
|
'procedimento_registro_principal.procedimento_id'
|
|
)
|
|
->whereColumn('procedimento_registro_compativel.procedimento_id', 'sigtap.procedimento.id')
|
|
->where('procedimento_principal.codigo', $this->procedimentoPrincipalCodigo);
|
|
});
|
|
}
|
|
|
|
return $procedimentos;
|
|
}
|
|
|
|
private function getSelectWithParametrizacao()
|
|
{
|
|
return DB::table('sigtap.procedimento')
|
|
->join(
|
|
'parametrizacao_procedimento',
|
|
'parametrizacao_procedimento.procedimento_codigo',
|
|
'sigtap.procedimento.codigo'
|
|
)
|
|
->select(collect([
|
|
'sigtap.procedimento.id',
|
|
'sigtap.procedimento.competencia_id',
|
|
'sigtap.procedimento.codigo',
|
|
'sigtap.procedimento.nome',
|
|
'sigtap.procedimento.quantidade_maxima_execucao',
|
|
'sigtap.procedimento.sexo',
|
|
'sigtap.procedimento.idade_minima',
|
|
'sigtap.procedimento.idade_maxima',
|
|
'sigtap.procedimento.valor_servico_ambilatorial',
|
|
$this->getSubSelectCidPrincipal(),
|
|
$this->getSubSelectExigeIdadeBpaConsolidado(),
|
|
$this->getSelectHasInstrumentoRegistroRaas(),
|
|
($this->withAtributosComplementares ? $this->getSelectAtributosComplementares() : null)
|
|
])->filter()->all())
|
|
->orderBy('parametrizacao_procedimento.apelido')
|
|
->whereNull('deleted_at');
|
|
}
|
|
|
|
private function getSelectAllProcedimentos()
|
|
{
|
|
return DB::table('sigtap.procedimento')
|
|
->select(collect([
|
|
'sigtap.procedimento.id',
|
|
'sigtap.procedimento.competencia_id',
|
|
'sigtap.procedimento.codigo',
|
|
'sigtap.procedimento.nome',
|
|
'sigtap.procedimento.quantidade_maxima_execucao',
|
|
'sigtap.procedimento.sexo',
|
|
'sigtap.procedimento.idade_minima',
|
|
'sigtap.procedimento.idade_maxima',
|
|
'sigtap.procedimento.valor_servico_ambilatorial',
|
|
$this->getSubSelectCidPrincipal(),
|
|
$this->getSubSelectExigeIdadeBpaConsolidado(),
|
|
$this->getSelectHasInstrumentoRegistroRaas(),
|
|
($this->withAtributosComplementares ? $this->getSelectAtributosComplementares() : null)
|
|
])->filter()->all())
|
|
->orderBy('procedimento.nome');
|
|
}
|
|
|
|
private function getSubSelectCidPrincipal()
|
|
{
|
|
return DB::raw("
|
|
(
|
|
SELECT TRUE
|
|
FROM sigtap.procedimento_cid
|
|
WHERE sigtap.procedimento_cid.procedimento_id = sigtap.procedimento.id
|
|
AND sigtap.procedimento_cid.principal = 'S'
|
|
LIMIT 1
|
|
) AS has_cid_principal
|
|
");
|
|
}
|
|
|
|
private function getSubSelectExigeIdadeBpaConsolidado()
|
|
{
|
|
return DB::raw("
|
|
(
|
|
SELECT TRUE
|
|
FROM sigtap.procedimento_detalhe
|
|
INNER JOIN sigtap.detalhe
|
|
ON sigtap.detalhe.id = sigtap.procedimento_detalhe.detalhe_id
|
|
WHERE sigtap.procedimento_detalhe.procedimento_id = sigtap.procedimento.id
|
|
AND sigtap.detalhe.codigo = '012'
|
|
LIMIT 1
|
|
) AS exige_idade_bpa_consolidado
|
|
");
|
|
}
|
|
|
|
private function getSelectHasInstrumentoRegistroRaas()
|
|
{
|
|
return DB::raw("
|
|
(
|
|
SELECT TRUE
|
|
FROM sigtap.procedimento_registro
|
|
INNER JOIN sigtap.registro
|
|
ON sigtap.registro.id = sigtap.procedimento_registro.registro_id
|
|
WHERE sigtap.procedimento_registro.procedimento_id = sigtap.procedimento.id
|
|
AND sigtap.registro.codigo IN ('" . Registro::RAAS_ATENCAO_DOMICILIAR . "', '" . Registro::RAAS_ATENCAO_PSICOSSOCIAL . "')
|
|
LIMIT 1
|
|
) AS has_instrumento_registro_raas
|
|
");
|
|
}
|
|
|
|
private function getSelectAtributosComplementares()
|
|
{
|
|
return DB::raw("
|
|
(
|
|
SELECT GROUP_CONCAT(sigtap.detalhe.codigo SEPARATOR '#')
|
|
FROM sigtap.procedimento_detalhe
|
|
INNER JOIN sigtap.detalhe
|
|
ON sigtap.detalhe.id = sigtap.procedimento_detalhe.detalhe_id
|
|
WHERE sigtap.procedimento_detalhe.procedimento_id = sigtap.procedimento.id
|
|
) AS atributos_complementares
|
|
");
|
|
}
|
|
}
|