<?php 

include ("../conexion.php");
    
require_once '../dompdf/autoload.inc.php';


require_once '../dompdf/lib/html5lib/Parser.php';
require_once '../dompdf/lib/php-font-lib/src/FontLib/Autoloader.php';
require_once '../dompdf/lib/php-svg-lib/src/autoload.php';
require_once '../dompdf/src/Autoloader.php';
Dompdf\Autoloader::register();

use Dompdf\Dompdf;

	$exento_tot    = 0;
	$gravado_tot   = 0;
	$subtotal_tot  = 0;
	$descuento_tot = 0;
	$impuesto_tot  = 0;
	$total_tot     = 0;
	$totnosujeto_fac   = 0  ; 
	$totnosujeto_not = 0  ; 
    $where_notas   = "";
    $where         = "";

$codigoHTML='

<!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>Lista</title>

</head>

   <script type="text/php">


        if ( isset($pdf) ) {

          $font = Font_Metrics::get_font("helvetica", "bold");
          $pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));

        }
        
</script>
<body>

<div align="center">
  <p>&nbsp;</p>';

       $parametros = "SELECT * FROM web_0000 ";
       $result_parametros = $mysqli->query($parametros);
       $row_para = $result_parametros->fetch_array();
     
	   $NombreComercial =  $row_para["NOMCOM"];
	   $razsoc          =  $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"];
	 
	 if ( $cdgtipven==2){ $tipo=" CREDITO"; };
	 if ( $cdgtipven==1){ $tipo=" CONTADO"; };
	 if ( $cdgtipven==""){ $tipo=" TOTAL"; };
	 

	 	 /////////////////        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.=" ORDER BY numcomp ASC";
	 
	 
	 	 /////////////////        where para facturas ********************
	 
	 
	if ($cdgtipven <> "") { $where.=" AND cdgtipven='$cdgtipven'"; }
	if ($cdgcaj <> "") { $where.=" AND cdgcaj='$cdgcaj'"; }
	if ($cdgcli <> "") { $where.=" AND cdgcli='$cdgcli'"; }
	if ($dsccli <> "") { $where.=" AND dsccli like '%$dsccli%'"; }

	$where.=" ORDER BY codfactura 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"];
	
     }
	
$codigoHTML='
    <table width="90%"  cellpadding="0" cellspacing="0"  border="0">
      <tr>
        <td width="3%" rowspan="5">&nbsp;</td>
        <td width="11%" rowspan="5"><img src="../img/logo.jpg" /></td>
        <td width="79%"><div align="center"><font color="blue" size="4"><strong>'.$NombreComercial.'</strong></font></div></td>
        <td width="7%">&nbsp;</td>
      </tr>
       <tr>
        <td height="12"><div align="center"><font size="2" >'.$razsoc.'</font></div></td>
        <td height="12">&nbsp;</td>
      </tr>
	  <tr>
        <td height="12"><div align="center"><font size="2" >Cedula '.$Cedula.'</font></div></td>
        <td height="12">&nbsp;</td>
      </tr>
	  
      <tr>
        <td height="12"><div align="center"><font size="2" >'.$Direccion.'</font></div></td>
        <td height="12">&nbsp;</td>
      </tr>
      <tr>
        <td height="11"><div align="center"><font size="2" >Numero de Telefono '.$Telefono.'</font></div></td>
        <td height="11">&nbsp;</td>
      </tr>
      <tr>
        <td height="22">&nbsp;</td>
        <td height="22">&nbsp;</td>
      </tr>
    </table>
	 

  <table width="100%" height="16%" border="1" cellpadding="0" cellspacing="0" >
      <tr>
        <td width="100%" height="11" colspan="4" align="center"><font size=3 ><strong>Reporte Analitico de Facturacion '.$tipo.'</strong></font></td>
      </tr>
      <tr>
        <td width="100%"  height="11" colspan="4" align="center">Fecha del <font size="2" >'.$fecini.'</font> al <font size="2" >'.$fecfin.'  </font>   Cajero: <font size="2" > '.$cdgcaj.' - '.$nomcomusu.' </font></td>
      </tr>
  </table>
   ';
 
	
	
 if ($cdgtipven <> "") {   ///   SI SELECIONA TIPO DE VENTA CREDITO  O CONTADO  






	 $codigoHTML.='
	 	   <table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
	<td width="100%" height="11" colspan="4" align="lefr"><font size=3 ><strong> FACTURACION DOLARES</strong></font></td>
	</tr>
  </table>
  
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
     
        <td width="30%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Factura</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>
        <td width="75%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Cliente</strong></font></td>
		<td width="10%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Plazo</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub. Exento </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Exonerado </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.NoSujeto </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Gravado </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Subtotal </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Descuento </strong></font></td>
          <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Ventas </strong></font></td>
		  <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Servicio </strong></font></td>
              <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total </strong></font></td>
 
    </tr>
  </table>';
  
  
  
 $consulta= "SELECT * FROM facturas WHERE fecha  between '$fechainicio' AND '$fechafin' AND  tipmoneda= 'USD'  ".$where  ;
    if ($resultado = $mysqli->query($consulta)){
        $count = 0;
  		while ($fila = $resultado->fetch_assoc()) {	
            $fecha         = $fila["fecha"];
            $fecha         = implode("-", array_reverse(explode("-", $fecha)));
            $numfactura    = $fila["numfactura"];
			//$numfactura    = substr($numfactura, -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($fila["totalfactura"], 2);
            $subexe        = number_format($fila["subexe"], 2);
            $subgra        = number_format($fila["subgra"], 2);
            $subtot        = number_format($fila["subtot"], 2);
            $impven        = number_format($fila["impven"], 2);
			$otrcar        = number_format($fila["otrcar"], 2);
            $destot        = number_format($fila["destot"], 2);
			
			
			$totexento     = number_format($fila["totexento"], 2);
			$totexonerado     = number_format($fila["totexonerado"], 2);
		    $totgravado   = number_format($fila["totgravado"], 2);
			$totnosujeto   = number_format($fila["totnosujeto"], 2);
		
            if($count == 0 )		
                $codigoHTML.='<table width="100%"  rules="none" border="0">';

            $codigoHTML.='    
                  <tr>
                    <td width="30%">  <font size=1 >'.$numfactura.'</font></td>
                    <td width="18%">  <font size=1 >'.$fecha.'</font></td>
                    <td width="10%">  <font size=1 align="left">'.$cdgcli.'</font></td>
                    <td width="60%">  <font size=1 align="left">'.$dsccli.'</font></td>
                    <td width="5%" align="center">  <font size=1 >'.$pladia.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totexento.'</font></td>
					 <td width="20%" align="right">  <font size=1 >'.$totexonerado.'</font></td>
					 <td width="20%" align="right">  <font size=1 >'.$totnosujeto.'</font></td>
                     <td width="20%" align="right">  <font size=1 >'.$totgravado.'</font></td>
                      <td width="20%" align="right">  <font size=1 >'.$subtot.'</font></td>

                    <td width="20%" align="right">  <font size=1 >'.$destot.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$impven.'</font></td>
					 <td width="20%" align="right">  <font size=1 >'.$otrcar.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totalfactura.'</font></td>
                  </tr> ';
              $count++;
       }
        if($count > 1) 
            $codigoHTML.='</table>';
 }
				
   
    $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(otrcar) AS imp_servicio, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado, SUM(totgravado) AS  totgravado FROM facturas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND  tipmoneda= 'USD' ".$where );

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

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

				$exento     = $dato["exento"];
				$gravado    = $dato["gravado"];
				$subtotal   = $dato["subtotal"];
				$descuento  = $dato["descuento"];
				$impuesto   = $dato["impuesto"];
				$imp_servicio   = $dato["imp_servicio"];
				$total      = $dato["total"];
				
								
				$totexento_fac    = $dato['totexento'];
				$totexonerado_fac    = $dato['totexonerado'];
				$totgravado_fac   = $dato['totgravado'];
				$totnosujeto_fac   = $dato['totnosujeto'];
			
$codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%" border="0" cellpadding="0" cellspacing="0">
         <tr>
            <td width="120%" height="12" align="left"><font size=2 > TOTAL FACTURAS DOLARES</font></td>
               <td width="20%" align="right"><font size=1 >'.number_format($totexento_fac, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totexonerado_fac, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totnosujeto_fac, 2).'</font></td>
		       <td width="20%" align="right"><font size=1 >'.number_format($totgravado_fac, 2).'</font></td>
		      <td width="20%" align="right"><font size=1 >'.number_format($subtotal, 2).'</font></td>

			  <td width="20%" align="right"><font size=1 >'.number_format($descuento, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($impuesto, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($imp_servicio, 2).'</font></td>
              <td width="20%" align="right"><font size=1 >'.number_format($total, 2).'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
     } 
	 
	/////////////////////////////////////////////////////////////////  CALCULA TARIFAS FACTURACION  ///////////////////////////////////////

