mirror of
https://gitlab.com/DanLegoff/mgap.git
synced 2026-11-16 14:22:37 +00:00
258 lines
10 KiB
PHP
258 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Perfil;
|
|
use App\Models\Rol;
|
|
use App\Models\Usuario;
|
|
use App\Support\HistoricalPeriod;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
use Inertia\Inertia;
|
|
|
|
class RolGestionController extends Controller
|
|
{
|
|
public function show(Rol $rol)
|
|
{
|
|
return Inertia::render('Roles/Gestionar', [
|
|
'rol' => $rol->load(['usuarios', 'perfiles']),
|
|
'usuarios' => Usuario::query()->where('activo', true)->orderBy('nombre')->get(),
|
|
'perfiles' => Perfil::query()->orderBy('nombre')->get(),
|
|
]);
|
|
}
|
|
|
|
public function updateField(Request $request, Rol $rol)
|
|
{
|
|
$validated = $request->validate([
|
|
'field' => ['required', Rule::in(['nombre', 'descripcion', 'fecha_inicio', 'fecha_fin'])],
|
|
'value' => ['nullable'],
|
|
]);
|
|
|
|
$rules = [
|
|
'nombre' => ['required', 'string', 'max:35'],
|
|
'descripcion' => ['nullable', 'string', 'max:185'],
|
|
'fecha_inicio' => ['nullable', 'date'],
|
|
'fecha_fin' => ['nullable', 'date'],
|
|
];
|
|
|
|
$value = validator(
|
|
['value' => $validated['value']],
|
|
['value' => $rules[$validated['field']]],
|
|
)->validate()['value'] ?? null;
|
|
|
|
if (in_array($validated['field'], ['fecha_inicio', 'fecha_fin'], true)) {
|
|
validator([
|
|
'fecha_inicio' => $validated['field'] === 'fecha_inicio' ? $value : $rol->fecha_inicio,
|
|
'fecha_fin' => $validated['field'] === 'fecha_fin' ? $value : $rol->fecha_fin,
|
|
], [
|
|
'fecha_inicio' => ['nullable', 'date'],
|
|
'fecha_fin' => ['nullable', 'date', 'after_or_equal:fecha_inicio'],
|
|
])->validate();
|
|
}
|
|
|
|
$rol->update([$validated['field'] => $value]);
|
|
|
|
return back()->with('success', 'Dato actualizado correctamente.');
|
|
}
|
|
|
|
public function attachUsuario(Request $request, Rol $rol)
|
|
{
|
|
$validated = $request->validate([
|
|
'usuario_id' => ['required', 'exists:usuarios,id'],
|
|
'fecha_inicio' => ['required', 'date'],
|
|
'fecha_fin' => ['nullable', 'date', 'after_or_equal:fecha_inicio'],
|
|
]);
|
|
$validated['fecha_fin'] = HistoricalPeriod::inclusiveEnd($validated['fecha_fin'] ?? null);
|
|
|
|
$inicio = Carbon::parse($validated['fecha_inicio']);
|
|
$fin = $validated['fecha_fin'] ? Carbon::parse($validated['fecha_fin']) : null;
|
|
|
|
$otrosRegistros = DB::table('rol_usuario')
|
|
->where('rol_id', $rol->id)
|
|
->where('usuario_id', $validated['usuario_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) {
|
|
$textoOtroFin = $otroFin ? $otroFin->format('d/m/Y') : 'la actualidad';
|
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
|
'fecha_inicio' => "Las fechas chocan con otro registro existente (del {$otroInicio->format('d/m/Y')} al {$textoOtroFin}).",
|
|
]);
|
|
}
|
|
}
|
|
|
|
$rol->usuarios()->attach($validated['usuario_id'], [
|
|
'fecha_inicio' => $validated['fecha_inicio'],
|
|
'fecha_fin' => $validated['fecha_fin'] ?? null,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
return back()->with('success', 'Usuario asociado correctamente.');
|
|
}
|
|
|
|
public function detachUsuario(Rol $rol, $pivotId)
|
|
{
|
|
DB::table('rol_usuario')->where('rol_id', $rol->id)->where('id', $pivotId)->delete();
|
|
|
|
return back()->with('success', 'Asignación eliminada correctamente.');
|
|
}
|
|
|
|
public function updateUsuarioPivot(Request $request, Rol $rol, $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 fecha de inicio.',
|
|
]);
|
|
$validated['fecha_fin'] = HistoricalPeriod::inclusiveEnd($validated['fecha_fin'] ?? null);
|
|
|
|
$registroActual = DB::table('rol_usuario')
|
|
->where('rol_id', $rol->id)
|
|
->where('id', $pivotId)
|
|
->firstOrFail();
|
|
$inicio = Carbon::parse($validated['fecha_inicio']);
|
|
$fin = $validated['fecha_fin'] ? Carbon::parse($validated['fecha_fin']) : null;
|
|
|
|
$otrosRegistros = DB::table('rol_usuario')
|
|
->where('rol_id', $rol->id)
|
|
->where('usuario_id', $registroActual->usuario_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;
|
|
|
|
$empiezaAntesDeQueTermineElOtro = $otroFin === null ? true : $inicio->lte($otroFin);
|
|
$terminaDespuesDeQueEmpieceElOtro = $fin === null ? true : $fin->gte($otroInicio);
|
|
|
|
if ($empiezaAntesDeQueTermineElOtro && $terminaDespuesDeQueEmpieceElOtro) {
|
|
$textoOtroFin = $otroFin ? $otroFin->format('d/m/Y') : 'la actualidad';
|
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
|
'fecha_inicio' => "Las fechas chocan con otro registro existente (del {$otroInicio->format('d/m/Y')} al {$textoOtroFin}).",
|
|
]);
|
|
}
|
|
}
|
|
|
|
DB::table('rol_usuario')
|
|
->where('rol_id', $rol->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.');
|
|
}
|
|
|
|
public function attachPerfil(Request $request, Rol $rol)
|
|
{
|
|
$validated = $request->validate([
|
|
'perfil_id' => ['required', 'exists:perfiles,id'],
|
|
'fecha_inicio' => ['required', 'date'],
|
|
'fecha_fin' => ['nullable', 'date', 'after_or_equal:fecha_inicio'],
|
|
]);
|
|
$validated['fecha_fin'] = HistoricalPeriod::inclusiveEnd($validated['fecha_fin'] ?? null);
|
|
|
|
$inicio = Carbon::parse($validated['fecha_inicio']);
|
|
$fin = $validated['fecha_fin'] ? Carbon::parse($validated['fecha_fin']) : null;
|
|
|
|
$otrosRegistros = DB::table('perfil_rol')
|
|
->where('rol_id', $rol->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) {
|
|
$textoOtroFin = $otroFin ? $otroFin->format('d/m/Y') : 'la actualidad';
|
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
|
'fecha_inicio' => "Las fechas chocan con otro registro existente (del {$otroInicio->format('d/m/Y')} al {$textoOtroFin}).",
|
|
]);
|
|
}
|
|
}
|
|
|
|
$rol->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(Rol $rol, $pivotId)
|
|
{
|
|
DB::table('perfil_rol')->where('rol_id', $rol->id)->where('id', $pivotId)->delete();
|
|
|
|
return back()->with('success', 'Asignación eliminada correctamente.');
|
|
}
|
|
|
|
public function updatePerfilPivot(Request $request, Rol $rol, $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 fecha de inicio.',
|
|
]);
|
|
$validated['fecha_fin'] = HistoricalPeriod::inclusiveEnd($validated['fecha_fin'] ?? null);
|
|
|
|
$registroActual = DB::table('perfil_rol')
|
|
->where('rol_id', $rol->id)
|
|
->where('id', $pivotId)
|
|
->firstOrFail();
|
|
$inicio = Carbon::parse($validated['fecha_inicio']);
|
|
$fin = $validated['fecha_fin'] ? Carbon::parse($validated['fecha_fin']) : null;
|
|
|
|
$otrosRegistros = DB::table('perfil_rol')
|
|
->where('rol_id', $rol->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;
|
|
|
|
$empiezaAntesDeQueTermineElOtro = $otroFin === null ? true : $inicio->lte($otroFin);
|
|
$terminaDespuesDeQueEmpieceElOtro = $fin === null ? true : $fin->gte($otroInicio);
|
|
|
|
if ($empiezaAntesDeQueTermineElOtro && $terminaDespuesDeQueEmpieceElOtro) {
|
|
$textoOtroFin = $otroFin ? $otroFin->format('d/m/Y') : 'la actualidad';
|
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
|
'fecha_inicio' => 'Las fechas chocan con otro registro existente.',
|
|
]);
|
|
}
|
|
}
|
|
|
|
DB::table('perfil_rol')
|
|
->where('rol_id', $rol->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.');
|
|
}
|
|
}
|