<?
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=librocompras.xls");
header("Pragma: no-cache");
header("Expires: 0");
*/
$id_autoriza=25;

/*Sesiones*/
include("../conex.php");
include("../autoriza.php");
if ($editar==0)  
{
?>
		<script>
		alert ("No Autorizado");
		parent.location.href = 'index.php';
		</script>
	<? 
	exit();
}
?>
<!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>Libro de Compra</title>
<?
include("../conex.php");
$txtfecha=$_POST["txtfecha"];
$dia = substr ($txtfecha, 0, 2);
$mes = substr ($txtfecha, 3, 2);
$mes2=$mes;
$año = substr ($txtfecha, 6, 4);
$ano2=$año;
if ($mes=='01') $mes = "ENERO";
if ($mes=='02') $mes = "FEBRERO";
if ($mes=='03') $mes = "MARZO";
if ($mes=='04') $mes = "ABRIL";
if ($mes=='05') $mes = "MAYO";
if ($mes=='06') $mes = "JUNIO";
if ($mes=='07') $mes = "JULIO";
if ($mes=='08') $mes = "AGOSTO";
if ($mes=='09') $mes = "SEPTIEMBRE";
if ($mes=='10') $mes = "OCTUBRE";
if ($mes=='11') $mes = "NOVIEMBRE";
if ($mes=='12') $mes = "DICIEMBRE";
?>
<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">

