<?
ini_set('error_reporting', 1);
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = 'index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
?>
<?
include("../../conex.php");
include("../../css/mascara_fecha.js");

?>
<script type="text/javascript" src="../css/validarut.js"></script>
<script language="JavaScript" src="../css/cal.js" type="text/JavaScript"></script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Facturar Nota Venta - FE</title>
</head>
<?

$cant = "1";
$flag=0;
$id_nv=$_POST["txt_nv"];
$id_bodega=$_POST["id_bodega"];
$lstdcto=33;
/********************************** Datos Empresa ******************************************************/
$sql_empresa = "SELECT * FROM empresa, comunas WHERE comunas.cod_comuna = empresa.comuna AND id=$id_empresa";
	$res_empresa = mysql_query($sql_empresa,$enlace);
	$row=0;
	while (mysql_fetch_object ($res_empresa))
		{
		$empresa_rut=mysql_result($res_empresa, $row, "rut_empresa");
		$empresa_nombre=mysql_result($res_empresa, $row, "nombre");
		$empresa_direccion=mysql_result($res_empresa, $row, "direccion");
		$empresa_ciudad=mysql_result($res_empresa, $row, "ciudad");
		$row=$row+1;
		}
		

//********************* Sacar Datos Nota Venta ******************************************/		

$sql_nv = "SELECT * FROM salida, tipo_dcto WHERE tipo_dcto.id = salida.tipo_dcto AND tipo_dcto.descripcion ='NOTA VENTA' AND salida.id=$id_nv AND salida.empresa = $id_empresa ";

//echo $sql_nv;
$res_nv= mysql_query($sql_nv,$enlace);
$nv_cont = 0;
$sqlnv_ivar='';
while ( mysql_fetch_object($res_nv))
	{
	$sqlnv_id=mysql_result($res_nv, $nv_cont, "salida.id");
	$id_nota_venta=$sqlnv_id;
	$sqlnv_id_sucursal=mysql_result($res_nv, $nv_cont, "salida.id_sucursal");
	$sqlnv_fecha=mysql_result($res_nv, $nv_cont, "fecha_emision");
	$sqlnv_fecha_ve=mysql_result($res_nv, $nv_cont, "fecha_vencimiento");
	$sqlnv_rut=mysql_result($res_nv, $nv_cont, "rut_cliente");
	$txtrut=$sqlnv_rut;
	$sqlnv_td=mysql_result($res_nv, $nv_cont, "tipo_dcto");
	$txtnotaventa=mysql_result($res_nv, $nv_cont, "num_dcto");
	$lstpago=mysql_result($res_nv, $nv_cont, "tipo_pago");
	$id_vendedor=mysql_result($res_nv, $nv_cont, "vendedor");
	$id_bodega=mysql_result($res_nv, $nv_cont, "bodega");
	$id_zona=mysql_result($res_nv, $nv_cont, "zona");
	$sql_descuento=intval(mysql_result($res_nv, $nv_cont, "descuento"));
	$sql_neto= intval( mysql_result($res_nv, $nv_cont, "neto"));
	$txtobs= mysql_result($res_nv, $nv_cont, "observacion");
	$firmado="";
	if ($sql_descuento!=0 && $sql_neto!=0) $por_descuento=intval (10000*$sql_descuento/($sql_neto+$sql_descuento))/100; else $por_descuento=0;
	
	$sql_iva=intval(mysql_result($res_nv, $nv_cont, "iva"));
	$sql_total=intval(mysql_result($res_nv, $nv_cont, "total"));
	$sqlnv_ivar=intval(mysql_result($res_nv, $nv_cont, "iva_retenido"));
	$txtnumoc=mysql_result($res_nv, $nv_cont, "orden_compra");
	$control=mysql_result($res_nv, $nv_cont, "documento_control");
	
	$nv_cont += 1;
	}
	if ($nv_cont==0 || $control!=0){
	?>
	<script language="javascript">
	alert("No existe nota de venta relacionada al numero ingresado o Facturada");
	//window.top.open('','_parent','');window.top.close();
	</script>
	<?
	}
	
	
	
	
