811 lines
34 KiB
PHP
811 lines
34 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Raas;
|
|
|
|
use App\Enums\Raas\CaraterAtendimento as RaasCaraterAtendimento;
|
|
use App\Enums\Raas\DestinoPaciente as RaasDestinoPaciente;
|
|
use App\Enums\Raas\LocalRealizacaoExame as RaasLocalRealizacaoExame;
|
|
use App\Enums\Raas\MotivoSaidaPermanencia as RaasMotivoSaidaPermanencia;
|
|
use App\Enums\Raas\OrigemPaciente as RaasOrigemPaciente;
|
|
use App\Enums\Sigtap\Procedimento as ProcedimentoEnum;
|
|
use App\Enums\Sigtap\Registro;
|
|
use App\Models\Ambulatorio\AtendimentoProcedimento as AmbulatorioAtendimentoProcedimento;
|
|
use App\Models\Cadastro\Estabelecimento\Unidade;
|
|
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
|
use App\Models\Cadastro\SujeitoAtencao\Usuario;
|
|
use App\Models\Raas\Atendimento as AtendimentoRaas;
|
|
use App\Models\Raas\AtendimentoProcedimento;
|
|
use App\Models\Raas\CaraterAtendimento;
|
|
use App\Models\Raas\DestinoPaciente;
|
|
use App\Models\Raas\LocalRealizacaoExame;
|
|
use App\Models\Raas\MotivoSaidaPermanencia;
|
|
use App\Models\Raas\OrigemPaciente;
|
|
use App\Models\Sigtap\Cid;
|
|
use App\Models\Sigtap\Classificacao;
|
|
use App\Models\Sigtap\Competencia;
|
|
use App\Models\Sigtap\Ocupacao;
|
|
use App\Models\Sigtap\Procedimento;
|
|
use App\Models\Sigtap\Servico;
|
|
use App\Services\Service;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
use App\Enums\Sigtap\Registro as RegistroEnum;
|
|
|
|
class ProcedimentoService extends Service
|
|
{
|
|
public static function datatable()
|
|
{
|
|
return DB::table('raas_atendimento as atendimento')
|
|
->join('cadastro_unidade as unidade', 'unidade.id', 'atendimento.unidade_id')
|
|
->join('sigtap.competencia as competencia', 'competencia.id', 'atendimento.competencia_id')
|
|
->join('raas_origem_paciente as origem_paciente', 'origem_paciente.id', 'atendimento.origem_paciente_id')
|
|
->join('raas_destino_paciente as destino_paciente', 'destino_paciente.id', 'atendimento.destino_paciente_id')
|
|
->join('cadastro_paciente as paciente', 'paciente.id', 'atendimento.paciente_id')
|
|
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'paciente.cidadao_id')
|
|
->leftJoin('sigtap.cid as cid_principal', 'cid_principal.id', 'atendimento.cid_principal_id')
|
|
->selectRaw("
|
|
atendimento.id,
|
|
unidade.nome_fantasia as unidade_nome_fantasia,
|
|
competencia.valor as competencia_valor,
|
|
atendimento.data_admissao,
|
|
origem_paciente.nome as origem_paciente_nome,
|
|
cidadao.nome as paciente_nome,
|
|
cid_principal.codigo as cid_principal_codigo,
|
|
cid_principal.nome as cid_principal_nome,
|
|
destino_paciente.nome as destino_paciente_nome,
|
|
atendimento.numero_autorizacao,
|
|
IF(atendimento.exportacao_id IS NULL, FALSE, TRUE) as exportado
|
|
");
|
|
}
|
|
|
|
public static function create()
|
|
{
|
|
switch (request()->origem) {
|
|
|
|
case 'geracao':
|
|
case 'exclusao': {
|
|
|
|
return [
|
|
'form' => self::getGeracaoExclusaoFormData(),
|
|
'model' => self::getGeracaoExclusaoModelData(),
|
|
];
|
|
}
|
|
case 'cadastro': {
|
|
|
|
return [
|
|
'additionals' => self::getCadastroAdditionalsData(),
|
|
'form' => self::getCadastroFormData(),
|
|
'model' => self::getCadastroModelData(),
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function edit($id)
|
|
{
|
|
$atendimentoRaas = AtendimentoRaas::findOrFail($id);
|
|
|
|
return [
|
|
'additionals' => self::getCadastroAdditionalsData(),
|
|
'form' => self::getCadastroFormData(),
|
|
'model' => self::getCadastroModelData($atendimentoRaas),
|
|
];
|
|
}
|
|
|
|
private static function getGeracaoExclusaoFormData()
|
|
{
|
|
return [
|
|
'unidades' => self::getUnidades(),
|
|
'competencias' => self::getCompetencias(),
|
|
];
|
|
}
|
|
|
|
private static function getGeracaoExclusaoModelData()
|
|
{
|
|
return [
|
|
'unidade_id' => null,
|
|
'competencia_id' => null,
|
|
];
|
|
}
|
|
|
|
private static function getCadastroAdditionalsData()
|
|
{
|
|
return [
|
|
'data_atual' => Carbon::today()->format('d/m/Y'),
|
|
];
|
|
}
|
|
|
|
private static function getCadastroFormData()
|
|
{
|
|
return [
|
|
'unidades' => self::getUnidades(),
|
|
'competencias' => self::getCompetencias(),
|
|
'motivos_saida_permanencia' => self::getMotivosSaidaPermanencia(),
|
|
'carateres_atendimento' => self::getCarateresAtendimento(),
|
|
'origens_paciente' => self::getOrigensPaciente(),
|
|
'destinos_paciente' => self::getDestinosPaciente(),
|
|
'procedimento' => [
|
|
'locais_realizacao_exame' => self::getLocaisRealizacaoExame(),
|
|
],
|
|
];
|
|
}
|
|
|
|
private static function getCadastroModelData($atendimentoRaas = null)
|
|
{
|
|
return [
|
|
'unidade_id' => $atendimentoRaas->unidade_id ?? null,
|
|
'competencia_id' => $atendimentoRaas->competencia_id ?? null,
|
|
'data_admissao' => isset($atendimentoRaas->data_admissao)
|
|
? $atendimentoRaas->data_admissao->format('d/m/Y')
|
|
: null,
|
|
'paciente' => is_null($atendimentoRaas)
|
|
? null
|
|
: $atendimentoRaas->paciente()->with([
|
|
'cidadao.racaCor',
|
|
'cidadao.etniaIndigena',
|
|
'cidadao.paisNascimento',
|
|
'cadastroIndividual'
|
|
])->first(),
|
|
'origem_paciente_id' => $atendimentoRaas->origem_paciente_id ?? null,
|
|
'cid_principal' => isset($atendimentoRaas->cidPrincipal)
|
|
? [
|
|
'id' => $atendimentoRaas->cidPrincipal->id,
|
|
'codigo' => $atendimentoRaas->cidPrincipal->codigo,
|
|
'nome' => $atendimentoRaas->cidPrincipal->nome,
|
|
]
|
|
: null,
|
|
'cid_secundario_1' => isset($atendimentoRaas->cidSecundario1)
|
|
? [
|
|
'id' => $atendimentoRaas->cidSecundario1->id,
|
|
'codigo' => $atendimentoRaas->cidSecundario1->codigo,
|
|
'nome' => $atendimentoRaas->cidSecundario1->nome,
|
|
]
|
|
: null,
|
|
'cid_secundario_2' => isset($atendimentoRaas->cidSecundario2)
|
|
? [
|
|
'id' => $atendimentoRaas->cidSecundario2->id,
|
|
'codigo' => $atendimentoRaas->cidSecundario2->codigo,
|
|
'nome' => $atendimentoRaas->cidSecundario2->nome,
|
|
]
|
|
: null,
|
|
'cid_secundario_3' => isset($atendimentoRaas->cidSecundario3)
|
|
? [
|
|
'id' => $atendimentoRaas->cidSecundario3->id,
|
|
'codigo' => $atendimentoRaas->cidSecundario3->codigo,
|
|
'nome' => $atendimentoRaas->cidSecundario3->nome,
|
|
]
|
|
: null,
|
|
'cid_causa_associada' => isset($atendimentoRaas->cidCausaAssociada)
|
|
? [
|
|
'id' => $atendimentoRaas->cidCausaAssociada->id,
|
|
'codigo' => $atendimentoRaas->cidCausaAssociada->codigo,
|
|
'nome' => $atendimentoRaas->cidCausaAssociada->nome,
|
|
]
|
|
: null,
|
|
'cobertura_estrategia_saude_familia' => $atendimentoRaas->cobertura_estrategia_saude_familia ?? false,
|
|
'unidade_cobertura_estrategia_familia_id' => $atendimentoRaas->unidade_cobertura_estrategia_familia_id ?? null,
|
|
'destino_paciente_id' => $atendimentoRaas->destino_paciente_id ?? null,
|
|
'motivo_saida_permanencia_id' => $atendimentoRaas->motivo_saida_permanencia_id ?? null,
|
|
'data_ocorrencia' => isset($atendimentoRaas->data_ocorrencia)
|
|
? $atendimentoRaas->data_ocorrencia->format('d/m/Y')
|
|
: null,
|
|
'data_conclusao' => isset($atendimentoRaas->data_conclusao)
|
|
? $atendimentoRaas->data_conclusao->format('d/m/Y')
|
|
: null,
|
|
'carater_atendimento_id' => $atendimentoRaas->carater_atendimento_id ?? null,
|
|
'numero_autorizacao' => $atendimentoRaas->numero_autorizacao ?? null,
|
|
'procedimentos' => is_null($atendimentoRaas)
|
|
? []
|
|
: $atendimentoRaas->procedimentos->map(fn ($procedimento) => [
|
|
'id' => $procedimento->id,
|
|
'saved' => true,
|
|
'usuario' => [
|
|
'id' => $procedimento->usuario->id,
|
|
'cidadao_nome' => $procedimento->usuario->cidadao->nome,
|
|
],
|
|
'ocupacao' => [
|
|
'id' => $procedimento->ocupacao->id,
|
|
'codigo' => $procedimento->ocupacao->codigo,
|
|
'nome' => $procedimento->ocupacao->nome,
|
|
],
|
|
'procedimento' => [
|
|
'id' => $procedimento->procedimento->id,
|
|
'codigo' => $procedimento->procedimento->codigo,
|
|
'nome' => $procedimento->procedimento->nome,
|
|
'quantidade_maxima_execucao' => $procedimento->procedimento->quantidade_maxima_execucao,
|
|
],
|
|
'quantidade' => $procedimento->quantidade,
|
|
'data_execucao' => $procedimento->data_execucao->format('d/m/Y'),
|
|
'servico' => isset($procedimento->servico)
|
|
? [
|
|
'id' => $procedimento->servico->id,
|
|
'nome' => $procedimento->servico->nome,
|
|
]
|
|
: null,
|
|
'classificacao' => $procedimento->classificacao
|
|
? [
|
|
'id' => $procedimento->classificacao->id,
|
|
'nome' => $procedimento->classificacao->nome,
|
|
]
|
|
: null,
|
|
'local_realizacao_exame_id' => $procedimento->local_realizacao_exame_id,
|
|
]),
|
|
'exportado' => !is_null($atendimentoRaas->exportacao_id ?? null),
|
|
];
|
|
}
|
|
|
|
private static function getUnidades()
|
|
{
|
|
return DB::table('cadastro_unidade as unidade')
|
|
->whereNull('unidade.deleted_at')
|
|
->whereNotNull('unidade.cnes')
|
|
->whereRaw("TRIM(unidade.cnes) <> ''")
|
|
->orderBy('unidade.nome_fantasia')
|
|
->selectRaw("
|
|
unidade.id,
|
|
unidade.nome_fantasia
|
|
")
|
|
->get();
|
|
}
|
|
|
|
private static function getCompetencias()
|
|
{
|
|
return DB::table('sigtap.competencia as competencia')
|
|
->orderByDesc('valor')
|
|
->selectRaw("
|
|
competencia.id,
|
|
competencia.valor
|
|
")
|
|
->take(4)
|
|
->get();
|
|
}
|
|
|
|
private static function getMotivosSaidaPermanencia()
|
|
{
|
|
return DB::table('raas_motivo_saida_permanencia as motivo')
|
|
->orderBy('motivo.nome')
|
|
->selectRaw("
|
|
motivo.id,
|
|
motivo.nome
|
|
")
|
|
->get();
|
|
}
|
|
|
|
private static function getCarateresAtendimento()
|
|
{
|
|
return DB::table('raas_carater_atendimento as carater')
|
|
->orderBy('carater.nome')
|
|
->selectRaw("
|
|
carater.id,
|
|
carater.nome
|
|
")
|
|
->get();
|
|
}
|
|
|
|
private static function getOrigensPaciente()
|
|
{
|
|
return DB::table('raas_origem_paciente as origem')
|
|
->orderBy('origem.nome')
|
|
->selectRaw("
|
|
origem.id,
|
|
origem.nome
|
|
")
|
|
->get();
|
|
}
|
|
|
|
private static function getDestinosPaciente()
|
|
{
|
|
return DB::table('raas_destino_paciente as destino')
|
|
->orderBy('destino.nome')
|
|
->selectRaw("
|
|
destino.id,
|
|
destino.nome
|
|
")
|
|
->get();
|
|
}
|
|
|
|
private static function getLocaisRealizacaoExame()
|
|
{
|
|
return DB::table('raas_local_realizacao_exame as local')
|
|
->orderBy('local.nome')
|
|
->selectRaw("
|
|
local.id,
|
|
local.nome
|
|
")
|
|
->get();
|
|
}
|
|
|
|
public static function store()
|
|
{
|
|
$response = null;
|
|
|
|
DB::transaction(function() use (&$response) {
|
|
|
|
switch (request()->origem) {
|
|
|
|
case 'geracao': {
|
|
|
|
$response = self::storeProcedimentosAutomaticos();
|
|
|
|
break;
|
|
}
|
|
case 'cadastro': {
|
|
|
|
$response = self::storeProcedimentoManual();
|
|
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
|
|
return $response;
|
|
}
|
|
|
|
private static function storeProcedimentosAutomaticos()
|
|
{
|
|
$hasAtendimentoSemAutorizacaoQuePrecisa = false;
|
|
$competencia = Competencia::findOrFail(request()->competencia_id);
|
|
|
|
DB::table('ambulatorio_atendimento as atendimento')
|
|
->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id')
|
|
->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id')
|
|
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id')
|
|
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id')
|
|
->leftJoin('ambulatorio_atendimento_raas as atendimento_raas', 'atendimento_raas.atendimento_id', 'atendimento.id')
|
|
->leftJoin('sigtap.classificacao as classificacao', 'classificacao.id', 'atendimento_raas.classificacao_id')
|
|
->whereNotNull('atendimento.finalizado_em')
|
|
->where('infraestrutura.unidade_id', request()->unidade_id)
|
|
->whereYear('jornada.data', Str::substr($competencia->valor, 0, 4))
|
|
->whereMonth('jornada.data', Str::substr($competencia->valor, 4, 2))
|
|
->selectRaw("
|
|
atendimento.id,
|
|
jornada.data,
|
|
agendamento.paciente_id,
|
|
atendimento_raas.origem_paciente_id,
|
|
atendimento_raas.destino_paciente_id,
|
|
atendimento_raas.motivo_saida_permanencia_id,
|
|
atendimento_raas.carater_atendimento_id,
|
|
atendimento_raas.numero_autorizacao,
|
|
classificacao.servico_id,
|
|
atendimento_raas.classificacao_id,
|
|
atendimento_raas.local_realizacao_exame_id
|
|
")
|
|
->addSelect([
|
|
'cid_principal_id' => function ($query) {
|
|
|
|
$query->select('pc.cid_id')
|
|
->from('ambulatorio_atendimento_soap_problema_condicao as pc')
|
|
->join('ambulatorio_atendimento_soap as s', 's.id', '=', 'pc.soap_id')
|
|
->whereColumn('s.atendimento_id', 'atendimento.id')
|
|
->whereNotNull('pc.cid_id')
|
|
->unionAll(
|
|
|
|
DB::table('ambulatorio_atendimento_procedimento as apc')
|
|
->select('apc.cid_id')
|
|
->join('sigtap.procedimento as proc', 'proc.id', '=', 'apc.procedimento_id')
|
|
->join('sigtap.procedimento_registro as procreg', 'procreg.procedimento_id', '=', 'proc.id')
|
|
->join('sigtap.registro as reg', 'reg.id', '=', 'procreg.registro_id')
|
|
->whereColumn('apc.atendimento_id', 'atendimento.id')
|
|
->where(function ($query) {
|
|
$query->where('reg.codigo', RegistroEnum::RAAS_ATENCAO_DOMICILIAR)
|
|
->orWhere('reg.codigo', RegistroEnum::RAAS_ATENCAO_PSICOSSOCIAL);
|
|
})
|
|
->whereNotNull('apc.cid_id')
|
|
)
|
|
->limit(1);
|
|
}
|
|
])
|
|
->unionAll(DB::table('ambulatorio_atendimento as atendimento')
|
|
->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id')
|
|
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id')
|
|
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id')
|
|
->leftJoin('ambulatorio_atendimento_raas as atendimento_raas', 'atendimento_raas.atendimento_id', 'atendimento.id')
|
|
->leftJoin('sigtap.classificacao as classificacao', 'classificacao.id', 'atendimento_raas.classificacao_id')
|
|
->whereNotNull('atendimento.finalizado_em')
|
|
->where('infraestrutura.unidade_id', request()->unidade_id)
|
|
->whereYear('demanda_espontanea.data', Str::substr($competencia->valor, 0, 4))
|
|
->whereMonth('demanda_espontanea.data', Str::substr($competencia->valor, 4, 2))
|
|
->selectRaw("
|
|
atendimento.id,
|
|
demanda_espontanea.data,
|
|
demanda_espontanea.paciente_id,
|
|
atendimento_raas.origem_paciente_id,
|
|
atendimento_raas.destino_paciente_id,
|
|
atendimento_raas.motivo_saida_permanencia_id,
|
|
atendimento_raas.carater_atendimento_id,
|
|
atendimento_raas.numero_autorizacao,
|
|
classificacao.servico_id,
|
|
atendimento_raas.classificacao_id,
|
|
atendimento_raas.local_realizacao_exame_id
|
|
")
|
|
->addSelect([
|
|
'cid_principal_id' => function ($query) {
|
|
|
|
$query->select('pc.cid_id')
|
|
->from('ambulatorio_atendimento_soap_problema_condicao as pc')
|
|
->join('ambulatorio_atendimento_soap as s', 's.id', '=', 'pc.soap_id')
|
|
->whereColumn('s.atendimento_id', 'atendimento.id')
|
|
->whereNotNull('pc.cid_id')
|
|
->unionAll(
|
|
|
|
DB::table('ambulatorio_atendimento_procedimento as apc')
|
|
->select('apc.cid_id')
|
|
->join('sigtap.procedimento as proc', 'proc.id', '=', 'apc.procedimento_id')
|
|
->join('sigtap.procedimento_registro as procreg', 'procreg.procedimento_id', '=', 'proc.id')
|
|
->join('sigtap.registro as reg', 'reg.id', '=', 'procreg.registro_id')
|
|
->whereColumn('apc.atendimento_id', 'atendimento.id')
|
|
->where(function ($query) {
|
|
$query->where('reg.codigo', RegistroEnum::RAAS_ATENCAO_DOMICILIAR)
|
|
->orWhere('reg.codigo', RegistroEnum::RAAS_ATENCAO_PSICOSSOCIAL);
|
|
})
|
|
->whereNotNull('apc.cid_id')
|
|
)
|
|
->limit(1);
|
|
}
|
|
])
|
|
)->get()
|
|
->each(function ($atendimento) use (&$hasAtendimentoSemAutorizacaoQuePrecisa) {
|
|
|
|
$atendimentoProcedimentos = DB::table('ambulatorio_atendimento_procedimento as atendimento_procedimento')
|
|
->join('sigtap.procedimento as procedimento', 'procedimento.id', 'atendimento_procedimento.procedimento_id')
|
|
->join('sigtap.procedimento_registro as procedimento_registro', 'procedimento_registro.procedimento_id', 'procedimento.id')
|
|
->join('sigtap.registro as registro', 'registro.id', 'procedimento_registro.registro_id')
|
|
->where('atendimento_procedimento.atendimento_id', $atendimento->id)
|
|
->where('registro.codigo', Registro::RAAS_ATENCAO_PSICOSSOCIAL)
|
|
->whereNull('atendimento_procedimento.raas_atendimento_procedimento_id')
|
|
->selectRaw("
|
|
atendimento_procedimento.id,
|
|
atendimento_procedimento.usuario_id,
|
|
atendimento_procedimento.ocupacao_id,
|
|
atendimento_procedimento.procedimento_id,
|
|
atendimento_procedimento.quantidade,
|
|
procedimento.codigo as procedimento_codigo
|
|
")
|
|
->get();
|
|
|
|
if ($atendimentoProcedimentos->isNotEmpty()) {
|
|
|
|
$algumProcedimentoExigeAutorizacao = self::algumProcedimentoExigeAutorizacao(
|
|
$atendimentoProcedimentos->pluck('procedimento_codigo'));
|
|
|
|
if ($algumProcedimentoExigeAutorizacao && is_null($atendimento->numero_autorizacao)) {
|
|
|
|
$hasAtendimentoSemAutorizacaoQuePrecisa = true;
|
|
}
|
|
|
|
$atendimentoRaas = AtendimentoRaas::create([
|
|
'unidade_id' => request()->unidade_id,
|
|
'competencia_id' => request()->competencia_id,
|
|
'data_admissao' => $atendimento->data,
|
|
'data_conclusao' => $atendimento->data,
|
|
'origem_paciente_id' => $atendimento->origem_paciente_id ?? RaasOrigemPaciente::ATENCAO_BASICA,
|
|
'paciente_id' => $atendimento->paciente_id,
|
|
'cobertura_estrategia_saude_familia' => false,
|
|
'unidade_cobertura_estrategia_familia_id' => null,
|
|
'cid_principal_id' => $atendimento->cid_principal_id,
|
|
'cid_secundario_1_id' => null,
|
|
'cid_secundario_2_id' => null,
|
|
'cid_secundario_3_id' => null,
|
|
'cid_causa_associada_id' => null,
|
|
'destino_paciente_id' => $atendimento->destino_paciente_id ?? RaasDestinoPaciente::PERMANENCIA,
|
|
'motivo_saida_permanencia_id' => $atendimento->motivo_saida_permanencia_id
|
|
?? RaasMotivoSaidaPermanencia::POR_CARACTERISTICAS_PROPRIAS_DA_DOENCA,
|
|
'data_ocorrencia' => null,
|
|
'carater_atendimento_id' => $atendimento->carater_atendimento_id ?? RaasCaraterAtendimento::ELETIVO,
|
|
'numero_autorizacao' => $algumProcedimentoExigeAutorizacao ? $atendimento->numero_autorizacao : null,
|
|
'is_automatico' => true,
|
|
]);
|
|
|
|
$atendimentoProcedimentos->each(function ($atendimentoProcedimento) use ($atendimento, $atendimentoRaas) {
|
|
|
|
$procedimentoExigeClassificacao = self::procedimentoExigeClassificacao($atendimentoProcedimento->procedimento_codigo);
|
|
|
|
$atendimentoProcedimentoRaas = $atendimentoRaas->procedimentos()->create([
|
|
'usuario_id' => $atendimentoProcedimento->usuario_id,
|
|
'ocupacao_id' => $atendimentoProcedimento->ocupacao_id,
|
|
'procedimento_id' => $atendimentoProcedimento->procedimento_id,
|
|
'quantidade' => $atendimentoProcedimento->quantidade,
|
|
'data_execucao' => $atendimento->data,
|
|
'servico_id' => $procedimentoExigeClassificacao ? $atendimento->servico_id : null,
|
|
'classificacao_id' => $procedimentoExigeClassificacao ? $atendimento->classificacao_id : null,
|
|
'local_realizacao_exame_id' => $atendimento->local_realizacao_exame_id ?? RaasLocalRealizacaoExame::CAPS,
|
|
]);
|
|
|
|
AmbulatorioAtendimentoProcedimento::where('id', $atendimentoProcedimento->id)
|
|
->update(['raas_atendimento_procedimento_id' => $atendimentoProcedimentoRaas->id]);
|
|
});
|
|
}
|
|
});
|
|
|
|
return [
|
|
'has_atendimento_sem_autorizacao_que_precisa' => $hasAtendimentoSemAutorizacaoQuePrecisa,
|
|
];
|
|
}
|
|
|
|
private static function procedimentoExigeClassificacao($codigo)
|
|
{
|
|
return $codigo === ProcedimentoEnum::ACOMPANHAMENTO_DE_PACIENTE_EM_SAUDE_MENTAL;
|
|
}
|
|
|
|
private static function algumProcedimentoExigeAutorizacao($codigos)
|
|
{
|
|
$exigeAutorizacao = false;
|
|
|
|
$codigos->each(function ($codigo) use (&$exigeAutorizacao) {
|
|
|
|
$exige = collect([
|
|
ProcedimentoEnum::ACOMPANHAMENTO_DE_PACIENTE_EM_SAUDE_MENTAL,
|
|
ProcedimentoEnum::ACOMPANHAMENTO_DE_PESSOAS_COM_NECESSIDADES_DECORRENTES_DO_USO_DE_ALCOOL_CRACK_E_OUTRAS_DROGAS,
|
|
ProcedimentoEnum::ACOMPANHAMENTO_DE_PESSOAS_ADULTAS_COM_SOFRIMENTO_OU_TRANSTORNOS_MENTAIS_DECORRENTES_DO_USO_DE_CRACK_ALCOOL_E_OUTRAS_DROGAS,
|
|
ProcedimentoEnum::ACOMPANHAMENTO_DA_POPULACAO_INFANTO_JUVENIL_COM_SOFRIMENTO_OU_TRANSTORNOS_MENTAIS_DECORRENTES_DO_USO_DE_CRACK_ALCOOL_E_OUTRAS_DROGAS,
|
|
])->contains($codigo);
|
|
|
|
if ($exige) {
|
|
|
|
$exigeAutorizacao = true;
|
|
}
|
|
});
|
|
|
|
return $exigeAutorizacao;
|
|
}
|
|
|
|
private static function storeProcedimentoManual()
|
|
{
|
|
self::validate();
|
|
|
|
$atendimentoRaas = new AtendimentoRaas();
|
|
|
|
$params = self::getSaveParams();
|
|
|
|
$atendimentoRaas->fill($params);
|
|
$atendimentoRaas->is_automatico = false;
|
|
$atendimentoRaas->save();
|
|
|
|
$atendimentoRaas->procedimentos()->createMany($params['procedimentos']);
|
|
|
|
return $atendimentoRaas;
|
|
}
|
|
|
|
public static function update($id)
|
|
{
|
|
$atendimentoRaas = AtendimentoRaas::findOrFail($id);
|
|
|
|
self::validate();
|
|
|
|
DB::transaction(function() use ($atendimentoRaas) {
|
|
|
|
$params = self::getSaveParams();
|
|
|
|
$atendimentoRaas->update($params);
|
|
|
|
$atendimentoRaas->procedimentos()
|
|
->whereNotIn('id', collect($params['procedimentos'])->filter(fn ($p) => !is_null($p['id']))->pluck('id'))
|
|
->each(function ($procedimento) {
|
|
|
|
AmbulatorioAtendimentoProcedimento::where('raas_atendimento_procedimento_id', $procedimento->id)
|
|
->update(['raas_atendimento_procedimento_id' => null]);
|
|
|
|
$procedimento->delete();
|
|
});
|
|
|
|
foreach ($params['procedimentos'] as $procedimentoParams) {
|
|
|
|
$atendimentoRaas->procedimentos()->updateOrCreate(
|
|
['id' => $procedimentoParams['id']],
|
|
$procedimentoParams
|
|
);
|
|
}
|
|
});
|
|
|
|
return $atendimentoRaas->fresh();
|
|
}
|
|
|
|
private static function validate()
|
|
{
|
|
return request()->validate([
|
|
'unidade_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(Unidade::class, 'id'),
|
|
],
|
|
'competencia_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(Competencia::class, 'id'),
|
|
],
|
|
'data_admissao' => [
|
|
'required',
|
|
'date_format:d/m/Y',
|
|
],
|
|
'data_conclusao' => [
|
|
'nullable',
|
|
'date_format:d/m/Y',
|
|
],
|
|
'origem_paciente_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(OrigemPaciente::class, 'id'),
|
|
],
|
|
'paciente_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(Paciente::class, 'id'),
|
|
],
|
|
'cobertura_estrategia_saude_familia' => [
|
|
'required',
|
|
'boolean',
|
|
],
|
|
'unidade_cobertura_estrategia_familia_id' => [
|
|
'nullable',
|
|
'required_if:cobertura_estrategia_saude_familia,true',
|
|
Rule::exists(Unidade::class, 'id'),
|
|
],
|
|
'cid_principal_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(Cid::class, 'id'),
|
|
],
|
|
'cid_secundario1_id' => [
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists(Cid::class, 'id'),
|
|
],
|
|
'cid_secundario2_id' => [
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists(Cid::class, 'id'),
|
|
],
|
|
'cid_secundario3_id' => [
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists(Cid::class, 'id'),
|
|
],
|
|
'cid_causa_associada_id' => [
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists(Cid::class, 'id'),
|
|
],
|
|
'destino_paciente_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(DestinoPaciente::class, 'id'),
|
|
],
|
|
'motivo_saida_permanencia_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(MotivoSaidaPermanencia::class, 'id'),
|
|
],
|
|
'data_ocorrencia' => [
|
|
'nullable',
|
|
'date_format:d/m/Y',
|
|
],
|
|
'carater_atendimento_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(CaraterAtendimento::class, 'id'),
|
|
],
|
|
'numero_autorizacao' => [
|
|
'nullable',
|
|
'digits_between:1,13',
|
|
],
|
|
'procedimentos' => [
|
|
'required',
|
|
'array',
|
|
],
|
|
'procedimentos.*.id' => [
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists(AtendimentoProcedimento::class, 'id'),
|
|
],
|
|
'procedimentos.*.usuario_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(Usuario::class, 'id'),
|
|
],
|
|
'procedimentos.*.ocupacao_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(Ocupacao::class, 'id'),
|
|
],
|
|
'procedimentos.*.procedimento_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(Procedimento::class, 'id'),
|
|
],
|
|
'procedimentos.*.quantidade' => [
|
|
'required',
|
|
'integer',
|
|
'min:1'
|
|
],
|
|
'procedimentos.*.data_execucao' => [
|
|
'required',
|
|
'date_format:d/m/Y',
|
|
],
|
|
'procedimentos.*.servico_id' => [
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists(Servico::class, 'id'),
|
|
],
|
|
'procedimentos.*.classificacao_id' => [
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists(Classificacao::class, 'id'),
|
|
],
|
|
'procedimentos.*.local_realizacao_exame_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(LocalRealizacaoExame::class, 'id'),
|
|
],
|
|
]);
|
|
}
|
|
|
|
private static function getSaveParams()
|
|
{
|
|
$params = request()->all();
|
|
|
|
$params['data_admissao'] = Carbon::createFromFormat('d/m/Y', $params['data_admissao']);
|
|
|
|
$params['data_conclusao'] = is_null($params['data_conclusao'])
|
|
? null : Carbon::createFromFormat('d/m/Y', $params['data_conclusao']);
|
|
|
|
$params['data_ocorrencia'] = is_null($params['data_ocorrencia'])
|
|
? null : Carbon::createFromFormat('d/m/Y', $params['data_ocorrencia']);
|
|
|
|
$params['cid_secundario_1_id'] = $params['cid_secundario1_id'];
|
|
$params['cid_secundario_2_id'] = $params['cid_secundario2_id'];
|
|
$params['cid_secundario_3_id'] = $params['cid_secundario3_id'];
|
|
|
|
unset($params['cid_secundario1_id'], $params['cid_secundario2_id'], $params['cid_secundario3_id']);
|
|
|
|
foreach ($params['procedimentos'] as &$procedimento) {
|
|
|
|
$procedimento['data_execucao'] = Carbon::createFromFormat('d/m/Y', $procedimento['data_execucao']);
|
|
}
|
|
|
|
unset($procedimento);
|
|
|
|
return $params;
|
|
}
|
|
|
|
public static function destroyAutomaticos($unidadeId, $competenciaId)
|
|
{
|
|
DB::table('raas_atendimento as atendimento')
|
|
->join('raas_atendimento_procedimento as procedimento', 'procedimento.atendimento_id', 'atendimento.id')
|
|
->where('atendimento.unidade_id', $unidadeId)
|
|
->where('atendimento.competencia_id', $competenciaId)
|
|
->where('atendimento.is_automatico', true)
|
|
->whereNull('atendimento.exportacao_id')
|
|
->selectRaw("
|
|
atendimento.id,
|
|
GROUP_CONCAT(procedimento.id SEPARATOR ',') as procedimentos_ids
|
|
")
|
|
->groupByRaw("atendimento.id")
|
|
->get()
|
|
->map(function ($atendimento) {
|
|
|
|
AmbulatorioAtendimentoProcedimento::whereRaw("raas_atendimento_procedimento_id IN ({$atendimento->procedimentos_ids})")
|
|
->update(['raas_atendimento_procedimento_id' => null]);
|
|
|
|
AtendimentoRaas::destroy($atendimento->id);
|
|
});
|
|
}
|
|
|
|
public static function destroy($id)
|
|
{
|
|
$atendimentoRaas = AtendimentoRaas::findOrFail($id);
|
|
|
|
DB::transaction(function () use ($atendimentoRaas) {
|
|
|
|
$atendimentoRaas->procedimentos->each(function ($procedimento) {
|
|
|
|
AmbulatorioAtendimentoProcedimento::where('raas_atendimento_procedimento_id', $procedimento->id)
|
|
->update(['raas_atendimento_procedimento_id' => null]);
|
|
});
|
|
|
|
$atendimentoRaas->delete();
|
|
});
|
|
}
|
|
}
|