<?php

session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
 include('DF80000.php');
$name="Facturacion X Dia.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="747"  border="0" align="center">
  <tr>
    <th colspan="9"> <div align="center"> FACTURACION X DIA</div></th>
  <tr>
    <td nowrap class="tdagre1">Codigo</td>
    <td nowrap class="tdagre1">Nombre</td>
    <td nowrap class="tdagre1">Mov. Fac </td>
    <td nowrap class="tdagre1">Numero Documento </td>
    <td nowrap class="tdagre1">Subtotal</td>
    <td nowrap class="tdagre1">IVA</td>
    <td nowrap class="tdagre1">Total Documento </td>
    <td nowrap class="tdagre1">Importe Pagado </td>
    <td nowrap class="tdagre1">Notas Credito </td>
    <td nowrap class="tdagre1">Cargos </td>
    <td nowrap class="tdagre1">Abonos </td>
    <td nowrap class="tdagre1">Saldo documento</td>
    <td nowrap class="tdagre1">Numero Fac. Anterior </td>
  </tr>
  <?php
 
	$sq_redocl=mysqli_query($con ,$_POST['busqueda']);
	$z=-1;
	$totales=NULL;
	$totgeneral=NULL;
	while(@$dt_redocl=mysqli_fetch_array($sq_redocl)){
		if ($dt_redocl['tip_cam'] > 0 && $dt_redocl['moneda']=='2') $tipcambio=$dt_redocl['tip_cam'];
		else $tipcambio=1;
		$dt_bascps=mysqli_fetch_array(mysqli_query($con ,"select * from dxbascps where codigo='".$dt_redocl['codigo']."'"));
		if($z==-1){
			echo '<tr><td colspan="13" class="typeagre"><div align="center"> Movimientos Del '.chfecha($dt_redocl['fec_doc'],'Y-m-d','d de M del Y').'&nbsp;</div></td></tr>';
			$z=$dt_redocl['fec_doc'];
		}elseif($z!=$dt_redocl['fec_doc']){
			imptotales("Total x dia: ",$totales);
			echo '<tr><td colspan="13" class="typeagre"><div align="center"> Movimientos Del '.chfecha($dt_redocl['fec_doc'],'Y-m-d','d de M del Y').'&nbsp;</div></td></tr>';
			$z=$dt_redocl['fec_doc'];
			$totales=NULL;
		}
		if($dt_redocl['tip_mot']!="R"){
			llenaTotales($totales,$dt_redocl);
			llenaTotales($totgeneral,$dt_redocl);
		}
		echo '<tr>';
		echo '<td nowrap class="regFacL" colspan="1">'.$dt_redocl['codigo'].'&nbsp;</td>';
		echo '<td nowrap class="regFacL" colspan="1">'.$dt_bascps['ap_paterno'].' '.$dt_bascps['ap_materno'].' '.$dt_bascps['nombre'].'&nbsp;</td>';
		echo '<td nowrap class="regFacL" colspan="1">'.$dt_redocl['tip_mot'].'&nbsp;</td>';
		echo '<td nowrap class="regFacL" colspan="1">'.$dt_redocl['num_doc'].'&nbsp;</td>';
		echo '<td nowrap class="regFacR" colspan="1">'.number_format($dt_redocl['sub_tot']*$tipcambio,2,'.',',').'</td>';
		echo '<td nowrap class="regFacR" colspan="1">'.number_format($dt_redocl['iva_tot']*$tipcambio,2,'.',',').'</td>';
		echo '<td nowrap class="regFacR" colspan="1">'.number_format($dt_redocl['total']*$tipcambio,2,'.',',').'</td>';
		echo '<td nowrap class="regFacR" colspan="1">'.number_format($dt_redocl['tot_pag']*$tipcambio,2,'.',',').'</td>';
		echo '<td nowrap class="regFacR" colspan="1">'.number_format($dt_redocl['tot_not']*$tipcambio,2,'.',',').'</td>';
		echo '<td nowrap class="regFacR" colspan="1">'.number_format($dt_redocl['tot_car']*$tipcambio,2,'.',',').'</td>';
		echo '<td nowrap class="regFacR" colspan="1">'.number_format($dt_redocl['tot_abo']*$tipcambio,2,'.',',').'</td>';
		echo '<td nowrap class="regFacR" colspan="1">'.number_format($dt_redocl['tot_sal']*$tipcambio,2,'.',',').'</td>';
		echo '<td nowrap class="regFacL" colspan="1">'.$dt_redocl['num_fac_ant'].'&nbsp;</td>';
		echo '</tr>';
	}
	imptotales("Total x dia: ",$totales);
	imptotales("Total General: ",$totgeneral,'granTotal');
	//imptotales("Saldo General Documentos: ",$totgeneral);

	
	function llenaTotales(&$totales, $dat){
		if ($dat['tip_cam'] > 0 && $dat['tip_mot']!='N') $tipcambio=$dat['tip_cam'];
		else $tipcambio=1;
		$totales[0]+=$dat["sub_tot"]*$tipcambio;
		$totales[1]+=$dat["iva_tot"]*$tipcambio;
		$totales[2]+=$dat["total"]*$tipcambio;
		$totales[3]+=$dat["tot_pag"]*$tipcambio;
		$totales[4]+=$dat["tot_not"]*$tipcambio;
		$totales[5]+=$dat["tot_car"]*$tipcambio;
		$totales[6]+=$dat["tot_abo"]*$tipcambio;
		$totales[7]+=$dat["tot_sal"]*$tipcambio;
	}
	
	function imptotales($leyenda,$totales,$class='parTotal'){
		echo '<tr>';
		echo '<td class="'.$class.'" colspan="4">'.$leyenda.'&nbsp;</td>';
		echo '<td class="'.$class.'" colspan="1">'.number_format($totales[0],2,'.',',').'&nbsp;</td>';
		echo '<td class="'.$class.'" colspan="1">'.number_format($totales[1],2,'.',',').'&nbsp;</td>';
		echo '<td class="'.$class.'" colspan="1">'.number_format($totales[2],2,'.',',').'&nbsp;</td>';
		echo '<td class="'.$class.'" colspan="1">'.number_format($totales[3],2,'.',',').'&nbsp;</td>';
		echo '<td class="'.$class.'" colspan="1">'.number_format($totales[4],2,'.',',').'&nbsp;</td>';
		echo '<td class="'.$class.'" colspan="1">'.number_format($totales[5],2,'.',',').'&nbsp;</td>';
		echo '<td class="'.$class.'" colspan="1">'.number_format($totales[6],2,'.',',').'&nbsp;</td>';
		echo '<td class="'.$class.'" colspan="1">'.number_format($totales[7],2,'.',',').'&nbsp;</td>';
		echo '<td class="'.$class.'" colspan="1">&nbsp;</td>';
		echo '</tr>';
	}
	?>
</table>


