<?php

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

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

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


	 $fecini    = $_GET["fechainicio"];
	 $fecfin    = $_GET["fechafin"];
	 $cdgfam    = $_GET["Familia"];
	 //$cdglin    = $_GET["Linea"];
	 $cdgpro    = $_GET["cdgpro"];
	 

	$horaini      =$_GET["horaini"];
	$horafin      =$_GET["horafin"];
	
	
	
	$horaini2      =$_GET["horaini"];
	$horafin2    =$_GET["horafin"];

/**********************HORA DE INICIO******************************/
if  ( $horaini<> "" ){

			$hora_ini = substr($horaini, 0, -5);// se le quitan los ultimos 5 caracteres (04:45AM)
			$minutos_ini = substr($horaini, 2, -2);// 
			$minutos_ini = str_replace ( ":", '', $minutos_ini); // le quito los dos puntos a los minutos y los agrego al final
			$tarde_ini  = substr($horaini,-2);//  devuelve am o pm

		if ($tarde_ini=="pm"){
		
				if  ($hora_ini=='1'){$hora_ini=13;}
			else if ($hora_ini=='2'){ $hora_ini=14;}
			else if ($hora_ini=='3'){$hora_ini=15;}
			else if ($hora_ini=='4'){$hora_ini=16;}
			else if ($hora_ini=='5'){$hora_ini=17;}
			else if ($hora_ini=='6'){$hora_ini=18;}
			else if ($hora_ini=='7'){$hora_ini=19;}
			else if ($hora_ini=='8'){$hora_ini=20;}
			else if ($hora_ini=='9'){$hora_ini=21;}
			else if ($hora_ini=='10'){$hora_ini=22;}
			else if ($hora_ini=='11'){$hora_ini=23;}
			else if ($hora_ini=='12'){$hora_ini=12;}

	}
	
$horaini= $hora_ini.':'.$minutos_ini;


}
/**********************HORA DE FIN******************************/
if  ( $horafin<> "" ){

			$hora_fin = substr($horafin, 0, -5);// se le quitan los ultimos 5 caracteres (04:45AM)
			$minutos_fin = substr($horafin, 2, -2);// 
			$minutos_fin = str_replace ( ":", '', $minutos_fin); // le quito los dos puntos a los minutos y los agrego al final
			$tarde_fin  = substr($horafin,-2);//  devuelve am o pm

		if ($tarde_fin=="pm"){
		
				if  ($hora_fin =='1'){$hora_fin =13;}
			else if ($hora_fin =='2'){ $hora_fin =14;}
			else if ($hora_fin =='3'){$hora_fin =15;}
			else if ($hora_fin =='4'){$hora_fin =16;}
			else if ($hora_fin =='5'){$hora_fin =17;}
			else if ($hora_fin =='6'){$hora_fin =18;}
			else if ($hora_fin =='7'){$hora_fin =19;}
			else if ($hora_fin =='8'){$hora_fin =20;}
			else if ($hora_fin =='9'){$hora_fin =21;}
			else if ($hora_fin =='10'){$hora_fin =22;}
			else if ($hora_fin =='11'){$hora_fin =23;}
			else if ($hora_fin =='12'){$hora_fin =24;}

	}
	

	
$horafin= $hora_fin .':'.$minutos_fin;



}


/****************************************************/

	 $cdglin    = "";
	 

	 
	 
	$exento_tot    = 0;
	$gravado_tot   = 0;
	$subtotal_tot  = 0;
	$descuento_tot = 0;
	$impuesto_tot  = 0;
	$total_tot     = 0;
    $where_notas   = "";
    $where         = "";
	$exento_fac    = 0;
	$gravado_fac   = 0;
	$subtotal_fac  = 0;
	$descuento_fac = 0;
	$impuesto_fac  = 0;
	$total_fac     = 0;	 
	$exento        = 0  ;
	$gravado       = 0 ;
	$subtotal      = 0 ;
	$descuento     = 0  ;
	$impuesto      = 0 ;
	$total         = 0  ;
	
	
		 	 /////////////////        where para facturas  y notas ********************


 $fechainicio= implode("-", array_reverse(explode("-", $fecini))); 
 $fechafin= implode("-", array_reverse(explode("-", $fecfin))); 



