<?php 


	require_once("../dompdf/dompdf_config.inc.php");
	include ("../conectar.php");
    


$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>';
  
$query="SELECT * FROM web_0000";
$rs_query=mysql_query($query);
$NombreComercial=mysql_result($rs_query,0,"NOMCOM");
$Cedula=mysql_result($rs_query,0,"CEDJUR");
$Direccion=mysql_result($rs_query,0,"DIREMP");
$Telefono=mysql_result($rs_query,0,"NUMTEL");


  
$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>
    ';
   
	 $fecini=$_GET[fechainicio];
	 $fecfin=$_GET[fechafin];
	 $cdgtipven=$_GET[combotipoventa];
	 
	
	 
 $fechainicio= implode("-", array_reverse(explode("-", $fecini))); 
 $fechafin= implode("-", array_reverse(explode("-", $fecfin))); 

					
						
	 
	 $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 Facturacion</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="13%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>Factura</strong></font></td>
        <td width="19%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>Fecha</strong></font></td>
        <td width="75%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Cliente</strong></font></td>
		<td width="10%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>Plazo</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>Imp.Ventas </strong></font></td>
              <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>Total </strong></font></td>
 
      </tr>
	   </table>';
          
 if ($cdgtipven <> "") {
 
      $consulta=mysql_query("SELECT * FROM facturas WHERE cdgtipven = '$cdgtipven' AND fecha  between '$fechainicio' AND '$fechafin'  ORDER BY numfactura ASC" );
        while($dato=mysql_fetch_array($consulta)){
				$codfactura =$dato['codfactura'];
				$numfactura =$dato['numfactura'];
				$fecha = $dato['fecha'];
 				$fecha= implode("-", array_reverse(explode("-", $fecha))); 		
				$cdgcli = $dato['cdgcli'];
				$cdgcli=sprintf('%04d',$cdgcli) ;
				$dsccli = $dato['dsccli'];
				$dsccli = substr($dsccli, 0, 40);
				$pladia = $dato['pladia'];
				$totalfactura = number_format($dato['totalfactura'], 2);
				$subtot = number_format($dato['subtot'], 2);
				$impven = number_format($dato['impven'], 2);
				$destot = number_format($dato['destot'], 2);
			
$codigoHTML.='
   <table width="100%"  rules="none" border="0">
    
      <tr>
        <td width="7%">  <font size=1 >'.$dato['numfactura'].'</font></td>
        <td width="8%">  <font size=1 >'.$fecha.'</font></td>
   	    <td width="10%">  <font size=1 align="left">'.$dato['cdgcli'].'</font></td>
   	    <td width="31%">  <font size=1 align="left">'.$dsccli.'</font></td>
		<td width="6%" align="center">  <font size=1 >'.$dato['pladia'].'</font></td>
        <td width="9%" align="right">  <font size=1 >'.$subtot.'</font></td>
		<td width="9%" align="right">  <font size=1 >'.$destot.'</font></td>
		 <td width="9%" align="right">  <font size=1 >'.$impven.'</font></td>
		<td width="12%" align="right">  <font size=1 >'.$totalfactura.'</font></td>
      </tr>
      
  </table>';
  
  	           } 
   $consulta=mysql_query("SELECT SUM(totalfactura) AS total , SUM(subtot) AS subtotal, SUM(destot) AS descuento , SUM(impven) AS impuesto FROM facturas WHERE  cdgtipven = '$cdgtipven' AND fecha  between '$fechainicio' AND '$fechafin' ");
        while($dato=mysql_fetch_array($consulta)){
			//	$INICIAL = number_format($dato['INICIAL'], 2);
				$subtotal = number_format($dato['subtotal'], 2);
				$descuento  = number_format($dato['descuento'], 2);
				$impuesto  = number_format($dato['impuesto'], 2);
				$total  = number_format($dato['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="120%" height="12" align="left"><font size=2 > TOTALES FINALES</font></td>
              <td width="20%" align="right"><font size=1 >'.$subtotal.'</font></td>

			  <td width="20%" align="right"><font size=1 >'.$descuento.'</font></td>
			 <td width="20%" align="right"><font size=1 >'.$impuesto.'</font></td>
              <td width="20%" align="right"><font size=1 >'.$total.'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
     } 
$codigoHTML.='

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


 }else {
	 
	      $consulta=mysql_query("SELECT * FROM facturas WHERE fecha  between '$fechainicio' AND '$fechafin'  ORDER BY numfactura ASC" );
        while($dato=mysql_fetch_array($consulta)){
				$codfactura =$dato['codfactura'];
				$numfactura =$dato['numfactura'];
				$fecha = $dato['fecha'];
 				$fecha= implode("-", array_reverse(explode("-", $fecha))); 		
				$cdgcli = $dato['cdgcli'];

				$cdgcli=sprintf('%04d', $cdgcli);
				$dsccli = $dato['dsccli'];
				$dsccli = substr($dsccli, 0, 40);
				$pladia = $dato['pladia'];
				$totalfactura = number_format($dato['totalfactura'], 2);
				$subtot = number_format($dato['subtot'], 2);
				$impven = number_format($dato['impven'], 2);
				$destot = number_format($dato['destot'], 2);
			
$codigoHTML.='
   <table width="100%"  rules="none" border="0">
    
      <tr>
        <td width="15%">  <font size=1 >'.$dato['numfactura'].'</font></td>
        <td width="18%">  <font size=1 >'.$fecha.'</font></td>
   	    <td width="10%">  <font size=1 align="left">'.$dato['cdgcli'].'</font></td>
   	    <td width="72%">  <font size=1 align="left">'.$dsccli.'</font></td>
		<td width="5	%" align="center">  <font size=1 >'.$dato['pladia'].'</font></td>
        <td width="20%" align="right">  <font size=1 >'.$subtot.'</font></td>

		<td width="20%" align="right">  <font size=1 >'.$destot.'</font></td>
		<td width="20%" align="right">  <font size=1 >'.$impven.'</font></td>
		<td width="20%" align="right">  <font size=1 >'.$totalfactura.'</font></td>
      </tr>
      
  </table>';
  
  	           } 
   $consulta=mysql_query("SELECT SUM(totalfactura) AS total , SUM(subtot) AS subtotal, SUM(destot) AS descuento , SUM(impven) AS impuesto FROM facturas WHERE fecha  between '$fechainicio' AND '$fechafin' ");
        while($dato=mysql_fetch_array($consulta)){
			//	$INICIAL = number_format($dato['INICIAL'], 2);
				$subtotal = number_format($dato['subtotal'], 2);
				$descuento  = number_format($dato['descuento'], 2);
				$impuesto  = number_format($dato['impuesto'], 2);
				$total  = number_format($dato['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="120%" height="12" align="left"><font size=2 > TOTALES FINALES</font></td>
              <td width="20%" align="right"><font size=1 >'.$subtotal.'</font></td>
       
			  <td width="20%" align="right"><font size=1 >'.$descuento.'</font></td>
			          <td width="20%" align="right"><font size=1 >'.$impuesto.'</font></td>
              <td width="20%" align="right"><font size=1 >'.$total.'</font></td>
         </tr>
       </table></td>
     </tr>
   </table>
   <p>';
   
     } 
$codigoHTML.='

   </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","1024M");
$dompdf->render();
$dompdf->stream("Estado de Cuenta.pdf", array("Attachment" => false));
 
	 
		 }	

?>
