<?php 
session_start();
class procesar_datos
{
	

	function procesar_datos()
	{	
		
		/*
		$_SESSION["ls_hostname"] = "localhost";
		$_SESSION["ls_login"] = "postgres";
		$_SESSION["ls_password"] = "adminipostel";
		$_SESSION["ls_database"] = "db_ipostel_2011";
		$_SESSION["ls_gestor"] = "POSTGRES";
		$_SESSION["ls_logo"] = "logo_ipsfa.jpg";
		$_SESSION["ls_width"] = "70";
		$_SESSION["ls_height"] = "70";
		$_SESSION["ls_port"] = "5432";					
		$_SESSION["la_empresa"]["codemp"] = "0001";
		$_SESSION["la_logusr"] = "SIGESP";	
		*/
		global $ruta;
		$ruta = '../';
		require_once($ruta."shared/class_folder/sigesp_include.php");
		$io_include=new sigesp_include();
		$io_conexion=$io_include->uf_conectar();
		require_once($ruta."shared/class_folder/class_sql.php");
		$this->io_sql=new class_sql($io_conexion);	
		require_once($ruta."shared/class_folder/class_mensajes.php");
		$this->io_mensajes=new class_mensajes();
		require_once($ruta."shared/class_folder/sigesp_conexiones.php");
		$this->io_conexiones=new conexiones();	
		require_once($ruta."shared/class_folder/class_funciones.php");
		$this->io_funciones=new class_funciones();		
		require_once($ruta."shared/class_folder/sigesp_c_seguridad.php");
		$this->io_seguridad= new sigesp_c_seguridad();				
        $this->ls_codemp=$_SESSION["la_empresa"]["codemp"];
		

	}


	function ejecutar($parametros=array()){
				
				
								
				
				$query_rs = "  SELECT * FROM rpc_proveedor WHERE cod_pro ILIKE'2%' OR cod_pro ILIKE'4%' ORDER BY nompro						  
				            ";
		
				$this->rs_data=$this->io_sql->select($query_rs);			
				
				//echo $numero.'.- '.$datos['codemp'].' | '.$datos['codorg'].' | '.$datos['desorg'].' | '.$datos['nivorg'].' | '.$datos['padorg'].'<br>';			
				
				
				if($this->rs_data==false){		
					$this->io_sql->rollback();					
					$metodo = 'ejecutar';
					$mensaje = '<b>CLASE:</b> '.get_class($this).' <br><b>METODO:</b> '.$metodo.' <br><b>ERROR->:</b><br>'.$this->io_sql->message;					
					$this->io_mensajes->message($mensaje);
					return false;
					
				}
				
				$numero=0;
				$this->io_sql->begin_transaction();
				foreach($this->rs_data as $datos){
				$numero++;	
					
					$rif = explode('-',$datos['rifpro']);
					$datos['tipo'] = substr($datos['cod_pro'],0,1)==2?1:2; 
					$codcliente = str_pad($numero,10,'0',STR_PAD_LEFT);	
									
					$query_rs = "  INSERT INTO cxc_clientes 
									(codemp, codcliente, tipperrif, numpririf, numterrif, 
									nitcli, id_tipo_cliente, nombre_cliente, cliente_abvr, id_zona, 
									id_vend, id_clasif_cliente, dircliente, direntrega, codpai, codest, 
									codmun, codpar, codciu, codpostal, faxcliente, telcliente, emailcliente, 
									webcliente, observcliente, sc_cuenta, estclient, limitecred, 
									diascred, descglob, cedularesp, nombreresp, cargoresp, emailresp, 
									fecregcliente, fecreg, usureg, horareg, fecmod, usumod, horamod)
					               VALUES ('0001', '".$codcliente."', '".$rif[0]."', '".$rif[1]."', '".$rif[2]."', '', 1, 
											'".$datos['nompro']."', '', 0, 1, ".$datos['tipo'].", '".$datos['dirpro']."', '', 
											'".$datos['codpai']."', 
											'".$datos['codest']."', 
											'".$datos['codmun']."', 
											'".$datos['codpar']."', 
											'--', 
											'', '', '".$datos['telpro']."', '', '', '', 
											'11203010102001', 'A', 0, 0, 0, '', '', '', '', '2015-01-01', '2015-01-01', 
											'ADMINISTRADOR', '16:28:00', NULL, NULL, NULL);
					  
				            ";
					
					//echo $query_rs.'<br><br>';
					$this->rs_data2=$this->io_sql->select($query_rs);
					
					
					if($this->rs_data2==false){		
						$this->io_sql->rollback();					
						$metodo = 'ejecutar';
						$mensaje = '<b>CLASE:</b> '.get_class($this).' <br><b>METODO:</b> '.$metodo.' <br><b>ERROR->:</b><br>'.$this->io_sql->message;					
						$this->io_mensajes->message($mensaje);
						return false;						
					}
					
					echo $codcliente.' - '.$datos['nompro'].' *******> '.$datos['rifpro'].'<br>';
					
					$ls_sql="   LOCK TABLE rpc_beneficiario IN ACCESS EXCLUSIVE MODE;
								INSERT INTO rpc_beneficiario (   codemp, ced_bene, codpai, codest, codmun, 
																  codpar, codtipcta, rifben, nombene, apebene, dirbene, 
																  telbene, celbene, email, sc_cuenta, codbansig, codban, ctaban, 
																  foto, fecregben, nacben, numpasben, tipconben, 
																  tipcuebanben, sc_cuentarecdoc,cliente) 
								  SELECT  codemp, tipperrif||numpririf||numterrif, codpai, codest, codmun, 
										codpar, 's1', tipperrif||'-'||numpririf||'-'||numterrif, nombre_cliente, '', dircliente, 
										telcliente, '', emailcliente, sc_cuenta, '', '---', '', 
										NULL, fecreg, tipperrif, '', 'O', NULL, '','1'
								  FROM cxc_clientes
								 WHERE codcliente='".$codcliente."'";
							 
											 
					$rs_data=$this->io_sql->select($ls_sql);
					if($rs_data===false){
					    $this->io_sql->rollback();						
						$metodo = 'InsertarBenefCliente';
						$mensaje = '<b>CLASE:</b> '.get_class($this).' <br><b>METODO:</b> '.$metodo.' <br><b>ERROR-></b><br>'.$this->io_sql->message;					
						$this->io_mensajes->message($mensaje);
						return false;				
					}
					
					
				}
				$this->io_sql->commit();
				
	
	}//end function ejecutar




}


$procesar = new procesar_datos;

$procesar->ejecutar();



?>