<?php
class ubigeoActions extends wtActions
{
    public function executeIndex()
    {
    }
    
    public function executeList()
    {
        LayoutTemplate::getInstance()->setUseBootstrap();
    	$this->block = $this->getRequestParameter( 'block' );
    	
    	$o_GSS_CompaniaFN = new GSS_CompaniaFN();
        $this->a_GSS_Compania= $o_GSS_CompaniaFN->select();
    }
    
    public function executeGrid()
    {
        set_header_utf8();
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        $o_GridAction = new GridAction();
        $o_GridAction->setClassFind( new MST_UbigeoFN() );
        
        $o_GridAction->addAssignFilter( "Tipo" );
                
        $o_GridAction->addConditionFilter( "Descripcion", MST_UbigeoFN::Descripcion, "LIKE", "%%s%" );
        
        $o_GridAction->addOrder( "Descripcion", MST_UbigeoFN::Descripcion );
                
        $o_GridAction->setNumberRecordsShow( 30 );
        
        $o_GridAction->setControlArray( false );
        
        $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();
        $a_ResultSelect    = $o_GridAction->getResultSelect();
        $a_Rs = array();
        foreach ( $a_ResultSelect as $iKey => $o_ObjClassBN )
        {
        	$TipoUbigeo = $o_ObjClassBN->getTipo();
        	$descripcionProv = "";
            $descripcionDist = "";
            $descripcionDpto = "";
            
            switch($TipoUbigeo){
            		
                	case "DEPT":
                				//$IdUbigeo = $o_MST_UbigeoDP->getIdUbigeo(); 
                				
                				$o_MST_UbigeoFN = new MST_UbigeoFN();
                				$o_MST_UbigeoBN = $o_MST_UbigeoFN->selectByPk($o_ObjClassBN->getIdUbigeo());
                				
                				$descripcionDpto = $o_MST_UbigeoBN->getDescripcion();
                				$descripcionProv = "";
                				$descripcionDist = "";
                				break;
                	case "PROV":		
                				$o_MST_UbigeoFN = new MST_UbigeoFN();
                				$o_MST_UbigeoBN = $o_MST_UbigeoFN->selectByPk($o_ObjClassBN->getIdUbigeo());
                				
                				$descripcionProv = $o_MST_UbigeoBN->getDescripcion();
                				$Departamento = $o_MST_UbigeoFN->getPertenece();
                				
                				$o_MST_UbigeoFN = new MST_UbigeoFN();
                				$o_MST_UbigeoBN = $o_MST_UbigeoFN->setIdUbigeo($Departamento);
                				$a_MST_UbigeoFN = $o_MST_UbigeoFN->select();
                				if(is_array($a_MST_UbigeoFN)){
                					foreach ($a_MST_UbigeoFN as $key => $o_MST_UbigeoFN){
                						$descripcionDpto = $o_MST_UbigeoFN->getDescripcion();
                					}
                				}
                				$descripcionDist = "";
                				break;
                	case "DIST":		
                				
                				$descripcionDist = $o_ObjClassBN->getDescripcion();
                				$Provincia = $o_ObjClassBN->getPertenece();
                				
                				$codUbigeoDIST = substr($o_ObjClassBN->getIdUbigeo(),0,4);
                				$o_MST_UbigeoFN = new MST_UbigeoFN();
                				$o_MST_UbigeoBN = $o_MST_UbigeoFN->setIdUbigeo($Provincia);
                				$a_MST_UbigeoFN = $o_MST_UbigeoFN->select();
                				if(is_array($a_MST_UbigeoFN)){
                					foreach ($a_MST_UbigeoFN as $key => $o_MST_UbigeoFN){
                						$descripcionProv = $o_MST_UbigeoFN->getDescripcion();
                						$Departamento 	 = $o_MST_UbigeoFN->getPertenece();
                						$o_MST_UbigeoFNT = new MST_UbigeoFN();
		                				$o_MST_UbigeoBN = $o_MST_UbigeoFNT->setIdUbigeo($Departamento);
		                				$a_MST_UbigeoFN = $o_MST_UbigeoFNT->select();
		                				if(is_array($a_MST_UbigeoFN)){
		                					foreach ($a_MST_UbigeoFN as $key => $o_MST_UbigeoFN){
		                						$descripcionDpto = $o_MST_UbigeoFN->getDescripcion();
		                					}
		                				}
                					}
                				}
                				break;
                }
        
            

            
           
                       
            $a_Rs[] = array(  
                                "IdUbigeo"   		 => utf8_encode( $o_ObjClassBN->getIdUbigeo() ),
                                "Tipo"         => utf8_encode( $o_ObjClassBN->getTipo() ),
                                "Departamento"         => utf8_encode( $descripcionDpto ),
                                "Provincia"			 => utf8_encode( $descripcionProv ),
                                "Distrito"      => utf8_encode( $descripcionDist ),
                           );
        }
        $a_ResultJson['rows'] = $a_Rs;
        //pr( $a_ResultJson );
        echo json_encode( $a_ResultJson );

        return ViewResponse::NONE;
    }
    
