Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->startOfDay(), 'end' => Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->endOfDay(), ]; $procedimentoAbpgId = request()->procedimento_abpg_id; $filtraOrientacaoIndividual = $procedimentoAbpgId === 'orientacao_individual'; $filtraAbpgRegular = !is_null($procedimentoAbpgId) && !$filtraOrientacaoIndividual; $demandaTipo = request()->demanda_tipo; $somenteAgendada = $demandaTipo === 'agendada'; $somenteEspontanea = $demandaTipo === 'espontanea'; $procedimentosAgenda = 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('cadastro_paciente as paciente', 'paciente.id', 'agendamento.paciente_id') ->join('cadastro_cidadao as paciente_cidadao', 'paciente_cidadao.id', 'paciente.cidadao_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('sigtap.ocupacao as ocupacao', 'ocupacao.id', 'atendimento_procedimento.ocupacao_id') ->join('cadastro_usuario as usuario', 'usuario.id', 'atendimento_procedimento.usuario_id') ->join('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_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($somenteEspontanea, fn ($q) => $q->whereRaw('1 = 0')) ->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()->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()->usuario_id, fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id)) ->when(request()->procedimento_codigo, fn ($q, $codigo) => $q->where('procedimento.codigo', $codigo)) ->whereNull('paciente.deleted_at') ->whereNull('paciente_cidadao.deleted_at') ->whereNull('lotacao.deleted_at') ->whereNull('infraestrutura.deleted_at') ->whereNull('unidade.deleted_at') ->whereNull('usuario.deleted_at') ->whereNull('usuario_cidadao.deleted_at') ->selectRaw(" jornada.data, paciente_cidadao.nome as paciente_nome, paciente_cidadao.data_nascimento as paciente_data_nascimento, LPAD(paciente.prontuario, 6, '0') as paciente_prontuario, unidade.nome_fantasia as unidade_nome_fantasia, COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome, ocupacao.codigo as ocupacao_codigo, ocupacao.nome as ocupacao_nome, especialidade.nome as especialidade_nome, usuario_cidadao.nome as usuario_nome, procedimento.codigo as procedimento_codigo, procedimento.nome as procedimento_nome, 'SIGTAP' as procedimento_tipo "); $procedimentosDemanda = 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_paciente as paciente', 'paciente.id', 'demanda_espontanea.paciente_id') ->join('cadastro_cidadao as paciente_cidadao', 'paciente_cidadao.id', 'paciente.cidadao_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('sigtap.ocupacao as ocupacao', 'ocupacao.id', 'atendimento_procedimento.ocupacao_id') ->join('cadastro_usuario as usuario', 'usuario.id', 'atendimento_procedimento.usuario_id') ->join('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_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($somenteAgendada, fn ($q) => $q->whereRaw('1 = 0')) ->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()->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()->usuario_id, fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id)) ->when(request()->procedimento_codigo, fn ($q, $codigo) => $q->where('procedimento.codigo', $codigo)) ->whereNull('paciente.deleted_at') ->whereNull('paciente_cidadao.deleted_at') ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ->whereNull('unidade.deleted_at') ->whereNull('usuario.deleted_at') ->whereNull('usuario_cidadao.deleted_at') ->selectRaw(" demanda_espontanea.data, paciente_cidadao.nome as paciente_nome, paciente_cidadao.data_nascimento as paciente_data_nascimento, LPAD(paciente.prontuario, 6, '0') as paciente_prontuario, unidade.nome_fantasia as unidade_nome_fantasia, COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome, ocupacao.codigo as ocupacao_codigo, ocupacao.nome as ocupacao_nome, especialidade.nome as especialidade_nome, usuario_cidadao.nome as usuario_nome, procedimento.codigo as procedimento_codigo, procedimento.nome as procedimento_nome, 'SIGTAP' as procedimento_tipo "); $testeRapidoAgenda = DB::table('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('cadastro_paciente as paciente', 'paciente.id', 'agendamento.paciente_id') ->join('cadastro_cidadao as paciente_cidadao', 'paciente_cidadao.id', 'paciente.cidadao_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') ->where('tr.is_abpg', true) ->where('soap.is_rascunho', false) ->when($filtraOrientacaoIndividual, fn ($q) => $q->whereRaw('1 = 0')) ->when($filtraAbpgRegular, fn ($q) => $q->where('tr.id', (int) $procedimentoAbpgId)) ->when($somenteEspontanea, fn ($q) => $q->whereRaw('1 = 0')) ->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()->ocupacao_id, fn ($q, $id) => $q->where('lotacao.ocupacao_id', $id)) ->when(request()->especialidade_id, fn ($q, $id) => $q->where('jornada.especialidade_id', $id)) ->when(request()->usuario_id, fn ($q, $id) => $q->where('lotacao.usuario_id', $id)) ->whereBetween('jornada.data', $periodo) ->selectRaw(" jornada.data, paciente_cidadao.nome as paciente_nome, paciente_cidadao.data_nascimento as paciente_data_nascimento, LPAD(paciente.prontuario, 6, '0') as paciente_prontuario, unidade.nome_fantasia as unidade_nome_fantasia, COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome, NULL as ocupacao_codigo, NULL as ocupacao_nome, NULL as especialidade_nome, NULL as usuario_nome, tr.codigo_ab as procedimento_codigo, tr.nome as procedimento_nome, 'ABPG' as procedimento_tipo "); $testeRapidoDemanda = DB::table('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_paciente as paciente', 'paciente.id', 'demanda_espontanea.paciente_id') ->join('cadastro_cidadao as paciente_cidadao', 'paciente_cidadao.id', 'paciente.cidadao_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_lotacao as lotacao', 'lotacao.id', 'atr.created_lotacao_id') ->where('tr.is_abpg', true) ->where('soap.is_rascunho', false) ->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()->ocupacao_id, fn ($q, $id) => $q->where('lotacao.ocupacao_id', $id)) ->when(request()->especialidade_id, fn ($q, $id) => $q->where('demanda_espontanea.especialidade_id', $id)) ->when(request()->usuario_id, fn ($q, $id) => $q->where('demanda_espontanea.usuario_id', $id)) ->when($filtraOrientacaoIndividual, fn ($q) => $q->whereRaw('1 = 0')) ->when($filtraAbpgRegular, fn ($q) => $q->where('tr.id', (int) $procedimentoAbpgId)) ->when($somenteAgendada, fn ($q) => $q->whereRaw('1 = 0')) ->whereBetween('demanda_espontanea.data', $periodo) ->selectRaw(" demanda_espontanea.data, paciente_cidadao.nome as paciente_nome, paciente_cidadao.data_nascimento as paciente_data_nascimento, LPAD(paciente.prontuario, 6, '0') as paciente_prontuario, unidade.nome_fantasia as unidade_nome_fantasia, COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome, NULL as ocupacao_codigo, NULL as ocupacao_nome, NULL as especialidade_nome, NULL as usuario_nome, tr.codigo_ab as procedimento_codigo, tr.nome as procedimento_nome, 'ABPG' as procedimento_tipo "); $agendaOrientacaoIndividual = DB::table('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.atendimento_id') ->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id') ->join('cadastro_paciente as paciente', 'paciente.id', 'agendamento.paciente_id') ->join('cadastro_cidadao as paciente_cidadao', 'paciente_cidadao.id', 'paciente.cidadao_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') ->whereNull('tr.codigo') ->where('soap.is_rascunho', false) ->where('soap.orientacao_individual_em_saude', true) ->when($filtraAbpgRegular, fn ($q) => $q->whereRaw('1 = 0')) ->when($somenteEspontanea, fn ($q) => $q->whereRaw('1 = 0')) ->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()->ocupacao_id, fn ($q, $id) => $q->where('lotacao.ocupacao_id', $id)) ->when(request()->especialidade_id, fn ($q, $id) => $q->where('jornada.especialidade_id', $id)) ->when(request()->usuario_id, fn ($q, $id) => $q->where('lotacao.usuario_id', $id)) ->whereBetween('jornada.data', $periodo) ->selectRaw(" jornada.data, paciente_cidadao.nome as paciente_nome, paciente_cidadao.data_nascimento as paciente_data_nascimento, LPAD(paciente.prontuario, 6, '0') as paciente_prontuario, unidade.nome_fantasia as unidade_nome_fantasia, COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome, NULL as ocupacao_codigo, NULL as ocupacao_nome, NULL as especialidade_nome, NULL as usuario_nome, 'ABPG042' as procedimento_codigo, tr.nome as procedimento_nome, 'ABPG' as procedimento_tipo "); $demandaOrientacaoIndividual = DB::table('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_paciente as paciente', 'paciente.id', 'demanda_espontanea.paciente_id') ->join('cadastro_cidadao as paciente_cidadao', 'paciente_cidadao.id', 'paciente.cidadao_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_lotacao as lotacao', 'lotacao.id', 'atr.created_lotacao_id') ->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()->ocupacao_id, fn ($q, $id) => $q->where('lotacao.ocupacao_id', $id)) ->when(request()->especialidade_id, fn ($q, $id) => $q->where('demanda_espontanea.especialidade_id', $id)) ->when(request()->usuario_id, fn ($q, $id) => $q->where('demanda_espontanea.usuario_id', $id)) ->whereNull('tr.codigo') ->where('soap.is_rascunho', false) ->where('soap.orientacao_individual_em_saude', true) ->when($filtraAbpgRegular, fn ($q) => $q->whereRaw('1 = 0')) ->when($somenteAgendada, fn ($q) => $q->whereRaw('1 = 0')) ->whereBetween('demanda_espontanea.data', $periodo) ->selectRaw(" demanda_espontanea.data, paciente_cidadao.nome as paciente_nome, paciente_cidadao.data_nascimento as paciente_data_nascimento, LPAD(paciente.prontuario, 6, '0') as paciente_prontuario, unidade.nome_fantasia as unidade_nome_fantasia, COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome, NULL as ocupacao_codigo, NULL as ocupacao_nome, NULL as especialidade_nome, NULL as usuario_nome, 'ABPG042' as procedimento_codigo, tr.nome as procedimento_nome, 'ABPG' as procedimento_tipo "); $procedimentos = $procedimentosAgenda ->unionAll($procedimentosDemanda) ->unionAll($testeRapidoAgenda) ->unionAll($testeRapidoDemanda) ->unionAll($agendaOrientacaoIndividual) ->unionAll($demandaOrientacaoIndividual) ->get(); $procedimentos->each(function ($procedimento) { $procedimento->paciente_idade = is_null($procedimento->paciente_data_nascimento) ? null : AgeService::getFormattedAge((new DateTime($procedimento->paciente_data_nascimento))); $procedimento->procedimento_codigo = MaskUtil::getProcedimentoMasked($procedimento->procedimento_codigo); }); $agrupado = $procedimentos->groupBy(function ($item){ return $item->paciente_prontuario . '|' . $item->data; }); $resultado = $agrupado->map(function ($items) { $first = $items->first(); return (object) [ 'data' => $first->data, 'paciente_nome' => $first->paciente_nome, 'paciente_data_nascimento' => $first->paciente_data_nascimento, 'paciente_prontuario' => $first->paciente_prontuario, 'paciente_idade' => $first->paciente_idade, 'unidade_nome_fantasia' => $first->unidade_nome_fantasia, 'infraestrutura_nome' => $first->infraestrutura_nome, 'ocupacao_codigo' => $first->ocupacao_codigo, 'ocupacao_nome' => $first->ocupacao_nome, 'especialidade_nome' => $first->especialidade_nome, 'usuario_nome' => $first->usuario_nome, 'procedimentos_sigtap' => $items ->where('procedimento_tipo', 'SIGTAP') ->map(fn ($i) => $i->procedimento_codigo) ->filter() ->values(), 'procedimentos_sigtap_descricao' => $items ->where('procedimento_tipo', 'SIGTAP') ->map(fn ($i) => $i->procedimento_nome) ->filter() ->values(), 'procedimentos_abpg' => $items ->where('procedimento_tipo', 'ABPG') ->map(fn ($i) => $i->procedimento_codigo) ->filter() ->values(), 'procedimentos_abpg_descricao' => $items ->where('procedimento_tipo', 'ABPG') ->map(fn ($i) => $i->procedimento_nome) ->filter() ->values(), ]; })->values(); $procedimentos = $resultado; return compact('procedimentos'); } public static function list() { $data = (object) [ 'additionals' => [], 'form' => [], 'model' => [], ]; $params = self::getParams(); $sets = [ 'periodo' => function () use ($data) { $data->model['periodo'] = ['start' => null, 'end' => null]; }, 'demanda_tipo' => function () use ($data, $params) { $demandaTipoData = self::getDemandaTipos($params); $data->form['tipos_demanda'] = $demandaTipoData['form']; $data->model['demanda_tipo'] = $demandaTipoData['model']; }, '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']; }, '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']; }, 'usuario' => function () use ($data, $params) { $usuariosData = self::getUsuariosData($params); $data->form['usuarios'] = $usuariosData['form']; $data->model['usuario_id'] = $usuariosData['model']; }, 'procedimento' => function () use ($data, $params) { $procedimentosData = self::getProcedimentosSigtapData($params); $data->form['procedimentos'] = $procedimentosData['form']; $data->model['procedimento_codigo'] = $procedimentosData['model']; }, 'procedimento_abpg' => function () use ($data, $params) { $procedimentosAbpgData = self::getProcedimentosAbpgData($params); $data->form['procedimentos_abpg'] = $procedimentosAbpgData['form']; $data->model['procedimento_abpg_id'] = $procedimentosAbpgData['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', 'procedimento_abpg_id', 'demanda_tipo', ]); 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'])) { $demandaTipo = $params['demanda_tipo'] ?? null; $comAgendada = is_null($demandaTipo) || $demandaTipo === 'agendada'; $comEspontanea = is_null($demandaTipo) || $demandaTipo === 'espontanea'; $unidades = DB::table('cadastro_unidade as unidade') ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('cadastro_infraestrutura as infraestrutura') ->where(fn ($q) => $q ->when($comAgendada, 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_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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') ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atr.soap_id') ->where('soap.is_rascunho', false) ) ->whereBetween('jornada.data', $params['periodo']) ->whereNull('lotacao.deleted_at') ) ) ->when($comEspontanea, fn ($q) => $q ->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') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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') ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atr.soap_id') ->where('soap.is_rascunho', false) ) ->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') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from ('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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', 'atr.soap_id') ->where('soap.is_rascunho', false) ) ) ->whereBetween('demanda_espontanea.data', $params['periodo']) ->whereNull('centro_custo.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from ('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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', 'atr.soap_id') ->where('soap.is_rascunho', false) ) ) ->whereBetween('jornada.data', $params['periodo']) ->whereNull('lotacao.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 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)) ->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)) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from ('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.atendimento_id') ->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_lotacao as lotacao', 'lotacao.id', 'atr.created_lotacao_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id') ->whereColumn('lotacao.ocupacao_id', 'ocupacao.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atr.soap_id') ->where('soap.is_rascunho', false) ) ) ->whereBetween('demanda_espontanea.data', $params['periodo']) ->whereNull('centro_custo.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from ('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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.ocupacao_id', 'ocupacao.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atr.soap_id') ->where('soap.is_rascunho', false) ) ) ->whereBetween('jornada.data', $params['periodo']) ->whereNull('lotacao.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)) ->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)) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from ('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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('demanda_espontanea.especialidade_id', 'especialidade.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atr.soap_id') ->where('soap.is_rascunho', false) ) ) ->whereBetween('demanda_espontanea.data', $params['periodo']) ->whereNull('centro_custo.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from ('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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('jornada.especialidade_id', 'especialidade.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atr.soap_id') ->where('soap.is_rascunho', false) ) ) ->whereBetween('jornada.data', $params['periodo']) ->whereNull('lotacao.deleted_at') ) ) ->orderBy('especialidade.nome') ->selectRaw(" especialidade.id, especialidade.nome ") ->get(); } return ['form' => $especialidades, '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)) ->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)) ->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)) ->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)) ->whereNull('centro_custo.deleted_at') ->whereNull('infraestrutura.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from ('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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('demanda_espontanea.usuario_id', 'usuario.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atr.soap_id') ->where('soap.is_rascunho', false) ) ) ->whereBetween('demanda_espontanea.data', $params['periodo']) ->whereNull('centro_custo.deleted_at') ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from ('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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.usuario_id', 'usuario.id') ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->whereColumn('soap.id', 'atr.soap_id') ->where('soap.is_rascunho', false) ) ) ->whereBetween('jornada.data', $params['periodo']) ->whereNull('lotacao.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 getProcedimentosSigtapData($params) { $procedimentos = collect(); $codigo = null; if (!is_null($params['periodo'])) { $agendamentosQuery = 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') ->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['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)) ->when($params['usuario_id'], fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id)) ->whereNull('lotacao.deleted_at') ->whereNull('infraestrutura.deleted_at') ->orderByRaw("CONCAT(procedimento.codigo, ' - ', procedimento.nome)") ->selectRaw(" procedimento.codigo, procedimento.nome ") ->distinct(); $demandaQuery = 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', 'demanda.id', 'atendimento.demanda_espontanea_id') ->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda.created_centro_custo_id') ->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.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.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('infraestrutura.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)) ->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(procedimento.codigo, ' - ', procedimento.nome)") ->selectRaw(" procedimento.codigo, procedimento.nome ") ->distinct(); $procedimentos = $agendamentosQuery ->union($demandaQuery) ->orderByRaw("CONCAT(codigo, ' - ', nome)") ->get(); $procedimentos->each(function ($procedimento) { $procedimento->descricao = MaskUtil::getProcedimentoMasked($procedimento->codigo) . " - {$procedimento->nome}"; unset($procedimento->nome); }); } return ['form' => $procedimentos, 'model' => $codigo]; } private static function getProcedimentosAbpgData($params) { $procedimentos = collect(); $id = null; if (!is_null($params['periodo'])) { $agendaQuery = DB::table('ambulatorio_teste_rapido as tr') ->join('ambulatorio_atendimento_soap_teste_rapido as atr', 'atr.teste_rapido_id', 'tr.id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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') ->where('tr.is_abpg', true) ->where('soap.is_rascunho', false) ->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') ->select('tr.id', 'tr.nome') ->distinct(); $demandaQuery = DB::table('ambulatorio_teste_rapido as tr') ->join('ambulatorio_atendimento_soap_teste_rapido as atr', 'atr.teste_rapido_id', 'tr.id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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') ->where('tr.is_abpg', true) ->where('soap.is_rascunho', false) ->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') ->select('tr.id', 'tr.nome') ->distinct(); $procedimentos = $agendaQuery ->union($demandaQuery) ->orderBy('nome') ->get(); $temOrientacaoIndividual = DB::table('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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') ->whereNull('tr.codigo') ->where('soap.is_rascunho', false) ->where('soap.orientacao_individual_em_saude', true) ->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)) ->exists() || DB::table('ambulatorio_atendimento_soap_teste_rapido as atr') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atr.soap_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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') ->whereNull('tr.codigo') ->where('soap.is_rascunho', false) ->where('soap.orientacao_individual_em_saude', true) ->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)) ->exists(); if ($temOrientacaoIndividual) { $procedimentos->push((object) ['id' => 'orientacao_individual', 'nome' => 'Orientação Individual em Saúde']); } } return ['form' => $procedimentos, 'model' => $id]; } private static function getDemandaTipos($params) { $tipos = collect(); $id = null; if (is_null($params['periodo'])) { return ['form' => $tipos, 'model' => $id]; } $comProcedimentoOuTeste = fn ($q) => $q ->whereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_procedimento as ap') ->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'ap.soap_id') ->whereColumn('ap.atendimento_id', 'atendimento.id') ->where(fn ($q) => $q->whereNull('soap.id')->orWhere('soap.is_rascunho', false)) ) ->orWhereExists(fn ($q) => $q ->selectRaw(1) ->from('ambulatorio_atendimento_soap as soap') ->join('ambulatorio_atendimento_soap_teste_rapido as atr', 'atr.soap_id', 'soap.id') ->join('ambulatorio_teste_rapido as tr', 'tr.id', 'atr.teste_rapido_id') ->whereColumn('soap.atendimento_id', 'atendimento.id') ->where('soap.is_rascunho', false) ->where(fn ($q) => $q ->where('tr.is_abpg', true) ->orWhere(fn ($q) => $q->whereNull('tr.codigo')->where('soap.orientacao_individual_em_saude', true)) ) ); $demandaAgendada = DB::table('ambulatorio_agendamento as agendamento') ->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id') ->join('ambulatorio_atendimento as atendimento', 'atendimento.agendamento_id', 'agendamento.id') ->whereBetween('jornada.data', $params['periodo']) ->where($comProcedimentoOuTeste) ->exists(); $demandaEspontanea = DB::table('ambulatorio_demanda_espontanea as demanda') ->join('ambulatorio_atendimento as atendimento', 'atendimento.demanda_espontanea_id', 'demanda.id') ->whereBetween('demanda.data', $params['periodo']) ->where($comProcedimentoOuTeste) ->exists(); if ($demandaAgendada) { $tipos->push(['value' => 'agendada', 'text' => 'Demanda Agendada']); } if ($demandaEspontanea) { $tipos->push(['value' => 'espontanea', 'text' => 'Demanda Espontânea']); } return ['form' => $tipos, 'model' => $id]; } }