$consulta3= ("SELECT * FROM facturas WHERE fecha  between '$fechainicio' AND '$fechafin' AND  tipmoneda= 'USD' ".$where  );
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R = 0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	if ($resultado3 = $mysqli->query($consulta3)) 


  	 while ($dato3 = $resultado3->fetch_assoc()) {			
	 $codfactura   = $dato3['codfactura'];

    $consulta4= ("SELECT codfactura, impuesto, cdgtar, cdgimp FROM factulinea WHERE  codfactura = '$codfactura' AND cdgimp = '01' ");

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

	 
  	 while ($dato4 = $resultado4->fetch_assoc()) {
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;

				$impuesto    = $dato4['impuesto'];
			  	$cdgtar      = $dato4['cdgtar'];
		
	     if ($cdgtar == "01") {	$I0R =  $impuesto;
	}elseif ($cdgtar == "02") { $I1R =  $impuesto;
	}elseif ($cdgtar == "03") { $I2R =  $impuesto;
	}elseif ($cdgtar == "04") { $I4R =  $impuesto;
	}elseif ($cdgtar == "05") { $I0T =  $impuesto;
	}elseif ($cdgtar == "06") { $I4T =  $impuesto;
	}elseif ($cdgtar == "07") { $I8T =  $impuesto;
	}elseif ($cdgtar == "08") { $I13 =  $impuesto;
	} 
	
	  $TOTAL_I0R =  $TOTAL_I0R + $I0R ;
	  $TOTAL_I1R =  $TOTAL_I1R + $I1R ;
	  $TOTAL_I2R =  $TOTAL_I2R + $I2R ;
	  $TOTAL_I4R =  $TOTAL_I4R + $I4R ;
	  $TOTAL_I0T =  $TOTAL_I0T + $I0T ;
	  $TOTAL_I4T =  $TOTAL_I4T + $I4T ;
	  $TOTAL_I8T =  $TOTAL_I8T + $I8T ;
	  $TOTAL_I13 =  $TOTAL_I13 + $I13 ;
	
		
	 } 		
	


	 } 		


 
	 
	 
	  
   $codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%"  cellpadding="0" cellspacing="0" border="1" >
         <tr>
           <td colspan="4"  height="12" align="left">&nbsp;</td>
           <td colspan="4"  bgcolor="#CCCCCC" align="CENTER">REDUCIDAS</td>
           <td colspan="3" bgcolor="#CCCCCC"  align="CENTER">TRANSITORIAS</td>
           <td align="CENTER" bgcolor="#CCCCCC" >GENERAL</td>
         </tr>
         <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > </font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >1%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >2%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >8%</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >13%</font></strong></td>
         </tr>

		 
		 <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > TOTAL IMPUESTOS</font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I1R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I2R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0T, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4T, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I8T, 2).'</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I13, 2).'</font></strong></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   /********************FATURACION CONTADO O CREDITO COLONES****************************/
   


	 $codigoHTML.='
	 	   <table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
	<td width="100%" height="11" colspan="3" align="lefr"><font size=3 ><strong> FACTURACION COLONES</strong></font></td>
	</tr>
  </table>
  
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
     
        <td width="30%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Factura</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>
        <td width="75%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Cliente</strong></font></td>
		<td width="10%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Plazo</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub. Exento </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Exonerado </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.NoSujeto </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Gravado </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Subtotal </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Descuento </strong></font></td>
          <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Ventas </strong></font></td>
		  <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Servicio </strong></font></td>
              <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total </strong></font></td>
 
    </tr>
  </table>';
  
  
  
 $consulta= "SELECT * FROM facturas WHERE fecha  between '$fechainicio' AND '$fechafin' AND  tipmoneda!= 'USD'   ".$where  ;
    if ($resultado = $mysqli->query($consulta)){
        $count = 0;
  		while ($fila = $resultado->fetch_assoc()) {	
            $fecha         = $fila["fecha"];
            $fecha         = implode("-", array_reverse(explode("-", $fecha)));
            $numfactura    = $fila["numfactura"];
			//$numfactura    = substr($numfactura, -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($fila["totalfactura"], 2);
            $subexe        = number_format($fila["subexe"], 2);
            $subgra        = number_format($fila["subgra"], 2);
            $subtot        = number_format($fila["subtot"], 2);
            $impven        = number_format($fila["impven"], 2);
			$otrcar        = number_format($fila["otrcar"], 2);
            $destot        = number_format($fila["destot"], 2);
			
			
			$totexento     = number_format($fila["totexento"], 2);
			$totexonerado     = number_format($fila["totexonerado"], 2);
		    $totgravado   = number_format($fila["totgravado"], 2);
			 $totnosujeto   = number_format($fila["totnosujeto"], 2);
		
            if($count == 0 )		
                $codigoHTML.='<table width="100%"  rules="none" border="0">';

            $codigoHTML.='    
                  <tr>
                    <td width="30%">  <font size=1 >'.$numfactura.'</font></td>
                    <td width="18%">  <font size=1 >'.$fecha.'</font></td>
                    <td width="10%">  <font size=1 align="left">'.$cdgcli.'</font></td>
                    <td width="60%">  <font size=1 align="left">'.$dsccli.'</font></td>
                    <td width="5%" align="center">  <font size=1 >'.$pladia.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totexento.'</font></td>
					 <td width="20%" align="right">  <font size=1 >'.$totnosujeto.'</font></td>
					  <td width="20%" align="right">  <font size=1 >'.$totexonerado.'</font></td>
                     <td width="20%" align="right">  <font size=1 >'.$totgravado.'</font></td>
                      <td width="20%" align="right">  <font size=1 >'.$subtot.'</font></td>

                    <td width="20%" align="right">  <font size=1 >'.$destot.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$impven.'</font></td>
					<td width="20%" align="right">  <font size=1 >'.$otrcar.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totalfactura.'</font></td>
                  </tr> ';
              $count++;
       }
        if($count > 1) 
            $codigoHTML.='</table>';
 }
				
   
    $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(otrcar) AS imp_servicio, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado, SUM(totgravado) AS  totgravado FROM facturas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND  tipmoneda!= 'USD'  ".$where );

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

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

				$exento     = $dato["exento"];
				$gravado    = $dato["gravado"];
				$subtotal   = $dato["subtotal"];
				$descuento  = $dato["descuento"];
				$impuesto   = $dato["impuesto"];
				$imp_servicio   = $dato["imp_servicio"];
				$total      = $dato["total"];
				
								
				$totexento_fac    = $dato['totexento'];
				$totexonerado_fac    = $dato['totexonerado'];
				$totgravado_fac   = $dato['totgravado'];
				$totnosujeto_fac   = $dato['totnosujeto'];
			
$codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%" border="0" cellpadding="0" cellspacing="0">
         <tr>
            <td width="120%" height="12" align="left"><font size=2 > TOTAL FACTURAS COLONES</font></td>
               <td width="20%" align="right"><font size=1 >'.number_format($totexento_fac, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totexonerado_fac, 2).'</font></td>
		       <td width="20%" align="right"><font size=1 >'.number_format($totnosujeto_fac, 2).'</font></td>			   
		       <td width="20%" align="right"><font size=1 >'.number_format($totgravado_fac, 2).'</font></td>
		      <td width="20%" align="right"><font size=1 >'.number_format($subtotal, 2).'</font></td>

			  <td width="20%" align="right"><font size=1 >'.number_format($descuento, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($impuesto, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($imp_servicio, 2).'</font></td>
              <td width="20%" align="right"><font size=1 >'.number_format($total, 2).'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
     } 
	 
	/////////////////////////////////////////////////////////////////  CALCULA TARIFAS FACTURACION  ///////////////////////////////////////

