0]; $consulta = DB::table('exame_laboratorial_agendamento_exame_bloqueado') ->select([ DB::raw("CONCAT(DATE_FORMAT(exame_laboratorial_agendamento_exame_bloqueado.created_at, '%d/%m/%Y'), ' ', DATE_FORMAT(exame_laboratorial_agendamento_exame_bloqueado.created_at, '%H:%i')) as data"), 'cadastro_cidadao.nome', 'cadastro_unidade.nome_fantasia as unidade', 'cidadao_profissional.nome as profissional', 'parametrizacao_exame.nome as exame', 'exame_laboratorial_agendamento_exame_bloqueado.valor', 'exame_laboratorial_agendamento_exame_bloqueado.supervisor_id', 'exame_laboratorial_agendamento_exame_bloqueado_motivo.descricao as motivo', DB::raw("CASE WHEN cidadao_supervisor.id IS NOT NULL THEN 'LIBERADO' ELSE 'BLOQUEADO' END as status"), DB::raw("CASE WHEN cidadao_supervisor.id IS NOT NULL THEN cidadao_supervisor.nome ELSE 'NÃO INFORMADO' END as supervisor") ]) ->join( 'parametrizacao_exame', 'exame_laboratorial_agendamento_exame_bloqueado.exame_id', 'parametrizacao_exame.id' ) ->join( 'cadastro_paciente', 'exame_laboratorial_agendamento_exame_bloqueado.paciente_id', 'cadastro_paciente.id' ) ->join( 'cadastro_cidadao', 'cadastro_paciente.cidadao_id', 'cadastro_cidadao.id' ) ->join( 'cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento_exame_bloqueado.created_lotacao_id' ) ->join( 'exame_laboratorial_agendamento_exame_bloqueado_motivo', 'exame_laboratorial_agendamento_exame_bloqueado_motivo.id', 'exame_laboratorial_agendamento_exame_bloqueado.motivo_id' ) ->join( 'cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id' ) ->join( 'cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id' ) ->join( 'cadastro_usuario as cadastro_profissional', 'cadastro_lotacao.usuario_id', 'cadastro_profissional.id' ) ->join( 'cadastro_cidadao as cidadao_profissional', 'cadastro_profissional.cidadao_id', 'cidadao_profissional.id' ) ->leftJoin( 'cadastro_usuario as usuario_supervisor', 'exame_laboratorial_agendamento_exame_bloqueado.supervisor_id', 'usuario_supervisor.id', ) ->leftJoin( 'cadastro_cidadao as cidadao_supervisor', 'usuario_supervisor.cidadao_id', 'cidadao_supervisor.id' ) ->when(request()->periodo['start'], function ($query) { $data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d'); $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '>=', $data); }) ->when(request()->periodo['end'], function ($query) { $data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d'); $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '<=', $data); }) ->when(request()->unidade_id, function ($query) { $query->where('cadastro_unidade.id', request()->unidade_id); }) ->when(request()->exame_id, function ($query) { $query->where('exame_laboratorial_agendamento_exame_bloqueado.exame_id', request()->exame_id); }) ->when(request()->profissional_id, function ($query) { $query->where('cidadao_profissional.id', request()->profissional_id); }) ->when(request()->supervisor_id, function ($query) { $query->where('cidadao_supervisor.id', request()->supervisor_id); }) ->when(request()->status, function ($query) { if (request()->status == 'bloqueado') { $query->whereNull('exame_laboratorial_agendamento_exame_bloqueado.supervisor_id'); } else { $query->whereNotNull('exame_laboratorial_agendamento_exame_bloqueado.supervisor_id'); } }) ->orderBy('exame_laboratorial_agendamento_exame_bloqueado.created_at', 'desc') ->get(); $totalizadores = [ 'quantidade_bloqueado' => $consulta->whereNull('supervisor_id')->count('id'), 'quantidade_liberado' => $consulta->whereNotNull('supervisor_id')->count('id'), 'quantidade_total' => $consulta->count('id'), 'total_bloqueado' => number_format($consulta->whereNull('supervisor_id')->sum('valor'), 2, ',', '.'), 'total_liberado' => number_format($consulta->whereNotNull('supervisor_id')->sum('valor'), 2, ',', '.'), 'total' => number_format($consulta->sum('valor'), 2, ',', '.'), ]; $consulta = $consulta->map(function ($item) { $item->valor = number_format($item->valor, 2, ',', '.'); return $item; }); return ['consulta' => $consulta, 'totalizadores' => $totalizadores]; } public static function list() { return [ 'additionals' => [], 'form' => [ 'data_atual' => Carbon::now()->format('d/m/Y'), 'unidades' => self::getUnidades(), 'exames' => self::getExames(), 'profissionais' => self::getProfissionais(), 'supervisores' => self::getSupervisores(), 'status' => [ ['value' => 'bloqueado', 'text' => 'BLOQUEADO'], ['value' => 'liberado', 'text' => 'LIBERADO'], ], ], 'model' => [ 'periodo' => [ 'start' => null, 'end' => null, ], 'unidade_id' => null, 'exame_id' => null, 'supervisor_id' => null, 'profissional_id' => null, 'status' => null, ], ]; } public static function getUnidades() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $unidades = DB::table('exame_laboratorial_agendamento_exame_bloqueado') ->select([ 'cadastro_unidade.id', DB::raw(' CASE WHEN cadastro_infraestrutura.principal THEN cadastro_unidade.nome_fantasia ELSE cadastro_infraestrutura.nome END AS infraestrutura_nome'), ]) ->join( 'cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento_exame_bloqueado.created_lotacao_id' ) ->join( 'cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id' ) ->join( 'cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id' ) ->where(function($query) use ($currentDate) { if (request()->periodo) { $data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d'); $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '>=', $data); $data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d'); $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '<=', $data); } else { $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', $currentDate); } }) ->groupBy([ 'cadastro_infraestrutura.unidade_id', ]); return $unidades->get(); } public static function getPrintExportData() { $resultado = self::index(); $itens = $resultado['consulta']; $unidade = Unidade::find(request()->unidade_id); $exame = Exame::find(request()->exame_id); $profissional = Cidadao::find(request()->profissional_id); $supervisor = Cidadao::find(request()->supervisor_id); return [ 'inicio' => request()->periodo['start'], 'fim' => request()->periodo['end'], 'unidade' => $unidade->nome_fantasia ?? 'Todas', 'exame' => $exame->nome ?? 'Todos', 'profissional' => $profissional->nome ?? 'Todos', 'supervisor' => $supervisor->nome ?? 'Todos', 'status' => request()->status ? strtoupper(request()->status) : 'Todos', 'colunas' => collect(request()->selectedCols), 'itens' => $itens, 'totalizadores' => $resultado['totalizadores'], ]; } public static function getExames() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); return DB::table('exame_laboratorial_agendamento_exame_bloqueado') ->select([ 'parametrizacao_exame.id AS exame_id', 'parametrizacao_exame.nome AS exame_nome', ]) ->join( 'cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento_exame_bloqueado.created_lotacao_id' ) ->join( 'cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id' ) ->join( 'cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id' ) ->join( 'parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame_bloqueado.exame_id' ) ->when(request()->unidade_id, function ($query) { $query->where('cadastro_unidade.id', request()->unidade_id); }) ->where(function($query) use ($currentDate) { if (request()->periodo) { $data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d'); $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '>=', $data); $data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d'); $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '<=', $data); } else { $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', $currentDate); } }) ->groupBy('parametrizacao_exame.id') ->orderBy('parametrizacao_exame.nome', 'ASC') ->get(); } public static function getProfissionais() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); return DB::table('exame_laboratorial_agendamento_exame_bloqueado') ->select([ 'cidadao_profissional.id AS profissional_id', 'cidadao_profissional.nome AS profissional_nome', ]) ->join( 'cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento_exame_bloqueado.created_lotacao_id' ) ->join( 'cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id' ) ->join( 'cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id' ) ->join( 'cadastro_usuario as cadastro_profissional', 'cadastro_lotacao.usuario_id', 'cadastro_profissional.id' ) ->join( 'cadastro_cidadao as cidadao_profissional', 'cadastro_profissional.cidadao_id', 'cidadao_profissional.id' ) ->when(request()->unidade_id, function ($query) { $query->where('cadastro_unidade.id', request()->unidade_id); }) ->when(request()->exame_id, function ($query) { $query->where('exame_laboratorial_agendamento_exame_bloqueado.exame_id', request()->exame_id); }) ->where(function($query) use ($currentDate) { if (request()->periodo) { $data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d'); $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '>=', $data); $data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d'); $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '<=', $data); } else { $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', $currentDate); } }) ->groupBy('cidadao_profissional.id') ->orderBy('cidadao_profissional.nome', 'ASC') ->get(); } public static function getSupervisores() { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); return DB::table('exame_laboratorial_agendamento_exame_bloqueado') ->select([ 'cidadao_supervisor.id AS supervisor_id', 'cidadao_supervisor.nome AS supervisor_nome', ]) ->join( 'cadastro_lotacao', 'cadastro_lotacao.id', 'exame_laboratorial_agendamento_exame_bloqueado.created_lotacao_id' ) ->join( 'cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id' ) ->join( 'cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id' ) ->join( 'cadastro_usuario as cadastro_profissional', 'cadastro_lotacao.usuario_id', 'cadastro_profissional.id' ) ->join( 'cadastro_cidadao as cidadao_profissional', 'cadastro_profissional.cidadao_id', 'cidadao_profissional.id' ) ->join( 'cadastro_usuario as usuario_supervisor', 'exame_laboratorial_agendamento_exame_bloqueado.supervisor_id', 'usuario_supervisor.id', ) ->join( 'cadastro_cidadao as cidadao_supervisor', 'usuario_supervisor.cidadao_id', 'cidadao_supervisor.id' ) ->when(request()->unidade_id, function ($query) { $query->where('cadastro_unidade.id', request()->unidade_id); }) ->when(request()->exame_id, function ($query) { $query->where('exame_laboratorial_agendamento_exame_bloqueado.exame_id', request()->exame_id); }) ->when(request()->profissional_id, function ($query) { $query->where('cidadao_profissional.id', request()->profissional_id); }) ->where(function($query) use ($currentDate) { if (request()->periodo) { $data = Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->format('Y-m-d'); $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '>=', $data); $data = Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->format('Y-m-d'); $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', '<=', $data); } else { $query->whereDate('exame_laboratorial_agendamento_exame_bloqueado.created_at', $currentDate); } }) ->groupBy('cidadao_profissional.id') ->orderBy('cidadao_profissional.nome', 'ASC') ->get(); } }