infraestrutura_id; $atendimentosComRaasPendentes = DB::table('ambulatorio_atendimento_procedimento') ->join('sigtap.procedimento_registro', 'procedimento_registro.procedimento_id', 'ambulatorio_atendimento_procedimento.procedimento_id') ->join('sigtap.registro', 'registro.id', 'procedimento_registro.registro_id') ->whereNull('ambulatorio_atendimento_procedimento.raas_atendimento_procedimento_id') ->where('registro.codigo', RegistroEnum::RAAS_ATENCAO_PSICOSSOCIAL) ->where(fn ($q) => $q ->whereNull('ambulatorio_atendimento_procedimento.soap_id') ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap') ->whereColumn('ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_procedimento.soap_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ) ) ->select('ambulatorio_atendimento_procedimento.atendimento_id') ->distinct(); return DB::table('ambulatorio_atendimento as atendimento') ->joinSub($atendimentosComRaasPendentes, 'raas_sub', fn ($join) => $join->on('raas_sub.atendimento_id', 'atendimento.id') ) ->leftJoin('painel_senha_fila as fila', 'fila.id', 'atendimento.fila_id') ->leftJoin('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->leftJoin('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->leftJoin('cadastro_lotacao as jornada_lotacao', 'jornada_lotacao.id', 'jornada.lotacao_id') ->leftJoin('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->leftJoin('cadastro_centro_custo as demanda_espontanea_centro_custo', 'demanda_espontanea_centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->leftJoin('cadastro_paciente as paciente', 'paciente.id', DB::raw("COALESCE(demanda_espontanea.paciente_id, agendamento.paciente_id)")) ->leftJoin('cadastro_cidadao as paciente_cidadao', 'paciente_cidadao.id', 'paciente.cidadao_id') ->leftJoin('cadastro_sexo as paciente_cidadao_sexo', 'paciente_cidadao_sexo.id', 'paciente_cidadao.sexo_id') ->leftJoin('cadastro_usuario as usuario', 'usuario.id', DB::raw("COALESCE(demanda_espontanea.usuario_id, jornada_lotacao.usuario_id)")) ->leftJoin('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_id') ->leftJoin('cadastro_especialidade as especialidade', 'especialidade.id', DB::raw("COALESCE(demanda_espontanea.especialidade_id, jornada.especialidade_id)")) ->whereRaw('COALESCE(jornada_lotacao.infraestrutura_id, demanda_espontanea_centro_custo.infraestrutura_id) = ?', [$infraestruturaId]) ->selectRaw(" atendimento.id, DATE_FORMAT(COALESCE(jornada.data, CAST(demanda_espontanea.created_at AS DATE)), '%d/%m/%Y') as data, paciente_cidadao.nome as paciente_nome, paciente_cidadao.nome_social as paciente_nome_social, paciente_cidadao.preferencia_nome_id as paciente_preferencia_nome_id, DATE_FORMAT(paciente_cidadao.data_nascimento, '%d/%m/%Y') as paciente_data_nascimento, paciente_cidadao.cpf as paciente_cpf, paciente_cidadao.cns as paciente_cns, paciente_cidadao.nome_mae as paciente_nome_mae, LPAD(paciente.prontuario, 7, '0') as paciente_prontuario, usuario_cidadao.nome as usuario_nome, especialidade.nome as especialidade_nome, DATE_FORMAT(agendamento.horario, '%H:%i') as horario_agendado, DATE_FORMAT(fila.created_at, '%H:%i') as horario_chegada "); } public static function create() { $atendimento = Atendimento::findOrFail(request()->atendimento_id); $hasProcedimentoRaasQuePrecisaDeClassificacao = self::hasProcedimentoRaasQuePrecisaDeClassificacao($atendimento->id); $hasProcedimentoRaasQuePrecisaDeAutorizacao = self::hasProcedimentoRaasQuePrecisaDeAutorizacao($atendimento->id); return [ 'additionals' => [ 'has_procedimento_raas_que_precisa_de_classificacao' => $hasProcedimentoRaasQuePrecisaDeClassificacao, 'has_procedimento_raas_que_precisa_de_autorizacao' => $hasProcedimentoRaasQuePrecisaDeAutorizacao, ], 'form' => [ 'origens_paciente' => OrigemPaciente::orderBy('nome')->get(['id', 'nome']), 'destinos_paciente' => DestinoPaciente::orderBy('nome')->get(['id', 'nome']), 'motivos_saida_permanencia' => MotivoSaidaPermanencia::orderBy('nome')->get(['id', 'nome']), 'carateres_atendimento' => CaraterAtendimento::orderBy('nome')->get(['id', 'nome']), 'classificacoes' => Classificacao::where('competencia_id', Competencia::max('id')) ->whereHas('servico', fn ($q) => $q ->where('codigo', ServicoEnum::SERVICO_DE_ATENCAO_PSICOSSOCIAL) ) ->whereIn('codigo', [ ClassificacaoServicoAtencaoPsicossocial::RESIDENCIA_TERAPEUTICA_EM_SAUDE_MENTAL, ClassificacaoServicoAtencaoPsicossocial::SERVICO_RESIDENCIAL_TERAPEUTICO_TIPO_I_SRT_TIPO_I, ClassificacaoServicoAtencaoPsicossocial::SERVICO_RESIDENCIAL_TERAPEUTICO_TIPO_II_SRT_TIPO_II, ]) ->orderBy('nome') ->get(['id', 'nome']), 'locais_realizacao_exame' => LocalRealizacaoExame::orderBy('nome')->get(['id', 'nome']), 'procedimentos' => self::getProcedimentos($atendimento), ], 'model' => [ 'origem_paciente_id' => $atendimento->raas->origem_paciente_id ?? null, 'destino_paciente_id' => $atendimento->raas->destino_paciente_id ?? null, 'motivo_saida_permanencia_id' => $atendimento->raas->motivo_saida_permanencia_id ?? $atendimento->especialidade->atencao_basica_motivo_saida_permanencia_id ?? null, 'carater_atendimento_id' => $atendimento->raas->carater_atendimento_id ?? null, 'numero_autorizacao' => $hasProcedimentoRaasQuePrecisaDeAutorizacao ? ($atendimento->raas->numero_autorizacao ?? $atendimento->especialidade->atencao_basica_numero_autorizacao ?? null ) : null, 'classificacao_id' => $hasProcedimentoRaasQuePrecisaDeClassificacao ? ($atendimento->raas->classificacao_id ?? (is_null($atendimento->especialidade) ? null : Classificacao::where('competencia_id', Competencia::max('id')) ->whereHas('servico', fn ($q) => $q ->where('codigo', ServicoEnum::SERVICO_DE_ATENCAO_PSICOSSOCIAL) ) ->where('codigo', $atendimento->especialidade->atencao_basica_classificacao_codigo) ->first()->id ?? null ) ) : null, 'local_realizacao_exame_id' => $atendimento->raas->local_realizacao_exame_id ?? null, ], ]; } private static function getProcedimentos($atendimento) { $procedimentos = 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') ->whereNull('atendimento_procedimento.raas_atendimento_procedimento_id') ->where('atendimento_procedimento.atendimento_id', $atendimento->id) ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->where('registro.codigo', RegistroEnum::RAAS_ATENCAO_PSICOSSOCIAL) ->selectRaw(" procedimento.id, procedimento.codigo, procedimento.nome ") ->distinct() ->get(); $procedimentos->each(function ($procedimento) { $procedimento->codigo = MaskUtil::getProcedimentoMasked($procedimento->codigo); }); return $procedimentos; } public static function store() { self::validate(); $atendimento = Atendimento::findOrFail(request()->atendimento_id); AtendimentoRaas::updateOrCreate( ['atendimento_id' => $atendimento->id], [ 'origem_paciente_id' => request()->origem_paciente_id, 'destino_paciente_id' => request()->destino_paciente_id, 'motivo_saida_permanencia_id' => request()->motivo_saida_permanencia_id, 'carater_atendimento_id' => request()->carater_atendimento_id, 'numero_autorizacao' => self::hasProcedimentoRaasQuePrecisaDeAutorizacao($atendimento->id) ? request()->numero_autorizacao : null, 'classificacao_id' => self::hasProcedimentoRaasQuePrecisaDeClassificacao($atendimento->id) ? request()->classificacao_id : null, 'local_realizacao_exame_id' => request()->local_realizacao_exame_id, ] ); return $atendimento->raas; } private static function validate() { request()->validate([ 'atendimento_id' => [ 'required', 'integer', Rule::exists(Atendimento::class, 'id'), ], 'origem_paciente_id' => [ 'required', 'integer', Rule::exists(OrigemPaciente::class, 'id'), ], 'destino_paciente_id' => [ 'required', 'integer', Rule::exists(DestinoPaciente::class, 'id'), ], 'motivo_saida_permanencia_id' => [ 'required', 'integer', Rule::exists(MotivoSaidaPermanencia::class, 'id'), ], 'carater_atendimento_id' => [ 'required', 'integer', Rule::exists(CaraterAtendimento::class, 'id'), ], 'numero_autorizacao' => [ 'nullable', Rule::requiredIf(fn () => self::hasProcedimentoRaasQuePrecisaDeAutorizacao(request()->atendimento_id)), 'digits_between:1,13', ], 'classificacao_id' => [ 'nullable', Rule::requiredIf(fn () => self::hasProcedimentoRaasQuePrecisaDeClassificacao(request()->atendimento_id)), 'integer', Rule::exists(Classificacao::class, 'id'), ], 'local_realizacao_exame_id' => [ 'required', 'integer', Rule::exists(LocalRealizacaoExame::class, 'id'), ], ]); } private static function hasProcedimentoRaasQuePrecisaDeClassificacao($atendimentoId) { return DB::table('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('sigtap.procedimento as procedimento', 'procedimento.id', 'atendimento_procedimento.procedimento_id') ->whereNull('atendimento_procedimento.raas_atendimento_procedimento_id') ->where('atendimento_procedimento.atendimento_id', $atendimentoId) ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->where('procedimento.codigo', ProcedimentoEnum::ACOMPANHAMENTO_DE_PACIENTE_EM_SAUDE_MENTAL) ->exists(); } private static function hasProcedimentoRaasQuePrecisaDeAutorizacao($atendimentoId) { return DB::table('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('sigtap.procedimento as procedimento', 'procedimento.id', 'atendimento_procedimento.procedimento_id') ->whereNull('atendimento_procedimento.raas_atendimento_procedimento_id') ->where('atendimento_procedimento.atendimento_id', $atendimentoId) ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereIn('procedimento.codigo', [ 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, ]) ->exists(); } }