<?
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>Factura FE de GE</title>
</head>
<?

$cant = "1";
$flag=0;
$id_nv=$_POST["txt_nv"];
$id_bodega=1; //$_POST["id_bodega"];
$lstdcto=33;
$total_cant=0;
$total_kilos=0;
/********************************** 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 WHERE id=$id_nv";



$res_nv= mysql_query($sql_nv,$enlace);
$nv_cont = 0;

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");
	
	$fecharef=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");
	
	$tiporef=mysql_result($res_nv, $nv_cont, "tipo_dcto");
	
	$sqlnv_nv=mysql_result($res_nv, $nv_cont, "num_dcto");
	$folioref=$sqlnv_nv;
	$codref=0;
	$razonref="Factura guia ".$folioref;
	$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"));
	$sql_iva=intval(mysql_result($res_nv, $nv_cont, "iva"));
	$sql_ila=intval(mysql_result($res_nv, $nv_cont, "ila"));
	$sql_iaba=intval(mysql_result($res_nv, $nv_cont, "iaba"));
	$sql_iaba_18=intval(mysql_result($res_nv, $nv_cont, "iaba18"));
	$sql_harina=intval(mysql_result($res_nv, $nv_cont, "impuestos"));
	$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");
	$despachar=mysql_result($res_nv, $nv_cont, "despachar");
	$pagada=mysql_result($res_nv, $nv_cont, "pagada");
	$nv_cont += 1;
	}
	if ($nv_cont==0 || $control!=0){
	?>
	<script language="javascript">
	alert("No existe GUIA 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 = $id_bodega 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>
	<?
		   }
	
	$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
	
		$firmado="";
		   $patente="";
				$tra_rut="";
						$dir_destino="";
								$com_destino="";
										$ciu_destino="";
?>


<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="<?=$sqlnv_rut?>"  readonly="1" size="23" maxlength="12"/>	  </td>
		
      <td>&nbsp;</td>
		<td width="286"><input name="lstsuc" id="lstsuc" disabled="disabled"     value="<?=$sucursal?>" ></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" /></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"  /></td>
</tr>
	<tr>
		<td width="94">Fax:</td>
	  <td width="148"><input name="txtfax" id="txtfax" type="text"  value="<?=$txtfax?>" size="23"  /></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"  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"  value="<?=$sqlnv_ivar;?>" /></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" size="23" readonly="1"  value="<?=$sql_num_factura;?>"  />		</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"  /></td>
	</tr>
	
	<tr>
		<td width="94">Zona</td>
		<td width="148">
		  <?
			$sql_umed = "SELECT * FROM zona ORDER BY descripcion";
			$res_umed = mysql_query($sql_umed,$enlace);
			$contb=0;
			?>
              <select name="lstzona" id="lstzona" style="width:160px;">
			  <option value="0" selected="selected">SELECCIONE ZONA</option>
			  <?
			  while (mysql_fetch_object ($res_umed))
				{
				$sql_umed_cod = mysql_result($res_umed, $contb, "id");
				$sql_umed_nom = mysql_result($res_umed, $contb, "descripcion");
			?>
                <option value="<?=$sql_umed_cod?>"  <? if ($id_zona==$sql_umed_cod){?> selected="selected"<? }?>><? echo $sql_umed_nom;?></option>               
                
			<?
				$contb = $contb+1;
			}
			?>
              </select>		</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" /></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"><?
			$sql_umed ="SELECT id , nombres AS descripcion FROM vendedor ORDER BY nombres ASC";
			$res_umed = mysql_query($sql_umed,$enlace);
			$contb=0;
			?>
              <select name="lstvendedor" id="lstvendedor" style="width:278px;">
			  <option value="0" selected="selected">SELECCIONE VENDEDOR</option>
			  <?
			  while (mysql_fetch_object ($res_umed))
				{
				$sql_umed_cod = mysql_result($res_umed, $contb, "id");
				$sql_umed_nom = mysql_result($res_umed, $contb, "descripcion");
			?>
                <option value="<?=$sql_umed_cod?>" <? if ($id_vendedor==$sql_umed_cod){?> selected="selected"<? }?>> <? echo $sql_umed_nom." (".$sql_umed_cod.")";?></option>
			<?
				$contb = $contb+1;
			}
			?>
              </select>		</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" >
     
      <?
			$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");
				?>
  				 <option value="<?=$ides?>" <? if ($lstpago==$ides){?> selected="selected"<? }?>> <? echo $nombres." (".$ides.")";?></option>
				<?
  				$cont = $cont + 1;
 			}
			
	
			?>
            
            
            
            
    </select> <?=$condicion_pago;?>   </td>
  </tr>
    	<tr>
		<td width="94">Guia</td>
		<td width="148" align="center"><input name="txtnotaventa" id="txtnotaventa" type="text" value="<?=$sqlnv_nv?>" size="10" /></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>
    <tr>
		<td width="94">Pagado</td>
		<td width="148" align="center"><input name="pagada" id="pagada" type="text" value="<?=$pagada?>" size="10" /></td>
		<td width="26">&nbsp;</td>
		<td width="26">&nbsp;</td>
		<td width="26">&nbsp;</td>
	</tr>
</table>


<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
  	<td><div align="right">Observacion:</div></td>
    <td><div align="left"> <input type="text" name="txtobs" id="txtobs" size="50" value="<?=$despachar;?>"/></div></td>
     <td>Firma</td>
       <td><input name="firmado" type="text" id="firmado" value="<?=$firmado?>" size="18" /></td>
       <td>Token</td>
     <td><input name="token" type="text" id="token" value="<?=$token?>" size="15" /></td>
  </tr>
</table>
 <table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td>Tipo_Ref&nbsp; </td>
      <td><input name="tiporef" type="text" id="tiporef" value="<?=$tiporef;?>" size="10" readonly="readonly" style=" text-align:center;border-width:1;width:90%" /></td>
		
    <td>Folio_Ref&nbsp; </td>
     <td><input name="folioref" type="text" id="folioref"  value="<?=$folioref;?>" readonly="readonly" size="10" style=" text-align:center;border-width:1;width:90%" /></td>
	
    <td>Fecha_Ref&nbsp; </td>    
    <td><input name="fecharef" type="text" id="fecharef"  value="<?=$fecharef?>" size="10" readonly="1" style=" text-align:center;border-width:1;width:90%"/></td>


 <td>Cod_Ref&nbsp;</td>
		
        <td><input name="codref" type="text" id="codref" value="<?=$codref;?>" size="5" style=" text-align:center;border-width:1;width:90%"/></td>
   
    <td>Razon_Ref&nbsp;</td>
    <td><input name="razonref" type="text" id="razonref"  value="<?=$razonref;?>" size="50" style=" text-align:center;border-width:1;width:90%"/></td>
  </tr>
 </table>

<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
  	<td><div align="right">Transporte Patente:</div></td>
    <td><div align="left"> <input type="text" name="patente" id="patente" size="10" value="<?=$patente;?>"/></div></td>
     <td width="10">Rut Transporte</td>
     <td><div align="left"> <input type="text" name="tra_rut" id="tra_rut" size="10" value="<?=$tra_rut;?>"/></div></td>
      <td width="10">Direccion Destino</td>
      <td><div align="left"> <input type="text" name="dir_destino" id="dir_destino" size="20" value="<?=$dir_destino;?>"/></div></td>
       <td width="10">Comuna Destino</td>
       <td><div align="left"> <input type="text" name="com_destino" id="com_destino" size="10" value="<?=$com_destino;?>"/></div></td>
        <td width="10">Ciudad Destino</td>
        <td><div align="left"> <input type="text" name="ciu_destino" id="ciu_destino" size="10" value="<?=$ciu_destino;?>"/></div></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="8%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Unidad</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"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">.</font></strong></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;
$txtsubtotal=0;
$cant_real=1;
while ( mysql_fetch_object($res_pnv))
	{
	$cod_producto=mysql_result($res_pnv, $pnv_cont, "cod_producto");
	$cantidad=mysql_result($res_pnv, $pnv_cont, "cantidad");
	$precio=intval(mysql_result($res_pnv, $pnv_cont, "precio_unitario"));
	$desc=mysql_result($res_pnv, $pnv_cont, "descuentos");
	$sub_total=intval(mysql_result($res_pnv, $pnv_cont, "sub_total"));
	$txtsubtotal+=$sub_total;
	$sql_pro = "SELECT * FROM productos WHERE codigo=$cod_producto";
     $res_pro= mysql_query($sql_pro,$enlace);
	   $detalle=mysql_result($res_pro, 0, "descripcion");
	   $peso=mysql_result($res_pro, 0, "peso");
	   
	  $cod_imp=0;
	      if (mysql_result($res_pro, 0, "ila") >0) $cod_imp=25;
	       if (mysql_result($res_pro, 0, "iaba")>0) $cod_imp=27;
	        if (mysql_result($res_pro, 0, "harina")>0) $cod_imp=19;
	  
	   
	   
         $unidad_medida=mysql_result($res_pro, 0, "unidad_medida");
		 $res_ven = mysql_query("SELECT * FROM unidad_medida WHERE codigo=$unidad_medida",$enlace);
	if(mysql_fetch_object($res_ven)) $medida = mysql_result($res_ven, 0, "descripcion");
	
	$total_cant+=$cantidad;
	  $total_kilos+=$cantidad*$peso;
		?>
		
		
		
		<tr> 
			<td><div align="left">
			    <input name="cantidad[<? echo "$cant_real";?>]" id="cantidad[<? echo "$cant_real";?>]" type="text" value="<? echo "$cantidad";?>" readonly="1" style="border-width:0;width:100%" border="0">
			</div></td>
			<td><div align="left">
				  <input name="unidad[<? echo "$cant_real";?>]" id="unidad[<? echo "$cant_real";?>]" type="text" value="<? echo "$medida";?>" readonly="1" style="border-width:0;width:100%" border="0">
			</div></td>
            <td><div>
				  <input name="codigo[<? echo "$cant_real";?>]" id="codigo[<? echo "$cant_real";?>]" type="text" value="<? echo "$cod_producto";?>" readonly="1" style=" text-align:center;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="<? echo "$detalle";?>" 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="<? echo "$precio";?>" 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="<? echo "$desc";?>" 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="<? echo "$sub_total";?>" readonly="1" style="border-width:0;text-align:right;width:100%"  border="0"  />
			</div></td>
            
            <td><div align="center">
			  <input name="cod_imp[<? echo "$cant_real";?>]" id="cod_imp[<? echo "$cant_real";?>]" type="text" value="<? echo "$cod_imp";?>" readonly="1" border="0" size="1" style="visibility:hidden"/>
			</div></td>
		</tr>
		<?  
		
	$pnv_cont += 1;
	$cant_real += 1;
	}
	if (abs(($sql_neto+$sql_descuento)-$txtsubtotal)>1)
	{
	?>
	<script language="javascript">
	alert("Guia con problemas .... corregir .....");
	//window.top.open('','_parent','');window.top.close();
	</script>
	<?
	}
	?>
</table>

<table width="580" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
	  	<td>&nbsp;</td>
        	<td>&nbsp;</td>
            	<td>&nbsp;</td>
  	 
	  <td width="179"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">SUB TOTAL</font></strong></div></td>
	  <td width="137"><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>&nbsp;</td>
        	<td>&nbsp;</td>
            	<td>&nbsp;</td>
  	 
		<td><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">DESCUENTO</font></strong></div></td>
	  <td width="137"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<input name="txtdes" id="txtdes" type="text" size="9" value="<?=$sql_descuento;?>" readonly="1" style="border-width:0; text-align:right" border="0" />
      </font></div></td>
    </tr>
	<tr>
	  	<td>&nbsp;</td>
  	  	<td>&nbsp;</td>
	  	<td>&nbsp;</td>
	  <td><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">NETO</font></strong></div></td>
	  <td width="137"><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>&nbsp;</td>
  	  	<td>&nbsp;</td>
	  	<td>&nbsp;</td>
	  <td><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">IVA 19%</font></strong></div></td>
	  <td width="137"><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>&nbsp;</td>
  	  	<td>&nbsp;</td>
	  	<td>&nbsp;</td>
	  <td><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">ILA 20.5%</font></strong></div></td>
	  <td width="137"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
		  $<input name="sql_ila" id="sql_ila" type="text" size="9" value="<?=$sql_ila?>" readonly="1" style="border-width:0;text-align:right" border="0">
	    </font></div></td>
    </tr>
    <tr>
		<td>&nbsp;</td>
  	  	<td>&nbsp;</td>
	  	<td>&nbsp;</td>
	  <td><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">IABA 10%</font></strong></div></td>
	  <td width="137"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
		  $<input name="sql_iaba" id="sql_iaba" type="text" size="9" value="<?=$sql_iaba?>" readonly="1" style="border-width:0;text-align:right" border="0">
	    </font></div></td>
    </tr>
     <tr>
		<td>&nbsp;</td>
  	  	<td>&nbsp;</td>
	  	<td>&nbsp;</td>
	  <td><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">IABA 18%</font></strong></div></td>
	  <td width="137"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
		  $<input name="sql_iaba_18" id="sql_iaba_18" type="text" size="9" value="<?=$sql_iaba_18?>" readonly="1" style="border-width:0;text-align:right" border="0">
	    </font></div></td>
    </tr>
    <tr>
		<td>&nbsp;</td>
  	  	<td>&nbsp;</td>
	  	<td>&nbsp;</td>
	  <td><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">ImptoHarina 12%</font></strong></div></td>
	  <td width="137"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
		  $<input name="sql_harina" id="sql_harina" type="text" size="9" value="<?=$sql_harina?>" readonly="1" style="border-width:0;text-align:right" border="0">
	    </font></div></td>
    </tr>
	<tr>
		<td>&nbsp;</td>
  	  	<td>&nbsp;</td>
	  	<td>&nbsp;</td>
  	  <td><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">TOTAL </font></strong></div></td>
  	  <td width="137"><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="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>
       <td><input type="button" name="Borra_desc" id="Borra_desc" value="Borra_desc" onClick="fun_Borra_desc();" disabled="disabled" /> </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="lstdcto" type="text" id="lstdcto" style="visibility:hidden" size="1" value="<?=$lstdcto?>"/>
<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="cant_real" type="text" id="cant_real" style="visibility:hidden" size="1" value="<?=$cant_real?>" />
<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="id_sucursal" type="text" id="id_sucursal" style="visibility:hidden" size="1" value="<?=$id_sucursal?>" />
<input name="id_condicion" type="text" id="id_condicion" style="visibility:hidden" size="1" value="<?=$id_condicion?>" />
<input name="condicion_pago" type="text" id="condicion_pago" style="visibility:hidden" size="1" value="<?=$condicion_pago?>" />

</form>

</body>
</html>

	

<script language="javascript">





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_guia_xml.php?cnt=<?=$cant_real?>';
				document.form1.target='_blank';
				document.form1.submit();
			/*	document.form1.txtguardado.value=1;
				document.form1.Envia_SII.disabled=false;	
				document.form1.Guardar.disabled=true;
				document.form1.Borra_desc.disabled=true;		
				*/			
			}
		else
			{
			alert('Ingrese todos los datos del cliente');
			return false;
			}
		}
	else
		{
		alert('Este documento ya ha sido guardado');
		return false;
		}
	}
function fun_salir()
	{
	
			window.top.open('','_parent','');window.top.close();
		
	}




function fun_Borra_desc()
	{
	 	document.form1.action="borra_descuentos.php";
		document.form1.target="_self";
		document.form1.submit();
	}

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;
	}	


</script>
