<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte_Proformas.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"];
	 $cdgtipven = $_GET["combotipoventa"];
	 $cdgcaj    = $_GET["cdgcaj"];
	 $cdgcli    = $_GET["cdgcli"];
	 $dsccli    = $_GET["dsccli"];
	 
	 
	$exento_tot    = 0;
	$gravado_tot   = 0;
	$subtotal_tot  = 0;
	$descuento_tot = 0;
	$impuesto_tot  = 0;
	$total_tot     = 0;
    $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  ;
	
	if ($cdgcli <> "") { $where.=" AND cdgcli='$cdgcli'"; }
	if ($dsccli <> "") { $where.=" AND dsccli like '%$dsccli%'"; }
	
		 	 /////////////////        where para facturas  y notas ********************
	 
	if ($cdgtipven <> "") { $where.=" AND cdgtipven='$cdgtipven'"; }

	 
	
	
	 	 /////////////////        where para facturas ********************
	 
	if ($cdgcaj <> "") { $where.=" AND cdgcaj='$cdgcaj'"; }
	 $where.=" ORDER BY numfactura ASC";
	 
	 
	 /////////////////        where para notas de credito ********************
	 
	 
	 if ($cdgcaj <> "") { $where_notas.=" AND cdgcaj='$cdgcaj'"; }
	 $where_notas.=" ORDER BY numcomp ASC";
	 
	 
	 
 $fechainicio= implode("-", array_reverse(explode("-", $fecini))); 
 $fechafin= implode("-", array_reverse(explode("-", $fecfin))); 


 if ($cdgcaj=="") {
	 
	 $nomcomusu="TODOS LOS CAJEROS";
	 
	     } else {


	   $query_codcaj="SELECT * FROM usuarios  WHERE codusu=$cdgcaj ";
       $resultado = $mysqli->query( $query_codcaj);
       $row = $resultado->fetch_array();
        
		$nomcomusu    = $row["nomcomusu"];
     }
					
?>
<!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 Analitico de Compras </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="11"  ><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="11" ><div align="center"><font size="3" ><?php echo $Razon?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="11" ><div align="center"><font size="2" ><?php echo $Cedula; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="11" ><div align="center"><font size="2" ><?php echo $Direccion; ?></font></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
      <td colspan="11" ><div align="center"><font size="2" ><?php echo $Telefono; ?></font></div></td>
    </tr>
    <tr>
      <td><span class="Estilo1"></span></td>
      <td colspan="11" ><span class="Estilo1"></span></td>
    </tr>
  <tr>
     <td></td>
 <td colspan="11"  bgcolor="skyblue"><div align="center"><strong><font size="3" >Reporte Analitico de Proformas</font>  </div></td>
  </tr>
     
     <tr>
       <td></td>
       <td colspan="11"   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></td>
       <td colspan="11"   border="1"><div align="center"><font size="3" >Cliente <?php echo $cdgcli; ?> - <?php echo $dsccli; ?></font></div></td>
     </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>Codigo</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>Fecha</strong></font></span></td>
	<td width="20%" align="center" bgcolor="skyblue"><span class="Estilo4"><strong>Plazo</strong></font></span></td>
    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Exento </strong></font></span></td> 
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Sub.Gravado </strong></font></span></td>
	    <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Subtotal </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Descuento </strong></font></span></td>
		<td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Impuesto </strong></font></span></td>
        <td width="20%" align="right" bgcolor="skyblue"><span class="Estilo4"><strong>Total </strong></font></span></td>
		
		<td width="20%" align="center" ><span class="Estilo4"><strong></strong></font></span></td>
	
   
  </tr>
  
<?PHP
			  


	    $consulta= "SELECT * FROM proformas WHERE  fecha  between '$fechainicio' AND '$fechafin'   ".$where;
		if ($resultado = $mysqli->query($consulta)) 

	
	  
 
  		while ($fila = $resultado->fetch_assoc()) {
	
        $fecha         = $fila["fecha"];
		$fecha         = implode("-", array_reverse(explode("-", $fecha)));
		$numfactura    = $fila["numfactura"];
		//$numfactura    = substr($numfactura,10, 10);
		$codfactura    = $fila["codfactura"];
		$cdgcli        = $fila["cdgcli"];
		$cdgcli        = sprintf("%04d",$cdgcli); 
		$dsccli        = $fila["dsccli"];
		$dsccli        = substr($dsccli, 0, 40);
		$pladia        = $fila["pladia"];
		$totalfactura  = number_format($fila["totalfactura"], 2);
		$subexe        = number_format($fila["subexe"], 2);
		$subgra        = number_format($fila["subgra"], 2);
		$subtot        = number_format($fila["subtot"], 2);
		$impven        = number_format($fila["impven"], 2);
		$destot        = number_format($fila["destot"], 2);
		
		

?>  

 <tr>
   <td></td>
   <td align="left"><font size=1 ><?php echo $numfactura?></font></td>
   <td align="center"><font size=1 ><?php echo $cdgcli?></font></td>
   <td align="left"><font size=1 ><?php echo $dsccli?></font></td>
   <td align="center"><font size=1 ><?php echo $fecha?></font></td>
   <td align="center"><font size=1 ><?php echo $pladia?></font></td>
   <td align="right"><font size=1 ><?php echo $subexe?></font></td>
   <td align="right"><font size=1 ><?php echo $subgra ?></font></td>
   <td align="right"><font size=1 ><?php echo  $subtot ?></font></td>
   <td align="right"><font size=1 ><?php echo $destot?></font></td>
   <td align="right"><font size=1 ><?php echo $impven ?></font></td>
   <td align="right"><font size=1 ><?php echo $totalfactura ?></font></td>
    
 </tr>
  
  <?php
	 
  	}	        
	
		 
		 
  
      $consulta2= ("SELECT SUM(totalfactura) AS total , SUM(subtot) AS subtotal, SUM(subexe) AS exento, SUM(subgra) AS gravado, SUM(destot) AS descuento , SUM(impven) AS impuesto FROM proformas WHERE  fecha  between '$fechainicio' AND '$fechafin'  ".$where );

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

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

				$exento_fac    = $dato['exento'];
				$gravado_fac   = $dato['gravado'];
				$subtotal_fac  = $dato['subtotal'];
				$descuento_fac = $dato['descuento'];
				$impuesto_fac  = $dato['impuesto'];
				$total_fac     = $dato['total'];
	 } 		
  
  ?>
  
</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="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 width="1%"></td>
      <td align="left"><strong><font size=1 >TOTAL FACTURAS</font></strong></td>
   <td align="center"><strong><font size=1 ></font></strong></td>
   <td align="left"><strong><font size=1 ></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($exento_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($gravado_fac, 2); ?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($subtotal_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($descuento_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($impuesto_fac , 2);?></font></strong></td>
   <td align="right"><strong><font size=1 ><?php echo number_format($total_fac , 2); ?></font></strong></td>
 </tr>
</table>
<?php
	
	


	  	
	


?>
    <?php
	
	
/*
				$exento = $exento_fac - $exento_not +  $exento_deb  ;
				$gravado = $gravado_fac - $gravado_not + $gravado_deb  ;
				$subtotal =$subtotal_fac - $subtotal_not + $subtotal_deb ;
				$descuento  = $descuento_fac - $descuento_not + $descuento_deb   ;
				$impuesto =  $impuesto_fac - $impuesto_not +  $impuesto_deb ;
				$total = $total_fac - $total_not +  $total_deb ;*/
  ?>
  
  
  




</body>
</html>

 <?php