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

$lstpago=2;
$id_autoriza=62;

/*Sesiones*/
include("../conex.php");
include("../autoriza.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>Notas de Venta</title>
</head>
<form id="form1" name="form1" method="post" action="">
<?


//******************************** 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");
	
//*************************************** fecha y hora ******************************************/	
		 //$fecha = date(d)."/".date(m)."/".date(Y);
		 $fecha = date("d-m-Y");
    	 $hora = date('H:i:s');
		 //$fecha_dia = date(Y)."/".date(m)."/".date(d); 
		 $fecha_dia = date("Y-m-d");
/***************** sacar valor de Pronto Pago ***********************************************/	
	$sql_par = "SELECT valor FROM parametros WHERE descripcion = 'PRONTO PAGO'";
	$res_par = mysql_query($sql_par,$enlace);
	$row=0;
	while (mysql_fetch_object ($res_par))
		{
		$par_pronto=mysql_result($res_par, $row, "valor");
		$row=$row+1;
		}		  
?>



<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 Venta N&ordm;  Oficina  </div>   </td>
    <td width="224"> <div align="center"> Fecha:<?=$fecha;?> <?=$hora;?>
    </div>     </td>
  </tr>
</table>

<table border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
     <td width="120">Bodega <input name="id_bodega" id="id_bodega" type="text" size=5/>	  </td>
    <td width="220">Vendedor
      <input name="vende" id="vende" type="text" />	  </td>
    <td width="350" align="center"><?
			$sql_umed ="SELECT id, razon_social, sucursal  FROM sucursales ORDER BY razon_social, sucursal ASC";
			$res_umed = mysql_query($sql_umed,$enlace);
			$contb=0;
			?>
      <select name="lstcliente" id="lstcliente" style="width:350px" onChange="fun_chequea();" >
        <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>
      
    <td width="27">Vta Mes</td>
	  <td width="48">
      <input name="ventas" id="ventas" type="text"  readonly="1" size="8"/>	  </td>
      <td width="31">Desc</td>
	  <td width="24">
      <input name="descuento_cliente" id="descuento_cliente" type="text"  readonly="1" size="4"/>	  </td>
      <td width="42">Buscar</td>
	  <td width="30">
      <input name="busca" id="busca" type="text"  onchange="fun_busca();"  size="5"/>	  </td>
         <td width="75" align="center">	<input type="button" name="CtaCte" value="CtaCte" onClick="fun_CtaCte();"></td>
         <td width="75" align="center">	<input type="button" name="Rotacion" value="Rotacion" onClick="fun_Rotacion();"></td>
  </tr>
 </table>

<table width="700" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
<tr bgcolor="#000000"> 
	  <td width="5%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Cant.</font></strong></div></td>
	 <td width="5%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Saldo</font></strong></div></td>
     <td width="5%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Peso</font></strong></div></td>
     <td width="5%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Codigo</font></strong></div></td>
	  <td width="60%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Detalle</font></strong></div></td>
	 <td width="10%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Precio</font></strong></div></td>
     <td width="8%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Desc</font></strong></div></td>
     <td width="17%"><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">&nbsp;</font></strong></div></td>
    </tr>
<? 
$totalkilos=0;
$txtst=0;

          $sql_pro = "select codigo, descripcion, precio, saldo, peso FROM productos WHERE empresa=$id_empresa AND cod_familia<>14  AND activo=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");
	$peso[$cant] = mysql_result($res_pro, $cant, "peso");
	$codigo[$cant] = mysql_result($res_pro, $cant, "codigo");
	$detalle[$cant] = mysql_result($res_pro, $cant, "descripcion");
	$desc[$cant] = 0;
	$precio[$cant] = mysql_result($res_pro, $cant, "precio");
	
	//  ***********  
	     //   $cont=0;
			 $desc_oferta[$cant]=35;
	//$sql_bc = "SELECT descuento FROM descuentos, descuentos_productos WHERE  id_descuento=descuentos.id AND fecha_inicio<='$fecha_dia' AND fecha_final>='$fecha_dia' AND id_producto=$codigo[$cant]";
		//	$res_bc = mysql_query($sql_bc,$enlace);
			//while (mysql_fetch_object ($res_bc))
	 		{
			// $desc_oferta[$cant] = (1- (  (1-$desc_oferta[$cant]/100) * (1-mysql_result($res_bc, $cont, "descuento")/100) ))  * 100;
	 		//$cont+=1;
     		}	
		
		
	//  *********** 		
	?>
	
   <tr> 
		
      
		<td width="9%"><div align="center"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
	<input name="cantidad[<?=$cant;?>]" id="cantidad[<?=$cant;?>]" type="text" value="<?=$cantidad[$cant];?>"  style="text-align:center;width:40%" onKeyPress="return validanum2(event,'<?=$cant;?>')" onBlur="calculo('<?=$cant;?>');" >        
	</font></strong></div></td>	 
    
      <td width="9%">		  	
