mirror of
https://gitlab.com/DanLegoff/mgap.git
synced 2026-11-16 14:12:39 +00:00
111 lines
3.4 KiB
PHP
111 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Permiso;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
|
|
class PermisoController extends Controller
|
|
{
|
|
private function applyPermisoFilters(Builder $query, Request $request): Builder
|
|
{
|
|
$this->applySearch($query, $request->input('search'), ['nombre', 'accion', 'estado', 'justificacion']);
|
|
|
|
if ($request->filled('nombre')) {
|
|
$query->where('nombre', 'like', '%'.trim((string) $request->input('nombre')).'%');
|
|
}
|
|
if ($request->filled('accion')) {
|
|
$query->where('accion', 'like', '%'.trim((string) $request->input('accion')).'%');
|
|
}
|
|
if ($request->filled('estado')) {
|
|
$query->where('estado', $request->input('estado'));
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$filterKeys = ['search', 'nombre', 'accion', 'estado'];
|
|
$query = Permiso::query();
|
|
$this->applyPermisoFilters($query, $request);
|
|
|
|
return Inertia::render('Permisos/Index', [
|
|
'items' => $query->orderBy('id')->get(),
|
|
'filters' => $request->only($filterKeys),
|
|
'filterOptions' => [
|
|
'estados' => Permiso::query()
|
|
->select('estado as value', 'estado as label')
|
|
->whereNotNull('estado')
|
|
->where('estado', '<>', '')
|
|
->distinct()
|
|
->orderBy('estado')
|
|
->get(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
$query = Permiso::query();
|
|
$this->applyPermisoFilters($query, $request);
|
|
|
|
$items = $query->get();
|
|
|
|
$fileName = 'permisos_'.now()->format('Ymd_His').'.csv';
|
|
|
|
$columns = ['ID', 'Nombre', 'Acción', 'Estado', 'Justificación'];
|
|
|
|
return $this->streamCsv($fileName, $columns, $items, fn ($item) => [
|
|
$item->id, $item->nombre, $item->accion, $item->estado, $item->justificacion,
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return Inertia::render('Permisos/Create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'nombre' => 'required|string|max:35',
|
|
'estado' => 'nullable|string|in:1,0',
|
|
'accion' => 'required|string|max:185',
|
|
'justificacion' => 'nullable|string|max:185',
|
|
]);
|
|
|
|
Permiso::create($validated);
|
|
|
|
return redirect()->route('permisos.index')->with('success', 'Permiso creado exitosamente.');
|
|
}
|
|
|
|
public function edit(Permiso $permiso)
|
|
{
|
|
return redirect()->route('permisos.gestionar', $permiso->id);
|
|
}
|
|
|
|
public function update(Request $request, Permiso $permiso)
|
|
{
|
|
$validated = $request->validate([
|
|
'nombre' => 'required|string|max:35',
|
|
'estado' => 'nullable|string|in:1,0',
|
|
'accion' => 'required|string|max:185',
|
|
'justificacion' => 'nullable|string|max:185',
|
|
]);
|
|
|
|
$permiso->update($validated);
|
|
|
|
return redirect()->route('permisos.index')->with('success', 'Permiso actualizado exitosamente.');
|
|
}
|
|
|
|
public function destroy(Permiso $permiso)
|
|
{
|
|
$this->deletePreservingHistory($permiso);
|
|
|
|
return redirect()->route('permisos.index')->with('success', 'Permiso eliminado exitosamente.');
|
|
}
|
|
}
|