<?php

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


$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."'";
		}
	}
}

 //$where.=" ORDER BY numfactura DESC";
	$venta            = 0;		
	$utilidad_total   = 0;
	$porcentaje_total = 0;
	$preven           = 0;
    $utilidad         = 0;
	$porcentaje       = 0;
	
	
	
	$costo_total_r  = 0;		

	$precio_venta = 0;


	
	
if ($result = $mysqli->query("SELECT * FROM factulinea WHERE  ".$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 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="8"  ><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="8" ><div align="center"><font size="3" ><?php echo $razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="8" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="8" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="8" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="8" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="8"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Analitico de Ventas X Utilidad</font>  </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="8"   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>Factura</strong></font></span></td>
    <td width="30%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>Fecha</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Codigo</strong></font></span></td> 
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Cliente </strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Venta</strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Costo</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>%</strong></font></span></td>
  </tr>
  
<?PHP
		
    	if ($filas > 0) { 

 $contador=0;
 $marcaestado=0;	
 $marcaborrado=0;		
						   					
				$consulta = "SELECT codfactura,cantidad,cdgcli ,fecfac , borrado, SUM(importe) AS preven, SUM(cosart * cantidad) AS costo_total, SUM(mondes) AS descuento, codigo FROM factulinea WHERE borrado = 0  AND   ".$where. " GROUP BY codfactura ";
if ($resultado = $mysqli->query($consulta)) 

 
    while ($fila = $resultado->fetch_assoc()) {
        $cantidad      = $fila["cantidad"];
        $marcaborrado  = $fila["borrado"];
        $fecha         = $fila["fecfac"];
		$fecfac        = implode("-", array_reverse(explode("-", $fecha)));

		$codfactura    = $fila["codfactura"];
		$cdgcli        = $fila["cdgcli"];
		$codigo        = $fila["codigo"];
		
		
		
		
					$query_tipart = "SELECT CDGBAR,tipart  FROM web_1001  WHERE   CDGBAR =   '$codigo' ";
					$rs_tipart = $mysqli->query($query_tipart);
					$row_tipart= $rs_tipart->fetch_assoc();
					
					$codigo = $row_tipart["CDGBAR"];
					$tipart = $row_tipart["tipart"];
					
					


			$query_facturas = "SELECT dsccli, numfactura  FROM facturas  WHERE   codfactura =   '$codfactura' ";
					$rs_facturas = $mysqli->query($query_facturas);
					$row = $rs_facturas->fetch_assoc();
					
					$dsccli = $row["dsccli"];
					$numfac = $row['numfactura'];
	              //  $numfac = substr($numfac, -10);
                  //  $dsccli = substr($dsccli, 0, 38);
	
				if ($marcaborrado == 1) { $borrado="Nula"; } else { $borrado=""; } 
				if ($contador % 2) { $fondolinea="itemParTabla"; } else { $fondolinea="itemImparTabla"; }
			

		

			$preven       = $fila["preven"];
		    $costo_total  = $fila["costo_total"];
			$descuento    = $fila["descuento"];
			
			if ( $tipart == 2){

			$costo_total = $preven *0.80;
			$utilidad     = $preven - $costo_total;
			$porcentaje   = $utilidad/$costo_total;
			$porcentaje   = $porcentaje *100;
			
			
			}
			
			if ($costo_total <> "0"  )  { 
			$preven       = $preven - $descuento;
			$utilidad     = $preven - $costo_total;
			$porcentaje   = $utilidad/$costo_total;
			$porcentaje   = $porcentaje *100;
			
			}
			


	
	$costo_total_r  = $costo_total_r + $costo_total ;		
	$utilidad_total = $utilidad_total + $utilidad ;
	
	
	$precio_venta = $precio_venta + $preven ; 
	
	
	
	$preven_f           = number_format($precio_venta,2,",",".");
	$costo_total_f           = number_format($costo_total_r,2,",",".");
	 $utilidad_total_f      = number_format($utilidad_total,2,",",".");

	
			
			
?>  

 <tr>
   <td></td>
   <td align="center"><font size=1 ><?php echo "N. ".$numfac?></font></td>
   <td align="center"><font size=1 ><?php echo $fecfac ?></font></td>
   <td align="center"><font size=1 ><?php echo $cdgcli?></font></td>
   <td align="left"><font size=1 ><?php echo $dsccli ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($preven,2,".",",")  ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($costo_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++;
		}
							
							
							
			$porcentaje_total   = $utilidad_total/$costo_total_r;
			$porcentaje_total   = $porcentaje_total *100;
			
			$porcentaje_total = number_format($porcentaje_total,2,",",".");

  ?>
  
  
  
  <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>
   <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="center" class="Estilo17">&nbsp;</td>
   <td align="right" class="Estilo6"><span class="Estilo17"></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo $preven_f; ?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo $costo_total_f ;?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo $utilidad_total_f; ?></span></td>
   <td align="right" class="Estilo6"><span class="Estilo17"><?php echo $porcentaje_total; ?></span></td>
 </tr>
</table>

<?php
			}
			
			
			

  ?>
</body>
</html>