<div align="center"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
			<input name="auxsaldo[<?=$cant;?>]" id="auxsaldo[<?=$cant;?>]" type="text" value="<?=$auxsaldo[$cant];?>" readonly="1" style="text-align:center;font-size:14px;width:100%">
	  </font></strong></div>  </td> 
      <td width="9%">		  	
<div align="center"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
			<input name="peso[<?=$cant;?>]" id="peso[<?=$cant;?>]" type="text" value="<?=$peso[$cant];?>"  readonly="1" style="text-align:center;font-size:14px;width:100%">
	  </font></strong></div>  </td> 
      <td width="9%">		  	
<div align="center"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
			<input name="codigo[<?=$cant;?>]" id="codigo[<?=$cant;?>]" type="text" value="<?=$codigo[$cant];?>" readonly="1" style="text-align:center;font-size:14px;width:100%">
	  </font></strong></div>  </td> 
      
	<td>		  	
<div align="center"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
			<input name="detalle[<?=$cant;?>]" id="detalle[<?=$cant;?>]" type="text" value="<?=$detalle[$cant];?>" readonly="1" style="font-size:14px;width:100%">
	  </font></strong></div>  </td>
	<td width="10%">		  	
<div align="center"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
  <input name="precio[<?=$cant;?>]"  id="precio[<?=$cant;?>]" type="text" value="<?=$precio[$cant];?>" readonly="1" style="text-align:center;font-size:14px;width:98%" />
</font></strong></div>  </td>
<td width="8%">		  	
<div align="center"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
  <input name="desc[<?=$cant;?>]"  id="desc[<?=$cant;?>]" type="text" value="<?=$desc[$cant];?>"  style="text-align:center;font-size:14px;width:98%" onBlur="modifica_desc('<?=$cant;?>');" />
</font></strong></div>  </td>	
<td width="14%">		  	
<div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
			<input name="total[<?=$cant;?>]" id="total[<?=$cant;?>]" type="text" value="<?=$precio[$cant]*$cantidad[$cant];?>"  readonly="1" style="text-align:center;font-size:14px;width:98%">
	  </font></strong></div>  </td>	
    <td width="5%">		  	
<div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
			<input name="desc_oferta[<?=$cant;?>]" id="desc_oferta[<?=$cant;?>]" type="text" value="<?=$desc_oferta[$cant];?>"  readonly="1"  style="visibility:hidden"  size="1">
	  </font></strong></div>  </td>	
    </tr>      
	<? 
	$cant+=1;
	}
	?>
	 <tr>    	
   <td>Total</td>
	  <td> kilos</td>
	  <td><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
	    <input name="totalkilos" id="totalkilos" type="text" value="<?=$totalkilos;?>" style="text-align:center;width:95%" />
	  </font></strong></td>
      <td>&nbsp;</td>
        <td width="42%"><div align="center"><strong>ORDEN COMPRA  <font color="#333333" face="Arial, Helvetica, sans-serif">
	 <input name="orden" id="orden" type="text" value="<?=$orden;?>"  style="text-align:center;width:40%" >        
	</font></strong></div></td>
       <td>&nbsp;</td>
	  <td><div align="left">Sub Total</div></td>
    <td width="14%" align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
	    <input name="txtst" id="txtst" type="text" value="<?=$txtst;?>" style="text-align:center;font-size:14px;width:98%">
		</font></strong></td>
	</tr>
