<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte_Comision_facturacion.xls");

 include ("../conexion.php");	

       $parametros = "SELECT * FROM web_0000 ";
       $result_parametros = $mysqli->query($parametros);
       $row_para = $result_parametros->fetch_array();
     
	   $NombreComercial =  $row_para["NOMCOM"];
	   $Razon          =  $row_para["RAZSOC"];
	   $Direccion       =  $row_para["DIREMP"];
	   $Cedula          =  $row_para["CEDJUR"];
	   $Telefono        =  $row_para["NUMTEL"];
  


	 $fecini    = $_GET["fechainicio"];
	 $fecfin    = $_GET["fechafin"];
	 $cdgven    = $_GET["cdgven"];

	 

	 
	 
    $where_notas   = "";
    $where         = "";
	$exento_fac    = 0;
	$gravado_fac   = 0;
	$subtotal_fac  = 0;
	$descuento_fac = 0;
	$impuesto_fac  = 0;
	$total_fac     = 0;	 
	$exento        = 0  ;
	$gravado       = 0 ;
	$subtotal      = 0 ;
	$descuento     = 0  ;
	$impuesto      = 0 ;
	$total         = 0  ;
	
	
		 	 /////////////////        where para facturas  y notas ********************


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



$where="1=1";
//if ($cdglin <> "") { $where.=" AND seguimiento_comicion_facturas.cdglin='$cdglin'"; }
//if ($cdgfam <> "") { $where.=" AND seguimiento_comicion_facturas.cdgfam='$cdgfam'"; }

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

if ($cdgven <> "") { $where.=" AND cdgven='$cdgven'"; }

 if ($cdgven=="") {
	 
	 $nomcomusu="TODOS LOS VENDEDORES";
	 
	     } else {


	   $query_codcaj="SELECT * FROM usuarios  WHERE codusu=$cdgven ";
       $resultado = $mysqli->query( $query_codcaj);
       $row = $resultado->fetch_array();
        
		$nomcomusu    = $row["nomcomusu"];
     }
		
		
