mirror of
https://gitlab.com/DanLegoff/mgap.git
synced 2026-11-16 14:12:39 +00:00
165 lines
5.8 KiB
PHP
165 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Area;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Str;
|
|
use Inertia\Inertia;
|
|
|
|
class AreaController extends Controller
|
|
{
|
|
private function applyAreaFilters(Builder $query, Request $request): Builder
|
|
{
|
|
foreach (['nombre', 'correo'] as $field) {
|
|
if ($request->filled($field)) {
|
|
$query->where($field, 'like', '%'.trim((string) $request->input($field)).'%');
|
|
}
|
|
}
|
|
|
|
if ($request->input('activo') !== null && $request->input('activo') !== '') {
|
|
$query->where('activo', $request->boolean('activo'));
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$filterKeys = ['search', 'nombre', 'responsable', 'correo', 'activo'];
|
|
$query = Area::with('usuarios');
|
|
$this->applyAreaFilters($query, $request);
|
|
$items = $this->filterByVisibleValues($query->orderBy('id')->get(), $request);
|
|
|
|
$items->each(function (Area $area): void {
|
|
$responsable = $area->responsableActualUsuario();
|
|
$area->setAttribute('responsable_actual', $responsable ? [
|
|
'id' => $responsable->id,
|
|
'nombre_completo' => trim("{$responsable->nombre} {$responsable->apellido}"),
|
|
] : null);
|
|
$area->makeHidden('usuarios');
|
|
});
|
|
|
|
return Inertia::render('Areas/Index', [
|
|
'items' => $items,
|
|
'filters' => $request->only($filterKeys),
|
|
'filterOptions' => [
|
|
'activos' => [
|
|
['value' => '1', 'label' => 'Sí'],
|
|
['value' => '0', 'label' => 'No'],
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
$query = Area::with('usuarios');
|
|
$this->applyAreaFilters($query, $request);
|
|
$items = $this->filterByVisibleValues($query->get(), $request);
|
|
|
|
return $this->streamCsv(
|
|
'areas_'.now()->format('Ymd_His').'.csv',
|
|
['Nombre', 'Responsable', 'Correo', 'Teléfono', 'Fecha Alta', 'Fecha Baja', 'Estado'],
|
|
$items,
|
|
fn ($item) => [
|
|
$item->nombre,
|
|
$item->responsable ?? 'Sin responsable',
|
|
$item->correo ?: '-',
|
|
$item->telefono ?: '-',
|
|
$item->fecha_alta?->format('j/n/Y') ?: '-',
|
|
$item->fecha_baja?->format('j/n/Y') ?: '-',
|
|
$item->activo ? 'Activa' : 'Inactiva',
|
|
],
|
|
);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return Inertia::render('Areas/Create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
Area::create($this->validateArea($request));
|
|
|
|
return redirect()->route('areas.index')->with('success', 'Área creada exitosamente.');
|
|
}
|
|
|
|
public function edit(Area $area)
|
|
{
|
|
return Inertia::render('Areas/Edit', ['item' => $area]);
|
|
}
|
|
|
|
public function update(Request $request, Area $area)
|
|
{
|
|
$area->update($this->validateArea($request));
|
|
|
|
return redirect()->route('areas.index')->with('success', 'Área actualizada exitosamente.');
|
|
}
|
|
|
|
public function destroy(Area $area)
|
|
{
|
|
$this->deletePreservingHistory($area);
|
|
|
|
return redirect()->route('areas.index')->with('success', 'Área eliminada exitosamente.');
|
|
}
|
|
|
|
private function validateArea(Request $request): array
|
|
{
|
|
return $request->validate([
|
|
'nombre' => ['required', 'string', 'max:35'],
|
|
'descripcion' => ['nullable', 'string', 'max:185'],
|
|
'correo' => ['nullable', 'email', 'max:35'],
|
|
'direccion' => ['nullable', 'string', 'max:35'],
|
|
'telefono' => ['nullable', 'string', 'regex:/^[0-9]*$/', 'max:35'],
|
|
'motivo_alta' => ['nullable', 'string', 'max:35'],
|
|
'activo' => ['nullable', 'boolean'],
|
|
'fecha_alta' => ['nullable', 'date'],
|
|
'fecha_baja' => ['nullable', 'date', 'after_or_equal:fecha_alta'],
|
|
]);
|
|
}
|
|
|
|
private function filterByVisibleValues(Collection $areas, Request $request): Collection
|
|
{
|
|
if ($request->filled('search')) {
|
|
$search = Str::lower(Str::ascii(trim((string) $request->input('search'))));
|
|
|
|
if (in_array($search, ['activo', 'activa', 'inactivo', 'inactiva'], true)) {
|
|
$active = in_array($search, ['activo', 'activa'], true);
|
|
$areas = $areas->filter(fn (Area $area): bool => $area->activo === $active);
|
|
} else {
|
|
$areas = $areas->filter(function (Area $area) use ($search): bool {
|
|
$visibleValues = implode(' ', [
|
|
$area->nombre,
|
|
$area->responsable ?? 'Sin responsable',
|
|
$area->correo ?: '-',
|
|
$area->telefono ?: '-',
|
|
$area->fecha_alta?->format('j/n/Y') ?: '-',
|
|
$area->fecha_baja?->format('j/n/Y') ?: '-',
|
|
$area->activo ? 'Activa' : 'Inactiva',
|
|
]);
|
|
|
|
return Str::contains(Str::lower(Str::ascii($visibleValues)), $search);
|
|
});
|
|
}
|
|
}
|
|
|
|
if ($request->filled('responsable')) {
|
|
$terms = preg_split('/\s+/', trim((string) $request->input('responsable')), -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
$areas = $areas->filter(function (Area $area) use ($terms): bool {
|
|
$responsable = Str::lower(Str::ascii($area->responsable ?? ''));
|
|
|
|
return collect($terms)->every(
|
|
fn (string $term): bool => Str::contains($responsable, Str::lower(Str::ascii($term)))
|
|
);
|
|
});
|
|
}
|
|
|
|
return $areas->values();
|
|
}
|
|
}
|