$consulta3= ("SELECT * FROM facturas WHERE fecha  between '$fechainicio' AND '$fechafin' AND  tipmoneda!= 'USD'  ".$where  );
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R = 0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	if ($resultado3 = $mysqli->query($consulta3)) 


  	 while ($dato3 = $resultado3->fetch_assoc()) {			
	 $codfactura   = $dato3['codfactura'];

    $consulta4= ("SELECT codfactura, impuesto, cdgtar, cdgimp FROM factulinea WHERE  codfactura = '$codfactura' AND cdgimp = '01' ");

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

	 
  	 while ($dato4 = $resultado4->fetch_assoc()) {
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;

				$impuesto    = $dato4['impuesto'];
			  	$cdgtar      = $dato4['cdgtar'];
		
	     if ($cdgtar == "01") {	$I0R =  $impuesto;
	}elseif ($cdgtar == "02") { $I1R =  $impuesto;
	}elseif ($cdgtar == "03") { $I2R =  $impuesto;
	}elseif ($cdgtar == "04") { $I4R =  $impuesto;
	}elseif ($cdgtar == "05") { $I0T =  $impuesto;
	}elseif ($cdgtar == "06") { $I4T =  $impuesto;
	}elseif ($cdgtar == "07") { $I8T =  $impuesto;
	}elseif ($cdgtar == "08") { $I13 =  $impuesto;
	} 
	
	  $TOTAL_I0R =  $TOTAL_I0R + $I0R ;
	  $TOTAL_I1R =  $TOTAL_I1R + $I1R ;
	  $TOTAL_I2R =  $TOTAL_I2R + $I2R ;
	  $TOTAL_I4R =  $TOTAL_I4R + $I4R ;
	  $TOTAL_I0T =  $TOTAL_I0T + $I0T ;
	  $TOTAL_I4T =  $TOTAL_I4T + $I4T ;
	  $TOTAL_I8T =  $TOTAL_I8T + $I8T ;
	  $TOTAL_I13 =  $TOTAL_I13 + $I13 ;
	
		
	 } 		
	


	 } 		


 
	 
	 
	  
   $codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%"  cellpadding="0" cellspacing="0" border="1" >
         <tr>
           <td colspan="4"  height="12" align="left">&nbsp;</td>
           <td colspan="4"  bgcolor="#CCCCCC" align="CENTER">REDUCIDAS</td>
           <td colspan="3" bgcolor="#CCCCCC"  align="CENTER">TRANSITORIAS</td>
           <td align="CENTER" bgcolor="#CCCCCC" >GENERAL</td>
         </tr>
         <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > </font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >1%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >2%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >8%</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >13%</font></strong></td>
         </tr>

		 
		 <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > TOTAL IMPUESTOS</font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I1R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I2R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0T, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4T, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I8T, 2).'</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I13, 2).'</font></strong></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
 
	    /******************NOTAS CREDITO DOLARES CONTADO O CREDIO*********************/
	 
 $codigoHTML.='
	 	 
	  <p>&nbsp;</p>

  <table width="100%" height="16%" border="1" cellpadding="0" cellspacing="0" >
      <tr>
        <td width="100%" height="21" colspan="3" align="center"><font size=3 ><strong>Reporte Notas de Credito</strong></font></td>
      </tr>
      <tr>
        <td height="14" colspan="3" align="center">Fecha del <font size="2" >'.$fecini.'</font> al <font size="2" >'.$fecfin.'  </font>   Cajero: <font size="2" > '.$cdgcaj.' - '.$nomcomusu.' </font></td>
      </tr>
  </table>
  ';
	
	 $codigoHTML.='
	   <table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
	<td width="100%" height="11" colspan="3" align="lefr"><font size=3 ><strong> NOTAS CREDITO DOLARES</strong></font></td>
	</tr>
  </table>
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
     
        <td width="30%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Factura</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>
        <td width="75%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Cliente</strong></font></td>
		<td width="10%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Plazo</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub. Exento </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Exonerado </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.NoSujeto </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Gravado </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Subtotal </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Descuento </strong></font></td>
          <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Ventas </strong></font></td>
		  <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Servicio </strong></font></td>
              <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total </strong></font></td>
 
    </tr>
  </table>';
  
		
		

	$consulta= "SELECT * FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin'  AND tipmoneda= 'USD' ".$where_notas ;
		if ($resultado = $mysqli->query($consulta)) 
        $count = 0;
  		while ($dato = $resultado->fetch_assoc()) {
	
				$codnota     = $dato["codnota"];
				$fecha       = $dato["fecha"];
 				$fecha       = implode("-", array_reverse(explode("-", $fecha))); 	
			    $numcomp     = $dato["numcomp"];
				//$numcomp     = substr($numcomp , -10);	
				$cdgcli      = $dato["cdgcli"];
				$cdgcli      = sprintf("%04d",$cdgcli); 
				$dsccli      = $dato["dsccli"];
				$totalnota   = number_format($dato["totalnota"], 2);
				$subexe      = number_format($dato["subexe"], 2);
				$subgra      = number_format($dato["subgra"], 2);
				$subtot      = number_format($dato["subtot"], 2);
				$impven      = number_format($dato["impven"], 2);
				$destot      = number_format($dato["destot"], 2);
				
			$totexento     = number_format($fila["totexento"], 2);
			$totexonerado     = number_format($fila["totexonerado"], 2);
		    $totgravado   = number_format($fila["totgravado"], 2);
			$totnosujeto   = number_format($fila["totnosujeto"], 2);
			
			
		    if($count == 0 )		
                $codigoHTML.='<table width="100%"  rules="none" border="0">';

            $codigoHTML.='    
   
   
                <tr>
                    <td width="30%">  <font size=1 >'.$numcomp.'</font></td>
                    <td width="18%">  <font size=1 >'.$fecha.'</font></td>
                    <td width="10%">  <font size=1 align="left">'.$cdgcli.'</font></td>
                    <td width="60%">  <font size=1 align="left">'.$dsccli.'</font></td>
                    <td width="5%" align="center">  <font size=1 >'.$pladia.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totexento.'</font></td>
					 <td width="20%" align="right">  <font size=1 >'.$totexonerado.'</font></td>
					 <td width="20%" align="right">  <font size=1 >'.$totnosujeto.'</font></td>
                     <td width="20%" align="right">  <font size=1 >'.$totgravado.'</font></td>
                      <td width="20%" align="right">  <font size=1 >'.$subtot.'</font></td>

                    <td width="20%" align="right">  <font size=1 >'.$destot.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$impven.'</font></td>
					 <td width="20%" align="right">  <font size=1 >'.$otrcar.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totalnota.'</font></td>
                  </tr> ';
				  
				  
              $count++;
       }
        if($count > 1) 
            $codigoHTML.='</table>';
 
			 
$consulta= ("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(otrcar) AS imp_servicio, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado, SUM(totgravado) AS  totgravado FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin'  AND tipmoneda= 'USD'".$where_notas );
		if ($resultado = $mysqli->query($consulta)) 

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

				$exento_not    = $dato["exento"];
				$gravado_not   = $dato["gravado"];
				$subtotal_not  = $dato["subtotal"];
				$descuento_not = $dato["descuento"];
				$impuesto_not  = $dato["impuesto"];
				$total_not     = $dato["total"];
				
				$totgravado_not  = $dato['totgravado'];
				$totexento_not    = $dato['totexento'];
				$totexonerado_not    = $dato['totexonerado'];
				$totnosujeto_not    = $dato['totnosujeto'];
			
	
			
$codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%" border="0" cellpadding="0" cellspacing="0">
         <tr>
            <td width="120%" height="12" align="left"><font size=2 > TOTAL NOTAS DOLARES</font></td>
               <td width="20%" align="right"><font size=1 >'.number_format($totexento_not, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totexonerado_not, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totnosujeto_not, 2).'</font></td>
		       <td width="20%" align="right"><font size=1 >'.number_format($totgravado_not, 2).'</font></td>
		      <td width="20%" align="right"><font size=1 >'.number_format($subtotal_not, 2).'</font></td>

			  <td width="20%" align="right"><font size=1 >'.number_format($descuento_not, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($impuesto_not, 2).'</font></td>
              <td width="20%" align="right"><font size=1 >'.number_format($total_not, 2).'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
   
   
 }
 
 
