<?
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>Libro de Venta</title>
<?
include("../conex.php");
$bodega=$_POST["lstbodega"];
if ($bodega==0) $bodegas='Todos';
else
	{
	$sql_bv = "SELECT * FROM bodega WHERE id = $bodega";
  	$resbv = mysql_query($sql_bv,$enlace);	
	while (mysql_fetch_object ($resbv))
			{
			$bodegas = mysql_result($resbv, 0, "descripcion");
			}
	}
$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">  <br>
<div align="center" class="titulos2">LIBRO DE VENTAS MES DE  <?=$mes?> DEL <?=$año?></div>
  <div align="center">
	<form name="form1" method="post" action="">
    <table width="90%" border="0">
      <tr bgcolor="#CCCCCC">
	  	<th width="2%" scope="col"><div align="center">Dia</div></th>
        <th width="10%" scope="col"><div align="center">RUT</div></th>
        <th width="30%" scope="col"><div align="center">Cliente</div></th>
         <th width="4%" scope="col"><div align="center">Vend</div></th>
        <th width="4%" scope="col"><div align="center">T/D</div></th>
        <th width="7%" scope="col"><div align="center">Nro</div></th>
        <th width="9%" scope="col"><div align="center">Exento</div></th>
		<th width="9%" scope="col"><div align="center">Neto</div></th>
        <th width="8%" scope="col"><div align="center">Iva</div></th>
        <th width="8%" scope="col"><div align="center">Total</div></th>
		<th width="8%" scope="col"><div align="center">Iva Ret.</div></th>
        <th width="7%" scope="col"><div align="center">Pagada</div></th>
          <th width="7%" scope="col"><div align="center">PDF</div></th>
           <th width="7%" scope="col"><div align="center">TrackID</div></th>
            <th width="7%" scope="col"><div align="center">Envia_SII_grupo</div></th>
  </tr>
		<?
		$total_exento=0;			
		$total_neto=0;
		$total_iva=0;
		$total_total=0;
		$total_ivar=0;
		
			$tdcto =$_POST["tdcto"];
				$doctos="(tipo_dcto.id=30 OR tipo_dcto.id=32 OR tipo_dcto.id=33 OR tipo_dcto.id=34 OR tipo_dcto.id=45 OR tipo_dcto.id=46 OR tipo_dcto.id=55 OR tipo_dcto.id=56 OR tipo_dcto.id=60 OR tipo_dcto.id=61)";
	if ($bodega==0)
		{			
			if ($tdcto==0)  $sqlfs = "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND extract(year from fecha_emision)='$ano2' AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  ";
			if ($tdcto==1) $sqlfs = "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND extract(year from fecha_emision)='$ano2' AND tipo_dcto.id=39 AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  ORDER BY num_dcto";
			if ($tdcto==2) $sqlfs =  "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND extract(year from fecha_emision)='$ano2' AND (tipo_dcto.id=50 OR tipo_dcto.id=52) AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa ORDER BY num_dcto";
			if ($tdcto==3) $sqlfs =  "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND extract(year from fecha_emision)='$ano2' AND ".$doctos." AND tipo_dcto.id = salida.tipo_dcto  ORDER BY salida.id";
		}
		else
		{
		if ($tdcto==0)  $sqlfs = "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND extract(year from fecha_emision)='$ano2'  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa AND bodega=$bodega ORDER BY num_dcto";
			if ($tdcto==1) $sqlfs = "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND extract(year from fecha_emision)='$ano2' AND tipo_dcto.id=39  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa AND bodega=$bodega ORDER BY num_dcto";
			if ($tdcto==2) $sqlfs =  "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND extract(year from fecha_emision)='$ano2' AND  (tipo_dcto.id=50 OR tipo_dcto.id=52) AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa AND bodega=$bodega ORDER BY num_dcto";
			if ($tdcto==3) $sqlfs =  "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND extract(year from fecha_emision)='$ano2' AND  ".$doctos."  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa AND bodega=$bodega ORDER BY tipo_dcto, num_dcto";
		}	
		  	$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0;
			while (mysql_fetch_object ($resfs))
				{
				$fecha = mysql_result($resfs, $cont, "fecha_emision");
				$dias = substr ($fecha, 8, 2);
				$vende = mysql_result($resfs, $cont, "vendedor");
				$dia = substr ($fecha, 8, 2);
				$id_dcto = mysql_result($resfs, $cont, "salida.id");
				  $id_nota[$cont]= $id_dcto;          				
				
				$dcto = mysql_result($resfs, $cont, "tipo_dcto.abr");
				$sel[$cont] = 0;
				
		if($tdcto==1) $tidcto="BOLETA"; else $tidcto="DOC";		
				$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;";
					
					}
					$token = mysql_result($resfs, $cont, "token");
					$factor = mysql_result($resfs, $cont, "factor");
				$nro = mysql_result($resfs, $cont, "num_dcto");
				$iva = $factor*mysql_result($resfs, $cont, "iva");
				$total_iva+=$iva;
				
				$pagada = $factor*mysql_result($resfs, $cont, "pagada");
				$pagada=number_format($pagada,0,',','.');
				
				$iva=number_format($iva,0,',','.');
				$neto= $factor*mysql_result($resfs, $cont, "neto");
				$total_neto+=$neto;
				$neto=number_format($neto,0,',','.');
								
				$exento= $factor*mysql_result($resfs, $cont, "exento");
				$total_exento+=$exento;
				$exento=number_format($exento,0,',','.');
				
				$total = $factor*mysql_result($resfs, $cont, "total");
				$total_total+=$total;
				
				$total=number_format($total,0,',','.');
				$ivar = $factor*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>
	            	<?=$dias?>
				</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>
	            	<?=$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><?=$vende?></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_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>
				  <?=$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>
                <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$pagada?>
			    </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_pdf('<?=$tidcto."_".$iddcto."_".$nro."_f.pdf";?>');"> <img src="../Images/pdf.gif"> </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>
				  <?=$token?>
			    </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>
				<input name="sel[<?=$cont;?>]" id="sel[<?=$cont;?>]" type="text" value="<?=$sel[$cont];?>"  style="text-align:center;width:30%" onkeypress="chequear('<?=$cont;?>');" > 
		      </strong></span></div></td>
                                
