
<?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"];
	   
	      $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"];
	 $fechainicio=$_GET["fechainicio"];
	 $fechafin=$_GET["fechafin"];
	 $where="1=1";
	 
			
	
	 

	 $fecini= implode("-", array_reverse(explode("-", $fechainicio))); 
	 $fecfin= implode("-", array_reverse(explode("-", $fechafin))); 
	 

		
	   $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" align="center"><font size=3 ><strong>Reporte Facturas VRS Recibos</strong></font></td>
      </tr>
      <tr>
        <td height="14" align="center"><font size="2">Fecha del '.$fechainicio.' al '.$fechafin.'</font></td>
      </tr>
      <tr>
        <td width="80%" height="14"><font size=2 >  Nombre del Cliente</font></td>
      </tr>
      <tr>
        <td height="35">
          <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>
      </tr>
  </table>
  ';
	
	
	 $codigoHTML.='
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td width="40%" align="center" bgcolor="#CCCCCC" ><font size=2 ><strong>DOCUMENTO</strong></font></td>
        <td width="20%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>FECHA</strong></font></td>

		<td width="19%" align="center" bgcolor="#CCCCCC"><font size=2 ><strong>INTERESES</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><strong><font size="2">CREDITO</font></strong></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=2 ><strong>DEBITO</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=2 ><strong>SALDO</strong></font></td>
		
 
    </tr>';
 // </table>';
          
	$interes_acumulado = 0;
	$total_general     = 0;
	$saldo_general     = 0;	
	$saldo_mayor   = 0;    
 	$total_general  = 0;
    $total_abonado  = 0;
	$total_int     =0;
	$total_abo     =0;


/*********************************PRIMERO MUESTRA LAS CXC****************************************************************************/	


$consulta2 = ("SELECT * FROM web_2101 WHERE CDGCLI=$Cdgcli AND modpro = 'CxC'  AND FECFAC between '$fecini' AND '$fecfin'" );


		if ($resultado4 = $mysqli->query($consulta2)) {
         $countador = 0;
 
  				while ($dato2 = $resultado4->fetch_assoc()) {
				

 

				$MONTOS   = number_format($dato2['TOTNET'], 2);
				$total    = $dato2['TOTNET'];
				$SALDOS   = number_format($dato2['SALACT'], 2);
				$salact   = $dato2['SALACT'];
				$fecfac   = $dato2['FECFAC'];
				$numfac   = $dato2['NUMFAC'];
				$numfac   = substr($numfac, -10);
				$fecha    = implode("-", array_reverse(explode("-", $fecfac))); 
				$fecvence = $dato2['FECVEN'];
				$fecven   = implode("-", array_reverse(explode("-", $fecvence))); 
				$hoy      = date("y-m-d");
				$cdgpla   = $dato2['PLADIA'];
				
		$segundos=strtotime($hoy) - strtotime($fecvence);
		$dias=intval($segundos/60/60/24);
	
	//	if 	($dias  > 0 && $salact  > 0 ) {
	//echo	$tazint=$_POST[tazint];
	//	$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;
	//}
	
	
	// echo $total;
	
	$interes_acumulado=$monint +$interes_acumulado;
	$total_general=$total_general + $total;
	$saldo_general=$saldo_general + $salact;		
	
	
	
	
	$saldo_mayor       =  $total_general -$total_abonado  ;
	
					
							
 //if($countador == 0 )		
   
         //  $codigoHTML.='<table width="100%"  rules="none" border="0">';

            
              
    

	
	/////////////////////////////////////////////   seleciona los recibos de pago aplicados a factura  /////////////////////////
	
	
	$consulta4= ("SELECT * FROM pagos_clientes_recibos_lineas  WHERE cdgcli='$Cdgcli' AND numfac = '$numfac' and borrado = 0 ");
	
       $resultado2 = $mysqli->query($consulta4);
      
	   $dato4= $resultado2->fetch_array();
	    $filas = $resultado2->num_rows;
		
		if ( $filas > 0 ){
		//$total_general=$total_general + $total;
		
				$codigoHTML.=' 
      <tr>
        <td width="40%">  <font size=2 >'."Factura N. ".$numfac.'</font></td>
        <td width="20%" align="center">  <font size=2 >'.$fecha.'</font></td>
   	    <td width="20%" align="center">  <font size=2 align="center"></font></td>
        <td width="20%" align="right">  <font size=2 >'.$MONTOS.'</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($saldo_mayor,2).'</font></td>
                  </tr> ';
				  
       			
 				
				$monabonado   = $dato4['monabo'];
				$moninteres   = $dato4['monint'];
				$monabo   = number_format($dato4['monabo'], 2);
				$monint   = number_format($dato4['monint'], 2);

				$numrec   = $dato4['numrec'];
				
				 
				
				$total_abonado=$total_abonado + $monabonado;
				
				$total_int=$total_int + $moninteres;
				$total_abo=$total_abo + $monabonado;
			
			
		$recibos = "SELECT * FROM pagos_clientes WHERE numrec = '$numrec' and estrec = 1 ";
		$result_recibos = $mysqli->query($recibos);
		$row_recibos = $result_recibos->fetch_array();
		
		$fecrec =  $row_recibos["fecrec"];		
		$fecrec    = implode("-", array_reverse(explode("-", $fecrec))); 
		
		
		$saldo_mayor       =  $total_general -$total_abonado  ;
	   
	   		
				            $codigoHTML.=' 
										<tr>
										<td width="40%">  <font size=2 >'."Recibo N. ".$numrec.'</font></td>
										<td width="20%" align="center">  <font size=2 >'.$fecrec.'</font></td>
										<td width="20%" align="right">  <font size="2">'.$monint.'</font></td>
										<td width="20%" align="right">  <font size=2 ></font></td>
										<td width="20%" align="right">  <font size="2">'.$monabo.'</font></td>
										<td width="20%" align="right">  <font size="2">'.number_format($saldo_mayor, 2).'</font></td>
										</tr> ';

	}
	
	
	
	}
	
	$countador++;
	
	 }
	 
	 
	 
	 
	 