//********************************* Sacar numero factura *********************************************/
	$sql_ultima_bol = "SELECT correlativo, tope FROM tipo_dcto_num_bodega  WHERE id_bodega =1 AND id_dcto=33";
	$res_ultima_bol = mysql_query($sql_ultima_bol,$enlace);
	$sql_num_bol=0;
	if(mysql_fetch_object($res_ultima_bol))	
	    $sql_num_bol = mysql_result($res_ultima_bol,0 ,"correlativo");
		$num_tope = mysql_result($res_ultima_bol,0 ,"tope");
			
	$sql_num_factura = $sql_num_bol + 1;
	
	       if ($sql_num_factura>$num_tope) 
		   {
		   ?>
	<script language="javascript">
	alert("Solicitar folio en SII para FE");
	//window.top.open('','_parent','');window.top.close();
	</script>
	<?
		   }
//*** Validar numero de FE en salida   lo hace en guardar   //* Guarda numero de FE 
     $txtndcto=$sql_num_factura;


	
	$res_ven = mysql_query("SELECT * FROM vendedor WHERE id=$id_vendedor",$enlace);
	if(mysql_fetch_object($res_ven)) $txtvendedor = mysql_result($res_ven, 0, "nombres");
    $res_ven = mysql_query("SELECT * FROM tipo_pago WHERE id=$lstpago",$enlace);
	if(mysql_fetch_object($res_ven)) $forma_pago = mysql_result($res_ven, 0, "descripcion");
	$res_ven = mysql_query("SELECT * FROM zona WHERE id=$id_zona",$enlace);
	if(mysql_fetch_object($res_ven)) $txtzona = mysql_result($res_ven, 0, "descripcion");
	$res_ven = mysql_query("SELECT * FROM bodega WHERE id=$id_bodega",$enlace);
	if(mysql_fetch_object($res_ven)) $txtbodega = mysql_result($res_ven, 0, "descripcion");
	
	$res_cli = mysql_query("SELECT * FROM sucursales, nombres WHERE nombres.rut=sucursales.rut AND id=$sqlnv_id_sucursal",$enlace);
	if(mysql_fetch_object($res_cli)) 
		{
		$id_sucursal = mysql_result($res_cli, 0, "sucursales.id");
		$sucursal = mysql_result($res_cli, 0, "sucursales.sucursal");
		$txtrsoc = mysql_result($res_cli, 0, "sucursales.razon_social");
		$txtactividad = mysql_result($res_cli, 0, "sucursales.rubro_giro");
		$txtemail = mysql_result($res_cli, 0, "sucursales.email");
		$txtfono = mysql_result($res_cli, 0, "sucursales.fono");
		$txtfax = mysql_result($res_cli, 0, "sucursales.fax");
		$id_region = mysql_result($res_cli, 0, "sucursales.region");
		$id_comuna = mysql_result($res_cli, 0, "sucursales.comuna");
		$txtciudad = mysql_result($res_cli, 0, "sucursales.ciudad");
		$txtdireccion = mysql_result($res_cli, 0, "sucursales.direccion");
		$txtmontocredito = mysql_result($res_cli, 0, "monto_credito");
		$txtdias_plazo = mysql_result($res_cli, 0, "nombres.dias_plazo");
		$id_condicion=mysql_result($res_cli, 0, "id_condicion");
		}
		 $res_ven = mysql_query("SELECT * FROM tipo_condicion_pago WHERE id=$id_condicion",$enlace);
       	if(mysql_fetch_object($res_ven)) $condicion_pago = mysql_result($res_ven, 0, "descripcion");
	$unixtime = time();
		$settime = $unixtime;
		$dia = date("d",$settime);
		$mes = date("m",$settime);
		$ano = date("Y",$settime); 
		$fecha= "$dia/$mes/$ano";    //  hoy
	
	  $unixtime = time();
		$settime = $unixtime  + ( 86400* $txtdias_plazo);
		$dia = date("d",$settime);
		$mes = date("m",$settime);
		$ano = date("Y",$settime); 
	
	$fecha_ve = "$dia/$mes/$ano";	// plazo
	
		
