/home/dvjjulio/softtrash/app/Http/Controllers/VehiculoController.php
<?php
namespace Trash\Http\Controllers;
use Illuminate\Http\Request;
use Trash\Http\Requests;
use Trash\Http\Controllers\Controller;
use Trash\User;
use Trash\Camiones;
use Auth;
class VehiculoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$chofers = User::all();
return view('catalogo.vehiculo')->with(compact('chofers'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function saveVehiculo()
{
//
$data = \Input::all();
$validator = $this->get_validate($data);
if( $validator->fails() )
return response()->json( array( 'status' => false, 'message' => $validator->errors()->all() ));
return response()->json( $this->store($data));
}
public function get_validate( $data ){
$array_rules = array(
'placas' => 'required',
'user_id' => 'required',
);
$messages = array(
'placas.required' => 'Por favor ingrese las Placas',
'user_id.required' => 'Por favor ingrese selecciona un Chofer',
);
return \Validator::make( $data, $array_rules, $messages );
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store($data, Camiones $c = null)
{
try{
\DB::beginTransaction();
if( $data[ 'id' ] != 'undefined'){
$camion = Camiones::find($data['id']);
$existChofer = $this->checkChofer($data);
if( $existChofer == false )throw new \Exception('Este Chofer: '.$data['usuario']. ' Ya tiene un Camión Asignado');
}
else {
$camion = new Camiones();
//$existVehiculo = $this->checkCamion($data);
$existChofer = $this->checkChofer($data);
/*if( $existVehiculo == false )throw new \Exception('Ya existe camion con placas: '.$data['placas']);*/
if( $existChofer == false )throw new \Exception('Este Chofer: '.$data['usuario']. ' Ya tiene un Camión Asignado');
}
$camion->placas = $data['placas'];
$camion->descripcion = $data['desc'];
$camion->user_id = $data['user_id'];
$camion->user_created = Auth::user()->id;
$camion->save();
} catch( \Exception $e ){
\DB::rollback();
return array( 'status' => false, 'message' => $e->getMessage() );
}
\DB::commit();
return array( 'status' => true , 'data' => $camion );
}
public function checkCamion($data){
$exist = Camiones::where('placas','=',$data['placas'])->get();
if( count( $exist ) < 1){
return true;
}else{
return false;
}
}
public function checkChofer($data){
//return $data['user_id'];
if($data['user_id'] == 0 | $data['user_id'] == 'Null' ){
return true;
}else{
$exist = Camiones::where('user_id','=',$data['user_id'])->get();
if( count( $exist ) < 1){
return true;
}else{
return false;
}
}
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function showVehiculo()
{
//
$camiones = new Camiones();
$camion = $camiones->getCamiones();
$chofers = User::all();
return view('catalogo.vehiculoAll')->with(compact('camion','chofers'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function getVehiculo()
{
//
$data = \Input::all();
$camiones = new Camiones();
return $camion = $camiones->getCamion($data['id']);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}