$where="1=1";
if ($cdglin <> "") { $where.=" AND vista_reporte_familias_notas.cdglin='$cdglin'"; }
if ($cdgfam <> "") { $where.=" AND vista_reporte_familias_notas.cdgfam='$cdgfam'"; }
if ($cdgpro<> "")  { $where.=" AND vista_reporte_familias_notas.cdgpro='$cdgpro'"; }

if (($fechainicio<>"") and ($fechafin<>"")) {
	 $where.=" AND vista_reporte_familias_notas.fecfac between '".$fechainicio."' AND '".$fechafin."'";
} else {
	if ($fechainicio<>"") {
	 	$where.=" AND vista_reporte_familias_notas.fecfac >='".$fechainicio."'";
	} else {
		if ($fechafin<>"") {
		$where.=" AND vista_reporte_familias_notas.fecfac <='".$fechafin."'";
		}
	}
}



if (($horaini<>"") and ($horafin<>"")) {
	 $where.=" AND vista_reporte_familias_notas.horfac between '".$horaini."' AND '".$horafin."'";
} else {
	if ($horaini<>"") {
	 	$where.=" AND vista_reporte_familias_notas.horfac>='".$horaini."'";
	} else {
		if ($horafin<>"") {
		$where.=" AND vista_reporte_familias_notas.horfac<='".$horafin."'";
		}
	}
}





 if ($cdgpro=="") {
	 
	 $nomcli="TODOS LOS CLIENTES";
	 
	     } else {


	   $query_codcaj="SELECT dscpro FROM proveedores  WHERE cdgpro=$cdgpro ";
       $resultado = $mysqli->query( $query_codcaj);
       $row = $resultado->fetch_array();
        
		$nomcli   = $row["dscpro"];
	
     }
	 
	 
	 
 if ($cdgfam=="") {
	 
	 $nomcomusu="TODAS LAS FAMILIAS";
	 
	     } else {


	   $query_codcaj="SELECT * FROM familias  WHERE cdgfam=$cdgfam ";
       $resultado = $mysqli->query( $query_codcaj);
       $row = $resultado->fetch_array();
        
		$nomcomusu    = $row["dscfam"];
     }		
		
		
		
		
		
