<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=informe anual compra venta.xls");
header("Pragma: no-cache");
header("Expires: 0");
?>
<!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>Informe Anual Compra Venta</title>
<?
include("../conex.php");

$periodo = $_POST["txtfecha"];
 $semestre = substr($periodo,3,2);
	   $anual = substr($periodo,6,4);
	if ($semestre>='07')
	{$inicio=$anual.'/07/01';
	  $final=$anual.'/12/31';
	}
	 else 
	{$inicio = $anual.'/01/01';
	 $final = $anual.'/06/30';
	 }
?>
<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="#FFFFFF" VLINK="#FFFFFF" ALINK="#FFFFFF">
<table width="900" border="0">
	<form name="form1" method="post" action="">
      <tr>
        <th width="50%"><div align="right">
            <input name="salir" type="button" class="Estilo1" id="salir" value="Salir" onclick="cerrar()"/>
        </div></th>
  <th width="50%"><div align="left">
			<input name="imprimir" type="button" class="Estilo1" id="imprimir" value="Imprimir" onclick="imprimir()"/>
        </div></th>
      <th width="50%"><div align="left">
			Periodo  <?=$inicio;?>   <?=$final;?>
        </div></th> 
      </tr>
        </form>
    </table>
    
<table width="905" border="0" >
<tr bgcolor="#CCCCCC" >
	  	<th width="138" scope="col"><div align="center">Rut</div></th>
        <th width="136" scope="col"><div align="center">IVA compras</div></th>
        <th width="136" scope="col"><div align="center">IVA Ret.Compras</div></th>
        <th width="136" scope="col"><div align="center">Cant.dcto recibidos</div></th>
        <th width="136" scope="col"><div align="center">IVA NC Recibidas</div></th>
        <th width="136" scope="col"><div align="center">Cant.NC Recibidas</div></th>
        <th width="136" scope="col"><div align="center">IVA ventas</div></th>
        <th width="136" scope="col"><div align="center">Iva NO ret Fact Compras</div></th>
        <th width="136" scope="col"><div align="center">Cant.Dctos emitidos</div></th>
        <th width="136" scope="col"><div align="center">IVA NC emitidas</div></th>
        <th width="136" scope="col"><div align="center">Cant. NC emitidas</div></th>
  </tr>
	  <?
	 
	

	        $sqlfs = "SELECT  nombres.rut as rut, razon_social  FROM nombres  ORDER BY rut";
		  	$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0;
		
	while (mysql_fetch_object ($resfs))
		{
	$iva_compras = 0;
	$iva_ret_compras=0;
	$cant_dcto_recibidas=0;
	$iva_nc_recibidas=0;
	$cant_nc_recibidas=0;
	$iva_ventas = 0;
	$iva_no_ret_fact_compras=0;
	$cant_dcto_emitidas=0;
	$iva_nc_emitidas=0;
	$cant_nc_emitidas=0;
	
		$rut = mysql_result($resfs, $cont, "rut");
		$nombre = mysql_result($resfs, $cont, "razon_social");
		
	//**********  Entrada  IVA Facturas compra **************
	    $conti=0;
		$sqldcto = "select  sum(iva), sum(iva_retenido), sum(abs(factor)) FROM entrada  WHERE (tipo_dcto=2 OR tipo_dcto=14) AND fecha_libro>='$inicio' AND fecha_libro<='$final' AND rut_proveedor='$rut'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$iva_compras = mysql_result($resdcto, $conti, "sum(iva)");
		$total_iva_compras+=$iva_compras;
		$iva_ret_compras = mysql_result($resdcto, $conti, "sum(iva_retenido)");
		$total_iva_ret_compras+=$iva_ret_compras;
		$cant_dcto_recibidas = mysql_result($resdcto, $conti, "sum(abs(factor))");
		$total_cant_dcto_recibidas+=$cant_dcto_recibidas;
		$conti+=1;
		}
	//**********  Entrada  IVA NC Compras **************
	    $conti=0;
		$sqldcto = "select  sum(iva), sum(abs(factor)) FROM entrada  WHERE (tipo_dcto=6 OR tipo_dcto=15) AND fecha_libro>='$inicio' AND fecha_libro<='$final' AND rut_proveedor='$rut'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$iva_nc_recibidas = mysql_result($resdcto, $conti, "sum(iva)");
		$total_iva_nc_recibidas+=$iva_nc_recibidas;
		$cant_nc_recibidas = mysql_result($resdcto, $conti, "sum(abs(factor))");
		$total_cant_nc_recibidas+=$cant_nc_recibidas;
		$conti+=1;
		}
	//**********  Entrada  IVA Facturas venta **************
	    $conti=0;
		$sqldcto = "select  sum(iva), sum(iva_retenido), sum(abs(factor)) FROM salida  WHERE tipo_dcto=2  AND fecha_emision>='$inicio' AND fecha_emision<='$final' AND rut_cliente='$rut'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$iva_ventas = mysql_result($resdcto, $conti, "sum(iva)");
		$total_iva_ventas+=$iva_ventas;
		$iva_ret_ventas = mysql_result($resdcto, $conti, "sum(iva_retenido)");
		$total_iva_ret_ventas+=$iva_ret_ventas;
		$cant_dcto_emitidas = mysql_result($resdcto, $conti, "sum(abs(factor))");
		$total_cant_dcto_emitidas+=$cant_dcto_emitidas;
		$conti+=1;
		}
	//**********  Entrada  IVA NC Venta **************
	    $conti=0;
		$sqldcto = "select  sum(iva), sum(abs(factor)) FROM salida WHERE tipo_dcto=6  AND fecha_emision>='$inicio' AND fecha_emision<='$final' AND rut_cliente='$rut'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$iva_nc_emitidas = mysql_result($resdcto, $conti, "sum(iva)");
		$total_iva_nc_emitidas+=$iva_nc_emitidas;
		$cant_nc_emitidas = mysql_result($resdcto, $conti, "sum(abs(factor))");
		$total_cant_nc_emitidas+=$cant_nc_emitidas;
		$conti+=1;
		}		
	$total = $iva_compras + $iva_ret_compras + $iva_nc_recibidas + $iva_ventas + $iva_no_ret_fact_compras + $iva_nc_emitidas;
	
	if($total!=0)	
	  {		
		  $iva_compras=number_format($iva_compras,0,',','.');
		  $iva_ret_compras=number_format($iva_ret_compras,0,',','.');
		  $cant_dcto_recibidas=number_format($cant_dcto_recibidas,0,',','.');
		  $iva_nc_recibidas=number_format($iva_nc_recibidas,0,',','.');
		  $cant_nc_recibidas=number_format($cant_nc_recibidas,0,',','.');
		  $iva_ventas=number_format($iva_ventas,0,',','.');
		  $iva_no_ret_fact_compras=number_format($iva_no_ret_fact_compras,0,',','.');
		  $cant_dcto_emitidas=number_format($cant_dcto_emitidas,0,',','.');
		  $iva_nc_emitidas=number_format($iva_nc_emitidas,0,',','.');
		  $cant_nc_emitidas=number_format($cant_nc_emitidas,0,',','.');
		  
		  
		  
				
	?>			
	
				<tr>
				
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="left"><strong> <?=$rut?> </strong></div></td>
                
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$iva_compras?> </strong></div></td>
 
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$iva_ret_compras?> </strong></div></td>
    
    <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?= $cant_dcto_recibidas?> </strong></div></td>
                
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$iva_nc_recibidas?> </strong></div></td>
 
   <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$cant_nc_recibidas?> </strong></div></td> 
    
   <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$iva_ventas?> </strong></div></td>
                   
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$iva_no_ret_fact_compras?> </strong></div></td>
 
   <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$cant_dcto_emitidas?> </strong></div></td>                   
               
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$iva_nc_emitidas?> </strong></div></td>
 
    <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$cant_nc_emitidas?> </strong></div></td> 

      </tr>	
		<?
		}
			$cont = $cont + 1;
		  }
		  
		  $total_iva_compras=number_format($total_iva_compras,0,',','.');
		  $total_iva_ret_compras=number_format($total_iva_ret_compras,0,',','.');
		  $total_cant_dcto_recibidas=number_format($total_cant_dcto_recibidas,0,',','.');
		  $total_iva_nc_recibidas=number_format($total_iva_nc_recibidas,0,',','.');
		  $total_cant_nc_recibidas=number_format($total_cant_nc_recibidas,0,',','.');
		  $total_iva_ventas=number_format($total_iva_ventas,0,',','.');
		  $total_iva_no_ret_fact_compras=number_format($total_iva_no_ret_fact_compras,0,',','.');
		  $total_cant_dcto_emitidas=number_format($total_cant_dcto_emitidas,0,',','.');
		  $total_iva_nc_emitidas=number_format($total_iva_nc_emitidas,0,',','.');
		  $total_cant_nc_emitidas=number_format($total_cant_nc_emitidas,0,',','.');
		?>
        
      <tr>
			
    <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="left"><strong> Totales </strong></div></td>
                
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$total_iva_compras?> </strong></div></td>
 
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$total_iva_ret_compras?> </strong></div></td>
    
    <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$total_cant_dcto_recibidas?> </strong></div></td>
                
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$total_iva_nc_recibidas?> </strong></div></td>
 
   <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$total_cant_nc_recibidas?> </strong></div></td> 
    
   <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$total_iva_ventas?> </strong></div></td>
                   
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$total_iva_no_ret_fact_compras?> </strong></div></td>
 
   <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$total_cant_dcto_emitidas?> </strong></div></td>                   
               
	<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$total_iva_nc_emitidas?> </strong></div></td>
 
    <td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><strong> <?=$total_cant_nc_emitidas?> </strong></div></td> 
		        
</tr>
    </table>
  </div>
</body>
</html>

<script language="javascript" type="text/javascript">

function cerrar()
{
window.top.open('','_parent','');
window.top.close();
}

function imprimir()
{
document.getElementById('salir').style.display = "none";
document.getElementById('imprimir').style.display = "none";
window.print();
document.getElementById('salir').style.display = "block";
document.getElementById('imprimir').style.display = "block";
}

</script>