Skip to content

Commit

Permalink
Gestion de empleados
Browse files Browse the repository at this point in the history
  • Loading branch information
Borja José committed Sep 9, 2015
1 parent c1557bb commit bcf5131
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 2 deletions.
135 changes: 135 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Helpers\AngularHelper;
use Hash;
use Validator;

class UserController extends Controller {
/*
* This function render's the create form for the employee
*/

public function getCreate() {
return view('user.create');
}

/*
* This function get's the post request and creates the new user into db
*/

public function postCreate(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',
'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]);
}

}
6 changes: 6 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
33 changes: 33 additions & 0 deletions database/migrations/2015_09_09_080436_userExtraFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UserExtraFields extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::table('users', function ($table) {
//Add some extrafields to turn users into employee
$table->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() {
//
}

}
4 changes: 2 additions & 2 deletions resources/views/template.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@
<i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<li><a href=""><i class="fa fa-circle-o"></i> Dar de alta</a></li>
<li><a href=""><i class="fa fa-circle-o"></i> Ver costes</a></li>
<li><a href="{{URL::to('/user/create')}}"><i class="fa fa-circle-o"></i> Dar de alta</a></li>
<li><a href="{{URL::to('/user/list')}}"><i class="fa fa-circle-o"></i> Listar empleados</a></li>
</ul>
</li>
<!-- //Element -->
Expand Down
123 changes: 123 additions & 0 deletions resources/views/user/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
@extends('template')
<!-- Content Header (Page header) -->
@section('content')
<section class="content-header">
<h1>
Empleados
<small>Crear</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> ></a></li>
<li class="active">Crear</li>
</ol>
</section>

<!-- Main content -->
<section class="content">

<div class="row">

<div class="col-md-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Formulario de creación</h3>
</div><!-- /.box-header -->
<!-- form start -->
<form role="form" method="post" action="{{URL::to('/user/create')}}">
{!! csrf_field() !!}
<div class="box-body">

<!-- Error zone -->
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<!-- Error zone end -->

<div class="row">

<div class="col-md-4">
<div class="form-group">
<label>Nombre y apellidos</label>
<input class="form-control" placeholder="" type="text" name="name">
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control" placeholder="" type="text" name="email">
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<label>Contraseña (plano)</label>
<input class="form-control" placeholder="" type="text" name="password">
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<label>Rol que desempeñará</label>
<select name="fk_role" class="form-control">
<option value="1">Administrador</option>
<option value="2">Administración</option>
<option value="3">Recepción</option>
<option value="4">Profesor</option>
</select>
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<label>Dirección postal</label>
<input class="form-control" placeholder="" type="text" name="address">
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<label>Código postal</label>
<input class="form-control" placeholder="" type="text" name="cp">
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<label>Población</label>
<input class="form-control" placeholder="" type="text" name="poblation">
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<label>DNI</label>
<input class="form-control" placeholder="" type="text" name="nif">
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<label>Importe bruto de nómina</label>
<input class="form-control" placeholder="" type="text" name="nomina">
</div>
</div>

<div class="col-md-12 center">
<hr/>
<button type="submit" class="btn btn-xs btn-success center"><i class="fa fa-plus"></i> Crear</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
@stop
70 changes: 70 additions & 0 deletions resources/views/user/list.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
@extends('template')
<!-- Content Header (Page header) -->
@section('content')
<section class="content-header">
<h1>
Empleados
<small>Listado</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> ></a></li>
<li class="active">Listado</li>
</ol>
</section>

<!-- Main content -->
<section class="content">

<div class="row">

<div class="col-md-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Usuarios / Empleados</h3>
</div><!-- /.box-header -->
<!-- form start -->
<div class="box-body">
<table class="table table-bordered">
<thead>
<tr>
<th>Nombre</th>
<th>Email</th>
<th style="width:30px;">Rol</th>
<th style="width:30px;"> </th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>
@if($user->fk_role == 0)
<small class="label pull-right">Sin rol</small>
@endif
@if($user->fk_role == 1)
<small class="label pull-right bg-green">Administrador</small>
@endif
@if($user->fk_role == 2)
<small class="label pull-right bg-blue">Administración</small>
@endif
@if($user->fk_role == 3)
<small class="label pull-right bg-yellow">Recepción</small>
@endif
@if($user->fk_role == 4)
<small class="label pull-right bg-red">Profesor</small>
@endif
</td>
<td>
<a href="{{URL::to('/user/view/'.$user->id)}}" class="btn btn-success btn-xs"><i class="fa fa-eye"></i>/<i class="fa fa-pencil"></i></a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
@stop
Loading

0 comments on commit bcf5131

Please sign in to comment.