$totalfacturas=0;
  
  if ($result = $mysqli->query("SELECT * FROM vista_reporte_familias_notas WHERE  borrado=0  AND tipmoneda = 'CRC' ".$where)) {
   $filas = $result->num_rows;
   $result->close();
}

					
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Reporte Detallado de Ventas por Familia</title>
<style type="text/css">
<!--
.Estilo1 {color: #CCCCCC}
.Estilo4 {font-size: medium}
.Estilo6 {
	color: #000000;
	font-weight: bold;
}
.Estilo17 {color: #000000; font-weight: bold; font-size: 14px; }
-->
</style>
</head>
<body>
<table width="92%" border="0" cellspacing="0" cellpadding="0">
    
    <tr>
      <td><div align="center"></div></td>
      <td colspan="13"  ><div align="center"><font size=8 ><?php echo $NombreComercial?></font></div>
      <div align="center"></div><div align="center"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="13" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="13" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="13" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="13" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="13" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="13"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Detallado de Notas por CLIENTES</font> en COLONES </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="13"   border="1"><div align="center"><font size="3" >Fecha del <?php echo $fecini; ?> Al <?php echo $fecfin; ?> -  <?php echo $nomcomusu;?> Desde <?php echo $horaini2; ?> Hasta <?php echo $horafin2; ?> - Cliente <?php echo $nomcli; ?></font></div></td>
     </tr>
     	

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

    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>CODIGO CLIENTE</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>CLIENTE</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>FECHA</strong></font></span></td>
    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>CODIGO ARTICULO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>ARTICULO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>CANTIDAD</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUB. EXENTO</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUB. GRAVADO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUBTOTAL</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue"><span class="Estilo4"><strong>DESCUENTO</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>IMPUESTO</strong></font></span></td> 
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>TOTAL</strong></font></span></td>
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>TARIFA %</strong></font></span></td>
  </tr>
  
<?PHP
		

				$cantidadfam=0; 
				$importefam=0;
				$mondesfam=0;
				$impuestofam=0;
				$totalfam=0;
				$subexefam=0;
				$subgrafam=0;
				


				  /*Impuestos TOTALES*/
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R =  0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	  	   /*Subtotales TOTALES*/
	  $SUBTOTAL_I0R =  0 ;
	  $SUBTOTAL_I1R =  0 ;
	  $SUBTOTAL_I2R =  0 ;
	  $SUBTOTAL_I4R =  0 ;
	  $SUBTOTAL_I0T =  0 ;
	  $SUBTOTAL_I4T =  0 ;
	  $SUBTOTAL_I8T =  0 ;
	  $SUBTOTAL_I13 =  0;
		/*	

		*/
							
 $consulta2="SELECT  tipmoneda, mondes,  importe,  impuesto,  cantidad, vista_reporte_familias_notas.cdgfam, vista_reporte_familias_notas.borrado, vista_reporte_familias_notas.codigo, web_1001.dscart, vista_reporte_familias_notas.fecfac ,vista_reporte_familias_notas.cdgtar , vista_reporte_familias_notas.tarimp , familias.dscfam , vista_reporte_familias_notas.cdgpro , proveedores.dscpro , fecfac FROM vista_reporte_familias_notas,familias, web_1001, proveedores WHERE  web_1001.cdgbar=vista_reporte_familias_notas.codigo  AND vista_reporte_familias_notas.cdgpro=proveedores.cdgpro  AND vista_reporte_familias_notas.cdgfam=familias.cdgfam  AND vista_reporte_familias_notas.borrado=0  AND tipmoneda = 'CRC' AND ".$where ;
 
 		/*Impuestos*/
	  $IMP_I0R =  0 ;
	  $IMP_I1R =  0 ;
	  $IMP_I2R =  0 ;
	  $IMP_I4R =  0 ;
	  $IMP_I0T =  0 ;
	  $IMP_I4T =  0 ;
	  $IMP_I8T =  0 ;
	  $IMP_I13 =  0;
	  
	   /*Subtotales*/
	  $SUB_TOTAL_I0R =  0 ;
	  $SUB_TOTAL_I1R =  0 ;
	  $SUB_TOTAL_I2R =  0 ;
	  $SUB_TOTAL_I4R =  0 ;
	  $SUB_TOTAL_I0T =  0 ;
	  $SUB_TOTAL_I4T =  0 ;
	  $SUB_TOTAL_I8T =  0 ;
	  $SUB_TOTAL_I13 =  0;
				  		
		

      
	    $contador=0;	
		if ($resultado2 = $mysqli->query($consulta2)) 
		
		  
  		while ($dato1 = $resultado2->fetch_assoc()) {     //  si encuentra datos
				$fecha      = $dato1['fecfac'];
 				$fecfac= implode("-", array_reverse(explode("-", $fecha))); 	
				$fecha2      = $dato1['fecfac'];
				$fecfac= implode("-", array_reverse(explode("-", $fecha2))); 
				$cdgpro     = $dato1['cdgpro'];
	            $dscpro     = $dato1['dscpro'];
				$fecfac     = $dato1['fecfac'];
				$codigo     = $dato1['codigo'];
	            $cdgfam     = $dato1['cdgfam'];
				$dscfam     = $dato1['dscfam'];
		     	$dscart     = $dato1['dscart'];
				$cdgtar     = $dato1['cdgtar'];
			    $tarimp     = $dato1['tarimp'];
				$cantidad   = $dato1['cantidad'];
				$dscart     = substr($dscart, 0, 80);	
				
				$cantidad   = $dato1['cantidad'];
				$importe    = $dato1['importe'];
				$mondes     = $dato1['mondes'];
				$impuesto   = $dato1['impuesto'];
				$SubTotal   = $importe - $mondes ;
						
				
				$total=$importe + $impuesto - $mondes ;
			
				$cantidadfam=$cantidadfam+$cantidad; 
				$importefam=$importefam+$importe;
				$mondesfam=$mondesfam+$mondes;
				$impuestofam=$impuestofam+$impuesto;
				$totalfam=$importefam-$mondesfam+$impuestofam;

	   $subexe =  0;
	   $subgra =  0;
	  
	  	if ($impuesto > 0) {  
		
		$subgra =  $importe;

		
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;
	  /*Subtotales*/
	 $SUB0R =  0;
	 $SUB1R =  0;
	 $SUB2R =  0;
	 $SUB4R =  0;
	 $SUB0T =  0;
	 $SUB4T =  0;
	 $SUB8T =  0;
	 $SUB13 =  0;
					
					
						
	
						     if ($cdgtar == "01") { $I0R =  $impuesto; $SUB0R = $SubTotal;
						}elseif ($cdgtar == "02") { $I1R =  $impuesto; $SUB1R = $SubTotal;
						}elseif ($cdgtar == "03") { $I2R =  $impuesto; $SUB2R = $SubTotal;
						}elseif ($cdgtar == "04") { $I4R =  $impuesto; $SUB4R = $SubTotal;
						}elseif ($cdgtar == "05") { $I0T =  $impuesto; $SUB0T = $SubTotal;
						}elseif ($cdgtar == "06") { $I4T =  $impuesto; $SUB4T = $SubTotal;
						}elseif ($cdgtar == "07") { $I8T =  $impuesto; $SUB8T = $SubTotal;
						}elseif ($cdgtar == "08") { $I13 =  $impuesto; $SUB13 = $SubTotal;
						}
						
						   /*Impuestos*/
						  $IMP_I0R =  $IMP_I0R + $I0R ;
						  $IMP_I1R =  $IMP_I1R + $I1R ;
						  $IMP_I2R =  $IMP_I2R + $I2R ;
						  $IMP_I4R =  $IMP_I4R + $I4R ;
						  $IMP_I0T =  $IMP_I0T + $I0T ;
						  $IMP_I4T =  $IMP_I4T + $I4T ;
						  $IMP_I8T =  $IMP_I8T + $I8T ;
						  $IMP_I13 =  $IMP_I13 + $I13 ;	 
					
							   /*Subtotales*/
						  $SUB_TOTAL_I0R =  $SUB_TOTAL_I0R + $SUB0R;
						  $SUB_TOTAL_I1R =  $SUB_TOTAL_I1R + $SUB1R;
						  $SUB_TOTAL_I2R =  $SUB_TOTAL_I2R + $SUB2R;
						  $SUB_TOTAL_I4R =  $SUB_TOTAL_I4R + $SUB4R;
						  $SUB_TOTAL_I0T =  $SUB_TOTAL_I0T + $SUB0T;
						  $SUB_TOTAL_I4T =  $SUB_TOTAL_I4T + $SUB4T;
						  $SUB_TOTAL_I8T =  $SUB_TOTAL_I8T + $SUB8T;
						  $SUB_TOTAL_I13 =  $SUB_TOTAL_I13 + $SUB13;
						  
						  
						  
						  		   /*Impuestos TOTALES*/
						  $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 ;	 
					
							   /*Subtotales TOTALES*/
						  $SUBTOTAL_I0R =  $SUBTOTAL_I0R + $SUB0R;
						  $SUBTOTAL_I1R =  $SUBTOTAL_I1R + $SUB1R;
						  $SUBTOTAL_I2R =  $SUBTOTAL_I2R + $SUB2R;
						  $SUBTOTAL_I4R =  $SUBTOTAL_I4R + $SUB4R;
						  $SUBTOTAL_I0T =  $SUBTOTAL_I0T + $SUB0T;
						  $SUBTOTAL_I4T =  $SUBTOTAL_I4T + $SUB4T;
						  $SUBTOTAL_I8T =  $SUBTOTAL_I8T + $SUB8T;
						  $SUBTOTAL_I13 =  $SUBTOTAL_I13 + $SUB13;
		 
		 	} else {
			
					$subexe =  $importe;
			}			
						
			$subexefam=$subexefam+$subexe;
			$subgrafam=$subgrafam+$subgra;
				

   
 ?> 
  
  <tr>
   <td></td>
      <td align="left"><font size=1 ><?php echo $dato1["cdgpro"] ?></font></td>
   	<?php	  $dscpro = substr($dato1["dscpro"], 0,40);  ?>	
   <td align="left"><font size=1 ><?php echo $dscpro ?></font></td>
   <td align="left"><font size=1 ><?php echo $fecfac ?></font></td>
   <td align="left"><font size=1 ><?php echo "cod.".$dato1["codigo"] ?></font></td>
   	<?php	  $dscart = substr($dato1["dscart"], 0,40);  ?>	
   <td align="left"><font size=1 ><?php echo $dscart ?></font></td>
   <td align="center"><font size=1 ><?php echo $dato1["cantidad"] ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($subexe,2)?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($subgra,2)?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($importe,2)?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($mondes,2) ?></font></td>
   <td align="right"><font size=1 ><?php  echo number_format($impuesto,2) ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($total,2) ?></font></td>
  <td align="right"><font size=1 ><?php echo number_format($tarimp,2) ?></font></td>
 </tr>
 
  <?php
      $contador++;
  	
	  } 	    //  cierra codigo de familia distinto 	
		        
	
 ?> 		
  

	<?php	 
	/*
     $consulta2= ("SELECT  SUM(importe) AS importe, SUM(mondes) AS mondes , SUM(impuesto) AS impuesto ,  SUM(cantidad) AS cantidad, factulinea.cdgfam, familias.cdgfam FROM factulinea, familias WHERE borrado=0 AND  factulinea.cdgfam = familias.cdgfam  AND ".$where );
	 
	 */
	    $consulta3= ("SELECT  SUM(importe) AS importe, SUM(mondes) AS mondes , SUM(impuesto) AS impuesto ,  SUM(cantidad) AS cantidad, vista_reporte_familias_notas.cdgfam, familias.cdgfam FROM vista_reporte_familias_notas, familias WHERE borrado=0 AND  vista_reporte_familias_notas.cdgfam = familias.cdgfam  AND tipmoneda = 'CRC' AND  ".$where );
		
		


	if ($resultado3 = $mysqli->query($consulta3)) 

  	 while ($dato = $resultado3->fetch_assoc()) {
  						
        $importe   = $dato["importe"];
        $mondes    = $dato["mondes"];
        $impuesto  = $dato["impuesto"];
		$cantidad  = $dato["cantidad"];
					
        $total=$importe - $mondes + $impuesto;
	

		$cantidad = number_format($cantidad,3);
	
	} 				
  
  ?>
  
</table>

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

 <tr>
   <td></td>
   <td align="left">&nbsp;</td>
   <td align="center">&nbsp;</td>
   <td align="left">&nbsp;</td>
   <td align="center">&nbsp;</td>
   <td align="center">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
 </tr>
 <tr>

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

      <td align="center"><strong><font size=1 ></font></strong></td>
   <td align="left"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($importe , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($mondes, 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($impuesto , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($total , 2); ?></font></strong></td>
 </tr>
</table>

<?php



  if ($result2 = $mysqli->query("SELECT * FROM vista_reporte_familias_notas WHERE  borrado=0  AND tipmoneda = 'USD' AND ".$where)) {
   $filas2 = $result2->num_rows;
   $result2->close();
}
     /////////////////////////////////////////////////////////   si tienes facturas en dolares ////////////////////////////////////////////////////////////////////////////

if($filas2 > 0 ) {

?>
<table width="92%" border="0" cellspacing="0" cellpadding="0">
    
    <tr>
      <td><div align="center"></div></td>
      <td colspan="13"  ><div align="center"><font size=8 ><?php echo $NombreComercial?></font></div>
      <div align="center"></div><div align="center"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="13" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="13" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="13" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="13" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="13" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="13"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Detallado de NOTAS por CLIENTES</font> en Dolares </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="13"   border="1"><div align="center"><font size="3" >Fecha del <?php echo $fecini; ?> Al <?php echo $fecfin; ?> -  <?php echo $nomcomusu;?> Desde <?php echo $horaini2; ?> Hasta <?php echo $horafin2; ?> - Cliente <?php echo $nomcli; ?></font></div></td>
     </tr>
     	

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

    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>CODIGO CLIENTE</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>CLIENTE</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>FECHA</strong></font></span></td>
    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>CODIGO ARTICULO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>ARTICULO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>CANTIDAD</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUB. EXENTO</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUB. GRAVADO</strong></font></span></td>
    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>SUBTOTAL</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue"><span class="Estilo4"><strong>DESCUENTO</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>IMPUESTO</strong></font></span></td> 
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>TOTAL</strong></font></span></td>
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>TARIFA %</strong></font></span></td>
  </tr>
  
<?PHP
		

				$cantidadfam=0; 
				$importefam=0;
				$mondesfam=0;
				$impuestofam=0;
				$totalfam=0;
				$subexefam=0;
				$subgrafam=0;
				


				  /*Impuestos TOTALES*/
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R =  0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	  	   /*Subtotales TOTALES*/
	  $SUBTOTAL_I0R =  0 ;
	  $SUBTOTAL_I1R =  0 ;
	  $SUBTOTAL_I2R =  0 ;
	  $SUBTOTAL_I4R =  0 ;
	  $SUBTOTAL_I0T =  0 ;
	  $SUBTOTAL_I4T =  0 ;
	  $SUBTOTAL_I8T =  0 ;
	  $SUBTOTAL_I13 =  0;
		/*	

		*/
							
 $consulta2="SELECT  tipmoneda, mondes,  importe,  impuesto,  cantidad, vista_reporte_familias_notas.cdgfam, vista_reporte_familias_notas.borrado, vista_reporte_familias_notas.codigo, web_1001.dscart, vista_reporte_familias_notas.fecfac ,vista_reporte_familias_notas.cdgtar , vista_reporte_familias_notas.tarimp , familias.dscfam , vista_reporte_familias_notas.cdgpro , proveedores.dscpro , fecfac FROM vista_reporte_familias_notas,familias, web_1001, proveedores WHERE  web_1001.cdgbar=vista_reporte_familias_notas.codigo  AND vista_reporte_familias_notas.cdgpro=proveedores.cdgpro  AND vista_reporte_familias_notas.cdgfam=familias.cdgfam  AND vista_reporte_familias_notas.borrado=0  AND tipmoneda = 'USD' AND ".$where ;
 
 		/*Impuestos*/
	  $IMP_I0R =  0 ;
	  $IMP_I1R =  0 ;
	  $IMP_I2R =  0 ;
	  $IMP_I4R =  0 ;
	  $IMP_I0T =  0 ;
	  $IMP_I4T =  0 ;
	  $IMP_I8T =  0 ;
	  $IMP_I13 =  0;
	  
	   /*Subtotales*/
	  $SUB_TOTAL_I0R =  0 ;
	  $SUB_TOTAL_I1R =  0 ;
	  $SUB_TOTAL_I2R =  0 ;
	  $SUB_TOTAL_I4R =  0 ;
	  $SUB_TOTAL_I0T =  0 ;
	  $SUB_TOTAL_I4T =  0 ;
	  $SUB_TOTAL_I8T =  0 ;
	  $SUB_TOTAL_I13 =  0;
				  		
		

      
	    $contador=0;	
		if ($resultado2 = $mysqli->query($consulta2)) 
		
		  
  		while ($dato1 = $resultado2->fetch_assoc()) {     //  si encuentra datos
				$fecha      = $dato1['fecfac'];
 				$fecfac= implode("-", array_reverse(explode("-", $fecha))); 	
				$fecha2      = $dato1['fecfac'];
				$fecfac= implode("-", array_reverse(explode("-", $fecha2))); 
				$cdgpro     = $dato1['cdgpro'];
	            $dscpro     = $dato1['dscpro'];
				$fecfac     = $dato1['fecfac'];
				$codigo     = $dato1['codigo'];
	            $cdgfam     = $dato1['cdgfam'];
				$dscfam     = $dato1['dscfam'];
		     	$dscart     = $dato1['dscart'];
				$cdgtar     = $dato1['cdgtar'];
			    $tarimp     = $dato1['tarimp'];
				$cantidad   = $dato1['cantidad'];
				$dscart     = substr($dscart, 0, 80);	
				
				$cantidad   = $dato1['cantidad'];
				$importe    = $dato1['importe'];
				$mondes     = $dato1['mondes'];
				$impuesto   = $dato1['impuesto'];
				$SubTotal   = $importe - $mondes ;
						
				
				$total=$importe + $impuesto - $mondes ;
			
				$cantidadfam=$cantidadfam+$cantidad; 
				$importefam=$importefam+$importe;
				$mondesfam=$mondesfam+$mondes;
				$impuestofam=$impuestofam+$impuesto;
				$totalfam=$importefam-$mondesfam+$impuestofam;

	   $subexe =  0;
	   $subgra =  0;
	  
	  	if ($impuesto > 0) {  
		
		$subgra =  $importe;

		
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;
	  /*Subtotales*/
	 $SUB0R =  0;
	 $SUB1R =  0;
	 $SUB2R =  0;
	 $SUB4R =  0;
	 $SUB0T =  0;
	 $SUB4T =  0;
	 $SUB8T =  0;
	 $SUB13 =  0;
					
					
						
	
						     if ($cdgtar == "01") { $I0R =  $impuesto; $SUB0R = $SubTotal;
						}elseif ($cdgtar == "02") { $I1R =  $impuesto; $SUB1R = $SubTotal;
						}elseif ($cdgtar == "03") { $I2R =  $impuesto; $SUB2R = $SubTotal;
						}elseif ($cdgtar == "04") { $I4R =  $impuesto; $SUB4R = $SubTotal;
						}elseif ($cdgtar == "05") { $I0T =  $impuesto; $SUB0T = $SubTotal;
						}elseif ($cdgtar == "06") { $I4T =  $impuesto; $SUB4T = $SubTotal;
						}elseif ($cdgtar == "07") { $I8T =  $impuesto; $SUB8T = $SubTotal;
						}elseif ($cdgtar == "08") { $I13 =  $impuesto; $SUB13 = $SubTotal;
						}
						
						   /*Impuestos*/
						  $IMP_I0R =  $IMP_I0R + $I0R ;
						  $IMP_I1R =  $IMP_I1R + $I1R ;
						  $IMP_I2R =  $IMP_I2R + $I2R ;
						  $IMP_I4R =  $IMP_I4R + $I4R ;
						  $IMP_I0T =  $IMP_I0T + $I0T ;
						  $IMP_I4T =  $IMP_I4T + $I4T ;
						  $IMP_I8T =  $IMP_I8T + $I8T ;
						  $IMP_I13 =  $IMP_I13 + $I13 ;	 
					
							   /*Subtotales*/
						  $SUB_TOTAL_I0R =  $SUB_TOTAL_I0R + $SUB0R;
						  $SUB_TOTAL_I1R =  $SUB_TOTAL_I1R + $SUB1R;
						  $SUB_TOTAL_I2R =  $SUB_TOTAL_I2R + $SUB2R;
						  $SUB_TOTAL_I4R =  $SUB_TOTAL_I4R + $SUB4R;
						  $SUB_TOTAL_I0T =  $SUB_TOTAL_I0T + $SUB0T;
						  $SUB_TOTAL_I4T =  $SUB_TOTAL_I4T + $SUB4T;
						  $SUB_TOTAL_I8T =  $SUB_TOTAL_I8T + $SUB8T;
						  $SUB_TOTAL_I13 =  $SUB_TOTAL_I13 + $SUB13;
						  
						  
						  
						  		   /*Impuestos TOTALES*/
						  $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 ;	 
					
							   /*Subtotales TOTALES*/
						  $SUBTOTAL_I0R =  $SUBTOTAL_I0R + $SUB0R;
						  $SUBTOTAL_I1R =  $SUBTOTAL_I1R + $SUB1R;
						  $SUBTOTAL_I2R =  $SUBTOTAL_I2R + $SUB2R;
						  $SUBTOTAL_I4R =  $SUBTOTAL_I4R + $SUB4R;
						  $SUBTOTAL_I0T =  $SUBTOTAL_I0T + $SUB0T;
						  $SUBTOTAL_I4T =  $SUBTOTAL_I4T + $SUB4T;
						  $SUBTOTAL_I8T =  $SUBTOTAL_I8T + $SUB8T;
						  $SUBTOTAL_I13 =  $SUBTOTAL_I13 + $SUB13;
		 
		 	} else {
			
					$subexe =  $importe;
			}			
						
			$subexefam=$subexefam+$subexe;
			$subgrafam=$subgrafam+$subgra;
				

   
 ?> 
  
  <tr>
   <td></td>
      <td align="left"><font size=1 ><?php echo $dato1["cdgpro"] ?></font></td>
   	<?php	  $dscpro = substr($dato1["dscpro"], 0,40);  ?>	
   <td align="left"><font size=1 ><?php echo $dscpro ?></font></td>
   <td align="left"><font size=1 ><?php echo $fecfac ?></font></td>
   <td align="left"><font size=1 ><?php echo "cod.".$dato1["codigo"] ?></font></td>
   	<?php	  $dscart = substr($dato1["dscart"], 0,40);  ?>	
   <td align="left"><font size=1 ><?php echo $dscart ?></font></td>
   <td align="center"><font size=1 ><?php echo $dato1["cantidad"] ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($subexe,2)?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($subgra,2)?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($importe,2)?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($mondes,2) ?></font></td>
   <td align="right"><font size=1 ><?php  echo number_format($impuesto,2) ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($total,2) ?></font></td>
  <td align="right"><font size=1 ><?php echo number_format($tarimp,2) ?></font></td>
 </tr>
 
  <?php
      $contador++;
  	
	  } 	    //  cierra codigo de familia distinto 	
		        
	
 ?> 		
  

	<?php	 
	/*
     $consulta2= ("SELECT  SUM(importe) AS importe, SUM(mondes) AS mondes , SUM(impuesto) AS impuesto ,  SUM(cantidad) AS cantidad, factulinea.cdgfam, familias.cdgfam FROM factulinea, familias WHERE borrado=0 AND  factulinea.cdgfam = familias.cdgfam  AND ".$where );
	 
	 */
	    $consulta3= ("SELECT  SUM(importe) AS importe, SUM(mondes) AS mondes , SUM(impuesto) AS impuesto ,  SUM(cantidad) AS cantidad, vista_reporte_familias_notas.cdgfam, familias.cdgfam FROM vista_reporte_familias_notas, familias WHERE borrado=0 AND  vista_reporte_familias_notas.cdgfam = familias.cdgfam  AND tipmoneda = 'USD' AND  ".$where );
		
		


	if ($resultado3 = $mysqli->query($consulta3)) 

  	 while ($dato = $resultado3->fetch_assoc()) {
  						
        $importe   = $dato["importe"];
        $mondes    = $dato["mondes"];
        $impuesto  = $dato["impuesto"];
		$cantidad  = $dato["cantidad"];
					
        $total=$importe - $mondes + $impuesto;
	

		$cantidad = number_format($cantidad,3);
	
	} 				
  
  ?>
  
</table>

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

 <tr>
   <td></td>
   <td align="left">&nbsp;</td>
   <td align="center">&nbsp;</td>
   <td align="left">&nbsp;</td>
   <td align="center">&nbsp;</td>
   <td align="center">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
 </tr>
 <tr>

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

      <td align="center"><strong><font size=1 ></font></strong></td>
   <td align="left"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($importe , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($mondes, 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($impuesto , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($total , 2); ?></font></strong></td>
 </tr>
</table>

<?php

}
    

  ?>





</body>
</html>