<?php
class vehiculoActions extends wtActions
{
    public function executeIndex()
    {
    }
    
    public function executeList()
    {
        LayoutTemplate::getInstance()->setUseBootstrap();
    	$this->block = $this->getRequestParameter( 'block' );
        
        $this->Titulo = "Vehiculo";
        $this->NomMod = ParameterWR::getInstance()->getModule();
    }
    
    public function executeGrid()
    {
        
        $block = $this->getRequestParameter( 'block' );
        $page  = $this->getRequestParameter( 'page' );
        
        //*set_header_utf8();
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        $o_GridAction = new GridAction();
        $o_GridAction->setClassFind( new TR_VehiculoFN() );
        
        $o_GridAction->addConditionFilter( "Descripcion", TR_VehiculoFN::VehiculoPlaca, "LIKE", "%%s%" );
        
        $o_GridAction->addOrder( "Descripcion", TR_VehiculoFN::VehiculoPlaca );
        
        $o_GridAction->addOrderDefault( TR_VehiculoFN::VehiculoPlaca , true );
        
        $o_GridAction->addRowsToShow( "IdVehiculo", "VehiculoPlaca", "VehiculoMarca", "VehiculoMdelo", "EstadoDesc" );
              
        $o_GridAction->setNumberRecordsShow( 15 );
        
        $o_GridAction->initialize();
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        $a_ResultJson['page']    = $o_GridAction->getPage();
        $a_ResultJson['total']   = $o_GridAction->getTotalPage();
        $a_ResultJson['records'] = $o_GridAction->getTotalRecords();
        $a_ResultJson['rows']    = $o_GridAction->getResultArray();
        //pr( $a_ResultJson );
        echo json_encode( $a_ResultJson );

        return ViewResponse::NONE;   
    }
    
    public function executeAddedit()
    {
        LayoutTemplate::getInstance()->setUseBootstrap();
        $this->block    = $this->getRequestParameter( 'block' );
        $this->Id       = $this->getRequestParameter( 'Id' );
        $this->TipoProc = $this->getRequestParameter( 'TipoProc' );
        
        $this->Titulo = "Nuevo Vehiculo";
        $this->NomMod = ParameterWR::getInstance()->getModule();
        
        /************/
        
        $o_DefaultBN = new TR_VehiculoFN();
        $o_DefaultBN->setEstado( 'A' );
        $this->o_DefaultBN = $o_DefaultBN;
        
        /*************/
        //$this->o_MST_UnidadMastBN = new MST_UnidadMastBN();
        if( $this->TipoProc == "upd" )
        {
            $this->Titulo = "Editar Vehiculo";
            $o_DefaultFN = new TR_VehiculoFN();
            $this->o_DefaultBN = $o_DefaultFN->selectByPK( $this->Id );
        }
    }
    
