<?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>

	<link href="../estilos/estilos.css" type="text/css" rel="stylesheet"></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="20%" rowspan="4"><img src="../img/logo.jpg" /></td>
        <td width="80%"><font color="red" size=4><strong>'.$NombreComercial.'</strong></font></td>
      </tr>
      <tr>
        <td height="12"><font size=2 >Cedula Juridica '.$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>';
  
 
if (!isset($codfactura)) { $codfactura=$_GET["codfactura"]; } else  {  $codfactura=$_POST["codfactura"]; }

	
	$consulta = "SELECT * FROM facturas_compras WHERE codfactura=$codfactura";
$resultado = $mysqli->query($consulta);
$row = $resultado->fetch_array();

$cdgpro   = $row["cdgpro"];
$dscpro   = $row["dscpro"];
$fecfac   = $row["fecfac"];
$fecfac   = implode("-", array_reverse(explode("-", $fecfac))); 
$fecpro   = $row["fecpro"];
$fecpro   = implode("-", array_reverse(explode("-", $fecpro))); 
$numfac   = $row["numfac"];
$fecven   = $row["fecven"];
$pladia   = $row["pladia"];
$subexe   = $row["subexe"];
$subgra   = $row["subgra"];
$subfac   = $row["subfac"];
$desfactot= $row["desfactot"];
$impfac   = $row["impfac"];
$flefac   = $row["flefac"];
$otrfac   = $row["otrfac"];
$totfac   = $row["totfac"];

	 $fleotrofac= $flefac+$otrfac;
	 

	  $subfac     = number_format($subfac, 2);
	  $desfactot  = number_format($desfactot, 2);
	  $impfac     = number_format($impfac, 2);
	  $fleotrofac = number_format($fleotrofac, 2);
	  $totfac     = number_format($totfac, 2);
 
	 
	 $codigoHTML.='
  <table width="100%" height="16%" border="1" cellpadding="0" cellspacing="0" >
      <tr>
        <td height="21" align="center"><font size=3 ><strong>Detalle de Compra </strong></font></td>
        <td height="21" align="center">Factura</td>
        <td height="21" align="center"><div align="center"><font size="3" ><strong>'.$numfac.' </strong></font></div></td>
      </tr>
      <tr>
        <td width="69%" height="14">Proveedor <font size="3" ><strong>'.$cdgpro.' - '.$dscpro.' </strong></font><font size=2 >&nbsp;  </font></td>
        <td width="11%"><div align="center"><font size="2" >Fecha</font></div></td>
        <td width="20%" height="14" align="center"><font size="2" >'.$fecfac.'</font></td>
      </tr>
  </table>
  ';
	
	
	 $codigoHTML.='
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
      <td width="13%" bgcolor="#CCCCCC" ><div align="center"><strong><font size="2">ARTICULO</font></strong></div></td>
      <td width="43%" bgcolor="#CCCCCC" ><div align="center"><font size=2 ><strong>NOMBRE DEL ARTICULO </strong></font></div></td>
        <td width="9%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>EXENTO</strong></font></td>
		<td width="11%" align="center" bgcolor="#CCCCCC"><strong><font size="2">CANTIDAD</font></strong></td>
      <td width="11%" align="right" bgcolor="#CCCCCC"><div align="center"><font size="2" ><strong>ANTERIOR</strong></font>
      </div>
      <div align="center"><font size=2 ><strong>  </strong></font></div></td>
      <td width="13%" align="right" bgcolor="#CCCCCC"><div align="center"><strong><font size="2">ACTUAL</font></strong></div></td>
 
    </tr>
  </table>';
        
		
	$consulta1 = ("SELECT * FROM factulinea_compra WHERE codfactura=$codfactura" );
        $count = 0;
	if ($resultado1 = $mysqli->query($consulta1)) 

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


				$PRE001 = number_format($dato['PRE001'], 2);
				$PREANT = number_format($dato['PREANT'], 2);
				
				if ($dato['implin']==0) {
				
				$artexe="( E )";
			
				 	 } else {
				 
				 		$artexe="";
					} 	
				
				 
				 
				 
				 	if ($dato['PRE001'] == $dato['PREANT']) {
			
				$DIF=" ";
			
				 } else {
				 
				 		$DIF=" #";
					} 	
			
	
   if($count == 0 )		
                $codigoHTML.='<table width="100%"  rules="none" border="0">';

							
  $codigoHTML.='    
    
      <tr>
        <td width="15%">  <div align="center"><font size=2 >'.$dato['codigo'].'</font></div></td>
        <td width="42%">  <font size=2 >'.$dato['dscart'].'</font></td>
        <td width="10%" align="center">  <div align="center"><font size=2 >'.$artexe.'</font></div></td>
   	    <td width="11%">  <div align="center"><font size=2 align="center">'.$dato['canart'].'</font></div></td>
        <td width="10%" align="right">  <font size=2 >'.$PREANT.'</font></td>
        <td width="14%" align="right">  <font size=2 >'.$PRE001.'</font></td>
		<td width="2%" align="right">  <font size=2 >'.$DIF.'</font></td>
     </tr> ';
              $count++;
      }
        if($count > 1) 
            $codigoHTML.='</table>';

				
	
			
$codigoHTML.='


   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" class="tabla" >
     <tr>
       <td height="15"><table width="100%" border="0" cellpadding="0" cellspacing="0">
         <tr>
           <td height="12" colspan="100%" align="left"><hr align="left"  width="100%" /></td>
          </tr>
         <tr>
           <td width="60%" height="12" align="left"><font size="2" >TOTALES FINALES</font></td>
           <td width="20%" align="right">Subtotal</td>
           <td width="20%" align="right"><font size="2" >'.$subfac.'</font></td>
         </tr>
         <tr>
           <td height="12" align="left">&nbsp;</td>
           <td align="right">Fletes/Otros </td>
           <td align="right"><font size="2" >'.$fleotrofac.'</font></td>
         </tr>
         <tr>
           <td height="12" align="left">&nbsp;</td>
           <td align="right">Descuento</td>
           <td align="right"><font size="2" >'.$desfactot.'</font></td>
         </tr>
         <tr>
           <td height="12" align="left">&nbsp;</td>
           <td align="right">Impuesto</td>
           <td align="right"><font size="2" >'.$impfac.'</font></td>
         </tr>
         <tr>
           <td height="12" align="left">&nbsp;</td>
           <td align="right">TOTAL</td>
           <td align="right"><font size="4" >'.$totfac.'</font></td>
         </tr>
         <tr>
            <td height="12" colspan="3" align="left"><hr align="left"  width="100%" /> </td>
          </tr>
       </table></td>
     </tr>
   </table>
   <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("Estado de Cuenta.pdf", array("Attachment" => false));

?>
