<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
?>
<!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>Consumo folios</title>
<?
include("../conex.php");
$txtfecha=$_POST["txtfecha"];
$dia = substr ($txtfecha, 0, 2);
$mes = substr ($txtfecha, 3, 2);
$año = substr ($txtfecha, 6, 4);

$txtfecha1=$_POST["txtfecha"];
$dia1 = substr ($txtfecha1, 0, 2);
$mes1 = substr ($txtfecha1, 3, 2);
$año1 = substr ($txtfecha1, 6, 4);

$desde=$año."-".$mes."-".$dia;
$hasta=$año1."-".$mes1."-".$dia1;

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">  <br>
<div align="center" class="titulos2">Folios a consumir DIA: <?=$dia?>/<?=$mes?>/<?=$año?></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">Cliente</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">Exento</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">Anulada</div></th>
		
      </tr>
		<?	
		$total_exento=0;		
		$total_neto=0;
		$total_iva=0;
		$total_total=0;
			
	 $sqlfs = "SELECT * FROM salida, tipo_dcto WHERE fecha_emision>='$desde' AND fecha_emision<='$hasta' AND (tipo_dcto.id=39 OR tipo_dcto.id=61) AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa ORDER BY num_dcto";
			
			
		  	$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0;
			while (mysql_fetch_object ($resfs))
				{
				$fecha = mysql_result($resfs, $cont, "fecha_emision");
				$dia = substr ($fecha, 8, 2);
				$id_dcto = mysql_result($resfs, $cont, "salida.id");
				$iddcto = mysql_result($resfs, $cont, "tipo_dcto");
			
				if($iddcto==39)$dcto='BOL';
			
				if($iddcto==61)$dcto='NC E';
				

				$rut = mysql_result($resfs, $cont, "rut_cliente");
				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;";
					
					}
					
				$doc_anulado = mysql_result($resfs, $cont, "anulado");
				$tot_anulados+=$doc_anulado;
				
				$nro = mysql_result($resfs, $cont, "num_dcto");
				$iva = mysql_result($resfs, $cont, "iva");
				if ($doc_anulado==0)
				{
				$total_iva+=$iva;
				}
				
				$iva=number_format($iva,0,',','.');
				$token= mysql_result($resfs, $cont, "token");
				$exento= mysql_result($resfs, $cont, "exento");
				$neto= mysql_result($resfs, $cont, "neto");
				if ($doc_anulado==0)
				{
				$total_neto+=$neto;
					$total_exento+=$exento;
				}
				$exento=number_format($exento,0,',','.');
				$neto=number_format($neto,0,',','.');
				$total = mysql_result($resfs, $cont, "total");
				if ($doc_anulado==0)
				{
				$total_total+=$total;
				}
				
				$total=number_format($total,0,',','.');
				$ivar = mysql_result($resfs, $cont, "iva_retenido");
				if ($doc_anulado==0)
				{
				$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?>
				</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-center-width:1px"><div align="center"><span class="Estilo1"><strong><?=$nro?></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>
				  <?=$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>
				  <?=$doc_anulado?>
			    </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_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>
				  <?=$tot_anulados;?>
			    </strong></span></div></td>
								
	        </tr>
    </table>
		<form name="form1" method="post" action="">
<input type="button" name="imprimir_xml" id="imprimir_xml" value="Consumo_folios_por_dia_SII_XML" onClick="fun_imprimir_xml();">
<input type="button" name="imprimir_mes_xml" id="imprimir_mes_xml" value="Consumo_folios_por_MES_SII_XML" onClick="fun_imprimir_mes_xml();">
       <input name="txtfecha" id="txtfecha" type="text" value="<?=$txtfecha;?>" style="visibility:hidden" size="1">
	  <input name="txt_nv" id="txt_nv" type="text" style="visibility:hidden" size="1">
      <input name="txtfecha1" id="txtfecha1" type="text" value="<?=$txtfecha1;?>" style="visibility:hidden" size="1">
	  <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_xml()
	{
	document.form1.action="consumo_folios_xml.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
function fun_imprimir_mes_xml()
	{
	document.form1.action="consumo_folios_mes_xml.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
</script>