<?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 valida_fecha($fechax){
				
				$fechax = trim($fechax);
				
				if(!$fechax){return "01-01-1900";}
				
				$fecha = explode("-",$fechax);
				
				if(count($fecha)<3){return "01-01-1900";}
				
				$fecha[2] = str_pad($fecha[2],4,"19",STR_PAD_LEFT);
				$fecha_completa = $fecha[0].'-'.$fecha[1].'-'.$fecha[2];
				
				return $fecha_completa;
	
	}
	
	
	function ejecutar($parametros=array()){
				
				$nombre_archivo = 'familiares.csv';
				$fp = fopen($nombre_archivo,"r");
				$numero = 1;
				
				while ($linea= fgets($fp,8072)){
							
							$datos = explode('|',$linea);

							
							//PERSONAL						
							$datos[0] = trim(str_replace(".","",$datos[0])); //cedula
							$datos[1] = trim($datos[1]); //nombre
							$datos[2] = trim($datos[2]); //apellido
							$datos[3] = trim($datos[3]); // edo civil
							$datos[4] = trim($datos[4]); // estatura
							$datos[5] = trim($datos[5]); // peso
							$datos[6] = trim($datos[6]); // telefono
							$datos[7] = trim($datos[7]); // cargo
							$datos[8] = trim($datos[8]); // correo
							$datos[9] = trim($datos[9]); // dirección
							
							//MADRE
							$datos[10] = trim($datos[10]); // nombre
							$datos[11] = trim($datos[11]); // apellido
							$datos[12] = trim($datos[12]); // parentesco
							$datos[13] = trim(str_replace(".","",$datos[13])); // cedula
							$datos[14] = trim($datos[14]); // fec nac
							$datos[15] = trim(str_replace(".","",$datos[15])); // sexo
							$datos[16] = trim($datos[16]); // peso
							$datos[17] = trim($datos[17]); // estatura
							
							//PADRE
							$datos[18] = trim($datos[18]);  // nombre
							$datos[19] = trim($datos[19]); // apellido
							$datos[20] = trim($datos[20]); // parentesco
							$datos[21] = trim(str_replace(".","",$datos[21])); // cedula
							$datos[22] = trim($datos[22]); // fec nac
							$datos[23] = trim(str_replace(".","",$datos[23])); // sexo
							$datos[24] = trim($datos[24]); // peso
							$datos[25] = trim($datos[25]); // estatura
							
							//CONYUGUE
							$datos[26] = trim($datos[26]);  // nombre
							$datos[27] = trim($datos[27]); // apellido
							$datos[28] = trim($datos[28]); // parentesco
							$datos[29] = trim(str_replace(".","",$datos[29])); // cedula
							$datos[30] = trim($datos[30]); // fec nac
							$datos[31] = trim(str_replace(".","",$datos[31])); // sexo
							$datos[32] = trim($datos[32]); // peso
							$datos[33] = trim($datos[33]); // estatura
							
							//HIJO 1
							$datos[34] = trim($datos[34]);  // nombre
							$datos[35] = trim($datos[35]); // apellido
							$datos[36] = trim($datos[36]); // parentesco
							$datos[37] = trim(str_replace(".","",$datos[37])); // cedula
							$datos[38] = trim($datos[38]); // fec nac
							$datos[39] = trim(str_replace(".","",$datos[39])); // sexo
							$datos[40] = trim($datos[40]); // peso
							$datos[41] = trim($datos[41]); // estatura
							
							//HIJO 2
							$datos[42] = trim($datos[42]);  // nombre
							$datos[43] = trim($datos[43]); // apellido
							$datos[44] = trim($datos[44]); // parentesco
							$datos[45] = trim(str_replace(".","",$datos[45])); // cedula
							$datos[46] = trim($datos[46]); // fec nac
							$datos[47] = trim(str_replace(".","",$datos[47])); // sexo
							$datos[48] = trim($datos[48]); // peso
							$datos[49] = trim($datos[49]); // estatura
							
							//HIJO 3
							$datos[50] = trim($datos[50]);  // nombre
							$datos[51] = trim($datos[51]); // apellido
							$datos[52] = trim($datos[52]); // parentesco
							$datos[53] = trim(str_replace(".","",$datos[53])); // cedula
							$datos[54] = trim($datos[54]); // fec nac
							$datos[55] = trim(str_replace(".","",$datos[55])); // sexo
							$datos[56] = trim($datos[56]); // peso
							$datos[57] = trim($datos[57]); // estatura
							
							//HIJO 4
							$datos[58] = trim($datos[58]);  // nombre
							$datos[59] = trim($datos[59]); // apellido
							$datos[60] = trim($datos[60]); // parentesco
							$datos[61] = trim(str_replace(".","",$datos[61])); // cedula
							$datos[62] = trim($datos[62]); // fec nac
							$datos[63] = trim(str_replace(".","",$datos[63])); // sexo
							$datos[64] = trim($datos[64]); // peso
							$datos[65] = trim($datos[65]); // estatura
							
							//HIJO 5
							$datos[66] = trim($datos[66]);  // nombre
							$datos[67] = trim($datos[67]); // apellido
							$datos[68] = trim($datos[68]); // parentesco
							$datos[69] = trim(str_replace(".","",$datos[69])); // cedula
							$datos[70] = trim($datos[70]); // fec nac
							$datos[71] = trim(str_replace(".","",$datos[71])); // sexo
							$datos[72] = trim($datos[72]); // peso
							$datos[73] = trim($datos[73]); // estatura
							
							//HIJO 6
							$datos[74] = trim($datos[74]);  // nombre
							$datos[75] = trim($datos[75]); // apellido
							$datos[76] = trim($datos[76]); // parentesco
							$datos[77] = trim(str_replace(".","",$datos[77])); // cedula
							$datos[78] = trim($datos[78]); // fec nac
							$datos[79] = trim(str_replace(".","",$datos[79])); // sexo
							$datos[80] = trim($datos[80]); // peso
							$datos[81] = trim($datos[81]); // estatura
							
							//HIJO 7
							$datos[82] = trim($datos[82]);  // nombre
							$datos[83] = trim($datos[83]); // apellido
							$datos[84] = trim($datos[84]); // parentesco
							$datos[85] = trim(str_replace(".","",$datos[85])); // cedula
							$datos[86] = trim($datos[86]); // fec nac
							$datos[87] = trim(str_replace(".","",$datos[87])); // sexo
							$datos[88] = trim($datos[88]); // peso
							$datos[89] = trim($datos[89]); // estatura
							
							//<option value="C" >Conyuge</option>
             				//<option value="H" >Hijo</option>
              				//<option value="P" >Progenitor</option>
							
							$strsql = "";
							
							if(!$datos[0]){echo "******************************* Persona sin cédula *****************************<br>"; continue;}
							if(!$datos[13] and !$datos[21] and !$datos[29] and !$datos[34] and !$datos[42] and !$datos[50] and !$datos[58] and !$datos[66] and !$datos[74] and !$datos[82]){echo "******************************* Persona sin familiares *****************************<br>"; continue;}
							$strsql2 = "SELECT codper FROM sno_personal WHERE cedper = '".$datos[0]."' ";
							$rs_personal = $this->io_sql->select($strsql2);	
							if($rs_personal==false){												
								$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;
								
							}
							
							if(!$rs_personal->RecordCount()){echo "******************************* La persona no existe en el sistema *****************************<br>"; continue;}

							$strsql3 = "SELECT codper FROM sno_familiar WHERE codper = '".str_pad($datos[0],10,"0",STR_PAD_LEFT)."' ";
							$rs_fam = $this->io_sql->select($strsql3);	
							if($rs_fam==false){												
								$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;
								
							}
							if($rs_fam->RecordCount()){echo "//////////******************************* Los familiares ya fueron cargados *****************************<br>"; continue;}

							
							
							if($datos[13]){//madre
							    
								
								
								$sqlx = "SELECT cedfam FROM sno_familiar WHERE cedfam = '".(integer)$datos[13]."' ";
								$rs_famx = $this->io_sql->select($sqlx);	
								if($rs_famx==false){												
									$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;
									
								}
								if(!$rs_famx->RecordCount()){							
								
										$strsql =  $strsql."INSERT INTO sno_familiar(
																		codemp, codper, cedfam, nomfam, apefam, sexfam, fecnacfam, nexfam, 
																		estfam, cedula)
																VALUES ('0001', '".
																		   str_pad($datos[0],10,"0",STR_PAD_LEFT)."', '".
																		   (integer)$datos[13]."', '".
																		   $datos[10]."', '".
																		   $datos[11]."', '".
																		   strtoupper($datos[15])."', '".
																		   $this->io_conexiones->formatea_fecha_bd($this->valida_fecha($datos[14]))."', 
																		   'P','0', '".
																		   (integer)$datos[13]."');
															";
								}
							
							}
							
							if($datos[21]){//padre
							
								$sqlx = "SELECT cedfam FROM sno_familiar WHERE cedfam = '".(integer)$datos[21]."' ";
								$rs_famx = $this->io_sql->select($sqlx);	
								if($rs_famx==false){												
									$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;
									
								}
								if(!$rs_famx->RecordCount()){	
									$strsql =  $strsql."INSERT INTO sno_familiar(
																codemp, codper, cedfam, nomfam, apefam, sexfam, fecnacfam, nexfam, 
																estfam, cedula)
														VALUES ('0001', '".
														           str_pad($datos[0],10,"0",STR_PAD_LEFT)."', '".
																   (integer)$datos[21]."', '".
																   $datos[18]."', '".
																   $datos[19]."', '".
																   strtoupper($datos[23])."', '".
																   $this->io_conexiones->formatea_fecha_bd($this->valida_fecha($datos[22]))."', 
																   'P','0', '".
																   (integer)$datos[21]."');
													";
													
								}
							
							}
							
							if($datos[29]){//conyugue
							
								$sqlx = "SELECT cedfam FROM sno_familiar WHERE cedfam = '".(integer)$datos[29]."' ";
								$rs_famx = $this->io_sql->select($sqlx);	
								if($rs_famx==false){												
									$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;
									
								}
								if(!$rs_famx->RecordCount()){
									$strsql =  $strsql."INSERT INTO sno_familiar(
																codemp, codper, cedfam, nomfam, apefam, sexfam, fecnacfam, nexfam, 
																estfam, cedula)
														VALUES ('0001', '".
														           str_pad($datos[0],10,"0",STR_PAD_LEFT)."', '".
																   (integer)$datos[29]."', '".
																   $datos[26]."', '".
																   $datos[27]."', '".
																   strtoupper($datos[31])."', '".
																   $this->io_conexiones->formatea_fecha_bd($this->valida_fecha($datos[30]))."', 
																   'C','0', '".
																   (integer)$datos[29]."');
													";
								}
							}
							
							if($datos[34]){//hijo 1
									
								if(!$datos[37]){$datos[37] = $datos[0].'01';}
								
								$sqlx = "SELECT cedfam FROM sno_familiar WHERE cedfam = '".(integer)$datos[37]."' ";
								$rs_famx = $this->io_sql->select($sqlx);	
								if($rs_famx==false){												
									$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;
									
								}
								if(!$rs_famx->RecordCount()){	
									$strsql =  $strsql."INSERT INTO sno_familiar(
																codemp, codper, cedfam, nomfam, apefam, sexfam, fecnacfam, nexfam, 
																estfam, cedula)
														VALUES ('0001', '".
														           str_pad($datos[0],10,"0",STR_PAD_LEFT)."', '".
																   (integer)$datos[37]."', '".
																   $datos[34]."', '".
																   $datos[35]."', '".
																   strtoupper($datos[39])."', '".
																   $this->io_conexiones->formatea_fecha_bd($this->valida_fecha($datos[38]))."', 
																   'H','0', '".
																   (integer)$datos[37]."');
													";
								}
									
							}
							
							if($datos[42]){//hijo 2
									
								if(!$datos[45]){$datos[45] = $datos[0].'02';}
								$sqlx = "SELECT cedfam FROM sno_familiar WHERE cedfam = '".(integer)$datos[45]."' ";
								$rs_famx = $this->io_sql->select($sqlx);	
								if($rs_famx==false){												
									$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;
									
								}
								if(!$rs_famx->RecordCount()){	
									$strsql =  $strsql."INSERT INTO sno_familiar(
																codemp, codper, cedfam, nomfam, apefam, sexfam, fecnacfam, nexfam, 
																estfam, cedula)
														VALUES ('0001', '".
														           str_pad($datos[0],10,"0",STR_PAD_LEFT)."', '".
																   (integer)$datos[45]."', '".
																   $datos[42]."', '".
																   $datos[43]."', '".
																   strtoupper($datos[47])."', '".
																   $this->io_conexiones->formatea_fecha_bd($this->valida_fecha($datos[46]))."', 
																   'H','0', '".
																   (integer)$datos[45]."');
													";
								}
							}
							
							
							if($datos[50]){//hijo 3
									
								if(!$datos[53]){$datos[53] = $datos[0].'03';}
								$sqlx = "SELECT cedfam FROM sno_familiar WHERE cedfam = '".(integer)$datos[53]."' ";
								$rs_famx = $this->io_sql->select($sqlx);	
								if($rs_famx==false){												
									$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;
									
								}
								if(!$rs_famx->RecordCount()){		
									$strsql =  $strsql."INSERT INTO sno_familiar(
																codemp, codper, cedfam, nomfam, apefam, sexfam, fecnacfam, nexfam, 
																estfam, cedula)
														VALUES ('0001', '".
														           str_pad($datos[0],10,"0",STR_PAD_LEFT)."', '".
																   (integer)$datos[53]."', '".
																   $datos[50]."', '".
																   $datos[51]."', '".
																   strtoupper($datos[55])."', '".
																   $this->io_conexiones->formatea_fecha_bd($this->valida_fecha($datos[54]))."', 
																   'H','0', '".
																   (integer)$datos[53]."');
													";
								}
							}	
							
							if($datos[58]){//hijo 4
									
									if(!$datos[61]){$datos[61] = $datos[0].'04';}
								$sqlx = "SELECT cedfam FROM sno_familiar WHERE cedfam = '".(integer)$datos[61]."' ";
								$rs_famx = $this->io_sql->select($sqlx);	
								if($rs_famx==false){												
									$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;
									
								}
								if(!$rs_famx->RecordCount()){			
									$strsql =  $strsql."INSERT INTO sno_familiar(
																codemp, codper, cedfam, nomfam, apefam, sexfam, fecnacfam, nexfam, 
																estfam, cedula)
														VALUES ('0001', '".
														           str_pad($datos[0],10,"0",STR_PAD_LEFT)."', '".
																   (integer)$datos[61]."', '".
																   $datos[58]."', '".
																   $datos[59]."', '".
																   strtoupper($datos[63])."', '".
																   $this->io_conexiones->formatea_fecha_bd($this->valida_fecha($datos[62]))."', 
																   'H','0', '".
																   (integer)$datos[61]."');
													";
								}
									
							}	
							
							if($datos[66]){//hijo 5
							
							    if(!$datos[69]){$datos[69] = $datos[0].'05';}
								$sqlx = "SELECT cedfam FROM sno_familiar WHERE cedfam = '".(integer)$datos[69]."' ";
								$rs_famx = $this->io_sql->select($sqlx);	
								if($rs_famx==false){												
									$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;
									
								}
								if(!$rs_famx->RecordCount()){				
									$strsql =  $strsql."INSERT INTO sno_familiar(
																codemp, codper, cedfam, nomfam, apefam, sexfam, fecnacfam, nexfam, 
																estfam, cedula)
														VALUES ('0001', '".
														           str_pad($datos[0],10,"0",STR_PAD_LEFT)."', '".
																   (integer)$datos[69]."', '".
																   $datos[66]."', '".
																   $datos[67]."', '".
																   strtoupper($datos[71])."', '".
																   $this->io_conexiones->formatea_fecha_bd($this->valida_fecha($datos[70]))."', 
																   'H','0', '".
																   (integer)$datos[69]."');
													";
								}
							}	
							
							if($datos[74]){//hijo 6
								
								if(!$datos[77]){$datos[77] = $datos[0].'06';}
							    $sqlx = "SELECT cedfam FROM sno_familiar WHERE cedfam = '".(integer)$datos[77]."' ";
								$rs_famx = $this->io_sql->select($sqlx);	
								if($rs_famx==false){												
									$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;
									
								}
								if(!$rs_famx->RecordCount()){			
									$strsql =  $strsql."INSERT INTO sno_familiar(
																	codemp, codper, cedfam, nomfam, apefam, sexfam, fecnacfam, nexfam, 
																	estfam, cedula)
															VALUES ('0001', '".
																	   str_pad($datos[0],10,"0",STR_PAD_LEFT)."', '".
																	   (integer)$datos[77]."', '".
																	   $datos[74]."', '".
																	   $datos[75]."', '".
																	   strtoupper($datos[79])."', '".
																	   $this->io_conexiones->formatea_fecha_bd($this->valida_fecha($datos[78]))."', 
																	   'H','0', '".
																	   (integer)$datos[77]."');
														";
								}
								
							}	
							
							if($datos[82]){//hijo 7
									
								if(!$datos[85]){$datos[85] = $datos[0].'07';}
								$sqlx = "SELECT cedfam FROM sno_familiar WHERE cedfam = '".(integer)$datos[85]."' ";
								$rs_famx = $this->io_sql->select($sqlx);	
								if($rs_famx==false){												
									$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;
									
								}
								if(!$rs_famx->RecordCount()){			
									$strsql =  $strsql."INSERT INTO sno_familiar(
																codemp, codper, cedfam, nomfam, apefam, sexfam, fecnacfam, nexfam, 
																estfam, cedula)
														VALUES ('0001', '".
														           str_pad($datos[0],10,"0",STR_PAD_LEFT)."', '".
																   (integer)$datos[85]."', '".
																   $datos[82]."', '".
																   $datos[83]."', '".
																   strtoupper($datos[87])."', '".
																   $this->io_conexiones->formatea_fecha_bd($this->valida_fecha($datos[86]))."', 
																   'H','0', '".
																   (integer)$datos[85]."');
													";
								}
									
							}				
							//echo sizeof($datos).'<br>';
							
																	
							echo $numero.'.- '.$strsql.'<br>';
							$numero++;
							
							
							
							
							
							
							$this->io_sql->begin_transaction();				
							$this->rs_data=$this->io_sql->select($strsql);			
							
							
							//echo $numero.'.- '.str_pad($datos[9],10,"0",STR_PAD_LEFT).' | '.trim($datos[9]).' | '.trim($datos[10]).' | '.trim($datos[11]).' | '.$datos['codtippersss'].' | '.$datos[6].' | '.$datos[7].' | '.$datos[8].'<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;
								
							}
							
							$this->io_sql->commit();					
							
							
				}
			
				
	
	}//end function ejecutar




}


$procesar = new procesar_datos;

$procesar->ejecutar();



?>