<tr>
   <td>&nbsp;</td>
	  <td>&nbsp;</td>
	  <td>&nbsp;</td>
       <td>&nbsp;</td>
        <td width="42%"><div align="right"><strong>Pronto Pago <font color="#333333" face="Arial, Helvetica, sans-serif">
	<input name="pronto" id="pronto" type="text" value="<?=$pronto;?>"  style="text-align:center;width:10%" onBlur="modifica_desc_pronto();" >        
	</font></strong></div></td>
       <td>&nbsp;</td>
	  <td><div align="left">Descuento</div></td>
    <td width="24%" align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
	    <input name="desc_pronto" id="desc_pronto" type="text" value="<?=$desc_pronto;?>" style="text-align:center;font-size:14px;width:98%">
		</font></strong></td>
	</tr>
<tr>
   <td>&nbsp;</td>
	  <td>&nbsp;</td>
	  <td>&nbsp;</td>
      <td>&nbsp;</td>
       <td>&nbsp;</td>
       <td>&nbsp;</td>
	  <td><div align="left">Total </div></td>
    <td width="14%" align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">
	    <input name="totales" id="totales" type="text" value="<?=$totales;?>" style="text-align:center;font-size:14px;width:98%">
		</font></strong></td>
	</tr>
 
    </table>
    
<table width="100" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
	  <td width="35%">&nbsp;</td>
		<td width="10%" align="center">	<input type="button" name="Guardar" value="Guardar" onClick="fun_guardar();" disabled="disabled"> </td>
        <td width="10%" align="center">	<input type="button" name="Nuevo" value="Nuevo" onClick="fun_nuevo();"></td>
        <td width="10%" align="center">	<input type="button" name="Salir" value="Salir" onClick="fun_salir();"></td>
		<td width="35%">&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"  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" />

</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()
	{		
	
			window.top.open('','_parent','');window.top.close();
	}
function fun_nuevo()
	{	
	fun_limpia_desc();
	 document.getElementById("lstcliente").value=0;	
	  document.getElementById("descuento_cliente").value=0;
	  document.getElementById("ventas").value=0;
	 	document.form1.Guardar.disabled = false;
		//parent.location.href = 'index.php';
		
	}	
function fun_guardar()
	{
	
	if (document.getElementById("lstcliente").value==0)
	{alert ("Ingrese cliente");	
	}
	else
	{
	
	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");
		   }
		else	
		  {
		 
        			
				 document.form1.action='guarda.php?cnt=<?=$cant?>';
				 document.form1.target='_blank';
				setTimeout('document.form1.submit();',500);	
    						
	
				
		  }
	  }
	}
  }