    public function executeSaveAddEdit()
    {
        $block    = $this->getRequestParameter( 'block' );
        $Id       = $this->getRequestParameter( 'Id' );
        $TipoProc = $this->getRequestParameter( 'TipoProc' );
        $TypeSub  = $this->getRequestParameter( 'TypeSub' );
        
        $IdCompania = myUser::getUser()->getIdBusiness();
        $IdSucursal = myUser::getUser()->getIdSucursal();
        
        $VehiculoPlaca = $this->getRequestParameter( 'VehiculoPlaca' );
        $VehiculoPlaca2 = $this->getRequestParameter( 'VehiculoPlaca2' );
        $VehiculoMarca  = $this->getRequestParameter( 'VehiculoMarca' );
        $VehiculoMdelo  = $this->getRequestParameter( 'VehiculoMdelo' );
        $NroMotor       = $this->getRequestParameter( 'NroMotor' );
        $Altura         = $this->getRequestParameter( 'Altura' );
        $Volumen        = $this->getRequestParameter( 'Volumen' );
        $NroSOAT        = $this->getRequestParameter( 'NroSOAT' );
        $VencSOAT       = $this->getRequestParameter( 'VencSOAT' );
        $NroMTC         = $this->getRequestParameter( 'NroMTC' );
        $RUCtransportista         = $this->getRequestParameter( 'RUCtransportista' );
        $Estado         = $this->getRequestParameter( 'Estado' );
        
        $o_Con = Conexion::getConexion();
        try 
        {
            $o_Con->begin();
            
            $o_DefaultDP = new TR_VehiculoDP();
            $o_DefaultDP->setVehiculoPlaca( $VehiculoPlaca );
            $o_DefaultDP->setVehiculoPlaca2( $VehiculoPlaca2 );
            $o_DefaultDP->setVehiculoMarca( $VehiculoMarca );
            $o_DefaultDP->setNroMotor( $NroMotor );
            $o_DefaultDP->setAltura( $Altura );
            $o_DefaultDP->setVolumen( $Volumen );
            $o_DefaultDP->setVehiculoMdelo( $VehiculoMdelo );
            $o_DefaultDP->setNroSOAT( $NroSOAT );
            $o_DefaultDP->setNroMTC( $NroMTC );
            $o_DefaultDP->setVencSOAT( $VencSOAT );
            $o_DefaultDP->setRUCtransportista( $RUCtransportista );
            $o_DefaultDP->setEstado( $Estado );
            if( $TipoProc == "nvo" )
            {
                //NUEVO
                $IdReg = TR_VehiculoFN::recuperarId();
                
                $o_DefaultDP->setIdCompania( $IdCompania );
                $o_DefaultDP->setIdSucursal( $IdSucursal );
                $o_DefaultDP->setIdVehiculo( $IdReg );
                $o_DefaultDP->setFechaCreacion( date( "Y-m-d H:i:s" ) );
                $o_DefaultDP->setUsuarioCreacion( myUser::getUser()->getUserId() );
                $o_DefaultDP->setFechaModificacion( date( "Y-m-d H:i:s" ) );
                $o_DefaultDP->setUsuarioModificacion( myUser::getUser()->getUserId() );
                $b_Result = $o_DefaultDP->save();
                $Id = $IdReg;
            }
            else 
            {
                //UPDATE
                $o_DefaultDP->setIdVehiculo( ( int ) $Id );
                $o_DefaultDP->setFechaModificacion( date( "Y-m-d H:i:s" ) );
                $o_DefaultDP->setUsuarioModificacion( myUser::getUser()->getUserId() );
                $b_Result = $o_DefaultDP->update();
            }
            $a_Rs['type_sub'] = $TypeSub;
            $a_Rs['error']    = false;
            $a_Rs['id']       = $Id;
            
            $o_Con->commit();
        }
        catch ( Exception $o_Exception )
        {
            $o_Con->rollback();
            
            $a_Rs['error']         = true;
            $a_Rs['mensaje_error'] = $o_Exception->getMessage();
        }
        echo json_encode( $a_Rs );
        return ViewResponse::NONE;
    }
    
    public function executeAudi()
    {
        $block        = $this->getRequestParameter( 'block' );
        $Id           = $this->getRequestParameter( 'Id' );
        
        $this->Topico = "Vehiculo";
        
        $o_DefaultFN = new TR_VehiculoFN();
        $this->o_ObjectBN = $o_DefaultFN->selectByPK( $Id );
    }
    
    public function executeViewdel()
    {
        $this->block    = $this->getRequestParameter( 'block' );
        $this->Id       = $this->getRequestParameter( 'Id' );
        $this->TipoProc = $this->getRequestParameter( 'TipoProc' );
        
        $this->NomMod = ParameterWR::getInstance()->getModule();
        
        $o_DefaultFN = new TR_VehiculoFN();
        $this->o_DefaultBN = $o_DefaultFN->selectByPK( $this->Id );
    }
    
    public function executeDeleteRow()
    {
        $block    = $this->getRequestParameter( 'block' );
        $Id       = $this->getRequestParameter( 'Id' );
        $TipoProc = $this->getRequestParameter( 'TipoProc' );
        try 
        {
            $o_DefaultDP = new TR_VehiculoDP();
            $o_DefaultDP->setIdVehiculo( ( int ) $Id );
            $o_DefaultDP->setEstado( "I" );
            $o_DefaultDP->setUsuarioModificacion( myUser::getUser()->getUserId() );
            $o_DefaultDP->setFechaModificacion( date( "Y-m-d H:i:s" ) );
            $o_DefaultDP->update();
            //$o_DefaultDP->delete();

            $a_Rs['error']   = false;
            $a_Rs['id']      = $Id;
        }
        catch ( Exception $o_Exception )
        {
            $a_Rs['error']   = true;
            $a_Rs['mensaje_error_ex'] = $o_Exception->getMessage();
            $a_Rs['mensaje_error'] = "No se pudo eliminar el registro.";
        }
        echo json_encode( $a_Rs );
        return ViewResponse::NONE;
    }
}