?>
<script type="text/javascript" src="../css/validarut.js"></script>
<body>
<form id="form1" name="form1" method="post" action="">
<input name="txtvalser" type="text" id="txtvalser" style="visibility:hidden" size="1" />
<table width="640" align="center" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="60px"><div align="center"><img src="../Images/logo.gif" width="60px" height="60px"></div></td>
	<td width="580px"><div align="left"><font size="1">
		<?=$empresa_nombre?><br>
		<?=$empresa_direccion?><br>
		<?=$empresa_ciudad;?>

		</font></div>
	</td>
     <td width="280px" align="left"><br>
      <input type="text" name="txttitulo" id="txttitulo" align="middle" style="height:50px;width:300px;border-width:0; text-align:left; font-size:18px" value="FACTURA ELECTRONICA Nº <?=$sql_num_factura;?>"/>
    </td>
    <td width="20%">&nbsp;</td>
  </tr>
</table>

<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
		<td width="94">Fecha:</td>
		<td width="148">
			<input name="txtfecha" type="text" style="border-width:0;" value="<?=$fecha?>" size="23" readonly="1">	  </td>
		<td width="26"><a  href="javascript:show_calendar('form1.txtfecha');"><img name="foto2" id="foto2" align="top" border="0" src="../Images/CALENDAR.gif" style="cursor:pointer" title="Presione aqui y seleccione la fecha" /></a></td>
		<td width="86"> Vencimiento</td>
		<td width="286">
			<input name="fecha_ve" type="text" id="fecha_ve" style="border-width:0;" value="<?=$fecha_ve?>" size="12" readonly="1" />	
			<a  href="javascript:show_calendar('form1.fechaven');"><img name="foto2" id="foto2" align="top" border="0" src="../Images/CALENDAR.gif" style="cursor:pointer" title="Presione aqui y seleccione la fecha" /></a>	  </td>
	</tr>
    
	<tr>
		<td width="94">R.U.T.</td>
		<td width="148">
			<input name="txtrut" type="text" id="txtrut" value="<?=$txtrut?>" readonly="1" size="23" maxlength="12"/>	  </td>
		<td width="26" align="left">	</td>
	  <td width="86" align="right"><div align="left">Sucursal: &nbsp;</div></td>
      
		<td width="286"><select name="lstsuc" disabled="disabled" style="width:278px" onChange="comprueba()">
        <option value="null"><?=$sucursal?> </option>
      </select></td>   
              
  </tr>
<tr>
		<td width="94">E-Mail</td>
		<td width="148">
			<input name="txtemail" id="txtemail" type="text" value="<?=$txtemail?>" size="23" > </td>
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">Señor: &nbsp;</div></td>
		<td width="286"><input name="txtrsoc" id="txtrsoc" type="text" value="<?=$txtrsoc?>" size="42" /></td>
	</tr>
<tr>
		<td width="94">Fono: </td>
	  <td width="148"><input name="txtfono" id="txtfono" type="text" value="<?=$txtfono;?>" size="23" onKeyPress="validaenter(event,6);" /></td>
		<td width="26">&nbsp;</td>
	  <td width="86" align="right"><div align="left">Actividad:&nbsp;      </div></td>
		<td width="286"><input name="txtactividad" id="txtactividad" type="text" value="<?=$txtactividad?>" size="42" onKeyPress="validaenter(event,3);" /></td>