function calculo(donde)
	{
	var cantidad, kilos, precio, desc, peso, total, tkilos, stot, i, totaluno, totaltot, j, f1, f2, letra, result,jsi, jsuser;
	
	precio = document.getElementById("precio["+donde+"]").value;
	if (Number(document.getElementById("auxsaldo["+donde+"]").value)<Number(document.getElementById("cantidad["+donde+"]").value))
		{
	alert("La cantidad ingresada es mayor al saldo existente\n Saldo: "+document.getElementById("auxsaldo["+donde+"]").value);
		document.getElementById("cantidad["+donde+"]").value='';
		
		}
	cantidad = document.getElementById("cantidad["+donde+"]").value;
	
	if (cantidad!='' )
		{
	
		if (cantidad >= 0 && precio >0)
			{
			fun_desc(donde);
		 
			desc=document.getElementById("desc["+donde+"]").value;
			document.getElementById("total["+donde+"]").value=Number(cantidad)*Number(precio)*(1-Number(desc)/100);
	
				result = document.getElementById("total["+donde+"]").value;	
				var valor_actual = result.split(".");
				var entero1 = Number(valor_actual[0]);
				var decimal1 = valor_actual[1];	
				if (decimal1==undefined) decimal1='11';
				decimal1=decimal1.charAt(0);
				if (decimal1>=5) entero1+=1;
				total = entero1;
				document.getElementById("total["+donde+"]").value=total;
	
	
	
			total=0;
			tkilos=0;	
			for (i=0; i<<?=$cant?>; i++)
				{ kilos=0;
				stot=document.getElementById("total["+i+"]").value;
				cantidad=Number(document.getElementById("cantidad["+i+"]").value);
				peso=Number(document.getElementById("peso["+i+"]").value);
				if (cantidad>=0 && peso>=0) kilos=cantidad*peso;
				if (stot>0)
					{
					total=Number(total) + Number(stot);
					}
				if (kilos>0)
					{
					tkilos=Number(tkilos) + Number(kilos);					
					}	
					
				}
				document.form1.txtst.value=total;
				document.form1.pronto.value=0;
				document.form1.desc_pronto.value=0;
				document.form1.totales.value=total;
			    document.form1.totalkilos.value=tkilos;
			}
		
		}
	
	}
		
function fun_chequea()
	{
	fun_limpia_desc();     //limpia desc		
	var rut=document.form1.lstcliente.value;
	PopWindow('status_cliente.php?rutc='+rut);
	}	
		
function PopWindow(pagina)
	{
   window.open(pagina,'TEST','width=600,height=1000,menubar=no,scrollbars=yes,toolbar=no,location=no,directories=no,resizable=no,top=70,left=300');
	}
	
function fun_desc(donde)
	{		
  document.getElementById("desc["+donde+"]").value=0;
  if ((document.getElementById("descuento_cliente").value)!='N')  
	{
	if ((document.getElementById("descuento_cliente").value)=='S')     
		{  
	   if ((document.getElementById("desc_oferta["+donde+"]").value)!=0)
	         {
			 document.getElementById("desc["+donde+"]").value=Number(document.getElementById("desc_oferta["+donde+"]").value);  
			 }
        }
		
	
    if ((document.getElementById("descuento_cliente").value)!=0 && (document.getElementById("descuento_cliente").value)!='S') 
	  
		{
		var desx=Number((1-((1-Number(document.getElementById("desc_oferta["+donde+"]").value)/100)*(1-Number(document.getElementById("descuento_cliente").value)/100)))*100).toFixed(0);
		     document.getElementById("desc["+donde+"]").value=desx;	
		}
	}
   }
			
function fun_limpia_desc()
	{	
	var i
	// status clientes   >0 o =0  o  N  o S
    
	 //limpia desc
	 for (i=0; i<<?=$cant?>; i++) 
	  {document.getElementById("desc["+i+"]").value=0;
	   document.getElementById("cantidad["+i+"]").value='';
	   document.getElementById("total["+i+"]").value=0;
	   }
	  document.getElementById("txtst").value=0; 
	  document.getElementById("pronto").value=0;
	  document.getElementById("desc_pronto").value=0;
	  document.getElementById("totales").value=0;
	  document.getElementById("totalkilos").value=0;
	  
	}

