<?php
include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
$CMD=V($_GET['CMD']);
$ZMD=$CMD;
/////////////////////////////////////////////////////
/*
LISTAR
++++++++++++++
BK_TABLAS
BK_VISTAS
BK_01-07=SACAR COPIA DE TODAS LAS TABLAS (ESTRUCTURA Y DATOS)
++++++++++++++
*/
/////////////////////////////////////////////////////
if($CMD=="LISTAR"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		//if($T!="ia_expedientes" && $T!="ia_dni_lin" && $T!="ia_dni_lin_pro" &&  $T!="ia_lotes_det_tip" &&  $T!="ia_lotes_det" &&  $T!="ven_tel_ven_t"){
		//if($T=="ven_tel_ven_t"){
		$TOT=gn("count(*)",$T,"");
		$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
		ejt($s);
		$CX++;
		//}
	}
}
/////////////////////////////////////////////////////
if($CMD=="BK_01"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T!="cotizaciones_det" && $T!="ginterna_det" && $T!="ventas_det" && $T!="cotizaciones" && 
		   $T!="ginterna" && $T!="guias_det" && $T!="cobroz" && $T!="ventas" && 
		   $T!="cotizaciones_imprimir" && $T!="apk" && $T!="compras_det" && $T!="stocks" && $T!="facturas_electronicas" ){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}

