<?php
	$StrCmd=V($_POST['TxtAc']);
	$StrEmp=V($_POST['CboEmp']);
	//+++++++++++++++
	function IMP_PERSONAL_BITACORA(){
		//BIT
		$_BIT_MOD_ID="0";
		$_BIT_SQL_CAB="SQL";
		$_BIT_SQL_DET="";
		$_BIT_PHP="imp_personal.php";
		$_BIT_ID=BIT_ADD("IMPORTAR DATOS","IMPORTAR DATOS DE PERSONAL","GUARDAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,"");
		//BIT	
		$cx=0;
		$data = new Spreadsheet_Excel_Reader();
		$data->setOutputEncoding('CP1251');
		$data->read('imp_personal.xls');
		error_reporting(E_ALL ^ E_NOTICE);
		$Fil=$data->sheets[0]['numRows'];
		$Col=$data->sheets[0]['numCols'];	
		for ($i = 2; $i <= $Fil; $i++) {
			$NOM=str_replace("'"," ",trim(V($data->sheets[0]['cells'][$i][1]))); 		
			$DIR=str_replace("'"," ",trim(V($data->sheets[0]['cells'][$i][2]))); 		
			$DIS=str_replace("'"," ",trim(V($data->sheets[0]['cells'][$i][3])));
			$DNI=str_replace("'"," ",trim(V($data->sheets[0]['cells'][$i][4])));
			$TEL=str_replace("'"," ",trim(V($data->sheets[0]['cells'][$i][5])));
			$CEL=str_replace("'"," ",trim(V($data->sheets[0]['cells'][$i][6])));
			$EMA=str_replace("'"," ",trim(V($data->sheets[0]['cells'][$i][7])));
			$CAR=str_replace("'"," ",trim(V($data->sheets[0]['cells'][$i][8])));
			//
			if(strlen($NOM)==0) break;
			if(strlen($DNI)==0) break;
			//
			if(strlen($DIS)>0){
				if(GetC("distritos","dis_nombre='$DIS'")==0){
					$ID_DIS=GenId("id_distrito","distritos");
					$s="INSERT INTO distritos VALUES ($ID_DIS,'$DIS')";
					ejt($s);
					BIT_DET_ADD($_BIT_ID,$s);
					//
				}else{
					$ID_DIS=gn("id_distrito","distritos","dis_nombre='$DIS'");
				}
			}else{
				$ID_DIS=0;
			}
			//
			if(strlen($CAR)>0){
				if(GetC("cargos","car_nombre='$CAR'")==0){
					$ID_CAR=GenId("id_cargo","cargos");
					$s="INSERT INTO cargos VALUES ($ID_CAR,'$CAR')";
					ejt($s);
					BIT_DET_ADD($_BIT_ID,$s);
					//
				}else{
					$ID_CAR=gn("id_cargo","cargos","car_nombre='$CAR'");
				}
			}else{
				$ID_CAR=0;
			}
			$ID_PER=gn("id_personal","personal","per_dni='$DNI'");
	
			if($ID_PER==0){
				$ID_PER=GenId("id_personal","personal");
				$s="INSERT INTO personal VALUES ".
				   "($ID_PER,'$NOM','$DIR',$ID_DIS,'1900-01-01','$DNI','$TEL','$CEL','1900-01-01','$EMA',$ID_CAR,'')";
				ejt($s);
				BIT_DET_ADD($_BIT_ID,$s);
				//
			}else{
				$s="UPDATE personal SET 
					per_nombre='$NOM',
					per_direccion='$DIR',
					per_id_distrito=$ID_DIS,
					per_dni='$DNI',
					per_tel='$TEL',
					per_cel='$CEL',
					per_email='$EMA',
					per_id_cargo=$ID_CAR
				WHERE id_personal=$ID_PER";
				ejt($s);	
				BIT_DET_ADD($_BIT_ID,$s);
				//	
			}
			//
		}
	}
	//
	if($StrCmd=="IMPORTAR"){
		$StrFileN = $_FILES['userfile']['name']; 
		$StrFileT = $_FILES['userfile']['type']; 		
		$StrExt=extension($StrFileN);
		//		
		if (!($StrExt== "xls" || $StrExt=="XLS")){ 
			$LStrMsg="La extension debe ser XLS : $StrExt";
			$LStrFoc="document.getElementById('userfile').focus();";
			$StrGT="S";	
		}else{			
			$StrFileNew="imp_personal.xls";
			if (move_uploaded_file($_FILES['userfile']['tmp_name'], $StrFileNew)){ 	
				//IMP_PERSONAL();	
				IMP_PERSONAL_BITACORA();	
				$LStrMsg="El archivo ha sido cargado correctamente ";
				$LStrFoc="document.getElementById('userfile').focus();";
				$StrGT="S";
			}else{ 
				$LStrMsg="Error al subir el archivo";
				$LStrFoc="document.getElementById('userfile').focus();";
				$StrGT="S";		
			} 
		}
		// 
	}
//*********************************************************

//*********************************************************
if($StrGT=="S"){
	if(strlen($LStrMsg)>0){
		if(strlen($LStrFoc)==0){
			$LStrMsg="onload=\"alert('$LStrMsg');\"";
		}else{
			$LStrMsg="onload=\"alert('$LStrMsg');$LStrFoc;\"";
		}
	}else{
		if(strlen($LStrFoc)>0){
			$LStrMsg="onload=\"$LStrFoc;\"";
		}
	}
}else{
	$LStrFoc="document.getElementById('userfile').focus();";
	$LStrMsg="onload=\"$LStrFoc;\"";
}

?>