This commit is contained in:
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ambulatorio;
|
||||
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioConsultaSolicitadaService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$params = self::getParams();
|
||||
|
||||
$agendamentos = DB::table('ambulatorio_agendamento')
|
||||
->select([
|
||||
DB::raw("DATE_FORMAT(ambulatorio_agendamento.created_at, '%d/%m/%Y') AS data"),
|
||||
'cadastro_unidade.nome_fantasia AS unidade_nome_fantasia',
|
||||
DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS infraestrutura_nome"),
|
||||
'cadastro_especialidade.nome AS especialidade_nome',
|
||||
'cadastro_cidadao.nome AS usuario_nome',
|
||||
DB::raw("COUNT(*) AS total"),
|
||||
])
|
||||
->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')
|
||||
->whereBetween('ambulatorio_agendamento.created_at', $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([
|
||||
DB::raw("DATE_FORMAT(ambulatorio_agendamento.created_at, '%d/%m/%Y')"),
|
||||
'cadastro_unidade.nome_fantasia',
|
||||
DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)"),
|
||||
'cadastro_especialidade.nome',
|
||||
'cadastro_cidadao.nome',
|
||||
])
|
||||
->get();
|
||||
|
||||
return [
|
||||
'itens' => $agendamentos,
|
||||
'totalizadores' => ['quantidade_agendamentos' => $agendamentos->reduce(fn ($a, $b) => $a + $b->total)],
|
||||
];
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => [
|
||||
'unidades' => [],
|
||||
'infraestruturas' => [],
|
||||
'especialidades' => [],
|
||||
'usuarios' => [],
|
||||
],
|
||||
'model' => [
|
||||
'periodo' => ['start' => null, 'end' => null],
|
||||
'unidade_id' => null,
|
||||
'infraestrutura_id' => null,
|
||||
'especialidade_id' => null,
|
||||
'usuario_id' => 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),
|
||||
];
|
||||
}
|
||||
case 'infraestruturas': {
|
||||
return [
|
||||
'infraestruturas' => self::getInfraestruturas($params),
|
||||
'especialidades' => self::getEspecialidades($params),
|
||||
'usuarios' => self::getUsuarios($params),
|
||||
];
|
||||
}
|
||||
case 'especialidades': {
|
||||
return [
|
||||
'especialidades' => self::getEspecialidades($params),
|
||||
'usuarios' => self::getUsuarios($params),
|
||||
];
|
||||
}
|
||||
case 'usuarios': {
|
||||
return [
|
||||
'usuarios' => self::getUsuarios($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'),
|
||||
];
|
||||
}
|
||||
|
||||
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')
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('cadastro_lotacao')
|
||||
->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id')
|
||||
->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')
|
||||
->whereColumn('ambulatorio_jornada.lotacao_id', 'cadastro_lotacao.id')
|
||||
->whereBetween('ambulatorio_agendamento.created_at', $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)
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('cadastro_lotacao')
|
||||
->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id')
|
||||
->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')
|
||||
->whereColumn('ambulatorio_jornada.lotacao_id', 'cadastro_lotacao.id')
|
||||
->whereBetween('ambulatorio_agendamento.created_at', $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')
|
||||
->whereBetween('ambulatorio_agendamento.created_at', $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')
|
||||
->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')
|
||||
->whereBetween('ambulatorio_agendamento.created_at', $params->periodo)
|
||||
->when($params->especialidade_id, fn ($q, $especialidadeId) => $q
|
||||
->where('ambulatorio_jornada.especialidade_id', $especialidadeId)
|
||||
)
|
||||
)
|
||||
)
|
||||
->orderBy('cadastro_cidadao.nome')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user