From bcf5131e28466f3c80347fde30c0645560f68a14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Jos=C3=A9?= Date: Wed, 9 Sep 2015 10:39:00 +0200 Subject: [PATCH] Gestion de empleados --- app/Http/Controllers/UserController.php | 135 ++++++++++++++++++ app/Http/routes.php | 6 + .../2015_09_09_080436_userExtraFields.php | 33 +++++ resources/views/template.blade.php | 4 +- resources/views/user/create.blade.php | 123 ++++++++++++++++ resources/views/user/list.blade.php | 70 +++++++++ resources/views/user/view.blade.php | 124 ++++++++++++++++ 7 files changed, 493 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/UserController.php create mode 100644 database/migrations/2015_09_09_080436_userExtraFields.php create mode 100644 resources/views/user/create.blade.php create mode 100644 resources/views/user/list.blade.php create mode 100644 resources/views/user/view.blade.php diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..596362a --- /dev/null +++ b/app/Http/Controllers/UserController.php @@ -0,0 +1,135 @@ +all(); + //Filter the data to check if any is wrong + $validator = Validator::make($data, [ + 'name' => 'required|max:254', + 'password' => 'required|max:254', + 'address' => 'required|max:254', + 'nif' => 'required|max:254', + 'nomina' => 'required|max:254', + ]); + //Check if validator fails + if (!$validator->fails()) { + $user = new User; + $user->name = $data['name']; + $user->email = $data['email']; + $user->password = Hash::make($data['password']); + $user->fk_company = 1; + $user->fk_role = $data['fk_role']; + $user->address = $data['address']; + $user->cp = $data['cp']; + $user->poblation = $data['poblation']; + $user->nif = $data['nif']; + $user->nomina = $data['nomina']; + + //Save the client + if ($user->save()) { + //Ok, go to view + return redirect('user/view/' . $user->id); + } else { + //ko, return to form + return redirect('user/create') + ->withErrors($validator) + ->withInput(); + } + } else { + //Innitial validation KO, redirect to form + return redirect('user/create') + ->withErrors($validator) + ->withInput(); + } + } + + /* + * This function get's the post request and updates the user + */ + + public function postUpdate(Request $request) { + //Get the data + $data = $request->all(); + //Filter the data to check if any is wrong + $validator = Validator::make($data, [ + 'name' => 'required|max:254', + 'address' => 'required|max:254', + 'nif' => 'required|max:254', + 'nomina' => 'required|max:254', + ]); + //Check if validator fails + if (!$validator->fails()) { + $user = User::find($data['id']); + $user->name = $data['name']; + $user->email = $data['email']; + //Only change password if put on the field + if (strlen($data['password']) > 0) + $user->password = Hash::make($data['password']); + $user->fk_company = 1; + $user->fk_role = $data['fk_role']; + $user->address = $data['address']; + $user->cp = $data['cp']; + $user->poblation = $data['poblation']; + $user->nif = $data['nif']; + $user->nomina = $data['nomina']; + + //Save the client + if ($user->save()) { + //Ok, go to view + return redirect('user/view/' . $user->id); + } else { + //ko, return to form + return redirect('user/create') + ->withErrors($validator) + ->withInput(); + } + } else { + //Innitial validation KO, redirect to form + return redirect('user/create') + ->withErrors($validator) + ->withInput(); + } + } + + /* + * This function render's the view form for the user + */ + + public function getView($id) { + //Load user + $user = User::find($id); + return view('user.view', ['user' => $user]); + } + + /* + * This function render's the list + */ + + public function getList() { + $users = User::all(); + return view('user.list', ['users' => $users]); + } + +} diff --git a/app/Http/routes.php b/app/Http/routes.php index 888883a..89df3fa 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -108,4 +108,10 @@ Route::get('/provider_invoice/pay/{id}', 'ProviderInvoiceController@setPayedInvoice'); Route::get('/provider_invoice/unpay/{id}', 'ProviderInvoiceController@setUnpayedInvoice'); Route::post('/provider_invoice/create/', 'ProviderInvoiceController@ajaxCreateInvoice'); + //Employee routes + Route::get('/user/create', 'UserController@getCreate'); + Route::get('/user/list', 'UserController@getList'); + Route::post('/user/create', 'UserController@postCreate'); + Route::get('/user/view/{id}', 'UserController@getView'); + Route::post('/user/update', 'UserController@postUpdate'); }); diff --git a/database/migrations/2015_09_09_080436_userExtraFields.php b/database/migrations/2015_09_09_080436_userExtraFields.php new file mode 100644 index 0000000..0274f0a --- /dev/null +++ b/database/migrations/2015_09_09_080436_userExtraFields.php @@ -0,0 +1,33 @@ +string('address')->nullable(); + $table->integer('cp')->nullable(); + $table->string('poblation')->nullable(); + $table->string('nif')->nullable(); + $table->decimal('nomina',15,8)->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() { + // + } + +} diff --git a/resources/views/template.blade.php b/resources/views/template.blade.php index a746abd..6b387de 100644 --- a/resources/views/template.blade.php +++ b/resources/views/template.blade.php @@ -242,8 +242,8 @@ diff --git a/resources/views/user/create.blade.php b/resources/views/user/create.blade.php new file mode 100644 index 0000000..ce8a262 --- /dev/null +++ b/resources/views/user/create.blade.php @@ -0,0 +1,123 @@ +@extends('template') + +@section('content') +
+

+ Empleados + Crear +

+ +
+ + +
+ +
+ +
+
+
+

Formulario de creación

+
+ +
+ {!! csrf_field() !!} +
+ + + @if (count($errors) > 0) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif + + +
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ +
+
+
+
+
+
+
+
+@stop \ No newline at end of file diff --git a/resources/views/user/list.blade.php b/resources/views/user/list.blade.php new file mode 100644 index 0000000..1dd29ab --- /dev/null +++ b/resources/views/user/list.blade.php @@ -0,0 +1,70 @@ +@extends('template') + +@section('content') +
+

+ Empleados + Listado +

+ +
+ + +
+ +
+ +
+
+
+

Usuarios / Empleados

+
+ +
+ + + + + + + + + + + @foreach($users as $user) + + + + + + + @endforeach + +
NombreEmailRol
{{$user->name}}{{$user->email}} + @if($user->fk_role == 0) + Sin rol + @endif + @if($user->fk_role == 1) + Administrador + @endif + @if($user->fk_role == 2) + Administración + @endif + @if($user->fk_role == 3) + Recepción + @endif + @if($user->fk_role == 4) + Profesor + @endif + + / +
+
+
+
+
+
+@stop \ No newline at end of file diff --git a/resources/views/user/view.blade.php b/resources/views/user/view.blade.php new file mode 100644 index 0000000..c8a1943 --- /dev/null +++ b/resources/views/user/view.blade.php @@ -0,0 +1,124 @@ +@extends('template') + +@section('content') +
+

+ Empleados + Crear +

+ +
+ + +
+ +
+ +
+
+
+

Formulario de edición / vista

+
+ +
+ {!! csrf_field() !!} +
+ + + @if (count($errors) > 0) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif + + +
+ +
+
+ + + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ +
+
+
+
+
+
+
+
+@stop \ No newline at end of file