/***********************************************************************************************************************************************************/			
 		
		$consulta= ("SELECT * FROM facturas WHERE cdgcli=$Cdgcli AND cdgtipven = 2 AND fecha between '$fecini' AND '$fecfin' AND ".$where );
		if ($resultado = $mysqli->query($consulta)) {
        $count = 0;
 
  				while ($dato = $resultado->fetch_assoc()) {

				$MONTOS   = number_format($dato['totcompro'], 2);
				$total   = $dato['totcompro'];
				$SALDOS   = 0.00;
				$salact   = 0.00;
				$fecfac   = $dato['fecha'];
				$numcomp   = $dato['numcomp'];
				$numfac   = substr($numcomp, -10);
				$fecha    = implode("-", array_reverse(explode("-", $fecfac))); 
				$fecvence = "0000-00-00";
				$fecven   = "0000-00-00"; 
				$hoy      = date("y-m-d");
				$cdgpla   = 0;
				$cdgtipven= $dato['cdgtipven'];
				$codfactura= $dato['codfactura'];
				
		$segundos=strtotime($hoy) - strtotime($fecvence);
		$dias=intval($segundos/60/60/24);
	

		$monint=0;
		$intfac=0;

	
	
	
	////////////////////////////////////////////   SELECT DE 2101 //////////////////////////////////////////////////////////////
	
	if($cdgtipven == 2) {
	
	$consulta2 = ("SELECT * FROM web_2101 WHERE CDGCLI=$Cdgcli AND numfac = '$numcomp' " );
       $resultado2 = $mysqli->query($consulta2);
       $dato2= $resultado2->fetch_array();
 

				$MONTOS   = number_format($dato2['TOTNET'], 2);
				$total    = $dato2['TOTNET'];
				$SALDOS   = number_format($dato2['SALACT'], 2);
				$salact   = $dato2['SALACT'];
				$fecfac   = $dato2['FECFAC'];
				$numfac   = $dato2['NUMFAC'];
				$numfac   = substr($numfac, -10);
				$fecha    = implode("-", array_reverse(explode("-", $fecfac))); 
				$fecvence = $dato2['FECVEN'];
				$fecven   = implode("-", array_reverse(explode("-", $fecvence))); 
				$hoy      = date("y-m-d");
				$cdgpla   = $dato2['PLADIA'];
				
		$segundos=strtotime($hoy) - strtotime($fecvence);
		$dias=intval($segundos/60/60/24);
	
	//	if 	($dias  > 0 && $salact  > 0 ) {
	//echo	$tazint=$_POST[tazint];
	//	$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;
	//}
	
	 }
	 
	
	$interes_acumulado=$monint +$interes_acumulado;
	$total_general=$total_general + $total;
	$saldo_general=$saldo_general + $salact;		
	
	
	
	
	$saldo_mayor       =  $total_general -$total_abonado  ;
	
	
					
							
   if($count == 0 )		
   
         //  $codigoHTML.='<table width="100%"  rules="none" border="0">';

            $codigoHTML.=' 
      <tr>
        <td width="40%">  <font size=2 >'."Factura N. ".$numfac.'</font></td>
        <td width="20%" align="center">  <font size=2 >'.$fecha.'</font></td>
   	    <td width="20%" align="center">  <font size=2 align="center"></font></td>
        <td width="20%" align="right">  <font size=2 >'.$MONTOS.'</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($saldo_mayor,2).'</font></td>
                  </tr> ';
				  
              $count++;
    
	
	
	/////////////////////////////////////////////   seleciona los recibos de pago aplicados a factura  /////////////////////////
	
	
	$consulta4= ("SELECT * FROM pagos_clientes_recibos_lineas  WHERE cdgcli='$Cdgcli' AND numfac = '$numcomp' and borrado = 0 ");
		if ($resultado4 = $mysqli->query($consulta4)) {
        $count = 0;
 
  				while ($dato4 = $resultado4->fetch_assoc()) {

				$monabonado   = $dato4['monabo'];
				$moninteres   = $dato4['monint'];
				$monabo   = number_format($dato4['monabo'], 2);
				$monint   = number_format($dato4['monint'], 2);

				$numrec   = $dato4['numrec'];
				
				$total_abonado=$total_abonado + $monabonado;
				
				$total_int=$total_int + $moninteres;
				$total_abo=$total_abo + $monabonado;
			
			
		   $recibos = "SELECT * FROM pagos_clientes WHERE numrec = '$numrec'  and estrec = 1 ";
       $result_recibos = $mysqli->query($recibos);
       $row_recibos = $result_recibos->fetch_array();
     
	   $fecrec =  $row_recibos["fecrec"];		
	   $fecrec    = implode("-", array_reverse(explode("-", $fecrec))); 
	   
	   
	   $saldo_mayor       =  $total_general -$total_abonado  ;
	   
	   		
				            $codigoHTML.=' 
										<tr>
										<td width="40%">  <font size=2 >'."Recibo N. ".$numrec.'</font></td>
										<td width="20%" align="center">  <font size=2 >'.$fecrec.'</font></td>
										<td width="20%" align="right">  <font size="2">'.$monint.'</font></td>
										<td width="20%" align="right">  <font size=2 ></font></td>
										<td width="20%" align="right">  <font size="2">'.$monabo.'</font></td>
										<td width="20%" align="right">  <font size="2">'.number_format($saldo_mayor, 2).'</font></td>
										</tr> ';
	
	}
	
	 }
	
	
	
	
	
	
	
	////////////////////////////////////////////////////////////////////       seleciona notas de credito aplicadas a la factura  //////////////////////////////////////////////////////////////
	
	
	$consulta4= ("SELECT * FROM pagos_clientes_notas_lineas  WHERE cdgcli='$Cdgcli' AND numfac = '$numcomp'  ");
		if ($resultado4 = $mysqli->query($consulta4)) {
        $count = 0;
 
  				while ($dato4 = $resultado4->fetch_assoc()) {

				$monabonado   = $dato4['monabo'];
				$moninteres   = 0;
				$monabo   = number_format($dato4['monabo'], 2);
				$monint   = number_format(0, 2);

				$codnota   = $dato4['codnota'];
				
				$total_abonado=$total_abonado + $monabonado;
				
				$total_int=$total_int + $moninteres;
				$total_abo=$total_abo + $monabonado;
			
			
		   $recibos = "SELECT * FROM facturas_notas WHERE codnota = '$codnota' ";
       $result_recibos = $mysqli->query($recibos);
       $row_recibos = $result_recibos->fetch_array();
     
	   $fecrec =  $row_recibos["fecha"];		
	   $fecrec    = implode("-", array_reverse(explode("-", $fecrec))); 
	   $numcomp =  $row_recibos["numcomp"];
	   $numcomp   = substr($numcomp, -10);
	   
	   
	   
	   $saldo_mayor       =  $total_general -$total_abonado  ;
	   
	   		
				            $codigoHTML.=' 
										<tr>
										<td width="40%">  <font size=2 >'."Nota N. ".$numcomp.'</font></td>
										<td width="20%" align="center">  <font size=2 >'.$fecrec.'</font></td>
										<td width="20%" align="right">  <font size="2">'.$monint.'</font></td>
										<td width="20%" align="right">  <font size=2 ></font></td>
										<td width="20%" align="right">  <font size="2">'.$monabo.'</font></td>
										<td width="20%" align="right">  <font size="2">'.number_format($saldo_mayor, 2).'</font></td>
										</tr> ';
	
	}
	
	 }
	 
	 
	 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	   }
      //  if($count > 1) 
          //  $codigoHTML.='</table>';
		  
 
     }


				
	$codigoHTML.='</table>';			
			
