<?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["RAZSOC"];
	   $razsoc          =  $row_para["RAZSOC"];
	   $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>';
  
   
   
	 $fecini=$_GET["fechainicio"];
	 $fecfin=$_GET["fechafin"];
	 $cdgtipven=$_GET["combotipoventa"];
	 $cdgtipcxp=$_GET["combotipocxp"];
	 
	 $fechainicio= implode("-", array_reverse(explode("-", $fecini))); 
 	$fechafin= implode("-", array_reverse(explode("-", $fecfin))); 
	
	$where="1=1";
if ($cdgtipven <> "") { $where.=" AND  web_2201.cdgtipven='$cdgtipven'"; }
if ($cdgtipcxp <> "") { $where.=" AND  web_2201.cdgtipcxp='$cdgtipcxp'"; }

if (($fechainicio<>"") and ($fechafin<>"")) {
	$where.=" AND web_2201.fecpro between '".$fechainicio."' AND '".$fechafin."'";
} else {
	if ($fechainicio<>"") {
		$where.=" and  web_2201.fecpro >='".$fechainicio."'";
	} else {
		if ($fechafin<>"") {
			$where.=" and  web_2201.fecpro <='".$fechafin."'";
		}
	}
}

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

//$where.=" ORDER BY fecpro ASC";					
if ($cdgtipcxp=="") {
	 
	$dsccxp="TODAS LAS CUENTAS";
	 
	     } else {


	 
	   $query_tipocxp="SELECT * FROM tipo_cxp WHERE cdgcxp=$cdgtipcxp ";
       $resultado = $mysqli->query($query_tipocxp);
       $row = $resultado->fetch_array();
	   
	   $dsccxp    = $row["dsccxp"];
     }	
	 
	 $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>Reporte Analitico de '.$dsccxp.' </strong></font></td>
      </tr>
      <tr>
        <td height="14" colspan="3" align="center">Fecha del <font size="2" >'.$fecini.'</font> al <font size="2" >'.$fecfin.'</font></td>
      </tr>
  </table>
  ';
	
	
	 $codigoHTML.='
