<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte_Facturacion_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"];
	 $cdgcli    = $_GET["cdgcli"];
	 

	$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
		echo	$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;}

	}
	
			if ($tarde_ini=="am"){
		
				if  ($hora_ini=='1'){$hora_ini='01';}
			else if ($hora_ini=='2'){$hora_ini='02';}
			else if ($hora_ini=='3'){$hora_ini='03';}
			else if ($hora_ini=='4'){$hora_ini='04';}
			else if ($hora_ini=='5'){$hora_ini='05';}
			else if ($hora_ini=='6'){$hora_ini='06';}
			else if ($hora_ini=='7'){$hora_ini='07';}
			else if ($hora_ini=='8'){$hora_ini='08';}
			else if ($hora_ini=='9'){$hora_ini='09';}
			else if ($hora_ini=='12'){$hora_ini='00';}

	}
	
	
	
$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 =12;}

	}
	
				if ($tarde_fin=="am"){
		
				if  ($hora_fin =='1'){$hora_fin ='01';}
			else if ($hora_fin =='2'){$hora_fin ='02';}
			else if ($hora_fin =='3'){$hora_fin ='03';}
			else if ($hora_fin =='4'){$hora_fin ='04';}
			else if ($hora_fin =='5'){$hora_fin ='05';}
			else if ($hora_fin =='6'){$hora_fin ='06';}
			else if ($hora_fin =='7'){$hora_fin ='07';}
			else if ($hora_fin =='8'){$hora_fin ='08';}
			else if ($hora_fin =='9'){$hora_fin ='09';}
			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 (($fechainicio<>"") and ($fechafin<>"")) {
	 $where.=" AND vista_reporte_familias.fecfac between '".$fechainicio."' AND '".$fechafin."'";
} else {
	if ($fechainicio<>"") {
	 	$where.=" AND vista_reporte_familias.fecfac >='".$fechainicio."'";
	} else {
		if ($fechafin<>"") {
		$where.=" AND vista_reporte_familias.fecfac <='".$fechafin."'";
		}
	}
}



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

 /////////////////////////   FILTRO COMBINADO FECHAS Y HORAS /////////////////////////////////////////////////////////

if (($fechainicio<>"") and ($fechafin<>"") and ($horaini<>"") and ($horafin<>"")) {

 $where="";
 $where="1=1";

$fecinicioHORA = $fechainicio."T".$horaini.":00";

$fecfinHORA = $fechafin."T".$horafin.":00";

$where.=" AND vista_reporte_familias.fechac >='".$fecinicioHORA."' AND vista_reporte_familias.fechac <='".$fecfinHORA."'" ;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if ($cdglin <> "") { $where.=" AND vista_reporte_familias.cdglin='$cdglin'"; }
if ($cdgfam <> "") { $where.=" AND vista_reporte_familias.cdgfam='$cdgfam'"; }
if ($cdgcli<> "") { $where.=" AND vista_reporte_familias.cdgcli='$cdgcli'"; }


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


	   $query_codcaj="SELECT DSCCLI FROM web_0301  WHERE CDGCLI=$cdgcli ";
       $resultado = $mysqli->query( $query_codcaj);
       $row = $resultado->fetch_array();
        
		$nomcli   = $row["DSCCLI"];
	
     }
	 
	 
	 
 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 WHERE  borrado=0  AND ".$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="11"  ><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="11" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="11" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="11" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="11" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="11" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="11"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Detallado de Ventas por Familia</font>  </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="11"   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</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>
	<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>LINEA</strong></font></span></td>
  </tr>
  
<?PHP
		
	//if ($filas > 0) {
// $contador=0;
// $marcaestado=0;	
// $marcaborrado=0;						
//					   					
//				$consulta = "SELECT SUM(mondes) AS mondes, SUM(importe) AS importe, SUM(impuesto) AS impuesto, SUM(cantidad) AS cantidad, factulinea.cdgfam, factulinea.borrado, factulinea.codigo, factulinea.dscart, factulinea.fecfac , familias.dscfam FROM factulinea,familias WHERE  factulinea.cdgfam=familias.cdgfam AND factulinea.borrado=0   AND ".$where. " GROUP BY  factulinea.codigo  ORDER BY  factulinea.cdgfam,  factulinea.dscart ASC " ;
//if ($resultado = $mysqli->query($consulta)) 
//
// 
//    while ($fila = $resultado->fetch_assoc()) {
//        $importe   = $fila["importe"];
//        $mondes    = $fila["mondes"];
//        $impuesto  = $fila["impuesto"];
//
//	    $total = $importe -  $mondes + $impuesto ;
				$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 SUM(mondes) AS mondes, SUM(importe) AS importe, SUM(impuesto) AS impuesto, SUM(cantidad) AS cantidad, factulinea.cdgfam, factulinea.borrado, factulinea.codigo, web_1001.dscart, factulinea.fecfac ,factulinea.cdgtar , factulinea.tarimp , familias.dscfam FROM factulinea,familias, web_1001 WHERE  web_1001.cdgbar=factulinea.codigo AND  factulinea.cdgfam=familias.cdgfam AND factulinea.borrado=0   AND ".$where. " GROUP BY  factulinea.codigo ,  factulinea.tarimp  ORDER BY  factulinea.cdgfam,  factulinea.dscart ASC " ;						
							
		*/
							
 $consulta2="SELECT SUM(mondes) AS mondes, SUM(importe) AS importe, SUM(impuesto) AS impuesto, SUM(cantidad) AS cantidad, vista_reporte_familias.cdgfam, vista_reporte_familias.borrado, vista_reporte_familias.codigo, web_1001.dscart, vista_reporte_familias.fecfac ,vista_reporte_familias.cdgtar , vista_reporte_familias.tarimp, vista_reporte_familias.cdglin , familias.dscfam FROM vista_reporte_familias,familias, web_1001 WHERE  web_1001.cdgbar=vista_reporte_familias.codigo AND  vista_reporte_familias.cdgfam=familias.cdgfam AND vista_reporte_familias.borrado=0   AND ".$where. " GROUP BY  vista_reporte_familias.codigo ,  vista_reporte_familias.tarimp  ORDER BY  vista_reporte_familias.cdgfam,  vista_reporte_familias.dscart ASC " ;
 
 		/*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;
				  		
		
		if ($resultado3 = $mysqli->query($consulta2)) 		
		if($dato = $resultado3->fetch_assoc()){ 
      	$cdgfam_actual = $dato['cdgfam'];

	   } 
		

      
	    $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))); 
				$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);	
				
				$cdglin     = $dato1['cdglin'];
				
			$query_cdglin="SELECT dsclin FROM lineas  WHERE cdgfam=$cdgfam AND cdglin=$cdglin";
			$resultado_cdglin = $mysqli->query($query_cdglin);
			$row_cdglin = $resultado_cdglin->fetch_array();
			$nomlinea    = $row_cdglin["dsclin"];


				if ($cdgfam_actual==$cdgfam) {   // si el codigo de famiia es distinto al siguiente
				
				$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;
				
	
	  } 	    //  cierra codigo de familia distinto 	
	

          if ($contador==0){ 
?>  
 
      <tr>
	     <td></td>
        <td> <strong> <font size=2 ><?php echo $cdgfam." - ".$dscfam ?></font></strong></td>
        <td>  <font size=2 ></font></td>
   	    <td>  <font size=1 align="right"></font></td>
   	    <td>  <font size=1 align="left"></font></td>
		<td align="center">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>


      </tr>


  
  <?php
  
       
		 } 
		 

	if ($cdgfam <>  $cdgfam_actual  ){ 
				
			 $cdgfam_actual = $dato1['cdgfam'];
				 
?>				 
 <tr>
 </tr>
 <tr>
   <td></td>
   <td align="left"><strong><font size=1 >Subtotal Familia</font></strong></td>
   <td align="left"><strong><font size=1 >&nbsp;</font></td>
   <td align="center"><strong><font size=1 ><?php echo number_format($cantidadfam, 2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subexefam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subgrafam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($importefam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($mondesfam,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php  echo number_format($impuestofam,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($totalfam,2) ?></font></strong></td>
 </tr>
   <tr>

 <tr>
   <td></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></td>

   <td colspan="4" align="center"  bgcolor="#33CC66" >REDUCIDA</td>
   <td align="center">&nbsp;</td>
   <td colspan="3" align="center" bgcolor="#33CC66"  >TRANSITORIO</td>
   <td align="right">&nbsp;</td>
   <td align="right" bgcolor="#33CC66" >GENERAL</td>
 </tr>
 <tr>
 
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo17">0%</td>
   <td align="right" class="Estilo17">1 % </td>
   <td align="right" class="Estilo17">2 % </td>
   <td align="right" class="Estilo17">4 % </td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6">0%</td>
   <td align="right" class="Estilo6">4 % </td>
   <td align="right" class="Estilo6">8 %</td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6">13 %</td>
 </tr>

 <tr>


   <td align="left" class="Estilo6">SUBTOTAL </td>
   <td align="right" class="Estilo17"><?php echo number_format($SUBTOTAL_I0R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($SUBTOTAL_I1R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($SUBTOTAL_I2R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($SUBTOTAL_I4R  , 2);?></td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($SUBTOTAL_I0T  , 2);?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($SUBTOTAL_I4T  , 2);?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($SUBTOTAL_I8T  , 2);?></span></td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($SUBTOTAL_I13  , 2);?></span></td>

   </tr>
   
    <tr>
	
   <td align="left" class="Estilo6">IMPUESTO </td>
   <td align="right" class="Estilo17"><?php echo number_format($TOTAL_I0R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($TOTAL_I1R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($TOTAL_I2R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($TOTAL_I4R  , 2);?></td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($TOTAL_I0T  , 2);?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($TOTAL_I4T  , 2);?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($TOTAL_I8T  , 2);?></span></td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($TOTAL_I13  , 2);?></span></td>
	</tr>
 
 	<tr>
    </tr>
  <tr>
      <td width="1%"></td>

    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>CODIGO</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>
			<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>LINEA</strong></font></span></td>
  </tr>
  
   <tr>
	     <td></td>
        <td> <strong> <font size=2 ><?php echo $cdgfam." - ".$dscfam ?></font></strong></td>
        <td>  <font size=2 ></font></td>
   	    <td>  <font size=1 align="right"></font></td>
   	    <td>  <font size=1 align="left"></font></td>
		<td align="center">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>


      </tr>
		
	  <?php	
				$cantidadfam =0;
				$importefam  =0;
				$mondesfam   =0;
				$impuestofam =0;
				$totalfam    =0;
				$total       =0;
				$subexefam   =0;
				$subgrafam   =0;

				$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;
	
							  /*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;
			
			 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 "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>
   <td align="right"><font size=1 ><?php echo $nomlinea ?></font></td> 


 </tr>
 
  <?php
      $contador++;
  	        
		 } 
 ?> 		
  
	 </tr>
 <tr>
   <td></td>
   <td align="left"><strong><font size=1 >Subtotal Familia</font></strong></td>
   <td align="left"><strong><font size=1 >&nbsp;</font></td>
   <td align="center"><strong><font size=1 ><?php echo number_format($cantidadfam, 2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subexefam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subgrafam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($importefam,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($mondesfam,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php  echo number_format($impuestofam,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($totalfam,2) ?></font></strong></td>
 </tr>	 
	 <tr>	 
		
		 </tr>	
	<tr>
   <td></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></td>

   <td colspan="4" align="center"  bgcolor="#33CC66" >REDUCIDA</td>
   <td align="center">&nbsp;</td>
   <td colspan="3" align="center" bgcolor="#33CC66"  >TRANSITORIO</td>
   <td align="right">&nbsp;</td>
   <td align="right" bgcolor="#33CC66" >GENERAL</td>
 </tr>
 <tr>
 
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo17">0%</td>
   <td align="right" class="Estilo17">1 % </td>
   <td align="right" class="Estilo17">2 % </td>
   <td align="right" class="Estilo17">4 % </td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6">0%</td>
   <td align="right" class="Estilo6">4 % </td>
   <td align="right" class="Estilo6">8 %</td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6">13 %</td>
 </tr>

 <tr>


   <td align="left" class="Estilo6">SUBTOTAL </td>
   <td align="right" class="Estilo17"><?php echo number_format($SUBTOTAL_I0R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($SUBTOTAL_I1R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($SUBTOTAL_I2R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($SUBTOTAL_I4R  , 2);?></td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($SUBTOTAL_I0T  , 2);?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($SUBTOTAL_I4T  , 2);?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($SUBTOTAL_I8T  , 2);?></span></td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($SUBTOTAL_I13  , 2);?></span></td>

   </tr>
   
    <tr>
	
   <td align="left" class="Estilo6">IMPUESTO </td>
   <td align="right" class="Estilo17"><?php echo number_format($TOTAL_I0R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($TOTAL_I1R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($TOTAL_I2R  , 2);?></td>
   <td align="right" class="Estilo17"><?php echo number_format($TOTAL_I4R  , 2);?></td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($TOTAL_I0T  , 2);?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($TOTAL_I4T  , 2);?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($TOTAL_I8T  , 2);?></span></td>
   <td align="right" class="Estilo6">&nbsp;</td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($TOTAL_I13  , 2);?></span></td>
	</tr>
 
 	<tr>
    </tr>
	<?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 );
	 
	 */
	    $consulta2= ("SELECT  SUM(importe) AS importe, SUM(mondes) AS mondes , SUM(impuesto) AS impuesto ,  SUM(cantidad) AS cantidad, vista_reporte_familias.cdgfam, familias.cdgfam FROM vista_reporte_familias, familias WHERE borrado=0 AND  vista_reporte_familias.cdgfam = familias.cdgfam  AND ".$where );
		
		


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

  	 while ($dato = $resultado2->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 FACTURACION</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="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>






</body>
</html>