<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
$provee =$_POST["lstprov"];
header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=facturaproveedores.xls");
header("Pragma: no-cache");
header("Expires: 0");
if ($provee==0) $proveedores='Todos';
else
	{
	$sql_bv = "SELECT * FROM nombres WHERE rut = '$provee'";
  	$resbv = mysql_query($sql_bv,$enlace);	
	while (mysql_fetch_object ($resbv))
			{
			$proveedores= mysql_result($resbv, 0, "razon_social");
			}
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Cuentas Corrientes</title>
<?
include("../conex.php");
$txtfecha=$_POST["txtfecha"];
$diad = substr ($txtfecha, 0, 2);
$mesd = substr ($txtfecha, 3, 2);
$añod = substr ($txtfecha, 6, 4);
$txtfecha1=$_POST["txtfecha1"];
$diah = substr ($txtfecha1, 0, 2);
$mesh = substr ($txtfecha1, 3, 2);
$añoh = substr ($txtfecha1, 6, 4);
$ini = $añod.'/'.$mesd.'/'.$diad;
$fin = $añoh.'/'.$mesh.'/'.$diah;
?>
<style type="text/css">
<!--
.Estilo1 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px;}
.Estilo2 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color:#FF0000}
-->
</style>
</head>
<body LINK="#000000" VLINK="#000000" ALINK="#000000"><br>
<div align="center" class="titulos2">Facturas de <? if ($provee==0) echo "todos los proveedores"; else echo "Proveedor ".$proveedores."";?></div>
  <div align="center">
    <table width="80%" border="0">
      <tr bgcolor="#CCCCCC">
	  	<th scope="col"><div align="center">Día</div></th>
        <th scope="col"><div align="center">RUT</div></th>
        <th scope="col"><div align="center">Proveedor</div></th>
        <th scope="col"><div align="center">T/D</div></th>
        <th scope="col"><div align="center">Nro</div></th>
		<th scope="col"><div align="center">Neto</div></th>
        <th scope="col"><div align="center">Iva</div></th>
        <th scope="col"><div align="center">Total</div></th>
		<th scope="col"><div align="center">Iva Ret.</div></th>
      </tr>
		<?	
		
		
		$total_neto=0;
		$total_iva=0;
		$total_total=0;
		$total_ivar=0;
		
			if ($provee==0) { $sqlfs = "SELECT * FROM entrada, tipo_dcto WHERE (fecha_emision>='$ini' and fecha_emision<='$fin') AND (tipo_dcto.descripcion='FACTURA' OR tipo_dcto.id=33) AND tipo_dcto.id = entrada.tipo_dcto AND entrada.empresa = $id_empresa  ORDER BY fecha_emision ASC";
			}
			else  { $sqlfs = "SELECT * FROM entrada, tipo_dcto WHERE (fecha_emision>='$ini' and fecha_emision<='$fin') AND (tipo_dcto.descripcion='FACTURA' OR tipo_dcto.id=33) AND tipo_dcto.id = entrada.tipo_dcto AND entrada.empresa = $id_empresa AND entrada.rut_proveedor='$provee' ORDER BY fecha_emision ASC";
			}

		  	$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0;
		while (mysql_fetch_object ($resfs))
				{
				$fecha = mysql_result($resfs, $cont, "fecha_emision");
				$dia = substr ($fecha, 8, 2);
				$mes = substr ($fecha, 5, 2);
				$año = substr ($fecha, 0, 4);
				$iddcto = mysql_result($resfs, $cont, "tipo_dcto");
				if($iddcto==1)$dcto='B';
				if($iddcto==2)$dcto='F';
				if($iddcto==3)$dcto='NV';
				if($iddcto==4)$dcto='C';
				if($iddcto==5)$dcto='G';
				if($iddcto==6)$dcto='NC';
				if($iddcto==7)$dcto='ND';
					if($iddcto==33)$dcto='FE';
				if($iddcto==56)$dcto='ND E';
				if($iddcto==61)$dcto='NC E';

				$rut = mysql_result($resfs, $cont, "rut_proveedor");
				if ($rut!=''){
				$sqlc = "SELECT * FROM nombres WHERE rut='$rut'";
		  		$resc = mysql_query($sqlc,$enlace);
				
				$nombre = mysql_result($resc, 0, "razon_social");
				}
				else 
					{
					$nombre = "Boleta";
					$rut="&nbsp;";
					
					}
					
					
				
		
		
				$id_entrada = mysql_result($resfs, $cont, "id");
				$nro = mysql_result($resfs, $cont, "num_dcto");
				$iva = mysql_result($resfs, $cont, "iva");
				$total_iva+=$iva;
				
				$iva=number_format($iva,0,',','.');
				$neto= mysql_result($resfs, $cont, "neto");
				$total_neto+=$neto;
				
				$neto=number_format($neto,0,',','.');
				$total = mysql_result($resfs, $cont, "total");
				$total_total+=$total;
				
				$total=number_format($total,0,',','.');
				$ivar = mysql_result($resfs, $cont, "iva_retenido");
				$total_ivar+=$ivar;
				
				$ivar=number_format($ivar,0,',','.');
		  	?>
			<tr>
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	          	<?=$dia.'/'.$mes.'/'.$año?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            <?=$rut?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            <?=$nombre?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            <?=$dcto?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            	<a  href="javascript:fun_fact('<?=$id_entrada;?>','<?=$dcto?>');"> <?=$nro?> </a>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
			    <?=$neto?> 
			    </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
			     <?=$iva?>
			    </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
			    <?=$total?>
			    </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
			    <?=$ivar?>
			    </strong></span></div></td>

	        </tr>	
			<?
			$cont+=1;
			
			}
			$total_neto=number_format($total_neto,0,',','.');
			$total_iva=number_format($total_iva,0,',','.');
			$total_total=number_format($total_total,0,',','.');
			$total_ivar=number_format($total_ivar,0,',','.');
			?>
			<tr>
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            	
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            	
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            	Total:
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$total_neto?>
			    </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$total_iva;?>
			    </strong></span></div></td> 
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$total_total;?>
			    </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$total_ivar?>
			    </strong></span></div></td>
	        </tr>
    </table>
			<form name="form1" method="post" action="">

	  <input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
	  <input name="txt_nv" id="txt_nv" type="text" style="visibility:hidden" size="1">
    </form>
  </div>
</body>
</html>

<script language="javascript" type="text/javascript">
function cerrar()
{
window.top.open('','_parent','');
window.top.close();
}
function fun_imprimir()
	{
	document.getElementById('imprimir').style.display = "none";
	window.print();
	document.getElementById('imprimir').style.display = "block";
	};
function fun_fact(cual,dct)
	{
	var donde;
	if (dct=='F') donde = "factura.php";
	if (dct=='B') donde = "boleta.php";
	if (dct=='NC') donde = "nc.php";
	if (dct=='ND') donde = "nd.php";
	document.form1.txt_nv.value=cual;
	document.form1.action=donde;
	document.form1.target="_blank";
	document.form1.submit();
	}
</script>