$consulta3= ("SELECT  codnota FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda= 'USD' ".$where_notas  );
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R = 0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	if ($resultado3 = $mysqli->query($consulta3)) 


  	 while ($dato3 = $resultado3->fetch_assoc()) {			
	 $codnota   = $dato3['codnota'];

    $consulta4= ("SELECT codnota, impuesto, cdgtar, cdgimp FROM factulinea_notas WHERE  codnota = '$codnota' AND cdgimp = '01' ");

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

	 
  	 while ($dato4 = $resultado4->fetch_assoc()) {
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;

				$impuesto    = $dato4['impuesto'];
			  	$cdgtar      = $dato4['cdgtar'];
		
	     if ($cdgtar == "01") {	$I0R =  $impuesto;
	}elseif ($cdgtar == "02") { $I1R =  $impuesto;
	}elseif ($cdgtar == "03") { $I2R =  $impuesto;
	}elseif ($cdgtar == "04") { $I4R =  $impuesto;
	}elseif ($cdgtar == "05") { $I0T =  $impuesto;
	}elseif ($cdgtar == "06") { $I4T =  $impuesto;
	}elseif ($cdgtar == "07") { $I8T =  $impuesto;
	}elseif ($cdgtar == "08") { $I13 =  $impuesto;
	} 
	
	  $TOTAL_I0R =  $TOTAL_I0R + $I0R ;
	  $TOTAL_I1R =  $TOTAL_I1R + $I1R ;
	  $TOTAL_I2R =  $TOTAL_I2R + $I2R ;
	  $TOTAL_I4R =  $TOTAL_I4R + $I4R ;
	  $TOTAL_I0T =  $TOTAL_I0T + $I0T ;
	  $TOTAL_I4T =  $TOTAL_I4T + $I4T ;
	  $TOTAL_I8T =  $TOTAL_I8T + $I8T ;
	  $TOTAL_I13 =  $TOTAL_I13 + $I13 ;
	
		
	 } 		
	


	 } 		


	  
   $codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%"  cellpadding="0" cellspacing="0" border="1" >
         <tr>
           <td colspan="4"  height="12" align="left">&nbsp;</td>
           <td colspan="4"  bgcolor="#CCCCCC" align="CENTER">REDUCIDAS</td>
           <td colspan="3" bgcolor="#CCCCCC"  align="CENTER">TRANSITORIAS</td>
           <td align="CENTER" bgcolor="#CCCCCC" >GENERAL</td>
         </tr>
         <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > </font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >1%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >2%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >8%</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >13%</font></strong></td>
         </tr>

		 
		 <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > TOTAL IMPUESTOS</font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I1R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I2R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0T, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4T, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I8T, 2).'</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I13, 2).'</font></strong></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
 
 /*************COLONES*****/
 
    /*****************************NOTAS COLONES*******************************************************/
   $codigoHTML.='
   	 	 <table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
	<td width="100%" height="11" colspan="3" align="lefr"><font size=3 ><strong> NOTAS CREDITO COLONES</strong></font></td>
	</tr>
  </table>
	  
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
    
        <td width="30%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Nota</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>
        <td width="60%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Cliente</strong></font></td>
		<td width="10%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong></strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Exento </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Exonerado</strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.NoSujeto</strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Gravado </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Subtotal </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Descuento </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Ventas </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Servicio </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total </strong></font></td>
 
    </tr>
  </table>';
		
		

	$consulta= "SELECT * FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin'  AND tipmoneda!= 'USD'  ".$where_notas ;
		if ($resultado = $mysqli->query($consulta)) 
        $count = 0;
  		while ($dato = $resultado->fetch_assoc()) {
	
				$codnota     = $dato["codnota"];
				$fecha       = $dato["fecha"];
 				$fecha       = implode("-", array_reverse(explode("-", $fecha))); 	
			    $numcomp     = $dato["numcomp"];
				//$numcomp     = substr($numcomp , -10);	
				$cdgcli      = $dato["cdgcli"];
				$cdgcli      = sprintf("%04d",$cdgcli); 
				$dsccli      = $dato["dsccli"];
				$totalnota   = number_format($dato["totalnota"], 2);
				$subexe      = number_format($dato["subexe"], 2);
				$subgra      = number_format($dato["subgra"], 2);
				$subtot      = number_format($dato["subtot"], 2);
				$impven      = number_format($dato["impven"], 2);
				$destot      = number_format($dato["destot"], 2);
				
				$totgravado     = number_format($fila["totgravado"], 2);
				$totexento     = number_format($fila["totexento"], 2);
				$totexonerado     = number_format($fila["totexonerado"], 2);
				$totnosujeto     = number_format($fila["totnosujeto"], 2);
				
		    if($count == 0 )		
                $codigoHTML.='<table width="100%"  rules="none" border="0">';

            $codigoHTML.='    
      <tr>
        <td width="18%">  <font size=1 >'.$numcomp.'</font></td>
        <td width="8%">  <font size=1 >'.$fecha.'</font></td>
   	    <td width="10%">  <font size=1 align="left">'.$cdgcli.'</font></td>
   	    <td width="25%">  <font size=1 align="left">'.$dsccli.'</font></td>
		
        <td width="10%" align="right">  <font size=1 >'.$totexento.'</font></td>
		<td width="10%" align="right">  <font size=1 >'.$totexonerado.'</font></td>
		<td width="11%" align="right">  <font size=1 >'.$totnosujeto.'</font></td>
		<td width="11%" align="right">  <font size=1 >'.$totgravado.'</font></td>
	    <td width="10%" align="right">  <font size=1 >'.$subtot.'</font></td>
		<td width="10%" align="right">  <font size=1 >'.$destot.'</font></td>
		 <td width="11%" align="right">  <font size=1 >'.$impven.'</font></td>
		<td width="10%" align="right">  <font size=1 >'.$totalnota.'</font></td>
   </tr> ';
              $count++;
       }
        if($count > 1) 
            $codigoHTML.='</table>';
 
			 
$consulta= ("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(otrcar) AS imp_servicio, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado,SUM(totgravado) AS  totgravado FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin'  AND tipmoneda!= 'USD' ".$where_notas );
		if ($resultado = $mysqli->query($consulta)) 

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

				$exento_not    = $dato["exento"];
				$gravado_not   = $dato["gravado"];
				$subtotal_not  = $dato["subtotal"];
				$descuento_not = $dato["descuento"];
				$impuesto_not  = $dato["impuesto"];
				$total_not     = $dato["total"];
				
				$totexento_not  = $dato["totexento"];
				$totexonerado_not   = $dato["totexonerado"];
				$totgravado_not   = $dato["totgravado"];
				$totnosujeto_not   = $dato["totnosujeto"];
			
	
			
$codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%" border="0" cellpadding="0" cellspacing="0">
         <tr>
            <td width="120%" height="12" align="left"><font size=2 > TOTAL NOTAS COLONES</font></td>
               <td width="20%" align="right"><font size=1 >'.number_format($totexento_not, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totexonerado_not, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totnosujeto_not, 2).'</font></td>
		       <td width="20%" align="right"><font size=1 >'.number_format($totgravado_not, 2).'</font></td>
		      <td width="20%" align="right"><font size=1 >'.number_format($subtotal_not, 2).'</font></td>

			  <td width="20%" align="right"><font size=1 >'.number_format($descuento_not, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($impuesto_not, 2).'</font></td>
              <td width="20%" align="right"><font size=1 >'.number_format($total_not, 2).'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
      } 
	 
	 
	 
	 /////////////////////////////////////////////////////////////////  CALCULA TARIFAS NOTAS ///////////////////////////////////////

