<?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"];
	   
	   $interes_acumulado=0;

 
$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"];
	    if (!isset($_GET["tazint"])) { $tazint=""; } else  {  $tazint=$_GET["tazint"]; }
	
		
	   $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"><font size=3 ><strong>Estado de Cuenta del Cliente</strong></font></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" ><font size=2 ><strong>FACTURA</strong></font></td>
        <td width="20%" bgcolor="#CCCCCC" ><font size=2 ><strong>FECHA</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>PLAZO</strong></font></td>
		<td width="19%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>VENCIMIENTO</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=2 ><strong>SALDO</strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=2 ><strong>INTERESES</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=2 ><strong>TOTAL</strong></font></td>
		
 
    </tr>
  </table>';
         
 

        $consulta= ("SELECT * FROM web_2101 WHERE CDGCLI=$Cdgcli AND salact > 0" );
		if ($resultado = $mysqli->query($consulta)) {
    $count = 0;
 
  				while ($dato = $resultado->fetch_assoc()) {

				$MONTOS   = number_format($dato['TOTNET'], 2);
				$SALDOS   = number_format($dato['SALACT'], 2);
				$salact   = $dato['SALACT'];
				$fecfac   = $dato['FECFAC'];
				$numfac   = $dato['NUMFAC'];
				$numfac   = substr($numfac, -10);
				$fecha    = implode("-", array_reverse(explode("-", $fecfac))); 
				$fecvence = $dato['FECVEN'];
				$fecven   = implode("-", array_reverse(explode("-", $fecvence))); 
				$hoy      = date("y-m-d");
				
		$segundos=strtotime($hoy) - strtotime($fecvence);
		$dias=intval($segundos/60/60/24);
	
		if 	($dias  > 0 && $tazint  > 0 ) {

		$taza=$tazint/100;
		$interes=$salact*$taza;
		$saldo=$interes/30;
		$monint=$saldo*$dias;
		$intfac=$saldo*$dias;
		$monint=round($monint,2);
		$intfac=round($intfac,2);
		} else {
		$monint=0;
		$intfac=0;
	}
	
	$TOTALES =  $salact +  $intfac ;
	$interes_acumulado=$monint +$interes_acumulado;
   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="19%" align="center">  <font size=2 >'.$dato['PLADIA'].'</font></td>
   	    <td width="20%" align="center">  <font size=2 align="center">'.$fecven.'</font></td>
        <td width="20%" align="right">  <font size=2 >'.$SALDOS.'</font></td>
		<td width="20%" align="right">  <font size=2 >'.number_format($monint,2).'</font></td>
        <td width="20%" align="right">  <font size=2 >'.number_format($TOTALES,2).'</font></td>
                  </tr> ';
              $count++;
       }
      //  if($count > 1) 
            
 
  
  	 //    
	      } 
		  
		  $codigoHTML.='<p>&nbsp;</p>';
		  
		  $codigoHTML.='</table>';
		
        $consulta2= ("SELECT SUM(SALACT)AS ACTUAL, SUM(TOTNET) AS INICIAL FROM web_2101 WHERE CDGCLI=$Cdgcli  AND SALACT > 0 ");

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

 
  				while ($dato2 = $resultado2->fetch_assoc()) {
			   
				$INICIAL = number_format($dato2['INICIAL'], 2);
				$ACTUAL  = number_format($dato2['ACTUAL'], 2);
				$TOTINT  = number_format($interes_acumulado, 2);
				$TOTAL   = $dato2['ACTUAL'] + $interes_acumulado;
				$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="80%" height="12" align="left"><font size=2 > TOTALES FINALES</font></td>
              <td width="20%" align="right"><font size=2 >'.$ACTUAL.'</font></td>
              <td width="20%" align="right"><font size=2 >'.$TOTINT.'</font></td>
			   <td width="20%" align="right"><font size=2 >'.$TOTAL.'</font></td>
	
         </tr>

       </table></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="38%" align="center">_____________________________</td>
       <td width="26%" align="center">&nbsp;</td>
       <td width="36%" 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>';

$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));

?>
