<?php

header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: filename=ComprasProveedor.xls");
header("Pragma: no-cache");
header("Expires: 0");

include "../conexion.php";
$proveedor=$_REQUEST["proveedor"];
 echo "<table width='100%' border='1' bordercolor='#000000' cellpadding='0' cellspacing='0' >";
echo "<tr ><td colspan='13'><CENTER>DETALLE DE COMPRAS POR PROVEEDOR</CENTER></td></tr>";
echo "<tr >";
echo "<td align='center'>Proveedor</td>";							
echo "<td align='center'>Factura</td>";
echo "<td align='center'>Valor</td>";							
echo "<td align='center'>Iva 19</td>";	
echo "<td align='center'>Iva 10</td>";	
echo "<td align='center'>Iva 5</td>";
echo "<td align='center'>Total Factura</td>";	
echo "<td align='center'>Pagos</td>";	
echo "<td align='center'>Saldos</td>";	
echo "<td align='center'>Fecha</td>";
echo "<td align='center'>Fecha Vence</td>";
echo "<td align='center'>Fecsis</td>";
echo "<td align='center'>Hora</td>";		
echo "</tr>";

if($proveedor>0){$sqlproveedor="where proveedor='".$proveedor."'";} else {$sqlproveedor="";}
 $sql_datos=mysqli_query($conexion,"SELECT proveedor,factura,valor,iva,fecha,fecsis,horasis,id,iva10,iva5,fechavence
						from inventario_proveedor
						$sqlproveedor
						 order by id desc");
	$c=1;	$sumaiva=0; $sumag=0;	$sumaI=0;		$sumacr=0;	 $sumas=0;$sumapagos=0;
	$iva10=0;$iva5=0;$sumavalor=0;$sumaiva=0;$sumaiva10=0;$sumaiva5=0;$sumatotal=0;
   while ($datos= mysqli_fetch_row($sql_datos))//04e
	  { 
		$proveedor=$datos[0];
		$factura=$datos[1];
		$valor=$datos[2];
		$iva=$datos[3];
		$fecha=$datos[4];
		$fecsis=$datos[5];
		$horasis=$datos[6];									
		$iva10=$datos[8];									
		$iva5=$datos[9];									
		$fechavence=$datos[10];									
		$total=$valor+$iva+$iva10+$iva5;
		
		$sumavalor=$sumavalor+$valor;
		$sumaiva=$sumaiva+$iva;
		$sumaiva10=$sumaiva10+$iva10;
		$sumaiva5=$sumaiva5+$iva5;
		$sumatotal=$sumatotal+$total;
		
		$sql_consulta=mysqli_query($conexion,"select nombre from proveedor where id='".$proveedor."'");
		while($datosrs= mysqli_fetch_row($sql_consulta)){ $nombrep=$datosrs[0];}
			
		$sumacr=$sumacr+$valor;
		$efectivopagado=0;$saldo=0;
		 $sql_consulta=mysqli_query($conexion,"select count(*) as contar from inventario_proveedor_pagos
											   where proveedor='".$proveedor."' and factura='".$factura."'"); 
		 while($datosrs= mysqli_fetch_row($sql_consulta)){ $contar=$datosrs[0];}
		 if($contar>=1)
		  {
			$sql_consulta=mysqli_query($conexion,"select sum(valor) as efectivopag from inventario_proveedor_pagos
				 where proveedor='".$proveedor."' and factura='".$factura."'"); 
			while($datosrs= mysqli_fetch_row($sql_consulta)){ $efectivopagado=$datosrs[0];}
		  }
		
		$saldo=	$valor-$efectivopagado;	
		$sumas=$sumas+$saldo;	
		$sumapagos=$sumapagos+$efectivopagado;			
		
		if($saldo>0){$color='#AD0000';} else {$color='#8DD18A';}
		
		 echo "<tr>
				  <td >$nombrep</td>
				  <td >$factura</td>
				  <td >".number_format($valor,2)."</td>
				  <td >".number_format($iva,2)."</td>
				  <td >".number_format($iva10,2)."</td>
				  <td >".number_format($iva5,2)."</td>
				  <td >".number_format($total,2)."</td>
				  <td >".number_format($efectivopagado,2)."</td>
				  <td bgcolor='$color'>".number_format($saldo,2)."</td>											  
				  <td >$fecha</td>
				  <td >$fechavence</td>
				  <td >$fecsis</td>
				  <td >$horasis</td>											  
				</tr>";
				$c=$c+1;
	  }	//04
	   echo "<tr  >
				  <td ></td>
				  <td ></td>
				  <td >".number_format($sumavalor,2)."</td>
				  <td >".number_format($sumaiva,2)."</td>
				  <td >".number_format($sumaiva10,2)."</td>
				  <td >".number_format($sumaiva5,2)."</td>
				  <td >".number_format($sumatotal,2)."</td>
				  <td ></td>
				  <td ></td>											  
				  <td ></td>
				  <td ></td>
				  <td ></td>
				  <td ></td>											  
				</tr>";

	echo "</table>";


?>