$consulta3= ("SELECT  codnota FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda!= 'USD'  ".$where_notas  );
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R = 0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	if ($resultado3 = $mysqli->query($consulta3)) 


  	 while ($dato3 = $resultado3->fetch_assoc()) {			
	 $codnota   = $dato3['codnota'];

    $consulta4= ("SELECT codnota, impuesto, cdgtar, cdgimp FROM factulinea_notas WHERE  codnota = '$codnota' AND cdgimp = '01' ");

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

	 
  	 while ($dato4 = $resultado4->fetch_assoc()) {
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;

				$impuesto    = $dato4['impuesto'];
			  	$cdgtar      = $dato4['cdgtar'];
		
	     if ($cdgtar == "01") {	$I0R =  $impuesto;
	}elseif ($cdgtar == "02") { $I1R =  $impuesto;
	}elseif ($cdgtar == "03") { $I2R =  $impuesto;
	}elseif ($cdgtar == "04") { $I4R =  $impuesto;
	}elseif ($cdgtar == "05") { $I0T =  $impuesto;
	}elseif ($cdgtar == "06") { $I4T =  $impuesto;
	}elseif ($cdgtar == "07") { $I8T =  $impuesto;
	}elseif ($cdgtar == "08") { $I13 =  $impuesto;
	} 
	
	  $TOTAL_I0R =  $TOTAL_I0R + $I0R ;
	  $TOTAL_I1R =  $TOTAL_I1R + $I1R ;
	  $TOTAL_I2R =  $TOTAL_I2R + $I2R ;
	  $TOTAL_I4R =  $TOTAL_I4R + $I4R ;
	  $TOTAL_I0T =  $TOTAL_I0T + $I0T ;
	  $TOTAL_I4T =  $TOTAL_I4T + $I4T ;
	  $TOTAL_I8T =  $TOTAL_I8T + $I8T ;
	  $TOTAL_I13 =  $TOTAL_I13 + $I13 ;
	
		
	 } 		
	


	 } 		


	  
   $codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%"  cellpadding="0" cellspacing="0" border="1" >
         <tr>
           <td colspan="4"  height="12" align="left">&nbsp;</td>
           <td colspan="4"  bgcolor="#CCCCCC" align="CENTER">REDUCIDAS</td>
           <td colspan="3" bgcolor="#CCCCCC"  align="CENTER">TRANSITORIAS</td>
           <td align="CENTER" bgcolor="#CCCCCC" >GENERAL</td>
         </tr>
         <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > </font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >1%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >2%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >8%</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >13%</font></strong></td>
         </tr>

		 
		 <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > TOTAL IMPUESTOS</font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I1R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I2R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0T, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4T, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I8T, 2).'</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I13, 2).'</font></strong></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>'; 
  

 }else {
 
	 $codigoHTML.='
	 
	 <table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
	<td width="100%" height="11" colspan="3" align="left"><font size=3 ><strong> FACTURACION DOLARES</strong></font></td>
	</tr>
  </table>
  
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
     
        <td width="30%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Factura</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>
        <td width="75%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Cliente</strong></font></td>
		<td width="10%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Plazo</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub. Exento </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Exonerado </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.NoSujeto </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Gravado </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Subtotal </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Descuento </strong></font></td>
          <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Ventas </strong></font></td>
		  <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Servicio </strong></font></td>
              <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total </strong></font></td>
 
    </tr>
  </table>';
 
	/********SI ES EN DOLARES*****/ 

    $consulta= "SELECT * FROM facturas WHERE fecha  between '$fechainicio' AND '$fechafin'   AND  tipmoneda= 'USD' ".$where  ;
    if ($resultado = $mysqli->query($consulta)){
        $count = 0;
  		while ($fila = $resultado->fetch_assoc()) {	
            $fecha         = $fila["fecha"];
            $fecha         = implode("-", array_reverse(explode("-", $fecha)));
            $numfactura    = $fila["numfactura"];
			//$numfactura    = substr($numfactura, -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($fila["totalfactura"], 2);
            $subexe        = number_format($fila["subexe"], 2);
            $subgra        = number_format($fila["subgra"], 2);
            $subtot        = number_format($fila["subtot"], 2);
            $impven        = number_format($fila["impven"], 2);
			$otrcar        = number_format($fila["otrcar"], 2);
            $destot        = number_format($fila["destot"], 2);
			
			$totexento     = number_format($fila["totexento"], 2);
			$totexonerado     = number_format($fila["totexonerado"], 2);
		    $totgravado   = number_format($fila["totgravado"], 2);
			$totnosujeto   = number_format($fila["totnosujeto"], 2);
			
			
            if($count == 0 )		
                $codigoHTML.='<table width="100%"  rules="none" border="0">';

            $codigoHTML.='    
			
                  <tr>
				  
                    <td width="30%">  <font size=1 >'.$numfactura.'</font></td>
                    <td width="18%">  <font size=1 >'.$fecha.'</font></td>
                    <td width="10%">  <font size=1 align="left">'.$cdgcli.'</font></td>
                    <td width="60%">  <font size=1 align="left">'.$dsccli.'</font></td>
                    <td width="5%" align="center">  <font size=1 >'.$pladia.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totexento.'</font></td>
					<td width="20%" align="right">  <font size=1 >'.$totexonerado.'</font></td>
					} <td width="20%" align="right">  <font size=1 >'.$totnosujeto.'</font></td>
                     <td width="20%" align="right">  <font size=1 >'.$totgravado.'</font></td>
                      <td width="20%" align="right">  <font size=1 >'.$subtot.'</font></td>

                    <td width="20%" align="right">  <font size=1 >'.$destot.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$impven.'</font></td>
					<td width="20%" align="right">  <font size=1 >'.$otrcar.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totalfactura.'</font></td>
                  </tr> ';
              $count++;
       }
        if($count > 1) 
            $codigoHTML.='</table>';
 }
				
   
    $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(otrcar) AS imp_servicio, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado, SUM(totgravado) AS  totgravado FROM facturas WHERE  fecha  between '$fechainicio' AND '$fechafin'  AND  tipmoneda= 'USD' ".$where  );

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

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

				$exento     = $dato["exento"];
				$gravado    = $dato["gravado"];
				$subtotal   = $dato["subtotal"];
				$descuento  = $dato["descuento"];
				$impuesto   = $dato["impuesto"];
				$imp_servicio   = $dato["imp_servicio"];
				$total      = $dato["total"];
				
				$totgravado_fac   = $dato['totgravado'];
				$totexento_fac    = $dato['totexento'];
				$totexonerado_fac    = $dato['totexonerado'];
				$totnosujeto_fac    = $dato['totnosujeto'];
				
			
$codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%" border="0" cellpadding="0" cellspacing="0">
         <tr>
            <td width="120%" height="12" align="left"><font size=2 > TOTAL FACTURAS DOLARES</font></td>
               <td width="20%" align="right"><font size=1 >'.number_format($totexento_fac, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totexonerado_fac, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totnosujeto_fac, 2).'</font></td>
		       <td width="20%" align="right"><font size=1 >'.number_format($totgravado_fac, 2).'</font></td>
		      <td width="20%" align="right"><font size=1 >'.number_format($subtotal, 2).'</font></td>

			  <td width="20%" align="right"><font size=1 >'.number_format($descuento, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($impuesto, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($imp_servicio, 2).'</font></td>
              <td width="20%" align="right"><font size=1 >'.number_format($total, 2).'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
     } 
	 
	 
	 	/////////////////////////////////////////////////////////////////  CALCULA TARIFAS FACTURACION  ///////////////////////////////////////