    public function executeAddedit()
    {
        LayoutTemplate::getInstance()->setUseBootstrap();
        $this->block      = $this->getRequestParameter( 'block' );
        $this->i_Id       = $this->getRequestParameter( 'i_Id' );
        $this->s_TipoProc = $this->getRequestParameter( 's_TipoProc' );
        
        //Listar todos los departamentos
        $o_MST_UbigeoFN = new MST_UbigeoFN();
        $o_MST_UbigeoFN->setTipo('DEPT');
        $this->a_MST_UbigeoFNDepartamentos = $o_MST_UbigeoFN->select();
        
              
        $this->s_Titulo = "Nuevo Ubigeo";
        $MST_UbigeoBN = new MST_UbigeoBN();
        $MST_UbigeoBN->setTipo('DEPT');
        $this->o_MST_UbigeoBN= $MST_UbigeoBN;
        $this->TipoUbigeo = "DEPT";
        $this->a_MST_UbigeoFNProvincias = null;
        $this->IdDpto = null;
        $this->IdProv = null;
        $this->IdDist = null;
      	
        
        
        
        if( $this->s_TipoProc == "upd" )
        {
        	
			$o_MST_UbigeoFN = new MST_UbigeoFN();
			$o_ObjClassBN =$o_MST_UbigeoFN->selectByPK( $this->i_Id );
			$TipoUbigeo = $o_ObjClassBN->getTipo();
			$this->o_MST_UbigeoBN = $o_ObjClassBN;
			$this->TipoUbigeo = $TipoUbigeo;
			$IdDpto = "";
			$IdProv = "";
			$IdDist = "";
			
              switch($TipoUbigeo){
		    		
		        	case "DEPT":
		        				//$IdUbigeo = $o_MST_UbigeoDP->getIdUbigeo(); 
		        				
		        				$o_MST_UbigeoFN = new MST_UbigeoFN();
		        				$o_MST_UbigeoBN = $o_MST_UbigeoFN->selectByPk($o_ObjClassBN->getIdUbigeo());
		        				
		        				$IdDpto = $o_MST_UbigeoBN->getIdUbigeo();
		        				$IdProv = "";
		        				$IdDist = "";
		        				break;
		        	case "PROV":	
		        				
		        					
		        				$o_MST_UbigeoFN = new MST_UbigeoFN();
		        				$o_MST_UbigeoBN = $o_MST_UbigeoFN->selectByPk($o_ObjClassBN->getIdUbigeo());
		        				
		        				$IdProv = $o_MST_UbigeoBN->getIdUbigeo();
		        				$Departamento = $o_MST_UbigeoBN->getPertenece();
		        				
		        				
		        				$o_MST_UbigeoFN = new MST_UbigeoFN();
		        				$o_MST_UbigeoFN->setIdUbigeo($Departamento);
		        				$a_MST_UbigeoFN = $o_MST_UbigeoFN->select();
		        				if(is_array($a_MST_UbigeoFN)){
		        					foreach ($a_MST_UbigeoFN as $key => $o_MST_UbigeoFN){
		        						$IdDpto = $o_MST_UbigeoFN->getIdUbigeo();
		        					}
		        				}
		        				
		        				$IdDist = "";
		        				break;
		        	case "DIST":		
		        				
		        				$IdDist = $o_ObjClassBN->getDescripcion();
		        				$Provincia = $o_ObjClassBN->getPertenece();
		        				
		        				$codUbigeoDIST = substr($o_ObjClassBN->getIdUbigeo(),0,4);
		        				$o_MST_UbigeoFN = new MST_UbigeoFN();
		        				$o_MST_UbigeoFN->setIdUbigeo($Provincia);
		        				$a_MST_UbigeoFN = $o_MST_UbigeoFN->select();
		        				if(is_array($a_MST_UbigeoFN)){
		        					foreach ($a_MST_UbigeoFN as $key => $o_MST_UbigeoFN){
		        						$IdProv = $o_MST_UbigeoFN->getIdUbigeo();
		        						$Departamento 	 = $o_MST_UbigeoFN->getPertenece();
		        						$o_MST_UbigeoFNT = new MST_UbigeoFN();
		                				$o_MST_UbigeoBN = $o_MST_UbigeoFNT->setIdUbigeo($Departamento);
		                				$a_MST_UbigeoFN = $o_MST_UbigeoFNT->select();
		                				if(is_array($a_MST_UbigeoFN)){
		                					foreach ($a_MST_UbigeoFN as $key => $o_MST_UbigeoFN){
		                						$IdDpto = $o_MST_UbigeoFN->getIdUbigeo();
		                						$o_MST_UbigeoFNT = new MST_UbigeoFN();
		                						$o_MST_UbigeoFNT->setPertenece($IdDpto);
						        				$a_MST_UbigeoFNT = $o_MST_UbigeoFNT->select();
						        				
						        				$this->a_MST_UbigeoFNProvincias = $a_MST_UbigeoFNT;
		                					}
		                				}
		        					}
		        				}
		        				break;
		        }
            
            $this->IdDpto = $IdDpto;
            $this->IdProv = $IdProv;
                       
            $this->s_Titulo = "Editar Ubigeo";
           
            
           
        }
    }
    
