format('Y-m-d'); $currentLotacao = self::getCurrentLotacao(); $cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail(); $agendamentos = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia', 'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia', 'parametrizacao_exame.codigo AS procedimento_codigo', 'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id', DB::raw('CASE WHEN cadastro_infraestrutura_coleta.principal THEN cadastro_unidade_coleta.nome_fantasia ELSE cadastro_infraestrutura_coleta.nome END AS coleta_infraestrutura_nome'), DB::raw('upper(parametrizacao_exame.nome) AS exame_nome'), DB::raw(' CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN cadastro_unidade_laboratorio.nome_fantasia ELSE cadastro_infraestrutura_laboratorio.nome END AS laboratorio_infraestrutura_nome' ), DB::raw("SUM(parametrizacao_exame.quantidade_reagente) AS quantidade_reagente"), DB::raw("COUNT(parametrizacao_exame.id) AS quantidade"), ]) ->join( 'exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id' ) ->join( 'cadastro_infraestrutura as cadastro_infraestrutura_coleta', 'cadastro_infraestrutura_coleta.id', 'exame_laboratorial_agendamento.coleta_infraestrutura_id' ) ->join( 'cadastro_unidade as cadastro_unidade_coleta', 'cadastro_unidade_coleta.id', 'cadastro_infraestrutura_coleta.unidade_id' ) ->join( 'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio', 'cadastro_infraestrutura_laboratorio.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id' ) ->join( 'cadastro_unidade as cadastro_unidade_laboratorio', 'cadastro_unidade_laboratorio.id', 'cadastro_infraestrutura_laboratorio.unidade_id' ) ->join( 'parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id' ) ->groupBy([ 'parametrizacao_exame.id', ]); if (request()->periodo) { $agendamentos->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']); $agendamentos->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']); } else { $agendamentos->where('exame_laboratorial_agendamento.data', $currentDate); } if (request()->exame_id) { $agendamentos->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id); } if (request()->coleta_unidade_id) { $agendamentos->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id); } if (request()->coleta_infraestrutura_id) { $agendamentos->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id); } if (request()->laboratorio_id) { $agendamentos->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $agendamentos->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } $agendamentos->where(function($query) { $query ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, ] ); }); $agendamentos = $agendamentos->get(); if (request()->periodo) { $periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y'); $periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y'); } else { $periodoStartFilter = $currentDate; $periodoEndFilter = $currentDate; } return [ 'data_atual' => Carbon::now()->format('d/m/Y'), 'paciente_nome' => request()->paciente_nome ?? '-', 'agendamento_numero' => request()->agendamento_numero ?? '-', 'prontuario' => request()->prontuario ?? '-', 'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS', 'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS', 'laboratorio_nome_fantasia' => request()->laboratorio_nome ?? 'TODOS', 'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS', 'exame_nome' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS', 'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia, 'unidade_id' => $currentLotacao->infraestrutura->unidade->id, 'infraestrutura_id' => $currentLotacao->infraestrutura->id, 'infraestrutura' => $currentLotacao->infraestrutura->nome, 'sistema' => [ 'ramo_atividade' => [ 'nome' => $cliente->sistema->ramoAtividade->nome ?? null, ], 'nome' => $cliente->sistema->nome ?? null, 'versao' => $cliente->sistema->versao ?? null, ], 'periodo' => [ 'start' => $periodoStartFilter, 'end' => $periodoEndFilter, ], 'colunas' => collect(request()->selectedCols), 'agendamentos' => $agendamentos->map(fn ($agendamento) => [ 'coleta_nome_fantasia' => $agendamento->coleta_nome_fantasia, 'coleta_infraestrutura_nome' => $agendamento->coleta_infraestrutura_nome, 'laboratorio_nome_fantasia' => $agendamento->laboratorio_nome_fantasia, 'laboratorio_infraestrutura_nome' => $agendamento->laboratorio_infraestrutura_nome, 'exame_nome' => $agendamento->exame_nome, 'procedimento_codigo' => !empty($agendamento->procedimento_codigo) ? $agendamento->procedimento_codigo : '-', 'quantidade_reagente' => $agendamento->quantidade_reagente ?? '0', 'quantidade' => $agendamento->quantidade, ]), ]; } }