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

	$exento_tot    = 0;
	$gravado_tot   = 0;
	$subtotal_tot  = 0;
	$descuento_tot = 0;
	$impuesto_tot  = 0;
	$total_tot     = 0;
    $where_notas   = "";
    $where         = "";
	
				$subexepro = "0";
				$subgrapro = "0";
				$subpro    = "0";
				$desprotot = "0";
				$imppro    = "0";
				$flepro    = "0";
				$otrpro    = "0";
				$totpro    = "0";
				
	
$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"];
	   
	   
	   // Ruta de la imagen remota
$image_url = '../img/logo.jpg';
$image_data = file_get_contents($image_url);
// Codificar la imagen en base64 para usarla en el HTML
$image_base64 = base64_encode($image_data);


  
$codigoHTML='
    <table width="100%"  cellpadding="0" cellspacing="0"  border="0">
      <tr>
        <td width="13%" rowspan="4"><img src="data:image/jpeg;base64,' . $image_base64 . '" /></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"];

	 $fechainicio= implode("-", array_reverse(explode("-", $fecha))); 

	
$where="1=1";
if ($cdgcli <> "") { $where.=" AND CDGCLI='$cdgcli'"; }

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.'</font></td>
      </tr>
    </table>
  ';
	
	 $codigoHTML.='
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
    
        <td width="34%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Factura</strong></font></td>
        <td width="8%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>

		<td width="3%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Dias vencido</strong></font></td>
        <td width="13%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Al Dia </strong></font></td> 
	    <td width="13%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>0 a 15 </strong></font></td>
	    <td width="13%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>15 a 30 </strong></font></td>
        <td width="13%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>30 a 60 </strong></font></td>
		<td width="13%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>60 a 90 </strong></font></td>
        <td width="13%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Mas de 90 </strong></font></td>
        <td width="13%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Vencido </strong></font></td>
        <td width="13%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Saldo</strong></font></td>
 

 
 
 
    </tr>
  </table>';
        
		
	

		
		
		
		$total_al_dia   = 0;
		$total_d0_15    = 0;
		$total_d15_30   = 0;
		$total_d30_60   = 0;
		$total_d60_90   = 0;
		$total_d90_mas  = 0;
		
		
		$consulta2="SELECT  *  FROM web_2101 WHERE   SALACT > 0  AND ".$where. "  ORDER BY web_2101.CDGCLI,web_2101.FECFAC ASC " ;
			
		  		
		if ($resultado3 = $mysqli->query($consulta2)) 		
		if($dato = $resultado3->fetch_assoc()){ 
      	 $cdgcli_actual = $dato['CDGCLI']; } 
         $cdgcli_actual = sprintf("%04d",$cdgcli_actual);
	    $contador  = 0;
		$count     = 0;	
		if ($resultado2 = $mysqli->query($consulta2)) {
		
		  
  		while ($dato1 = $resultado2->fetch_assoc()) {     //  si encuentra datos

			$al_dia="0";
			$d0_15="0";
			$d15_30="0";
			$d30_60="0";
			$d60_90="0";
			$d90_mas=0;

      
		$fecfac        = $dato1["FECFAC"];
		$fecfac        = implode("-", array_reverse(explode("-", $fecfac)));
		$numfac        = $dato1["NUMFAC"];
	//	$numfac        = substr($numfac, -10);
		$cdgcli        = $dato1["CDGCLI"];
		$cdgcli        = sprintf("%04d",$cdgcli); 
		$dsccli        = $dato1["DSCCLI"];
		$dsccli        = substr($dsccli, 0, 45);
		$pladia        = $dato1["PLADIA"];
		$salact        = $dato1["SALACT"];
		$fecven        = $dato1["FECVEN"];
		
		$fecvencimiento = implode("-", array_reverse(explode("-", $fecven)));
		
		 $segundos=strtotime($fechainicio) - strtotime($fecven);  ///calcula los dias de vencimiento
		 $diaven=intval($segundos/60/60/24);		

			//////////////  Asigna el saldo pendiente segun la categoria de vencimiento //////// 
				 
		if($diaven <= 0 ){         
			$al_dia=$salact;
			$d0_15="0";
			$d15_30="0";
			$d30_60="0";
			$d60_90="0";
			$d90_mas="0";

			}
			elseif(($diaven > 0) && ($diaven <= 15)) {
			$al_dia="0";
			$d0_15=$salact;
			$d15_30="0";
			$d30_60="0";
			$d60_90="0";
			$d90_mas="0";
			}
			elseif(($diaven > 15) && ($diaven <= 30)) {
			$al_dia="0";
			$d0_15="0";
			$d15_30=$salact;
			$d30_60="0";
			$d60_90="0";
			$d90_mas="0";
			
			}
			elseif(($diaven > 30) && ($diaven <= 60)) {
			$al_dia="0";
			$d0_15="0";
			$d15_30="0";
			$d30_60=$salact;
			$d60_90="0";
			$d90_mas="0";
			}
			elseif(($diaven > 60) && ($diaven <= 90)) {
			$al_dia="0";
			$d0_15="0";
			$d15_30="0";
			$d30_60="0";
			$d60_90=$salact;
			$d90_mas="0";
	
		}else{
			$al_dia="0";
			$d0_15="0";
			$d15_30="0";
			$d30_60="0";
			$d60_90="0";
			$d90_mas=$salact;
		}


		$total_al_dia   = $total_al_dia + $al_dia ;
		$total_d0_15    = $total_d0_15 + $d0_15;
		$total_d15_30   = $total_d15_30 + $d15_30;
		$total_d30_60   = $total_d30_60 + $d30_60;
		$total_d60_90   = $total_d60_90 + $d60_90;
		$total_d90_mas  = $total_d90_mas + $d90_mas;

        $vencido =  $d0_15 + $d15_30 + $d30_60 +  $d60_90 +  $d90_mas ;
		
		$total_cxc =  $total_al_dia +   $vencido;

        
	
          if ($contador==0){ 


  $codigoHTML.='
  <table width="100%"  rules="none" border="0">
    
      <tr>
        <td width="100%"> <strong> <font size=1 >'.$cdgcli.' - '.$dsccli.'</font></strong></td>
        <td width="18%">  <font size=2 ></font></td>
   	    <td width="9%">  <font size=1 align="right"></font></td>
   	    <td width="75%">  <font size=1 align="left"></font></td>
		<td width="5	%" align="center">  <font size=1 ></font></td>
        <td width="20%" align="right">  <font size=1 ></font></td>
        <td width="20%" align="right">  <font size=1 ></font></td>


      </tr>

      
  </table>';


 } 





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

           
			
			if ($cdgcli <>  $cdgcli_actual  ){ 
		 
		 		
						 
		$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  cdgcli= $cdgcli_actual AND ".$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"];
	$salpagtot=$salventot+$saldiatot+$intmortot;
	$salacttot=$salventot+$saldiatot;		
			
				$cdgcli_SUBTOTAL = $dato1['CDGCLI'];
				$cdgcli_actual = $dato1['CDGCLI'];
			    $cdgcli_actual = sprintf("%04d",$cdgcli_actual);

				 $codigoHTML.='

     
      <tr>
 		

        <td width="34%">  <font size=1 ></font></td>
        <td width="8%">  <font size=1 ></font></td>
   	  
		<td width="3%" align="center">  <font size=1 ></font></td>
        <td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
        <td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
	 </tr>	
		 <tr>
		  
	    <td width="34%"> <font size=1 align="right"></font></td>
		<td width="8%"><strong><font size=1 >SUBTOTAL </font></strong></td>
       
		<td width="3	%" align="center">  <font size=1 ></font></td>
     	<td width="13%" align="right">  <strong><font size=1 >'.number_format( $saldiatot , 2).'</font></strong></td>
        <td width="13%" align="right">  <strong><font size=1 >'.number_format($d0_15tot , 2).'</font></strong></td>
	    <td width="13%" align="right">  <strong><font size=1 >'.number_format($d15_30tot, 2).'</font></strong></td>
        <td width="13%" align="right">  <strong><font size=1 >'.number_format($d30_60tot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($d60_90tot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($d90_mastot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($salventot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($salacttot, 2).'</font></strong></td>

      </tr>
	  <tr>

	  
	  	<td width="34%"> <strong><font size=1 >'.$cdgcli.' - '.$dsccli.'</font></strong></td>
		<td width="8%">  <font size=1 ></font></td>
     
		<td width="3	%" align="center">  <font size=1 ></font></td>
     	<td width="13%" align="right">  <strong><font size=1 ></font></strong></td>
        <td width="13%" align="right">  <strong><font size=1 ></font></strong></td>
	    <td width="13%" align="right">  <strong><font size=1 ></font></strong></td>
        <td width="13%" align="right">  <strong><font size=1 ></font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 ></font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 ></font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 ></font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 ></font></strong></td>


      </tr> ';

		   	$cdgcli_SUBTOTAL = $dato1['CDGCLI'];
				$cdgcli_actual = $dato1['CDGCLI'];
			    $cdgcli_actual = sprintf("%04d",$cdgcli_actual);

   }
   $codigoHTML.=' 
    
      <tr>
        <td width="34%">  <font size=1 >'.$numfac.'</font></td>
        <td width="8%">  <font size=1 >'.$fecfac.'</font></td>
 
		<td width="3%" align="center">  <font size=1 >'.$diaven.'</font></td>
        <td width="13%" align="right">  <font size=1 >'.number_format($al_dia, 2).'</font></td>
		<td width="13%" align="right">  <font size=1 >'.number_format($d0_15, 2).'</font></td>
		<td width="13%" align="right">  <font size=1 >'.number_format($d15_30, 2).'</font></td>
        <td width="13%" align="right">  <font size=1 >'.number_format($d30_60, 2).'</font></td>
		<td width="13%" align="right">  <font size=1 >'.number_format($d60_90, 2).'</font></td>
		<td width="13%" align="right">  <font size=1 >'.number_format($d90_mas, 2).'</font></td>
		<td width="13%" align="right">  <font size=1 >'.number_format($vencido, 2).'</font></td>
		<td width="13%" align="right">  <font size=1 >'.number_format($salact, 2).'</font></td>
   </tr> ';
   
   			         
    $contador++;
  	       
			$al_dia="0";
			$d0_15="0";
			$d15_30="0";
			$d30_60="0";
			$d60_90="0";
			$d90_mas=0;
			 




	  $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  cdgcli= $cdgcli_actual AND ".$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"];
	$salpagtot=$salventot+$saldiatot+$intmortot;
	$salacttot=$salventot+$saldiatot;		
			//    cambio version   8.0.2 
			//	$cdgcli_SUBTOTAL = $dato1['CDGCLI'];
			//	$cdgcli_actual = $dato1['CDGCLI'];
			//    $cdgcli_actual = sprintf("%04d",$cdgcli_actual);

				 $codigoHTML.='
     
	   <table width="100%"  rules="none" border="0">
     
      <tr>
 		

        <td width="34%">  <font size=1 ></font></td>
        <td width="8%">  <font size=1 ></font></td>
   	  
		<td width="3%" align="center">  <font size=1 ></font></td>
        <td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
        <td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
		<td width="13%" align="right">________<font size=1 ></font></td>
	 </tr>	
		 <tr>
		  
	    <td width="34%"> <font size=1 align="right"></font></td>
		<td width="8%"><strong><font size=1 >SUBTOTAL </font></strong></td>
       
		<td width="3	%" align="center">  <font size=1 ></font></td>
     	<td width="13%" align="right">  <strong><font size=1 >'.number_format( $saldiatot , 2).'</font></strong></td>
        <td width="13%" align="right">  <strong><font size=1 >'.number_format($d0_15tot , 2).'</font></strong></td>
	    <td width="13%" align="right">  <strong><font size=1 >'.number_format($d15_30tot, 2).'</font></strong></td>
        <td width="13%" align="right">  <strong><font size=1 >'.number_format($d30_60tot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($d60_90tot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($d90_mastot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($salventot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($salacttot, 2).'</font></strong></td>

      </tr>

	  </table>';

  	///////////////////////////////////////////////////////   CALCULO DE TOTALES  ///////////////////////////////////////
	
	
	
	$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"];
	$salpagtot=$salventot+$saldiatot+$intmortot;
	$salacttot=$salventot+$saldiatot;	
	
	
$codigoHTML.='
		<p>
   <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="48%" height="12" align="left"><strong><font size=2 > TOTALES FINALES</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format( $saldiatot , 2).'</font></strong></td>
        <td width="13%" align="right">  <strong><font size=1 >'.number_format($d0_15tot , 2).'</font></strong></td>
	    <td width="13%" align="right">  <strong><font size=1 >'.number_format($d15_30tot, 2).'</font></strong></td>
        <td width="13%" align="right">  <strong><font size=1 >'.number_format($d30_60tot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($d60_90tot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($d90_mastot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($salventot, 2).'</font></strong></td>
		<td width="13%" align="right">  <strong><font size=1 >'.number_format($salacttot, 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);
$dompdf->render();


// add the header
//$canvas = $dompdf->get_canvas();
//$font = Font_Metrics::get_font("helvetica", "bold");
//
//// the same call as in my previous example
//$canvas->page_text(745, 555, "Pagina : {PAGE_NUM} de {PAGE_COUNT}",
//                   $font, 8, array(0,0,0));

ini_set("memory_limit","7000M");

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


?>
