<?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"];
	   


 
$codigoHTML='
    <table width="100%"  cellpadding="0" cellspacing="0"  border="0">
      <tr>
        <td width="13%" rowspan="4"><img src="../img/logo.jpg"  /></td>
        <td width="87%"><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>';
  
   
	 $fecha          = $_GET["fechainicio"];
	 $cdgcli         = $_GET["cdgcli"];
	 $dsccli         = strtoupper($_GET["dsccli"]);
	 $combotipoplazo = $_GET["combotipoplazo"];
	 $estado_com     = $_GET["estado"];
	 $interes        = $_GET["interes"];
	 $estado         = substr($estado_com,0,3);
	 
	 $fechainicio= implode("-", array_reverse(explode("-", $fecha))); 

	
$where="1=1";
if ($cdgcli <> "") { $where.=" AND cdgcli='$cdgcli'"; }
if ($estado <> "") { $where.=" AND modpro='$estado'"; }
if ($combotipoplazo == 1 ) { $where.=" AND al_dia > 0"; }
if ($combotipoplazo == 2 ) { $where.=" AND 0_15 > 0"; }
if ($combotipoplazo == 3 ) { $where.=" AND 15_30 > 0"; }
if ($combotipoplazo == 4 ) { $where.=" AND 30_60 > 0"; }
if ($combotipoplazo == 5 ) { $where.=" AND 60_90 > 0"; }
if ($combotipoplazo == 6 ) { $where.=" AND 90_mas > 0"; }
if ($dsccli <> "") { $where.=" AND dsccli like '%".$dsccli."%'"; }


			
	
if ($combotipoplazo=="") {
	 
	$dsctipvencimiento="GENERAL";
	 
	     } else {

    
	$consulta = "SELECT * FROM tipo_vencimiento WHERE cdgtipvencimiento=$combotipoplazo ";
    $resultado = $mysqli->query($consulta);
    $row = $resultado->fetch_array();
    $dsctipvencimiento = $row["dsctipvencimiento"];
	
	 }
						 
	 $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>Cartera por Cobrar  '.$dsctipvencimiento.' </strong></font></td>
      </tr>
      <tr>
        <td height="14" colspan="3" align="center">Fecha de Corte al  <font size="2" >'.$fecha.' - '.$estado_com.' </font></td>
      </tr>
    </table>
  ';
	
	
	 $codigoHTML.='
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
    

        <td width="59%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Cliente</strong></font></td>
		<td width="10%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Telefono</strong></font></td>
		<td width="17%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>0 A 15</strong></font></td>
        <td width="17%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>15 A 30</strong></font></td>
        <td width="17%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>30 A 60</strong></font></td>
		<td width="17%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>60 A 90</strong></font></td>
        <td width="17%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Mas de 90</strong></font></td>
        <td width="17%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Al Dia</strong></font></td>
		<td width="17%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Vencido</strong></font></td>
        <td width="17%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Saldo</strong></font></td>
		<td width="17%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Interes</strong></font></td>
		<td width="17%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total</strong></font></td>
            
 
      </tr>
	   </table>';

	
	 
		
			$sel_resultado="SELECT 	cdgcli,dsccli, sum(al_dia) as al_dia , sum(0_15) as 0_15, sum(15_30) as 15_30, sum(30_60) as 30_60 , sum(60_90) as 60_90  , sum(90_mas) as 90_mas , sum(intmor) as intmor, sum(salven) as salven FROM cartera_cxc WHERE  ".$where. "  GROUP BY cdgcli ORDER BY dsccli ASC " ;

