<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = 'index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
$id_bodega=1;
$lstpago=2;
$id_autoriza=44;
?>
<?
/*Sesiones*/
include("../conex.php");

?>

<!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>Venta</title>
</head>
<?
/********************************** Datos vendedor ******/
	$sql_vendedor = "SELECT * FROM vendedor, usuarios WHERE vendedor.id=usuarios.id_vendedor AND usuarios.id=$id_user";
	$res_vendedor = mysql_query($sql_vendedor,$enlace);
	
		$id_vendedor=mysql_result($res_vendedor, 0, "vendedor.id");
        $vende=mysql_result($res_vendedor, 0, "nombres");



/********************************** Datos Empresa ****************** seleccionar cual empresa  es el ingreso  en sesion******/
	$sql_empresa = "SELECT * FROM empresa WHERE id=$id_empresa;";
	$res_empresa = mysql_query($sql_empresa,$enlace);
	
		$empresa_nombre=mysql_result($res_empresa, 0, "nombre");

//************************************************** Busca numero documento ***********************************************/	
	$sql_par = "SELECT correlativo FROM tipo_dcto_num_bodega  WHERE id_bodega = $id_bodega AND id_dcto=3";
	$res_par = mysql_query($sql_par,$enlace);	
		$txtndcto=mysql_result($res_par, 0, "correlativo");
		$txtndcto+=1;	
/************************************************** fecha ***********************************************/	
		 $fecha = date(d)."/".date(m)."/".date(Y);
    	 $hora = date('H:i:s');  
?>

<form id="form1" name="form1" method="post" action="guarda.php" target="_self">

<table width="503" align="center" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="63"><div align="center"><img src="../Images/logo.gif" width="60px" height="60px"></div></td>
	<td width="75"><div align="left"><font size="1"><?=$empresa_nombre?></font></div></td>
    <td width="141"><div align="center"> Nota de Venta N&ordm;&nbsp;<b><?=$txtndcto;?></b></div></td>
    <td width="224"><div align="center"> Fecha:&nbsp;<?=$fecha;?><br/>Hora:&nbsp;<?=$hora;?></div></td>
  </tr>
</table>

<table width="363" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="90"><div align="center">Vendedor: <b><?=$vende;?></b></div></td>
    <td width="273" align="center">
	  <?
	  $sql_umed ="SELECT id, razon_social, sucursal  FROM sucursales where id_vendedor=$id_vendedor ORDER BY razon_social, sucursal ASC";
	  $res_umed = mysql_query($sql_umed,$enlace);
	  $contb=0;
	  ?>
	  <select name="lstcliente" id="lstcliente" style="width:240px" > 
	  <option value="0" selected="selected">Cliente(Todos)</option>
	  <?
	  while(mysql_fetch_object($res_umed)){
	    $sql_umed_cod = mysql_result($res_umed, $contb, "id");
		$sql_umed_nom = mysql_result($res_umed, $contb, "razon_social");
		$sql_umed_suc = mysql_result($res_umed, $contb, "sucursal");
	  ?>
      <option value="<?=$sql_umed_cod?>"><? echo $sql_umed_nom.'  '.$sql_umed_suc;?></option>
	  <?
      $contb = $contb+1;
	  }
	  ?>
      </select>
    </td>
  </tr>
</table>