</tr>
	<tr>
		<td width="94">Fax:</td>
	  <td width="148"><input name="txtfax" id="txtfax" type="text"  value="<?=$txtfax?>" size="23" onKeyPress="validaenter(event,7);" /></td>
		<td width="26">&nbsp;</td>
	  <td width="86" align="right"><div align="left">Regi&oacute;n: &nbsp;</div></td>
		<td width="286"><select name="lstreg" onChange="rellenaCombo('form1');" style="width:278px">
          <option value="null" selected>SELECCIONE REGION
            <?
			$cons_region = mysql_query("SELECT * FROM regiones;", $enlace);
 			$cont=0;
			while ( mysql_fetch_object ($cons_region) ) 
 			{
 				$ides=mysql_result($cons_region, $cont, "Cod_Region");
  				$nombres=mysql_result($cons_region, $cont, "Region");
			
			?>
         <option value="<?=$ides?>"  <? if ($id_region==$ides){?> selected="selected"<? }?>><? echo $nombres;?></option>           <?
  				$cont = $cont + 1;
 			
			}
			?>
              </select>	</td>
	</tr>
	<tr>
		<td width="94">Iva Retenido:</td>
	  <td width="148"><input name="txtivar" id="txtivar" type="text" size="23" onKeyPress="return validan(event);" /></td>
		<td width="26">&nbsp;</td>
	  <td width="86" align="right"><div align="left">Comuna:&nbsp;</div></td>
	 <td width="286"><select name="lstcom" style="width:278px">
          <option value="null" selected>SELECCIONE COMUNA
            <?
			$cons_region = mysql_query("SELECT * FROM comunas;", $enlace);
 			$cont=0;
			while ( mysql_fetch_object ($cons_region) ) 
 			{
 				$ides=mysql_result($cons_region, $cont, "cod_comuna");
  				$nombres=mysql_result($cons_region, $cont, "comuna");
			
			?>
         <option value="<?=$ides?>"  <? if ($id_comuna==$ides){?> selected="selected"<? }?>><? echo $nombres;?></option>           <?
  				$cont = $cont + 1;
 			
			}
			?>
              </select>	</td>
	</tr>
		<tr>
		<td width="94">N&ordm; Dcto </td>
	<td width="148">
<input type="text" name="txtndcto" id="txtndcto" size="23" readonly="1" value="<?=$txtndcto;?>"/>		</td>
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">Ciudad:&nbsp;</div></td>
		<td width="286"><input name="txtciudad" id="txtciudad" type="text" value="<?=$txtciudad?>" size="42" onKeyPress="validaenter(event,4);" /></td>
	</tr>
	
	<tr>
		<td width="94">Zona</td>
        <td width="148"><input name="id_zona" id="id_zona" type="text" readonly="1" value="<?=$id_zona?>" size="23"  /></td>

		
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">Direcci&oacute;n:</div></td>
		<td width="286"><input name="txtdireccion" id="txtdireccion" type="text" value="<?=$txtdireccion?>" size="42" onKeyPress="validaenter(event,5);" /></td>
	</tr>
	<tr>
		<td width="94">Bodega</td>
		<td width="148">
		  <input type="text" name="txtbodega" id="txtbodega" size="23" readonly="1" value="<?= $txtbodega;?>"/>		</td>
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">Vendedor:</div></td>
        
        		  <td width="286"><input type="text" name="id_vendedor" id="id_vendedor" size="23" readonly="1" value="<?= $id_vendedor;?>"/>
		</td>
	</tr>
     <tr>
    <td>Credito</td>
    <td><input name="txtmontocredito" type="text" id="txtmontocredito" style="border-width:0;text-align:right;font-size:14px;" value="<?=$txtmontocredito;?>" size="14" readonly="1" /></td>
    <td>&nbsp;</td>
    <td>Tipo Pago:</td>
    <td><select name="lstpago" id="lstpago" style="width:200px" onChange="validactacte();">
     
      <?
			$cons_pago = mysql_query("SELECT * FROM tipo_pago WHERE descripcion ='CONTADO' OR descripcion = 'CREDITO'", $enlace);
 			$cont=0;
			while ( mysql_fetch_object ($cons_pago) ) 
 			{
 				$ides=mysql_result($cons_pago, $cont, "id");
  				$nombres=mysql_result($cons_pago, $cont, "descripcion");
  				if($lstpago==$ides){$sel="selected";}
				else {$sel="";}
				echo "              <option value=\"".$ides."\"$sel>".$nombres."\n";
				
  				$cont = $cont + 1;
 			}
			?>
    </select> 
    <input name="condicion_pago" id="condicion_pago" type="text" value="<?=$condicion_pago?>" size="12"/>
   </td>
       
  </tr>
    	<tr>
		<td width="94">Nota Venta</td>
		<td width="148"><input name="txtnotaventa" id="txtnotaventa" type="text" value="<?=$txtnotaventa?>" size="12" /></td>
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">O.C.</div></td>
		<td width="286"><input name="txtnumoc" id="txtnumoc" type="text" value="<?=$txtnumoc?>" size="12"/></td>
	</tr>
