<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte_Diario_Facturacion.xls");

 include ("../conexion.php");	

       $parametros = "SELECT * FROM web_0000 ";
       $result_parametros = $mysqli->query($parametros);
       $row_para = $result_parametros->fetch_array();
     
	   $NombreComercial =  $row_para["NOMCOM"];
	   $Razon          =  $row_para["RAZSOC"];
	   $Direccion       =  $row_para["DIREMP"];
	   $Cedula          =  $row_para["CEDJUR"];
	   $Telefono        =  $row_para["NUMTEL"];
  



	 $fecini    = $_GET["fechainicio"];
	 $fecfin    = $_GET["fechafin"];
	 $cdgtipven = $_GET["combotipoventa"];
	 $cdgcaj    = $_GET["cdgcaj"];
	 $cdgcli    = $_GET["cdgcli"];
	 $dsccli    = $_GET["dsccli"];
	 
	 
	$exento_tot    = 0;
	$gravado_tot   = 0;
	$subtotal_tot  = 0;
	$descuento_tot = 0;
	$impuesto_tot  = 0;
	$total_tot     = 0;
    $where_notas   = "";
    $where         = "";
	$exento_fac    = 0;
	$gravado_fac   = 0;
	$subtotal_fac  = 0;
	$descuento_fac = 0;
	$impuesto_fac  = 0;
	$impuesto_fac  = 0;
	$total_fac     = 0;	 
	$exento        = 0  ;
	$gravado       = 0 ;
	$subtotal      = 0 ;
	$descuento     = 0  ;
	$impuesto      = 0 ;
	$otrcar      = 0 ;
	$total         = 0  ;
	$otrcar_fac   = 0;
	
	$totexento_fac 	 = 0  ;
	$totexento_not   = 0  ;
	$totexento_deb   = 0  ;
	$totexonerado_fac   = 0  ;
	$totexonerado_not   = 0  ;
	$totexonerado_deb   = 0  ;
	$totgravado_fac  = 0  ;
	$totgravado_not  = 0  ;
	$totgravado_deb  = 0  ;
	
		
	$totnosujeto_fac   = 0  ; 
	$totnosujeto_not = 0  ; 
	
	 $where_notas_group   = "";
    $where_group        = "";
				
				
				
				
	
	
		 	 /////////////////        where para facturas  y notas ********************
	 
	if ($cdgtipven <> "") { $where.=" AND cdgtipven='$cdgtipven'"; }

	 
	
	
	 	 /////////////////        where para facturas ********************
	 
	if ($cdgcaj <> "") { $where.=" AND cdgcaj='$cdgcaj'"; }
	if ($cdgcli<> "") { $where.=" AND cdgcli='$cdgcli'"; }
	if ($dsccli<> "") { $where.=" AND dsccli like '%$dsccli%'"; }	
	$where_group.=" GROUP BY facturas.fecha ORDER BY fecha ASC";
	 
	 
	 
	 
	 
	 /////////////////        where para notas de credito ********************
	 
	 
	 if ($cdgcaj <> "") { $where_notas.=" AND cdgcaj='$cdgcaj'"; }
	 if ($cdgcli<> "") { $where_notas.=" AND cdgcli='$cdgcli'"; }
	 if ($dsccli<> "") { $where_notas.=" AND dsccli like '%$dsccli%'"; }	
	 $where_notas_group.=" GROUP BY facturas_notas.fecha ORDER BY numcomp ASC";
	 
	 
	 
 $fechainicio= implode("-", array_reverse(explode("-", $fecini))); 
 $fechafin= implode("-", array_reverse(explode("-", $fecfin))); 


 if ($cdgcaj=="") {
	 
	 $nomcomusu="TODOS LOS CAJEROS";
	 
	     } else {


	   $query_codcaj="SELECT * FROM usuarios  WHERE codusu=$cdgcaj ";
       $resultado = $mysqli->query( $query_codcaj);
       $row = $resultado->fetch_array();
        
		$nomcomusu    = $row["nomcomusu"];
     }
					
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Reporte Analitico de Compras </title>
<style type="text/css">
<!--
.Estilo1 {color: #CCCCCC}
.Estilo4 {font-size: medium}
.Estilo5 {color: #000000}
-->
</style>
</head>
<body>
<table width="92%" border="0" cellspacing="0" cellpadding="0">
    
    <tr>
      <td><div align="center"></div></td>
      <td colspan="10"  ><div align="center"><font size=8 ><?php echo $NombreComercial?></font></div>
      <div align="center"></div><div align="center"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="10" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="10" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="10" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="10" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="10" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="10"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Diario de Facturación</font>  </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="10"   border="1"><div align="center"><font size="3" >Fecha del <?php echo $fecini; ?> Al <?php echo $fecfin; ?> -  <?php echo $nomcomusu; ?></font></div></td>
     </tr>
        <tr>
       <td></td>
    
	<td colspan="10"   border="1"></td>
	
	

  <tr>
   <td width="1%"></td>
	 <td colspan="10"    bgcolor="skyblue" border="1"><div align="left" class="Estilo5"><font size="3" >FACTURA DE VENTA COLONES</font></div></td> 
  <tr>
      <td width="1%"></td>




    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>Fecha</strong></font></span></td>

    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Exento </strong></font></span></td> 
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Exonerado </strong></font></span></td>
		    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub .NoSujeto</strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Gravado </strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Subtotal </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Descuento </strong></font></span></td>
		<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Impuesto </strong></font></span></td>
		<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Imp. Servicio </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Total </strong></font></span></td>
		
		
   
  </tr>
  
<?PHP
	

	 $consulta2= "SELECT fecha,sum(totalfactura) as totalfactura,sum(subexe) AS subexe,sum(subgra) AS subgra,sum(totnosujeto) AS totnosujeto,sum(subtot) AS subtot,sum(impven) AS impven,sum(destot) AS destot,sum(totgravado) AS totgravado,sum(totexento) AS totexento,sum(totexonerado) AS totexonerado ,sum(otrcar) AS otrcar  FROM facturas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda = 'CRC'  ".$where.$where_group;
		

		if ($resultado2 = $mysqli->query($consulta2)) 

	
	  
 
  		while ($fila2 = $resultado2->fetch_assoc()) {
	
        $fecha         = $fila2["fecha"];
		$fecha         = implode("-", array_reverse(explode("-", $fecha)));
		//$numfactura    = $fila["numfactura"];
		//$numfactura    = substr($numfactura  , 10, 10);
		//$codfactura    = $fila["codfactura"];
		//$cdgcli        = $fila["cdgcli"];
		//$cdgcli        = sprintf("%04d",$cdgcli); 
		//$dsccli        = $fila["dsccli"];
		//$dsccli        = substr($dsccli, 0, 40);
		//$pladia        = $fila["pladia"];
		$totalfactura  = number_format($fila2["totalfactura"], 2);
		$subexe        = number_format($fila2["subexe"], 2);
		$subgra        = number_format($fila2["subgra"], 2);
		$subtot        = number_format($fila2["subtot"], 2);
		$impven        = number_format($fila2["impven"], 2);
		$otrcar        = number_format($fila2["otrcar"], 2);
		$destot        = number_format($fila2["destot"], 2);
		
		$totgravado     = number_format($fila2["totgravado"], 2);
		$totexento     = number_format($fila2["totexento"], 2);
		$totexonerado     = number_format($fila2["totexonerado"], 2);
		$totnosujeto     = number_format($fila2["totnosujeto"], 2);	
		
		
		


	  


	 			
							
			
			

?>  

 <tr>
   <td></td>



   <td align="center"><font size=1 ><?php echo $fecha?></font></td>
   <td align="right"><font size=1 ><?php echo $totexento?></font></td>
   <td align="right"><font size=1 ><?php echo $totexonerado ?></font></td>
      <td align="right"><font size=1 ><?php echo $totnosujeto ?></font></td>
   <td align="right"><font size=1 ><?php echo $totgravado ?></font></td>
   <td align="right"><font size=1 ><?php echo  $subtot ?></font></td>
   <td align="right"><font size=1 ><?php echo $destot?></font></td>
   <td align="right"><font size=1 ><?php echo $impven ?></font></td>
   <td align="right"><font size=1 ><?php echo $otrcar ?></font></td>
   <td align="right"><font size=1 ><?php echo $totalfactura ?></font></td>
    
 </tr>
  
  <?php
	 
  	        
		 } 
		 
  
      $consulta2= ("SELECT SUM(totalfactura) AS total , SUM(subtot) AS subtotal, SUM(subexe) AS exento, SUM(subgra) AS gravado, SUM(totnosujeto) AS totnosujeto, SUM(destot) AS descuento , SUM(impven) AS impuesto, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado, SUM(totgravado) AS  totgravado , SUM(otrcar) AS  otrcar FROM facturas WHERE  fecha  between '$fechainicio' AND '$fechafin'  AND tipmoneda = 'CRC' ".$where );

	if ($resultado2 = $mysqli->query($consulta2)) 

  	 while ($dato = $resultado2->fetch_assoc()) {

				$exento_fac    = $dato['exento'];
				$gravado_fac   = $dato['gravado'];
				$subtotal_fac  = $dato['subtotal'];
				$descuento_fac = $dato['descuento'];
				$impuesto_fac  = $dato['impuesto'];
				$otrcar_fac    = $dato['otrcar'];
				$total_fac     = $dato['total'];
				
				$totgravado_fac   = $dato['totgravado'];
				$totexento_fac    = $dato['totexento'];
				$totexonerado_fac    = $dato['totexonerado'];
				$totnosujeto_fac    = $dato['totnosujeto'];
				
				
	 } 		
  
  ?>
  
</table>

<table width="92%" border="0" cellspacing="0" cellpadding="0">

 <tr>
   <td></td>



   <td align="center"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
 </tr>

 
 
  <tr>

     <td width="1%"></td>
      <td align="left"><strong><font size=1 >TOTAL FACTURAS</font></strong></td>

   <td width="12%" align="right"><strong><font size=1 ><?php echo number_format($totexento_fac , 2);?></font></strong></td>
   <td width="12%" align="right"><strong><font size=1 ><?php echo number_format($totexonerado_fac , 2);?></font></strong></td>
      <td align="right"><strong><font size=1 ><?php echo number_format($totnosujeto_fac, 2); ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($totgravado_fac, 2); ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subtotal_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($descuento_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($impuesto_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($otrcar_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($total_fac , 2); ?></font></strong></td>
 </tr>
 
 
 
</table>


<table width="92%" border="0" cellspacing="0" cellpadding="0">
   
	<tr>
   <td width="12%"></td>
   <td colspan="2" align="left">&nbsp;</td>
   <td width="12%" align="center">&nbsp;</td>

   <td width="12%" align="center">&nbsp;</td>
   <td width="12%" align="center">&nbsp;</td>
   <td width="12%" align="right">&nbsp;</td>
   <td width="16%" align="right">&nbsp;</td>
   <td width="16%" align="right">&nbsp;</td>
 </tr> 
  <tr>
      <td width="1%"></td>


 <tr>
   <td width="1%"></td>
	 <td colspan="10"    bgcolor="skyblue" border="1"><div align="left"><font size="3" >NOTAS DE CREDITO COLONES</font></div></td> 
  <tr>
      <td width="1%"></td>

    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>Fecha</strong></font></span></td>

    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Exento </strong></font></span></td> 
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Exonerado </strong></font></span></td>
		<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.NoSujeto </strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Gravado </strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Subtotal </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Descuento </strong></font></span></td>
		<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Impuesto </strong></font></span></td>
		<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Imp. Servicio </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Total </strong></font></span></td>
		
		
   
  </tr>
  
<?PHP
	

	      $consulta3= ("SELECT fecha,SUM(totalnota) AS totalnota , SUM(subtot) AS subtot, SUM(subexe) AS subexe, SUM(subgra) AS subgra, SUM(totnosujeto) AS totnosujeto, SUM(destot) AS destot , SUM(impven) AS impven, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado, SUM(totgravado) AS  totgravado , SUM(otrcar) AS  otrcar FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda = 'CRC' ".$where_notas. $where_notas_group );
		if ($resultado3 = $mysqli->query($consulta3)) 

  		while ($dato3 = $resultado3->fetch_assoc()) {
	
			//	$codnota     = $dato["codnota"];
				//$numcomp     = $dato["numcomp"];
			//	$numcomp     = substr($numcomp  , 10, 10);
				$fecha       = $dato3["fecha"];
 				$fecha       = implode("-", array_reverse(explode("-", $fecha))); 		
				//$cdgcli      = $dato["cdgcli"];
				//$cdgcli      = sprintf("%04d",$cdgcli); 
				//$dsccli      = $dato["dsccli"];
				$totalnota   = number_format($dato3["totalnota"], 2);
				$subexe      = number_format($dato3["subexe"], 2);
				$subgra      = number_format($dato3["subgra"], 2);
				$subtot      = number_format($dato3["subtot"], 2);
				$impven      = number_format($dato3["impven"], 2);
				$otrcar      = number_format($dato3["otrcar"], 2);
				$destot      = number_format($dato3["destot"], 2);
				
				$totgravado     = number_format($dato3["totgravado"], 2);
				$totexento     = number_format($dato3["totexento"], 2);
				$totexonerado     = number_format($dato3["totexonerado"], 2);
				$totnosujeto     = number_format($dato3["totnosujeto"], 2);
			
			

?>  

 <tr>
   <td></td>



   <td align="center"><font size=1 ><?php echo $fecha?></font></td>
   <td align="right"><font size=1 ><?php echo $totexento?></font></td>
    <td align="right"><font size=1 ><?php echo $totexonerado ?></font></td>
	<td align="right"><font size=1 ><?php echo $totnosujeto ?></font></td>
   <td align="right"><font size=1 ><?php echo $totgravado ?></font></td>
   <td align="right"><font size=1 ><?php echo  $subtot ?></font></td>
   <td align="right"><font size=1 ><?php echo $destot?></font></td>
   <td align="right"><font size=1 ><?php echo $impven ?></font></td>
   <td align="right"><font size=1 ><?php echo $otrcar ?></font></td>
   <td align="right"><font size=1 ><?php echo $totalnota ?></font></td>
    
 </tr>
  
  <?php
	 
  	        
		 } 
		 

  
     $consulta4= ("SELECT SUM(totalnota) AS total , SUM(subtot) AS subtotal, SUM(subexe) AS exento, SUM(subgra) AS gravado, SUM(totnosujeto) AS totnosujeto, SUM(destot) AS descuento , SUM(impven) AS impuesto, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado,SUM(totgravado) AS  totgravado  ,SUM(otrcar) AS  otrcar  FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda = 'CRC' ".$where_notas  );
				

		if ($resultado4 = $mysqli->query($consulta4)) 

  		while ($dato4 = $resultado4->fetch_assoc()) {

				$exento_not    = $dato4["exento"];
				$gravado_not   = $dato4["gravado"];
				$subtotal_not  = $dato4["subtotal"];
				$descuento_not = $dato4["descuento"];
				$impuesto_not  = $dato4["impuesto"];
				$otrcar_not    = $dato4["otrcar"];
				$total_not     = $dato4["total"];
				
				$totgravado_not      = $dato4["totgravado"];
				$totexento_not   = $dato4["totexento"];
				$totexonerado_not   = $dato4["totexonerado"];
				$totnosujeto_not   = $dato4["totnosujeto"];

	
	
	 } 	
  
  ?>
  
</table>

<table width="92%" border="0" cellspacing="0" cellpadding="0">

 <tr>
   <td width="12%"></td>
   <td colspan="2" align="left">&nbsp;</td>
   <td width="12%" align="center">&nbsp;</td>

   <td width="12%" align="center">&nbsp;</td>
   <td width="12%" align="center">&nbsp;</td>
   <td width="12%" align="right">&nbsp;</td>
   <td width="16%" align="right">&nbsp;</td>
   <td width="16%" align="right">&nbsp;</td>
 </tr>

 
 
  <tr>

     <td width="1%"></td>
      <td align="left"><strong><font size=1 >TOTAL NOTAS CREDITO</font></strong></td>

   <td align="right"><strong><font size=1 ><?php echo  number_format($totexento_not , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($totexonerado_not , 2);?></font></strong></td>
      <td align="right"><strong><font size=1 ><?php echo  number_format($totnosujeto_not , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($totgravado_not , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format( $subtotal_not , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($descuento_not , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($impuesto_not , 2); ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($otrcar_not , 2); ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($total_not , 2); ?></font></strong></td>
 </tr>
 
   <tr>
      <td width="1%"></td>


 <tr>
 
</table>

                                                                                  <!--REPORTE EN DOLARES-->

<table>
  <tr>
   <td width="1%"></td>
	 <td colspan="10"    bgcolor="skyblue" border="1"><div align="left"><font size="3" >FACTURA DE VENTA DOLARES</font></div></td> 
  <tr>
      <td width="1%"></td>




    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>Fecha</strong></font></span></td>

    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Exento </strong></font></span></td> 
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Exonerado </strong></font></span></td>
		    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.NoSujeto </strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Gravado </strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Subtotal </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Descuento </strong></font></span></td>
		<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Impuesto </strong></font></span></td>
		<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Imp. Servicio </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Total </strong></font></span></td>
		
		
   
  </tr>
  
<?PHP
	

	 $consulta2= "SELECT fecha,sum(totalfactura) as totalfactura,sum(subexe) AS subexe,sum(subgra) AS subgra,sum(totnosujeto) AS totnosujeto,sum(subtot) AS subtot,sum(impven) AS impven,sum(destot) AS destot,sum(totgravado) AS totgravado,sum(totexento) AS totexento,sum(totexonerado) AS totexonerado ,sum(otrcar) AS otrcar  FROM facturas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda = 'USD'  ".$where.$where_group;
		

		if ($resultado2 = $mysqli->query($consulta2)) 

	
	  
 
  		while ($fila2 = $resultado2->fetch_assoc()) {
	
        $fecha         = $fila2["fecha"];
		$fecha         = implode("-", array_reverse(explode("-", $fecha)));
		//$numfactura    = $fila["numfactura"];
		//$numfactura    = substr($numfactura  , 10, 10);
		//$codfactura    = $fila["codfactura"];
		//$cdgcli        = $fila["cdgcli"];
		//$cdgcli        = sprintf("%04d",$cdgcli); 
		//$dsccli        = $fila["dsccli"];
		//$dsccli        = substr($dsccli, 0, 40);
		//$pladia        = $fila["pladia"];
		$totalfactura  = number_format($fila2["totalfactura"], 2);
		$subexe        = number_format($fila2["subexe"], 2);
		$subgra        = number_format($fila2["subgra"], 2);
		$subtot        = number_format($fila2["subtot"], 2);
		$impven        = number_format($fila2["impven"], 2);
		$otrcar        = number_format($fila2["otrcar"], 2);
		$destot        = number_format($fila2["destot"], 2);
		
		$totgravado     = number_format($fila2["totgravado"], 2);
		$totexento     = number_format($fila2["totexento"], 2);
		$totexonerado     = number_format($fila2["totexonerado"], 2);
		$totnosujeto     = number_format($fila2["totnosujeto"], 2);
		
		
		


	  


	 			
							
			
			

?>  

 <tr>
   <td></td>



   <td align="center"><font size=1 ><?php echo $fecha?></font></td>
   <td align="right"><font size=1 ><?php echo $totexento?></font></td>
   <td align="right"><font size=1 ><?php echo $totexonerado ?></font></td>
      <td align="right"><font size=1 ><?php echo $totnosujeto ?></font></td>
   <td align="right"><font size=1 ><?php echo $totgravado ?></font></td>
   <td align="right"><font size=1 ><?php echo  $subtot ?></font></td>
   <td align="right"><font size=1 ><?php echo $destot?></font></td>
   <td align="right"><font size=1 ><?php echo $impven ?></font></td>
   <td align="right"><font size=1 ><?php echo $otrcar ?></font></td>
   <td align="right"><font size=1 ><?php echo $totalfactura ?></font></td>
    
 </tr>
  
  <?php
	 
  	        
		 } 
		 

  
      $consulta2= ("SELECT SUM(totalfactura) AS total , SUM(subtot) AS subtotal, SUM(subexe) AS exento, SUM(subgra) AS gravado, SUM(totnosujeto) AS totnosujeto, SUM(destot) AS descuento , SUM(impven) AS impuesto, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado, SUM(totgravado) AS  totgravado , SUM(otrcar) AS  otrcar FROM facturas WHERE  fecha  between '$fechainicio' AND '$fechafin'  AND tipmoneda = 'USD' ".$where );

	if ($resultado2 = $mysqli->query($consulta2)) 

  	 while ($dato = $resultado2->fetch_assoc()) {

				$exento_fac    = $dato['exento'];
				$gravado_fac   = $dato['gravado'];
				$subtotal_fac  = $dato['subtotal'];
				$descuento_fac = $dato['descuento'];
				$impuesto_fac  = $dato['impuesto'];
				$otrcar_fac    = $dato['otrcar'];
				$total_fac     = $dato['total'];
				
				$totgravado_fac   = $dato['totgravado'];
				$totexento_fac    = $dato['totexento'];
				$totexonerado_fac    = $dato['totexonerado'];
				$totnosujeto_fac    = $dato['totnosujeto'];
				
				
	 } 		
  
  ?>
  
</table>

<table width="92%" border="0" cellspacing="0" cellpadding="0">

 <tr>
   <td></td>



   <td align="center"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
   <td align="right"><font size=1 ></font></td>
 </tr>

 
 
  <tr>

     <td width="1%"></td>
      <td align="left"><strong><font size=1 >TOTAL FACTURAS</font></strong></td>

   <td width="12%" align="right"><strong><font size=1 ><?php echo number_format($totexento_fac , 2);?></font></strong></td>
   <td width="12%" align="right"><strong><font size=1 ><?php echo number_format($totexonerado_fac , 2);?></font></strong></td>
      <td align="right"><strong><font size=1 ><?php echo number_format($totnosujeto_fac, 2); ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($totgravado_fac, 2); ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subtotal_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($descuento_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($impuesto_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($otrcar_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($total_fac , 2); ?></font></strong></td>
 </tr>
 
 
 
</table>


<table width="92%" border="0" cellspacing="0" cellpadding="0">
   
	<tr>
   <td width="12%"></td>
   <td colspan="2" align="left">&nbsp;</td>
   <td width="12%" align="center">&nbsp;</td>

   <td width="12%" align="center">&nbsp;</td>
   <td width="12%" align="center">&nbsp;</td>
   <td width="12%" align="right">&nbsp;</td>
   <td width="16%" align="right">&nbsp;</td>

 </tr> 
  <tr>
      <td width="1%"></td>


 <tr>
   <td width="1%"></td>
	 <td colspan="10"    bgcolor="skyblue" border="1"><div align="left"><font size="3" >NOTAS DE CREDITO DOLARES</font></div></td> 
  <tr>
      <td width="1%"></td>

    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>Fecha</strong></font></span></td>

    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Exento </strong></font></span></td> 
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Exonerado </strong></font></span></td>
		    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.NoSujeto </strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Gravado </strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Subtotal </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Descuento </strong></font></span></td>
		<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Impuesto </strong></font></span></td>
		<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Imp. Servicio </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Total </strong></font></span></td>
		
		
   
  </tr>
  
<?PHP
	

	      $consulta3= ("SELECT fecha,SUM(totalnota) AS totalnota , SUM(subtot) AS subtot, SUM(subexe) AS subexe, SUM(subgra) AS subgra, SUM(totnosujeto) AS totnosujeto, SUM(destot) AS destot , SUM(impven) AS impven, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado, SUM(totgravado) AS  totgravado , SUM(otrcar) AS  otrcar FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda = 'USD' ".$where_notas. $where_notas_group );
		if ($resultado3 = $mysqli->query($consulta3)) 

  		while ($dato3 = $resultado3->fetch_assoc()) {
	
			//	$codnota     = $dato["codnota"];
				//$numcomp     = $dato["numcomp"];
			//	$numcomp     = substr($numcomp  , 10, 10);
				$fecha       = $dato3["fecha"];
 				$fecha       = implode("-", array_reverse(explode("-", $fecha))); 		
				//$cdgcli      = $dato["cdgcli"];
				//$cdgcli      = sprintf("%04d",$cdgcli); 
				//$dsccli      = $dato["dsccli"];
				$totalnota   = number_format($dato3["totalnota"], 2);
				$subexe      = number_format($dato3["subexe"], 2);
				$subgra      = number_format($dato3["subgra"], 2);
				$subtot      = number_format($dato3["subtot"], 2);
				$impven      = number_format($dato3["impven"], 2);
				$otrcar      = number_format($dato3["otrcar"], 2);
				$destot      = number_format($dato3["destot"], 2);
				
				$totgravado     = number_format($dato3["totgravado"], 2);
				$totexento     = number_format($dato3["totexento"], 2);
				$totexonerado     = number_format($dato3["totexonerado"], 2);
				$totnosujeto     = number_format($dato3["totnosujeto"], 2);
			
			

?>  

 <tr>
   <td></td>



   <td align="center"><font size=1 ><?php echo $fecha?></font></td>
   <td align="right"><font size=1 ><?php echo $totexento?></font></td>
    <td align="right"><font size=1 ><?php echo $totexonerado ?></font></td>
	   <td align="right"><font size=1 ><?php echo $totnosujeto ?></font></td>
   <td align="right"><font size=1 ><?php echo $totgravado ?></font></td>
   <td align="right"><font size=1 ><?php echo  $subtot ?></font></td>
   <td align="right"><font size=1 ><?php echo $destot?></font></td>
   <td align="right"><font size=1 ><?php echo $impven ?></font></td>
   <td align="right"><font size=1 ><?php echo $otrcar ?></font></td>
   <td align="right"><font size=1 ><?php echo $totalnota ?></font></td>
    
 </tr>
  
  <?php
	 
  	        
		 } 
		 

  
     $consulta4= ("SELECT SUM(totalnota) AS total , SUM(subtot) AS subtotal, SUM(subexe) AS exento, SUM(subgra) AS gravado, SUM(totnosujeto) AS totnosujeto, SUM(destot) AS descuento , SUM(impven) AS impuesto, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado,SUM(totgravado) AS  totgravado  ,SUM(otrcar) AS  otrcar  FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda = 'USD' ".$where_notas  );
				

		if ($resultado4 = $mysqli->query($consulta4)) 

  		while ($dato4 = $resultado4->fetch_assoc()) {

				$exento_not    = $dato4["exento"];
				$gravado_not   = $dato4["gravado"];
				$subtotal_not  = $dato4["subtotal"];
				$descuento_not = $dato4["descuento"];
				$impuesto_not  = $dato4["impuesto"];
				$otrcar_not    = $dato4["otrcar"];
				$total_not     = $dato4["total"];
				
				$totgravado_not      = $dato4["totgravado"];
				$totexento_not   = $dato4["totexento"];
				$totexonerado_not   = $dato4["totexonerado"];
				$totnosujeto_not   = $dato4["totnosujeto"];

	
	
	 } 	
  
  ?>
  
</table>

<table width="92%" border="0" cellspacing="0" cellpadding="0">

 <tr>
   <td width="12%"></td>
   <td colspan="2" align="left">&nbsp;</td>
   <td width="12%" align="center">&nbsp;</td>

   <td width="12%" align="center">&nbsp;</td>
   <td width="12%" align="center">&nbsp;</td>
   <td width="12%" align="right">&nbsp;</td>
   <td width="16%" align="right">&nbsp;</td>
   <td width="16%" align="right">&nbsp;</td>
 </tr>

 
 
  <tr>

     <td width="1%"></td>
      <td align="left"><strong><font size=1 >TOTAL NOTAS CREDITO</font></strong></td>

   <td align="right"><strong><font size=1 ><?php echo  number_format($totexento_not , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($totexonerado_not , 2);?></font></strong></td>
      <td align="right"><strong><font size=1 ><?php echo  number_format($totnosujeto_not , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($totgravado_not , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format( $subtotal_not , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($descuento_not , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($impuesto_not , 2); ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($otrcar_not , 2); ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo  number_format($total_not , 2); ?></font></strong></td>
 </tr>
 
 
 
</table>


</body>
</html>

 <?php