<?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"];
	   $razsoc          =  $row_para["RAZSOC"];
	   $Direccion       =  $row_para["DIREMP"];
	   $Cedula          =  $row_para["CEDJUR"];
	   $Telefono        =  $row_para["NUMTEL"];
  

 
$codigoHTML='
    <table width="100%"  cellpadding="0" cellspacing="0"  border="0">
      <tr>
        <td width="13%" rowspan="4"><img src="../img/Logo_reportes.png"  /></td>
        <td width="87%"><font color="red" 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="22"><font size=2 >Numero de Telefono '.$Telefono.'</font></td>
      </tr>
    </table>';
  
  
     $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 pedidos_lineas.fecfac between '".$fechainicio."' AND '".$fechafin."'";
} else {
	if ($fechainicio<>"") {
		$where.=" and  pedidos_lineas.fecfac >='".$fechainicio."'";
	} else {
		if ($fechafin<>"") {
			$where.=" and  pedidos_lineas.fecfac <='".$fechafin."'";
		}
	}
}

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


	 
	 $codigoHTML.='
  <table width="100%" height="16%" border="1" cellpadding="0" cellspacing="0" >
      <tr>
      <td width="100%" height="21" colspan="3" align="center"><font size=3 ><strong>Reporte Detallado de Pedidos</strong></font></td>
      </tr>
      <tr>
        <td height="14" colspan="3" align="center">Fecha del <font size="2" >'.$fecini.'</font> al <font size="2" >'.$fecfin.'</font></td>
      </tr>
  </table>
  ';
	
	
	 $codigoHTML.='
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
    
        <td width="14%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>CODIGO</strong></font></td>
    
		<td width="27%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>ARTICULO</strong></font></td> 
	    <td width="29%" align="right" bgcolor="#CCCCCC"><div align="center"><font size=1 ><strong>CANTIDAD</strong></font></div></td>
    
 
        <td width="30%" align="right" bgcolor="#CCCCCC"><div align="center">DETALLE</div></td>
    </tr>
  </table>
';
         


    $consulta= "SELECT SUM(mondes) AS mondes, SUM(importe) AS importe, SUM(impuesto) AS impuesto, SUM(cantidad) AS cantidad, pedidos_lineas.cdgfam, pedidos_lineas.borrado, pedidos_lineas.codigo, pedidos_lineas.dscart, pedidos_lineas.fecfac , familias.dscfam FROM pedidos_lineas,familias WHERE  pedidos_lineas.cdgfam=familias.cdgfam AND pedidos_lineas.borrado=0   AND ".$where. " GROUP BY  pedidos_lineas.codigo  ORDER BY    pedidos_lineas.dscart ASC " ;
    if ($resultado = $mysqli->query($consulta)){
        $count = 0;
  		while ($fila = $resultado->fetch_assoc()) {	
            $fecha         = $fila["fecfac"];
            $fecfac       = implode("-", array_reverse(explode("-", $fecha)));
			$fecha2         = $fila["fecfac"];
            $fecfac2       = implode("-", array_reverse(explode("-", $fecha2)));
            $codigo        = $fila["codigo"];
            $cdgfam        = $fila["cdgfam"];
            $dscfam        = $fila["dscfam"];
            $dscart        = $fila["dscart"];
			$dscart = substr($dscart, 0, 80);	
            $cantidad  = $fila["cantidad"];
        
				
   if($count == 0 )		
                $codigoHTML.='  <table width="100%"  rules="none" border="0">';

            $codigoHTML.='    
 
    
      <tr>

        <td width="14%">  <font size=1 >'.$codigo.'</font></td>
   	    <td width="27%">  <font size=1 align="right">'.$dscart.'</font></td>
		<td width="29%" align="center">  <font size=1 >'.number_format($cantidad, 2).'</font></td>

        <td width="30%" align="right">______________________________________</td>
   </tr> ';
              $count++;
       }	 } 
        if($count > 1) 
            $codigoHTML.='</table>';



	
 $consulta2= ("SELECT  SUM(importe) AS importe, SUM(mondes) AS mondes , SUM(impuesto) AS impuesto ,  SUM(cantidad) AS cantidad, pedidos_lineas.cdgfam, familias.cdgfam FROM pedidos_lineas, familias WHERE borrado=0 AND  pedidos_lineas.cdgfam = familias.cdgfam  AND ".$where);

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

  	 while ($dato = $resultado2->fetch_assoc()) {

				$importe     = $dato["importe"];
				$impuesto    = $dato["impuesto"];
				$mondes      = $dato["mondes"];
				$cantidad    = $dato["cantidad"];
			    $total=$importe - $mondes + $impuesto;
	}			
					 
				 $codigoHTML.=' ';
  	
	
$codigoHTML.='
<table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%" border="0" cellpadding="0" cellspacing="0">
         <tr>
            <td width="120%" height="12" align="left"><strong><font size=2 > TOTALES FINALES</font></strong></td>
           
               <td width="20%" align="right"><strong><font size=1 >'.number_format($cantidad, 2).'</font></strong></td>

         </tr>
       </table></td>
     </tr>
  </table>
   <p>';
   
    
$codigoHTML.='

   </p>

</div>
</body>
</html>';

$codigoHTML=utf8_decode($codigoHTML);
$dompdf=new DOMPDF();

	 
$dompdf->set_paper( "A4", "landscape" ); // a4,horizontal
$dompdf->load_html($codigoHTML);
ini_set("memory_limit","7000M");
$dompdf->render();

$dompdf->stream("Reporte de Pedidos.pdf", array("Attachment" => false));
	

?>