<table width="100%" role="rows" cellpadding="0" cellspacing="0" border="1">
    <tr>
    
        <td width="24%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Factura</strong></font></td>
        <td width="18%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>
        <td width="18%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>F.Proceso</strong></font></td>
		<td width="5%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Plazo</strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Exento </strong></font></td> 
	    <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Sub.Gravado </strong></font></td>
	    <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Subtotal </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Descuento </strong></font></td>
		<td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Impuesto </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Flete </strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Otros </strong></font></td>
              <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total </strong></font></td>
 
    </tr>
  </table>';
        
		  $consulta2="SELECT  *  FROM web_2201,proveedores WHERE web_2201.cdgpro=proveedores.cdgpro AND web_2201.borrado=0   AND ".$where. "  ORDER BY web_2201.dscpro,web_2201.fecfac ASC " ;
				  		 
		  		
		if ($resultado3 = $mysqli->query($consulta2)) 		
		if($dato = $resultado3->fetch_assoc()){ 
      	 $cdgpro_actual = $dato['cdgpro']; } 
         $cdgpro_actual = sprintf("%04d",$cdgpro_actual);
	    $contador=0;
		$count     = 0;		
		if ($resultado2 = $mysqli->query($consulta2)) 
		
		  
  		while ($dato1 = $resultado2->fetch_assoc()) {     //  si encuentra datos


        $fecpro        = $dato1["fecpro"];
		$fecpro        = implode("-", array_reverse(explode("-", $fecpro)));
		$fecfac        = $dato1["fecfac"];
		$fecfac        = implode("-", array_reverse(explode("-", $fecfac)));
		$numfac        = $dato1["numfac"];
		$codfactura    = $dato1["codfactura"];
		$cdgpro        = $dato1["cdgpro"];
		$cdgpro        = sprintf("%04d",$cdgpro); 
		$dscpro        = $dato1["dscpro"];
		$dscpro        = substr($dscpro, 0, 45);
		$pladia        = $dato1["pladia"];
		$subgra        = $dato1["subgra"];
		$subexe        = $dato1["subexe"];
		$totfac        = $dato1["totfac"];
		$subfac        = $dato1["subfac"];
		$flefac        = $dato1["flefac"];
		$impfac        = $dato1["impfac"];
		$otrfac        = $dato1["otrfac"];
		$desfactot     = $dato1["desfactot"];


          if ($contador==0){ 


  $codigoHTML.='
  <table width="100%"  rules="none" border="0">
    
      <tr>
        <td width="17%" colspan="6" > <strong> <font size=2 >'.$cdgpro.' - '.$dscpro.'</font></strong></td>


        <td width="20%" align="right">  <font size=1 ></font></td>
		<td width="20%" 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>
		<td width="20%" align="center"> <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 ($cdgpro <>  $cdgpro_actual  ){ 
				
				$cdgpro_actual = $dato1['cdgpro'];
			    $cdgpro_actual = sprintf("%04d",$cdgpro_actual);

				 $codigoHTML.='

     
      <tr>
 		<td width="17%" colspan="4" height="12" align="right"><font size=2 ></font></td>
		
		<td width="20%" align="right">  <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>
        <td width="20%" align="right">  <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>
		<td width="20%" align="right">  <font size=1 >_____________</font></td>
		<td width="20%" align="right">  <font size=1 >_____________</font></td>
      	  </tr>

   <tr>
  		<td width="17%" colspan="4"  align="right"><strong><font size=2 > Subtotal Proveedor</font></strong></td>

		<td width="20%" align="right">  <strong><font size=1 >'.number_format($subexepro, 2).'</font></strong></td>
        <td width="20%" align="right">  <strong><font size=1 >'.number_format($subgrapro, 2).'</font></strong></td>
	    <td width="20%" align="right">  <strong><font size=1 >'.number_format($subpro, 2).'</font></strong></td>
        <td width="20%" align="right">  <strong><font size=1 >'.number_format($desprotot, 2).'</font></strong></td>
		<td width="20%" align="right">  <strong><font size=1 >'.number_format($imppro, 2).'</font></strong></td>
		<td width="20%" align="right">  <strong><font size=1 >'.number_format($flepro, 2).'</font></strong></td>
		<td width="20%" align="right">  <strong><font size=1 >'.number_format($otrpro, 2).'</font></strong></td>
		<td width="20%" align="right">  <strong><font size=1 >'.number_format($totpro, 2).'</font></strong></td>
      </tr>';
  
  $codigoHTML.='

    
      <tr>
        <td width="17%" colspan="6" > <strong> <font size=2 >'.$cdgpro.' - '.$dscpro.'</font></strong></td>


        <td width="20%" align="right">  <font size=1 ></font></td>
		<td width="20%" 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>
		<td width="20%" align="center"> <font size=1 ></font></td>
        <td width="20%" align="right">  <font size=1 ></font></td>


      </tr>';
				$subexepro = "0";
				$subgrapro = "0";
				$subpro    = "0";
				$desprotot = "0";
				$imppro    = "0";
				$flepro    = "0";
				$otrpro    = "0";
				$totpro    = "0";
				
			 } 
				
				
				
				$subgra        = $dato1["subgra"];
				$subexe        = $dato1["subexe"];
				$totfac        = $dato1["totfac"];
				$subfac        = $dato1["subfac"];
				$flefac        = $dato1["flefac"];
				$impfac        = $dato1["impfac"];
				$otrfac        = $dato1["otrfac"];
				$desfactot     = $dato1["desfactot"];
				
				$subexepro=$subexepro+$subexe;
				$subgrapro=$subgrapro+$subgra;
				$subpro=$subpro+$subfac;
				$desprotot=$desprotot+$desfactot;
				$imppro=$imppro+$impfac;
				$flepro=$flepro+$flefac;
				$otrpro=$otrpro+$otrfac;
				$totpro=$totpro+$totfac;
			
			

				

$codigoHTML.='

    
      <tr>
        <td width="24%">  <font size=1 >'.$numfac.'</font></td>
        <td width="18%" align="center">  <font size=1  >'.$fecfac.'</font></td>
   	    <td width="18%" align="center">  <font size=1  >'.$fecpro.'</font></td>
		<td width="5%" align="center">  <font size=1 >'.$pladia.'</font></td>
        <td width="20%" align="right">  <font size=1 >'.number_format($subexe, 2).'</font></td>
		<td width="20%" align="right">  <font size=1 >'.number_format($subgra, 2).'</font></td>
		<td width="20%" align="right">  <font size=1 >'.number_format($subfac, 2).'</font></td>
        <td width="20%" align="right">  <font size=1 >'.number_format($desfactot, 2).'</font></td>
		<td width="20%" align="right">  <font size=1 >'.number_format($impfac, 2).'</font></td>
		<td width="20%" align="right">  <font size=1 >'.number_format($flefac, 2).'</font></td>
		<td width="20%" align="right">  <font size=1 >'.number_format($otrfac, 2).'</font></td>
		<td width="20%" align="right">  <font size=1 >'.number_format($totfac, 2).'</font></td>
      </tr>';
  
  
  
  $contador++;
	  $count++;
       }
   
        if($count > 1) 
            $codigoHTML.='</table>';
			
			
 

    


	  
  	        
		

	
 $consulta2= ("SELECT SUM(totfac) AS total , SUM(subfac) AS subtotal, SUM(desfactot) AS descuento , SUM(impfac) AS impuesto , SUM(flefac) AS flete, SUM(otrfac) AS otros, SUM(subexe) AS subexento, SUM(subgra) AS subgravado FROM web_2201 WHERE borrado=0 AND ".$where );

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

  	 while ($dato = $resultado2->fetch_assoc()) {

				$flete      = $dato['flete'];
				$otros      = $dato['otros'];
				$subtotal   = $dato['subtotal'];
				$descuento  = $dato['descuento'];
				$impuesto   = $dato['impuesto'];
				$total      = $dato['total'];
				$subexento  = $dato['subexento'];
				$subgravado = $dato['subgravado'];
	 } 		
	
	 
				 $codigoHTML.='
   <table width="100%"  rules="none" border="0">
      <tr>
 		<td width="65%" colspan="4" height="12" align="right"><font size=2 ></font></td>
		
		<td width="20%" align="right">  <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>
        <td width="20%" align="right">  <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>
		<td width="20%" align="right">  <font size=1 >_____________</font></td>
		<td width="20%" align="right">  <font size=1 >_____________</font></td>
      </tr>

      <tr>
  		<td width="65%" colspan="4"  align="right"><strong><font size=2 > Subtotal Proveedor</font></strong></td>

		<td width="20%" align="right">  <strong><font size=1 >'.number_format($subexepro, 2).'</font></strong></td>
        <td width="20%" align="right">  <strong><font size=1 >'.number_format($subgrapro, 2).'</font></strong></td>
	    <td width="20%" align="right">  <strong><font size=1 >'.number_format($subpro, 2).'</font></strong></td>
        <td width="20%" align="right">  <strong><font size=1 >'.number_format($desprotot, 2).'</font></strong></td>
		<td width="20%" align="right">  <strong><font size=1 >'.number_format($imppro, 2).'</font></strong></td>
		<td width="20%" align="right">  <strong><font size=1 >'.number_format($flepro, 2).'</font></strong></td>
		<td width="20%" align="right">  <strong><font size=1 >'.number_format($otrpro, 2).'</font></strong></td>
		<td width="20%" align="right">  <strong><font size=1 >'.number_format($totpro, 2).'</font></strong></td>
      </tr>

      
  </table>';
  	
	
$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="65%" colspan="4"  height="12" align="left"><strong><font size=2 > TOTALES FINALES</font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >'.number_format($subexento, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($subgravado, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($subtotal, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($descuento, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($impuesto, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($flete, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($otros, 2).'</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >'.number_format($total, 2).'</font></strong></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   

	/////////////////////////////////////////////////////////////////  CALCULA TARIFAS FACTURACION  ///////////////////////////////////////

$consulta3= ("SELECT  *  FROM web_2201,proveedores WHERE web_2201.cdgpro=proveedores.cdgpro AND web_2201.borrado=0   AND ".$where. "  ORDER BY web_2201.dscpro,web_2201.fecfac ASC" );
	  $TOTAL_I0R =  0 ;
	  $TOTAL_I1R =  0 ;
	  $TOTAL_I2R = 0 ;
	  $TOTAL_I4R =  0 ;
	  $TOTAL_I0T =  0 ;
	  $TOTAL_I4T =  0 ;
	  $TOTAL_I8T =  0 ;
	  $TOTAL_I13 =  0 ;
	  
	if ($resultado3 = $mysqli->query($consulta3)) 


  	 while ($dato3 = $resultado3->fetch_assoc()) {			
	 $numfac   = $dato3['numfac'];
	 $cdgpro   = $dato3['cdgpro'];


       $compras = "SELECT * FROM facturas_compras WHERE   numfac = '$numfac'  AND  cdgpro = '$cdgpro'  ";
       $result_compras = $mysqli->query($compras);
       $row_compras = $result_compras->fetch_array();
        $filas_compras = $result_compras->num_rows;
	   

if ( $filas_compras  > 0) { 
   $codfactura =  $row_compras["codfactura"]; }else {  $codfactura  = "0"; }



    $consulta4= ("SELECT codfactura, implin, cdgtar, cdgimp FROM factulinea_compra WHERE  codfactura = '$codfactura' AND cdgimp = '1' ");

	if ($resultado4 = $mysqli->query($consulta4)) 

	 
  	 while ($dato4 = $resultado4->fetch_assoc()) {
	 $I0R =  0;
	 $I1R =  0;
	 $I2R =  0;
	 $I4R =  0;
	 $I0T =  0;
	 $I4T =  0;
	 $I8T =  0;
	 $I13 =  0;

				$impuesto    = $dato4['implin'];
			  	$cdgtar      = $dato4['cdgtar'];
		
	     if ($cdgtar == "01") {	$I0R =  $impuesto;
	}elseif ($cdgtar == "02") { $I1R =  $impuesto;
	}elseif ($cdgtar == "03") { $I2R =  $impuesto;
	}elseif ($cdgtar == "04") { $I4R =  $impuesto;
	}elseif ($cdgtar == "05") { $I0T =  $impuesto;
	}elseif ($cdgtar == "06") { $I4T =  $impuesto;
	}elseif ($cdgtar == "07") { $I8T =  $impuesto;
	}elseif ($cdgtar == "08") { $I13 =  $impuesto;
	} 
	
	  $TOTAL_I0R =  $TOTAL_I0R + $I0R ;
	  $TOTAL_I1R =  $TOTAL_I1R + $I1R ;
	  $TOTAL_I2R =  $TOTAL_I2R + $I2R ;
	  $TOTAL_I4R =  $TOTAL_I4R + $I4R ;
	  $TOTAL_I0T =  $TOTAL_I0T + $I0T ;
	  $TOTAL_I4T =  $TOTAL_I4T + $I4T ;
	  $TOTAL_I8T =  $TOTAL_I8T + $I8T ;
	  $TOTAL_I13 =  $TOTAL_I13 + $I13 ;
	
		
	 } 		
	


	 } 		

   
   
   $codigoHTML.='
   <table width="100%" height="15" role="rows" cellpadding="0" cellspacing="0" border="1">
     <tr>
       <td height="15"><table width="100%"  cellpadding="0" cellspacing="0" border="1" >
         <tr>
           <td colspan="4"  height="12" align="left">&nbsp;</td>
           <td colspan="4"  bgcolor="#CCCCCC" align="CENTER">REDUCIDAS</td>
           <td colspan="3" bgcolor="#CCCCCC"  align="CENTER">TRANSITORIAS</td>
           <td align="CENTER" bgcolor="#CCCCCC" >GENERAL</td>
         </tr>
         <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > </font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >1%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >2%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >0%</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >4%</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >8%</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >13%</font></strong></td>
         </tr>

		 
		 <tr>
              <td width="65%" colspan="4"  height="12" align="left"><strong><font size=2 > TOTAL IMPUESTOS</font></strong></td>

              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I1R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I2R, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4R, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I0T, 2).'</font></strong></td>
              <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I4T, 2).'</font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I8T, 2).'</font></strong></td>
		      <td width="20%" align="right"><strong><font size=1 >'.number_format($TOTAL_I13, 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));
 


?>