<div align="center" class="titulos2">LIBRO DE COMPRAS MES DE  <?=$mes?> DEL <?=$año?></div>
  <div align="center">
 
 <table width="77%" border="0">
  <tr bgcolor="#CCCCCC">
	  	<th width="5%" scope="col"><div align="center">Tipo Doc</div></th>
      <th width="5%" scope="col"><div align="center">Folio</div></th>
      <th width="5%" scope="col"><div align="center">Rut Contraparte</div></th>
      <th width="2%" scope="col"><div align="center">Tasa Impuesto</div></th>
      <th width="10%" scope="col"><div align="center">Razón Social Contraparte</div></th>
      <th width="5%" scope="col"><div align="center">Tipo Impuesto[1=IVA:2=LEY 18211]</div></th>
	  <th width="8%" scope="col"><div align="center">Fecha Emisión</div></th>
      <th width="8%" scope="col"><div align="center">Anulado[A]</div></th>
	  <th width="8%" scope="col"><div align="center">Monto Exento</div></th>
       <th width="8%" scope="col"><div align="center">Monto Neto</div></th>
      <th width="8%" scope="col"><div align="center">Monto IVA (Recuperable)</div></th>
      <th width="8%" scope="col"><div align="center">Cod IVA no Rec</div></th>
      <th width="8%" scope="col"><div align="center">Monto IVA no Rec</div></th>
      <th width="8%" scope="col"><div align="center">IVA Uso Común</div></th>
      <th width="8%" scope="col"><div align="center">Cod Otro Imp (Con Crédito)</div></th>
      <th width="8%" scope="col"><div align="center">Tasa Otro Imp (Con Crédito)</div></th>
      <th width="8%" scope="col"><div align="center">Monto Otro Imp (Con Crédito)</div></th>
       <th width="8%" scope="col"><div align="center">Monto Total</div></th>
       <th width="8%" scope="col"><div align="center">Monto Otro Imp Sin Crédito</div></th>
        <th width="8%" scope="col"><div align="center">Monto Activo Fijo</div></th>
  <th width="8%" scope="col"><div align="center">Monto IVA Activo Fijo</div></th>
   <th width="8%" scope="col"><div align="center">IVA No Retenido</div></th>
    <th width="8%" scope="col"><div align="center">Tabacos - Puros</div></th>
     <th width="8%" scope="col"><div align="center">Tabacos - Cigarrillos</div></th> 
  <th width="8%" scope="col"><div align="center">Tabacos - Elaborados</div></th>
  <th width="8%" scope="col"><div align="center">Impuesto a Vehiculos Automóviles</div></th>
  <th width="8%" scope="col"><div align="center">Codigo sucursal SII</div></th> 
  
  <th width="8%" scope="col"><div align="center">Numero Interno</div></th>
   <th width="8%" scope="col"><div align="center">Emisor/Receptor</div></th>
        
      
    </tr>
		<?	
		
		$total_exento=0;
		$total_neto=0;
		$total_iva=0;
		$total_total=0;
		$total_ivar=0;
		
			$tdcto =$_POST["tdcto"];
			if ($tdcto==0) { $sqlfs = "SELECT * FROM entrada, tipo_dcto WHERE extract(month from fecha_libro)='$mes2' AND extract(year from fecha_libro)='$ano2' AND tipo_dcto.id = entrada.tipo_dcto AND entrada.empresa = $id_empresa";
			}
			else if ($tdcto==1) { $sqlfs = "SELECT * FROM entrada, tipo_dcto WHERE extract(month from fecha_libro)='$mes2' AND extract(year from fecha_libro)='$ano2' AND tipo_dcto.descripcion='BOLETA' AND tipo_dcto.id = entrada.tipo_dcto AND entrada.empresa = $id_empresa";
			}
			else if ($tdcto==2) { $sqlfs = "SELECT * FROM entrada, tipo_dcto WHERE extract(month from fecha_libro)='$mes2' AND extract(year from fecha_libro)='$ano2' AND (tipo_dcto.descripcion='GUIAS') AND tipo_dcto.id = entrada.tipo_dcto AND entrada.empresa = $id_empresa";
			}
			else if ($tdcto==3) { $sqlfs = "SELECT * FROM entrada, tipo_dcto WHERE extract(month from fecha_libro)='$mes2' AND extract(year from fecha_libro)='$ano2' AND (tipo_dcto.descripcion='FACTURA' OR tipo_dcto.descripcion='NC' OR tipo_dcto.descripcion='ND' OR tipo_dcto.descripcion='FACTURA ELECTRONICA' OR tipo_dcto.descripcion='NC ELECTRONICA' OR tipo_dcto.descripcion='ND ELECTRONICA') AND tipo_dcto.id = entrada.tipo_dcto AND entrada.empresa = $id_empresa  ORDER BY correlativo";
			}

		  	$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0;
		while (mysql_fetch_object ($resfs))
				{
				$correlativo = mysql_result($resfs, $cont, "correlativo");
				$fecha = mysql_result($resfs, $cont, "fecha_emision");
				$dia = substr ($fecha, 5, 5);
				
				$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==8)$dcto='F C';
				if($iddcto==14)$dcto='F E';
				if($iddcto==15)$dcto='NC E';
				if($iddcto==16)$dcto='ND E';
				$id_entrada = mysql_result($resfs, $cont, "entrada.id");
				$nro = mysql_result($resfs, $cont, "num_dcto");
				$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;";
					}
				if($iddcto==6 || $iddcto==15)       //  NOTAS DE CREDITO SE RESTAN
				{ $fp=-1; }
				else
				{ $fp=1;}
				$exento= $fp * mysql_result($resfs, $cont, "exento");
				$iva = $fp * mysql_result($resfs, $cont, "iva");
				$neto= $fp * mysql_result($resfs, $cont, "neto");
				$total = $fp * mysql_result($resfs, $cont, "total");
				$ivar = mysql_result($resfs, $cont, "iva_retenido");
				
				$total_exento+=$exento;
				$total_neto+=$neto;
				$total_total+=$total;
				$total_iva+=$iva;
				$total_ivar+=$ivar;
				
				$exento=number_format($exento,0,',','.');
				$iva=number_format($iva,0,',','.');
				$neto=number_format($neto,0,',','.');
				$ivar=number_format($ivar,0,',','.');
                $total=number_format($total,0,',','.');
		  	?>
			<tr>
		<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong><?=$dia?></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="center"><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="center"><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><?=$exento?></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><?=$ivar?></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><?=$correlativo?></strong></span></div></td>
    </tr>	
			<?
			$cont+=1;
			
			}
			$total_exento=number_format($total_exento,0,',','.');
			$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_exento?> </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_ivar?></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>
	        </tr>
    </table>
    
<form name="form1" method="post" action="">
  <input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
  <input type="button" name="clasificacion" id="clasificacion" value="Clasificaci&oacute;n" onClick="fun_clasificacion();">
  <input name="txt_nv" id="txt_nv" type="text" style="visibility:hidden" size="1">
  <input name="txtfecha" id="txtfecha" type="text" style="visibility:hidden" value="<?=$txtfecha?>" 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";
	document.getElementById('clasificacion').style.display = "none";
	window.print();
	document.getElementById('imprimir').style.display = "block";
	document.getElementById('clasificacion').style.display = "block"
	}

function fun_fact(cual,dct)
	{
	var donde;
	if (dct=='F' || dct=='F E' || dct=='F C') donde = "factura.php";
	if (dct=='G' || dct=='G E') donde = "guia.php";
	if (dct=='NC' || dct=='NC E') donde = "nc.php";
	if (dct=='ND' || dct=='ND E') donde = "nd.php";
	document.form1.txt_nv.value=cual;
	document.form1.action=donde;
	document.form1.target="_blank";
	document.form1.submit();
	}
function fun_clasificacion()
	{
	document.form1.action="clasificacion.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
</script>