<?php
 
session_start();
include("DMMYSQLG.php");
require_once("DF80000.php");

?>
<html>
<head>
<title>Migrar Inventario</title>
</head>
<body>
<h2>!!! Espere un momento Importanto...!!!</h2>
<img src="GIF32.gif" width="123" height="6" id="imagen_espera" name="imagen_espera">

<?php
 
$odbcAlmacen=odbc_connect("almacenCom","","");
$res=odbc_exec($odbcAlmacen,"select * from TBLARTIC1");
	
//borrar tablas
 mysqli_query($con ,"delete from dginvent");
 mysqli_query($con ,"delete from dgbasico");
 mysqli_query($con ,"delete from dglineas"); 

$timeIni=date('H:i:s');  
while($reg=odbcFetchArray($res)){
	$dt_basico=mysqli_fetch_array(mysqli_query($con ,"SELECT count(*) as numRows, gpo_basico as idGru FROM dgbasico WHERE des_basico='".$reg['CMPDESAG']."' AND ubicacion='A' "));
	if($dt_basico['numRows']==0){
		$rs=mysqli_query($con ,"INSERT INTO dgbasico SET des_basico='".$reg['CMPDESAG']."',cve_basico='".$reg['CMPAGRUP']."', ubicacion='A'");
		$dt_basico['idGru']=mysqli_insert_id();
	}
	$dt_linea=mysqli_fetch_array(mysqli_query($con ,"SELECT count(*) as numRows, linea as idAlm FROM dglineas WHERE des_linea='".$reg['CMPDESAL']."' AND ubicacion='A' "));
	if($dt_linea['numRows']==0){
		$rs=mysqli_query($con ,"INSERT INTO dglineas SET des_linea='".$reg['CMPDESAL']."',cve_linea='".$reg['CMPALMAC']."',ubicacion='A'");
		$dt_linea['idAlm']=mysqli_insert_id();
	}	
	$dt_uni=odbcFetchArray(odbc_exec($odbcAlmacen,"SELECT * FROM TBLUNIME WHERE CMPCODIG='".$reg['CMPUNIME']."'"));
	$dt_unimed=mysqli_fetch_array(mysqli_query($con ,"SELECT count(*) as numRows,cve_uni as idUniMed FROM dggrupos WHERE des_uni='".$dt_uni['CMPDESCR']."' "));
	if($dt_unimed['numRows']==0){
		$rs=mysqli_query($con ,"INSERT INTO dggrupos SET des_uni='".$dt_uni['CMPDESCR']."'");
		$dt_unimed['idUniMed']=mysqli_insert_id();
	}
	$dt_uni=odbcFetchArray(odbc_exec($odbcAlmacen,"SELECT * FROM TBLUNIME WHERE CMPCODIG='".$reg['CMPUNIDE']."'"));
	$dt_unimedb=mysqli_fetch_array(mysqli_query($con ,"SELECT count(*) as numRows,cve_uni as idUniMed FROM dggrupos WHERE des_uni='".$dt_uni['CMPDESCR']."' "));
	if($dt_unimedb['numRows']==0){
		$rs=mysqli_query($con ,"INSERT INTO dggrupos SET des_uni='".$dt_uni['CMPDESCR']."'");
		$dt_unimedb['idUniMed']=mysqli_insert_id();
	}				
	$rg=mysqli_query($con ,"insert into dginvent (nparte1, np_desc1,linea, gpo_basico, cve_uni_com, capacidad, cve_uni_vta,proteinas, lipidos, carboidratos, kcalorias, calcio, sodio, potacio, precio, cos_prom, modulo,ubicacion) values ('".$reg['CMPCODIG']."', '".$reg['CMPDESCR']."', '".$dt_linea['idAlm']."', '".$dt_basico['idGru']."','".$dt_unimed['idUniMed']."','".$reg['CMPCAPAC']."', '".$dt_unimedb['idUniMed']."', '".$reg['CMPPROTE']."', '".$reg['CMPLIPID']."', '".$reg['CMPCARBO']."', '".$reg['CMPCALOR']."', '".$reg['CMPCALCI']."', '".$reg['CMPSODIO']."', '".$reg['CMPPOTAC']."', '".$reg['CMPCOSTO']."', '".$reg['CMPCOSPR']."', 'RES','A')");
	echo mysqli_error($con);
	
}
function odbcFetchArray(&$res){    
    $i = 0;
    $j = 0;
	
    $toReturn = "";
    if(odbc_fetch_row($res)){
        for ($j = 1; $j <= odbc_num_fields($res); $j++){        
             $field_name = odbc_field_name($res, $j);
             $ar[$field_name] = trim(odbc_result($res, $field_name));
        }
   		return $ar;
	}else{
		return false;    
	}
}

