0]; $date = request()->query('periodo'); if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $date['start'])) { $periodo['start'] = Carbon::createFromFormat('d/m/Y', $date['start'])->format('Y-m-d'); $periodo['end'] = Carbon::createFromFormat('d/m/Y', $date['end'])->format('Y-m-d'); } else { $periodo['start'] = $date['start']; $periodo['end'] = $date['end']; } $consulta = DB::table('exame_laboratorial_agendamento') ->join( 'exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id' ) ->join( 'cadastro_unidade', 'exame_laboratorial_agendamento.unidade_id', 'cadastro_unidade.id' ) ->join( 'cadastro_paciente', 'exame_laboratorial_agendamento_exame.paciente_id', 'cadastro_paciente.id' ) ->join( 'cadastro_cidadao', 'cadastro_paciente.cidadao_id', 'cadastro_cidadao.id' ) ->join( 'cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id' ) ->join( 'cadastro_cidadao as cadastro_cidadao_1', 'cadastro_cidadao_1.id', 'cadastro_usuario.cidadao_id' ) ->join( 'exame_laboratorial_agendamento_exame_status', 'exame_laboratorial_agendamento_exame.status_id', 'exame_laboratorial_agendamento_exame_status.id' ) ->join( 'cadastro_infraestrutura', 'exame_laboratorial_agendamento.coleta_infraestrutura_id', 'cadastro_infraestrutura.id' ) ->join( 'cadastro_unidade as cadastro_unidade_1', 'cadastro_infraestrutura.unidade_id', 'cadastro_unidade_1.id' ) ->whereIn( 'exame_laboratorial_agendamento_exame.status_id', [ StatusAgendamento::REALIZADO, StatusAgendamento::PENDENTE_ASSINATURA, StatusAgendamento::ASSINADO, StatusAgendamento::PENDENTE_ENTREGA, StatusAgendamento::ENTREGUE, ] ) ->when(request()->query('unidade_id'), fn ($q, $id) => $q->where('exame_laboratorial_agendamento.unidade_id', $id)) ->when(request()->query('infraestrutura_id'), fn ($q, $id) => $q->where('exame_laboratorial_agendamento.infraestrutura_id', $id)) ->when(request()->query('medico_id'), fn ($q, $id) => $q->where('exame_laboratorial_agendamento.medico_id', $id)) ->select( 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento.data', DB::raw('cadastro_cidadao.nome AS paciente'), DB::raw('cadastro_cidadao_1.nome AS medico'), DB::raw('cadastro_unidade.nome_fantasia AS unidade'), DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta,"%d/%m/%Y") AS data_coleta'), DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data,"%d/%m/%Y") AS data_pedido'), DB::raw("IFNULL(cadastro_infraestrutura.nome, cadastro_unidade_1.nome_fantasia) AS local") ); if(isset($periodo)){ $consulta = $consulta->whereBetween('exame_laboratorial_agendamento.data', [$periodo['start'], $periodo['end']]); } $consulta = $consulta->groupBy('exame_laboratorial_agendamento.id') ->orderBy('exame_laboratorial_agendamento.id') ->get(); $totalizadores = [ 'total' => $consulta->count(), ]; return ['consulta' => $consulta, 'totalizadores' => $totalizadores]; } public static function list() { $unidades = self::getUnidades(); return [ 'additionals' => [], 'form' => [ 'data_atual' => Carbon::now()->format('d/m/Y'), 'periodo' => [ 'start' => Carbon::now()->startOfMonth()->format('Y-m-d'), 'end' => Carbon::now()->endOfMonth()->format('Y-m-d'), ], 'unidades' => Unidade::orderBy('nome_fantasia') ->whereIn('id', $unidades->pluck('unidade_id')) ->get() ->map(fn ($query) => [ 'id' => $query->id, 'nome_fantasia' => $query->nome_fantasia, 'infraestruturas' => $query->infraestruturas->sortBy('nome')->map(fn ($infraestrutura) => [ 'id' => $infraestrutura->id, 'nome' => $infraestrutura->nome, 'principal' => $infraestrutura->principal, ])->values() ]), 'medicos' => self::getMedicos()->map(fn ($medicos) => [ 'medico_id' => $medicos->laboratorio_unidade_id, 'medico_nome' => $medicos->usuario, ])->values(), ], 'model' => [ 'periodo' => [ 'start' => null, 'end' => null, ], 'unidade_id' => null, 'infraestrutura_id' => null, 'medico_id' => null, ], ]; } public static function loadFields() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $medicos = self::getMedicos(); $consulta = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', 'parametrizacao_exame.id AS exame_id', 'parametrizacao_exame.nome AS exame_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( 'parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id' ) ->groupBy([ 'exame_laboratorial_agendamento_exame.exame_id', ]); if (request()->periodo) { $consulta->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']); $consulta->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']); } else { $consulta->where('exame_laboratorial_agendamento.data', $currentDate); } if (request()->unidade_id) { $consulta->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id); } if (request()->medico_id) { $consulta->where('exame_laboratorial_agendamento.medico_id', request()->medico_id); } $consulta = $consulta->get(); return [ 'form' => [ 'periodo' => [ 'start' => $currentDate, 'end' => $currentDate, ], 'data_atual' => Carbon::now()->format('d/m/Y'), ], 'model' => [ 'unidades' => self::getUnidades()->map(fn ($unidade) => [ 'unidade_id' => $unidade->unidade_id, 'unidade_nome' => $unidade->unidade, ])->values(), 'medicos' => self::getMedicos()->map(fn ($medicos) => [ 'medico_id' => $medicos->laboratorio_unidade_id, 'medico_nome' => $medicos->nome, ])->values(), ], ]; } private static function getUnidades() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $unidade = DB::table('exame_laboratorial_agendamento') ->select([ 'exame_laboratorial_agendamento.unidade_id', 'cadastro_unidade.nome_fantasia AS unidade', ]) ->join( 'exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id' ) ->join( 'cadastro_unidade', 'cadastro_unidade.id', 'exame_laboratorial_agendamento.unidade_id' ) ->where(function($query) use ($currentDate) { if (request()->periodo) { $data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d'); $query->where('exame_laboratorial_agendamento.data', '>=', $data); $data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d'); $query->where('exame_laboratorial_agendamento.data', '<=', $data); } else { $query->where('exame_laboratorial_agendamento.data', $currentDate); } }) ->groupBy([ 'exame_laboratorial_agendamento.unidade_id', ]); return $unidade->get(); } private static function getMedicos() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $medico = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.medico_id AS laboratorio_unidade_id', 'cadastro_cidadao.nome AS usuario', ]) ->join( 'exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id' ) ->join( 'cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id' ) ->join( 'cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id' ) ->when(request()->unidade_id, function ($query) { $query->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id); }) ->when(request()->infraestrutura_id, function ($query) { $query->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id); }) ->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO) ->where(function($query) use ($currentDate) { if (request()->periodo) { $data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d'); $query->where('exame_laboratorial_agendamento.data', '>=', $data); $data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d'); $query->where('exame_laboratorial_agendamento.data', '<=', $data); } else { $query->where('exame_laboratorial_agendamento.data', $currentDate); } }) ->groupBy([ 'exame_laboratorial_agendamento.medico_id', ]) ->orderBy('cadastro_cidadao.nome', 'asc'); return $medico->get(); } public static function getPrintExportData() { $resultado = self::index(); $itens = $resultado['consulta']; $unidade = Unidade::find(request()->unidade_id); $medico = Usuario::find(request()->medico_id); $infraestrutura = Infraestrutura::select([ DB::raw('IFNULL(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS local') ]) ->join( 'cadastro_unidade', 'cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id' ) ->where('cadastro_infraestrutura.id', request()->infraestrutura_id) ->first(); return [ 'inicio' => request()->periodo['start'], 'fim' => request()->periodo['end'], 'unidade' => $unidade->nome_fantasia ?? 'Todas', 'medico' => $medico->nome ?? 'Todos', 'local' => $infraestrutura->local ?? 'Todos', 'colunas' => collect(request()->selectedCols), 'itens' => $itens, 'totalizadores' => $resultado['totalizadores'], ]; } }