init commit
All checks were successful
build-api-image / build (push) Successful in 51s

This commit is contained in:
Antonio Lopes dos Santos
2026-08-04 16:52:02 -03:00
commit afec302c3e
11594 changed files with 1636728 additions and 0 deletions

View File

@@ -0,0 +1,625 @@
<?php
namespace App\Services\Ambulatorio;
use App\Services\Service;
use App\Utils\MaskUtil;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class RelatorioQuantitativoProcedimentoService extends Service
{
public static function index()
{
$totalizadores = (object) [
'quantidade' => 0,
];
$periodo = [
'start' => Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->startOfDay(),
'end' => Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->endOfDay(),
];
$procedimentos = DB::query()
->fromSub(DB::table('sigtap.procedimento as procedimento')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id')
->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id')
->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id')
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id')
->join('cadastro_usuario as usuario', 'usuario.id', 'atendimento_procedimento.usuario_id')
->join('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_id')
->where(fn ($q) => $q
->whereExists(fn ($q) => $q
->selectRaw(1)
->from('ambulatorio_atendimento_soap as soap')
->whereColumn('soap.id', 'atendimento_procedimento.soap_id')
->where('soap.is_rascunho', false)
)
->orWhereNotExists(fn ($q) => $q
->selectRaw(1)
->from('ambulatorio_atendimento_soap as soap')
->whereColumn('soap.id', 'atendimento_procedimento.soap_id')
)
)
->whereBetween('jornada.data', $periodo)
->when(request()->unidade_id, fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id))
->when(request()->ocupacao_id, fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id))
->when(request()->especialidade_id, fn ($q, $id) => $q->where('atendimento_procedimento.especialidade_id', $id))
->when(request()->usuario_id, fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id))
->when(request()->procedimento_codigo, fn ($q, $codigo) => $q->where('procedimento.codigo', $codigo))
->whereNull('lotacao.deleted_at')
->whereNull('infraestrutura.deleted_at')
->whereNull('usuario.deleted_at')
->whereNull('usuario_cidadao.deleted_at')
->selectRaw("
usuario_cidadao.nome as usuario_nome,
procedimento.codigo as procedimento_codigo,
procedimento.nome as procedimento_nome,
SUM(atendimento_procedimento.quantidade) as quantidade
")
->groupByRaw("
usuario_cidadao.nome,
procedimento.codigo,
procedimento.nome
")
->unionAll(DB::table('sigtap.procedimento as procedimento')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id')
->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id')
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id')
->join('cadastro_usuario as usuario', 'usuario.id', 'atendimento_procedimento.usuario_id')
->join('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_id')
->where(fn ($q) => $q
->whereExists(fn ($q) => $q
->selectRaw(1)
->from('ambulatorio_atendimento_soap as soap')
->whereColumn('soap.id', 'atendimento_procedimento.soap_id')
->where('soap.is_rascunho', false)
)
->orWhereNotExists(fn ($q) => $q
->selectRaw(1)
->from('ambulatorio_atendimento_soap as soap')
->whereColumn('soap.id', 'atendimento_procedimento.soap_id')
)
)
->whereBetween('demanda_espontanea.data', $periodo)
->when(request()->unidade_id, fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id))
->when(request()->ocupacao_id, fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id))
->when(request()->especialidade_id, fn ($q, $id) => $q->where('atendimento_procedimento.especialidade_id', $id))
->when(request()->usuario_id, fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id))
->when(request()->procedimento_codigo, fn ($q, $codigo) => $q->where('procedimento.codigo', $codigo))
->whereNull('centro_custo.deleted_at')
->whereNull('infraestrutura.deleted_at')
->whereNull('usuario.deleted_at')
->whereNull('usuario_cidadao.deleted_at')
->selectRaw("
usuario_cidadao.nome as usuario_nome,
procedimento.codigo as procedimento_codigo,
procedimento.nome as procedimento_nome,
SUM(atendimento_procedimento.quantidade) as quantidade
")
->groupByRaw("
usuario_cidadao.nome,
procedimento.codigo,
procedimento.nome
")
)
, 'sub')
->groupByRaw("
sub.usuario_nome,
sub.procedimento_codigo,
sub.procedimento_nome
")
->selectRaw("
sub.usuario_nome,
sub.procedimento_codigo,
sub.procedimento_nome,
SUM(sub.quantidade) as quantidade
")
->get();
$procedimentos->each(function ($procedimento) use ($totalizadores) {
$procedimento->procedimento_descricao = MaskUtil::getProcedimentoMasked($procedimento->procedimento_codigo)
. " - {$procedimento->procedimento_nome}";
unset($procedimento->codigo, $procedimento->nome);
$totalizadores->quantidade += $procedimento->quantidade;
});
return compact('procedimentos', 'totalizadores');
}
public static function list()
{
$data = (object) [
'additionals' => [],
'form' => [],
'model' => [],
];
$params = self::getParams();
$sets = [
'periodo' => function () use ($data) {
$data->model['periodo'] = ['start' => null, 'end' => null];
},
'unidade' => function () use ($data, $params) {
$unidadesData = self::getUnidadesData($params);
$data->form['unidades'] = $unidadesData['form'];
$data->model['unidade_id'] = $unidadesData['model'];
},
'infraestrutura' => function () use ($data, $params) {
$infraestruturasData = self::getInfraestruturasData($params);
$data->form['infraestruturas'] = $infraestruturasData['form'];
$data->model['infraestrutura_id'] = $infraestruturasData['model'];
},
'ocupacao' => function () use ($data, $params) {
$ocupacoesData = self::getOcupacoesData($params);
$data->form['ocupacoes'] = $ocupacoesData['form'];
$data->model['ocupacao_id'] = $ocupacoesData['model'];
},
'especialidade' => function () use ($data, $params) {
$especialidadesData = self::getEspecialidadesData($params);
$data->form['especialidades'] = $especialidadesData['form'];
$data->model['especialidade_id'] = $especialidadesData['model'];
},
'usuario' => function () use ($data, $params) {
$usuariosData = self::getUsuariosData($params);
$data->form['usuarios'] = $usuariosData['form'];
$data->model['usuario_id'] = $usuariosData['model'];
},
'procedimento' => function () use ($data, $params) {
$procedimentosData = self::getProcedimentosData($params);
$data->form['procedimentos'] = $procedimentosData['form'];
$data->model['procedimento_codigo'] = $procedimentosData['model'];
},
];
$load = is_null(request()->load_from);
if ($load) {
$data->additionals = [
'data_atual' => Carbon::today()->format('d/m/Y')
];
}
foreach ($sets as $field => $set) {
if ($load) {
$set();
}
else if ($field === request()->load_from) {
$load = true;
}
}
foreach ($data as $property => $itens) {
if (empty($itens)) {
unset($data->{$property});
}
}
return $data;
}
private static function getParams()
{
$params = request()->only([
'periodo',
'unidade_id',
'infraestrutura_id',
'ocupacao_id',
'especialidade_id',
'usuario_id',
'procedimento_codigo',
]);
if (isset($params['periodo'])
&& !is_null($params['periodo']['start'])
&& !is_null($params['periodo']['end'])
) {
$start = Carbon::createFromFormat('d/m/Y', $params['periodo']['start'])->startOfDay();
$end = Carbon::createFromFormat('d/m/Y', $params['periodo']['end'])->endOfDay();
$params['periodo']['start'] = $start;
$params['periodo']['end'] = $end;
}
else {
$params['periodo'] = null;
}
return $params;
}
private static function getUnidadesData($params)
{
$unidades = collect();
$id = null;
if (!is_null($params['periodo'])) {
$queryAgendados = DB::table('cadastro_unidade as unidade')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.unidade_id', 'unidade.id')
->join('cadastro_lotacao as lotacao', 'lotacao.infraestrutura_id', 'infraestrutura.id')
->join('ambulatorio_jornada as jornada', 'jornada.lotacao_id', 'lotacao.id')
->join('ambulatorio_agendamento as agendamento', 'agendamento.jornada_id', 'jornada.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.agendamento_id', 'agendamento.id')
->leftJoin('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.atendimento_id', 'atendimento.id')
->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->whereBetween('jornada.data', $params['periodo'])
->where('soap.is_rascunho', false)
->whereNull('lotacao.deleted_at')
->whereNull('infraestrutura.deleted_at')
->whereNull('unidade.deleted_at')
->selectRaw("unidade.id, unidade.nome_fantasia");
$queryEspontanea = DB::table('cadastro_unidade as unidade')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.unidade_id', 'unidade.id')
->join('cadastro_centro_custo as centro_custo', 'centro_custo.infraestrutura_id', 'infraestrutura.id')
->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.created_centro_custo_id', 'centro_custo.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.demanda_espontanea_id', 'demanda_espontanea.id')
->leftJoin('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.atendimento_id', 'atendimento.id')
->join('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->whereBetween('demanda_espontanea.data', $params['periodo'])
->where('soap.is_rascunho', false)
->whereNull('centro_custo.deleted_at')
->whereNull('infraestrutura.deleted_at')
->whereNull('unidade.deleted_at')
->selectRaw("unidade.id, unidade.nome_fantasia");
$unidades = $queryAgendados
->union($queryEspontanea)
->distinct()
->orderBy('nome_fantasia')
->get();
}
return ['form' => $unidades, 'model' => $id];
}
private static function getInfraestruturasData($params)
{
$infraestruturas = collect();
$id = null;
if (!is_null($params['periodo']) && !is_null($params['unidade_id'])) {
$queryAgendados = DB::table('cadastro_infraestrutura as infraestrutura')
->join('cadastro_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id')
->join('cadastro_lotacao as lotacao', 'lotacao.infraestrutura_id', 'infraestrutura.id')
->join('ambulatorio_jornada as jornada', 'jornada.lotacao_id', 'lotacao.id')
->join('ambulatorio_agendamento as agendamento', 'agendamento.jornada_id', 'jornada.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.agendamento_id', 'agendamento.id')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.atendimento_id', 'atendimento.id')
->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->where('infraestrutura.unidade_id', $params['unidade_id'])
->whereBetween('jornada.data', $params['periodo'])
->where(function ($q) {
$q->where('soap.is_rascunho', false)
->orWhereNull('soap.id');
})
->whereNull('lotacao.deleted_at')
->whereNull('infraestrutura.deleted_at')
->whereNull('unidade.deleted_at')
->selectRaw("
infraestrutura.id,
COALESCE(infraestrutura.nome, unidade.nome_fantasia) as nome
");
$queryEspontanea = DB::table('cadastro_infraestrutura as infraestrutura')
->join('cadastro_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id')
->join('cadastro_centro_custo as centro_custo', 'centro_custo.infraestrutura_id', 'infraestrutura.id')
->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.created_centro_custo_id', 'centro_custo.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.demanda_espontanea_id', 'demanda_espontanea.id')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.atendimento_id', 'atendimento.id')
->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->where('infraestrutura.unidade_id', $params['unidade_id'])
->whereBetween('demanda_espontanea.data', $params['periodo'])
->where(function ($q) {
$q->where('soap.is_rascunho', false)
->orWhereNull('soap.id');
})
->whereNull('centro_custo.deleted_at')
->whereNull('infraestrutura.deleted_at')
->whereNull('unidade.deleted_at')
->selectRaw("
infraestrutura.id,
COALESCE(infraestrutura.nome, unidade.nome_fantasia) as nome
");
$infraestruturas = $queryAgendados
->union($queryEspontanea)
->distinct()
->orderBy('nome')
->get();
}
return ['form' => $infraestruturas, 'model' => $id];
}
private static function getOcupacoesData($params)
{
$ocupacoes = collect();
$id = null;
if (!is_null($params['periodo'])) {
$queryAgendados = DB::table('sigtap.ocupacao as ocupacao')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.ocupacao_id', 'ocupacao.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id')
->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id')
->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id')
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id')
->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->whereBetween('jornada.data', $params['periodo'])
->where(function ($q) {
$q->where('soap.is_rascunho', false)
->orWhereNull('soap.id');
})
->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id))
->whereNull('lotacao.deleted_at')
->whereNull('infraestrutura.deleted_at')
->selectRaw("
ocupacao.id,
CONCAT(ocupacao.codigo, ' - ', ocupacao.nome) as descricao,
ocupacao.codigo,
ocupacao.nome
");
$queryEspontanea = DB::table('sigtap.ocupacao as ocupacao')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.ocupacao_id', 'ocupacao.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id')
->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id')
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id')
->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->whereBetween('demanda_espontanea.data', $params['periodo'])
->where(function ($q) {
$q->where('soap.is_rascunho', false)
->orWhereNull('soap.id');
})
->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id))
->whereNull('centro_custo.deleted_at')
->whereNull('infraestrutura.deleted_at')
->selectRaw("
ocupacao.id,
CONCAT(ocupacao.codigo, ' - ', ocupacao.nome) as descricao,
ocupacao.codigo,
ocupacao.nome
");
$ocupacoes = $queryAgendados
->union($queryEspontanea)
->distinct()
->orderBy('codigo')
->orderBy('nome')
->get()
->map(function($item) {
return [
'id' => $item->id,
'descricao' => $item->descricao
];
});
}
return ['form' => $ocupacoes, 'model' => $id];
}
private static function getEspecialidadesData($params)
{
$especialidades = collect();
$id = null;
if (!is_null($params['periodo']) && !is_null($params['ocupacao_id'])) {
$queryAgendados = DB::table('cadastro_especialidade as especialidade')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.especialidade_id', 'especialidade.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id')
->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id')
->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id')
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id')
->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->where('especialidade.ocupacao_id', $params['ocupacao_id'])
->whereBetween('jornada.data', $params['periodo'])
->where(function ($q) {
$q->where('soap.is_rascunho', false)
->orWhereNull('soap.id');
})
->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id))
->whereNull('lotacao.deleted_at')
->whereNull('infraestrutura.deleted_at')
->selectRaw("especialidade.id, especialidade.nome");
$queryEspontanea = DB::table('cadastro_especialidade as especialidade')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.especialidade_id', 'especialidade.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id')
->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id')
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id')
->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->where('especialidade.ocupacao_id', $params['ocupacao_id'])
->whereBetween('demanda_espontanea.data', $params['periodo'])
->where(function ($q) {
$q->where('soap.is_rascunho', false)
->orWhereNull('soap.id');
})
->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id))
->whereNull('centro_custo.deleted_at')
->whereNull('infraestrutura.deleted_at')
->selectRaw("especialidade.id, especialidade.nome");
$especialidades = $queryAgendados
->union($queryEspontanea)
->distinct()
->orderBy('nome')
->get();
}
return ['form' => $especialidades, 'model' => $id];
}
private static function getUsuariosData($params)
{
$usuarios = collect();
$id = null;
if (!is_null($params['periodo'])) {
$queryAgendados = DB::table('cadastro_usuario as usuario')
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'usuario.cidadao_id')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.usuario_id', 'usuario.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id')
->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id')
->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id')
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id')
->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->whereBetween('jornada.data', $params['periodo'])
->where(function ($q) {
$q->where('soap.is_rascunho', false)
->orWhereNull('soap.id');
})
->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id))
->when($params['ocupacao_id'], fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id))
->when($params['especialidade_id'], fn ($q, $id) => $q->where('atendimento_procedimento.especialidade_id', $id))
->whereNull('lotacao.deleted_at')
->whereNull('infraestrutura.deleted_at')
->whereNull('usuario.deleted_at')
->whereNull('cidadao.deleted_at')
->selectRaw("usuario.id, cidadao.nome");
$queryEspontanea = DB::table('cadastro_usuario as usuario')
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'usuario.cidadao_id')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.usuario_id', 'usuario.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id')
->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id')
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id')
->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->whereBetween('demanda_espontanea.data', $params['periodo'])
->where(function ($q) {
$q->where('soap.is_rascunho', false)
->orWhereNull('soap.id');
})
->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id))
->when($params['ocupacao_id'], fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id))
->when($params['especialidade_id'], fn ($q, $id) => $q->where('atendimento_procedimento.especialidade_id', $id))
->whereNull('centro_custo.deleted_at')
->whereNull('infraestrutura.deleted_at')
->whereNull('usuario.deleted_at')
->whereNull('cidadao.deleted_at')
->selectRaw("usuario.id, cidadao.nome");
$usuarios = $queryAgendados
->union($queryEspontanea)
->distinct()
->orderBy('nome')
->get();
}
return ['form' => $usuarios, 'model' => $id];
}
private static function getProcedimentosData($params)
{
$procedimentos = collect();
$codigo = null;
if (!is_null($params['periodo'])) {
$queryAgendados = DB::table('sigtap.procedimento as procedimento')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id')
->join('ambulatorio_agendamento as agendamento', 'agendamento.id', 'atendimento.agendamento_id')
->join('ambulatorio_jornada as jornada', 'jornada.id', 'agendamento.jornada_id')
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'jornada.lotacao_id')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id')
->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->whereBetween('jornada.data', $params['periodo'])
->where(function ($q) {
$q->where('soap.is_rascunho', false)
->orWhereNull('soap.id');
})
->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('lotacao.infraestrutura_id', $id))
->when($params['ocupacao_id'], fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id))
->when($params['especialidade_id'], fn ($q, $id) => $q->where('atendimento_procedimento.especialidade_id', $id))
->when($params['usuario_id'], fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id))
->whereNull('lotacao.deleted_at')
->whereNull('infraestrutura.deleted_at')
->select('procedimento.codigo', 'procedimento.nome');
$queryEspontanea = DB::table('sigtap.procedimento as procedimento')
->join('ambulatorio_atendimento_procedimento as atendimento_procedimento', 'atendimento_procedimento.procedimento_id', 'procedimento.id')
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'atendimento_procedimento.atendimento_id')
->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id')
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'demanda_espontanea.created_centro_custo_id')
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id')
->leftJoin('ambulatorio_atendimento_soap as soap', 'soap.id', 'atendimento_procedimento.soap_id')
->whereBetween('demanda_espontanea.data', $params['periodo'])
->where(function ($q) {
$q->where('soap.is_rascunho', false)
->orWhereNull('soap.id');
})
->when($params['unidade_id'], fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
->when($params['infraestrutura_id'], fn ($q, $id) => $q->where('centro_custo.infraestrutura_id', $id))
->when($params['ocupacao_id'], fn ($q, $id) => $q->where('atendimento_procedimento.ocupacao_id', $id))
->when($params['especialidade_id'], fn ($q, $id) => $q->where('atendimento_procedimento.especialidade_id', $id))
->when($params['usuario_id'], fn ($q, $id) => $q->where('atendimento_procedimento.usuario_id', $id))
->whereNull('centro_custo.deleted_at')
->whereNull('infraestrutura.deleted_at')
->select('procedimento.codigo', 'procedimento.nome');
$procedimentos = $queryAgendados
->union($queryEspontanea)
->distinct()
->orderBy('codigo')
->orderBy('nome')
->get();
$procedimentos->each(function ($procedimento) {
$procedimento->descricao = MaskUtil::getProcedimentoMasked($procedimento->codigo)
. " - {$procedimento->nome}";
unset($procedimento->nome);
});
}
return ['form' => $procedimentos, 'model' => $codigo];
}
}