select([ 'cid.codigo AS cid_codigo', 'cid.nome AS cid_nome', 'cadastro_unidade.nome_fantasia AS unidade_nome_fantasia', DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS infraestrutura_nome"), DB::raw("COALESCE(COUNT(IF(ambulatorio_jornada.data BETWEEN '" . $params->periodo['start'] . "' AND '" . $params->periodo['end'] . "', 1, NULL)), 0) AS quantidade_jornada"), DB::raw("COALESCE(COUNT(IF(ambulatorio_demanda_espontanea.data BETWEEN '{$params->periodo['start']}' AND '{$params->periodo['end']}', 1, NULL)), 0) AS quantidade_demanda"), ]) ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_problema_condicao.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->leftJoin('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->leftJoin('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->leftJoin('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->leftJoin('cadastro_lotacao', function ($join) { $join->on('cadastro_lotacao.id', '=', 'ambulatorio_jornada.lotacao_id') ->orOn('cadastro_lotacao.id', '=', 'ambulatorio_demanda_espontanea.created_lotacao_id'); }) ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id') ->join('sigtap.cid', 'cid.id', 'ambulatorio_atendimento_soap_problema_condicao.cid_id') ->leftJoin('cadastro_paciente as paciente_agendamento', 'paciente_agendamento.id', '=', 'ambulatorio_agendamento.paciente_id') ->leftJoin('cadastro_paciente as paciente_demanda', 'paciente_demanda.id', '=', 'ambulatorio_demanda_espontanea.paciente_id') ->join('cadastro_cidadao', function($join) { $join->on('cadastro_cidadao.id', '=', DB::raw('COALESCE(paciente_agendamento.cidadao_id, paciente_demanda.cidadao_id)')); }) ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereNull('cadastro_lotacao.deleted_at') ->whereNull('cadastro_infraestrutura.deleted_at') ->whereNull('cadastro_unidade.deleted_at') ->havingRaw("quantidade_demanda + quantidade_jornada > 0") ->when($params->unidade_id, fn ($q, $unidadeId) => $q ->where('cadastro_infraestrutura.unidade_id', $unidadeId) ) ->when($params->infraestrutura_id, fn ($q, $infraestruturaId) => $q ->where('cadastro_infraestrutura.id', $infraestruturaId) ) ->when($params->ocupacao_id, function ($q, $ocupacaoId) { $q->where(function ($query) use ($ocupacaoId) { $query->where('cadastro_lotacao.ocupacao_id', $ocupacaoId) ->orWhere('ambulatorio_demanda_espontanea.ocupacao_id', $ocupacaoId); }); }) ->when($params->cid_id, fn ($q, $cidId) => $q ->where('cid.id', $cidId) ) ->when($params->idade_de, fn ($q, $idadeDe) => $q ->whereRaw(DB::raw("TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE) >= " . $idadeDe)) ) ->when($params->idade_ate, fn ($q, $idadeAte) => $q ->whereRaw(DB::raw("TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE) <= " . $idadeAte)) ) ->groupBy([ 'cid.codigo', 'cid.nome', 'cadastro_unidade.nome_fantasia', DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)"), ]) ->get() ->map(function($query) { $query->cid = $query->cid_codigo . ' - ' . $query->cid_nome; $query->quantidade = $query->quantidade_jornada + $query->quantidade_demanda; unset($query->cid_codigo, $query->cid_nome, $query->quantidade_jornada, $query->quantidade_demanda); return $query; }); return ['itens' => $atendimentos]; } public static function create() { return [ 'form' => [ 'unidades' => [], 'infraestruturas' => [], 'ocupacoes' => [], ], 'model' => [ 'periodo' => ['start' => null, 'end' => null], 'unidade_id' => null, 'infraestrutura_id' => null, 'ocupacao_id' => null, 'cid' => null, 'idade_de' => null, 'idade_ate' => null, ], ]; } public static function list() { $params = self::getParams(); switch (request()->listar) { case 'unidades': { return [ 'unidades' => self::getUnidades($params), 'infraestruturas' => self::getInfraestruturas($params), 'ocupacoes' => self::getOcupacoes($params), ]; } case 'infraestruturas': { return [ 'infraestruturas' => self::getInfraestruturas($params), 'ocupacoes' => self::getOcupacoes($params), ]; } case 'ocupacoes': { return [ 'ocupacoes' => self::getOcupacoes($params), ]; } } } private static function getParams() { return (object) [ 'periodo' => [ 'start' => Carbon::createFromFormat('d/m/Y', request()->get('periodo')['start'])->setTime(0, 0, 0), 'end' => Carbon::createFromFormat('d/m/Y', request()->get('periodo')['end'])->setTime(23, 59, 59) ], 'unidade_id' => request()->get('unidade_id'), 'infraestrutura_id' => request()->get('infraestrutura_id'), 'ocupacao_id' => request()->get('ocupacao_id'), 'cid_id' => request()->get('cid_id'), 'idade_de' => request()->get('idade_de'), 'idade_ate' => request()->get('idade_ate'), ]; } private static function getUnidades($params) { return DB::table('cadastro_unidade') ->select([ 'cadastro_unidade.id', 'cadastro_unidade.nome_fantasia', ]) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_problema_condicao') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_problema_condicao.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereColumn('cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereNotNull('ambulatorio_atendimento_soap_problema_condicao.cid_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ) ->orWhereExists(fn ($q) => $q ->from('ambulatorio_atendimento_soap_problema_condicao') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_problema_condicao.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereColumn('cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereNotNull('ambulatorio_atendimento_soap_problema_condicao.cid_id') ->whereNotNull('ambulatorio_demanda_espontanea.ocupacao_id') ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ) ->orderBy('cadastro_unidade.nome_fantasia') ->get(); } private static function getInfraestruturas($params) { return DB::table('cadastro_infraestrutura') ->select([ 'cadastro_infraestrutura.id', DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS nome"), ]) ->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id') ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->where(fn ($q) => $q ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_problema_condicao') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_problema_condicao.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereNotNull('ambulatorio_atendimento_soap_problema_condicao.cid_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_problema_condicao') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_problema_condicao.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereNotNull('ambulatorio_atendimento_soap_problema_condicao.cid_id') ->whereNotNull('ambulatorio_demanda_espontanea.ocupacao_id') ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ) ) ->orderByRaw('COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)') ->get(); } private static function getOcupacoes($params) { return DB::table('sigtap.ocupacao') ->select([ 'ocupacao.id', DB::raw("CONCAT(ocupacao.codigo, ' - ', ocupacao.nome) AS nome"), ]) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_problema_condicao') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_problema_condicao.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereColumn('cadastro_lotacao.ocupacao_id', 'ocupacao.id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereNotNull('ambulatorio_atendimento_soap_problema_condicao.cid_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->when($params->unidade_id, fn ($q, $unidadeId) => $q ->where('cadastro_infraestrutura.unidade_id', $unidadeId) ) ->when($params->infraestrutura_id, fn ($q, $infraestruturaId) => $q ->where('cadastro_infraestrutura.id', $infraestruturaId) ) ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_problema_condicao') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_problema_condicao.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereColumn('ambulatorio_demanda_espontanea.ocupacao_id', 'ocupacao.id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereNotNull('ambulatorio_atendimento_soap_problema_condicao.cid_id') ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->where('cadastro_infraestrutura.unidade_id', $params->unidade_id) ->where('cadastro_infraestrutura.id', $params->infraestrutura_id) ) ->orderByRaw("CONCAT(ocupacao.codigo, ' - ', ocupacao.nome)") ->get(); } public static function autocomplete() { $params = self::getParams(); $cids = DB::table('ambulatorio_atendimento_soap_problema_condicao') ->select([ 'ambulatorio_jornada.data', 'cadastro_infraestrutura.unidade_id', 'cadastro_infraestrutura.id AS infraestrutura_id', 'cadastro_lotacao.ocupacao_id', 'ambulatorio_atendimento_soap_problema_condicao.cid_id', 'cadastro_cidadao.data_nascimento', 'ambulatorio_atendimento_soap.is_rascunho', ]) ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_problema_condicao.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->join('cadastro_paciente', 'cadastro_paciente.id', 'ambulatorio_agendamento.paciente_id') ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id') ->union( DB::table('ambulatorio_atendimento_soap_problema_condicao') ->select([ 'ambulatorio_demanda_espontanea.data', 'cadastro_infraestrutura.unidade_id', 'cadastro_infraestrutura.id AS infraestrutura_id', 'ambulatorio_demanda_espontanea.ocupacao_id', 'ambulatorio_atendimento_soap_problema_condicao.cid_id', 'cadastro_cidadao.data_nascimento', 'ambulatorio_atendimento_soap.is_rascunho', ]) ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_problema_condicao.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->join('cadastro_paciente', 'cadastro_paciente.id', 'ambulatorio_demanda_espontanea.paciente_id') ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id') ); return DB::table('sigtap.cid') ->select([ 'cid.id', 'cid.codigo', 'cid.nome', ]) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from(DB::raw("({$cids->toSql()}) AS x")) ->whereColumn('x.cid_id', 'cid.id') ->where('x.is_rascunho', false) ->whereBetween('x.data', $params->periodo) ->when($params->unidade_id, fn ($q, $unidadeId) => $q ->where('x.unidade_id', $unidadeId) ) ->when($params->infraestrutura_id, fn ($q, $infraestruturaId) => $q ->where('x.infraestrutura_id', $infraestruturaId) ) ); } }