[], 'quantidade_procedimentos' => 0, ]; $periodo = [ 'start' => Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->startOfDay(), 'end' => Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->endOfDay(), ]; $procedimentos = collect(); DB::table('sigtap.procedimento as procedimento') ->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id') ->join('cadastro_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id') ->join('cadastro_usuario as usuario', 'usuario.id', 'atendimento_procedimento.usuario_id') ->join('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_id') ->join('sigtap.ocupacao as ocupacao', 'ocupacao.id', 'atendimento_procedimento.ocupacao_id') ->leftJoin('cadastro_especialidade as especialidade', 'especialidade.id', 'atendimento_procedimento.especialidade_id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('jornada.data', $periodo) ->when(request()->unidade_id, fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id)) ->when(request()->usuario_id, fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id)) ->when(request()->ocupacao_id, fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id)) ->when(request()->especialidade_id, fn ($q, $id) => $q->where('atendimento_procedimento.especialidade_id', $id)) ->when(request()->procedimento_codigo, fn ($q, $codigo) => $q->where('procedimento.codigo', $codigo)) ->whereNull('lotacao.deleted_at') ->whereNull('infraestrutura.deleted_at') ->whereNull('unidade.deleted_at') ->whereNull('usuario.deleted_at') ->whereNull('usuario_cidadao.deleted_at') ->selectRaw(" atendimento_procedimento.atendimento_id, atendimento_procedimento.pre_atendimento_id, unidade.nome_fantasia as unidade_nome_fantasia, infraestrutura.id as infraestrutura_id, COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome, usuario.id as usuario_id, usuario_cidadao.nome as usuario_nome, ocupacao.codigo as ocupacao_codigo, especialidade.id as especialidade_id, especialidade.nome as especialidade_nome, procedimento.codigo as procedimento_codigo, procedimento.nome as procedimento_nome, atendimento_procedimento.quantidade ") ->unionAll(DB::table('sigtap.procedimento as procedimento') ->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id') ->join('cadastro_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id') ->join('cadastro_usuario as usuario', 'usuario.id', 'atendimento_procedimento.usuario_id') ->join('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_id') ->join('sigtap.ocupacao as ocupacao', 'ocupacao.id', 'atendimento_procedimento.ocupacao_id') ->leftJoin('cadastro_especialidade as especialidade', 'especialidade.id', 'atendimento_procedimento.especialidade_id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('demanda_espontanea.data', $periodo) ->when(request()->unidade_id, fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id)) ->when(request()->usuario_id, fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id)) ->when(request()->ocupacao_id, fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id)) ->when(request()->especialidade_id, fn ($q, $id) => $q->where('atendimento_procedimento.especialidade_id', $id)) ->when(request()->procedimento_codigo, fn ($q, $codigo) => $q->where('procedimento.codigo', $codigo)) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ->whereNull('unidade.deleted_at') ->whereNull('usuario.deleted_at') ->whereNull('usuario_cidadao.deleted_at') ->selectRaw(" atendimento_procedimento.atendimento_id, atendimento_procedimento.pre_atendimento_id, unidade.nome_fantasia as unidade_nome_fantasia, infraestrutura.id as infraestrutura_id, COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome, usuario.id as usuario_id, usuario_cidadao.nome as usuario_nome, ocupacao.codigo as ocupacao_codigo, especialidade.id as especialidade_id, especialidade.nome as especialidade_nome, procedimento.codigo as procedimento_codigo, procedimento.nome as procedimento_nome, atendimento_procedimento.quantidade ") ) ->get() ->each(function ($item) use ($procedimentos, $totalizadores) { $infraestrutura = $procedimentos->where('infraestrutura_id', $item->infraestrutura_id)->first(); if (is_null($infraestrutura)) { $infraestrutura = (object) [ 'infraestrutura_id' => $item->infraestrutura_id, 'infraestrutura_nome' => $item->infraestrutura_nome, 'unidade_nome_fantasia' => $item->unidade_nome_fantasia, 'usuarios' => collect(), 'quantidade_atendimentos' => [], 'quantidade_procedimentos' => 0, ]; $procedimentos->push($infraestrutura); } if (is_null($item->pre_atendimento_id)) { $infraestrutura->quantidade_atendimentos["atendimento_{$item->atendimento_id}"] = 1; } else { $infraestrutura->quantidade_atendimentos["pre_atendimento_{$item->pre_atendimento_id}"] = 1; } $infraestrutura->quantidade_procedimentos += $item->quantidade; $usuario = $infraestrutura->usuarios->where('usuario_id', $item->usuario_id)->first(); if (is_null($usuario)) { $usuario = (object) [ 'usuario_id' => $item->usuario_id, 'usuario_nome' => $item->usuario_nome, 'procedimentos' => collect(), 'quantidade_atendimentos' => [], 'quantidade_procedimentos' => 0, ]; $infraestrutura->usuarios->push($usuario); } if (is_null($item->pre_atendimento_id)) { $usuario->quantidade_atendimentos["atendimento_{$item->atendimento_id}"] = 1; } else { $usuario->quantidade_atendimentos["pre_atendimento_{$item->pre_atendimento_id}"] = 1; } $usuario->quantidade_procedimentos += $item->quantidade; $procedimento = $usuario->procedimentos ->where('especialidade_id', $item->especialidade_id) ->where('procedimento_codigo', $item->procedimento_codigo) ->first(); if (is_null($procedimento)) { $procedimento = (object) [ 'especialidade_id' => $item->especialidade_id, 'especialidade_nome' => $item->especialidade_nome, 'ocupacao_codigo' => $item->ocupacao_codigo, 'procedimento_codigo' => $item->procedimento_codigo, 'procedimento_descricao' => MaskUtil::getProcedimentoMasked($item->procedimento_codigo) . " - {$item->procedimento_nome}", 'quantidade' => 0, ]; $usuario->procedimentos->push($procedimento); } $procedimento->quantidade += $item->quantidade; if (is_null($item->pre_atendimento_id)) { $totalizadores->quantidade_atendimentos["atendimento_{$item->atendimento_id}"] = 1; } else { $totalizadores->quantidade_atendimentos["pre_atendimento_{$item->pre_atendimento_id}"] = 1; } $totalizadores->quantidade_procedimentos += $item->quantidade; }); $procedimentos->each(function ($procedimento) { $procedimento->quantidade_atendimentos = collect($procedimento->quantidade_atendimentos)->sum(); $procedimento->usuarios->each(function ($usuario) { $usuario->quantidade_atendimentos = collect($usuario->quantidade_atendimentos)->sum(); }); }); $totalizadores->quantidade_atendimentos = collect($totalizadores->quantidade_atendimentos)->sum(); return compact('procedimentos', 'totalizadores'); } public static function list() { $data = (object) [ 'additionals' => [], 'form' => [], 'model' => [], ]; $params = self::getParams(); $sets = [ 'periodo' => function () use ($data) { $data->model['periodo'] = ['start' => null, 'end' => null]; }, 'unidade' => function () use ($data, $params) { $unidadesData = self::getUnidadesData($params); $data->form['unidades'] = $unidadesData['form']; $data->model['unidade_id'] = $unidadesData['model']; }, 'infraestrutura' => function () use ($data, $params) { $infraestruturasData = self::getInfraestruturasData($params); $data->form['infraestruturas'] = $infraestruturasData['form']; $data->model['infraestrutura_id'] = $infraestruturasData['model']; }, 'usuario' => function () use ($data, $params) { $usuariosData = self::getUsuariosData($params); $data->form['usuarios'] = $usuariosData['form']; $data->model['usuario_id'] = $usuariosData['model']; }, 'ocupacao' => function () use ($data, $params) { $ocupacoesData = self::getOcupacoesData($params); $data->form['ocupacoes'] = $ocupacoesData['form']; $data->model['ocupacao_id'] = $ocupacoesData['model']; }, 'especialidade' => function () use ($data, $params) { $especialidadesData = self::getEspecialidadesData($params); $data->form['especialidades'] = $especialidadesData['form']; $data->model['especialidade_id'] = $especialidadesData['model']; }, 'procedimento' => function () use ($data, $params) { $procedimentosData = self::getProcedimentosData($params); $data->form['procedimentos'] = $procedimentosData['form']; $data->model['procedimento_codigo'] = $procedimentosData['model']; }, ]; $load = is_null(request()->load_from); if ($load) { $data->additionals = [ 'data_atual' => Carbon::today()->format('d/m/Y') ]; } foreach ($sets as $field => $set) { if ($load) { $set(); } else if ($field === request()->load_from) { $load = true; } } foreach ($data as $property => $itens) { if (empty($itens)) { unset($data->{$property}); } } return $data; } private static function getParams() { $params = request()->only([ 'periodo', 'unidade_id', 'infraestrutura_id', 'ocupacao_id', 'especialidade_id', 'usuario_id', 'procedimento_codigo', ]); if (isset($params['periodo']) && !is_null($params['periodo']['start']) && !is_null($params['periodo']['end']) ) { $start = Carbon::createFromFormat('d/m/Y', $params['periodo']['start'])->startOfDay(); $end = Carbon::createFromFormat('d/m/Y', $params['periodo']['end'])->endOfDay(); $params['periodo']['start'] = $start; $params['periodo']['end'] = $end; } else { $params['periodo'] = null; } return $params; } private static function getUnidadesData($params) { $unidades = collect(); $id = null; if (!is_null($params['periodo'])) { $unidades = DB::table('cadastro_unidade as unidade') ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('cadastro_infraestrutura as infraestrutura') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->whereColumn('lotacao.infraestrutura_id', 'infraestrutura.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('jornada.data', $params['periodo']) ->whereNull('lotacao.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->whereColumn('centro_custo.infraestrutura_id', 'infraestrutura.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('demanda_espontanea.data', $params['periodo']) ->whereNull('centro_custo.deleted_at') ) ) ->whereColumn('infraestrutura.unidade_id', 'unidade.id') ->whereNull('infraestrutura.deleted_at') ) ->whereNull('unidade.deleted_at') ->orderBy('unidade.nome_fantasia') ->selectRaw(" unidade.id, unidade.nome_fantasia ") ->get(); } return ['form' => $unidades, 'model' => $id]; } private static function getInfraestruturasData($params) { $infraestruturas = collect(); $id = null; if (!is_null($params['periodo']) && !is_null($params['unidade_id'])) { $infraestruturas = DB::table('cadastro_infraestrutura as infraestrutura') ->join('cadastro_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id') ->where('infraestrutura.unidade_id', $params['unidade_id']) ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->whereColumn('lotacao.infraestrutura_id', 'infraestrutura.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('jornada.data', $params['periodo']) ->whereNull('lotacao.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->whereColumn('centro_custo.infraestrutura_id', 'infraestrutura.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('demanda_espontanea.data', $params['periodo']) ->whereNull('centro_custo.deleted_at') ) ) ->whereNull('infraestrutura.deleted_at') ->whereNull('unidade.deleted_at') ->orderByRaw("COALESCE(infraestrutura.nome, unidade.nome_fantasia)") ->selectRaw(" infraestrutura.id, COALESCE(infraestrutura.nome, unidade.nome_fantasia) as nome ") ->get(); } return ['form' => $infraestruturas, 'model' => $id]; } private static function getUsuariosData($params) { $usuarios = collect(); $id = null; if (!is_null($params['periodo'])) { $usuarios = DB::table('cadastro_usuario as usuario') ->join('cadastro_cidadao as cidadao', 'cidadao.id', 'usuario.cidadao_id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id') ->whereColumn('atendimento_procedimento.usuario_id', 'usuario.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('jornada.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id)) ->whereNull('lotacao.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id') ->whereColumn('atendimento_procedimento.usuario_id', 'usuario.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('demanda_espontanea.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id)) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ) ->whereNull('usuario.deleted_at') ->whereNull('cidadao.deleted_at') ->orderBy('cidadao.nome') ->selectRaw(" usuario.id, cidadao.nome ") ->get(); } return ['form' => $usuarios, 'model' => $id]; } private static function getOcupacoesData($params) { $ocupacoes = collect(); $id = null; if (!is_null($params['periodo'])) { $ocupacoes = DB::table('sigtap.ocupacao as ocupacao') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id') ->whereColumn('atendimento_procedimento.ocupacao_id', 'ocupacao.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('jornada.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id)) ->when($params['usuario_id'], fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id)) ->whereNull('lotacao.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id') ->whereColumn('atendimento_procedimento.ocupacao_id', 'ocupacao.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('demanda_espontanea.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id)) ->when($params['usuario_id'], fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id)) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ) ->orderByRaw("CONCAT(ocupacao.codigo, ' - ', ocupacao.nome)") ->selectRaw(" ocupacao.id, CONCAT(ocupacao.codigo, ' - ', ocupacao.nome) as descricao ") ->get(); } return ['form' => $ocupacoes, 'model' => $id]; } private static function getEspecialidadesData($params) { $especialidades = collect(); $id = null; if (!is_null($params['periodo']) && !is_null($params['ocupacao_id'])) { $especialidades = DB::table('cadastro_especialidade as especialidade') ->where('especialidade.ocupacao_id', $params['ocupacao_id']) ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id') ->whereColumn('atendimento_procedimento.especialidade_id', 'especialidade.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('jornada.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id)) ->when($params['usuario_id'], fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id)) ->when($params['ocupacao_id'], fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id)) ->whereNull('lotacao.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as atendimento_procedimento') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id') ->whereColumn('atendimento_procedimento.especialidade_id', 'especialidade.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->whereBetween('demanda_espontanea.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id)) ->when($params['usuario_id'], fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id)) ->when($params['ocupacao_id'], fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id)) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ) ->orderBy('especialidade.nome') ->selectRaw(" especialidade.id, especialidade.nome ") ->get(); } return ['form' => $especialidades, 'model' => $id]; } private static function getProcedimentosData($params) { $procedimentos = collect(); $codigo = null; if (!is_null($params['periodo'])) { $procedimentos = DB::table('sigtap.procedimento as procedimento') ->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ->where('soap.is_rascunho', false) ) ->orWhereNotExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atendimento_procedimento.soap_id') ) ) ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_agendamento as agendamento') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id') ->whereColumn('agendamento.id', 'atendimento.agendamento_id') ->whereBetween('jornada.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id)) ->whereNull('lotacao.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_demanda_espontanea as demanda_espontanea') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id') ->whereColumn('demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->whereBetween('demanda_espontanea.data', $params['periodo']) ->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id)) ->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id)) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ) ->when($params['usuario_id'], fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id)) ->when($params['ocupacao_id'], fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id)) ->when($params['especialidade_id'], fn ($q, $id) => $q->where('atendimento_procedimento.especialidade_id', $id)) ->orderByRaw("CONCAT(procedimento.codigo, ' - ', procedimento.nome)") ->selectRaw(" procedimento.codigo, procedimento.nome ") ->distinct() ->get(); $procedimentos->each(function ($procedimento) { $procedimento->descricao = MaskUtil::getProcedimentoMasked($procedimento->codigo) . " - {$procedimento->nome}"; unset($procedimento->nome); }); } return ['form' => $procedimentos, 'model' => $codigo]; } }