mirror of
https://gitlab.com/DanLegoff/mgap.git
synced 2026-11-16 14:22:37 +00:00
132 lines
5.0 KiB
PHP
132 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Perfil;
|
|
use App\Models\Permiso;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
use Inertia\Inertia;
|
|
|
|
class PermisoGestionController extends Controller
|
|
{
|
|
public function show(Permiso $permiso)
|
|
{
|
|
return Inertia::render('Permisos/Gestionar', [
|
|
'permiso' => $permiso->load('perfiles'),
|
|
'perfiles' => Perfil::orderBy('nombre')->get(),
|
|
]);
|
|
}
|
|
|
|
public function updateField(Request $request, Permiso $permiso)
|
|
{
|
|
$validated = $request->validate([
|
|
'field' => ['required', Rule::in(['nombre', 'accion', 'estado', 'justificacion'])],
|
|
'value' => ['nullable'],
|
|
]);
|
|
|
|
$rules = [
|
|
'nombre' => ['required', 'string', 'max:35'],
|
|
'accion' => ['required', 'string', 'max:185'],
|
|
'estado' => ['nullable', 'string', 'in:1,0'],
|
|
'justificacion' => ['nullable', 'string', 'max:185'],
|
|
];
|
|
$value = validator(
|
|
['value' => $validated['value']],
|
|
['value' => $rules[$validated['field']]],
|
|
)->validate()['value'] ?? null;
|
|
|
|
$permiso->update([$validated['field'] => $value]);
|
|
|
|
return back()->with('success', 'Dato actualizado correctamente.');
|
|
}
|
|
|
|
public function attachPerfil(Request $request, Permiso $permiso)
|
|
{
|
|
$validated = $request->validate([
|
|
'perfil_id' => ['required', 'exists:perfiles,id'],
|
|
'fecha_inicio' => ['required', 'date'],
|
|
'fecha_fin' => ['nullable', 'date', 'after_or_equal:fecha_inicio'],
|
|
]);
|
|
|
|
$inicio = Carbon::parse($validated['fecha_inicio']);
|
|
$fin = $validated['fecha_fin'] ? Carbon::parse($validated['fecha_fin']) : null;
|
|
|
|
$otrosRegistros = DB::table('permiso_perfil')->where('permiso_id', $permiso->id)->where('perfil_id', $validated['perfil_id'])->get();
|
|
|
|
foreach ($otrosRegistros as $otro) {
|
|
$otroInicio = Carbon::parse($otro->fecha_inicio);
|
|
$otroFin = $otro->fecha_fin ? Carbon::parse($otro->fecha_fin) : null;
|
|
|
|
$empiezaAntes = $otroFin === null ? true : $inicio->lte($otroFin);
|
|
$terminaDespues = $fin === null ? true : $fin->gte($otroInicio);
|
|
|
|
if ($empiezaAntes && $terminaDespues) {
|
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
|
'fecha_inicio' => 'Las fechas chocan con otro registro existente.',
|
|
]);
|
|
}
|
|
}
|
|
|
|
$permiso->perfiles()->attach($validated['perfil_id'], [
|
|
'fecha_inicio' => $validated['fecha_inicio'],
|
|
'fecha_fin' => $validated['fecha_fin'] ?? null,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
return back()->with('success', 'Perfil asociado correctamente.');
|
|
}
|
|
|
|
public function detachPerfil(Permiso $permiso, $pivotId)
|
|
{
|
|
DB::table('permiso_perfil')->where('permiso_id', $permiso->id)->where('id', $pivotId)->delete();
|
|
|
|
return back()->with('success', 'Asignación de perfil eliminada correctamente.');
|
|
}
|
|
|
|
public function updatePerfilPivot(Request $request, Permiso $permiso, $pivotId)
|
|
{
|
|
$validated = $request->validate([
|
|
'fecha_inicio' => ['required', 'date'],
|
|
'fecha_fin' => ['nullable', 'date', 'after_or_equal:fecha_inicio'],
|
|
], ['fecha_fin.after_or_equal' => 'La fecha de fin no puede ser menor a la de inicio.']);
|
|
|
|
$registroActual = DB::table('permiso_perfil')
|
|
->where('permiso_id', $permiso->id)
|
|
->where('id', $pivotId)
|
|
->firstOrFail();
|
|
$inicio = Carbon::parse($validated['fecha_inicio']);
|
|
$fin = $validated['fecha_fin'] ? Carbon::parse($validated['fecha_fin']) : null;
|
|
|
|
$otrosRegistros = DB::table('permiso_perfil')->where('permiso_id', $permiso->id)->where('perfil_id', $registroActual->perfil_id)->where('id', '!=', $pivotId)->get();
|
|
|
|
foreach ($otrosRegistros as $otro) {
|
|
$otroInicio = Carbon::parse($otro->fecha_inicio);
|
|
$otroFin = $otro->fecha_fin ? Carbon::parse($otro->fecha_fin) : null;
|
|
|
|
$empiezaAntes = $otroFin === null ? true : $inicio->lte($otroFin);
|
|
$terminaDespues = $fin === null ? true : $fin->gte($otroInicio);
|
|
|
|
if ($empiezaAntes && $terminaDespues) {
|
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
|
'fecha_inicio' => 'Las fechas chocan con otro registro existente.',
|
|
]);
|
|
}
|
|
}
|
|
|
|
DB::table('permiso_perfil')
|
|
->where('permiso_id', $permiso->id)
|
|
->where('id', $pivotId)
|
|
->update([
|
|
'fecha_inicio' => $validated['fecha_inicio'],
|
|
'fecha_fin' => $validated['fecha_fin'],
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
return back()->with('success', 'Asignación actualizada correctamente.');
|
|
}
|
|
}
|