format('d/m/Y'); $currentLotacao = self::getCurrentLotacao(); return [ 'form' => [ 'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia, 'infraestrutura' => $currentLotacao->infraestrutura->nome, 'sexos' => Sexo::orderBy('descricao')->get(['id', 'descricao'])->makeHidden('identificador') ], 'model' => [ 'periodo' => [ 'start' => $currentDate, 'end' => $currentDate, ], 'sexo_id' => null, 'paciente' => null, 'data_nascimento' => null, 'faa' => null, ], 'additionals' => [ 'periodo_min_date' => $now->copy()->subYears(130)->format('d/m/Y'), 'current_date' => $now->format('d/m/Y') ] ]; } public static function datatable($request) { self::validateDatatableRequest($request); $atendimentos = DB::table('pronto_atendimento_atendimento AS atendimento') ->leftJoin('painel_senha_fila AS fila', 'fila.id', 'atendimento.fila_id') ->leftJoin('painel_senha_opcao_entrada AS opcao_entrada', 'opcao_entrada.id', 'fila.opcao_entrada_id') ->leftJoin('pronto_atendimento_assistencia_social AS assistencia', 'assistencia.atendimento_id', 'atendimento.id') ->join('pronto_atendimento_faa AS faa', 'faa.id', 'atendimento.faa_id') ->join('cadastro_paciente AS paciente', 'paciente.id', 'faa.paciente_id') ->join('cadastro_cidadao AS cidadao', 'cidadao.id', 'paciente.cidadao_id') ->leftJoin('cadastro_sexo AS sexo', 'sexo.id', 'cidadao.sexo_id') ->join('pronto_atendimento_atendimento_recepcao AS recepcao', 'recepcao.atendimento_id', 'atendimento.id') ->join('cadastro_centro_custo AS centro_custo', 'centro_custo.id', 'atendimento.created_centro_custo_id') ->join('cadastro_infraestrutura AS infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id') ->select([ 'atendimento.id AS atendimento_id', 'recepcao.atendido_em AS data', 'recepcao.acompanhante AS acompanhante', 'infraestrutura.nome as infraestrutura', 'faa.id AS faa', 'faa.numero AS faa_numero', 'assistencia.id AS assistencia_id', 'assistencia.anotacao AS anotacao', DB::raw("CONCAT(opcao_entrada.sigla, fila.senha) AS senha"), 'fila.created_at AS senha_data_hora_emissao', 'sexo.descricao AS sexo', 'paciente.prontuario AS paciente_prontuario', 'cidadao.data_nascimento AS paciente_data_nascimento', 'cidadao.nome_mae AS paciente_nome_mae', 'cidadao.nome AS paciente_nome', 'cidadao.nome_social AS paciente_nome_social', 'cidadao.preferencia_nome_id as paciente_preferencia_nome_id', 'cidadao.caracteristicas_fisicas AS paciente_caracteristicas_fisicas', 'paciente.sequencial_desconhecido_em AS paciente_sequencial_desconhecido_em', ]) ->where('centro_custo.infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id) ->whereBetween('recepcao.atendido_em', [ Carbon::createFromFormat('d/m/Y', $request->periodo['start'])->setTime(0, 0, 0), Carbon::createFromFormat('d/m/Y', $request->periodo['end'])->setTime(23, 59, 59) ]) ->when($request->sexo_id, function ($query, $value) { $query->where('cidadao.sexo_id', $value); }) ->when($request->paciente, function ($query, $value) { $query->where('cidadao.nome', 'like', "{$value}%"); }) ->when($request->data_nascimento, function ($query, $value) { $query->whereDate('cidadao.data_nascimento', Carbon::createFromFormat('d/m/Y', $value)); }) ->when($request->faa, function ($query, $value) { $query->where('faa.numero', $value); }) ->get() ->map(function ($atendimento) { $atendimento->impressoes = self::getImpressoes($atendimento); if (is_null($atendimento->paciente_nome_mae)) { $atendimento->paciente_nome_mae = 'Não informado'; } if (is_null($atendimento->paciente_caracteristicas_fisicas)) { $atendimento->paciente_caracteristicas_fisicas = 'Não informado'; } $atendimento->paciente_is_desconhecido = !is_null($atendimento->paciente_sequencial_desconhecido_em); $atendimento->data = (new Carbon($atendimento->data))->format('d/m/Y'); $atendimento->paciente_prefere_nome_social = $atendimento ->paciente_preferencia_nome_id === PreferenciaNome::NOME_SOCIAL; $atendimento->paciente_data_nascimento = is_null($atendimento->paciente_data_nascimento) ? 'Não informada' : (new Carbon($atendimento->paciente_data_nascimento))->format('d/m/Y'); unset( $atendimento->acompanhante, $atendimento->infraestrutura, $atendimento->senha, $atendimento->senha_data_hora_emissao, $atendimento->paciente_preferencia_nome_id, $atendimento->paciente_sequencial_desconhecido_em ); return $atendimento; }); return $atendimentos; } private static function getImpressoes($atendimento) { $atendimentoId = $atendimento->atendimento_id; return [ 'recepcao' => [ [ 'value' => 'etiqueta', 'text' => 'Etiqueta', 'disabled' => false, 'data' => [ 'atendido_em' => (new Carbon($atendimento->data))->format('d/m/Y H:i'), 'infraestrutura' => $atendimento->infraestrutura, 'acompanhante' => $atendimento->acompanhante ?? '', 'paciente' => $atendimento->paciente_nome, 'data_nascimento' => is_null($atendimento->paciente_data_nascimento) ? '' : (new Carbon($atendimento->paciente_data_nascimento))->format('d/m/Y'), 'senha' => $atendimento->senha ] ], [ 'value' => 'raat', 'text' => 'RAAT', 'disabled' => Atendimento::where('id', $atendimentoId) ->whereHas('faa.raat')->get()->isEmpty() ], [ 'value' => 'senha', 'text' => 'Senha', 'disabled' => Atendimento::where('id', $atendimentoId) ->whereHas('fila')->get()->isEmpty(), 'data' => [ 'data' => is_null($atendimento->senha_data_hora_emissao) ? '' : (new Carbon($atendimento->senha_data_hora_emissao))->format('d/m/Y'), 'hora' => is_null($atendimento->senha_data_hora_emissao) ? '' : (new Carbon($atendimento->senha_data_hora_emissao))->format('H:i:s'), 'senha' => $atendimento->senha, ] ], ], 'medicoEnfermagem' => [ [ 'value' => 'atestado', 'text' => 'Atestado', 'disabled' => AtendimentoMedico::where('atendimento_id', $atendimentoId) ->whereHas('atestado')->get()->isEmpty() ], [ 'value' => 'declaracaoAcompanhante', 'text' => 'Declaração de Comparecimento - Acompanhante', 'disabled' => AtendimentoMedico::where('atendimento_id', $atendimentoId) ->whereHas('declaracoesComparecimento', function ($query) { $query->where('tipo_id', DeclaracaoComparecimentoTipo::ACOMPANHANTE); })->get()->isEmpty() ], [ 'value' => 'declaracaoPaciente', 'text' => 'Declaração de Comparecimento - Paciente', 'disabled' => AtendimentoMedico::where('atendimento_id', $atendimentoId) ->whereHas('declaracoesComparecimento', function ($query) { $query->where('tipo_id', DeclaracaoComparecimentoTipo::PACIENTE); })->get()->isEmpty() ], [ 'value' => 'encaminhamentoHospitalar', 'text' => 'Encaminhamento Hospitalar', 'disabled' => AtendimentoMedico::where('atendimento_id', $atendimentoId) ->whereHas('encaminhamentoHospitalar')->get()->isEmpty() ], [ 'value' => 'examesImagem', 'text' => 'Exames - Imagem', 'disabled' => Atendimento::where('id', $atendimentoId) ->whereHas('pedidosExames.exames.exame', function ($query) { $query->where('tipo_id', Tipo::IMAGEM); })->get()->isEmpty() ], [ 'value' => 'examesLaboratorial', 'text' => 'Exames - Laboratorial', 'disabled' => Atendimento::where('id', $atendimentoId) ->whereHas('pedidosExames.exames.exame', function ($query) { $query->where('tipo_id', Tipo::LABORATORIAL); })->get()->isEmpty() ], [ 'value' => 'guiaEncaminhamento', 'text' => 'Guia de Encaminhamento', 'disabled' => AtendimentoMedico::where('atendimento_id', $atendimentoId) ->whereHas('encaminhamentos')->get()->isEmpty() ], [ 'value' => 'orientacoesPaciente', 'text' => 'Orientação ao paciente', 'disabled' => AtendimentoMedico::where('atendimento_id', $atendimentoId) ->whereNotNull('orientacoes')->get()->isEmpty() ], [ 'value' => 'prescricaoInterna', 'text' => 'Prescrição Interna', 'disabled' => AtendimentoMedico::where('atendimento_id', $atendimentoId) ->whereHas('prescricoesInternas')->get()->isEmpty() ], [ 'value' => 'receitaMedica', 'text' => 'Receita Médica', 'disabled' => AtendimentoMedico::where('atendimento_id', $atendimentoId) ->whereHas('receita')->get()->isEmpty() ], [ 'value' => 'servicoEnfermagem', 'text' => 'Serviços Enfermagem', 'disabled' => ServicoEnfermagem::whereHas('atendimentoMedico', function ($query) use ($atendimentoId) { $query->where('atendimento_id', $atendimentoId); }) ->orWhereHas('atendimentoEnfermagem', function ($query) use ($atendimentoId) { $query->where('atendimento_id', $atendimentoId); }) ->orWhereHas('pacienteObservacao', function ($query) use ($atendimentoId) { $query->where('atendimento_id', $atendimentoId); }) ->get() ->isEmpty() ], ], 'assistenciaSocial' => [ [ 'value' => 'assistencia', 'text' => 'Assistencia', 'disabled' => false, 'data' => [ 'paciente' => $atendimento->paciente_nome, 'anotacao' => $atendimento->anotacao ] ], ] ]; } private static function validateDatatableRequest($request) { $minDate = Carbon::now()->subYears(130)->format('Y-m-d'); $request->validate([ 'periodo.start' => [ 'required', 'date_format:d/m/Y', "after_or_equal:{$minDate}", 'before_or_equal:periodo.end', ], 'periodo.end' => [ 'required', 'date_format:d/m/Y', 'after_or_equal:periodo.start', 'before_or_equal:now', ], 'sexo_id' => [ 'nullable', 'integer', ], 'paciente' => [ 'nullable', 'string', ], 'data_nascimento' => [ 'nullable', 'date_format:d/m/Y', ], 'faa' => [ 'nullable', 'integer', ], ]); } }