<?php

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


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




$where="1=1";






	$totalfacturas   = 0;
	$utilidad_total  = 0;
	$total_ventas    = 0;
	$cantidad_total  = 0;
	
	$exiact_total = 0;
	
	
	
	$consulta2 = "SELECT PR.CDGBAR, PR.CDGBAR2, PR.EXIACT, PR.CDGART, PR.DSCART, PR.estart FROM web_1001 AS PR WHERE  PR.estart = 1 AND NOT EXISTS( SELECT 1 FROM factulinea AS PE WHERE PE.codigo = PR.CDGBAR ) AND NOT EXISTS( SELECT 1 FROM factulinea AS PE WHERE PE.codigo = PR.CDGBAR2 ) " ;
	
	 $result_pa= $mysqli->query($consulta2);
	
	 $filas = $result_pa->num_rows;
	 
	 
?>
<!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 DE ARTICULOS MENOS VENDIDOS</title>
</head>
<body>
<table width="50%" border="0" cellspacing="0" cellpadding="0">
    <tr>   </tr>
  <tr>
     <td></td>
 <td colspan="9"  bgcolor="skyblue"><div align="center"><strong> LISTA DE ARTICULOS NO VENDIDOS</strong></div></td>
  </tr>
  <tr>
     <td></td>
 <td colspan="9"  bgcolor=""><div align="center"><font size="3" >Cantidad <?php echo $filas ; ?> Este Reporte <?php echo $filtro ; ?>  </font></div></td>
  </tr>
     <tr>   </tr>
  <tr>
      <td width="10%"></td>
    <td  border="1" width="10%"><div align="center"><strong>CODIGO</strong></div></td>
    <td width="12%"><div align="center"><strong>BARRAS</strong></div></td>
    <td width="37%"><div align="center"><strong>ARTICULO</strong></div></td>
    <td width="16%"><div align="center"><strong>CANTIDAD</strong></div></td>
    <td width="25%"><div align="center"><strong>EXISTENCIA</strong></div></td>
	<td width="25%"><div align="center"><strong>VENTA</strong></div></td>
	<td width="25%"><div align="center"><strong>UTILIDAD</strong></div></td>
	<td width="25%"><div align="center"><strong>%</strong></div></td>
	
  </tr>
  
<?PHP

	$contador=0;
$marcaestado=0;	
$marcaborrado=0;		

	
$consulta = "SELECT PR.CDGBAR, PR.CDGBAR2, PR.EXIACT, PR.CDGART, PR.DSCART, PR.estart FROM web_1001 AS PR WHERE  PR.estart = 1 AND NOT EXISTS( SELECT 1 FROM factulinea AS PE WHERE PE.codigo = PR.CDGBAR ) AND NOT EXISTS( SELECT 1 FROM factulinea AS PE WHERE PE.codigo = PR.CDGBAR2)  LIMIT  " .$filtro ;

if ($resultado = $mysqli->query($consulta)) 

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

		
	$cdgbar  = $res["CDGBAR"];	
	$exiact  = $res["EXIACT"];
	$codigo  = $res["CDGART"];
	$codigo  = $res["CDGART"];
	$dscart  = $res["DSCART"];
	
	$cdgbar  = "Cod. ".$cdgbar;
	
	
	
		
		
	//$consulta1 = "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 codigo='$cdgbar' AND borrado=0   AND ".$where. " GROUP BY  codigo  ORDER BY SUM(cantidad) ASC   LIMIT  " .$filtro ;
	
	
		// $result = $mysqli->query($consulta1);
		// $row = $result->fetch_array();
		 
		 $importe      = 0;
        $mondes       = 0;
        $impuesto     = 0;
		$total        = 0;
		$utilidad     = 0;
		$costo_total  = 0;
		$cantidad     = 0;

		 
	
				   
									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;
		
		$exiact_total = $exiact_total + $exiact ;
		
		
			
		?>
		
		<?php	  $dscart = substr($dscart, 0,70);  ?>	


								
 <tr>
   <td></td>
   <td><div align="left"><?php echo $codigo; ?></div></td>
	<td><div align="left"><?php echo $cdgbar; ?></div></td>
	<td><?php echo $dscart; ?></td>
	<td><div align="right"><?php echo number_format($cantidad,2)  ?></div></td>
	<td><div align="right"><?php echo number_format($exiact,2) ?></div></td>    
	<td><div align="right"><?php echo number_format($total,2) ?></div></td>     
	<td><div align="right"><?php echo number_format($utilidad,2) ?></div></td>  
	<td><div align="right"><?php echo number_format($porcentaje,2) ?></div></td>                      
 </tr> 
  <?php
  
 
}
  ?>
  
  <tr>
   <td></td>
   <td><div align="left"></div></td>
	<td><div align="left"><strong>Totales</strong></div></td>
	<td></td>
	<td><div align="right"><?php echo number_format($cantidad_total,2)  ?></div></td>
	<td><div align="right"><?php echo number_format($exiact_total,2) ?></div></td>    
	<td><div align="right"><?php echo number_format($total_ventas,2) ?></div></td>     
	<td><div align="right"><?php echo number_format($utilidad_total,2) ?></div></td>  
	<td><div align="right"></div></td>                      
 </tr> 
 
 
</table>
</body>
</html>