function modifica_desc(donde)
	{
	var desc		
	 desc = document.getElementById("desc["+donde+"]").value; 
	 calculo_desc(donde);
 if ((document.getElementById("descuento_cliente").value)!='N')		  
	{
  if ((document.getElementById("descuento_cliente").value)=='S')     
	 {
	 if (Number(desc)>=Number(document.getElementById("desc_oferta["+donde+"]").value))
	         {
			document.getElementById("desc["+donde+"]").value = Number(document.getElementById("desc_oferta["+donde+"]").value); 
			 calculo_desc(donde);
			 }
     } 
		
   else
   
	 {
	  if (Number(desc)>=((1-((1-Number(document.getElementById("desc_oferta["+donde+"]").value)/100)*(1-Number(document.getElementById("descuento_cliente").value)/100)))*100))
	    {
		var desx=Number((1-((1-Number(document.getElementById("desc_oferta["+donde+"]").value)/100)*(1-Number(document.getElementById("descuento_cliente").value)/100)))*100).toFixed(2);
	      document.getElementById("desc["+donde+"]").value = desx;
		  
		   calculo_desc(donde);
	    }	
	  }
	
	 }
	} 
		
function calculo_desc(donde)
	{
	var cantidad, kilos, precio, desc, peso, total, tkilos, stot, i, totaluno, totaltot, j, f1, f2, letra, result,jsi, jsuser;
	
	precio = document.getElementById("precio["+donde+"]").value;
	
	cantidad = document.getElementById("cantidad["+donde+"]").value;
	
	if (cantidad!='' )
		{
	
		if (cantidad >= 0 && precio >0)
			{
		
		
			desc=document.getElementById("desc["+donde+"]").value;
			document.getElementById("total["+donde+"]").value=(Number(cantidad)*Number(precio)*(1-Number(desc)/100)).toFixed(0);
	
				
	
	
	
			total=0;
			tkilos=0;	
			for (i=0; i<<?=$cant?>; i++)
				{ kilos=0;
				stot=document.getElementById("total["+i+"]").value;
				cantidad=Number(document.getElementById("cantidad["+i+"]").value);
				peso=Number(document.getElementById("peso["+i+"]").value);
				if (cantidad>=0 && peso>=0) kilos=cantidad*peso;
				if (stot>0)
					{
					total=Number(total) + Number(stot);
					}
				if (kilos>0)
					{
					tkilos=Number(tkilos) + Number(kilos);					
					}	
					
				}
				document.form1.txtst.value=total;
				document.form1.desc_pronto.value=0;
				document.form1.totales.value=total;
			    document.form1.totalkilos.value=tkilos;
			}
		
		}
	
	}
	
	
function modifica_desc_pronto()
{
var desc, desc_pronto, sub_total, total, par_pronto;
	
	desc = document.getElementById("pronto").value;
	par_pronto=<?=$par_pronto?>;
	if (desc>=par_pronto*100)
	 {desc=par_pronto*100;
	  document.form1.pronto.value=desc;
	 }
	
    sub_total = document.getElementById("txtst").value;
	desc_pronto=(sub_total*desc/100).toFixed(0)
               
				document.form1.desc_pronto.value=desc_pronto;
				document.form1.totales.value=sub_total-desc_pronto;
}	
	
	
function fun_CtaCte()
{
var rut=document.form1.lstcliente.value;

if(rut!=0)
  {
    document.form1.action="muestra_facturas.php?id="+rut;
	document.form1.target="_blank";
	document.form1.submit();
	
  }

}

function fun_Rotacion()
{
var rut=document.form1.lstcliente.value;

if(rut!=0)
  {
    document.form1.action="rotacion.php?id="+rut;
	document.form1.target="_blank";
	document.form1.submit();
	
  }
}

function fun_busca()
	{	
	var i,bus
	bus=document.getElementById("busca").value;
     alert(bus);
	 for (i=0; i<<?=$cant?>; i++) 
	  {
	  
	  if (document.getElementById("codigo["+i+"]").value==bus)
	  { document.getElementById("cantidad["+i+"]").focus();}
	 
	 }
	}				
</script>