<?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=$_POST["cdgcli"];
     $numrec=$_POST["numrec"];
 
	   $consulta = "SELECT * FROM web_0301 WHERE CDGCLI=$Cdgcli";
       $resultado = $mysqli->query($consulta);
       $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 height="21" colspan="2" align="center"><strong><font size="3">TRAMITE DE FACTURAS PARA CANCELACION</font></strong></td>
      </tr>
      <tr>
        <td width="80%" height="14"><font size=2 >  Nombre del Cliente</font></td>
        <td width="20%" height="14" align="center"><font size=2 >Fecha</font></td>
      </tr>
      <tr>
        <td>
          <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>
        <td height="35" align="center"><font size=2 > '.date( "d/m/Y" ).'</font></td>
      </tr>
  </table>
  ';
	
	
	 $codigoHTML.='
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
        <td width="21%" bgcolor="#CCCCCC" align="center" ><font size=2 ><strong>FACTURA</strong></font></td>
        <td width="20%" bgcolor="#CCCCCC" align="center" ><font size=2 ><strong>FECHA</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>M. INICIAL</strong></font></td>
		<td width="19%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>VENCE</strong></font></td>
        <td width="20%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>ATRAZO</strong></font></td>
		<td width="20%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>S. ACTUAL</strong></font></td>
        <td width="20%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>INTERES</strong></font></td>
        <td width="20%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>TOTAL</strong></font></td>
    
		
 
    </tr>
  </table>';
          
		   $consulta= ("SELECT * FROM tramites_clientes_lineastmp WHERE CDGCLI=$Cdgcli AND numrec=$numrec AND borrado = 1 ");
		if ($resultado = $mysqli->query($consulta))  {
        $count = 0;
 
  				while ($dato = $resultado->fetch_assoc()) {

 
				$salfac = number_format($dato['salfac'], 2);
				$intfac = number_format($dato['intfac'], 2);
				$monini = number_format($dato['totfac'], 2);
				$saldo_factura   =  $dato['salfac'];
				$interes_factura =  $dato['intfac'];
				$monto_incial    =  $dato['totfac'];
				$numfac   = $dato['numfac'];
				$numfac   = substr($numfac, -10);
				$totpag   = number_format($saldo_factura+ $interes_factura, 2);

				$fecfac=$dato['fecfac'];
				$fecha= implode("-", array_reverse(explode("-", $fecfac))); 
				$fecvence=$dato['fecven'];
				$fecven= implode("-", array_reverse(explode("-", $fecvence))); 
				$hoy=date("y-m-d");

							
   if($count == 0 )		
                $codigoHTML.='<table width="100%"  rules="none" border="0">';

            $codigoHTML.='   
      <tr>
        <td width="21%">  <font size=2 >'.$numfac.'</font></td>
        <td width="20%" align="center">  <font size=2 >'.$fecha.'</font></td>
		    <td width="20%" align="right">  <font size=2 >'.$monini.'</font></td>
	  <td width="20%" align="center">  <font size=2 align="center">'.$fecven.'</font></td>
        <td width="19%" align="center">  <font size=2 >'.$dato['diaven'].'</font></td>
        <td width="20%" align="right">  <font size=2 >'.$salfac.'</font></td>
		 <td width="20%" align="right">  <font size=2 >'.$intfac.'</font></td>
        <td width="20%" align="right">  <font size=2 >'.$totpag.'</font></td>
                  </tr> ';
              $count++;
       }
        if($count > 0) 
            $codigoHTML.='</table>';
 
     }

		
		        $consulta2= ("SELECT SUM(salfac)AS salfac, SUM(monint) AS monint FROM tramites_clientes_lineastmp WHERE CDGCLI=$Cdgcli AND numrec=$numrec AND borrado = 1 " );
		if ($resultado2 = $mysqli->query($consulta2)) 

 
  				while ($dato2 = $resultado2->fetch_assoc()) {
			   
			    $salfac = number_format($dato2['salfac'], 2);
				$monint = number_format($dato2['monint'], 2);
				$total= $dato2['monint'] + $dato2['salfac'];
				$total= number_format($total,2);
			
				
			
$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="100%" height="12" align="left"><font size="2" > TOTALES FINALES</font></td>
           <td width="20%" align="right"><font size="2" >'.$salfac.'</font></td>
           <td width="20%" align="right"><font size="2" >'.$monint.'</font></td>
           <td width="20%" align="right"><font size="2" >'.$total.'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>';
   
   $codigoHTML.='
   
   <table width="100%" border="0">
     <tr>
       <td align="center"><font size="1"> RECIBO LAS FACTURAS ANOTADAS AQUI, PARA EL RESPECITVO TRAMITE DE PAGO A '.$NombreComercial.', EN CASO DE EXTRAVIO</td>
     </tr>
     <tr>
       <td align="center"><font size="1"> DE LAS FACTURAS CONSIGNADAS, SE TIENE POR ACEPTADO EL VALOR TOTAL DE ESTA FORMULA EN SUSTITUCION DE LAS FACTURAS </td>
     </tr>
   </table>
   <p>';
   
     } 
$codigoHTML.='

   <table width="100%" border="0">
     <tr>
      <td align="center"><div align="left"><strong><font size=2 >'.$cueban1.'</font></strong></div></td>
     </tr>
     <tr>
       <td align="center"><div align="left"><strong><font size=2 >'.$cueban2.'</font></strong></div></td>
     </tr>
	   <tr>
       <td align="center"><div align="left"><strong><font size=2 >'.$nomcue.'</font></strong></div></td>
     </tr>
   </table>
   
   
   </p>
   <p>&nbsp;</p>
   <p>&nbsp;</p>
   <p>&nbsp;</p>
   <table width="100%" border="0">
     <tr>
       <td width="46%" align="center">__________________________________</td>
       <td width="7%" align="center">&nbsp;</td>
       <td width="47%" align="center">__________________________________</td>
     </tr>
     <tr>
      <td align="center"><strong><em><font size=2 >'.$NombreComercial.'</font></em></strong></td>
      <td align="center">&nbsp;</td>
      <td align="center"><strong><em><font size=2 >Cliente '.$NombreCliente.'</font></em></strong></td>
     </tr>
     <tr>
       <td align="center"><strong><em><font size=2 >Firma y sello</font></em></strong></td>
       <td align="center">&nbsp;</td>
       <td align="center"><strong><em><font size=2 >Recibido conforme</font></em></strong></td>
     </tr>
   </table>
   
   <p>&nbsp;</p>
</div>
</body>
</html>';
$numrec_borrar=$numrec-10;

		$consulta1 = "DELETE FROM tramites_clientes_lineastmp  WHERE numrec <= '$numrec_borrar'  ";
	    $mysqli->query($consulta1);

$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("Tramite de Facturas.pdf", array("Attachment" => false));

?>