$consulta3= ("SELECT * FROM facturas WHERE fecha  between '$fechainicio' AND '$fechafin' AND  tipmoneda= 'USD' ".$where  );
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R = 0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	if ($resultado3 = $mysqli->query($consulta3)) 


  	 while ($dato3 = $resultado3->fetch_assoc()) {			
	 $codfactura   = $dato3['codfactura'];

    $consulta4= ("SELECT codfactura, impuesto, cdgtar, cdgimp FROM factulinea WHERE  codfactura = '$codfactura' AND cdgimp = '01' ");

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

	 
  	 while ($dato4 = $resultado4->fetch_assoc()) {
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;

				$impuesto    = $dato4['impuesto'];
			  	$cdgtar      = $dato4['cdgtar'];
		
	     if ($cdgtar == "01") {	$I0R =  $impuesto;
	}elseif ($cdgtar == "02") { $I1R =  $impuesto;
	}elseif ($cdgtar == "03") { $I2R =  $impuesto;
	}elseif ($cdgtar == "04") { $I4R =  $impuesto;
	}elseif ($cdgtar == "05") { $I0T =  $impuesto;
	}elseif ($cdgtar == "06") { $I4T =  $impuesto;
	}elseif ($cdgtar == "07") { $I8T =  $impuesto;
	}elseif ($cdgtar == "08") { $I13 =  $impuesto;
	} 
	
	  $TOTAL_I0R =  $TOTAL_I0R + $I0R ;
	  $TOTAL_I1R =  $TOTAL_I1R + $I1R ;
	  $TOTAL_I2R =  $TOTAL_I2R + $I2R ;
	  $TOTAL_I4R =  $TOTAL_I4R + $I4R ;
	  $TOTAL_I0T =  $TOTAL_I0T + $I0T ;
	  $TOTAL_I4T =  $TOTAL_I4T + $I4T ;
	  $TOTAL_I8T =  $TOTAL_I8T + $I8T ;
	  $TOTAL_I13 =  $TOTAL_I13 + $I13 ;
	
		
	 } 		
	


	 } 		


 
	 
	 
	  
   $codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%"  cellpadding="0" cellspacing="0" border="1" >
         <tr>
           <td colspan="4"  height="12" align="left">&nbsp;</td>
           <td colspan="4"  bgcolor="#CCCCCC" align="CENTER">REDUCIDAS</td>
           <td colspan="3" bgcolor="#CCCCCC"  align="CENTER">TRANSITORIAS</td>
           <td align="CENTER" bgcolor="#CCCCCC" >GENERAL</td>
         </tr>
         <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > </font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >1%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >2%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >8%</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >13%</font></strong></td>
         </tr>

		 
		 <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > TOTAL IMPUESTOS</font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I1R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I2R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0T, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4T, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I8T, 2).'</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I13, 2).'</font></strong></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
   
	 /*************************SI ES EN COLONES*************************************/
	 
	 $codigoHTML.='
	 
	 
	 	 <table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
	<td width="100%" height="11" colspan="3" align="lefr"><font size=3 ><strong> FACTURACION COLONES</strong></font></td>
	</tr>
  </table>
  
  
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">

    <tr>
  
        <td width="30%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Factura</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>
        <td width="75%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Cliente</strong></font></td>
		<td width="10%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Plazo</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub. Exento </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Exonerado </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.NoSujeto </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Gravado </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Subtotal </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Descuento </strong></font></td>
          <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Ventas </strong></font></td>
		  <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Servicio </strong></font></td>
              <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total </strong></font></td>
 
    </tr>
  </table>'; 
	 
    $consulta= "SELECT * FROM facturas WHERE fecha  between '$fechainicio' AND '$fechafin'  AND  tipmoneda!= 'USD' ".$where  ;
    if ($resultado = $mysqli->query($consulta)){
        $count = 0;
  		while ($fila = $resultado->fetch_assoc()) {	
            $fecha         = $fila["fecha"];
            $fecha         = implode("-", array_reverse(explode("-", $fecha)));
            $numfactura    = $fila["numfactura"];
			//$numfactura    = substr($numfactura, -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($fila["totalfactura"], 2);
            $subexe        = number_format($fila["subexe"], 2);
            $subgra        = number_format($fila["subgra"], 2);
            $subtot        = number_format($fila["subtot"], 2);
            $impven        = number_format($fila["impven"], 2);
			$otrcar        = number_format($fila["otrcar"], 2);
            $destot        = number_format($fila["destot"], 2);
			
			$totexento     = number_format($fila["totexento"], 2);
			$totexonerado     = number_format($fila["totexonerado"], 2);
		    $totgravado   = number_format($fila["totgravado"], 2);
			 $totnosujeto   = number_format($fila["totnosujeto"], 2);
			
			
            if($count == 0 )		
                $codigoHTML.='<table width="100%"  rules="none" border="0">';

            $codigoHTML.='    
                  <tr>
                    <td width="30%">  <font size=1 >'.$numfactura.'</font></td>
                    <td width="18%">  <font size=1 >'.$fecha.'</font></td>
                    <td width="10%">  <font size=1 align="left">'.$cdgcli.'</font></td>
                    <td width="60%">  <font size=1 align="left">'.$dsccli.'</font></td>
                    <td width="5%" align="center">  <font size=1 >'.$pladia.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totexento.'</font></td>
					<td width="20%" align="right">  <font size=1 >'.$totexonerado.'</font></td>
					<td width="20%" align="right">  <font size=1 >'.$totnosujeto.'</font></td>
                     <td width="20%" align="right">  <font size=1 >'.$totgravado.'</font></td>
                      <td width="20%" align="right">  <font size=1 >'.$subtot.'</font></td>

                    <td width="20%" align="right">  <font size=1 >'.$destot.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$impven.'</font></td>
					<td width="20%" align="right">  <font size=1 >'.$otrcar.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totalfactura.'</font></td>
                  </tr> ';
              $count++;
       }
        if($count > 1) 
            $codigoHTML.='</table>';
 }
				
   
    $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(otrcar) AS imp_servicio, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado, SUM(totgravado) AS  totgravado FROM facturas WHERE  fecha  between '$fechainicio' AND '$fechafin'   AND  tipmoneda!= 'USD' ".$where  );

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

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

				$exento     = $dato["exento"];
				$gravado    = $dato["gravado"];
				$subtotal   = $dato["subtotal"];
				$descuento  = $dato["descuento"];
				$impuesto   = $dato["impuesto"];
				$imp_servicio   = $dato["imp_servicio"];
				$total      = $dato["total"];
				
				$totgravado_fac   = $dato['totgravado'];
				$totexento_fac    = $dato['totexento'];
				$totexonerado_fac    = $dato['totexonerado'];
				$totnosujeto_fac    = $dato['totnosujeto'];
				
			
$codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%" border="0" cellpadding="0" cellspacing="0">
         <tr>
            <td width="120%" height="12" align="left"><font size=2 > TOTAL FACTURAS COLONES</font></td>
               <td width="20%" align="right"><font size=1 >'.number_format($totexento_fac, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totexonerado_fac, 2).'</font></td>
			    <td width="20%" align="right"><font size=1 >'.number_format($totnosujeto_fac, 2).'</font></td>
		       <td width="20%" align="right"><font size=1 >'.number_format($totgravado_fac, 2).'</font></td>
		      <td width="20%" align="right"><font size=1 >'.number_format($subtotal, 2).'</font></td>

			  <td width="20%" align="right"><font size=1 >'.number_format($descuento, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($impuesto, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($imp_servicio, 2).'</font></td>
              <td width="20%" align="right"><font size=1 >'.number_format($total, 2).'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
     } 
	 
	 
	 	/////////////////////////////////////////////////////////////////  CALCULA TARIFAS FACTURACION  ///////////////////////////////////////