<table width="293" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#000000">
<tr bgcolor="#000000"> 
<td width="36" align="center"><div><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Can</font></strong></div></td>
<td width="24" align="center"><div><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Sal</font></strong></div></td>
<td width="117" align="center"><div><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Detalle</font></strong></div></td>
<td width="106" align="center"><div><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Precio</font></strong></div></td>
<td width="10" align="center" bordercolor="#FFFFFF" bgcolor="#FFFFFF">&nbsp;</td>
</tr>
<? 
$totalkilos=0;
$txtst=0;

          $sql_pro = "select codigo, descripcion, precio, saldo FROM productos WHERE empresa=$id_empresa AND cod_familia<>1 ORDER BY descripcion ASC";


		  	$res_pro = mysql_query($sql_pro,$enlace);	
			$cant=0;
			
	while (mysql_fetch_object ($res_pro))

	{
	$cantidad[$cant] ='';
	$auxsaldo[$cant] = mysql_result($res_pro, $cant, "saldo");
	$codigo[$cant] = mysql_result($res_pro, $cant, "codigo");
	$detalle[$cant] = mysql_result($res_pro, $cant, "descripcion");
	$precio[$cant] = mysql_result($res_pro, $cant, "precio");
	?>
	
<tr>
  <td><input name="cantidad[<?=$cant;?>]" id="cantidad[<?=$cant;?>]" type="text" value="<?=$cantidad[$cant];?>" onKeyPress="return validanum2(event,'<?=$cant;?>')"size="3" style="text-align:center;font-size:10px;">        
  </td>	 
    
  <td><input name="auxsaldo[<?=$cant;?>]" id="auxsaldo[<?=$cant;?>]" type="text" value="<?=$auxsaldo[$cant];?>" readonly="1" size="3" style="text-align:center;font-size:10px;">
  </td> 
     
  <td><input name="detalle[<?=$cant;?>]" id="detalle[<?=$cant;?>]" type="text" value="<?=$detalle[$cant];?>" readonly="1"  size="25" style="text-align:left;font-size:10px;" >
  </td>

  <td><input name="precio[<?=$cant;?>]"  id="precio[<?=$cant;?>]" type="text" value="<?=$precio[$cant];?>"  readonly="1"  size="5" style="text-align:center;font-size:10px;">
  </td>	
  
  <td><input name="codigo[<?=$cant;?>]" id="codigo[<?=$cant;?>]" type="text" value="<?=$codigo[$cant];?>" readonly="1" style="visibility:hidden"  size="1">
  </td> 
</tr>      
	<? 
	$cant+=1;
	}
	?>
	
</table>
  
<table border="0" align="center" >
  <tr>
    <td align="center" colspan="3"><input type="button" name="Submit" value="Guardar" onClick="fun_guardar();"/>&nbsp;<input type="button" name="Submit" value="Nuevo" onClick="fun_nuevo();" />&nbsp;<input type="button" name="Submit" value="Salir" onClick="fun_salir();" />
    </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<input name="txtndcto" type="text" id="txtndcto" value="<?=$txtndcto;?>" style="visibility:hidden" size="1" />
<input name="id_vendedor" type="text" id="id_vendedor" value="<?=$id_vendedor;?>" style="visibility:hidden" size="1" />
<input name="id_bodega" type="text" id="id_bodega" value="<?=$id_bodega;?>" style="visibility:hidden" size="1" />
<input name="fecha" type="text" id="fecha" value="<?=$fecha;?>" style="visibility:hidden" size="1" />
<input name="fecha2" type="text" id="fecha2" value="<?=$fecha;?>" style="visibility:hidden" size="1" />
<input name="hora" type="text" id="hora" value="<?=$hora;?>" style="visibility:hidden" size="1" />
<input name="txtdiasplazo" type="text" id="txtdiasplazo" style="visibility:hidden" size="1" />
<input name="lstpago" type="text" id="lstpago" value="<?=$lstpago;?>" style="visibility:hidden" size="1" />
<input name="cnt" type="text" id="cnt" value="<?=$cant;?>" style="visibility:hidden" size="1" />

</form>
</body>
</html>


<script language="Javascript">

var nav4 = window.Event ? true : false;

function validanum(evt)
	{
	var key = nav4 ? evt.which : evt.keyCode;
	return (key < 13 || (key >= 48 && key <= 57));
	}
function validanum2(evt)
	{
	var key = nav4 ? evt.which : evt.keyCode;
	return (key < 13 || (key >= 48 && key <= 57) || key==46);
	}
function vnum(evt)
	{
	var key = nav4 ? evt.which : evt.keyCode;
	return (key < 13 || (key >= 48 && key <= 57));
	}
	
		
function fun_salir()
	{	
		
		parent.location.href = '../index.php';
	}
function fun_nuevo()
	{		
		parent.location.href = 'index.php';
	}	


function fun_guardar()
	{
	
	if (document.getElementById("lstcliente").value==0)
	{
	alert ("Ingrese cliente");	
	return false;
	}
	
	
	
	var con=<?=$cant?>;
	
	if (con!=0)
		{	
		
	var jsi,jsf;
	jsf=0;
	for (jsi=0;jsi<con;jsi++)
		{
		if (document.getElementById("cantidad["+jsi+"]").value!='')
			{
			jsf+=1;
			}
		}
	if (jsf==0)
		   {alert ("Vender Productos");
		   return false;
		   }
		   
	document.form1.submit(); 
    						
		
	
  }
 }
		


</script>