$codigoHTML.='
 <p>&nbsp;</p>

 <table width="100%" border="0">

         <tr>
            <td width="40%" height="12" align="left"><font size=2 > TOTALES FINALES</font></td>
              <td width="20%" align="right"><font size=2 ></font></td>
              <td width="20%" align="right"><font size=2 >'.number_format($total_int, 2).'</font></td>
	          <td width="20%" align="right"><font size=2 >'.number_format($total_general, 2).'</font></td>
              <td width="20%" align="right"><font size=2 >'.number_format($total_abo, 2).'</font></td>
			  <td width="20%" align="right"><font size=2 >'.number_format($saldo_mayor, 2).'</font></td>
         </tr>

	

   <p>';
   
$codigoHTML.='</table>';


$codigoHTML.='

   </p>
   <p>&nbsp;</p>
   <p>&nbsp;</p>

    <table width="100%" border="0">
  
     <tr>
	  <td width="10%" align="right"><font size=2 ></font></td>
	   <td width="10%" align="right"><font size=2 ></font></td>
	   <td width="10%" align="right"><font size=2 ></font></td>
       <td align="center">_____________________________</td>
	    <td width="10%" align="right"><font size=2 ></font></td>
		 <td width="10%" align="right"><font size=2 ></font></td>
		 <td width="10%" align="right"><font size=2 ></font></td>
     </tr>
     <tr>
	 	  <td width="10%" align="right"><font size=2 ></font></td>
	   <td width="10%" align="right"><font size=2 ></font></td>
	   <td width="10%" align="right"><font size=2 ></font></td>
      <td align="center"><strong><em><font size=2 >'.$NombreComercial.'</font></em></strong></td>
		<td width="10%" align="right"><font size=2 ></font></td>
		<td width="10%" align="right"><font size=2 ></font></td>
			<td width="10%" align="right"><font size=2 ></font></td>
     </tr>
     <tr>
	 	  <td width="10%" align="right"><font size=2 ></font></td>
	   <td width="10%" align="right"><font size=2 ></font></td>
	   <td width="10%" align="right"><font size=2 ></font></td>
       <td align="center"><strong><em><font size=2 >Firma y sello</font></em></strong></td>
		<td width="10%" align="right"><font size=2 ></font></td>
		<td width="10%" align="right"><font size=2 ></font></td>
		<td width="10%" align="right"><font size=2 ></font></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","7000M");
$dompdf->render();
$dompdf->stream("Historico de Cliente Facturas VRS Recibos.pdf", array("Attachment" => false));

?>
