This commit is contained in:
559
api/app/Services/Ambulatorio/RelatorioIdentificacaoService.php
Normal file
559
api/app/Services/Ambulatorio/RelatorioIdentificacaoService.php
Normal file
@@ -0,0 +1,559 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ambulatorio;
|
||||
|
||||
use App\Services\Service;
|
||||
use App\Utils\MaskUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioIdentificacaoService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$params = self::getParams();
|
||||
|
||||
$atendimentos = DB::table('ambulatorio_atendimento_procedimento')
|
||||
->select([
|
||||
DB::raw("DATE_FORMAT(ambulatorio_jornada.data, '%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',
|
||||
'procedimento.codigo AS procedimento_codigo',
|
||||
'procedimento.nome AS procedimento_nome',
|
||||
DB::raw("COUNT(*) AS quantidade"),
|
||||
])
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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_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')
|
||||
->join('sigtap.procedimento', 'procedimento.id', 'ambulatorio_atendimento_procedimento.procedimento_id')
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
)
|
||||
)
|
||||
->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)
|
||||
)
|
||||
->when($params->procedimento_id, fn ($q, $procedimentoId) => $q
|
||||
->where('procedimento.id', $procedimentoId)
|
||||
)
|
||||
->union(
|
||||
DB::table('ambulatorio_atendimento_procedimento')
|
||||
->select([
|
||||
DB::raw("DATE_FORMAT(ambulatorio_demanda_espontanea.data, '%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',
|
||||
'procedimento.codigo AS procedimento_codigo',
|
||||
'procedimento.nome AS procedimento_nome',
|
||||
DB::raw("COUNT(*) AS quantidade"),
|
||||
])
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->join('sigtap.procedimento', 'procedimento.id', 'ambulatorio_atendimento_procedimento.procedimento_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', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
)
|
||||
)
|
||||
->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)
|
||||
)
|
||||
->when($params->procedimento_id, fn ($q, $procedimentoId) => $q
|
||||
->where('procedimento.id', $procedimentoId)
|
||||
)
|
||||
->groupBy([
|
||||
DB::raw("DATE_FORMAT(ambulatorio_demanda_espontanea.data, '%d/%m/%Y')"),
|
||||
'cadastro_unidade.nome_fantasia',
|
||||
DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)"),
|
||||
'cadastro_especialidade.nome',
|
||||
'cadastro_cidadao.nome',
|
||||
'procedimento.codigo',
|
||||
'procedimento.nome',
|
||||
])
|
||||
)
|
||||
->groupBy([
|
||||
DB::raw("DATE_FORMAT(ambulatorio_jornada.data, '%d/%m/%Y')"),
|
||||
'cadastro_unidade.nome_fantasia',
|
||||
DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)"),
|
||||
'cadastro_especialidade.nome',
|
||||
'cadastro_cidadao.nome',
|
||||
'procedimento.codigo',
|
||||
'procedimento.nome',
|
||||
])
|
||||
->get()
|
||||
->map(function($query) {
|
||||
$query->procedimento = MaskUtil::getProcedimentoMasked($query->procedimento_codigo) . ' - ' . $query->procedimento_nome;
|
||||
|
||||
unset($query->procedimento_codigo);
|
||||
unset($query->procedimento_nome);
|
||||
|
||||
return $query;
|
||||
});
|
||||
|
||||
return [
|
||||
'itens' => $atendimentos,
|
||||
'totalizadores' => ['quantidade' => $atendimentos->reduce(fn ($a, $b) => $a + $b->quantidade)],
|
||||
];
|
||||
}
|
||||
|
||||
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,
|
||||
'procedimento' => 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'),
|
||||
'procedimento_id' => request()->get('procedimento_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('ambulatorio_atendimento_procedimento')
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
)
|
||||
)
|
||||
->whereBetween('ambulatorio_jornada.data', $params->periodo)
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->from('ambulatorio_atendimento_procedimento')
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_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_procedimento')
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
)
|
||||
)
|
||||
->whereBetween('ambulatorio_jornada.data', $params->periodo)
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('ambulatorio_atendimento_procedimento')
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_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',
|
||||
])
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('ambulatorio_atendimento_procedimento')
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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('ambulatorio_jornada.especialidade_id', 'cadastro_especialidade.id')
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_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_procedimento')
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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.especialidade_id', 'cadastro_especialidade.id')
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
)
|
||||
)
|
||||
->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')
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('ambulatorio_atendimento_procedimento')
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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.usuario_id', 'cadastro_usuario.id')
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_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)
|
||||
)
|
||||
->when($params->especialidade_id, fn ($q, $especialidadeId) => $q
|
||||
->where('ambulatorio_jornada.especialidade_id', $especialidadeId)
|
||||
)
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('ambulatorio_atendimento_procedimento')
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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.usuario_id', 'cadastro_usuario.id')
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'ambulatorio_atendimento_procedimento.soap_id')
|
||||
)
|
||||
)
|
||||
->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();
|
||||
}
|
||||
|
||||
public static function autocomplete()
|
||||
{
|
||||
$params = self::getParams();
|
||||
|
||||
$procedimentos = DB::table('ambulatorio_atendimento_procedimento')
|
||||
->select([
|
||||
'ambulatorio_jornada.data',
|
||||
'cadastro_infraestrutura.unidade_id',
|
||||
'cadastro_lotacao.infraestrutura_id',
|
||||
'ambulatorio_jornada.especialidade_id',
|
||||
'cadastro_lotacao.usuario_id',
|
||||
'ambulatorio_atendimento_procedimento.procedimento_id',
|
||||
'ambulatorio_atendimento_procedimento.soap_id',
|
||||
])
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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')
|
||||
->union(
|
||||
DB::table('ambulatorio_atendimento_procedimento')
|
||||
->select([
|
||||
'ambulatorio_demanda_espontanea.data',
|
||||
'cadastro_infraestrutura.unidade_id',
|
||||
'cadastro_lotacao.infraestrutura_id',
|
||||
'ambulatorio_demanda_espontanea.especialidade_id',
|
||||
'ambulatorio_demanda_espontanea.usuario_id',
|
||||
'ambulatorio_atendimento_procedimento.procedimento_id',
|
||||
'ambulatorio_atendimento_procedimento.soap_id',
|
||||
])
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_procedimento.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('sigtap.procedimento', 'procedimento.id', 'ambulatorio_atendimento_procedimento.procedimento_id')
|
||||
);
|
||||
|
||||
return DB::table('sigtap.procedimento')
|
||||
->select([
|
||||
'procedimento.id',
|
||||
'procedimento.codigo',
|
||||
'procedimento.nome',
|
||||
])
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from(DB::raw("({$procedimentos->toSql()}) AS x"))
|
||||
->whereColumn('x.procedimento_id', 'procedimento.id')
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'x.soap_id')
|
||||
->where('soap.is_rascunho', false)
|
||||
)
|
||||
->orWhereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.id', 'x.soap_id')
|
||||
)
|
||||
)
|
||||
->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)
|
||||
)
|
||||
->when($params->especialidade_id, fn ($q, $especialidadeId) => $q
|
||||
->where('x.especialidade_id', $especialidadeId)
|
||||
)
|
||||
->when($params->usuario_id, fn ($q, $usuarioId) => $q
|
||||
->where('x.usuario_id', $usuarioId)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user