<td><input name="id_nota[<?=$cont;?>]" type="text" id="id_nota[<?=$cont;?>]" value="<?=$id_nota[$cont];?>" readonly="1" style=""  size="1"></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></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>
				  <?=$total_ivar?>
			    </strong></span></div></td>
	        </tr>
    </table>
   <table width="90%" border="0">
	<tr>
    <td>
  <input type="button" name="lote" id="lote" value="Envia_SII_Grupo_Dcto" onClick="fun_envia_grupo();" disabled="disabled" />
		  <input type="button" name="imprimir_xml" id="imprimir_xml" value="Envio_Libro_Venta_SII" onClick="fun_imprimir_xml();"> <input type="button" name="imprimir_guia_xml" id="imprimir_guia_xml" value="Envio_Libro_Guias_SII" onClick="fun_imprimir_guia_xml();">
	  <input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
	  <input type="button" name="clasificacion" id="clasificacion" value="Clasificacion" onClick="fun_clasificacion();">
	 
       </td>
    </tr>
    <tr> 
    <td> 
     <input type="button" name="Reenvio" id="Reenvio" value="Reenvio_Libro_Venta_SII" onClick="fun_reenvio_imprimir_xml();">
       <input type="text" name="cod_reenvio" id="cod_reenvio" value="<?=$cod_reenvio;?>" > 
         Token:<input type="text" name="token" id="token" value="<?=$token;?>" readonly="1">
	  <input name="txt_nv" id="txt_nv" type="text" style="visibility:hidden" size="1">
        <input name="txtfecha" id="txtfecha" type="text" value="<?=$txtfecha;?>" style="visibility:hidden" size="1">
          <input name="bodega" id="bodega" type="text" value="<?=$bodega;?>" style="visibility:hidden" size="1">
            <input name="cont" id="cont" type="text" value="<?=$cont;?>" style="visibility:hidden" size="1">
			<input type="button" name="reenvia_correo" id="reenvia_correo" value="Reenviar Correos" onClick="reenvio_correo_todos();">
            </td>
     </tr>          
 </table>
    </form>
  </div>
</body>
</html>

<script language="javascript" type="text/javascript">
function cerrar()
{
window.top.open('','_parent','');
window.top.close();
}
function fun_imprimir_xml()
	{
	var answer = confirm ("¿Seguro que desea Enviar Libro_VENTA_XML a SII?");
	if (answer)
	
	{
	document.form1.action="libro_venta_xml.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
	}
function fun_imprimir_guia_xml()
	{
	var answer = confirm ("¿Seguro que desea Enviar Libro_GUIAS_XML a SII?");
	if (answer)
	
	{
	document.form1.action="libro_guias_xml.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
	}	
function fun_imprimir()
	{
	document.getElementById('imprimir').style.display = "none";
	window.print();
	document.getElementById('imprimir').style.display = "block";
	};
function fun_fact(cual)
	{
	var donde;
	donde = "factura.php";
	
	document.form1.txt_nv.value=cual;
	document.form1.action=donde;
	document.form1.target="_blank";
	document.form1.submit();
	}
	
function fun_fact_pdf(cual)
	{
	 var id_not=cual;
document.form1.action="../LibFE/DOC/PDF/"+id_not;
	document.form1.target="_blank";
	document.form1.submit();
	}
function fun_reenvio_imprimir_xml()
	{
	var answer = confirm ("¿Seguro que desea REEnviar Libro_VENTA_XML a SII, debe tener codigo?");
	if (answer)
	
	{
	document.form1.action="libro_venta_xml.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
	}
	
function fun_envia_grupo()
	{
	if (chequear()==true)
	{ 

	document.form1.action= "envia_sii_grupo_dctos.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
	}

function chequear()
	{
	var jsi;
	var jsf;
	jsf=0;
	for (jsi=0;jsi<=<?=$cont;?>;jsi++)
		{
		//if (document.getElementById("sel["+jsi+"]").value!=0)
			{
			jsf+=1;
			} 
		}
//var answer = confirm ("¿Chequea?");
	if (jsf==0)
	           { 
	         document.form1.lote.disabled=true;
	          return false;
	           } 
	         else 
	          { 
	          document.form1.lote.disabled=false;
	           return true;
			   }
	}	

function reenvio_correo_todos()
	{
	var answer = confirm ("¿Seguro que desea reenviar todos los correos?");
	if (answer)
	
	{
	document.form1.action="reenvio_correo_todos.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
	}		
</script>