    public function executeRequest()
    {
        $block      = $this->getRequestParameter( 'block' );
        $IdDepartamento = $this->getRequestParameter( 'IdDepartamento' );
        $TipoProc   = $this->getRequestParameter( 'TipoProc' );
        
        switch ( $TipoProc )
        {
            case "LoadProv":
                $o_MST_UbigeoFN = new MST_UbigeoFN();
                $o_MST_UbigeoFN->setPertenece( $IdDepartamento);
        		$o_MST_UbigeoFN->setTipo('PROV');
        		$a_Result = $o_MST_UbigeoFN->select();
                $s_NameId  = "getIdUbigeo";
                $s_NameDes = "getDescripcion";
            break;
        }
        foreach ( $a_Result as $i_Key => $o_ObjectBN )
        {
            $a_lst[$o_ObjectBN->$s_NameId()] = $o_ObjectBN->$s_NameDes();
        }
        echo json_encode( $a_lst );
        return ViewResponse::NONE;
    }
    
    public function executeSaveAddEdit()
    {
        $block      = $this->getRequestParameter( 'block' );
        $i_Id       = $this->getRequestParameter( 'i_Id' );
        $s_TipoProc = $this->getRequestParameter( 's_TipoProc' );
        $TypeSub    = $this->getRequestParameter( 'TypeSub' );
        
        $TipoUbigeo         = $this->getRequestParameter( 'TipoUbigeo' );
        $IdDepartamento		= $this->getRequestParameter( 'IdDepartamento' );
        $IdProvincia			= $this->getRequestParameter( 'IdProvincia' );
        $Descripcion		= $this->getRequestParameter( 'Descripcion' );
        
        try 
        {
            $o_MST_UbigeoDP = new MST_UbigeoDP();
            $o_MST_UbigeoDP->setTipo( $TipoUbigeo );
            $o_MST_UbigeoDP->setDescripcion( $Descripcion );
            
            if( $s_TipoProc == "nvo" )
            {
                //INSERT
                
                switch($TipoUbigeo){
                	case "DEPT":
                				//$IdUbigeo = $o_MST_UbigeoDP->getIdUbigeo(); 
                			
                				
                				$o_MST_UbigeoFN = new MST_UbigeoFN();
                				
                				$o_MST_UbigeoFN->setTipo($TipoUbigeo);
                				
                				$a_MST_UbigeoFN = $o_MST_UbigeoFN->select();
                				
                				
                				$numeroDepartamentos = count($a_MST_UbigeoFN);
                				$codUbigeoDPTOT = str_pad($numeroDepartamentos + 1,2,"00",STR_PAD_LEFT);
                				$idUbigeo = $codUbigeoDPTOT."0000";
                				$idUbigeoPertenece = "";
                				break;
                	case "PROV":		
                				$codUbigeoDPTO = substr($IdDepartamento,0,2);
                				$o_MST_UbigeoFN = new MST_UbigeoFN();
                				$o_MST_UbigeoFN->setPertenece($IdDepartamento);
                				$a_MST_UbigeoFN = $o_MST_UbigeoFN->select();
                				$numeroProvincias = count($a_MST_UbigeoFN);
                				$codUbigeoPROVT = str_pad($numeroProvincias + 1,2,"00",STR_PAD_LEFT);
                				$idUbigeo = $codUbigeoDPTO.$codUbigeoPROVT."00";
                				$idUbigeoPertenece = $IdDepartamento;
                				break;
                	case "DIST":		
                				$codUbigeoDPTO = substr($IdDepartamento,0,2);
                				$codUbigeoPROVT = substr($IdProvincia,2,2);;
                				$o_MST_UbigeoFN = new MST_UbigeoFN();
                				$o_MST_UbigeoFN->setPertenece($IdProvincia);
                				$a_MST_UbigeoFN = $o_MST_UbigeoFN->select();
                				$numeroDistritos = count($a_MST_UbigeoFN);
                				$codUbigeoDIST = str_pad($numeroDistritos + 1,2,"00",STR_PAD_LEFT);
                				$idUbigeo = $codUbigeoDPTO.$codUbigeoPROVT.$codUbigeoDIST;
                				$idUbigeoPertenece = $IdProvincia;
                				break;
                }
                
                $o_MST_UbigeoDP->setIdUbigeo( $idUbigeo );
                $o_MST_UbigeoDP->setPertenece($idUbigeoPertenece);
            
                $o_MST_UbigeoDP->setFechaCreacion( date( "Y-m-d H:i:s" ) );
                $o_MST_UbigeoDP->setUsuarioCreacion( myUser::getUser()->getUserId() );
                $o_MST_UbigeoDP->setEstado('A');
                
                $b_Result = $o_MST_UbigeoDP->save();
                $i_Id = $idUbigeo;
            }
            else 
            {
                //UPDATE
                              
                
                $o_MST_UbigeoDP->setIdUbigeo(  $i_Id );
                $o_MST_UbigeoDP->setDescripcion( $Descripcion );
                
                $o_MST_UbigeoDP->setFechaModificacion( date( "Y-m-d H:i:s" ) );
                $o_MST_UbigeoDP->setUsuarioModificacion( myUser::getUser()->getUserId() );
                $b_Result = $o_MST_UbigeoDP->update();
            }
            $a_Rs['type_sub'] = $TypeSub;
            $a_Rs['error']    = false;
            $a_Rs['id']       = $i_Id;
        }
        catch ( Exception $o_Exception )
        {
            $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' );
        $i_Id       = $this->getRequestParameter( 'i_Id' );
        
                
        $o_MST_Ubigeo = new MST_UbigeoFN();
        $this->o_ObjectBN = $o_MST_Ubigeo->selectByPK( $i_Id);
    }
    
    public function executeViewdel()
    {
        $this->block      = $this->getRequestParameter( 'block' );
        $this->i_Id       = $this->getRequestParameter( 'i_Id' );
        $this->s_TipoProc = $this->getRequestParameter( 's_TipoProc' );
        
         //Listar todos los departamentos
        $o_MST_UbigeoFN = new MST_UbigeoFN();
        $o_MST_UbigeoFN->setTipo('DEPT');
        $this->a_MST_UbigeoFNDepartamentos = $o_MST_UbigeoFN->select();
        
              
        $o_MST_UbigeoFN = new MST_UbigeoFN();
        $o_ObjClassBN = $o_MST_UbigeoFN->selectByPK( $this->i_Id );
        $this->o_MST_UbigeoBN = $o_ObjClassBN;
        $TipoUbigeo = $o_ObjClassBN->getTipo();
        $this->TipoUbigeo = $TipoUbigeo;
        $this->a_MST_UbigeoFNProvincias = null;
        $this->IdDpto = null;
        $this->IdProv = null;
        $this->IdDist = null;
        
        switch($TipoUbigeo){
		    		
	        	case "DEPT":
	        				//$IdUbigeo = $o_MST_UbigeoDP->getIdUbigeo(); 
	        				
	        				$o_MST_UbigeoFN = new MST_UbigeoFN();
	        				$o_MST_UbigeoBN = $o_MST_UbigeoFN->selectByPk($o_ObjClassBN->getIdUbigeo());
	        				
	        				$IdDpto = $o_MST_UbigeoBN->getIdUbigeo();
	        				$IdProv = "";
	        				$IdDist = "";
	        				break;
	        	case "PROV":	
	        				
	        					
	        				$o_MST_UbigeoFN = new MST_UbigeoFN();
	        				$o_MST_UbigeoBN = $o_MST_UbigeoFN->selectByPk($o_ObjClassBN->getIdUbigeo());
	        				
	        				$IdProv = $o_MST_UbigeoBN->getIdUbigeo();
	        				$Departamento = $o_MST_UbigeoBN->getPertenece();
	        				
	        				
	        				$o_MST_UbigeoFN = new MST_UbigeoFN();
	        				$o_MST_UbigeoFN->setIdUbigeo($Departamento);
	        				$a_MST_UbigeoFN = $o_MST_UbigeoFN->select();
	        				if(is_array($a_MST_UbigeoFN)){
	        					foreach ($a_MST_UbigeoFN as $key => $o_MST_UbigeoFN){
	        						$IdDpto = $o_MST_UbigeoFN->getIdUbigeo();
	        					}
	        				}
	        				
	        				$IdDist = "";
	        				break;
	        	case "DIST":		
	        				
	        				$IdDist = $o_ObjClassBN->getDescripcion();
	        				$Provincia = $o_ObjClassBN->getPertenece();
	        				
	        				$codUbigeoDIST = substr($o_ObjClassBN->getIdUbigeo(),0,4);
	        				$o_MST_UbigeoFN = new MST_UbigeoFN();
	        				$o_MST_UbigeoFN->setIdUbigeo($Provincia);
	        				$a_MST_UbigeoFN = $o_MST_UbigeoFN->select();
	        				if(is_array($a_MST_UbigeoFN)){
	        					foreach ($a_MST_UbigeoFN as $key => $o_MST_UbigeoFN){
	        						$IdProv = $o_MST_UbigeoFN->getIdUbigeo();
	        						$Departamento 	 = $o_MST_UbigeoFN->getPertenece();
	        						$o_MST_UbigeoFNT = new MST_UbigeoFN();
	                				$o_MST_UbigeoBN = $o_MST_UbigeoFNT->setIdUbigeo($Departamento);
	                				$a_MST_UbigeoFN = $o_MST_UbigeoFNT->select();
	                				if(is_array($a_MST_UbigeoFN)){
	                					foreach ($a_MST_UbigeoFN as $key => $o_MST_UbigeoFN){
	                						$IdDpto = $o_MST_UbigeoFN->getIdUbigeo();
	                						$o_MST_UbigeoFNT = new MST_UbigeoFN();
	                						$o_MST_UbigeoFNT->setPertenece($IdDpto);
					        				$a_MST_UbigeoFNT = $o_MST_UbigeoFNT->select();
					        				
					        				$this->a_MST_UbigeoFNProvincias = $a_MST_UbigeoFNT;
	                					}
	                				}
	        					}
	        				}
	        				break;
	        }
            
            $this->IdDpto = $IdDpto;
            $this->IdProv = $IdProv;
            
    }
    
    public function executeDeleteRow()
    {
        $block      = $this->getRequestParameter( 'block' );
        $i_Id       = $this->getRequestParameter( 'i_Id' );
        $s_TipoProc = $this->getRequestParameter( 's_TipoProc' );
        try 
        {
            $o_MST_UbigeoDP = new MST_UbigeoDP();
            $o_MST_UbigeoDP->setIdUbigeo(  $i_Id );
            $o_MST_UbigeoDP->setEstado( "I" );
            $o_MST_UbigeoDP->update();

            $a_Rs['error']   = false;
            $a_Rs['id']      = $i_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;
    }

}