<?php

session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
 include('DF80000.php');
$name="Cardex.xls"; 
if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
	header('Content-Type: application/force-download');
else
	header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="'.$name.'"');
?> 
<table width="945"  align="center">
  <tr>
    <th colspan="13">Cardex</th>
  </tr>
  <tr>
    <th width="46"><div align="center">Fecha</div></th>
    <th width="51"><div align="center">No.Parte</div></th>
    <th width="60"><div align="center">Almacen</div></th>
    <th width="60"><div align="center">Tip.Mov.</div></th>
    <th width="61"><div align="center">No. Doc. </div></th>
    <th width="82"><div align="center">Pro.Clien.Cod.</div></th>
    <th width="78"><div align="center">Pre.Mov.Cos</div></th>
    <th width="67"><div align="center">Exi. Inicial</div></th>
    <th width="77"><div align="center">Mov. Entrada </div></th>
    <th width="68"><div align="center">Mov. Salida </div></th>
    <th width="66"><div align="center">Exi. Final </div></th>
    <th width="84"><div align="center">Folio Interno </div></th>
    <th width="63"><div align="center">No. Poliza </div></th>
  </tr>
  	 <?php

	//se da formato a fechas
	$fecha_inicial = chfecha($_POST['fec_ini'],'d/m/Y','Y-m-d');
	$fecha_final = chfecha($_POST['fec_fin'],'d/m/Y','Y-m-d');
	$dataBus=NULL;
	if(trim($_POST['id_prod'])!="")
		$dataBus[]="nparte1='".$_POST['id_prod']."'";
	$dataBus[]="fec_mov between '$fecha_inicial' AND '$fecha_final'";
	
	$WHERE=implode(" AND ",$dataBus);
	$sq="select * from dgcardex WHERE $WHERE order by nparte1,fec_mov,n_reg";
	$sql = mysqli_query($con ,$sq);
	$idProdAct='-1';
	while(@$reg = mysqli_fetch_array($sql)){
		if($reg["nparte1"]!=$idProdAct){
			if($idProdAct!='-1'){
				echo "<tr>";
				echo "<td colspan='8'><div align=\"RIGHT\" class='inputcodigo'><div align=\"RIGHT\">".'MOVIMIENTOS: '.$conMov."</div></td>";
				echo "<td><div align=\"RIGHT\">".$canMovEntTot."</div></td>";
				echo "<td><div align=\"RIGHT\">".$canMovSalTot."</div></td>";
				echo "<td><div align=\"RIGHT\">".$exiIni."</div></td>";
				echo "<td><div align=\"RIGHT\">".'&nbsp;'."</div></td>";
				echo "<td><div align=\"RIGHT\">".'&nbsp;'."</div></td>";
				echo "</tr>";
				$conMov=0;
				$canMovEntTot=0;
				$canMovSalTot=0;
			}		
			$fecMovExp=explode("-",$reg['fec_mov']);
			$dt_invacuIni=mysqli_fetch_array(mysqli_query($con ,"SELECT dginvacu.*,dginvent.np_desc1 FROM dginvacu INNER JOIN dginvent ON(dginvent.nparte1=dginvacu.nparte1) WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   dginvacu.axo='$fecMovExp[0]' AND dginvacu.mes='$fecMovExp[1]' AND dginvacu.nparte1 ='".$reg['nparte1']."'"));
			$dt_cardexAnt=mysqli_fetch_array(mysqli_query($con ,"SELECT SUM(mov_ent) as movEnt, SUM(mov_sal) as movSal FROM dgcardex WHERE nparte1='".$reg['nparte1']."' AND (fec_mov >='$fecMovExp[0]-$fecMovExp[1]-01' AND fec_mov <'$fecha_inicial')"));
			//echo "SELECT SUM(mov_ent) as movEnt, SUM(mov_sal) as movSal FROM dgcardex WHERE nparte1='".$reg['nparte1']."' AND (fec_mov >='$fecMovExp[0]-$fecMovExp[1]-01' AND fec_mov <'$fecha_inicial')";
			$exiIni=$dt_invacuIni['exi_ini']+$dt_cardexAnt['movEnt']-$dt_cardexAnt['movSal'];
			echo "<tr><td class=\"estilotabla\" colspan=\"13\"><div align=\"center\">".$dt_invacuIni['nparte1'].'-'.$dt_invacuIni['np_desc1']."</div></td></tr>";
			echo "<tr><td class=\"inputcodigo\" colspan=\"13\"><div align=\"RIGHT\">".' EXISTENCIA INICIAL: '.$exiIni."</div></td></tr>";
			$idProdAct=$reg["nparte1"];
		}		
		$conMov++;
		$canMovEntTot +=$reg['mov_ent'];
		$canMovSalTot +=$reg['mov_sal'];
		$fec1 = $reg["fec_mov"];
		include('DF20000.php');
		echo "<tr>";
		echo "<td><div align=\"LEFT\">".$f_f."</div></td>";
		echo "<td><div align=\"LEFT\">".$reg["nparte1"]."</div></td>";
		echo "<td><div align=\"LEFT\">".$reg["almacen"]."</div></td>";
		echo "<td><div align=\"LEFT\">".$reg["tip_mov"]."</div></td>";
		echo "<td><div align=\"LEFT\">".$reg["fol_recep"]."</div></td>";
		echo "<td><div align=\"LEFT\">".$reg["codigo"]."</div></td>";
		echo "<td><div align=\"RIGHT\">".$reg["cos_pro_mov"]."</div></td>";
		echo "<td><div align=\"RIGHT\">".$exiIni."</div></td>";
		echo "<td><div align=\"RIGHT\">".$reg["mov_ent"]."</div></td>";
		echo "<td><div align=\"RIGHT\">".$reg["mov_sal"]."</div></td>";
		echo "<td><div align=\"RIGHT\">".($exiIni +=$reg["mov_ent"]-$reg["mov_sal"])."</div></td>";
		//sin campos
		echo "<td><div align=\"RIGHT\">".'&nbsp;'."</div></td>";
		echo "<td><div align=\"RIGHT\">".'&nbsp;'."</div></td>";
		echo "</tr>";
	
	}
	echo "<tr>";
	echo "<td colspan='8'><div align=\"RIGHT\" class='inputcodigo'><div align=\"CENTER\">".'MOVIMIENTOS: '.$conMov."</div></td>";
	echo "<td class='inputcodigo'><div align=\"RIGHT\">".$canMovEntTot."</div></td>";
	echo "<td class='inputcodigo'><div align=\"RIGHT\">".$canMovSalTot."</div></td>";
	echo "<td class='inputcodigo'><div align=\"RIGHT\">".$exiIni."</div></td>";
	echo "<td class='inputcodigo'><div align=\"RIGHT\">".'&nbsp;'."</div></td>";
	echo "<td class='inputcodigo'><div align=\"RIGHT\">".'&nbsp;'."</div></td>";
	echo "</tr>";	
	mysqli_close($con);
	?>	  
</table>

