Files
rkm-conectasus-ci-sandbox/api/app/Services/Ambulatorio/RelatorioEstatisticoStatusService.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

524 lines
30 KiB
PHP

<?php
namespace App\Services\Ambulatorio;
use App\Enums\Ambulatorio\AgendamentoStatus;
use App\Services\Service;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class RelatorioEstatisticoStatusService extends Service
{
public static function index()
{
$params = self::getParams();
$demandas = collect();
if (is_null($params->origem) || $params->origem === 'agendamento') {
DB::table('ambulatorio_agendamento')
->select([
'cadastro_usuario.id AS usuario_id',
'cadastro_cidadao.nome AS usuario_nome',
'cadastro_especialidade.id AS especialidade_id',
'cadastro_especialidade.nome AS especialidade_nome',
'cadastro_unidade.id AS unidade_id',
'cadastro_unidade.nome_fantasia AS unidade_nome_fantasia',
'cadastro_infraestrutura.id AS infraestrutura_id',
DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS infraestrutura_nome"),
DB::raw("SUM(CASE WHEN ambulatorio_agendamento.status_id IN (" . AgendamentoStatus::REALIZADO . ", " . AgendamentoStatus::FALTA_DO_PACIENTE . ", " . AgendamentoStatus::FALTA_DO_MEDICO . ") THEN 1 ELSE 0 END) AS quantidade_atendimento"),
DB::raw("SUM(CASE WHEN ambulatorio_agendamento.status_id = " . AgendamentoStatus::REALIZADO . " THEN 1 ELSE 0 END) AS quantidade_atendimento_realizado"),
DB::raw("SUM(CASE WHEN ambulatorio_agendamento.status_id = " . AgendamentoStatus::FALTA_DO_PACIENTE . " THEN 1 ELSE 0 END) AS quantidade_falta_paciente"),
DB::raw("SUM(CASE WHEN ambulatorio_agendamento.status_id = " . AgendamentoStatus::FALTA_DO_MEDICO . " THEN 1 ELSE 0 END) AS quantidade_falta_medico"),
])
->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_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
->join('cadastro_especialidade', 'cadastro_especialidade.id', 'ambulatorio_jornada.especialidade_id')
->join('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id')
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
->whereIn('ambulatorio_agendamento.status_id', [AgendamentoStatus::REALIZADO, AgendamentoStatus::FALTA_DO_PACIENTE, AgendamentoStatus::FALTA_DO_MEDICO])
->whereBetween('ambulatorio_jornada.data', $params->periodo)
->when($params->unidade_id, fn ($q, $unidadeId) => $q
->where('cadastro_unidade.id', $unidadeId)
)
->when($params->infraestrutura_id, fn ($q, $infraestruturaId) => $q
->where('cadastro_infraestrutura.id', $infraestruturaId)
)
->when($params->especialidade_id, fn ($q, $especialidadeId) => $q
->where('cadastro_especialidade.id', $especialidadeId)
)
->when($params->usuario_id, fn ($q, $usuarioId) => $q
->where('cadastro_usuario.id', $usuarioId)
)
->groupBy([
'cadastro_usuario.id',
'cadastro_cidadao.nome',
'cadastro_especialidade.id',
'cadastro_especialidade.nome',
'cadastro_unidade.id',
'cadastro_unidade.nome_fantasia',
'cadastro_infraestrutura.id',
DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)"),
])
->get()
->each(function($query) use ($demandas) {
$demandas->push((object) [
'usuario_id' => $query->usuario_id,
'usuario_nome' => $query->usuario_nome,
'especialidade_id' => $query->especialidade_id,
'especialidade_nome' => $query->especialidade_nome,
'unidade_id' => $query->unidade_id,
'unidade_nome_fantasia' => $query->unidade_nome_fantasia,
'infraestrutura_id' => $query->infraestrutura_id,
'infraestrutura_nome' => $query->infraestrutura_nome,
'agendamento_quantidade_atendimento_realizado' => $query->quantidade_atendimento_realizado,
'agendamento_porcentagem_atendimento_realizado' => round($query->quantidade_atendimento > 0 ? $query->quantidade_atendimento_realizado * 100 / $query->quantidade_atendimento : 0),
'agendamento_quantidade_falta_paciente' => $query->quantidade_falta_paciente,
'agendamento_porcentagem_falta_paciente' => round($query->quantidade_atendimento > 0 ? $query->quantidade_falta_paciente * 100 / $query->quantidade_atendimento : 0),
'agendamento_quantidade_falta_medico' => $query->quantidade_falta_medico,
'agendamento_porcentagem_falta_medico' => round($query->quantidade_atendimento > 0 ? $query->quantidade_falta_medico * 100 / $query->quantidade_atendimento : 0),
'agendamento_quantidade_atendimento' => $query->quantidade_atendimento,
'demanda_espontanea_quantidade_atendimento_realizado' => 0,
'demanda_espontanea_porcentagem_atendimento_realizado' => 0,
'demanda_espontanea_quantidade_atendimento' => 0,
]);
});
}
if (is_null($params->origem) || $params->origem === 'demanda-espontanea') {
DB::table('ambulatorio_demanda_espontanea')
->select([
'cadastro_usuario.id AS usuario_id',
'cadastro_cidadao.nome AS usuario_nome',
'cadastro_especialidade.id AS especialidade_id',
'cadastro_especialidade.nome AS especialidade_nome',
'cadastro_unidade.id AS unidade_id',
'cadastro_unidade.nome_fantasia AS unidade_nome_fantasia',
'cadastro_infraestrutura.id AS infraestrutura_id',
DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS infraestrutura_nome"),
DB::raw("COUNT(*) AS quantidade_atendimento_realizado"),
])
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.demanda_espontanea_id', 'ambulatorio_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_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
->leftJoin('cadastro_especialidade', 'cadastro_especialidade.id', 'ambulatorio_demanda_espontanea.especialidade_id')
->leftJoin('cadastro_usuario', 'cadastro_usuario.id', 'ambulatorio_demanda_espontanea.usuario_id')
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
->whereNotNull('ambulatorio_atendimento.finalizado_em')
->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo)
->when($params->unidade_id, fn ($q, $unidadeId) => $q
->where('cadastro_unidade.id', $unidadeId)
)
->when($params->infraestrutura_id, fn ($q, $infraestruturaId) => $q
->where('cadastro_infraestrutura.id', $infraestruturaId)
)
->when($params->especialidade_id, fn ($q, $especialidadeId) => $q
->where('cadastro_especialidade.id', $especialidadeId)
)
->when($params->usuario_id, fn ($q, $usuarioId) => $q
->where('cadastro_usuario.id', $usuarioId)
)
->groupBy([
'cadastro_usuario.id',
'cadastro_cidadao.nome',
'cadastro_especialidade.id',
'cadastro_especialidade.nome',
'cadastro_unidade.id',
'cadastro_unidade.nome_fantasia',
'cadastro_infraestrutura.id',
DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)"),
])
->get()
->each(function($query) use ($demandas) {
$demanda = $demandas->where('usuario_id', $query->usuario_id)
->where('especialidade_id', $query->especialidade_id)
->where('unidade_id', $query->unidade_id)
->where('infraestrutura_id', $query->infraestrutura_id)
->first();
if (is_null($demanda)) {
$demanda = (object) [
'usuario_id' => $query->usuario_id,
'usuario_nome' => $query->usuario_nome,
'especialidade_id' => $query->especialidade_id,
'especialidade_nome' => $query->especialidade_nome,
'unidade_id' => $query->unidade_id,
'unidade_nome_fantasia' => $query->unidade_nome_fantasia,
'infraestrutura_id' => $query->infraestrutura_id,
'infraestrutura_nome' => $query->infraestrutura_nome,
'agendamento_quantidade_atendimento_realizado' => 0,
'agendamento_porcentagem_atendimento_realizado' => 0,
'agendamento_quantidade_falta_paciente' => 0,
'agendamento_porcentagem_falta_paciente' => 0,
'agendamento_quantidade_falta_medico' => 0,
'agendamento_porcentagem_falta_medico' => 0,
'agendamento_quantidade_atendimento' => 0,
];
$demandas->push($demanda);
}
$demanda->demanda_espontanea_quantidade_atendimento_realizado = $query->quantidade_atendimento_realizado;
$demanda->demanda_espontanea_porcentagem_atendimento_realizado = $query->quantidade_atendimento_realizado > 0 ? 100 : 0;
$demanda->demanda_espontanea_quantidade_atendimento = $query->quantidade_atendimento_realizado;
});
}
$demandas->each(function ($query) {
unset(
$query->usuario_id,
$query->especialidade_id,
$query->unidade_id,
$query->infraestrutura_id
);
});
$agendamentoQuantidadeAtendimentoRealizado = $demandas->reduce(fn ($a, $b) => $a + $b->agendamento_quantidade_atendimento_realizado);
$agendamentoQuantidadeFaltaPaciente = $demandas->reduce(fn ($a, $b) => $a + $b->agendamento_quantidade_falta_paciente);
$agendamentoQuantidadeFaltaMedico = $demandas->reduce(fn ($a, $b) => $a + $b->agendamento_quantidade_falta_medico);
$agendamentoQuantidadeAtendimento = $demandas->reduce(fn ($a, $b) => $a + $b->agendamento_quantidade_atendimento);
$demandaEspontaneaQuantidadeAtendimentoRealizado = $demandas->reduce(fn ($a, $b) => $a + $b->demanda_espontanea_quantidade_atendimento_realizado);
$demandaEspontaneaQuantidadeAtendimento = $demandas->reduce(fn ($a, $b) => $a + $b->demanda_espontanea_quantidade_atendimento);
return [
'itens' => $demandas,
'totalizadores' => [
'agendamento_quantidade_atendimento_realizado' => $agendamentoQuantidadeAtendimentoRealizado,
'agendamento_porcentagem_atendimento_realizado' => round($agendamentoQuantidadeAtendimento > 0 ? $agendamentoQuantidadeAtendimentoRealizado * 100 / $agendamentoQuantidadeAtendimento : 0, 2),
'agendamento_quantidade_falta_paciente' => $agendamentoQuantidadeFaltaPaciente,
'agendamento_porcentagem_falta_paciente' => round($agendamentoQuantidadeAtendimento > 0 ? $agendamentoQuantidadeFaltaPaciente * 100 / $agendamentoQuantidadeAtendimento : 0, 2),
'agendamento_quantidade_falta_medico' => $agendamentoQuantidadeFaltaMedico,
'agendamento_porcentagem_falta_medico' => round($agendamentoQuantidadeAtendimento > 0 ? $agendamentoQuantidadeFaltaMedico * 100 / $agendamentoQuantidadeAtendimento : 0, 2),
'agendamento_quantidade_atendimento' => $agendamentoQuantidadeAtendimento,
'demanda_espontanea_quantidade_atendimento_realizado' => $demandaEspontaneaQuantidadeAtendimentoRealizado,
'demanda_espontanea_porcentagem_atendimento_realizado' => $demandaEspontaneaQuantidadeAtendimentoRealizado > 0 ? 100 : 0,
'demanda_espontanea_quantidade_atendimento' => $demandaEspontaneaQuantidadeAtendimento
],
];
}
public static function create()
{
return [
'form' => [
'unidades' => [],
'infraestruturas' => [],
'especialidades' => [],
'usuarios' => [],
'origens' => [],
],
'model' => [
'periodo' => ['start' => null, 'end' => null],
'unidade_id' => null,
'infraestrutura_id' => null,
'especialidade_id' => null,
'usuario_id' => null,
'origem' => null,
],
];
}
public static function list()
{
$params = self::getParams();
switch (request()->listar) {
case 'unidades': {
return [
'unidades' => self::getUnidades($params),
'infraestruturas' => self::getInfraestruturas($params),
'especialidades' => self::getEspecialidades($params),
'usuarios' => self::getUsuarios($params),
'origens' => self::getOrigens($params),
];
}
case 'infraestruturas': {
return [
'infraestruturas' => self::getInfraestruturas($params),
'especialidades' => self::getEspecialidades($params),
'usuarios' => self::getUsuarios($params),
'origens' => self::getOrigens($params),
];
}
case 'especialidades': {
return [
'especialidades' => self::getEspecialidades($params),
'usuarios' => self::getUsuarios($params),
'origens' => self::getOrigens($params),
];
}
case 'usuarios': {
return [
'usuarios' => self::getUsuarios($params),
'origens' => self::getOrigens($params),
];
}
case 'origens': {
return [
'origens' => self::getOrigens($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'),
'especialidade_id' => request()->get('especialidade_id'),
'usuario_id' => request()->get('usuario_id'),
'origem' => request()->get('origem'),
];
}
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('cadastro_infraestrutura')
->whereColumn('cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id')
->where(fn ($q) => $q
->whereExists(fn ($q) => $q
->select(DB::raw(1))
->from('cadastro_lotacao')
->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id')
->whereExists(fn ($q) => $q
->select(DB::raw(1))
->from('ambulatorio_agendamento')
->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id')
->whereColumn('ambulatorio_jornada.lotacao_id', 'cadastro_lotacao.id')
->whereIn('ambulatorio_agendamento.status_id', [AgendamentoStatus::REALIZADO, AgendamentoStatus::FALTA_DO_PACIENTE, AgendamentoStatus::FALTA_DO_MEDICO])
->whereBetween('ambulatorio_jornada.data', $params->periodo)
)
)
->orWhereExists(fn ($q) => $q
->select(DB::raw(1))
->from('ambulatorio_demanda_espontanea')
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.demanda_espontanea_id', 'ambulatorio_demanda_espontanea.id')
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id')
->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id')
->whereNotNull('ambulatorio_atendimento.finalizado_em')
->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('cadastro_lotacao')
->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id')
->whereExists(fn ($q) => $q
->select(DB::raw(1))
->from('ambulatorio_agendamento')
->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id')
->whereColumn('ambulatorio_jornada.lotacao_id', 'cadastro_lotacao.id')
->whereIn('ambulatorio_agendamento.status_id', [AgendamentoStatus::REALIZADO, AgendamentoStatus::FALTA_DO_PACIENTE, AgendamentoStatus::FALTA_DO_MEDICO])
->whereBetween('ambulatorio_jornada.data', $params->periodo)
)
)
->orWhereExists(fn ($q) => $q
->select(DB::raw(1))
->from('ambulatorio_demanda_espontanea')
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.demanda_espontanea_id', 'ambulatorio_demanda_espontanea.id')
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id')
->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id')
->whereNotNull('ambulatorio_atendimento.finalizado_em')
->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo)
)
)
->orderByRaw('COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)')
->get();
}
private static function getEspecialidades($params)
{
return DB::table('cadastro_especialidade')
->select([
'cadastro_especialidade.id',
'cadastro_especialidade.nome',
])
->where(fn ($q) => $q
->whereExists(fn ($q) => $q
->select(DB::raw(1))
->from('ambulatorio_agendamento')
->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('ambulatorio_jornada.especialidade_id', 'cadastro_especialidade.id')
->whereIn('ambulatorio_agendamento.status_id', [AgendamentoStatus::REALIZADO, AgendamentoStatus::FALTA_DO_PACIENTE, AgendamentoStatus::FALTA_DO_MEDICO])
->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_demanda_espontanea')
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.demanda_espontanea_id', 'ambulatorio_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.especialidade_id', 'cadastro_especialidade.id')
->whereNotNull('ambulatorio_atendimento.finalizado_em')
->whereBetween('ambulatorio_demanda_espontanea.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)
)
)
)
->orderBy('cadastro_especialidade.nome')
->get();
}
private static function getUsuarios($params)
{
return DB::table('cadastro_usuario')
->select([
'cadastro_usuario.id',
'cadastro_cidadao.nome',
])
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
->where(fn ($q) => $q
->whereExists(fn ($q) => $q
->select(DB::raw(1))
->from('cadastro_lotacao')
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
->whereColumn('cadastro_lotacao.usuario_id', 'cadastro_usuario.id')
->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)
)
->whereExists(fn ($q) => $q
->select(DB::raw(1))
->from('ambulatorio_agendamento')
->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id')
->whereColumn('ambulatorio_jornada.lotacao_id', 'cadastro_lotacao.id')
->whereIn('ambulatorio_agendamento.status_id', [AgendamentoStatus::REALIZADO, AgendamentoStatus::FALTA_DO_PACIENTE, AgendamentoStatus::FALTA_DO_MEDICO])
->whereBetween('ambulatorio_jornada.data', $params->periodo)
->when($params->especialidade_id, fn ($q, $especialidadeId) => $q
->where('ambulatorio_jornada.especialidade_id', $especialidadeId)
)
)
)
->orWhereExists(fn ($q) => $q
->select(DB::raw(1))
->from('ambulatorio_demanda_espontanea')
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.demanda_espontanea_id', 'ambulatorio_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.usuario_id', 'cadastro_usuario.id')
->whereNotNull('ambulatorio_atendimento.finalizado_em')
->whereBetween('ambulatorio_demanda_espontanea.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)
)
->when($params->especialidade_id, fn ($q, $especialidadeId) => $q
->where('ambulatorio_demanda_espontanea.especialidade_id', $especialidadeId)
)
)
)
->orderBy('cadastro_cidadao.nome')
->get();
}
private static function getOrigens($params)
{
$origens = collect();
$hasDemandaAgendada = DB::table('ambulatorio_agendamento')
->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')
->whereIn('ambulatorio_agendamento.status_id', [AgendamentoStatus::REALIZADO, AgendamentoStatus::FALTA_DO_PACIENTE, AgendamentoStatus::FALTA_DO_MEDICO])
->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)
)
->when($params->especialidade_id, fn ($q, $especialidadeId) => $q
->where('ambulatorio_jornada.especialidade_id', $especialidadeId)
)
->when($params->usuario_id, fn ($q, $usuarioId) => $q
->where('cadastro_lotacao.usuario_id', $usuarioId)
)
->exists();
$hasDemandaEspontanea = DB::table('ambulatorio_demanda_espontanea')
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.demanda_espontanea_id', 'ambulatorio_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')
->whereNotNull('ambulatorio_atendimento.finalizado_em')
->whereBetween('ambulatorio_demanda_espontanea.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)
)
->when($params->especialidade_id, fn ($q, $especialidadeId) => $q
->where('ambulatorio_demanda_espontanea.especialidade_id', $especialidadeId)
)
->when($params->usuario_id, fn ($q, $usuarioId) => $q
->where('ambulatorio_demanda_espontanea.usuario_id', $usuarioId)
)
->exists();
if ($hasDemandaAgendada) {
$origens->push((object) ['value' => 'agendamento', 'text' => 'Agendamento']);
}
if ($hasDemandaEspontanea) {
$origens->push((object) ['value' => 'demanda-espontanea', 'text' => 'Demanda espontânea']);
}
return $origens;
}
}