</table>


<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
  	<td width="10%"><div align="right">Observacion:</div></td>
    <td width="90%"><div align="left"> <input type="text" name="txtobs" id="txtobs" size="60" value="<?=$txtobs;?>"/></div></td>
     <td width="10">Firma</td>
       <td><input name="firmado" type="text" id="firmado" value="<?=$firmado?>" size="30" /></td>
    
  </tr>
</table>

<table width="640" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
	<tr bgcolor="#000000"> 
		<td width="8%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Cant.</font></strong></div></td>
		<td width="12%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">C&oacute;digo</font></strong></div></td>
		<td width="35%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Detalle</font></strong></div></td>
		
		<td width="12%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Precio</font></strong></div></td>
        <td width="12%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Desc %</font></strong></div></td>
		<td width="12%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Total $</font></strong></div></td>
        	<td width="1%">&nbsp;</td>
            <td width="1%">&nbsp;</div></td>
    </tr>
<?
$sql_pnv = "SELECT * FROM movimiento WHERE id_soe=$id_nota_venta AND empresa = $id_empresa";
$res_pnv= mysql_query($sql_pnv,$enlace);
$pnv_cont = 0;
$totalunidades=0;
$totalkilos=0;
$txtsubtotal=0;
$cant_real=1;
while ( mysql_fetch_object($res_pnv))
	{
	$cod_producto[$cant_real]=mysql_result($res_pnv, $pnv_cont, "cod_producto");
	$cantidad[$cant_real]=mysql_result($res_pnv, $pnv_cont, "cantidad");
	$precio[$cant_real]=intval(mysql_result($res_pnv, $pnv_cont, "precio_unitario"));
	$desc[$cant_real]= (mysql_result($res_pnv, $pnv_cont, "descuentos"));
	$sub_total[$cant_real]=intval(mysql_result($res_pnv, $pnv_cont, "sub_total"));
	$txtsubtotal+=$sub_total[$cant_real];
	$sql_pro = "SELECT * FROM productos WHERE codigo=$cod_producto[$cant_real]";
     $res_pro= mysql_query($sql_pro,$enlace);
	   $detalle[$cant_real]=mysql_result($res_pro, 0, "descripcion");
	    $peso=mysql_result($res_pro, 0, "peso");
 $afecto[$cant_real]=1;
 $detalle_1[$cant_real]='';
 
  $totalunidades=$totalunidades+$cantidad[$cant_real];
    $totalkilos=$totalkilos+($cantidad[$cant_real]*$peso);	
 
		?>
	
   
	
		
		<tr> 
			<td><div align="left">
			    <input name="cantidad[<? echo "$cant_real";?>]" id="cantidad[<? echo "$cant_real";?>]" type="text" value="<?=$cantidad[$cant_real];?>" readonly="1" style="border-width:0;width:100%" border="0">
			</div></td>
			<td><div align="left">
				  <input name="cod_producto[<? echo "$cant_real";?>]" id="cod_producto[<? echo "$cant_real";?>]" type="text" value="<?=$cod_producto[$cant_real];?>" readonly="1" style="border-width:0;width:100%" border="0">
			</div></td>
			<td><div align="left">  
				<input name="detalle[<? echo "$cant_real";?>]" id="detalle[<? echo "$cant_real";?>]" type="text" value="<?=$detalle[$cant_real];?>" readonly="1" style="border-width:0;width:100%" border="0">
              
			</div></td>
			
			  
			<td><div align="right">
				<input name="precio[<? echo "$cant_real";?>]" id="precio[<? echo "$cant_real";?>]" type="text" value="<?=$precio[$cant_real];?>" readonly="1" style="border-width:0;text-align:right;width:100%" border="0">
			</div></td>
			
            <td><div align="right">
              <input name="desc[<? echo "$cant_real";?>]" id="desc[<? echo "$cant_real";?>]" type="text" value="<?=$desc[$cant_real];?>" readonly="1" style="border-width:0;text-align:right;width:100%" border="0" />
            </div></td>
            	
			<td><div align="right">
				<input name="sub_total[<? echo "$cant_real";?>]" id="sub_total[<? echo "$cant_real";?>]" type="text" value="<?=$sub_total[$cant_real];?>" readonly="1" style="border-width:0;text-align:right;width:100%" border="0">
			</div></td>
            <td>
              <input name="afecto[<? echo "$cant_real";?>]" id="afecto[<? echo "$cant_real";?>]" type="text" value="<?=$afecto[$cant_real];?>" size="1" style="visibility:hidden" ></td>
            <td>
                <input name="detalle_1[<? echo "$cant_real";?>]" id="detalle_1[<? echo "$cant_real";?>]" type="text" value="<?=$detalle_1[$cant_real];?>" size="1" style="visibility:hidden" >            </td>
		</tr>
		<?  
		
	$pnv_cont += 1;
	$cant_real += 1;
	}
	
	if (abs(($sql_neto+$sql_descuento)-$txtsubtotal)>1)
	{
	?>
	<script language="javascript">
	alert("Nota de venta con problemas .... corregir .....");
	window.top.open('','_parent','');window.top.close();
	</script>
	<?
	}
	?>
