Files
rkm-conectasus-ci-sandbox/api/app/Services/ProntoAtendimento/Plantao/RelatorioPlantaoService.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

668 lines
31 KiB
PHP

<?php
namespace App\Services\ProntoAtendimento\Plantao;
use App\Enums\Cadastro\Estabelecimento\Servico;
use App\Services\Service;
use Carbon\Carbon;
use DateInterval;
use DateTime;
use Illuminate\Support\Facades\DB;
class RelatorioPlantaoService extends Service
{
public static function index()
{
$params = request()->all();
$plantoesBuilder = self::getPlantoesBuilder();
if (!$plantoesBuilder) {
return [];
}
$plantoesBuilder = $plantoesBuilder
->select(
'pronto_atendimento_plantao.id',
'pronto_atendimento_plantao.inicio',
'pronto_atendimento_plantao.fim',
'pronto_atendimento_plantao.servico_id',
'pronto_atendimento_plantao.lotacao_id',
'cadastro_unidade.nome_fantasia',
'cadastro_infraestrutura.nome as local_atendimento',
'cadastro_infraestrutura.principal as infra_principal',
'sigtap.ocupacao.nome as especialidade',
'cadastro_usuario.id as profissional_id',
'cadastro_cidadao.nome as profissional',
);
return $plantoesBuilder
->orderBy('pronto_atendimento_plantao.inicio')
->get()
->map(function ($plantao) use ($plantoesBuilder, $params) {
$localAtendimento = $plantao->infra_principal ? $plantao->nome_fantasia : $plantao->local_atendimento;
$start = Carbon::createFromFormat('Y-m-d H:i:s', $plantao->inicio);
$end = Carbon::createFromFormat('Y-m-d H:i:s', $plantao->fim);
$plantoes = $plantoesBuilder->get();
$horasTrabalhadas = self::getHorasTrabalhadas($params, $plantoes, $plantao->id);
$horasPeriodo = self::getStringHorasFromDiff($params, $start->diff($end));
$horasTotal = self::getHorasTotal($params, $plantoes, $plantao->profissional_id);
$primeiroAtendimento = self::getPrimeiroAtendimento($plantao);
$ultimoAtendimento = self::getUltimoAtendimento($plantao);
$ultimoAtendimento = self::getUltimoAtendimento($plantao);
$quantidadeAtendimentos = self::getQuantidadeAtendimentos($plantao);
$totalAtendimentos = self::getTotalAtendimentos($params, $plantao);
return [
'start' => $start->format('d/m/Y H:i:s'),
'end' => $end->format('d/m/Y H:i:s'),
'nome_fantasia' => $plantao->nome_fantasia,
'local_atendimento' => $localAtendimento,
'especialidade' => $plantao->especialidade,
'profissional' => $plantao->profissional,
'horas_periodo' => $horasPeriodo,
'horas_trabalhadas' => $horasTrabalhadas,
'horas_total' => $horasTotal,
'hora_primeiro_atendimento' => $primeiroAtendimento->horario ?? '-',
'hora_ultimo_atendimento' => $ultimoAtendimento->horario ?? '-',
'quantidade_atendimento' => $quantidadeAtendimentos,
'total_atendimento' => $totalAtendimentos,
];
});
}
public static function getPrimeiroAtendimento($plantao) {
if ($plantao->servico_id == Servico::CONSULTA_MEDICA) {
return DB::table('pronto_atendimento_atendimento_medico')
->select(DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario'))
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_atendimento_medico.atendimento_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_atendimento_medico.created_lotacao_id')
->on('painel_senha_fila_historico.centro_custo_id', '=', 'pronto_atendimento_atendimento_medico.created_centro_custo_id');
})
->where('pronto_atendimento_atendimento_medico.plantao_id', $plantao->id)
->orderBy('painel_senha_fila_historico.created_at', 'asc')
->first();
} else if ($plantao->servico_id == Servico::ENFERMAGEM) {
$consultaEnfermagem = DB::table('pronto_atendimento_atendimento_enfermagem')
->select(DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario'))
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_atendimento_enfermagem.atendimento_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_atendimento_enfermagem.created_lotacao_id')
->on('painel_senha_fila_historico.centro_custo_id', '=', 'pronto_atendimento_atendimento_enfermagem.created_centro_custo_id');
})
->where('plantao_id', $plantao->id);
$consultaTriagem = DB::table('pronto_atendimento_atendimento_triagem')
->select(DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario'))
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_atendimento_triagem.atendimento_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_atendimento_triagem.created_lotacao_id')
->on('painel_senha_fila_historico.centro_custo_id', '=', 'pronto_atendimento_atendimento_triagem.created_centro_custo_id');
})
->where('plantao_id', $plantao->id);
$consultaObservacao = DB::table('pronto_atendimento_paciente_observacao')
->select(DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario'))
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_paciente_observacao.atendimento_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_paciente_observacao.created_lotacao_id')
->on('painel_senha_fila_historico.centro_custo_id', '=', 'pronto_atendimento_paciente_observacao.created_centro_custo_id');
})
->where('plantao_id', $plantao->id);
return $consultaEnfermagem
->union($consultaTriagem)
->union($consultaObservacao)
->orderBy('horario', 'asc')
->first();
} else if ($plantao->servico_id == Servico::EXAME_RAIO_X) {
return DB::table('pronto_atendimento_pedido_exame')
->select(
DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario')
)
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_pedido_exame.atendimento_id')
->join('pronto_atendimento_plantao', 'pronto_atendimento_plantao.id', 'pronto_atendimento_pedido_exame.plantao_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_plantao.lotacao_id');
})
->where('pronto_atendimento_pedido_exame.plantao_id', $plantao->id)
->orderBy('painel_senha_fila_historico.created_at', 'asc')
->first();
} else if ($plantao->servico_id == Servico::NAO_SE_APLICA) {
return DB::table('pronto_atendimento_atendimento_recepcao')
->select(DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario'))
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_atendimento_recepcao.atendimento_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_atendimento_recepcao.created_lotacao_id')
->on('painel_senha_fila_historico.centro_custo_id', '=', 'pronto_atendimento_atendimento_recepcao.created_centro_custo_id');
})
->where('plantao_id', $plantao->id)
->orderBy('painel_senha_fila_historico.created_at', 'asc')
->first();
}
}
public static function getUltimoAtendimento($plantao) {
if ($plantao->servico_id == Servico::CONSULTA_MEDICA) {
return DB::table('pronto_atendimento_atendimento_medico')
->select(DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario'))
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_atendimento_medico.atendimento_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_atendimento_medico.created_lotacao_id')
->on('painel_senha_fila_historico.centro_custo_id', '=', 'pronto_atendimento_atendimento_medico.created_centro_custo_id');
})
->where('pronto_atendimento_atendimento_medico.plantao_id', $plantao->id)
->orderBy('painel_senha_fila_historico.created_at', 'desc')
->first();
} else if ($plantao->servico_id == Servico::ENFERMAGEM) {
$consultaEnfermagem = DB::table('pronto_atendimento_atendimento_enfermagem')
->select(DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario'))
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_atendimento_enfermagem.atendimento_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_atendimento_enfermagem.created_lotacao_id')
->on('painel_senha_fila_historico.centro_custo_id', '=', 'pronto_atendimento_atendimento_enfermagem.created_centro_custo_id');
})
->where('plantao_id', $plantao->id);
$consultaTriagem = DB::table('pronto_atendimento_atendimento_triagem')
->select(DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario'))
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_atendimento_triagem.atendimento_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_atendimento_triagem.created_lotacao_id')
->on('painel_senha_fila_historico.centro_custo_id', '=', 'pronto_atendimento_atendimento_triagem.created_centro_custo_id');
})
->where('plantao_id', $plantao->id);
$consultaObservacao = DB::table('pronto_atendimento_paciente_observacao')
->select(DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario'))
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_paciente_observacao.atendimento_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_paciente_observacao.created_lotacao_id')
->on('painel_senha_fila_historico.centro_custo_id', '=', 'pronto_atendimento_paciente_observacao.created_centro_custo_id');
})
->where('plantao_id', $plantao->id);
return $consultaEnfermagem
->union($consultaTriagem)
->union($consultaObservacao)
->orderBy('horario', 'desc')
->first();
} else if ($plantao->servico_id == Servico::EXAME_RAIO_X) {
return DB::table('pronto_atendimento_pedido_exame')
->select(
DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario')
)
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_pedido_exame.atendimento_id')
->join('pronto_atendimento_plantao', 'pronto_atendimento_plantao.id', 'pronto_atendimento_pedido_exame.plantao_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_plantao.lotacao_id');
})
->where('pronto_atendimento_pedido_exame.plantao_id', $plantao->id)
->orderBy('painel_senha_fila_historico.created_at', 'desc')
->first();
} else if ($plantao->servico_id == Servico::NAO_SE_APLICA) {
return DB::table('pronto_atendimento_atendimento_recepcao')
->select(DB::raw('DATE_FORMAT(painel_senha_fila_historico.created_at, "%d/%m/%Y %H:%i:%s") as horario'))
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_atendimento_recepcao.atendimento_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_atendimento_recepcao.created_lotacao_id')
->on('painel_senha_fila_historico.centro_custo_id', '=', 'pronto_atendimento_atendimento_recepcao.created_centro_custo_id');
})
->where('plantao_id', $plantao->id)
->orderBy('painel_senha_fila_historico.created_at', 'desc')
->first();
}
}
public static function getQuantidadeAtendimentos($plantao) {
if ($plantao->servico_id == Servico::CONSULTA_MEDICA) {
return DB::table('pronto_atendimento_atendimento_medico')
->where('pronto_atendimento_atendimento_medico.plantao_id', $plantao->id)
->count();
} else if ($plantao->servico_id == Servico::ENFERMAGEM) {
$consultaEnfermagem = DB::table('pronto_atendimento_atendimento_enfermagem')
->where('plantao_id', $plantao->id);
$consultaTriagem = DB::table('pronto_atendimento_atendimento_triagem')
->where('plantao_id', $plantao->id);
$consultaObservacao = DB::table('pronto_atendimento_paciente_observacao')
->where('plantao_id', $plantao->id);
return $consultaEnfermagem->count() + $consultaTriagem->count() + $consultaObservacao->count();
} else if ($plantao->servico_id == Servico::EXAME_RAIO_X) {
return DB::table('pronto_atendimento_pedido_exame')
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_pedido_exame.atendimento_id')
->join('pronto_atendimento_plantao', 'pronto_atendimento_plantao.id', 'pronto_atendimento_pedido_exame.plantao_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_plantao.lotacao_id');
})
->where('pronto_atendimento_pedido_exame.plantao_id', $plantao->id)
->count();
} else if ($plantao->servico_id == Servico::NAO_SE_APLICA) {
return DB::table('pronto_atendimento_atendimento_recepcao')
->where('plantao_id', $plantao->id)
->count();
}
}
public static function getTotalAtendimentos($params, $plantao) {
$startEndDateTimeInCarbon = self::getStartEndDateTimeInCarbon($params);
$start = $startEndDateTimeInCarbon['start'];
$end = $startEndDateTimeInCarbon['end'];
if ($plantao->servico_id == Servico::CONSULTA_MEDICA) {
return DB::table('pronto_atendimento_atendimento_medico')
->whereBetween('pronto_atendimento_atendimento_medico.created_at', [$start, $end])
->where('pronto_atendimento_atendimento_medico.created_lotacao_id', $plantao->lotacao_id)
->count();
} else if ($plantao->servico_id == Servico::ENFERMAGEM) {
$consultaEnfermagem = DB::table('pronto_atendimento_atendimento_enfermagem')
->whereBetween('pronto_atendimento_atendimento_enfermagem.created_at', [$start, $end])
->where('pronto_atendimento_atendimento_enfermagem.created_lotacao_id', $plantao->lotacao_id);
$consultaTriagem = DB::table('pronto_atendimento_atendimento_triagem')
->whereBetween('pronto_atendimento_atendimento_triagem.created_at', [$start, $end])
->where('pronto_atendimento_atendimento_triagem.created_lotacao_id', $plantao->lotacao_id);
$consultaObservacao = DB::table('pronto_atendimento_paciente_observacao')
->whereBetween('pronto_atendimento_paciente_observacao.created_at', [$start, $end])
->where('pronto_atendimento_paciente_observacao.created_lotacao_id', $plantao->lotacao_id);
return $consultaEnfermagem->count() + $consultaTriagem->count() + $consultaObservacao->count();
} else if ($plantao->servico_id == Servico::EXAME_RAIO_X) {
return DB::table('pronto_atendimento_pedido_exame')
->join('pronto_atendimento_atendimento', 'pronto_atendimento_atendimento.id', 'pronto_atendimento_pedido_exame.atendimento_id')
->join('pronto_atendimento_plantao', 'pronto_atendimento_plantao.id', 'pronto_atendimento_pedido_exame.plantao_id')
->join('painel_senha_fila_historico', function ($join) {
$join->on('painel_senha_fila_historico.fila_id', '=', 'pronto_atendimento_atendimento.fila_id')
->on('painel_senha_fila_historico.created_lotacao_id', '=', 'pronto_atendimento_plantao.lotacao_id');
})
->whereBetween('painel_senha_fila_historico.created_at', [$start, $end])
->where('painel_senha_fila_historico.created_lotacao_id', $plantao->lotacao_id)
->count();
} else if ($plantao->servico_id == Servico::NAO_SE_APLICA) {
return DB::table('pronto_atendimento_atendimento_recepcao')
->whereBetween('pronto_atendimento_atendimento_recepcao.created_at', [$start, $end])
->where('pronto_atendimento_atendimento_recepcao.created_lotacao_id', $plantao->lotacao_id)
->count();
}
}
public static function getHorarioFim($plantao) {
return DB::table('pronto_atendimento_atendimento_recepcao')
->select([
'atendido_em',
])
->where('plantao_id', $plantao->id)
->orderBy('atendido_em', 'desc')
->first();
}
public static function getHorasTrabalhadas($params, $plantoes, $plantaoId)
{
$horasTrabalhadas = 0;
foreach($plantoes as $key => $plantao) {
if ($plantao->id !== $plantaoId) {
continue;
}
$plantaoStart = Carbon::createFromFormat('Y-m-d H:i:s', $plantao->inicio);
$plantaoEnd = Carbon::createFromFormat('Y-m-d H:i:s', $plantao->fim);
$totalMinutos = $plantaoStart->diffInMinutes($plantaoEnd);
$horas = intdiv($totalMinutos, 60);
$minutos = $totalMinutos % 60;
$horasTrabalhadas = sprintf('%02d:%02d', $horas, $minutos);
}
return $horasTrabalhadas;
}
public static function getHorasTotal($params, $plantoes, $profissionalId)
{
$startEndDateTimeInCarbon = self::getStartEndDateTimeInCarbon($params);
$total = 0;
$plantoes = $plantoes->sortBy('inicio');
$startToCalc = null;
$endToCalc = null;
foreach($plantoes as $key => $plantao) {
if ($plantao->profissional_id != $profissionalId) {
continue;
}
$plantaoStart = Carbon::createFromFormat('Y-m-d H:i:s', $plantao->inicio);
$plantaoEnd = Carbon::createFromFormat('Y-m-d H:i:s', $plantao->fim);
$periodoStart = $startEndDateTimeInCarbon['start'];
$periodoEnd = $startEndDateTimeInCarbon['end'];
$diffStart = round($periodoStart->floatDiffInMinutes($plantaoStart, false));
$diffEnd = round($periodoEnd->floatDiffInMinutes($plantaoEnd, false));
if (is_null($startToCalc)) {
$startToCalc = $plantaoStart->copy();
if ($plantaoStart->format('d/m/Y') === $periodoStart->format('d/m/Y') && $diffStart < 0) {
$startToCalc = $periodoStart->copy();
}
}
if (is_null($endToCalc)) {
$endToCalc = $plantaoEnd->copy();
if ($plantaoEnd->format('d/m/Y') === $startEndDateTimeInCarbon['end']->format('d/m/Y') && $diffEnd > 0) {
$endToCalc = $startEndDateTimeInCarbon['end']->copy();
}
} else {
if ($plantaoEnd->format('d/m/Y') === $periodoEnd->format('d/m/Y') && $diffEnd > 0) {
$endToCalc->addMinutes($plantaoStart->floatDiffInMinutes($periodoEnd));
} else {
$endToCalc->addMinutes($plantaoStart->floatDiffInMinutes($plantaoEnd));
}
}
}
$horasTrabalhadas = $startToCalc->diff($endToCalc);
return self::getStringHorasFromDiff($params, $horasTrabalhadas);
}
private static function getStringHorasFromDiff($params, $diff)
{
$startEndDateTimeInCarbon = self::getStartEndDateTimeInCarbon($params);
$daysOfYaer = $startEndDateTimeInCarbon['end']->isLeapYear() ? 366 : 365;
$horas = ($diff->y * $daysOfYaer) + ($diff->m * 30) + ($diff->d * 24) + $diff->h;
$minutos = $diff->i;
return str_pad($horas, 2, '0', STR_PAD_LEFT) . ':' . str_pad($minutos, 2, '0', STR_PAD_LEFT);
}
public static function list()
{
return [
'form' => self::getForm(),
'model' => self::getModel(),
'additionals' => [
'data_atual' => Carbon::today()->format('d/m/Y'),
]
];
}
private static function getForm()
{
return [
'unidades' => self::getUnidades(),
'locais_atendimento' => self::getLocaisAtendimento(),
'especialidades' => self::getEspecialidades(),
'profissionais' => self::getProfissionais(),
];
}
private static function getUnidades()
{
$params = request()->all();
$plantoesBuilder = self::getPlantoesBuilder();
if (!$plantoesBuilder) {
return [];
}
return $plantoesBuilder
->select(
'cadastro_unidade.id',
'cadastro_unidade.nome_fantasia'
)
->distinct()
->get();
}
private static function getLocaisAtendimento()
{
$params = request()->all();
$plantoesBuilder = self::getPlantoesBuilder();
if (!$plantoesBuilder) {
return [];
}
$locaisAtendimento = $plantoesBuilder
->select(
'cadastro_unidade.id as unidade_id',
'cadastro_unidade.nome_fantasia',
'cadastro_infraestrutura.id',
'cadastro_infraestrutura.nome',
'cadastro_infraestrutura.principal'
);
return $locaisAtendimento->distinct()
->get()
->map(function($query) {
$nome = $query->principal ? $query->nome_fantasia : $query->nome;
$collect = collect([
'id' => $query->id,
'unidade_id' => $query->unidade_id,
'nome' => $nome,
]);
return $collect->sortBy('nome');
});
}
private static function getEspecialidades()
{
$params = request()->all();
$plantoesBuilder = self::getPlantoesBuilder();
if (!$plantoesBuilder) {
return [];
}
return $plantoesBuilder
->select(
'sigtap.ocupacao.id',
'sigtap.ocupacao.nome'
)
->orderBy('nome')
->distinct()
->get();
}
private static function getProfissionais()
{
$params = request()->all();
$plantoesBuilder = self::getPlantoesBuilder();
if (!$plantoesBuilder) {
return [];
}
return $plantoesBuilder
->select(
'cadastro_usuario.id',
'cadastro_cidadao.nome'
)
->orderBy('nome')
->distinct()
->get();
}
private static function getPlantoesBuilder()
{
if (!request()->get('periodo')) {
return null;
}
$params = request()->all();
$startEndDateTimeInCarbon = self::getStartEndDateTimeInCarbon($params);
$inicio = $startEndDateTimeInCarbon['start'];
$fim = $startEndDateTimeInCarbon['end'];
$unidadeId = $params['unidade_id'];
$localAtendimentoId = $params['local_atendimento_id'];
$especialidadeId = $params['especialidade_id'];
$profissionalId = $params['profissional_id'];
$plantoes = DB::table('pronto_atendimento_plantao')
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'pronto_atendimento_plantao.lotacao_id')
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
->join('sigtap.ocupacao', 'sigtap.ocupacao.id', 'cadastro_lotacao.ocupacao_id')
->join('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id')
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
->whereNull('pronto_atendimento_plantao.deleted_at')
->where(function ($query) use ($inicio, $fim) {
$query->whereBetween('inicio', [$inicio, $fim])
->orWhereBetween('fim', [$inicio, $fim])
->orWhere(function ($query) use ($inicio) {
$query->where('inicio', '<=', $inicio)
->where('fim', '>=', $inicio);
})
->orWhere(function ($query) use ($fim) {
$query->where('inicio', '<=', $fim)
->where('fim', '>=', $fim);
});
});
if (!is_null($unidadeId)) {
$plantoes->where('cadastro_unidade.id', $unidadeId);
}
if (!is_null($localAtendimentoId)) {
$plantoes->where('cadastro_infraestrutura.id', $localAtendimentoId);
}
if (!is_null($especialidadeId)) {
$plantoes->where('cadastro_lotacao.ocupacao_id', $especialidadeId);
}
if (!is_null($profissionalId)) {
$plantoes->where('cadastro_lotacao.usuario_id', $profissionalId);
}
return $plantoes;
}
private static function getStartEndDateTimeInCarbon($params)
{
$startAt = $params['periodo']['time']['start'];
if (empty($startAt)) {
$startAt = '00:00:00';
}
$endsAt = $params['periodo']['time']['end'];
if (empty($endsAt)) {
$endsAt = '23:59:00';
}
$start = Carbon::createFromFormat('d/m/Y H:i:s', "{$params['periodo']['date']['start']} {$startAt}");
$end = Carbon::createFromFormat('d/m/Y H:i:s', "{$params['periodo']['date']['end']} {$endsAt}");
return [
'start' => $start,
'end' => $end,
];
}
private static function getModel()
{
return [
'periodo' => [
'date' => [
'start' => $params['periodo']['date']['start'] ?? null,
'end' => $params['periodo']['date']['end'] ?? null,
],
'time' => [
'start' => $params['periodo']['time']['start'] ?? null,
'end' => $params['periodo']['time']['end'] ?? null,
]
],
'unidade_id' => null,
'localAtendimento_id' => null,
'especialidade_id' => null,
'profissional_id' => null,
];
}
}