$consulta3= ("SELECT * FROM facturas WHERE fecha  between '$fechainicio' AND '$fechafin'  AND  tipmoneda!= 'USD' ".$where  );
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R = 0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	if ($resultado3 = $mysqli->query($consulta3)) 


  	 while ($dato3 = $resultado3->fetch_assoc()) {			
	 $codfactura   = $dato3['codfactura'];

    $consulta4= ("SELECT codfactura, impuesto, cdgtar, cdgimp FROM factulinea WHERE  codfactura = '$codfactura' AND cdgimp = '01' ");

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

	 
  	 while ($dato4 = $resultado4->fetch_assoc()) {
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;

				$impuesto    = $dato4['impuesto'];
			  	$cdgtar      = $dato4['cdgtar'];
		
	     if ($cdgtar == "01") {	$I0R =  $impuesto;
	}elseif ($cdgtar == "02") { $I1R =  $impuesto;
	}elseif ($cdgtar == "03") { $I2R =  $impuesto;
	}elseif ($cdgtar == "04") { $I4R =  $impuesto;
	}elseif ($cdgtar == "05") { $I0T =  $impuesto;
	}elseif ($cdgtar == "06") { $I4T =  $impuesto;
	}elseif ($cdgtar == "07") { $I8T =  $impuesto;
	}elseif ($cdgtar == "08") { $I13 =  $impuesto;
	} 
	
	  $TOTAL_I0R =  $TOTAL_I0R + $I0R ;
	  $TOTAL_I1R =  $TOTAL_I1R + $I1R ;
	  $TOTAL_I2R =  $TOTAL_I2R + $I2R ;
	  $TOTAL_I4R =  $TOTAL_I4R + $I4R ;
	  $TOTAL_I0T =  $TOTAL_I0T + $I0T ;
	  $TOTAL_I4T =  $TOTAL_I4T + $I4T ;
	  $TOTAL_I8T =  $TOTAL_I8T + $I8T ;
	  $TOTAL_I13 =  $TOTAL_I13 + $I13 ;
	
		
	 } 		
	


	 } 		


 
	 
	 
	  
   $codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%"  cellpadding="0" cellspacing="0" border="1" >
         <tr>
           <td colspan="4"  height="12" align="left">&nbsp;</td>
           <td colspan="4"  bgcolor="#CCCCCC" align="CENTER">REDUCIDAS</td>
           <td colspan="3" bgcolor="#CCCCCC"  align="CENTER">TRANSITORIAS</td>
           <td align="CENTER" bgcolor="#CCCCCC" >GENERAL</td>
         </tr>
         <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > </font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >1%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >2%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >8%</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >13%</font></strong></td>
			  
         </tr>

		 
		 <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > TOTAL IMPUESTOS</font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I1R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I2R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0T, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4T, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I8T, 2).'</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I13, 2).'</font></strong></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
   /*************************************************************************************************************************/
   
   
   
   
	    /////////////////////////////////////////////////////////////////////   notas de  credito /////////////////////////////////
	 
 $codigoHTML.='
	 	 
	  <p>&nbsp;</p>
	  	  <p>&nbsp;</p>
		  	  <p>&nbsp;</p>
			  	  <p>&nbsp;</p>
				  	  <p>&nbsp;</p>
					  	  <p>&nbsp;</p>	  <p>&nbsp;</p>
						  	  <p>&nbsp;</p>
							  	  <p>&nbsp;</p>
								  	  <p>&nbsp;</p>
									  	  <p>&nbsp;</p>
										  	  <p>&nbsp;</p>
											  	  <p>&nbsp;</p>	  <p>&nbsp;</p>
												  	  <p>&nbsp;</p>
													  	  <p>&nbsp;</p>	  <p>&nbsp;</p>
														  
										

  <table width="100%" height="16%" border="1" cellpadding="0" cellspacing="0" >
      <tr>
        <td width="100%" height="21" colspan="4" align="center"><font size=3 ><strong>Reporte Notas de Credito</strong></font></td>
      </tr>
      <tr>
        <td height="14" colspan="4" align="center">Fecha del <font size="2" >'.$fecini.'</font> al <font size="2" >'.$fecfin.'  </font>   Cajero: <font size="2" > '.$cdgcaj.' - '.$nomcomusu.' </font></td>
      </tr>
  </table>
  ';
	
	 $codigoHTML.='
	 
	    	 	 <table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
	<td width="100%" height="11" colspan="4" align="lefr"><font size=3 ><strong> NOTAS CREDITO DOLARES </strong></font></td>
	</tr>
  </table>
  
  
	<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
     
        <td width="30%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Factura</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>
        <td width="75%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Cliente</strong></font></td>
		<td width="10%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Plazo</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub. Exento </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Exonerado </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.NoSujeto </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Gravado </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Subtotal </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Descuento </strong></font></td>
          <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Ventas </strong></font></td>
		  <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Servicio </strong></font></td>
              <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total </strong></font></td>
			  
			  
			  
			  
 
    </tr>
  </table>';
		
		
		

	$consulta= "SELECT * FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda= 'USD'   ".$where_notas ;
		if ($resultado = $mysqli->query($consulta)) {
        $count = 0;
  		while ($dato = $resultado->fetch_assoc()) {
	
				$codnota     = $dato["codnota"];
				$fecha       = $dato["fecha"];
 				$fecha       = implode("-", array_reverse(explode("-", $fecha))); 	
			    $numcomp     = $dato["numcomp"];
				//$numcomp     = substr($numcomp , -10);	
				$cdgcli      = $dato["cdgcli"];
				$cdgcli      = sprintf("%04d",$cdgcli); 
				$dsccli      = $dato["dsccli"];
				$totalnota   = number_format($dato["totalnota"], 2);
				$subexe      = number_format($dato["subexe"], 2);
				$subgra      = number_format($dato["subgra"], 2);
				$subtot      = number_format($dato["subtot"], 2);
				$impven      = number_format($dato["impven"], 2);
				$destot      = number_format($dato["destot"], 2);
				
				$totgravado     = number_format($fila["totgravado"], 2);
				$totexento     = number_format($fila["totexento"], 2);
				$totexonerado     = number_format($fila["totexonerado"], 2);
				$totnosujeto     = number_format($fila["totnosujeto"], 2);
				
		    if($count == 0 )		
                $codigoHTML.='<table width="100%"  rules="none" border="0">';

            $codigoHTML.='    
     
                <tr>
                    <td width="30%">  <font size=1 >'.$numcomp.'</font></td>
                    <td width="19%">  <font size=1 >'.$fecha.'</font></td>
                    <td width="90%">  <font size=1 align="left">'.$dsccli.'</font></td>
                    <td width="10%" align="center">  <font size=1 >'.$pladia.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totexento.'</font></td>
					<td width="20%" align="right">  <font size=1 >'.$totexonerado.'</font></td>
					  <td width="20%" align="right">  <font size=1 >'.$totnosujeto.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totgravado.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$subtot.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$destot.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$impven.'</font></td>
					<td width="20%" align="right">  <font size=1 >'.$otrcar.'</font></td>
                    <td width="20%" align="right">  <font size=1 >'.$totalnota.'</font></td>
                  </tr> ';
   
   
              $count++;
       }
        if($count >= 1) 
            $codigoHTML.='</table>';
 
		  }
		  
		  
		  
		  $consulta= ("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(otrcar) AS imp_servicio, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado,SUM(totgravado) AS  totgravado FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda= 'USD'  ".$where_notas );
		if ($resultado = $mysqli->query($consulta)) 

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

				$exento_not    = $dato["exento"];
				$gravado_not   = $dato["gravado"];
				$subtotal_not  = $dato["subtotal"];
				$descuento_not = $dato["descuento"];
				$impuesto_not  = $dato["impuesto"];
				$imp_servicio_not  = $dato["imp_servicio"];
				$total_not     = $dato["total"];
				
				$totexento_not  = $dato["totexento"];
				$totexonerado_not   = $dato["totexonerado"];
				$totgravado_not   = $dato["totgravado"];
				$totnosujeto_not   = $dato["totnosujeto"];
			
	
			
$codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%" border="0" cellpadding="0" cellspacing="0">
         <tr>
            <td width="135%" height="12" align="left"><font size=2 > TOTAL NOTAS</font></td>
               <td width="20%" align="right"><font size=1 >'.number_format($totexento_not, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totexonerado_not, 2).'</font></td>
			   	<td width="20%" align="right"><font size=1 >'.number_format($totnosujeto_not, 2).'</font></td>
		       <td width="20%" align="right"><font size=1 >'.number_format($totgravado_not, 2).'</font></td>
		       <td width="20%" align="right"><font size=1 >'.number_format($subtotal_not, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($descuento_not, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($impuesto_not, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($imp_servicio_not, 2).'</font></td>
               <td width="20%" align="right"><font size=1 >'.number_format($total_not, 2).'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>
   
  
   <p>';
   
      } 
	
	
	
	 
	 /////////////////////////////////////////////////////////////////  CALCULA TARIFAS NOTAS ///////////////////////////////////////

