select([ 'cadastro_cidadao.caracteristicas_fisicas', 'cadastro_cidadao.nome AS paciente_nome', 'cadastro_sexo.descricao AS sexo', 'cadastro_paciente.prontuario', 'cadastro_paciente.sequencial_desconhecido', 'pronto_atendimento_faa.id as faa_id', 'pronto_atendimento_faa.numero as faa_numero', DB::raw('DATE_FORMAT(pronto_atendimento_atendimento_recepcao.atendido_em, "%d/%m/%Y") AS data'), DB::raw(' ( CASE WHEN cadastro_cidadao.nome_mae IS NULL THEN "Não informado" ELSE cadastro_cidadao.nome_mae END ) as nome_mae' ), DB::raw(' ( CASE WHEN DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") IS NULL THEN "Não informada" ELSE DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") END ) as data_nascimento '), ]) ->join( 'pronto_atendimento_faa', 'pronto_atendimento_faa.id', 'pronto_atendimento_atendimento.faa_id' ) ->join( 'pronto_atendimento_atendimento_recepcao', 'pronto_atendimento_atendimento_recepcao.atendimento_id', 'pronto_atendimento_atendimento.id' ) ->join( 'cadastro_paciente', 'cadastro_paciente.id', 'pronto_atendimento_faa.paciente_id' ) ->join( 'cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id' ) ->join( 'cadastro_sexo', 'cadastro_sexo.id', 'cadastro_cidadao.sexo_id' ) ->join( 'cadastro_centro_custo', 'cadastro_centro_custo.id', 'pronto_atendimento_atendimento.created_centro_custo_id' ) ->where('pronto_atendimento_faa.finalizado', true) ->whereNull('pronto_atendimento_faa.deleted_at') ->where('cadastro_centro_custo.infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id); if (!request()->get('order')) { $atendimentos->orderByDesc('pronto_atendimento_atendimento_recepcao.atendido_em'); } return datatables() ->of($atendimentos) ->filterColumn('data', function ($query, $keyword) { $query->whereRaw("DATE_FORMAT(pronto_atendimento_atendimento_recepcao.atendido_em, '%d/%m/%Y') like ?", ["%{$keyword}%"]) ->orWhereRaw( "CONCAT( SUBSTR(pronto_atendimento_atendimento_recepcao.atendido_em,9,2), SUBSTR(pronto_atendimento_atendimento_recepcao.atendido_em,6,2), SUBSTR(pronto_atendimento_atendimento_recepcao.atendido_em,1,4) ) like ?", ["%{$keyword}%"] ); }) ->orderColumn('data', function ($query, $order) { $query->orderBy('pronto_atendimento_atendimento_recepcao.atendido_em', $order); }) ->skipTotalRecords() ->make(true); } public function create() { return response()->json([ 'form' => [ 'centros_custo_encaminhamento' => self::getCentroCustoLogado()->infraestrutura->centrosCusto() ->whereHas('setor', function ($query) { $query->whereNotIn('tipo_id', [ SetorTipo::RECEPCAO, SetorTipo::TRIAGEM, ]); }) ->get() ->map(fn ($cc) => [ 'id' => $cc->id, 'setor_nome' => $cc->setor->nome ]) ->sortBy('setor_nome') ->values(), ] ], Response::HTTP_OK); } public function update($id) { $this->auth(__FUNCTION__); $faa = ReaberturaAtendimento::findOrFail($id); $reabertura = new FaaReabertura; $reabertura->faa_id = $id; $reabertura->save(); DB::transaction(function () use ($faa) { if (is_null(request()->centro_custo_id)) { $faa->update([ 'finalizado' => false, 'reaberto_em' => Carbon::Now(), 'reabertura_usuario_id' => Lotacao::find(auth()->payload()->get('lotacao_id'))->usuario_id, ]); } else { $faa->update([ 'desfecho_id' => null, 'finalizado' => false, 'reaberto_em' => Carbon::Now(), 'reabertura_usuario_id' => Lotacao::find(auth()->payload()->get('lotacao_id'))->usuario_id, ]); $fila = $faa->atendimentos()->first()->fila; if ($fila !== null) { $fila->update([ 'finalizado' => false, 'evadido' => false, 'reaberto_em' => Carbon::now(), 'reabertura_lotacao_id' => auth()->payload()->get('lotacao_id'), ]); FilaService::forward([ 'centro_custo_id_para' => request()->centro_custo_id, ], $fila->id); } } }); return response()->json($faa->fresh(), Response::HTTP_OK); } private function getCentroCustoLogado() { return CentroCusto::findOrFail(auth()->payload()->get('centro_custo_id')); } }