diff --git a/app/Exports/ChoferesExport.php b/app/Exports/ChoferesExport.php new file mode 100644 index 0000000..cb9f25a --- /dev/null +++ b/app/Exports/ChoferesExport.php @@ -0,0 +1,24 @@ +get(); + } + + public function headings(): array + { + return [ + 'ID', + 'Nombre', + 'Tipo de Licencia', + ]; + } +} diff --git a/app/Http/Controllers/ChoferController.php b/app/Http/Controllers/ChoferController.php new file mode 100644 index 0000000..94fd693 --- /dev/null +++ b/app/Http/Controllers/ChoferController.php @@ -0,0 +1,97 @@ + null]); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + $request->validate([ + 'nombre' => 'required|string|max:255', + 'tipo_licencia' => 'required|string|max:255', + ]); + + Chofer::create($request->all()); + return redirect()->route('choferes.index')->with('success', 'Chofer creado exitosamente.'); + } + + /** + * Display the specified resource. + */ + public function show(Chofer $chofer) + { + // + } + + /** + * Show the form for editing the specified resource. + */ + public function edit($id) + { + $chofer = Chofer::findOrFail($id); + return view('choferesCrearEditar', compact('chofer')); + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, $id) + { + $request->validate([ + 'nombre' => 'required|string|max:255', + 'tipo_licencia' => 'required|string|max:255', + ]); + + $chofer = Chofer::findOrFail($id); + $chofer->update($request->all()); + return redirect()->route('choferes.index')->with('success', 'Chofer actualizado exitosamente.'); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy($id) + { + $chofer = Chofer::findOrFail($id); + $chofer->delete(); + return redirect()->route('choferes.index')->with('success', 'Chofer eliminado exitosamente.'); + } + + public function exportExcel() + { + return \Maatwebsite\Excel\Facades\Excel::download(new \App\Exports\ChoferesExport, 'choferes.xlsx'); + } + + public function exportPDF() + { + $choferes = \App\Models\Chofer::all(); + $pdf = \PDF::loadView('exports.choferes-pdf', ['choferes' => $choferes]); + return $pdf->download('choferes.pdf'); + } +} diff --git a/app/Http/Controllers/PrestamoController.php b/app/Http/Controllers/PrestamoController.php index 5490623..c06ebbf 100644 --- a/app/Http/Controllers/PrestamoController.php +++ b/app/Http/Controllers/PrestamoController.php @@ -40,7 +40,13 @@ class PrestamoController extends Controller */ public function create() { - return view('prestamosCrearEditar', ['prestamo' => null]); // No se necesita pasar préstamos + $vehiculos = \App\Models\tiposVeiculos::where('status', true)->get(); + $choferes = \App\Models\Chofer::all(); + return view('prestamosCrearEditar', [ + 'prestamo' => null, + 'vehiculos' => $vehiculos, + 'choferes' => $choferes + ]); } /** @@ -48,16 +54,27 @@ class PrestamoController extends Controller */ public function store(Request $request) { + // Validación de datos + $request->validate([ + 'nombre_solicitante' => 'required|string|max:255', + 'chofer_id' => 'required|exists:choferes,id', + 'destino' => 'required|string|max:255', + 'fecha_hora_salida' => 'required|date', + 'fecha_hora_llegada' => 'required|date', + 'motivo' => 'required|string|max:255', + 'domicilio' => 'required|string|max:255', + 'numero_personas' => 'required|integer', + 'vehiculo_id' => 'required|exists:tipos_veiculos,id' + ]); + // Preparar los datos $datos = $request->all(); - $datos['chofer'] = $request->has('chofer') ? 1 : 0; // Convertir 'on' a 1, o ausencia a 0 + $datos['chofer'] = $request->has('chofer') ? 1 : 0; $prestamo = new Prestamo($datos); - $prestamo->estado = 'pendiente'; // Estado inicial + $prestamo->estado = 'pendiente'; $prestamo->save(); - // Aquí puedes agregar notificaciones para los administradores - return redirect()->route('prestamos.index') ->with('success', 'Préstamo solicitado correctamente. Esperando aprobación.'); } @@ -68,7 +85,13 @@ class PrestamoController extends Controller public function edit($id) { $prestamo = Prestamo::findOrFail($id); // Busca el préstamo por ID - return view('prestamosCrearEditar', ['prestamo' => $prestamo]); // Pasa el préstamo a la vista + $vehiculos = \App\Models\tiposVeiculos::where('status', true)->get(); + $choferes = \App\Models\Chofer::all(); + return view('prestamosCrearEditar', [ + 'prestamo' => $prestamo, + 'vehiculos' => $vehiculos, + 'choferes' => $choferes + ]); // Pasa el préstamo a la vista } /** diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 81a033f..0496971 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -67,5 +67,6 @@ class Kernel extends HttpKernel 'profesor' => \App\Http\Middleware\CheckProfesorRole::class, 'admin' => \App\Http\Middleware\CheckAdminRole::class, 'servicios' => \App\Http\Middleware\CheckServiciosRole::class, + 'adminOrServicios' => \App\Http\Middleware\AdminOrServiciosRole::class, ]; } diff --git a/app/Http/Middleware/AdminOrServiciosRole.php b/app/Http/Middleware/AdminOrServiciosRole.php new file mode 100644 index 0000000..87c506f --- /dev/null +++ b/app/Http/Middleware/AdminOrServiciosRole.php @@ -0,0 +1,23 @@ +check() || !in_array(auth()->user()->rol, ['admin', 'servicios'])) { + return redirect()->route('dashboard')->with('error', 'No tienes permisos para acceder a esta sección.'); + } + return $next($request); +} +} \ No newline at end of file diff --git a/app/Http/Middleware/CheckAdminRole.php b/app/Http/Middleware/CheckAdminRole.php index a5911b8..97c5764 100644 --- a/app/Http/Middleware/CheckAdminRole.php +++ b/app/Http/Middleware/CheckAdminRole.php @@ -11,9 +11,10 @@ class CheckAdminRole public function handle(Request $request, Closure $next): Response { if (!auth()->check() || auth()->user()->rol !== 'admin') { - return redirect('/')->with('error', 'No tienes permisos de administrador para acceder a esta sección.'); + return redirect()->route('dashboard') + ->with('error', 'No tienes permisos de administrador para acceder a esta sección.'); } return $next($request); } -} \ No newline at end of file +} diff --git a/app/Models/Chofer.php b/app/Models/Chofer.php new file mode 100644 index 0000000..c185392 --- /dev/null +++ b/app/Models/Chofer.php @@ -0,0 +1,18 @@ +belongsTo(\App\Models\tiposVeiculos::class, 'vehiculo_id'); +} } diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 5522aa2..eaca45b 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -4,6 +4,7 @@ namespace App\Providers; // use Illuminate\Support\Facades\Gate; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; +use Illuminate\Support\Facades\Gate; class AuthServiceProvider extends ServiceProvider { @@ -20,9 +21,11 @@ class AuthServiceProvider extends ServiceProvider * Register any authentication / authorization services. */ public function boot(): void - { - $this->registerPolicies(); +{ + $this->registerPolicies(); - // - } + Gate::define('gestionar-prestamos', function ($user) { + return in_array($user->rol, ['admin', 'servicios']); + }); +} } diff --git a/database/migrations/2025_03_27_174121_create_prestamos_table.php b/database/migrations/2025_03_27_174121_create_prestamos_table.php index 2ceb81f..3ae0b9b 100644 --- a/database/migrations/2025_03_27_174121_create_prestamos_table.php +++ b/database/migrations/2025_03_27_174121_create_prestamos_table.php @@ -21,6 +21,7 @@ return new class extends Migration $table->string('domicilio'); $table->integer('numero_personas'); $table->boolean('chofer')->default(false); // Opción de sí (true) o no (false) + $table->foreignId('vehiculo_id')->constrained('tipos_veiculos')->onDelete('cascade'); $table->timestamps(); }); } diff --git a/database/migrations/2025_03_28_182126_add_estado_to_prestamos_table.php b/database/migrations/2025_03_28_182126_add_estado_to_prestamos_table.php deleted file mode 100644 index af9986e..0000000 --- a/database/migrations/2025_03_28_182126_add_estado_to_prestamos_table.php +++ /dev/null @@ -1,28 +0,0 @@ -string('estado')->default('pendiente'); // pendiente, aceptado, rechazado - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::table('prestamos', function (Blueprint $table) { - $table->dropColumn('estado'); - }); - } -}; diff --git a/database/migrations/2025_04_01_191325_add_estado_to_prestamos_table.php b/database/migrations/2025_04_01_191325_add_estado_to_prestamos_table.php index c5d3a01..1384d82 100644 --- a/database/migrations/2025_04_01_191325_add_estado_to_prestamos_table.php +++ b/database/migrations/2025_04_01_191325_add_estado_to_prestamos_table.php @@ -9,21 +9,22 @@ return new class extends Migration /** * Run the migrations. */ - public function up(): void + public function up() { Schema::table('prestamos', function (Blueprint $table) { - // $table->enum('estado', ['pendiente', 'aceptado', 'rechazado'])->default('pendiente')->after('chofer'); - // Línea comentada porque la columna ya existe + $table->string('estado')->default('pendiente'); }); } /** * Reverse the migrations. */ - public function down(): void - { - Schema::table('prestamos', function (Blueprint $table) { + public function down() +{ + Schema::table('prestamos', function (Blueprint $table) { + if (Schema::hasColumn('prestamos', 'estado')) { $table->dropColumn('estado'); - }); + } + }); } }; diff --git a/database/migrations/2025_03_28_175646_add_estado_to_prestamos_table.php b/database/migrations/2025_05_22_205902_create_chofers_table.php similarity index 53% rename from database/migrations/2025_03_28_175646_add_estado_to_prestamos_table.php rename to database/migrations/2025_05_22_205902_create_chofers_table.php index af9986e..e81227b 100644 --- a/database/migrations/2025_03_28_175646_add_estado_to_prestamos_table.php +++ b/database/migrations/2025_05_22_205902_create_chofers_table.php @@ -9,20 +9,21 @@ return new class extends Migration /** * Run the migrations. */ - public function up(): void - { - Schema::table('prestamos', function (Blueprint $table) { - // - }); - } + public function up() +{ + Schema::create('choferes', function (Blueprint $table) { + $table->id(); + $table->string('nombre'); + $table->string('tipo_licencia'); + $table->timestamps(); + }); +} /** * Reverse the migrations. */ public function down(): void { - Schema::table('prestamos', function (Blueprint $table) { - // - }); + Schema::dropIfExists('choferes'); } }; diff --git a/database/migrations/2025_05_22_212123_add_chofer_id_to_prestamos_table.php b/database/migrations/2025_05_22_212123_add_chofer_id_to_prestamos_table.php new file mode 100644 index 0000000..c7a6195 --- /dev/null +++ b/database/migrations/2025_05_22_212123_add_chofer_id_to_prestamos_table.php @@ -0,0 +1,25 @@ +foreignId('chofer_id')->nullable()->constrained('choferes')->onDelete('set null'); + }); +} +public function down() +{ + Schema::table('prestamos', function (Blueprint $table) { + $table->dropForeign(['chofer_id']); + $table->dropColumn('chofer_id'); + }); +} +}; diff --git a/resources/views/choferes.blade.php b/resources/views/choferes.blade.php new file mode 100644 index 0000000..43b0e83 --- /dev/null +++ b/resources/views/choferes.blade.php @@ -0,0 +1,95 @@ +@extends('layouts.dashboard') + +@section('content') +
Fecha de generación: {{ date('d/m/Y H:i:s') }}
+ID | +Nombre | +Tipo de Licencia | +Fecha de Creación | +
---|---|---|---|
{{ $chofer->id }} | +{{ $chofer->nombre }} | +{{ $chofer->tipo_licencia }} | +{{ $chofer->created_at->format('d/m/Y') }} | +
{{ $message }}
+ @enderror +ID | -Tipo de Licencia | -Estado | -Acciones | -
---|---|---|---|
{{ $tipoLicencia->id }} | -- - {{ $tipoLicencia->tipoLicencia }} - | -- @if($tipoLicencia->eliminado == 1) - - Activo - - @else - - Inactivo - - @endif - | -- - | -