/home/dvjjulio/softtrash/app/Http/Controllers/GiroController.php
<?php

namespace Trash\Http\Controllers;

use Illuminate\Http\Request;

use Trash\Http\Requests;
use Trash\Http\Controllers\Controller;
use Trash\Giro;

class GiroController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
         return view('catalogo.giro');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function saveGiro()
    {
        //
        $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( 
                'giro'        => 'required',
                );
        $messages    =  array(
                'giro.required'       => 'Por favor ingrese el Giro de la Empresa',
                 );

        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, Giro $g = null)
    {
        try{
            \DB::beginTransaction();
            if( $data[ 'id' ] != 'undefined'){
                $giro = Giro::find($data['id']);
                $existGiro=$this->checkGiro($data);
                if( $existGiro==false )throw new \Exception('Ya existe giro: '.$data['giro']);
            }
            else {
                $giro = new Giro();
                $existGiro=$this->checkGiro($data);
                if( $existGiro==false )throw new \Exception('Ya existe giro: '.$data['giro']);
            }
            $giro->giro         = $data['giro'];
            $giro->status_id          = 1;

            $giro->save();

        } catch( \Exception $e ){
            \DB::rollback();
            return array( 'status' => false, 'message' => $e->getMessage() );
        }
        \DB::commit();
        return  array( 'status' => true , 'data' => $giro );
    }

    public function checkGiro($data){
         $exist = Giro::where('giro','=',$data['giro'])->get();
        if( count( $exist ) < 1){
             return true;
         }else{
            return false;
         }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function showGiro()
    {
        //
        $giro =   Giro::all();
        return view('catalogo.giroAll')->with(compact('giro'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getGiro()
    {
        //
        $data = \Input::all();
        $giro = Giro::find($data['id']);
        return response()->json(array('status'=> true, 'data'=>$giro));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}