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


    $where         = "";
	

	$totnetcli    = "0";
	$totsalcli    = "0";
				
	 $hoy    =date("y-m-d");
$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"];
  
$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>';
  
   
   

$cdgtipven   =$_GET["combotipoventa"];
$fecini =$_GET["fechainicio"];
$fecfin    =$_GET["fechafin"];

$gesven=$_GET["vendedor"];

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


$where="1=1";
if ($gesven <> "")    { $where.=" AND web_0301.gesven='$gesven'"; }
if ($cdgtipven <> "") { $where.=" AND cdgtipven='$cdgtipven'"; }

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

//$where.=" ORDER BY fecven ASC";					
if ($gesven=="") {	 
	 $nomcomusu="TODOS LOS GESTORES";
	 
	     } else {
		
$consulta = "SELECT * FROM usuarios WHERE codusu='$gesven'";
$resultado = $mysqli->query($consulta);
$row = $resultado->fetch_array();
 $nomcomusu = $row["usu"];
     }	
	 
 
 //$where.=" AND facturas.estrechazo=0";
 	 
	 if ( $cdgtipven==2){ $tipo=" CREDITO"; };
	 if ( $cdgtipven==1){ $tipo=" CONTADO"; };
	 if ( $cdgtipven==""){ $tipo=" TODAS "; };
 
	 $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 Detallado Gestor de Pedidos '.$nomcomusu.'  </strong></font></td>
      </tr>
	       <tr>
      <td width="100%" height="21" colspan="3" align="center"><font size=3 ><strong>Tipo '.$tipo.' </strong></font></td>
      </tr>
      <tr>
        <td width="100%"  height="11" 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="20%"  align="center"bgcolor="#CCCCCC" ><font size=1 ><strong>FACTURA</strong></font></td>
        <td width="20%" align="center" bgcolor="#CCCCCC" ><font size=1 ><strong>FECHA FACTURA</strong></font></td>
        <td width="20%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong></strong></font></td>
		<td width="20%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong>PLAZO</strong></font></td>
		<td width="20%" align="center" bgcolor="#CCCCCC"><font size=1 ><strong></strong></font></td>
        <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong></strong></font></td> 
	    <td width="20%" align="right" bgcolor="#CCCCCC"><font size=1 ><strong>TOTAL</strong></font></td>

 
    </tr>
  </table>';
        
		  $consulta2="SELECT  *  FROM pedidos,web_0301 WHERE pedidos.cdgcli=web_0301.cdgcli   AND ".$where. "  ORDER BY pedidos.dsccli,pedidos.numfactura 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


       // $fecven        = $dato1["FECVEN"];
	//	$fecven        = implode("-", array_reverse(explode("-", $fecven)));
		$fecfac        = $dato1["fecha"];
		$fecfac        = implode("-", array_reverse(explode("-", $fecfac)));
		$numfac        = $dato1["numfactura"];	
		$cdgcli        = $dato1["cdgcli"];
		$cdgcli        = sprintf("%04d",$cdgcli); 
		$dsccli        = $dato1["dsccli"];
		$dsccli        = substr($dsccli, 0, 45);
		$pladia        = $dato1["pladia"];
		//$totnet        = $dato1["totcompro"];
		$totnet        = $dato1["totalfactura"];
		
		//$salact        = $dato1["saldo"];
	
		//$vencimiento        = $dato1["FECVEN"];
		//$segundos=strtotime($hoy) - strtotime($vencimiento);
		//$dias_vencido=intval($segundos/60/60/24);
	

          if ($contador==0){ 


  $codigoHTML.='
  <table width="100%"  rules="none" border="0">
    
      <tr>
        <td width="80%"  colspan="4" > <strong> <font size=2 >'.$cdgcli.' - '.$dsccli.'</font></strong></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>


      </tr>

      
  </table>';


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


				 $codigoHTML.='

     
      <tr>
 		<td width="20%"  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>
      	  </tr>

   <tr>
  		<td width="20%"  colspan="5" height="12" align="right"><strong><font size=2 > Subtotal Cliente</font></strong></td>
	
		<td width="20%" align="right">  <strong><font size=1 ></font></strong></td>
		<td width="20%" align="right">  <strong><font size=1 >'.number_format($totnetcli, 2).'</font></strong></td>
      </tr>
  

    
      <tr>
        <td width="20%"  colspan="4" ><strong>  <font size=2 >'.$cdgcli.' - '.$dsccli.'</font></strong></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>


      </tr>';
	  
	  			$cdgcli_actual = $dato1['CDGCLI'];
			    $cdgcli_actual = sprintf("%04d",$cdgcli_actual);

				$totnetcli    = "0";
			//	$totsalcli    = "0";
				
			 } 
				
				
				
		

				$totnetcli=$totnetcli+$totnet;
			//	$totsalcli=$totsalcli+$salact;
			
			


            $codigoHTML.='   
				


    
      <tr>
        <td width="20%"align="center">  <font size=1 >'.$numfac.'</font></td>
        <td width="20%"align="center">  <font size=1 >'.$fecfac.'</font></td>
   	    <td width="20%"align="center">  <font size=1 align="right"></font></td>
		<td width="20%" align="center">  <font size=1 >'.$pladia.'</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 >'.number_format($totnet, 2).'</font></td>

                  </tr> ';
				  
				  
	  
     	    $contador++;
			
			$count++;	
			  }



			
			

	 
				 $codigoHTML.='

   <tr>
 		<td width="20%" 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>
      	  </tr>

   <tr>
  		<td width="20%" colspan="5" height="12" align="right"><strong><font size=2 > Subtotal Cliente</font></strong></td>
	
		<td width="20%" align="right">  <strong><font size=1 ></font></strong></td>
		<td width="20%" align="right">  <strong><font size=1 >'.number_format($totnetcli, 2).'</font></strong></td>
      </tr>';
 
	 
	 				         if($count > 1) 
            $codigoHTML.='</table>';
       }

			
				
 $consulta2= ("SELECT  sum(totalfactura) as totalfactura, sum(subtot) as subtot, sum(destot) as destot, sum(impven) as impven  FROM pedidos,web_0301 WHERE pedidos.cdgcli=web_0301.cdgcli   AND ".$where. "  ORDER BY pedidos.dsccli,pedidos.numfactura ASC " );

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

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

			//	$saldo      = $dato['saldo'];
				$total      = $dato['totalfactura'];
	
	 } 		
	
			
	
$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="60%" colspan="4" height="12" align="left"><strong><font size=2 > TOTAL FINAL</font></strong></td>

			  <td width="20%" align="right"><strong><font size=1 ></font></strong></td>
			    <td width="20%" align="right"><strong><font size=1 ></font></strong></td>
				  <td width="20%" align="right"><strong><font size=1 ></font></strong></td>
			  <td width="20%" align="right"><strong><font size=1 ></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>';
	
    
$codigoHTML.='

   </p>


</div>
</body>
</html>';

$codigoHTML=utf8_decode($codigoHTML);
$dompdf=new DOMPDF();
//$dompdf->set_paper( "A4", "landscape" ); // a4,horizontal
$dompdf->set_paper( "letter", "portrait" ); // 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("Reporte_cxc.pdf", array("Attachment" => false));
 


?>