if ($resultado = $mysqli->query($sel_resultado)) 
        $count = 0;
 
    while ($fila = $resultado->fetch_assoc()) {
        $al_dia  = $fila["al_dia"];
        $d0_15   = $fila["0_15"];
		$d15_30  = $fila["15_30"];
		$d30_60  = $fila["30_60"];
        $d60_90  = $fila["60_90"];
        $d90_mas = $fila["90_mas"];
        $salven  = $fila["salven"];
        $intmor  = $fila["intmor"];
		
		if($interes <> 1  )  { $intmor=0; }
		
		
		$salpag  = $salven+$al_dia+$intmor;
	    $salact  = $salven+$al_dia;
		$cdgcli  = $fila["cdgcli"];
		$cdgcli  = sprintf("%06d",$cdgcli); 
		$dsccli  = $fila["dsccli"];				                
		$salpag=$salven+$al_dia+$intmor;
		$salact=$salven+$al_dia;
		
			
		
				  $clientes = "SELECT NUMTEL FROM web_0301  where cdgcli = '$cdgcli' ";
       $result_clientes = $mysqli->query($clientes);
       $row_clientes = $result_clientes->fetch_array();
     
	   $numtel =  $row_clientes["NUMTEL"];
	   $numtel = substr($numtel,0,8);

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

            $codigoHTML.='   
    
      <tr>
  
   	    <td width="9%">  <font size=1 align="right">'.$cdgcli.'</font></td>
   	    <td width="50%">  <font size=1 align="left">'.$dsccli.'</font></td>
		<td width="10%">  <font size=1 align="left">'.$numtel.'</font></td>
        <td width="17%" align="right">  <font size=1 >'.number_format($d0_15, 2).'</font></td>
        <td width="17%" align="right">  <font size=1 >'.number_format($d15_30, 2).'</font></td>
		<td width="17%" align="right">  <font size=1 >'.number_format($d30_60, 2).'</font></td>
		<td width="17%" align="right">  <font size=1 >'.number_format($d60_90, 2).'</font></td>
		<td width="17%" align="right">  <font size=1 >'.number_format($d90_mas, 2).'</font></td>
		<td width="17%" align="right">  <font size=1 >'.number_format($al_dia, 2).'</font></td>
		<td width="17%" align="right">  <font size=1 >'.number_format($salven, 2).'</font></td>
		<td width="17%" align="right">  <font size=1 >'.number_format($salact, 2).'</font></td>
		<td width="17%" align="right">  <font size=1 >'.number_format($intmor, 2).'</font></td>
		<td width="17%" align="right">  <font size=1 >'.number_format($salpag, 2).'</font></td>
		
                  </tr> ';
              $count++;
       }
        if($count > 1) 
            $codigoHTML.='</table>';
			   
			  
	
	   	$sel_Saldo="SELECT  sum(0_15) as 0_15tot,sum(15_30) as 15_30tot ,sum(30_60) as 30_60tot,sum(60_90) as 60_90tot, sum(90_mas) as 90_mastot, sum(salven) as salventot , sum(al_dia) as saldiatot , sum(intmor) as intmortot FROM cartera_cxc WHERE  ".$where ;	
    $result = $mysqli->query($sel_Saldo);
    $row = $result->fetch_array();
	
    $saldiatot = $row["saldiatot"];
	$salventot = $row["salventot"];
	$d0_15tot = $row["0_15tot"];
	$d15_30tot = $row["15_30tot"];
	$d30_60tot = $row["30_60tot"];
	$d60_90tot = $row["60_90tot"];
	$d90_mastot = $row["90_mastot"];	
	$intmortot = $row["intmortot"];
	
	if($interes <> 1  )  { $intmortot=0; }	
	
	
	$salpagtot=$salventot+$saldiatot+$intmortot;
	$salacttot=$salventot+$saldiatot;	
		
$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="69%" height="12" align="left"><font size=2 > TOTALES FINALES</font></td>
			  <td width="17%" align="right"><font size=1 >'.number_format($d0_15tot, 2).'</font></td>
              <td width="17%" align="right"><font size=1 >'.number_format($d15_30tot, 2).'</font></td>
			  <td width="17%" align="right"><font size=1 >'.number_format($d30_60tot, 2).'</font></td>
		      <td width="17%" align="right"><font size=1 >'.number_format($d60_90tot, 2).'</font></td>
			  <td width="17%" align="right"><font size=1 >'.number_format($d90_mastot, 2).'</font></td> 
			  <td width="17%" align="right"><font size=1 >'.number_format($saldiatot, 2).'</font></td>
              <td width="17%" align="right"><font size=1 >'.number_format($salventot, 2).'</font></td>
			  <td width="17%" align="right"><font size=1 >'.number_format($salacttot, 2).'</font></td>
              <td width="17%" align="right"><font size=1 >'.number_format($intmortot, 2).'</font></td>
			  <td width="17%" align="right"><font size=1 >'.number_format($salpagtot, 2).'</font></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("Resumen Cartera CXC.pdf", array("Attachment" => false));
 


?>