</table>

<br>
<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="58%">&nbsp;</td>
	  <td width="17%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">SUB TOTAL</font></strong></div></td>
	  <td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<input name="sql_subtotal" id="sql_subtotal" type="text" size="9" value="<?=$txtsubtotal?>" readonly="1" style="border-width:0; text-align:right" border="0">
        </font></div></td>
    </tr>
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="58%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        <input name="por_descuento" id="por_descuento" type="text" size="9" value="<?=$por_descuento;?>" readonly="1" style="border-width:0; text-align:right" border="0">%
      </font></div></td>
	  <td width="17%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">DESCUENTO</font></strong></div></td>
	  <td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<input name="sql_descuento" id="sql_descuento" type="text" size="9" value="<?=$sql_descuento;?>" readonly="1" style="border-width:0; text-align:right" border="0">
        </font></div></td>
    </tr>
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="58%">&nbsp;</td>
	  <td width="17%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">NETO</font></strong></div></td>
	  <td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<input name="sql_neto" id="sql_neto" type="text" size="9" value="<?=$sql_neto?>" readonly="1" style="border-width:0; text-align:right" border="0">
        </font></div></td>
    </tr>
	<tr>
		<td width="5%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="58%">&nbsp;</td>
	  <td width="17%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">IVA</font></strong></div></td>
	  <td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
		  $<input name="sql_iva" id="sql_iva" type="text" size="9" value="<?=$sql_iva?>" readonly="1" style="border-width:0;text-align:right" border="0">
	    </font></div></td>
    </tr>
	<tr>
		<td width="5%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="58%">&nbsp;</td>
  	  <td width="17%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">TOTAL </font></strong></div></td>
  	  <td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
	    $<input name="sql_total" id="sql_total" type="text" size="9" value="<?=$sql_total?>" readonly="1" style="border-width:0;text-align:right" border="0">
      </font></div></td>
    </tr>
