<?php

session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
require_once("DF80000.php"); 
$name="Listado_Facturas.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="1185"  align="center">
      <tr>
        <th colspan="15">Listado de Facturas</th>
      </tr>
      <tr>
        <th width="55"><div align="center">Fol.Fac.</div></th>
        <th width="68"><div align="center">Tip.Vent.</div></th>
        <th width="68"><div align="center">Tip.Fac.</div></th>
		<th width="80"><div align="center">Fec.Fac.</div></th>
		<th width="85"><div align="center">Num.Fac.</div></th>
        <th width="85"><div align="center">Fec.Ven.</div></th>
        <th width="95"><div align="center">Fol.Ven.</div></th>
		<th width="85"><div align="center">Cod.Cli.</div></th>
		<th width="70"><div align="center">Nom.Cli</div></th>
        <th width="70"><div align="center">Por.Des.</div></th>
        <th width="42"><div align="center">Almacen.</div></th>
		<th width="50"><div align="center">Sub.Tot.</div></th>
		<th width="75"><div align="center">Iva.</div></th>
		<th width="75"><div align="center">Total</div></th>		
		<th width="77"><div align="center">Cancela.</div></th>
      </tr>
	<?php

		if($_POST['setipven']!="T"){
			$and=" AND tip_ven='".$_POST['setipven']."'";
		}
		$fec = $_POST['t1'];
		include('DF20000.php');
		$fecha_inicial = $f_m;
		$fec = $_POST['t2'];
		include('DF20000.php');
		$fecha_final = $f_m;
		
		$sq_factur=mysqli_query($con ,"SELECT * FROM dgfactur where fec_fac between '$fecha_inicial' AND '$fecha_final' $and order by tip_fac, num_fac");
		while($reg = mysqli_fetch_array($sq_factur)){
			echo "<tr class=\"tdeli\">";
			echo "<td><div align='center'>" . $reg["fol_fac"] . "</div></td>";
		$dt_tipven=mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dgtipven where  tip_ven = '".$reg['tip_ven']."'"));
			echo "<td nowrap><div align='center'>" . $dt_tipven["des_tipven"] . "</div></td>";
		    echo "<td><div align='center'>" . $reg["tip_fac"] . "</div></td>";
			echo "<td><div align='center'>" .chfecha($reg["fec_fac"],'Y-m-d','d/m/Y') . "</div></td>";
			echo "<td><div align='center'>" . $reg["num_fac"] . "</div></td>";
			echo "<td><div align='center'>" .chfecha($reg["fec_ven"],'Y-m-d','d/m/Y') . "</div></td>";
			echo "<td><div align='center'>" . $reg["fol_ven"] . "</div></td>";
			echo "<td><div align='center'>" . $reg["cod_cli"] . "</div></td>";
			$dt_catcli=mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dgcatcli where  cod_cli = ' $reg['cod_cli']'"));
			echo "<td nowrap><div align='center'>" .$dt_catcli["ap_pat_cli"]." ".$dt_catcli["ap_mat_cli"]." ".$dt_catcli["nom_cli"] . "</div>            </td>";
			echo "<td><div align='center'>" . $reg["por_des"] . "</div></td>";
			$dt_almacen=mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dgalmacen where almacen = '".$reg['alm_gen']."'"));
			echo "<td><div align='center'>" . $dt_almacen["desc_almacen"] . "</div></td>";
			echo "<td><div align='center'>" .number_format($reg["sub_ven"],2,'.','') . "</div></td>";
			echo "<td><div align='center'>" .number_format($reg["iva_ven"],2,'.','') . "</div></td>";
			echo "<td><div align='center'>" .number_format($reg["tot_ven"],2,'.','') . "</div></td>";
			if ( $reg["cancelado"]=='1' || $reg["cancelado"]=='S')
				echo "<td><div align='center'>SI</div></td>";
			else
				echo "<td><div align='center'>NO</div></td>";
			if($_POST['tip_rep']=='2'){
				echo "<tr><td colspan=\"15\">";
				echo "<table border=\"1\" align=\"center\">";
				$sq_factud=mysqli_query($con ,"SELECT dgfactud.*,dginvent.np_desc1 as desProd FROM dgfactud LEFT JOIN dginvent ON (dgfactud.nparte1=dginvent.nparte1) WHERE  dgfactud.cons_fac='".$reg['fol_fac']."'");
				//echo "SELECT * FROM dgfactud WHERE  cons_fac='".$reg['fol_fac']."'<br>";
				while($dt_factud=mysqli_fetch_array($sq_factud)){
					echo "<tr>";
					echo "<td><div align='center'>" . chfecha($dt_factud['fec_fac'],'Y-m-d','d/m/Y') . "</div></td>";
					echo "<td><div align='center'>" . $dt_factud['num_fac'] . "</div></td>";
					echo "<td><div align='center'>" . $dt_factud['tip_fac'] . "</div></td>";
					echo "<td><div align='center'>" . $dt_factud['nparte1'].'-'.$dt_factud['desProd']. "</div></td>";
					echo "</tr>";
				}
				echo "</table>";
				echo "</td></tr>";
			}
		
		}
		mysqli_close($con);
	?>
    </table>