$totalfacturas=0;
  
  if ($result = $mysqli->query("SELECT * FROM seguimiento_comicion_facturas WHERE   AND ".$where)) {
   $filas = $result->num_rows;
   $result->close();
}

					
?>
<!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>Reporte Detallado Comision por Recuperacion CXC</title>
<style type="text/css">
<!--
.Estilo1 {color: #CCCCCC}
.Estilo4 {font-size: medium}
.Estilo6 {
	color: #000000;
	font-weight: bold;
}
.Estilo17 {color: #000000; font-weight: bold; font-size: 14px; }
-->
</style>
</head>
<body>
<table width="92%" border="0" cellspacing="0" cellpadding="0">
    
    <tr>
      <td><div align="center"></div></td>
      <td colspan="9"  ><div align="center"><font size=8 ><?php echo $NombreComercial?></font></div>
      <div align="center"></div><div align="center"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="9" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="9" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="9" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="9" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="9" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="9"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Detallado Comisión por Facturacion </font>  </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="9"   border="1"><div align="center"><font size="3" >Fecha del <?php echo $fecini; ?> Al <?php echo $fecfin; ?> -  <?php echo $nomcomusu; ?></font></div></td>
     </tr>
     
  <tr>
      <td width="1%"></td>

    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>FACTURA</strong></font></span></td>

	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>FECHA FACTURA</strong></font></span></td>

	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>CLIENTE</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>MONTO FAC</strong></font></span></td>

	<td width="20%" align="center" bgcolor="skyblue"><span class="Estilo4"><strong>PORCENTAJE</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>MONTO COMISION</strong></font></span></td> 

  </tr>
  
<?PHP

$montoven=0; 
$montototalabono=0;
				

			
 $consulta2="SELECT * FROM seguimiento_comicion_facturas  WHERE  ".$where." ORDER BY  cdgven,  numfac ASC " ;
 
		  		
		
		if ($resultado3 = $mysqli->query($consulta2)) 		
		if($dato = $resultado3->fetch_assoc()){ 
      	$cdgven_actual = $dato['cdgven'];

	   } 
		

      
	    $contador=0;	
		if ($resultado2 = $mysqli->query($consulta2)) 
		
		  
  		while ($dato1 = $resultado2->fetch_assoc()) {     //  si encuentra datos
		$numfac      = $dato1['numfac'];
		
		
				$fecfac      = $dato1['fecfac'];
 				$fecfac= implode("-", array_reverse(explode("-", $fecfac))); 	
				
				
				
	            $cdgven     = $dato1['cdgven'];
				$dscven     = $dato1['dscven'];
				$cdgcli	     = $dato1['cdgcli'];
		     	$dsccli     = $dato1['dsccli'];
				
			    $porcentaje     = $dato1['porcentaje'];
				$monto   = $dato1['monto'];
				$totalfactura   = $dato1['totalfactura'];
				

				
				
				$dsccli     = substr($dsccli, 0, 80);	


				if ($cdgven_actual==$cdgven) {   // si el codigo de famiia es distinto al siguiente
				
				$monto   = $dato1['monto'];
				$totalfactura     = $dato1["totalfactura"];

				
			
				$montoven=$montoven+$monto; 
				$montototalabono = $montototalabono  + $totalfactura;
				
	
	  } 	    //  cierra codigo de familia distinto 	
	

          if ($contador==0){ 
?>  
 
      <tr>
	     <td></td>
        <td> <strong> <font size=2 ><?php echo $cdgven." - ".$dscven ?></font></strong></td>
        <td>  <font size=2 ></font></td>
   	    <td>  <font size=1 align="right"></font></td>
   	    <td>  <font size=1 align="left"></font></td>
		<td align="center">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>


      </tr>


  
  <?php
  
       
		 } 
		 

	if ($cdgven <>  $cdgven_actual  ){ 
				
			 $cdgven_actual = $dato1['cdgven'];
				 
?>				 
 <tr>
 </tr>
 <tr>
   <td></td>
   <td align="left"><strong><font size=1 >Subtotal Vendedor</font></strong></td>
   <td align="left"><strong><font size=1 >&nbsp;</font></td>
   <td align="center"><strong><font size=1 ></font></strong></td>

    <td align="right"><strong><font size=1 ><?php echo number_format($montototalabono,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ></font></strong></td>

   <td align="right"><strong><font size=1 ><?php echo number_format($montoven,2) ?></font></strong></td>

 </tr>
   <tr>

 
    </tr>
  <tr>
      <td width="1%"></td>

    <td width="20%"  align="center"bgcolor="skyblue" ><span class="Estilo4"><strong>FACTURA</strong></font></span></td>

    <td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>FECHA FACTURA</strong></font></span></td>

	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>CLIENTE</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue" ><span class="Estilo4"><strong>MONTO FAC</strong></font></span></td>

	<td width="20%" align="center" bgcolor="skyblue"><span class="Estilo4"><strong>PORCENTAJE</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>MONTO COMISION</strong></font></span></td> 
  </tr>
  
   <tr>
	     <td></td>
        <td> <strong> <font size=2 ><?php echo $cdgven." - ".$dscven ?></font></strong></td>
        <td>  <font size=2 ></font></td>
   	    <td>  <font size=1 align="right"></font></td>
   	    <td>  <font size=1 align="left"></font></td>
		<td align="center">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>
        <td  align="right">  <font size=1 ></font></td>


      </tr>
		
	  <?php	
				$montoven =0;
				$montototalabono=0;


				$monto   = $dato1['monto'];
				$totalfactura     = $dato1["totalfactura"];

				
			
				$montoven=$montoven+$monto; 
				$montototalabono = $montototalabono  + $totalfactura;
	 } 		
	 
	    
 ?> 
  
  <tr>
   <td></td>
   <td align="center"><font size=1 ><?php echo "N.".$dato1["numfac"] ?></font></td>

   	<?php	 // $dscart = substr($dato1["dscart"], 0,40);  ?>	
   <td align="center"><font size=1 ><?php echo $fecfac ?></font></td>

   <td align="left"><font size=1 ><?php echo $dsccli ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($totalfactura,2)?></font></td>

   <td align="center"><font size=1 ><?php echo number_format($porcentaje,2) ?></font></td>
   <td align="right"><font size=1 ><?php  echo number_format($monto,2) ?></font></td>

 </tr>
 
  <?php
      $contador++;
  	        
		 } 
 ?> 		
  
	 </tr>
 <tr>
  <tr>
 </tr>
   <td></td>
   <td align="left"><strong><font size=1 >Subtotal Vendedor</font></strong></td>
   <td align="left"><strong><font size=1 >&nbsp;</font></td>
   <td align="center"><strong><font size=1 ></font></strong></td>


    <td align="right"><strong><font size=1 ><?php echo number_format($montototalabono,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ></font></strong></td>

   <td align="right"><strong><font size=1 ><?php echo number_format($montoven,2) ?></font></strong></td>

 </tr>	 
	 <tr>	 
		
		 </tr>	

	<?php	
	
   $consulta2= "SELECT  SUM(monto) AS monto , SUM(totalfactura) AS totalfactura FROM seguimiento_comicion_facturas  WHERE  ".$where." ORDER BY  cdgven,  dsccli ASC " ;
   
	if ($resultado2 = $mysqli->query($consulta2)) 

  	 while ($dato = $resultado2->fetch_assoc()) {
  						
        $monto   = $dato["monto"];
		$totalfactura   = $dato["totalfactura"];
		
		

	} 				

  ?>
  
</table>

<table width="92%" border="0" cellspacing="0" cellpadding="0">

 <tr>
   <td></td>
   <td align="left">&nbsp;</td>
   <td align="center">&nbsp;</td>
   <td align="left">&nbsp;</td>
   <td align="center">&nbsp;</td>
   <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   <td align="right">&nbsp;</td>
   
 </tr>
 <tr>

     <td width="1%"></td>
      <td align="left"><strong><font size=1 >TOTAL COMISION</font></strong></td>
   <td align="center"><strong><font size=1 ></font></strong></td>

      <td align="center"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($totalfactura,2) ?></font></strong></td>

   <td align="right"><strong><font size=1 ></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($monto, 2);?></font></strong></td>

 </tr>
</table>






</body>
</html>