<?php
 
session_start();
include("DMMYSQLG.php");
?>
<html>
<head>
<title>Migrar Cardex</title>
</head>
<body>
<h2>!!! Espere un momento Importanto...!!!</h2>
<img src="GIF32.gif" width="123" height="6" id="imagen_espera" name="imagen_espera">
<?php
 
@$nar = fopen('archivos/'.$_REQUEST['t1'], "r");
if (!isset($_REQUEST['t1']) or !$nar){
	echo '<h2>EL ARCHIVO NO EXISTE</h2>';
	echo '<meta content="1; url=DI61200_PR.php" HTTP-EQUIV="REFRESH">';
	exit();
}
$arrTipMov=array(
"II"=>array(
		"id"=>"II",
		"des"=>"COMPRA",
		"fieldInvAcu"=>"compras",
		"polNum"=>1,
		"fieldCardex"=>"movEnt"
	),
"CO"=>array(
		"id"=>"CO",
		"des"=>"COMPRA",
		"fieldInvAcu"=>"compras",
		"polNum"=>1,
		"fieldCardex"=>"movEnt"
	),
"CD"=>array(
		"id"=>"CD",
		"des"=>"DEVOLUCION DE COMPRA",
		"fieldInvAcu"=>"dev_com",
		"polNum"=>-1,
		"fieldCardex"=>"movSal"
	),
"VE"=>array(
		"id"=>"VE",
		"des"=>"VENTA",
		"fieldInvAcu"=>"ventas",
		"polNum"=>-1,
		"fieldCardex"=>"movSal"
	),	
"DV"=>array(
		"id"=>"DV",
		"des"=>"DEVOLUCION DE VENTA",
		"fieldInvAcu"=>"dev_ven",
		"polNum"=>1,
		"fieldCardex"=>"requi"
	),																							
"TS"=>array(
		"id"=>"TS",
		"des"=>"TRASPASO SALIDA",
		"fieldInvAcu"=>"tra_sal",
		"polNum"=>-1,
		"fieldCardex"=>"movSal"
	),
"TE"=>array(
		"id"=>"TE",
		"des"=>"TRASPASO ENTRADA",
		"fieldInvAcu"=>"tra_ent",
		"polNum"=>1,
		"fieldCardex"=>"movEnt"
	),
"EV"=>array(
		"id"=>"EV",
		"des"=>"ENTRADAS VARIAS",
		"fieldInvAcu"=>"dev_requi",
		"polNum"=>1,
		"fieldCardex"=>"movEnt"
	),
"SV"=>array(
		"id"=>"SV",
		"des"=>"SALIDAS VARIAS",
		"fieldInvAcu"=>"requi",
		"polNum"=>-1,
		"fieldCardex"=>"movSal"
	)																
);
function actCalAcu(&$totales){
	$datos['exiFin']=$this->datos['exiIni'];
	foreach($this->arrTipMov as $idTipMov=>$datTipMov){
		$totales[$datTipMov['fieldInvAcu']]=$this->checkData($totales[$datTipMov['fieldInvAcu']],0.00);
		$this->datos[$datTipMov['fieldInvAcu']] =round($totales[$datTipMov['fieldInvAcu']],2);
		$this->datos['exiFin'] +=$totales[$datTipMov['fieldInvAcu']]*$datTipMov['polNum'];
	}
	$this->datos[$this->arrTipMov[$this->datos['tipMov']]['fieldCardex']]=round($this->datos['canMov'],2);		
}


//limpia la tabla
$sq = mysqli_query($con ,"delete from dgcardex");
$sq = mysqli_query($con ,"delete from dginvacu");
//$sq = mysqli_query($con ,"drop table dgcardex");
$nar = fopen('archivos/'.$_REQUEST['t1'], "r");
$sq = mysqli_query($con ,"optimize table dginvacu");
$sq = mysqli_query($con ,"repair table dginvacu");
//leer el archuvo
/*
*0=>idProducto
*1=>fecha movimiento
*2=>tipo de movimiento
*3=>numero de folio
*4=>consecutivo de folio
*5=>almacen
*6=>movimiento de entrada
*7=>movimiento de salida
*8=>costo promedio
*/
while ($datos=fgetcsv($nar,500,"|")){
	// genera codigo a insertar en tabla
	$dt_bascps=mysqli_fetch_array(mysqli_query($con ,"SELECT codigo FROM dxbascps WHERE no_suc='$datos[10]'"));
	$sql = "insert into dgcardex SET nparte1='$datos[0]', fec_mov='$datos[1]', tip_mov='$datos[2]', num_fol='$datos[3]', con_fol='$datos[4]', almacen='$datos[5]', mov_ent='$datos[6]', mov_sal='$datos[7]', cos_pro_mov='$datos[8]', pre_comven='$datos[9]', codigo='$codigo', tip_cam='$datos[10]'";
	// ejecuta codigo en tabla
	$auxDatos=explode('-',$datos[2]);
	$result = mysqli_query($con ,$sql);
	$auxFecMov=explode("-",$datos[1]);
	$auxTipMov=explode("-",$datos[2]);
	$dt_invacuExi=mysqli_fetch_array(mysqli_query($con ,"SELECT count(*) as numRows, n_reg as idInvAcu FROM dginvacu WHERE nparte1='$datos[0]' AND axo='$auxFecMov[0]' AND mes='$auxFecMov[1]' "));
	if($dt_invacuExi['numRows']>0){
		$rsInvAcu=mysqli_query($con ,"
			UPDATE 
				dginvacu 
			SET ".$arrTipMov[$auxDatos[0]]['fieldInvAcu']."=".$arrTipMov[$auxDatos[0]]['fieldInvAcu']."+".number_format($datos[6]+$datos[7],2,'.','').",
				exi_fin= exi_ini+compras-dev_com+tra_ent-tra_sal-ventas+dev_ven-requi+dev_requi
			WHERE
				n_reg=$dt_invacuExi['idInvAcu']
		");
	}
	else{
		$dt_invAcuAnt=mysqli_fetch_array(mysqli_query($con ,$sqAcuAnt="SELECT n_reg as idInvAcu, exi_fin as exiFin FROM dginvacu WHERE nparte1='$datos[0]' AND DATE_FORMAT(CONCAT(axo,'-',mes,'-','01'),'%Y-%m-%d')<DATE_FORMAT(CONCAT('$auxFecMov[0]','-','$auxFecMov[1]','-','01'),'%Y-%m-%d') ORDER BY axo desc,mes desc LIMIT 1"));
	//	echo $sqAcuAnt."<br>";
		//if($dt_invAcuAnt['numRows']>0){
			$exiIni=$dt_invAcuAnt['exiFin'];
		//}else{
		//	$exiIni=0;
		//}
		$rsInvAcu=mysqli_query($con ,$sqAcu="
			INSERT INTO 
				dginvacu 
			SET ".$arrTipMov[$auxDatos[0]]['fieldInvAcu']."=".number_format($datos[6]+$datos[7],2,'.','').",
				exi_ini='$exiIni',
				exi_fin= exi_ini+compras-dev_com+tra_ent-tra_sal-ventas+dev_ven-requi+dev_requi,
				nparte1='$datos[0]', 
				axo='$auxFecMov[0]', 
				mes='$auxFecMov[1]'
		");	
		//echo $sqAcu."<br>";
	}
	//echo 'Importando Cuenta :'.$datos[0].'<br>';
}
//cierra el archivo
fclose ($archivo);
//cierra la conexxion
mysqli_close($con);
echo '<h2>FELICIDADES SE AGREGARON DATOS AL CARDEX</h2>' ;
?>  
</body>
</html>