</table>
<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
  	<td width="100%" align="center"><!--<input type="button" name="Envia_SII" value="Envia_SII" onClick="fun_Imprimir_XML();" disabled="disabled" />
<input type="button" name="Imprimir_PDF" value="Imprimir_PDF" onClick="fun_Factura_PDF();" disabled="disabled" />

	  <input type="button" name="Guardar" id="Guardar" value="Guardar" onClick="fun_guardar();" />-->
	 <input type="button" name="Salir" id="Salir" value="Salir" onClick="fun_salir();" />
   
     </td>
     
	</tr>
</table>
<input name="txtcom" type="text" id="txtcom" style="visibility:hidden" size="1" />
<input name="txtcompser" type="text" id="txtcompser" style="visibility:hidden" size="1" />
<input name="txtguardado" type="text" id="txtguardado" style="visibility:hidden" size="1" value="0"/>
<input name="id_nota_venta" type="text" id="id_nota_venta" style="visibility:hidden" size="1" value="<?=$id_nota_venta?>"/>
<input name="txtnv" type="text" id="txtnv" style="visibility:hidden" size="1" value="<?=$id_nv;?>" />
<input name="num_factura" type="text" id="num_factura" style="visibility:hidden" size="1" value="<?=$sql_num_factura;?>" />
<input name="id_bodega" type="text" id="id_bodega" style="visibility:hidden" size="1" value="<?=$id_bodega?>" />
<input name="id_vendedor" type="text" id="id_vendedor" style="visibility:hidden" size="1" value="<?=$id_vendedor?>" />
<input name="id_zona" type="text" id="id_zona" style="visibility:hidden" size="1" value="<?=$id_zona?>" />
 
 <input name="txtzona" type="text" id="txtzona" style="visibility:hidden" size="1" value="<?=$txtzona?>" />
<input name="id_sucursal" type="text" id="id_sucursal" style="visibility:hidden" size="1" value="<?=$id_sucursal?>" />
<input name="lstdcto" type="text" id="lstdcto" style="visibility:hidden" size="1" value="<?=$lstdcto?>" />
<input name="id_condicion" type="text" id="id_condicion" style="visibility:hidden" size="1" value="<?=$id_condicion?>" />
<input name="cant_real" type="text" id="cant_real" style="visibility:hidden" size="1" value="<?=$cant_real?>" />
<input name="totalunidades" type="text" id="totalunidades" style="visibility:hidden" size="1" value="<?=$totalunidades?>" />
<input name="totalkilos" type="text" id="totalkilos" style="visibility:hidden" size="1" value="<?=$totalkilos?>" />
</form>

</body>
</html>
<script>
	var nav4 = window.Event ? true : false;
	<!-- Ingreso solo numeros y k K - . -->
function valida(evt)
	{
	var key = nav4 ? evt.which : evt.keyCode;
	if (key==13) document.form1.txtrsoc.focus()
	return (key <= 13 || (key >= 48 && key <= 57) || (key == 107) || (key == 75) || (key == 45) || (key == 46));
	}
function validan(evt)
	{
	var key = nav4 ? evt.which : evt.keyCode;
	if (key==13) document.form1.txtrsoc.focus()
	return (key <= 13 || (key >= 48 && key <= 57) || (key == 46));
	}
