This commit is contained in:
@@ -0,0 +1,639 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ambulatorio;
|
||||
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Enums\Ambulatorio\AgendamentoStatus;
|
||||
class RelatorioQuantitativoAtendimentoService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$totalizadores = (object) [
|
||||
'quantidade' => 0,
|
||||
];
|
||||
|
||||
self::setDays($totalizadores);
|
||||
|
||||
$periodo = [
|
||||
'start' => Carbon::create(request()->periodo['ano'], request()->periodo['mes'])
|
||||
->modify('first day of this month')
|
||||
->startOfDay(),
|
||||
'end' => Carbon::create(request()->periodo['ano'], request()->periodo['mes'])
|
||||
->modify('last day of this month')
|
||||
->endOfDay()
|
||||
];
|
||||
|
||||
$infraestruturas = collect();
|
||||
|
||||
DB::query()
|
||||
->fromSub(DB::table('ambulatorio_atendimento as atendimento')
|
||||
->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_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id')
|
||||
->join('cadastro_usuario as usuario', 'usuario.id', 'lotacao.usuario_id')
|
||||
->join('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->where('agendamento.status_id', AgendamentoStatus::REALIZADO)
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.atendimento_id', 'atendimento.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('lotacao.ocupacao_id', $id))
|
||||
->when(request()->especialidade_id, fn ($q, $id) => $q->where('jornada.especialidade_id', $id))
|
||||
->whereNull('lotacao.deleted_at')
|
||||
->whereNull('infraestrutura.deleted_at')
|
||||
->whereNull('unidade.deleted_at')
|
||||
->whereNull('usuario.deleted_at')
|
||||
->whereNull('usuario_cidadao.deleted_at')
|
||||
->selectRaw("
|
||||
atendimento.id as id,
|
||||
LPAD(EXTRACT(DAY FROM jornada.data), 2, '0') as dia,
|
||||
unidade.nome_fantasia as unidade_nome_fantasia,
|
||||
infraestrutura.id as infraestrutura_id,
|
||||
COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome,
|
||||
usuario.id as usuario_id,
|
||||
usuario_cidadao.nome as usuario_nome
|
||||
")
|
||||
->unionAll(DB::table('ambulatorio_atendimento as atendimento')
|
||||
->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_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id')
|
||||
->join('cadastro_usuario as usuario', 'usuario.id', 'demanda_espontanea.usuario_id')
|
||||
->join('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_id')
|
||||
->join('cadastro_lotacao as lotacao', function ($join) {
|
||||
$join->on('lotacao.usuario_id', '=', 'usuario.id')
|
||||
->on('lotacao.infraestrutura_id', '=', 'infraestrutura.id');
|
||||
})
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->whereColumn('soap.atendimento_id', 'atendimento.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('demanda_espontanea.ocupacao_id', $id))
|
||||
->when(request()->especialidade_id, fn ($q, $id) => $q->where('demanda_espontanea.especialidade_id', $id))
|
||||
->whereNull('centro_custo.deleted_at')
|
||||
->whereNull('infraestrutura.deleted_at')
|
||||
->whereNull('unidade.deleted_at')
|
||||
->whereNull('usuario.deleted_at')
|
||||
->whereNull('usuario_cidadao.deleted_at')
|
||||
->selectRaw("
|
||||
atendimento.id as id,
|
||||
LPAD(EXTRACT(DAY FROM demanda_espontanea.data), 2, '0') as dia,
|
||||
unidade.nome_fantasia as unidade_nome_fantasia,
|
||||
infraestrutura.id as infraestrutura_id,
|
||||
COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome,
|
||||
usuario.id as usuario_id,
|
||||
usuario_cidadao.nome as usuario_nome
|
||||
")
|
||||
->distinct()
|
||||
)
|
||||
->when(is_null(request()->especialidade_id), fn ($q) => $q
|
||||
->unionAll(DB::table('ambulatorio_atendimento_pre_atendimento as pre_atendimento')
|
||||
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'pre_atendimento.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', 'pre_atendimento.created_lotacao_id')
|
||||
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id')
|
||||
->join('cadastro_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id')
|
||||
->join('cadastro_usuario as usuario', 'usuario.id', 'lotacao.usuario_id')
|
||||
->join('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->where('agendamento.status_id', AgendamentoStatus::REALIZADO)
|
||||
->whereBetween('jornada.data', $periodo)
|
||||
->where('usuario.id', 'lotacao.usuario_id')
|
||||
->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('lotacao.ocupacao_id', $id))
|
||||
->whereNull('lotacao.deleted_at')
|
||||
->whereNull('infraestrutura.deleted_at')
|
||||
->whereNull('unidade.deleted_at')
|
||||
->whereNull('usuario.deleted_at')
|
||||
->whereNull('usuario_cidadao.deleted_at')
|
||||
->selectRaw("
|
||||
atendimento.id as id,
|
||||
LPAD(EXTRACT(DAY FROM jornada.data), 2, '0') as dia,
|
||||
unidade.nome_fantasia as unidade_nome_fantasia,
|
||||
infraestrutura.id as infraestrutura_id,
|
||||
COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome,
|
||||
usuario.id as usuario_id,
|
||||
usuario_cidadao.nome as usuario_nome
|
||||
")
|
||||
)
|
||||
->unionAll(DB::table('ambulatorio_atendimento_pre_atendimento as pre_atendimento')
|
||||
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'pre_atendimento.atendimento_id')
|
||||
->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id')
|
||||
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'pre_atendimento.created_lotacao_id')
|
||||
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id')
|
||||
->join('cadastro_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id')
|
||||
->join('cadastro_usuario as usuario', 'usuario.id', 'lotacao.usuario_id')
|
||||
->join('cadastro_cidadao as usuario_cidadao', 'usuario_cidadao.id', 'usuario.cidadao_id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->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('lotacao.infraestrutura_id', $id))
|
||||
->when(request()->ocupacao_id, fn ($q, $id) => $q->where('lotacao.ocupacao_id', $id))
|
||||
->whereNull('lotacao.deleted_at')
|
||||
->whereNull('infraestrutura.deleted_at')
|
||||
->whereNull('unidade.deleted_at')
|
||||
->whereNull('usuario.deleted_at')
|
||||
->whereNull('usuario_cidadao.deleted_at')
|
||||
->selectRaw("
|
||||
atendimento.id as id,
|
||||
LPAD(EXTRACT(DAY FROM demanda_espontanea.data), 2, '0') as dia,
|
||||
unidade.nome_fantasia as unidade_nome_fantasia,
|
||||
infraestrutura.id as infraestrutura_id,
|
||||
COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura_nome,
|
||||
usuario.id as usuario_id,
|
||||
usuario_cidadao.nome as usuario_nome
|
||||
")
|
||||
)
|
||||
)
|
||||
, 'sub')
|
||||
->groupByRaw("
|
||||
sub.dia,
|
||||
sub.unidade_nome_fantasia,
|
||||
sub.infraestrutura_id,
|
||||
sub.infraestrutura_nome,
|
||||
sub.usuario_id,
|
||||
sub.usuario_nome
|
||||
")
|
||||
->selectRaw("
|
||||
sub.dia,
|
||||
sub.unidade_nome_fantasia,
|
||||
sub.infraestrutura_id,
|
||||
sub.infraestrutura_nome,
|
||||
sub.usuario_id,
|
||||
sub.usuario_nome,
|
||||
COUNT(*) as quantidade
|
||||
")
|
||||
->get()
|
||||
->each(function ($item) use ($infraestruturas, $totalizadores) {
|
||||
|
||||
$infraestrutura = $infraestruturas->where('infraestrutura_id', $item->infraestrutura_id)->first();
|
||||
|
||||
if (is_null($infraestrutura)) {
|
||||
|
||||
$infraestrutura = (object) [
|
||||
'infraestrutura_id' => $item->infraestrutura_id,
|
||||
'infraestrutura_nome' => $item->infraestrutura_nome,
|
||||
'unidade_nome_fantasia' => $item->unidade_nome_fantasia,
|
||||
'usuarios' => collect(),
|
||||
'quantidade' => 0,
|
||||
];
|
||||
|
||||
self::setDays($infraestrutura);
|
||||
|
||||
$infraestruturas->push($infraestrutura);
|
||||
}
|
||||
|
||||
$infraestrutura->{"quantidade{$item->dia}"} += $item->quantidade;
|
||||
$infraestrutura->quantidade += $item->quantidade;
|
||||
|
||||
$usuario = $infraestrutura->usuarios->where('usuario_id', $item->usuario_id)->first();
|
||||
|
||||
if (is_null($usuario)) {
|
||||
|
||||
$usuario = (object) [
|
||||
'usuario_id' => $item->usuario_id,
|
||||
'usuario_nome' => $item->usuario_nome,
|
||||
'quantidade' => 0,
|
||||
];
|
||||
|
||||
self::setDays($usuario);
|
||||
|
||||
$infraestrutura->usuarios->push($usuario);
|
||||
}
|
||||
|
||||
$usuario->{"quantidade{$item->dia}"} = $item->quantidade;
|
||||
$usuario->quantidade += $item->quantidade;
|
||||
|
||||
$totalizadores->{"quantidade{$item->dia}"} += $item->quantidade;
|
||||
$totalizadores->quantidade += $item->quantidade;
|
||||
});
|
||||
|
||||
return self::setZeroToHypen(compact('infraestruturas', 'totalizadores'));
|
||||
}
|
||||
|
||||
private static function setDays($data)
|
||||
{
|
||||
$date = Carbon::create(request()->periodo['ano'], request()->periodo['mes']);
|
||||
$month = $date->month;
|
||||
$lastDay = $date->modify('last day of this month')->day;
|
||||
|
||||
$date->modify('first day of this month');
|
||||
|
||||
while ($date->month === $month && $date->day <= $lastDay) {
|
||||
|
||||
$currentDay = Str::padLeft($date->day, 2, '0');
|
||||
|
||||
$data->{"quantidade{$currentDay}"} = 0;
|
||||
|
||||
$date->addDay();
|
||||
}
|
||||
}
|
||||
|
||||
private static function setZeroToHypen($data)
|
||||
{
|
||||
$setEmpty = function ($item) {
|
||||
|
||||
for ($i = 1; $i <= 31; $i++) {
|
||||
|
||||
$key = 'quantidade' . Str::padLeft($i, 2, '0');
|
||||
|
||||
if (isset($item->{$key}) && $item->{$key} === 0) {
|
||||
|
||||
$item->{$key} = '-';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
foreach ($data['infraestruturas'] as $infraestrutura) {
|
||||
|
||||
$setEmpty($infraestrutura);
|
||||
|
||||
foreach ($infraestrutura->usuarios as $usuario) {
|
||||
|
||||
$setEmpty($usuario);
|
||||
}
|
||||
}
|
||||
|
||||
$setEmpty($data['totalizadores']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$data = (object) [
|
||||
'additionals' => [],
|
||||
'form' => [],
|
||||
'model' => [],
|
||||
];
|
||||
|
||||
$params = self::getParams();
|
||||
|
||||
$sets = [
|
||||
'periodo' => function () use ($data) {
|
||||
|
||||
$data->form['meses'] = self::getMeses();
|
||||
|
||||
$data->model['periodo'] = ['ano' => null, 'mes' => 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'];
|
||||
},
|
||||
];
|
||||
|
||||
$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',
|
||||
]);
|
||||
|
||||
if (isset($params['periodo'])
|
||||
&& !is_null($params['periodo']['ano'])
|
||||
&& !is_null($params['periodo']['mes'])
|
||||
) {
|
||||
|
||||
$start = Carbon::create($params['periodo']['ano'], $params['periodo']['mes'])
|
||||
->modify('first day of this month')
|
||||
->startOfDay();
|
||||
|
||||
$end = Carbon::create($params['periodo']['ano'], $params['periodo']['mes'])
|
||||
->modify('last day of this month')
|
||||
->endOfDay();
|
||||
|
||||
$params['periodo'] = ['start' => $start, 'end' => $end];
|
||||
}
|
||||
else {
|
||||
|
||||
$params['periodo'] = null;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
public static function getMeses()
|
||||
{
|
||||
return collect([
|
||||
['value' => 1, 'text' => 'JANEIRO'],
|
||||
['value' => 2, 'text' => 'FEVEREIRO'],
|
||||
['value' => 3, 'text' => 'MARÇO'],
|
||||
['value' => 4, 'text' => 'ABRIL'],
|
||||
['value' => 5, 'text' => 'MAIO'],
|
||||
['value' => 6, 'text' => 'JUNHO'],
|
||||
['value' => 7, 'text' => 'JULHO'],
|
||||
['value' => 8, 'text' => 'AGOSTO'],
|
||||
['value' => 9, 'text' => 'SETEMBRO'],
|
||||
['value' => 10, 'text' => 'OUTUBRO'],
|
||||
['value' => 11, 'text' => 'NOVEMBRO'],
|
||||
['value' => 12, 'text' => 'DEZEMBRO'],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function getUnidadesData($params)
|
||||
{
|
||||
$unidades = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params['periodo'])) {
|
||||
|
||||
$unidades = DB::table('cadastro_unidade as unidade')
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('cadastro_infraestrutura as infraestrutura')
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento as atendimento')
|
||||
->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')
|
||||
->whereColumn('lotacao.infraestrutura_id', 'infraestrutura.id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereBetween('jornada.data', $params['periodo'])
|
||||
->whereNull('lotacao.deleted_at')
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento as atendimento')
|
||||
->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')
|
||||
->whereColumn('centro_custo.infraestrutura_id', 'infraestrutura.id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereBetween('demanda_espontanea.data', $params['periodo'])
|
||||
->whereNull('centro_custo.deleted_at')
|
||||
)
|
||||
)
|
||||
->whereColumn('infraestrutura.unidade_id', 'unidade.id')
|
||||
->whereNull('infraestrutura.deleted_at')
|
||||
)
|
||||
->whereNull('unidade.deleted_at')
|
||||
->orderBy('unidade.nome_fantasia')
|
||||
->selectRaw("
|
||||
unidade.id,
|
||||
unidade.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'])) {
|
||||
|
||||
$infraestruturas = DB::table('cadastro_infraestrutura as infraestrutura')
|
||||
->join('cadastro_unidade as unidade', 'unidade.id', 'infraestrutura.unidade_id')
|
||||
->where('infraestrutura.unidade_id', $params['unidade_id'])
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento as atendimento')
|
||||
->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')
|
||||
->whereColumn('lotacao.infraestrutura_id', 'infraestrutura.id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereBetween('jornada.data', $params['periodo'])
|
||||
->whereNull('lotacao.deleted_at')
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento as atendimento')
|
||||
->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')
|
||||
->whereColumn('centro_custo.infraestrutura_id', 'infraestrutura.id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereBetween('demanda_espontanea.data', $params['periodo'])
|
||||
->whereNull('centro_custo.deleted_at')
|
||||
)
|
||||
)
|
||||
->whereNull('infraestrutura.deleted_at')
|
||||
->whereNull('unidade.deleted_at')
|
||||
->orderByRaw("COALESCE(infraestrutura.nome, unidade.nome_fantasia)")
|
||||
->selectRaw("
|
||||
infraestrutura.id,
|
||||
COALESCE(infraestrutura.nome, unidade.nome_fantasia) as nome
|
||||
")
|
||||
->get();
|
||||
}
|
||||
|
||||
return ['form' => $infraestruturas, 'model' => $id];
|
||||
}
|
||||
|
||||
private static function getOcupacoesData($params)
|
||||
{
|
||||
$ocupacoes = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params['periodo'])) {
|
||||
|
||||
$ocupacoes = DB::table('sigtap.ocupacao as ocupacao')
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_pre_atendimento as pre_atendimento')
|
||||
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'pre_atendimento.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', 'pre_atendimento.created_lotacao_id')
|
||||
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id')
|
||||
->whereColumn('lotacao.ocupacao_id', 'ocupacao.id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereBetween('jornada.data', $params['periodo'])
|
||||
->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')
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_pre_atendimento as pre_atendimento')
|
||||
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'pre_atendimento.atendimento_id')
|
||||
->join('ambulatorio_demanda_espontanea as demanda_espontanea', 'demanda_espontanea.id', 'atendimento.demanda_espontanea_id')
|
||||
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'pre_atendimento.created_lotacao_id')
|
||||
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', 'lotacao.infraestrutura_id')
|
||||
->whereColumn('lotacao.ocupacao_id', 'ocupacao.id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereBetween('demanda_espontanea.data', $params['periodo'])
|
||||
->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')
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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')
|
||||
->whereColumn('lotacao.ocupacao_id', 'ocupacao.id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereBetween('jornada.data', $params['periodo'])
|
||||
->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')
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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')
|
||||
->whereColumn('demanda_espontanea.ocupacao_id', 'ocupacao.id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereBetween('demanda_espontanea.data', $params['periodo'])
|
||||
->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')
|
||||
)
|
||||
)
|
||||
->orderByRaw("CONCAT(ocupacao.codigo, ' - ', ocupacao.nome)")
|
||||
->selectRaw("
|
||||
ocupacao.id,
|
||||
CONCAT(ocupacao.codigo, ' - ', ocupacao.nome) as descricao
|
||||
")
|
||||
->get();
|
||||
}
|
||||
|
||||
return ['form' => $ocupacoes, 'model' => $id];
|
||||
}
|
||||
|
||||
private static function getEspecialidadesData($params)
|
||||
{
|
||||
$especialidades = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params['periodo']) && !is_null($params['ocupacao_id'])) {
|
||||
|
||||
$especialidades = DB::table('cadastro_especialidade as especialidade')
|
||||
->where('especialidade.ocupacao_id', $params['ocupacao_id'])
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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')
|
||||
->whereColumn('jornada.especialidade_id', 'especialidade.id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereBetween('jornada.data', $params['periodo'])
|
||||
->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')
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('ambulatorio_atendimento_soap as soap')
|
||||
->join('ambulatorio_atendimento as atendimento', 'atendimento.id', 'soap.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')
|
||||
->whereColumn('demanda_espontanea.especialidade_id', 'especialidade.id')
|
||||
->whereNotNull('atendimento.finalizado_em')
|
||||
->whereBetween('demanda_espontanea.data', $params['periodo'])
|
||||
->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')
|
||||
)
|
||||
)
|
||||
->orderBy('especialidade.nome')
|
||||
->selectRaw("
|
||||
especialidade.id,
|
||||
especialidade.nome
|
||||
")
|
||||
->get();
|
||||
}
|
||||
|
||||
return ['form' => $especialidades, 'model' => $id];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user