<?php
include ("../conexion.php");
    
require_once '../dompdf/autoload.inc.php';


require_once '../dompdf/lib/html5lib/Parser.php';
require_once '../dompdf/lib/php-font-lib/src/FontLib/Autoloader.php';
require_once '../dompdf/lib/php-svg-lib/src/autoload.php';
require_once '../dompdf/src/Autoloader.php';
Dompdf\Autoloader::register();

use Dompdf\Dompdf;


$codigoHTML='
<!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</title>

</head>


   <script type="text/php">

        if ( isset($pdf) ) {

          $font = Font_Metrics::get_font("helvetica", "bold");
          $pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));

        }
        
</script>
<body>
<div align="center">
  <p>&nbsp;</p>';
  
  $parametros = "SELECT * FROM web_0000 ";
       $result_parametros = $mysqli->query($parametros);
       $row_para = $result_parametros->fetch_array();
     
	   $NombreComercial =  $row_para["NOMCOM"];
	   $Direccion       =  $row_para["DIREMP"];
	   $Cedula          =  $row_para["CEDJUR"];
	   $Telefono        =  $row_para["NUMTEL"];
	   $cueban1         =  $row_para["CUEBAN1"];
	   $cueban2         =  $row_para["CUEBAN2"];
	   $nomcue          =  $row_para["NOMCUE"];
	   

 
$codigoHTML='
    <table width="100%"  cellpadding="0" cellspacing="0"  border="0">
      <tr>
        <td width="13%" rowspan="5"><img src="../img/logo.jpg" /></td>
        <td width="87%"><font color="blue" size=4><strong>'.$NombreComercial.'</strong></font></td>
      </tr>
      <tr>
        <td height="12"><font size=2 >Cedula '.$Cedula.'</font></td>
      </tr>
      <tr>
        <td height="12"><font size=2 >'.$Direccion.'</font></td>
      </tr>
      <tr>
        <td height="11"><font size=2 >Numero de Telefono '.$Telefono.'</font></td>
      </tr>
      <tr>
        <td height="22">&nbsp;</td>
      </tr>
    </table>
    ';
  
   
 $cdgcli=$_GET["cdgcli"];
 $filtro=$_GET["filtro"];

$fechainicio=$_GET["fechainicio"];
$fechafin=$_GET["fechafin"];
 $orderby="cantidad";
 $fechainicio= implode("-", array_reverse(explode("-", $fechainicio))); 
$fechafin= implode("-", array_reverse(explode("-", $fechafin))); 



$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 ($cdgcli <> "") { $where.=" AND cdgcli='$cdgcli'"; }
//if ($cdgfam <> "") { $where.=" AND pedidos_lineas.cdgfam='$cdgfam'"; }

  if ($result = $mysqli->query("SELECT * FROM pedidos_lineas WHERE  borrado=0 and  ".$where )) {
  $filas = $result->num_rows;
   $result->close();
}



$query_busqueda="SELECT * FROM pedidos_lineas WHERE borrado = 0 AND ".$where;
    $resultado = $mysqli->query($query_busqueda);
       $row = $resultado->fetch_array();


	
	 
			$query="SELECT * FROM web_0301 WHERE CDGCLI=$cdgcli";
		    $resultado = $mysqli->query($query);
       $row = $resultado->fetch_array();
	 
	 
	 	$CedulaCliente    = $row["CEDCLI"];
		$TelefonoCliente  = $row["NUMTEL"];
		$CodigoCliente    = $row["CDGCLI"];
	    $DireccionCliente = $row["DIRCLI"];
		$NombreCliente    = $row["DSCCLI"];

	 
	 $codigoHTML.='
  <table width="100%" height="16%" border="1" cellpadding="0" cellspacing="0" >
      <tr>
        <td width="80%" height="21" align="center"><font size=3 ><strong>Productos con mas venta x Cliente </strong></font></td>
      </tr>
      
      <tr>
        <td height="35">
          <table width="100%"  border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td width="100%" height="21"><font size=3 >  <strong>'.$CodigoCliente.' - '.$NombreCliente.' ('.$CedulaCliente.')</strong></font></td>
            </tr>
            <tr>
              <td height="12"><font size=2 >  '.$DireccionCliente.' - Telefono: '.$TelefonoCliente.'</font></td>
            </tr>
          </table>        </td>
      </tr>
  </table>
  ';
	
	
	 $codigoHTML.='
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
        <td width="11%" bgcolor="#CCCCCC" ><div align="center"><font size=2 ><strong>CODIGO</strong></font></div></td>
        <td width="51%" bgcolor="#CCCCCC" ><div align="center"><font size=2 ><strong>ARTICULO</strong></font></div></td>
        <td align="right" bgcolor="#CCCCCC"><div align="center"><font size=2 ><strong>CANTIDAD</strong></font></div></td>
    </tr>
  </table>
';


				$sel_resultado="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, cdgcli  FROM pedidos_lineas WHERE borrado=0   AND ".$where. " GROUP BY  codigo  ORDER BY $orderby DESC " ;
								   $sel_resultado=$sel_resultado."  limit ".$filtro;
				
						
							  
						   $contador=0;
						   $marcaestado=0;	
						   $marcaborrado=0;						   
				if ($resultado = $mysqli->query($sel_resultado)) 

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

		$cantidad     = $fila["cantidad"];
		$codigo       = $fila["codigo"];
		$dscart       = $fila["dscart"];
	
							

       
							
$codigoHTML.='
   <table width="100%"  rules="none" border="0">
    
      <tr>
        <td width="14%">  <font size=2 >'.$codigo.'</font></td>
        <td width="48%" align="center">  <div align="left"><font size=2 >'.$dscart.'</font>  <font size=2 ></font>  <font size=2 align="center"></font>  <font size=2 ></font></div></td>
        <td width="23%" align="right">  <font size=2 >'.number_format($cantidad,2).'</font></td>
        <td width="15%" align="right">  <font size=2 ></font></td>
      </tr>
  </table>
   ';
 				$contador++;
							}
								
			
$codigoHTML.='
   <p>';
   
     
$codigoHTML.='

   </p>
   <p>&nbsp;</p>
   <p>&nbsp;</p>
   <p>&nbsp;</p>
   <p>&nbsp;</p>
</div>
</body>
</html>';

$codigoHTML=utf8_decode($codigoHTML);
$dompdf=new DOMPDF();
$dompdf->set_paper( "A4", "portrait" ); // a4,vertical
$dompdf->load_html($codigoHTML);
ini_set("memory_limit","256M");
$dompdf->render();
$dompdf->stream("Reporte Top 100 x Cliente.pdf", array("Attachment" => false));

?>