/////////////////////////////////////////////////////
if($CMD=="BK_02"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="cotizaciones_det"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
/////////////////////////////////////////////////////
if($CMD=="BK_03"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="ginterna_det"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
/////////////////////////////////////////////////////
if($CMD=="BK_04"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="ventas_det"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
/////////////////////////////////////////////////////
if($CMD=="BK_05"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="cotizaciones"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
/////////////////////////////////////////////////////
if($CMD=="BK_06"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="ginterna"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
/////////////////////////////////////////////////////
if($CMD=="BK_07"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="guias_det"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
/////////////////////////////////////////////////////
if($CMD=="BK_08"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="cobroz"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
/////////////////////////////////////////////////////
if($CMD=="BK_09"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="ventas"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
//$T!="cotizaciones_imprimir" && $T!="apk" && $T!="compras_det" && $T!="stocks" && $T!="facturas_electronicas"
if($CMD=="BK_10"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="cotizaciones_imprimir"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
//
if($CMD=="BK_11"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="apk"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
//
if($CMD=="BK_12"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="compras_det"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
//
if($CMD=="BK_13"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="stocks"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
//
if($CMD=="BK_14"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		if($T=="facturas_electronicas"){
			$TOT=gn("count(*)",$T,"");
			$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
			ejt($s);
			$CX++;
		}
	}
	$CMD="BK_DATA";
}
/////////////////////////////////////////////////////
function GET_SQL_CREATE($TABLA){
	$SQL="";
	$r = mysql_fetch_row(grs('SHOW CREATE TABLE '.$TABLA));
	$SQL= "\n\n".$r[1].";\n\n";  
	return $SQL;
}
/////////////////////////////////////////////////////
function GET_SQL_INSERT($TABLA,$L1,$L2){
	$return="";	
	if($L1==0 && $L2==0){
		$SQL="SELECT * FROM ".$TABLA;
	}else{
		$SQL="SELECT * FROM ".$TABLA. " LIMIT $L1,$L2";
	}
	echo $SQL."<br>";
	$result = grs($SQL);
	$num_fields = mysql_num_fields($result);      	
	//echo $num_fields."<br>";
	while($row = $result->fetch_row()){
		$return.= "INSERT INTO ".$TABLA." VALUES (";
			for($j=0; $j<$num_fields; $j++){
				$row[$j] = addslashes($row[$j]);
				$row[$j] = preg_replace("/\n/","\\n",$row[$j]);
				if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
				if ($j<($num_fields-1)) { $return.= ','; }
			}
		$return.= ");\n";
	}
	//$return.="\n\n\n";
	echo $SQL."<br>";
	//echo strlen($return);
	return $return;	
}
/////////////////////////////////////////////////////
function CREATE_FILE($FILE_NAME,$CONTENT){
	$FILE_HWND = fopen($FILE_NAME,'w+');
	fwrite($FILE_HWND,$CONTENT);
	fclose($FILE_HWND);
}
/////////////////////////////////////////////////////
function DB_GET_TAB_STRUCTURE(){
	$SQL="";
	$s="SELECT bkd_nom as TAB FROM bk_det ORDER BY bkd_nom";
	$rs = grs($s);
	while($r = $rs->fetch_assoc()){
		$SQL=$SQL.GET_SQL_CREATE($r['TAB']);
	}
	return $SQL;	
}
/////////////////////////////////////////////////////

if($CMD=="BK_TABLAS"){
	ejt("DELETE FROM bk_det");
	$CX=1;
	$rs = grs("show full tables where Table_Type = 'BASE TABLE'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		$TOT=gn("count(*)",$T,"");
		$s="INSERT INTO bk_det VALUES ($CX,0,'$T','T',$TOT)";
		ejt($s);
		$CX++;
	}
	$CMD="BK_CT";
}
if($CMD=="BK_CT"){
	$s=DB_GET_TAB_STRUCTURE();
	CREATE_FILE("backup/TS.txt",$s);
	echo "FIN_CT";
}
if($CMD=="BK_VISTAS"){
	ejt("DELETE FROM bk_det ");
	$CX=GenId("id_bkd","bk_det");
	$rs = grs("show full tables where Table_Type = 'VIEW'");
	while($r = $rs->fetch_row()){
		$T= $r[0];
		$s="INSERT INTO bk_det VALUES ($CX,0,'$T','V',0)";
		ejt($s);
		$CX++;		
	}
	//
	$s="SELECT bkd_nom as TAB FROM bk_det 
		WHERE bkd_tipo='V' AND bkd_id_bk=0 ORDER BY id_bkd ASC";
	$rs = grs($s);
	$s="";
	while($r = $rs->fetch_assoc()){
		$TAB=$r['TAB'];
		$RV = mysql_fetch_row(grs('SHOW CREATE TABLE '.$TAB));
		$StrVIEW=$RV[1];
		$StrVIEW=str_replace("ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER"," ",$StrVIEW);
		$StrVIEW=str_replace("ALGORITHM=UNDEFINED DEFINER=`redcotea`@`localhost` SQL SECURITY DEFINER"," ",$StrVIEW);
		$s.= "\n".$StrVIEW.";\n";
	}
	//
	CREATE_FILE("backup/VISTAS.txt",$s);
	echo "FIN_VISTAS";
}
/////////////////////////////////////////////////////
if($CMD=="BK_DATA"){
	//AGRUPAR EN 300MIL APROX
	$MIL=1000;
	$GRUPO=5*$MIL;
	//
	$GRU_CON=0;
	$GRU_VACIO=true;
	$STR_SQL="";
	$GRU_SQL="";
	$GRU_CTR_PRO=0;
	$TOT_CTR_PRO=0;
	$TOT_REG=gn("SUM(bkd_tot)","bk_det","bkd_id_bk=0");
	$s="SELECT bkd_nom as TAB,bkd_tot as TOT 
		FROM bk_det 
		WHERE bkd_tipo='T' AND bkd_id_bk=0 ORDER BY bkd_tot ASC";
	$rs = grs($s);
	while($r = $rs->fetch_assoc()){
		$TAB=$r['TAB'];
		$TOT=$r['TOT'];
		//
		if($GRUPO>=$TOT){
			$GRU_FRE=$GRUPO - $GRU_CTR_PRO;
			if($GRU_FRE>=$TOT){
				$GRU_CTR_PRO=$GRU_CTR_PRO + $TOT;
				$GRU_SQL=$GRU_SQL.GET_SQL_INSERT($TAB,0,0);
				if($GRU_CTR_PRO>=$GRUPO){
					$GRU_CON++;
					CREATE_FILE("backup/".$GRU_CON.".txt",$GRU_SQL);
					$GRU_SQL="";
					$GRU_CTR_PRO=0;
				}
			}else{			
				$TOT_SAL=$TOT - $GRU_FRE;
				//
				$GRU_CTR_PRO=$GRU_CTR_PRO + $GRU_FRE;
				$GRU_SQL=$GRU_SQL.GET_SQL_INSERT($TAB,0,$GRU_FRE);
				if($GRU_CTR_PRO>=$GRUPO){
					$GRU_CON++;
					CREATE_FILE("backup/".$GRU_CON.".txt",$GRU_SQL);
					$GRU_SQL="";
					$GRU_CTR_PRO=0;
				}
				//
				$GRU_SQL=$GRU_SQL.GET_SQL_INSERT($TAB,$GRU_FRE,$TOT_SAL);
				if($TOT_SAL>0){
					$GRU_CON++;
					CREATE_FILE("backup/".$GRU_CON."_.txt",$GRU_SQL);
					$GRU_SQL="";
					$GRU_CTR_PRO=0;
				}			
			}
		}else{
			$INICIO=0;
			$PARTES=floor($TOT/$GRUPO);
			$SALDOS=$TOT - ($PARTES * $GRUPO);
			for($i=1;$i<=$PARTES;$i++){
				$GRU_SQL=GET_SQL_INSERT($TAB,$INICIO,$GRUPO);
				$INICIO=$INICIO+$GRUPO;
				$GRU_CON++;
				CREATE_FILE("backup/".$GRU_CON."_g".$i.".txt",$GRU_SQL);			
			}
			//
			$GRU_SQL=GET_SQL_INSERT($TAB,$INICIO,$SALDOS);
			$GRU_CON++;
			CREATE_FILE("backup/".$GRU_CON."_gs.txt",$GRU_SQL);
		}
	}	
	//
	echo "FIN ".$ZMD;
}
?>