function validaenter(evt, donde)
	{
	var key = nav4 ? evt.which : evt.keyCode;
	if (key==13&& donde==1) document.form1.txtemail.focus();
	if (key==13&& donde==2) document.form1.txtactividad.focus();
	if (key==13&& donde==3) document.form1.txtciudad.focus();
	if (key==13&& donde==4) document.form1.txtdireccion.focus();
	if (key==13&& donde==5) document.form1.txtfono.focus();
	if (key==13&& donde==6) document.form1.txtfax.focus();
	if (key==13&& donde==7) document.form1.menubol4.focus();
	}

function fun_guardar()
	{
	if (document.form1.txtguardado.value==0)
		{
		
		if (document.form1.txtrut.value!='' && document.form1.txtrsoc.value!='' && document.form1.txtdireccion.value!='' && document.form1.txtciudad.value!='' && document.form1.lstcom.value!= 'null')
			{
			
				document.form1.action="guarda_factura.php?cnt=<?=$cnt?>";
				document.form1.target="_blank";
				document.form1.submit();
				document.form1.txtguardado.value=1;
					document.form1.Guardar.disabled=true;
					   document.form1.Salir.disabled=true;
				    	 document.form1.Imprimir_PDF.disabled=true;
						 	document.form1.Envia_SII.disabled=false;
			}
		else
			{
			alert('Ingrese todos los datos del cliente');
			return false;
			}
		}
	else
		{
		alert('Este documento ya ha sido guardado');
		return false;
		}
	}
function fun_salir()
	{
	
		var answer = confirm ("¿Seguro desea salir?");
		if (answer)
			{
			window.top.open('','_parent','');window.top.close();
			}
		
	}
</script>
<script language="javascript">
function rellenaCombo(formulario)
	{
	with (document.forms[formulario]) 
        {
          var region = lstreg[lstreg.selectedIndex].value; 
          var n = lstcom.length;  
          
          lstcom.disabled = false;  
          
          for (var i = 0; i < n; ++i)
            lstcom.remove(lstcom.options[i]); 
          
          lstcom[0] = new Option("SELECCIONE CIUDAD", 'null'); 
          
          if (region != 'null')  
          {
   <?php
   
    $cons_region = mysql_query("SELECT * FROM regiones;", $enlace);
    $cont=0;
    while ( mysql_fetch_object ($cons_region) )
    {
     $reg = mysql_result($cons_region, $cont, "Cod_Region");
   ?>
            if (region == '<?php echo $reg;?>')
            {
    <?php
    
      $cons_comuna = mysql_query("SELECT * FROM comunas WHERE region = ".$reg." ORDER BY comuna;", $enlace);
      $cont2=0;
      while ( mysql_fetch_object ($cons_comuna) )
      {
       $id_comuna = mysql_result($cons_comuna, $cont2, "cod_comuna");
       $comuna = mysql_result($cons_comuna, $cont2, "comuna");
    ?>
              lstcom[lstcom.length] = new Option("<?php echo $comuna;?>", '<?php echo $id_comuna;?>');
    <?php
    
	   	$cont2 = $cont2 + 1;
      }
    ?>
            }
   <?php
   
   		$cont = $cont + 1;
    }
   ?>
            lstcom.focus();  
          }
          else  
          {
            lstcom.disabled = true;  
            lstreg.focus(); 
          }
          
          lstcom.selectedIndex = 0;  
        }
	}
function valval()
	{
	var reg=document.form1.lstreg.value;
	rellenaCombo('form1');
	document.form1.lstcom.value=document.form1.txtcom.value;
	}

function fun_Factura_PDF()
	{
	var firma = document.form1.firmado.value;
	document.form1.action = '../LibFE/DOC/PDF/'+firma;
	document.form1.target='_blank';
	document.form1.submit();
		
		document.form1.Salir.disabled=false;
	}	
function fun_Imprimir_XML()
	{
	document.form1.action = 'factura_xml.php?cnt=<?=$cant_real?>';
	document.form1.target='_blank';
	document.form1.submit();
    //document.form1.Envia_SII.disabled=true;
	document.form1.Imprimir_PDF.disabled=false;
	
	}




</script>
