427 lines
22 KiB
PHP
427 lines
22 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 RelatorioEstatisticoPacienteService extends Service
|
|
{
|
|
public static function index()
|
|
{
|
|
$params = self::getParams();
|
|
|
|
$itens = DB::table('ambulatorio_agendamento')
|
|
->select([
|
|
'ambulatorio_agendamento.id',
|
|
DB::raw("DATE_FORMAT(ambulatorio_jornada.data, '%d/%m/%Y') AS data"),
|
|
'cadastro_especialidade.nome AS especialidade_nome',
|
|
'cadastro_cidadao_usuario.nome AS usuario_nome',
|
|
'cadastro_cidadao.nome AS paciente_nome',
|
|
'cadastro_cidadao.cns AS paciente_cns',
|
|
DB::raw("CONCAT(cadastro_cidadao.nome, ' / ', IFNULL(cadastro_cidadao.cns, '')) AS paciente"),
|
|
'ambulatorio_agendamento_status.nome AS agendamento_status_nome',
|
|
'cadastro_cidadao.data_nascimento',
|
|
DB::raw("TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE) AS paciente_idade"),
|
|
])
|
|
->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_especialidade', 'cadastro_especialidade.id', 'ambulatorio_jornada.especialidade_id')
|
|
->join('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id')
|
|
->join('cadastro_cidadao AS cadastro_cidadao_usuario', 'cadastro_cidadao_usuario.id', 'cadastro_usuario.cidadao_id')
|
|
->join('cadastro_paciente', 'cadastro_paciente.id', 'ambulatorio_agendamento.paciente_id')
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
|
->join('ambulatorio_agendamento_status', 'ambulatorio_agendamento_status.id', 'ambulatorio_agendamento.status_id')
|
|
->whereBetween('ambulatorio_jornada.data', $params->periodo)
|
|
->where('cadastro_infraestrutura.unidade_id', $params->unidade_id)
|
|
->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)
|
|
)
|
|
->when($params->status_id, fn ($q, $statusId) => $q
|
|
->where('ambulatorio_agendamento_status.id', $statusId)
|
|
)
|
|
->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))
|
|
)
|
|
->union(
|
|
DB::table('ambulatorio_atendimento')
|
|
->select([
|
|
'ambulatorio_atendimento.id',
|
|
DB::raw("DATE_FORMAT(ambulatorio_demanda_espontanea.data, '%d/%m/%Y') AS data"),
|
|
'cadastro_especialidade.nome AS especialidade_nome',
|
|
'cadastro_cidadao_usuario.nome AS usuario_nome',
|
|
'cadastro_cidadao.nome AS paciente_nome',
|
|
'cadastro_cidadao.cns AS paciente_cns',
|
|
DB::raw("CONCAT(cadastro_cidadao.nome, ' / ', IFNULL(cadastro_cidadao.cns, '')) AS paciente"),
|
|
DB::raw("
|
|
(
|
|
SELECT ambulatorio_agendamento_status.nome
|
|
FROM ambulatorio_agendamento_status
|
|
WHERE ambulatorio_agendamento_status.id = " . AgendamentoStatus::REALIZADO . "
|
|
) AS agendamento_status_nome
|
|
"),
|
|
'cadastro_cidadao.data_nascimento',
|
|
DB::raw("TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, CURRENT_DATE) AS paciente_idade"),
|
|
])
|
|
->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_especialidade', 'cadastro_especialidade.id', 'ambulatorio_demanda_espontanea.especialidade_id')
|
|
->join('cadastro_usuario', 'cadastro_usuario.id', 'ambulatorio_demanda_espontanea.usuario_id')
|
|
->join('cadastro_cidadao AS cadastro_cidadao_usuario', 'cadastro_cidadao_usuario.id', 'cadastro_usuario.cidadao_id')
|
|
->join('cadastro_paciente', 'cadastro_paciente.id', 'ambulatorio_demanda_espontanea.paciente_id')
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
|
->whereNotNull('ambulatorio_atendimento.finalizado_em')
|
|
->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo)
|
|
->where('cadastro_infraestrutura.unidade_id', $params->unidade_id)
|
|
->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)
|
|
)
|
|
->when(
|
|
$params->status_id,
|
|
fn($q, $statusId) => $q
|
|
->where(DB::raw("
|
|
(
|
|
SELECT ambulatorio_agendamento_status.id
|
|
FROM ambulatorio_agendamento_status
|
|
WHERE ambulatorio_agendamento_status.id = " . AgendamentoStatus::REALIZADO . "
|
|
)
|
|
"), $statusId)
|
|
)
|
|
->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))
|
|
)
|
|
)
|
|
->get();
|
|
|
|
return [
|
|
'itens' => $itens,
|
|
'totalizadores' => ['quantidade_atendimentos' => $itens->count()],
|
|
];
|
|
}
|
|
|
|
public static function create()
|
|
{
|
|
return [
|
|
'form' => [
|
|
'unidades' => [],
|
|
'infraestruturas' => [],
|
|
'especialidades' => [],
|
|
'usuarios' => [],
|
|
'status' => [],
|
|
],
|
|
'model' => [
|
|
'periodo' => ['start' => null, 'end' => null],
|
|
'unidade_id' => null,
|
|
'infraestrutura_id' => null,
|
|
'especialidade_id' => null,
|
|
'usuario_id' => null,
|
|
'status_id' => null,
|
|
'idade_de' => null,
|
|
'idade_ate' => null,
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function list()
|
|
{
|
|
$params = self::getParams();
|
|
|
|
switch (request()->listar) {
|
|
case 'unidades': {
|
|
return [
|
|
'unidades' => self::getUnidades($params),
|
|
];
|
|
}
|
|
case 'infraestruturas': {
|
|
return [
|
|
'infraestruturas' => self::getInfraestruturas($params),
|
|
'especialidades' => self::getEspecialidades($params),
|
|
'usuarios' => self::getUsuarios($params),
|
|
'status' => self::getStatus($params),
|
|
];
|
|
}
|
|
case 'especialidades': {
|
|
return [
|
|
'especialidades' => self::getEspecialidades($params),
|
|
'usuarios' => self::getUsuarios($params),
|
|
'status' => self::getStatus($params),
|
|
];
|
|
}
|
|
case 'usuarios': {
|
|
return [
|
|
'usuarios' => self::getUsuarios($params),
|
|
'status' => self::getStatus($params),
|
|
];
|
|
}
|
|
case 'status': {
|
|
return [
|
|
'status' => self::getStatus($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'),
|
|
'status_id' => request()->get('status_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',
|
|
])
|
|
->where(fn ($q) => $q
|
|
->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_jornada.data', $params->periodo)
|
|
)
|
|
)
|
|
)
|
|
)
|
|
->orWhereExists(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_demanda_espontanea')
|
|
->whereColumn('ambulatorio_demanda_espontanea.created_lotacao_id', 'cadastro_lotacao.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('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_jornada.data', $params->periodo)
|
|
)
|
|
)
|
|
)
|
|
->orWhereExists(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_demanda_espontanea')
|
|
->whereColumn('ambulatorio_demanda_espontanea.created_lotacao_id', 'cadastro_lotacao.id')
|
|
->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')
|
|
->whereBetween('ambulatorio_jornada.data', $params->periodo)
|
|
->where('cadastro_infraestrutura.unidade_id', $params->unidade_id)
|
|
->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('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')
|
|
->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo)
|
|
->where('cadastro_infraestrutura.unidade_id', $params->unidade_id)
|
|
->when($params->infraestrutura_id, fn ($q, $infraestruturaId) => $q
|
|
->where('cadastro_infraestrutura.id', $infraestruturaId)
|
|
)
|
|
)
|
|
)
|
|
->orderBy('cadastro_especialidade.nome')
|
|
->get();
|
|
}
|
|
|
|
private static function getUsuarios($params)
|
|
{
|
|
$queryAgendamentos = DB::table('cadastro_usuario')
|
|
->select([
|
|
'cadastro_usuario.id',
|
|
'cadastro_cidadao.nome',
|
|
])
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.usuario_id', 'cadastro_usuario.id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
|
->join('ambulatorio_jornada', 'ambulatorio_jornada.lotacao_id', 'cadastro_lotacao.id')
|
|
->where('cadastro_infraestrutura.unidade_id', $params->unidade_id)
|
|
->where('cadastro_lotacao.ativo', 1)
|
|
->whereBetween('ambulatorio_jornada.data', $params->periodo)
|
|
->when($params->infraestrutura_id, function ($q, $infraestruturaId) {
|
|
return $q->where('cadastro_infraestrutura.id', $infraestruturaId);
|
|
})
|
|
->when($params->especialidade_id, function ($q, $especialidadeId) {
|
|
return $q->where('ambulatorio_jornada.especialidade_id', $especialidadeId);
|
|
});
|
|
|
|
$queryDemandas = DB::table('cadastro_usuario')
|
|
->select([
|
|
'cadastro_usuario.id',
|
|
'cadastro_cidadao.nome',
|
|
])
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
|
->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.usuario_id', 'cadastro_usuario.id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
|
->where('cadastro_infraestrutura.unidade_id', $params->unidade_id)
|
|
->where('cadastro_lotacao.ativo', 1)
|
|
->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo)
|
|
->when($params->infraestrutura_id, function ($q, $infraestruturaId) {
|
|
return $q->where('cadastro_infraestrutura.id', $infraestruturaId);
|
|
})
|
|
->when($params->especialidade_id, function ($q, $especialidadeId) {
|
|
return $q->where('ambulatorio_demanda_espontanea.especialidade_id', $especialidadeId);
|
|
});
|
|
|
|
return $queryAgendamentos
|
|
->union($queryDemandas)
|
|
->orderBy('nome')
|
|
->get();
|
|
}
|
|
|
|
private static function getStatus($params)
|
|
{
|
|
return DB::table('ambulatorio_agendamento_status')
|
|
->select([
|
|
'ambulatorio_agendamento_status.id',
|
|
'ambulatorio_agendamento_status.nome',
|
|
])
|
|
->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_agendamento.status_id', 'ambulatorio_agendamento_status.id')
|
|
->whereBetween('ambulatorio_jornada.data', $params->periodo)
|
|
->where('cadastro_infraestrutura.unidade_id', $params->unidade_id)
|
|
->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)
|
|
)
|
|
)
|
|
->orWhereExists(fn ($q) => $q
|
|
->select(DB::raw(1))
|
|
->from('ambulatorio_demanda_espontanea')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
|
->where('ambulatorio_agendamento_status.id', AgendamentoStatus::REALIZADO)
|
|
->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo)
|
|
->where('cadastro_infraestrutura.unidade_id', $params->unidade_id)
|
|
->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)
|
|
)
|
|
)
|
|
->orderBy('ambulatorio_agendamento_status.nome')
|
|
->get();
|
|
}
|
|
}
|