$consulta3= ("SELECT  codnota FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda= 'USD'  ".$where_notas  );
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R = 0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	if ($resultado3 = $mysqli->query($consulta3)) 


  	 while ($dato3 = $resultado3->fetch_assoc()) {			
	 $codnota   = $dato3['codnota'];

    $consulta4= ("SELECT codnota, impuesto, cdgtar, cdgimp FROM factulinea_notas WHERE  codnota = '$codnota' AND cdgimp = '01' ");

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

	 
  	 while ($dato4 = $resultado4->fetch_assoc()) {
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;

				$impuesto    = $dato4['impuesto'];
			  	$cdgtar      = $dato4['cdgtar'];
		
	     if ($cdgtar == "01") {	$I0R =  $impuesto;
	}elseif ($cdgtar == "02") { $I1R =  $impuesto;
	}elseif ($cdgtar == "03") { $I2R =  $impuesto;
	}elseif ($cdgtar == "04") { $I4R =  $impuesto;
	}elseif ($cdgtar == "05") { $I0T =  $impuesto;
	}elseif ($cdgtar == "06") { $I4T =  $impuesto;
	}elseif ($cdgtar == "07") { $I8T =  $impuesto;
	}elseif ($cdgtar == "08") { $I13 =  $impuesto;
	} 
	
	  $TOTAL_I0R =  $TOTAL_I0R + $I0R ;
	  $TOTAL_I1R =  $TOTAL_I1R + $I1R ;
	  $TOTAL_I2R =  $TOTAL_I2R + $I2R ;
	  $TOTAL_I4R =  $TOTAL_I4R + $I4R ;
	  $TOTAL_I0T =  $TOTAL_I0T + $I0T ;
	  $TOTAL_I4T =  $TOTAL_I4T + $I4T ;
	  $TOTAL_I8T =  $TOTAL_I8T + $I8T ;
	  $TOTAL_I13 =  $TOTAL_I13 + $I13 ;
	
		
	 } 		
	


	 } 		


	  
   $codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%"  cellpadding="0" cellspacing="0" border="1" >
         <tr>
           <td colspan="4"  height="12" align="left">&nbsp;</td>
           <td colspan="4"  bgcolor="#CCCCCC" align="CENTER">REDUCIDAS</td>
           <td colspan="3" bgcolor="#CCCCCC"  align="CENTER">TRANSITORIAS</td>
           <td align="CENTER" bgcolor="#CCCCCC" >GENERAL</td>
         </tr>
         <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > </font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >1%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >2%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >8%</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >13%</font></strong></td>
         </tr>

		 
		 <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > TOTAL IMPUESTOS</font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I1R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I2R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0T, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4T, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I8T, 2).'</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I13, 2).'</font></strong></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
   
      
   /*****************************NOTAS COLONES*******************************************************/
   $codigoHTML.='
   	 	 <table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
	<td width="100%" height="11" colspan="3" align="lefr"><font size=3 ><strong> NOTAS CREDITO COLONES</strong></font></td>
	</tr>
  </table>
	  
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
    
        <td width="30%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Factura</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>
        <td width="75%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Cliente</strong></font></td>
		<td width="10%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Plazo</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub. Exento </strong></font></td>
		 <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Exonerado </strong></font></td>
		 		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.NoSujeto </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Gravado </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Subtotal </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Descuento </strong></font></td>
          <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Ventas </strong></font></td>
		  <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Imp.Servicio </strong></font></td>
              <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total </strong></font></td>
 
    </tr>
  </table>';
		
		

	$consulta= "SELECT * FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin'  AND tipmoneda!= 'USD'  ".$where_notas ;
		if ($resultado = $mysqli->query($consulta)) 
        $count = 0;
  		while ($dato = $resultado->fetch_assoc()) {
	
				$codnota     = $dato["codnota"];
				$fecha       = $dato["fecha"];
 				$fecha       = implode("-", array_reverse(explode("-", $fecha))); 	
			    $numcomp     = $dato["numcomp"];
				//$numcomp     = substr($numcomp , -10);	
				$cdgcli      = $dato["cdgcli"];
				$cdgcli      = sprintf("%04d",$cdgcli); 
				$dsccli      = $dato["dsccli"];
				$totalnota   = number_format($dato["totalnota"], 2);
				$subexe      = number_format($dato["subexe"], 2);
				$subgra      = number_format($dato["subgra"], 2);
				$subtot      = number_format($dato["subtot"], 2);
				$impven      = number_format($dato["impven"], 2);
				$otrcar      = number_format($dato["otrcar"], 2);
				$destot      = number_format($dato["destot"], 2);
				
				$totgravado     = number_format($fila["totgravado"], 2);
				$totexento     = number_format($fila["totexento"], 2);
				$totexonerado     = number_format($fila["totexonerado"], 2);
				$totnosujeto     = number_format($fila["totnosujeto"], 2);
				
		    if($count == 0 )		
                $codigoHTML.='<table width="100%"  rules="none" border="0">';

            $codigoHTML.='    
      <tr>
        <td width="18%">  <font size=1 >'.$numcomp.'</font></td>
        <td width="10%">  <font size=1 >'.$fecha.'</font></td>
   	  
   	    <td width="35%">  <font size=1 align="left">'.$dsccli.'</font></td>
		<td width="10%" align="center">  <font size=1 >'.$pladia.'</font></td>
        <td width="10%" align="right">  <font size=1 >'.$totexento.'</font></td>
		<td width="10%" align="right">  <font size=1 >'.$totexonerado.'</font></td>
		<td width="11%" align="right">  <font size=1 >'.$totnosujeto.'</font></td>
		<td width="11%" align="right">  <font size=1 >'.$totgravado.'</font></td>
	    <td width="10%" align="right">  <font size=1 >'.$subtot.'</font></td>
		<td width="10%" align="right">  <font size=1 >'.$destot.'</font></td>
		 <td width="11%" align="right">  <font size=1 >'.$impven.'</font></td>
		  <td width="11%" align="right">  <font size=1 >'.$otrcar.'</font></td>
		<td width="10%" align="right">  <font size=1 >'.$totalnota.'</font></td>
   </tr> ';
              $count++;
       }
        if($count >= 1) 
            $codigoHTML.='</table>';



			 
$consulta= ("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(otrcar) AS imp_servicio, SUM(totexento) AS  totexento, SUM(totexonerado) AS  totexonerado,SUM(totgravado) AS  totgravado FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin'  AND tipmoneda!= 'USD' ".$where_notas );
		if ($resultado = $mysqli->query($consulta)) 

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

				$exento_not    = $dato["exento"];
				$gravado_not   = $dato["gravado"];
				$subtotal_not  = $dato["subtotal"];
				$descuento_not = $dato["descuento"];
				$impuesto_not  = $dato["impuesto"];
				$imp_servicio_not  = $dato["imp_servicio"];
				$total_not     = $dato["total"];
				
				$totexento_not  = $dato["totexento"];
				$totexonerado_not   = $dato["totexonerado"];
				$totgravado_not   = $dato["totgravado"];
				$totnosujeto_not   = $dato["totnosujeto"];
			
	
			
$codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%" border="0" cellpadding="0" cellspacing="0">
         <tr>
            <td width="145%" height="12" align="left"><font size=2 > TOTAL NOTAS</font></td>
               <td width="20%" align="right"><font size=1 >'.number_format($totexento_not, 2).'</font></td>
			   <td width="20%" align="right"><font size=1 >'.number_format($totexonerado_not, 2).'</font></td>
			   	 <td width="20%" align="right"><font size=1 >'.number_format($totnosujeto_not, 2).'</font></td>
		       <td width="20%" align="right"><font size=1 >'.number_format($totgravado_not, 2).'</font></td>
		      <td width="20%" align="right"><font size=1 >'.number_format($subtotal_not, 2).'</font></td>

			  <td width="20%" align="right"><font size=1 >'.number_format($descuento_not, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($impuesto_not, 2).'</font></td>
			 <td width="20%" align="right"><font size=1 >'.number_format($imp_servicio_not, 2).'</font></td>
              <td width="20%" align="right"><font size=1 >'.number_format($total_not, 2).'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
      } 
	 
	 
	 
	 /////////////////////////////////////////////////////////////////  CALCULA TARIFAS NOTAS ///////////////////////////////////////

$consulta3= ("SELECT  codnota FROM facturas_notas WHERE  fecha  between '$fechainicio' AND '$fechafin' AND tipmoneda!= 'USD'  ".$where_notas  );
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R = 0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	if ($resultado3 = $mysqli->query($consulta3)) 


  	 while ($dato3 = $resultado3->fetch_assoc()) {			
	 $codnota   = $dato3['codnota'];

    $consulta4= ("SELECT codnota, impuesto, cdgtar, cdgimp FROM factulinea_notas WHERE  codnota = '$codnota' AND cdgimp = '01' ");

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

	 
  	 while ($dato4 = $resultado4->fetch_assoc()) {
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;

				$impuesto    = $dato4['impuesto'];
			  	$cdgtar      = $dato4['cdgtar'];
		
	     if ($cdgtar == "01") {	$I0R =  $impuesto;
	}elseif ($cdgtar == "02") { $I1R =  $impuesto;
	}elseif ($cdgtar == "03") { $I2R =  $impuesto;
	}elseif ($cdgtar == "04") { $I4R =  $impuesto;
	}elseif ($cdgtar == "05") { $I0T =  $impuesto;
	}elseif ($cdgtar == "06") { $I4T =  $impuesto;
	}elseif ($cdgtar == "07") { $I8T =  $impuesto;
	}elseif ($cdgtar == "08") { $I13 =  $impuesto;
	} 
	
	  $TOTAL_I0R =  $TOTAL_I0R + $I0R ;
	  $TOTAL_I1R =  $TOTAL_I1R + $I1R ;
	  $TOTAL_I2R =  $TOTAL_I2R + $I2R ;
	  $TOTAL_I4R =  $TOTAL_I4R + $I4R ;
	  $TOTAL_I0T =  $TOTAL_I0T + $I0T ;
	  $TOTAL_I4T =  $TOTAL_I4T + $I4T ;
	  $TOTAL_I8T =  $TOTAL_I8T + $I8T ;
	  $TOTAL_I13 =  $TOTAL_I13 + $I13 ;
	
		
	 } 		
	


	 } 		


	  
   $codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%"  cellpadding="0" cellspacing="0" border="1" >
         <tr>
           <td colspan="4"  height="12" align="left">&nbsp;</td>
           <td colspan="4"  bgcolor="#CCCCCC" align="CENTER">REDUCIDAS</td>
           <td colspan="3" bgcolor="#CCCCCC"  align="CENTER">TRANSITORIAS</td>
           <td align="CENTER" bgcolor="#CCCCCC" >GENERAL</td>
         </tr>
         <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > </font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >1%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >2%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >8%</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >13%</font></strong></td>
         </tr>

		 
		 <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > TOTAL IMPUESTOS</font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I1R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I2R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0T, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4T, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I8T, 2).'</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I13, 2).'</font></strong></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>'; 
  


	
$codigoHTML.='


 
</div>
</body>
</html>';

}

$codigoHTML=utf8_decode($codigoHTML);
$dompdf=new DOMPDF();

	 
$dompdf->set_paper( "A4", "landscape" ); // a4,horizontal
$dompdf->load_html($codigoHTML);
ini_set("memory_limit","7000M");
$dompdf->render();

$dompdf->stream("Reporte de Ventas.pdf", array("Attachment" => false));
	

?>
