<?
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>
<!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>Guia/Factura</title>
</head>
<?

$cant = "1";
$flag=0;
$id_nv=$_POST["txt_nv"];
/********************************** 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");
		$empresa_comuna=mysql_result($res_empresa, $row, "comunas.comuna");
		$empresa_actividad=mysql_result($res_empresa, $row, "actividad");
		$empresa_rut_representante=mysql_result($res_empresa, $row, "rut_representante");
		$empresa_nombre_representante=mysql_result($res_empresa, $row, "nombre_representante");
		$row=$row+1;
		}
$sql_nv = "SELECT s.id AS id,s.tipo_pago AS fp, s.num_dcto AS nv, s.tipo_dcto AS tdc, s.rut_cliente AS rc, s.fecha_emision AS fe,s. fecha_vencimiento AS fv, s.neto AS neto, s.iva AS iva, s.total AS total, s.iva_retenido AS ivar FROM salida AS s, tipo_dcto AS td WHERE s.tipo_dcto = td.id AND td.descripcion ='NC' AND s.num_dcto=$id_nv AND s.empresa = $id_empresa";
$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, "id");
	$id_dsal=$sqlnv_id;
	$sqlnv_nv=mysql_result($res_nv, $nv_cont, "nv");
	$sqlnv_td=mysql_result($res_nv, $nv_cont, "tdc");
	$id_td=$sqlnv_td;
	$fechaven=mysql_result($res_nv, $nv_cont, "fv");
	$id_fp=mysql_result($res_nv, $nv_cont, "fp");
	$sqlnv_rc=mysql_result($res_nv, $nv_cont, "rc");
	$sqlnv_fe=mysql_result($res_nv, $nv_cont, "fe");
	$sql_neto=mysql_result($res_nv, $nv_cont, "neto");
	$sql_iva=mysql_result($res_nv, $nv_cont, "iva");
	$sql_total=mysql_result($res_nv, $nv_cont, "total");
	$sqlnv_ivar=mysql_result($res_nv, $nv_cont, "ivar");
	$nv_cont += 1;
	}
	if ($nv_cont==0){
	?>
	<script language="javascript">
		alert("No existe Factura relacionada al numero ingresado");
		window.top.open('','_parent','');
		window.top.close();
	</script>
	<?
	}
	$ano = substr ($fechaven, 0, 4);
	$mes = substr ($fechaven, 5, 2);
	$dia = substr ($fechaven, 8, 2); 
	$fechaven = "$dia/$mes/$ano";
	$ano = substr ($sqlnv_fe, 0, 4);
	$mes = substr ($sqlnv_fe, 5, 2);
	$dia = substr ($sqlnv_fe, 8, 2); 
	$fecha_em = "$dia/$mes/$ano";	
?>

<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="180px"><div align="left"><font size="1">
		<?=$empresa_nombre?><br>
		<?=$empresa_direccion?><br>
		<?=$empresa_ciudad;?>

		</font></div>
	</td>
    <td width="400px"><div align="left"><font size="6">NC Nš <?=$id_nv?></font></div></td>
  </tr>
</table>

<br>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr> 
		<td width="10%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"></font></strong></div></td>
		<td width="20%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"></font></strong></div></td>
		<td width="40%"><div align="center"></div></td>
		<td width="20%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"></font></strong></div></td>
		<td width="10%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"></font></strong></div></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_em?>" size="23" readonly="1">
	  </td>
		<td width="26">&nbsp;</td>
		<td width="86">Vencimiento</td>
		<td width="286"><input name="fechaven" type="text" id="fechaven" style="border-width:0;" value="<?=$fechaven?>" size="12" readonly="1" />	
	</td>
	</tr>
	<tr>
		<td width="94">R.U.T.</td>
		<td width="148">
			<input name="txtrut" type="text" value="<?=$sqlnv_rc?>" onBlur="comprueba()" size="23" maxlength="12" readonly="1" />
	  </td>
		<td width="26" align="left"><a onClick="JavaScript:PopWindow('buscacliente.php')">
			<IMG align="middle" border="0" src="Images/modificar.gif" style="cursor:pointer" title="Presione aqui para buscar cliente" width="16" height="16"></a>		</td>
		<td width="86" align="right"><div align="left">Seņor: &nbsp;</div></td>
		<td width="286">
		  <input name="txtrsoc" id="txtrsoc" type="text" size="44" readonly="1"  />
		</td>           
  </tr>
<tr>
		<td width="94">E-Mail</td>
		<td width="148">
			<input name="txtemail" id="txtemail" type="text" size="23" readonly="1" >
		</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" size="44" readonly="1" >
		</td>
	</tr>
<tr>
		<td width="94">Fono: </td>
		<td width="148"><input name="txtfono" id="txtfono" type="text" size="23" readonly="1"  /></td>
		<td width="23">&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:290px" readonly="1" >
        <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");
  				echo "              <option value=\"".$ides."\">".$nombres."\n";
  				$cont = $cont + 1;
 			}
			?>
      </select></td>
</tr>
	<tr>
		<td width="94">Fax:</td>
		<td width="148"><input name="txtfax" id="txtfax" type="text"  size="23" readonly="1"  /></td>
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">Comuna:&nbsp;		</div></td>
		<td width="286"><select name="lstcom" disabled="disabled" style="width:290px">
        <option value="null">SELECCIONE COMUNA
        </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?>" readonly="1" /></td>
		<td width="26">&nbsp;</td>
		<td width="88" align="right"><div align="left">Ciudad:&nbsp;		</div></td>
		<td width="286"><input name="txtciudad" id="txtciudad" type="text" size="44" readonly="1" /></td>
	</tr>
		<tr>
		<td width="94">&nbsp;</td>
		<td width="148">&nbsp;</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" size="44" readonly="1"  /></td>
	</tr>
</table>

<br>
<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="40%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Detalle</font></strong></div></td>
		<td width="16%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Nš Chassis</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">Total $</font></strong></div></td>
    </tr>
<?
$sql_pnv = "SELECT * FROM movimiento WHERE numero_dcto = $sqlnv_nv AND tipo_dcto = $sqlnv_td AND empresa = $id_empresa";
$res_pnv= mysql_query($sql_pnv,$enlace);
$pnv_cont = 0;
$descuentos=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=mysql_result($res_pnv, $pnv_cont, "precio_unitario");
	$descuento=mysql_result($res_pnv, $pnv_cont, "descuentos");
	$descuentos+=$descuento;
	$ltotal=$precio*$cantidad;
	$txtsubtotal+=$ltotal;
	/********************************** Verifica Seriado *********************************************************************/
	
	$sql_bpro = "SELECT * FROM productos WHERE codigo = $cod_producto AND empresa = $id_empresa";
	$res_bpro = mysql_query($sql_bpro,$enlace);
	$row=0;
	$bpro_seriado=0;
	while (mysql_fetch_object ($res_bpro))
		{
		$bpro_seriado = mysql_result($res_bpro, $row, "producto_seriado_param");
		$detalle = mysql_result($res_bpro, $row, "descripcion");
		$row=$row+1;
		}
	if ($bpro_seriado==1){
		$sql_s = "SELECT * FROM seriado WHERE num_salida=$sqlnv_id";
		$res_s = mysql_query($sql_s,$enlace);
		$rows=0;
		$chassis="";
		while (mysql_fetch_object ($res_s))
			{
			$chassis = mysql_result($res_s, $rows, "chassis");
			$rows=$rows+1;
			}
		}
		?>
		
		
		
		<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="codigo[<? echo "$cant_real";?>]" id="codigo[<? echo "$cant_real";?>]" type="text" value="<? echo "$cod_producto";?>" 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="<? echo "$detalle";?>" readonly="1" style="border-width:0;width:100%" border="0">
			</div></td>
			<td><div align="left">  
			
				<input name="seriado[<? echo "$cant_real";?>]" id="seriado[<? echo "$cant_real";?>]" type="text" <? if ($bpro_seriado==0 || $chassis!='' ){?> readonly="1"<? }?> style="border-width:0;width:100%" border="0"  <? if ($bpro_seriado==0 || $chassis=='' ){ echo "onBlur='busca_seriado('$cant_real');";}?> <? if ($bpro_seriado ==1){ echo "value='$chassis'";}?> >
			</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="total[<? echo "$cant_real";?>]" id="total[<? echo "$cant_real";?>]" type="text" value="<? echo "$ltotal";?>" readonly="1" style="border-width:0;text-align:right;width:100%" border="0">
			</div></td>
		</tr>
		<?  
		
	$pnv_cont += 1;
	$cant_real += 1;
	}
	?>
	
