<?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"];
     $cdgprovin    = $_GET["cdgprovin"];
	 $cdgcanton    = $_GET["cdgcanton"];
	 $cdgdistri    = $_GET["cdgdistri"];
     $cdgbarrio    = $_GET["cdgbarrio"];
	
	 

     
	 

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


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

if ($cdgcli <> "")    { $where.=" AND x.cdgcli ='".$cdgcli."'";  }
if ($cdgprovin >=1) { $where.=" AND z.cdgprovin ='".$cdgprovin."'"; 
	
	$query = "SELECT * FROM hac_provincias where cdgprovin = $cdgprovin ";
	$resultado=$mysqli->query($query);
	$row = $resultado->fetch_array();
	$dscprovin = $row['dscprovin'];
	
	$sector.= "Provincia: ".$dscprovin;


	 }
	 
	 
if ($cdgcanton >= 1) { $where.=" AND z.cdgcanton ='".$cdgcanton."'"; 

	$query2 = "SELECT * FROM hac_cantones where  cdgprovin = $cdgprovin AND  cdgcanton = $cdgcanton   ";
	$resultado2=$mysqli->query($query2);
	$row2 = $resultado2->fetch_array();
	$dsccanton = $row2['dsccanton'];
	
	$sector.= " - Canton: ".$dsccanton;

	}
	
if ($cdgdistri >= 1) { $where.=" AND z.cdgdistri ='".$cdgdistri."'";

	$query3 = "SELECT * FROM hac_distritos where cdgprovin = $cdgprovin AND  cdgcanton = $cdgcanton   AND  cdgdistri = $cdgdistri   ";
	$resultado3=$mysqli->query($query3);
	$row3 = $resultado3->fetch_array();
	$dscdistri = $row3['dscdistri']; 
	
	$sector.= " - Distrito: ".$dscdistri;
	
	}
	
if ($cdgbarrio >= 1) { $where.=" AND z.cdgbarrio ='".$cdgbarrio."'";

	$query4 = "SELECT * FROM hac_barrios where cdgprovin = $cdgprovin AND  cdgcanton = $cdgcanton   AND  cdgdistri = $cdgdistri AND cdgbarrio = $cdgbarrio  ";
	$resultado4=$mysqli->query($query4);
	$row4 = $resultado4->fetch_array();
	$dscbarrio = $row4['dscbarrio'];
	
	$sector.= " - Barrio: ".$dscbarrio;
	
	 }

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


			
	 $where.=" AND x.fecfac between '".$fechainicio."' AND '".$fechafin."'";
} else {
	if ($fechainicio<>"") {
	 	$where.=" AND x.fecfac>='".$fechainicio."'";
	} else {
		if ($fechafin<>"") {
		$where.=" AND x.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="4"  ><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="4" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="4" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="4" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="4" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="4" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="4"  bgcolor="#66CCFF"><div align="center"><strong><font size="3" >Reporte Ventas Clientes x Articulos </font>  </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="4"   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="4"   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>Codigo</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>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, z.cdgprovin , z.cdgcanton
 FROM factulinea x ,WEB_0301 z 
 WHERE  x.cdgcli=z.cdgcli   AND ".$where."  GROUP BY  x.codigo , x.cdgcli  ORDER BY x.cdgcli ";





		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"];


		
	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="4" 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="4" 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 </ </font></strong></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="right"><strong><font size=1 ><?php  echo number_format($totalmontotlin ,2) ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php ?></font></strong></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="4" 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="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($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 Comprobantes </font></strong></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="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 


	  $consulta = "SELECT x.codigo, x.dscart, sum(x.cantidad) as total_cantidad, 
 sum(x.montotlin) as total_montotlin , x.cdgcli , z.dsccli
 FROM factulinea x ,WEB_0301 z 
WHERE  x.cdgcli=z.cdgcli  AND ".$where;
$result3 = $mysqli->query($consulta);
$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 >TOTAL GENERAL</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>