echo '<h2>FELICIDADES SE CREO INVENTARIO CON: '.$conx.' REGISTROS</h2>' ;
echo '<h2>FELICIDADES SE CREO INV. ALMACEN CON: '.$conAlm.' REGISTROS</h2>' ;
echo '<h2>TIEMPO DE IMPORTACION: '.difTime($timeIni,$timeFin).'</h2>';

/*FUNCIONES VARIAS*/
function char_invalido($string, $reemplazo=' ',$opciones=' -_|"\'/*-+@#():=<>,;ż?Ħ!'){
	$permitidos = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ0123456789.";
	$permitidos = $permitidos.$opciones;
	$auxString;
	for($i=0 ; $i < strlen($string) ; $i++){
		if(strpos($permitidos, $string[$i]) === false){
			$reempaux=$reemplazo;
			//if($string[$i]=='Ñ' || $string[$i]=='ñ')	$reempaux='n';
			$auxString.=$reempaux;
		}
		else
			$auxString=$auxString.$string[$i];
	}
	return $auxString;
}

function getKey_LINEAS($values){
	if(trim($values['desLin'])=="") return 0;
	$sql=mysqli_query($con ,"select * from dglineas where des_linea = '".$values['desLin']."'");
	if(mysqli_num_rows($sql) == 0){
		$rs=mysqli_query($con ,"insert into dglineas (des_linea,acc_fec, acc_hor, usuario) values ('".$values['desLin']."', CURDATE(), CURTIME(),'".$_SESSION['num_usu']."')");
		$id = mysqli_insert_id(); 
	}else{
		$row=mysqli_fetch_array($sql);
		$id = $row['linea']; 
	}
	return $id;
}

function getKey_MARCAS($values){
	if(trim($values['desMar'])=="") return 0;
	$sql=mysqli_query($con ,"select * from dxmarcas where des_marca = '".$values['desMar']."'");
	if(mysqli_num_rows($sql) == 0){
		$rs=mysqli_query($con ,"insert into dxmarcas (des_marca,acc_fec, acc_hor, usuario) values ('".$values['desMar']."', CURDATE(), CURTIME(),'".$_SESSION['num_usu']."')");
		$id = mysqli_insert_id(); 
	}else{
		$row=mysqli_fetch_array($sql);
		$id = $row['cve_marca']; 
	}
	return $id;
}

function getKey_GRUPOS($values){
	if(trim($values['desGru'])=="") return 0;
	$sql=mysqli_query($con ,"select gpo_basico,lim_vta1 as limVta1,lim_vta2 as limVta2,lim_vta3 as limVta3 from dgbasico where des_basico = '".$values['desGru']."'");
	if(mysqli_num_rows($sql) == 0){
		$rs=mysqli_query($con ,"insert into dgbasico (des_basico,lim_vta1, lim_vta2, lim_vta3, acc_fec, acc_hor, usuario) values ('".$values['desGru']."', '".$values['limVta1']."', '".$values['limVta2']."', '".$values['limVta3']."',CURDATE(), CURTIME(),'".$_SESSION['num_usu']."')");
		$values['idGru']=mysqli_insert_id();
	}else{
		@$row=mysqli_fetch_array($sql);
		$values['idGru']=$row['gpo_basico'];
		$values['limVta1']=$row['limVta1'];
		$values['limVta2']=$row['limVta2'];
		$values['limVta3']=$row['limVta3'];
	}
	return $values;
}

function getKey_MEDIDAS($values){
	if(trim($values['desUni'])=="") return 0;
	$sql=mysqli_query($con ,"select * from dggrupos where des_uni= '".$values['desUni']."'");
	if(mysqli_num_rows($sql) == 0){
		$rs=mysqli_query($con ,"insert into dggrupos (des_uni,acc_fec, acc_hor, usuario) values ('".$values['desUni']."', CURDATE(), CURTIME(),'".$_SESSION['num_usu']."')");
		$id = mysqli_insert_id(); 
	}else{
		$row=mysqli_fetch_array($sql);
		$id = $row['cve_uni']; 
	}
	return $id;
}
?>
</body>
</html>