<?php

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

$orderby     = $_GET["orderby"];
$fecini      = $_GET["fechainicio"];
$fecfin      = $_GET["fechafin"];

if (""==$_GET["filtro"]) { $filtro=10000; } else  { $filtro=$_GET["filtro"]; }


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



$where="1=1";


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


  
  if ($result = $mysqli->query("SELECT * FROM factulinea WHERE  ".$where ." LIMIT  " .$filtro )) {
   $filas = $result->num_rows;
   $result->close();
}
 


	$totalfacturas   = 0;
	$utilidad_total  = 0;
	$total_ventas    = 0;
	$cantidad_total  = 0;
	
	
    $consulta2= ("SELECT  SUM(importe) AS importe , SUM(mondes) AS mondes , SUM(impuesto) AS impuesto FROM  factulinea WHERE  ".$where. " LIMIT  " .$filtro);

	if ($resultado2 = $mysqli->query($consulta2)) 
	while ($dato = $resultado2->fetch_assoc()) {	
	
	 $importe   = $dato["importe"];
	 $mondes    = $dato["mondes"];
	 $impuesto  = $dato["impuesto"];
	 $totalfacturas = $importe-$mondes+$impuesto;
	
}

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

<title>Reporte Analitico de Ventas </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="6"  ><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="6" ><div align="center"><font size="3" ><?php echo $razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="6" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="6" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="6" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="6" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="6"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Ventas TOP 100</font>  </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="6"   border="1"><div align="center"><font size="3" >Fecha del <?php echo $fecini; ?> Al <?php echo $fecfin;  ?></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="30%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>Articulo</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Cantidad</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>Utilidad</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Porcentaje</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(importe-mondes+impuesto) AS total, SUM(cosart*cantidad) AS costo_total,
	SUM((importe-mondes)-(cosart*cantidad)) AS utilidad, SUM(impuesto) AS impuesto, SUM(cantidad) AS cantidad,  borrado, codigo,dscart, fecfac  FROM factulinea WHERE borrado=0   AND ".$where. " GROUP BY  codigo  ORDER BY $orderby DESC LIMIT  " .$filtro ;
if ($resultado = $mysqli->query($consulta)) 

					 
    while ($fila = $resultado->fetch_assoc()) {
        $importe      = $fila["importe"];
        $mondes       = $fila["mondes"];
        $impuesto     = $fila["impuesto"];
		$total        = $fila["total"];
		$utilidad     = $fila["utilidad"];
		$costo_total  = $fila["costo_total"];
		$cantidad     = $fila["cantidad"];
		$codigo       = $fila["codigo"];
		$dscart       = $fila["dscart"];
		
				   
									if ($costo_total<>0){
									$porcentaje=($utilidad/$costo_total)*100;
									}else{ 
									$porcentaje=100;
									}
							
		$utilidad_total=$utilidad_total+$utilidad;
		$total_ventas=$total_ventas+$total;
		$cantidad_total=$cantidad_total+$cantidad;
							
			
			
?>  

 <tr>
   <td></td>
   <td align="left"><font size=1 ><?php echo "cod.".$codigo?></font></td>
   <td align="left"><font size=1 ><?php echo $dscart ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($cantidad,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($utilidad,2,".",",") ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($porcentaje,2,".",",") ?></font></td>
 </tr>
  
	<?php $contador++;
							}
						
			}
  ?>
  
  
  
  <table width="92%"  border="0" cellspacing="0" cellpadding="0">

 <tr>
   <td></td>

   <td align="center">&nbsp;</td>
   <td align="left">&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%" class="Estilo17"></td>
      <td align="left" class="Estilo6"><span class="Estilo17">TOTALES</span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"></span></td>
    <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($cantidad_total,2,".",",") ?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($total_ventas,2,".",",") ?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo number_format($utilidad_total,2,".",",")?></span></td>
 </tr>
</table>



</body>
</html>