211 lines
9.3 KiB
PHP
211 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ExameLaboratorial;
|
|
|
|
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
|
use App\Models\Cadastro\Feriado\Feriado;
|
|
use App\Models\ExameLaboratorial\AgendamentoExame\Agendamento;
|
|
use App\Models\ExameLaboratorial\Jornada\Jornada;
|
|
use App\Models\ExameLaboratorial\Jornada\SemanaJornada;
|
|
use App\Models\ExameLaboratorial\Jornada\TipoJornada;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\IntegracaoAplicativo;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as UnidadeParametrizacao;
|
|
use App\Services\Service;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class JornadaService extends Service
|
|
{
|
|
public static function datatable()
|
|
{
|
|
return DB::table('cadastro_unidade')
|
|
->select([
|
|
'cadastro_infraestrutura.id',
|
|
DB::raw("
|
|
CONCAT(
|
|
cadastro_unidade.nome_fantasia,
|
|
' - ',
|
|
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
|
|
cadastro_infraestrutura.nome
|
|
ELSE
|
|
cadastro_unidade.nome_fantasia
|
|
END
|
|
) AS nome_fantasia
|
|
"),
|
|
])
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id')
|
|
->whereExists(function($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('exame_laboratorial_parametrizacao_unidade')
|
|
->whereColumn('exame_laboratorial_parametrizacao_unidade.unidade_id', 'cadastro_unidade.id')
|
|
->whereColumn('exame_laboratorial_parametrizacao_unidade.infraestrutura_id', 'cadastro_infraestrutura.id')
|
|
->where('exame_laboratorial_parametrizacao_unidade.unidade_coleta', true)
|
|
->whereNull('exame_laboratorial_parametrizacao_unidade.deleted_at');
|
|
});
|
|
}
|
|
|
|
public static function show($id)
|
|
{
|
|
|
|
$unidades = UnidadeParametrizacao::where('infraestrutura_id', $id)->first();
|
|
|
|
$integracaoAplicativo = IntegracaoAplicativo::first();
|
|
|
|
$laboratorios = UnidadeParametrizacao::where('laboratorio', true)
|
|
->whereNotNull('infraestrutura_id')
|
|
->get()
|
|
->map(fn ($param) => [
|
|
'id' => $param->infraestrutura_id,
|
|
'nome' => $param->infraestrutura->nome ?? null,
|
|
])
|
|
->unique('id')
|
|
->sortBy('nome')
|
|
->values();
|
|
|
|
return [
|
|
'form' => [
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
'min_date' => Carbon::now()->subDays(90)->format('d/m/Y'),
|
|
'max_date' => Carbon::now()->addYear()->format('d/m/Y'),
|
|
'unidade' => $unidades->unidade->nome_fantasia,
|
|
'infraestrutura' => $unidades->infraestrutura->nome,
|
|
'habilita_agendamento_app' => $integracaoAplicativo->habilita_agendamento_conectasus_cidadao ?? false,
|
|
'laboratorios' => $laboratorios,
|
|
'feriados' => Feriado::orderBy('id')
|
|
->get()
|
|
->map(fn ($feriado) => [
|
|
'id' => $feriado->id,
|
|
'data' => (new Carbon($feriado->data))->format('d/m/Y') ?? null,
|
|
'tipo_id' => $feriado->tipo_id,
|
|
'tipo_nome' => $feriado->tipo->descricao,
|
|
'observacao' => $feriado->observacao,
|
|
'modulos' => $feriado->modulos->pluck('id'),
|
|
]),
|
|
'dias_semana' => SemanaJornada::orderBy('id')
|
|
->get()
|
|
->map(fn ($semanaJornadas) => [
|
|
'id' => $semanaJornadas->id,
|
|
'nome' => $semanaJornadas->nome,
|
|
'disabled' => false,
|
|
]),
|
|
'tipos' => TipoJornada::orderBy('id')
|
|
->get()
|
|
->map(fn ($tipo) => [
|
|
'id' => $tipo->id,
|
|
'nome' => $tipo->nome
|
|
]),
|
|
],
|
|
'model' => [
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
'unidade_id' => $unidades->unidade_id,
|
|
'infraestrutura_id' => $unidades->infraestrutura_id,
|
|
'jornadas' => Jornada::where('unidade_id', $unidades->unidade_id)
|
|
->where('infraestrutura_id', $unidades->infraestrutura_id)
|
|
->get()
|
|
->map(function ($jornada) use ($unidades) {
|
|
|
|
$statusPermitidos = [
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::NOVA_COLETA,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
];
|
|
|
|
$agendamentosQuery = Agendamento::where('jornada_id', $jornada->id)
|
|
->where('coleta_infraestrutura_id', $unidades->infraestrutura_id) // agora funciona
|
|
->whereHas('agendamentoExame', function ($query) use ($statusPermitidos) {
|
|
$query->whereIn('status_id', $statusPermitidos);
|
|
});
|
|
|
|
$agendamentos = $agendamentosQuery->count();
|
|
|
|
$primeiroHorario = (clone $agendamentosQuery)
|
|
->orderBy('horario', 'asc')
|
|
->value('horario');
|
|
|
|
$ultimoHorario = (clone $agendamentosQuery)
|
|
->orderBy('horario', 'desc')
|
|
->value('horario');
|
|
|
|
return [
|
|
'jornada_id' => $jornada->id,
|
|
'unidade_id' => $jornada->unidade_id,
|
|
'infraestrutura_id' => $jornada->infraestrutura_id,
|
|
'semana_id' => $jornada->semana_id,
|
|
'dias_semana' => $jornada->semana_id,
|
|
'tipo_id' => $jornada->tipo_id,
|
|
'tipo_nome' => $jornada->tipo->nome ?? null,
|
|
'data' => (new Carbon($jornada->data))->format('d/m/Y') ?? null,
|
|
'periodo' => [
|
|
'start' => (new Carbon($jornada->data))->format('d/m/Y') ?? null,
|
|
'end' => (new Carbon($jornada->data))->format('d/m/Y') ?? null,
|
|
],
|
|
'hora_inicial' => (new Carbon($jornada->hora_inicial))->format('H:i') ?? null,
|
|
'hora_fim' => (new Carbon($jornada->hora_fim))->format('H:i') ?? null,
|
|
'vagas' => $jornada->vagas ?? null,
|
|
'vagas_app' => $jornada->vagas_app ?? null,
|
|
'laboratorio_prestador_infraestrutura_id' => $jornada->laboratorio_prestador_infraestrutura_id ?? null,
|
|
'laboratorio_prestador_nome' => $jornada->laboratorioPrestadorInfraestrutura->nome ?? null,
|
|
'tempo_atendimento' => $jornada->tempo_atendimento ?? null,
|
|
'selecionado' => false,
|
|
'agendamentos' => $agendamentos,
|
|
'primeiro_horario' => $primeiroHorario,
|
|
'ultimo_horario' => $ultimoHorario,
|
|
'vagas_disponiveis' => (($jornada->vagas + ($jornada->vagas_app ?? 0)) - $agendamentos) >= 0
|
|
? (($jornada->vagas + ($jornada->vagas_app ?? 0)) - $agendamentos) : 0,
|
|
];
|
|
})
|
|
->sortBy('data')
|
|
->values(),
|
|
'jornadasNotSave' => null,
|
|
'periodo' => [
|
|
'start' => Carbon::now()->format('d/m/Y'),
|
|
'end' => Carbon::now()->format('d/m/Y'),
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function store(Request $request)
|
|
{
|
|
DB::transaction(function () use ($request) {
|
|
|
|
self::saveJornada($request);
|
|
});
|
|
}
|
|
|
|
private static function saveJornada($request)
|
|
{
|
|
$ids = [];
|
|
|
|
foreach ($request->jornadas as $params) {
|
|
|
|
$tipo = Jornada::where('data', Carbon::createFromFormat('d/m/Y', $params['data'])->format('Y-m-d'))
|
|
->where('unidade_id', $params['unidade_id'])
|
|
->where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->where('hora_inicial', $params['hora_inicial'])
|
|
->first() ?? new Jornada();
|
|
|
|
$tipo->fill($params);
|
|
|
|
$tipo->data = Carbon::createFromFormat('d/m/Y', $params['data']);
|
|
|
|
$tipo->save();
|
|
|
|
$ids[] = $tipo->id;
|
|
}
|
|
|
|
Jornada::where('unidade_id', $request->unidade_id)
|
|
->where('infraestrutura_id', $request->infraestrutura_id)
|
|
->whereNotIn('id', $ids)
|
|
->get()
|
|
->each(function ($jornada) {
|
|
$jornada->delete();
|
|
});
|
|
}
|
|
}
|