</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="35%">&nbsp;</td>
		<td width="40%"><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="35%">&nbsp;</td>
		<td width="40%"><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="<?=$descuentos;?>" 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="35%">&nbsp;</td>
		<td width="40%"><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="35%">&nbsp;</td>
		<td width="40%"><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="35%">&nbsp;</td>
	  	<td width="40%"><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="Anular" id="Anular" value="Anular" style="visibility:hidden" onClick="fun_anular();" />
			<input type="button" name="salir" id="salir" value="Salir" onClick="fun_salir2();" /></td>
			<input type="button" name="Imprimir" id="imprimir" style="visibility:hidden" value="Imprimir" onClick="fun_imprimir();" />
	  	  <input type="button" name="guardar" id="guardar" style="visibility:hidden" value="Guardar" onClick="fun_guardar();" />
	  	  
	</tr>
</table>
<input name="txtcom" type="text" id="txtcom" style="visibility:hidden" size="1" />
</form>

</body>
</html>
<script>


function PopWindow(pagina)
	{
	window.open(pagina,'TEST','width=1,height=1,menubar=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no,top=70,left=300');
	}

function comprueba()
	{
	if (Rut(document.form1.txtrut.value)) PopWindow('compruebaf.php?rutc='+document.form1.txtrut.value);
	}
	
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;
	document.form1.lstreg.readOnly= 1;
	document.form1.lstcom.readOnly= 1;
	}

function fun_salir2()
	{
	window.top.open('','_parent','');window.top.close();
	}	
document.form1.txtrut.focus();
document.form1.txtrsoc.focus();
</script>
