<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte_Ventas_Clientes_x_articulos.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"];
	// $cdgcli    = $_GET["cdgcli"];
  //   $cdgbar    = $_GET["cdgbar"];
	
	 

     
	 

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


$where="1=1";
$sector="Sector: ";

//if ($cdgbar <> "")    { $where.=" AND x.codigo ='".$cdgbar."'";  }

	 

if (($fechainicio<>"") and ($fechafin<>"")) {


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


	 
			
?>
<!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 Ventas Clientes x Articulos</title>
<style type="text/css">
<!--
.Estilo1 {color: #CCCCCC}
.Estilo4 {font-size: medium}
.Estilo18 {
	color: #3399CC;
	font-weight: bold;
}
-->
</style>
</head>
<body>

<table width="92%" border="0" cellspacing="0" cellpadding="0">
    
    <tr>
      <td><div align="center"></div></td>
      <td colspan="8"  ><div align="center"><font size=5 ><?php echo $NombreComercial?></font></div>
      <div align="center"></div><div align="center"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="8" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="8" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="8" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="8" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="8" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="8"  bgcolor="#66CCFF"><div align="center"><strong><font size="3" >Reporte Ventas Clientes x Articulos </font>  </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="8"   border="1"><div align="center"><font size="3" >Fecha del <?php echo $fecini; ?> Al <?php echo $fecfin; ?></font></div></td>
     </tr>
	 
	 
	      <tr>
       <td></td>
       <td colspan="8"   border="1"><div align="center"><font size="3" > <?php //echo $sector; ?></font></div></td>
     </tr>
	


<?php  
 /*   		 
if ($combotipomoneda == "USD") { 
$where.=" AND x.tipmoneda='USD'"; 
 	
 } // fin con dolares
     		 
elseif ($combotipomoneda <> "USD") { 
//`tipmoneda` != 'USD'

$where.=" AND x.tipmoneda!= 'USD'";   // todos los distintos a USD


 }  
 */  
?>
 <td></td>
    

	 
  <tr>
      <td width="1%"></td>
	  <td width="10%" align="center" bgcolor="#66CCFF"><span class="Estilo4"><strong>Fecha</strong></font></span></td>
	<td width="100%" align="center" bgcolor="#66CCFF"><span class="Estilo4"><strong>Cod. Cliente</strong></font></span></td>
		<td width="30%" align="center" bgcolor="#66CCFF"><span class="Estilo4"><strong>Cliente</strong></font></span></td>
	<td width="10%" align="center" bgcolor="#66CCFF"><span class="Estilo4"><strong>Cod. Articulo</strong></font></span></td>
	<td width="31%" align="center" bgcolor="#66CCFF"><span class="Estilo4"><strong>Articulo</strong></font></span></td>
	<td width="20%" align="center" bgcolor="#66CCFF"><span class="Estilo4"><strong>Cantidad</strong></font></span></td>
		<td width="20%" align="center" bgcolor="#66CCFF"><span class="Estilo4"><strong>Precio Unitario</strong></font></span></td>
    <td width="20%"  align="center"bgcolor="#66CCFF" ><span class="Estilo4"><strong>Total</strong></font></span></td>
  </tr>
  
<?PHP
		

/*
	  $consulta = "SELECT x.codigo, x.dscart, sum(x.cantidad) as cantidad, 
 sum(x.montotlin) as montotlin , x.cdgcli , z.dsccli
 FROM factulinea x ,tmp_clientes z 
 WHERE  x.cdgcli=z.cdgcli   AND ".$where."  GROUP BY  x.codigo , x.cdgcli  ORDER BY x.cdgcli ";
 */
 $consulta="SELECT
	 
     x0.codigo AS codigo,
     x0.dscart AS dscart,
	 x0.cantidad as cantidad,
	 x0.cantidad as cantidad,
	 x0.montotlin as montotlin,
	  x0.cdgcli AS cdgcli,
	  x1.dsccli AS dsccli,
	  x0.fecfac as fecfac

FROM
     factulinea x0 INNER JOIN tmp_clientes x1 ON x0.cdgcli = x1.cdgcli
     INNER JOIN tmp_web_1001 x2 ON x0.codigo = x2.cdgart   WHERE  ".$where."   ORDER BY x0.cdgcli ,  x0.dscart ";
	 
	 

		if ($resultado3 = $mysqli->query($consulta)) 		
		if($dato = $resultado3->fetch_assoc()){ 
      	$cdgcli_actual = $dato['cdgcli'];
		

	   } 
	   

	    $montotlin = 0;
		$cantidad = 0;
					
		$totalcantidad =0;		
		$totalmontotlin = 0;
		
	   
$contador=0;  
if ($resultado = $mysqli->query($consulta)) 

 
    while ($fila = $resultado->fetch_assoc()) {
		$codigo     = $fila["codigo"];
		$cantidad   = $fila["cantidad"];
	    $dscart     = $fila["dscart"];
		$montotlin  = $fila["montotlin"];
		$cdgcli     = $fila["cdgcli"];
		$dsccli     = $fila["dsccli"];
		$fecfac     = $fila["fecfac"];
        $precio     = $montotlin / $cantidad;

		
	if ($cdgcli_actual==$cdgcli) {   // si el codigo de  es distinto al siguiente
	
				
		$totalcantidad =$totalcantidad + $cantidad;		
		$totalmontotlin = $totalmontotlin + $montotlin;

	}
	
	

//		$consult = "SELECT * FROM web_0301 WHERE cdgcli = '$cdgcli' ";
//		$result = $mysqli->query($consult);
//		$fila2 = $result->fetch_array();
//
//         $dsccli = $fila2["dsccli"];
		 
	

          if ($contador==0){ 
?>  
 
      <tr>
        <td></td>
        <td colspan="8" bgcolor="#FFFFFF">&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></td>
        <td colspan="8" bgcolor="#66CCFF"> <div align="center"><strong> <font size=2 ><?php echo $cdgcli." - ".$dsccli ?></font></strong>  <font size=2 ></font>  <font size=1 align="right"></font>  <font size=1 align="left"></font></div></td>
        <td width="7%" align="center">  <font size=1 ></font></td>
        <td width="2%"  align="right">  <font size=1 ></font></td>
        <td width="2%"  align="right">  <font size=1 ></font></td>
		 <td width="2%"  align="right">  <font size=1 ></font></td>
		  <td width="2%"  align="right">  <font size=1 ></font></td>
      </tr>


  
  <?php
  
       
		 } 
		 

	if ($cdgcli <>  $cdgcli_actual  ){ 
				
			 $cdgcli_actual = $fila['cdgcli'];
				 
?> 
 <tr>
   <td></td>
     <td align="left"><strong><font size=1 >Subtotal Ventas </font></strong></td>
   <td align="left"><strong><font size=1 >&nbsp;</font></td>
      <td align="left"><strong><font size=1 >&nbsp;</font></td>
	     <td align="left"><strong><font size=1 >&nbsp;</font></td>
		    <td align="left"><strong><font size=1 >&nbsp;</font></td>

   <td align="right"><strong><font size=1 ><?php  echo number_format($totalcantidad ,2) ?></font></strong></td>
   			   <td align="left"><strong><font size=1 >&nbsp;</font></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($totalmontotlin,2) ?></font></strong></td>
    <td align="left"><strong><font size=1 >&nbsp;</font></td>
 </tr>
 
  <tr>
   <td></td>

   <td align="center">&nbsp;</td>
   <td align="left">&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>
   <td align="right">&nbsp;</td>
   <td width="3%" align="right">&nbsp;</td>
 </tr>
 
	      <tr>
       <td></td>
  <tr>
      <td width="1%"></td>
	  
		<td colspan="8" align="center" bgcolor="#66CCFF"><span class="Estilo4"><font size=2 ><?php echo $cdgcli." - ".$dsccli ?></font></span></td>
  </tr>
        <tr>
	     <td></td>
        <td colspan="4">&nbsp;</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>
		 <td  align="right">  <font size=1 ></font></td>
		  <td  align="right">  <font size=1 ></font></td>
      </tr>

  
 
<?php

		$totalcantidad =0;		
		$totalmontotlin = 0;
		
		$codigo     = $fila["codigo"];
		$cantidad   = $fila["cantidad"];
	    $dscart     = $fila["dscart"];
		$montotlin  = $fila["montotlin"];
		$cdgcli     = $fila["cdgcli"];
		
		
		
		$totalcantidad =$totalcantidad + $cantidad;		
		$totalmontotlin = $totalmontotlin + $montotlin;
		
		
		
		
		
} // fin 	if ($cdgcli <>  $cdgcli_actual  ){ 



?> 
<tr>
   <td></td>
   <td align="center"><font size=1 ><?php echo $fecfac ?></font></td>
   <td align="left"><font size=1 ><?php echo $cdgcli ?></font></td>
   <td align="left"><font size=1 ><?php echo $dsccli ?></font></td>
   <td align="left"><font size=1 ><?php echo $codigo ?></font></td>
   <td align="left"><font size=1 ><?php echo  $dscart ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($cantidad,2) ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($precio,2) ?></font></td>
   <td align="right"><font size=1 ><?php echo number_format($montotlin,2) ?></font></td>
      <td align="right"><font size=1 ><?php   ?></font></td>
 </tr>
  <?php 
	$contador++;
	} //fin while


?>
   </tr>
 <tr>
   <td></td>
   <td align="left"><strong><font size=1 >Subtotal Ventas </font></strong></td>
   <td align="left"><strong><font size=1 >&nbsp;</font></td>
      <td align="left"><strong><font size=1 >&nbsp;</font></td>
	     <td align="left"><strong><font size=1 >&nbsp;</font></td>
		    <td align="left"><strong><font size=1 >&nbsp;</font></td>

   <td align="right"><strong><font size=1 ><?php  echo number_format($totalcantidad ,2) ?></font></strong></td>
   			   <td align="left"><strong><font size=1 >&nbsp;</font></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($totalmontotlin,2) ?></font></strong></td>
    <td align="left"><strong><font size=1 >&nbsp;</font></td>
 </tr>

  <tr>
       <td></td>
     </tr>
</table>




<?php 

 $consulta3="SELECT
	 
     x0.codigo AS codigo,
     x0.dscart AS dscart,
	 x0.cantidad as total_cantidad,
	 x0.montotlin as total_montotlin,
	  x0.cdgcli AS cdgcli,
	  x1.dsccli AS dsccli

FROM
     factulinea x0 INNER JOIN tmp_clientes x1 ON x0.cdgcli = x1.cdgcli
     INNER JOIN tmp_web_1001 x2 ON x0.codigo = x2.cdgart   WHERE  ".$where;
	 


$result3 = $mysqli->query($consulta3);
$fila3 = $result3->fetch_array(); 
  
   $total_cantidad    = $fila3["total_cantidad"];
   $total_montotlin   = $fila3["total_montotlin"];
   



 
	?>
<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="right">&nbsp;</td>
   <td align="right">&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></td>
   <td align="left"><strong><font size=1 ></font></strong></td>
   <td align="left"><strong><font size=1 >&nbsp;</font></td>
   <td align="right"><strong><font size=1 ><?php //echo number_format($total_cantidad ,2)?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php //echo number_format($total_montotlin  ,2)?></font></strong></td>


 </tr>
</table>
</body>
</html>