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', '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'), ]) ->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id') ->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_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') ->groupBy([ 'exame_laboratorial_agendamento.coleta_infraestrutura_id', ]); if (request()->prontuario) { $agendamentos->where('cadastro_paciente.prontuario', request()->prontuario); } if (request()->agendamento_id) { $agendamentos->where('exame_laboratorial_agendamento.id', request()->agendamento_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()->unidade_id) { $agendamentos->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $agendamentos->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_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); } if (request()->status_id) { $agendamentos->where(function($query) { foreach (request()->status_id as $status) { if ($status == StatusAgendamento::NAO_REALIZADO) { $query->orWhereIn( 'status_id', [ StatusAgendamento::NAO_REALIZADO, StatusAgendamento::FALTOU, StatusAgendamento::NOVA_COLETA, StatusAgendamento::OUTROS, ] ); } else if ($status == StatusAgendamento::AGENDADO) { $query ->orWhere('status_id', StatusAgendamento::AGENDADO) ->orWhereNull('status_id'); } else { $query->orWhere('status_id', $status); } } }); } $agendamentos = $agendamentos->get(); if (request()->periodo) { $periodoStart = Carbon::create(request()->periodo['start'])->format('Y-m-d'); $periodoEnd = Carbon::create(request()->periodo['end'])->format('Y-m-d'); $periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y'); $periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y'); } else { $periodoStart = $currentDate; $periodoEnd = $currentDate; $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 ?? '-', 'unidade_nome' => request()->unidade_nome ?? 'TODAS', 'infraestrutura_nome' => request()->infraestrutura_nome ?? 'TODOS', 'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS', 'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS', 'laboratorio_nome' => request()->laboratorio_nome ?? 'TODOS', 'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS', 'exame_grupo' => 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, ], 'status' => collect(request()->status_selecionado), 'total_status' => collect(request()->status_selecionado) ->map(function ($query) { return $query["id"]; }) ->toArray(), 'colunas' => collect(request()->selectedCols), 'unidade_coleta' => $agendamentos->map(fn ($coleta) => [ 'coleta_nome_fantasia' => $coleta->coleta_nome_fantasia, 'coleta_infraestrutura_nome' => $coleta->coleta_infraestrutura_nome, 'total_exames' => AgendamentoExame::whereHas('agendamento', function($query) use ($coleta) { $query ->where('coleta_infraestrutura_id', $coleta->coleta_infraestrutura_id) ->where('data', '>=', request()->periodo['start']) ->where('data', '<=', request()->periodo['end']) ->whereHas('paciente', function($query) { if (request()->prontuario) { $query->where('prontuario', request()->prontuario); } }); if (request()->agendamento_id) { $query->where('agendamento_id', request()->agendamento_id); } if (request()->unidade_id) { $query->where('unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $query->where('infraestrutura_id', request()->infraestrutura_id); } if (request()->coleta_unidade_id) { $query->where('coleta_unidade_id', request()->coleta_unidade_id); } if (request()->coleta_infraestrutura_id) { $query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id); } if (request()->laboratorio_id) { $query->where('laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } }) ->where(function($query) { if (request()->exame_id) { $query->where('exame_id', request()->exame_id); } if (request()->status_id) { $query->where(function($query) { foreach (request()->status_id as $status) { if ($status == StatusAgendamento::NAO_REALIZADO) { $query->orWhereIn( 'status_id', [ StatusAgendamento::NAO_REALIZADO, StatusAgendamento::FALTOU, StatusAgendamento::NOVA_COLETA, StatusAgendamento::OUTROS, ] ); } else if ($status == StatusAgendamento::AGENDADO) { $query ->orWhere('status_id', StatusAgendamento::AGENDADO) ->orWhereNull('status_id'); } else { $query->orWhere('status_id', $status); } } }); } }) ->count(), 'agendamentos' => Agendamento::whereHas('agendamentoExame', function($query) { if (request()->exame_id) { $query->where('exame_id', request()->exame_id); } if (request()->status_id) { $query->where(function($query) { foreach (request()->status_id as $status) { if ($status == StatusAgendamento::NAO_REALIZADO) { $query->orWhereIn( 'status_id', [ StatusAgendamento::NAO_REALIZADO, StatusAgendamento::FALTOU, StatusAgendamento::NOVA_COLETA, StatusAgendamento::OUTROS, ] ); } else if ($status == StatusAgendamento::AGENDADO) { $query ->orWhere('status_id', StatusAgendamento::AGENDADO) ->orWhereNull('status_id'); } else { $query->orWhere('status_id', $status); } } }); } }) ->whereHas('paciente', function($query) { if (request()->prontuario) { $query->where('prontuario', request()->prontuario); } }) ->where('coleta_infraestrutura_id', $coleta->coleta_infraestrutura_id) ->where(function($query) { if (request()->agendamento_id) { $query->where('agendamento_id', request()->agendamento_id); } if (request()->infraestrutura_id) { $query->where('infraestrutura_id', request()->infraestrutura_id); } if (request()->unidade_id) { $query->where('unidade_id', request()->unidade_id); } if (request()->coleta_unidade_id) { $query->where('coleta_unidade_id', request()->coleta_unidade_id); } if (request()->laboratorio_id) { $query->where('laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } }) ->where('data', '>=', request()->periodo['start']) ->where('data', '<=', request()->periodo['end']) ->get() ->map(fn ($agendamento) => [ 'agendamento_id' => $agendamento->id, 'agendamento_numero' => $agendamento->agendamento_numero, 'paciente_nome' => $agendamento->paciente->cidadao->nome ?? null, 'paciente_nome_social' => $agendamento->paciente->cidadao->nome_social, 'prefereNomeSocial' => $agendamento->paciente->cidadao ->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL, 'paciente_prontuario' => $agendamento->paciente->prontuario, 'data' => $agendamento->data ?? null, 'data_coleta' => (new Carbon($agendamento->data_coleta))->format('d/m/Y') ?? null, 'exames' => AgendamentoExame::where('agendamento_id', $agendamento->id) ->where(function($query) { if (request()->exame_id) { $query->where('exame_id', request()->exame_id); } if (request()->status_id) { $query->where(function($query) { foreach (request()->status_id as $status) { if ($status == StatusAgendamento::NAO_REALIZADO) { $query->orWhereIn( 'status_id', [ StatusAgendamento::NAO_REALIZADO, StatusAgendamento::FALTOU, StatusAgendamento::NOVA_COLETA, StatusAgendamento::OUTROS, ] ); } else if ($status == StatusAgendamento::AGENDADO) { $query ->orWhere('status_id', StatusAgendamento::AGENDADO) ->orWhereNull('status_id'); } else { $query->orWhere('status_id', $status); } } }); } }) ->get() ->map(function ($query) use ($agendamento) { return [ 'id' => $query->id, 'exame_id' => $query->exame_id, 'exame_nome' => mb_strtoupper($query->exame->nome), 'procedimento' => $query->exame->procedimento ? $query->exame->procedimento->nome : "SEM PROCEDIMENTO", 'agendamento_numero' => $agendamento->agendamento_numero, 'data_agendamento' => $agendamento->data->format('d/m/Y'), 'status' => $query->status_id ? self::getStatus($query) : 'AGENDADO', 'data_coleta' => $query->data_coleta ? $query->data_coleta->format('d/m/Y') : '-', 'unidade' => $agendamento->unidade->nome_fantasia, 'infraestrutura_nome' => $agendamento->infraestrutura->nome, 'unidade_coleta' => $agendamento->coletaUnidade->nome_fantasia, 'coleta_infraestrutura_nome' => $agendamento->coletaInfraestrutura->nome, 'laboratorio' => $agendamento->laboratorioUnidade->nome_fantasia, 'laboratorio_infraestrutura_nome' => $agendamento->laboratorioInfraestrutura->nome, ]; }) ->values(), ]) ->sortBy('data') ->values(), ]), 'agendado' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) { $query ->where('data', '>=', $periodoStart) ->where('data', '<=', $periodoEnd) ->whereHas('paciente', function($query) { if (request()->prontuario) { $query->where('prontuario', request()->prontuario); } }); if (request()->agendamento_id) { $query->where('agendamento_id', request()->agendamento_id); } if (request()->unidade_id) { $query->where('unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $query->where('infraestrutura_id', request()->infraestrutura_id); } if (request()->coleta_unidade_id) { $query->where('coleta_unidade_id', request()->coleta_unidade_id); } if (request()->coleta_infraestrutura_id) { $query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id); } if (request()->laboratorio_id) { $query->where('laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } }) ->where(function($query) { if (request()->exame_id) { $query->where('exame_id', request()->exame_id); } }) ->where(function($query) { $query ->where('status_id', StatusAgendamento::AGENDADO) ->orWhereNull('status_id'); }) ->count(), 'nao_coletado' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) { $query ->where('data', '>=', $periodoStart) ->where('data', '<=', $periodoEnd) ->whereHas('paciente', function($query) { if (request()->prontuario) { $query->where('prontuario', request()->prontuario); } }); if (request()->agendamento_id) { $query->where('agendamento_id', request()->agendamento_id); } if (request()->unidade_id) { $query->where('unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $query->where('infraestrutura_id', request()->infraestrutura_id); } if (request()->coleta_unidade_id) { $query->where('coleta_unidade_id', request()->coleta_unidade_id); } if (request()->coleta_infraestrutura_id) { $query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id); } if (request()->laboratorio_id) { $query->where('laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } }) ->where(function($query) { if (request()->exame_id) { $query->where('exame_id', request()->exame_id); } }) ->where(function($query) { $query->whereIn( 'status_id', [ StatusAgendamento::NAO_REALIZADO, StatusAgendamento::FALTOU, StatusAgendamento::NOVA_COLETA, StatusAgendamento::OUTROS, ] ); }) ->count(), 'coleta_realizada' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) { $query ->where('data', '>=', $periodoStart) ->where('data', '<=', $periodoEnd) ->whereHas('paciente', function($query) { if (request()->prontuario) { $query->where('prontuario', request()->prontuario); } }); if (request()->agendamento_id) { $query->where('agendamento_id', request()->agendamento_id); } if (request()->unidade_id) { $query->where('unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $query->where('infraestrutura_id', request()->infraestrutura_id); } if (request()->coleta_unidade_id) { $query->where('coleta_unidade_id', request()->coleta_unidade_id); } if (request()->coleta_infraestrutura_id) { $query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id); } if (request()->laboratorio_id) { $query->where('laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } }) ->where(function($query) { if (request()->exame_id) { $query->where('exame_id', request()->exame_id); } }) ->where('status_id', StatusAgendamento::REALIZADO) ->count(), 'pendente_assinatura' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) { $query ->where('data', '>=', $periodoStart) ->where('data', '<=', $periodoEnd) ->whereHas('paciente', function($query) { if (request()->prontuario) { $query->where('prontuario', request()->prontuario); } }); if (request()->agendamento_id) { $query->where('agendamento_id', request()->agendamento_id); } if (request()->unidade_id) { $query->where('unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $query->where('infraestrutura_id', request()->infraestrutura_id); } if (request()->coleta_unidade_id) { $query->where('coleta_unidade_id', request()->coleta_unidade_id); } if (request()->coleta_infraestrutura_id) { $query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id); } if (request()->laboratorio_id) { $query->where('laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } }) ->where(function($query) { if (request()->exame_id) { $query->where('exame_id', request()->exame_id); } }) ->where('status_id', StatusAgendamento::PENDENTE_ASSINATURA) ->count(), 'pendente_impressao' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) { $query ->where('data', '>=', $periodoStart) ->where('data', '<=', $periodoEnd) ->whereHas('paciente', function($query) { if (request()->prontuario) { $query->where('prontuario', request()->prontuario); } }); if (request()->agendamento_id) { $query->where('agendamento_id', request()->agendamento_id); } if (request()->unidade_id) { $query->where('unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $query->where('infraestrutura_id', request()->infraestrutura_id); } if (request()->coleta_unidade_id) { $query->where('coleta_unidade_id', request()->coleta_unidade_id); } if (request()->coleta_infraestrutura_id) { $query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id); } if (request()->laboratorio_id) { $query->where('laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } }) ->where(function($query) { if (request()->exame_id) { $query->where('exame_id', request()->exame_id); } }) ->where('status_id', StatusAgendamento::ASSINADO) ->where('entregue', false) ->where('impressao', false) ->count(), 'pendente_entrega' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) { $query ->where('data', '>=', $periodoStart) ->where('data', '<=', $periodoEnd) ->whereHas('paciente', function($query) { if (request()->prontuario) { $query->where('prontuario', request()->prontuario); } }); if (request()->agendamento_id) { $query->where('agendamento_id', request()->agendamento_id); } if (request()->unidade_id) { $query->where('unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $query->where('infraestrutura_id', request()->infraestrutura_id); } if (request()->coleta_unidade_id) { $query->where('coleta_unidade_id', request()->coleta_unidade_id); } if (request()->coleta_infraestrutura_id) { $query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id); } if (request()->laboratorio_id) { $query->where('laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } }) ->where(function($query) { if (request()->exame_id) { $query->where('exame_id', request()->exame_id); } }) ->where('status_id', StatusAgendamento::ASSINADO) ->where('entregue', false) ->where('impressao', true) ->count(), 'entregue' => AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) { $query ->where('data', '>=', $periodoStart) ->where('data', '<=', $periodoEnd) ->whereHas('paciente', function($query) { if (request()->prontuario) { $query->where('prontuario', request()->prontuario); } }); if (request()->agendamento_id) { $query->where('agendamento_id', request()->agendamento_id); } if (request()->unidade_id) { $query->where('unidade_id', request()->unidade_id); } if (request()->infraestrutura_id) { $query->where('infraestrutura_id', request()->infraestrutura_id); } if (request()->coleta_unidade_id) { $query->where('coleta_unidade_id', request()->coleta_unidade_id); } if (request()->coleta_infraestrutura_id) { $query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id); } if (request()->laboratorio_id) { $query->where('laboratorio_unidade_id', request()->laboratorio_id); } if (request()->laboratorio_infraestrutura_id) { $query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id); } }) ->where(function($query) { if (request()->exame_id) { $query->where('exame_id', request()->exame_id); } }) ->where('entregue', true) ->count(), ]; } public static function getStatus($query) { if ($query->entregue) { return 'ENTREGUE'; } else if ($query->impressao) { return 'PENDENTE DE ENTREGA'; } else if ($query->status_id == StatusAgendamento::ASSINADO) { return 'PENDENTE DE IMPRESSÃO'; } else if ($query->status_id == StatusAgendamento::REALIZADO) { return 'COLETA REALIZADA'; } else if ($query->status_id == StatusAgendamento::NAO_REALIZADO || $query->status_id == StatusAgendamento::FALTOU || $query->status_id == StatusAgendamento::NOVA_COLETA || $query->status_id == StatusAgendamento::OUTROS) { return 'NÃO COLETADO'; } return $query->status->descricao; } }