<?php
ini_set("session.cookie_lifetime",60*60*10);
ini_set("session.gc_maxlifetime", 60*60*10);
session_start();
include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
if(strlen($_SESSION['SYS_USER_ID'])==0){JsDir("login.php");	return;}
$StrId=V($_POST['TxtId']);
$StrCmd=V($_POST['TxtAc']);
//-------------------------++++++++++
$StrI=V($_GET['I']);	
$StrH=V($_GET['H']);	
$StrM=V($_GET['M']);	
//-------------------------++++++
if(strlen($StrI)>0){
	$StrPre=NF(gn("temc_precio","temp_compras2","temc_index=$StrI AND cod_temc='$StrH'"),2);
	$StrCan=gn("temc_cant","temp_compras2","temc_index=$StrI AND cod_temc='$StrH'");
	$StrImp=NF(gn("temc_imp","temp_compras2","temc_index=$StrI AND cod_temc='$StrH'"),2);
	$IDPRO=gn("temc_id_producto","temp_compras2","temc_index=$StrI AND cod_temc='$StrH'");
	$StrUni=gn("can_uni","temp_compras2","temc_index=$StrI AND cod_temc='$StrH'");
}else{
	$StrPre="";
	$StrCan="";
	$StrUni="";
}
//-------------------------
if($StrCmd=="OK"){
	//
	$BIT_STOCK=V($_POST['BIT_STOCK']);
	$BIT_STOCK_USU=V($_POST['BIT_STOCK_USU']);
	$BIT_PRECIO=V($_POST['BIT_PRECIO']);
	$BIT_PRECIO_USU=V($_POST['BIT_PRECIO_USU']);
	//
	$BIT_STOCK=vbval($BIT_STOCK);
	$BIT_STOCK_USU=gn("id_usuario","usuarios","nom='$BIT_STOCK_USU'");
	$BIT_PRECIO=vbval($BIT_PRECIO);
	$BIT_PRECIO_USU=gn("id_usuario","usuarios","nom='$BIT_PRECIO_USU'");
	//
	$StrC=V($_POST['TxtCan']);	
	$StrP=V($_POST['TxtPre']);
	$StrU=V($_POST['TxtUni']);
	$StrI=V($_POST['TxtI']);
	$StrH=V($_POST['TxtH']);
	$StrX=round((vbval($StrC) * vbval($StrU)) * vbval($StrP),2);
	$SQL="UPDATE temp_compras2 SET 
			temc_cant=$StrC,
			temc_precio=$StrP,
			can_uni=$StrU,
			temc_imp=$StrX 
		WHERE cod_temc='$StrH' AND temc_index=$StrI";
	ejt($SQL);
	//
	if($BIT_STOCK>0 || $BIT_PRECIO>0){
		$SQL="UPDATE temp_compras2 
			  SET bit_stock=$BIT_STOCK,
			  	  bit_stock_usu=$BIT_STOCK_USU,
				  bit_precio=$BIT_PRECIO,
				  bit_precio_usu=$BIT_PRECIO_USU
			  WHERE cod_temc='$StrH' AND temc_index=$StrI ";
		ejt($SQL);
		//echo $SQL;
	}
	//
	$StrMsg="onload=\"window.opener.document.Frm.CmdUP.click();window.close();\"";	
}else{
	$StrMsg="onload=\"document.getElementById('TxtPre').select();\"";	
}

?>
<!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=utf-8" />
<title>COTIZACIONES - EDITAR</title>
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="jquery.maskedinput.min.js" type="text/javascript"></script>
<script type="text/javascript" src="fe/funciones.js"></script>
<script type="text/javascript">

function Aceptar(){	
	//
	var BIT_STOCK=0;
	var BIT_STOCK_USU="";
	var BIT_PRECIO=0;
	var BIT_PRECIO_USU="";
	//
	var codO=gv("TxtCodO");
	var c=gv("TxtCan");
	var pie=gv("TxtUni");
	var p=gv("TxtPre");
	var po=gv("TxtPreO");
	var Usu=gv("TxtUsu");
	var Pas=gv("TxtAut");
	//
	var c_ori=gv("TxtCanO");
	var pie_ori=gv("TxtUniO");
	//
	var tot_pie=jVal(c)*jVal(pie);
	var tot_pie_ori=jVal(c_ori)*jVal(pie_ori);
	
	//
	if(jVal(c)<=0){
		alert("La cantidad debe ser mayor a cero");
		jFocus("TxtCan");
		return;
	}
	if(jVal(pie)<=0){
		alert("La cantidad de piezas debe ser mayor a cero");
		jFocus("TxtCan");
		return;
	}
	if(jVal(p)<0){
		alert("El precio no puede ser un valor negativo");
		jFocus("TxtPre");
		return;
	}	
	//
	var PROCEDER=0;
	if(codO=="A001" || codO=="A002"){
		document.getElementById("CmdB").disabled=true;
		document.getElementById("TxtAc").value="OK";
		document.getElementById("Frm").submit();
		return;
	}
	//
	if(jVal(c)>jVal(c_ori)){
		if(MP_OK(Usu,Pas)==false){
			alert("La cantidad es mayor a la cantidad inicial");
			sv("TxtUsu","");
			sv("TxtAut","");
			jFocus("TxtUsu");
			return;
		}else{
			BIT_STOCK=1;
			BIT_STOCK_USU=Usu;
			PROCEDER=1;
		}
	}
	//
	if(jVal(pie)>jVal(pie_ori)){
		if(PROCEDER==0){
			if(MP_OK(Usu,Pas)==false){
				alert("La cantidad de piezas es mayor a la cantidad de piezas inicial");
				sv("TxtUsu","");
				sv("TxtAut","");
				jFocus("TxtUsu");
				return;
			}else{
				BIT_STOCK=1;
				BIT_STOCK_USU=Usu;
				PROCEDER=1;
			}
		}else{
			BIT_STOCK=1;
			BIT_STOCK_USU=Usu;
			PROCEDER=1;
		}
	}
	//	
	if(jVal(p)<jVal(po)){
		if(PROCEDER==0){
			if(MP_OK(Usu,Pas)==false){
				alert("Para ingresar un precio menor ingrese la autorizacion");
				sv("TxtUsu","");
				sv("TxtAut","");
				jFocus("TxtUsu");
				return;
			}else{
				BIT_PRECIO=1;
				BIT_PRECIO_USU=Usu;
			}
		}else{
			BIT_PRECIO=1;
			BIT_PRECIO_USU=Usu;
		}
	}
	//
	//
	sv("BIT_STOCK",BIT_STOCK);
	sv("BIT_STOCK_USU",BIT_STOCK_USU);
	sv("BIT_PRECIO",BIT_PRECIO);
	sv("BIT_PRECIO_USU",BIT_PRECIO_USU);
	//
	//
	document.getElementById("CmdB").disabled=true;
	document.getElementById("TxtAc").value="OK";
	document.getElementById("Frm").submit();
}
//
function MP_OK(Usu,Pas){
	var pol_mp=gv("POL_MP");
	pol_mp=pol_mp.toUpperCase();
	Usu=Usu.toUpperCase();
	Pas=Pas.toUpperCase();
	if(jL(pol_mp)==0){
		alert("No se pudo determinar las autorizaciones");
		return false;
	}
	if(pol_mp=="X"){
		return false;
	}
	var U=pol_mp.split("@")[0];
	var P=pol_mp.split("@")[1];
	U=U.split("|");
	P=P.split("|");
	for(i=0;i<U.length;i++){
		if(jL(U[i])>0){
			if(U[i]==Usu && P[i]==Pas){
				return true;
				break;				
			}
		}
	}
	return false;
}
//
function fImporte(){
	var Pre=document.getElementById("TxtPre").value;
	var Can=document.getElementById("TxtCan").value;
	var Uni=document.getElementById("TxtUni").value;
	var Importe=parseFloat("0");

	if(Pre.trim().length==0)Pre="0";
	if(Can.trim().length==0)Can="0";
	if(Uni.trim().length==0)Can="0";

	Pre=parseFloat(Pre);
	Can=parseFloat(Can);
	Uni=parseFloat(Uni);

	Importe=(Can * Uni) * Pre;
	Importe=Importe.toFixed(2);
	document.getElementById("TxtImp").value=Importe;
}

</script>
<script>
$(document).ready(function(){
   $("#TxtDex").mask("99-99-99");
   $("#TxtHax").mask("99-99-99");
});
</script>
<style type="text/css">
<!--
body {
background-color: #CCC;
}
#Layer1 {
	position:absolute;
	left:345px;
	top:115px;
	width:123px;
	height:35px;
	z-index:1;
}
.centrarFrm{
	width:650px;
	height:280px;
	position:absolute;
	left:50%;
	top:50%;
	background-color:#666666;
	margin:-140px 0 0 -325px;
}
#Layer2 {
position:absolute;
left:409px;
top:205px;
width:82px;
height:24px;
z-index:1;
}
.Estilo1 {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
color: #FFFFFF;
}
.f2 {font-family: Arial, Helvetica, sans-serif; font-weight: bold; color: #FFFFFF; font-size: 12px; }
.Estilo3 {color: #FFFFFF; font-size: 12px; font-family: Arial, Helvetica, sans-serif;}
.Estilo4 {color: #000099}
.Estilo5 {color: #000000}

-->
</style>

</head>
<body  <?php echo $StrMsg;?>>
<form id="Frm" name="Frm" method="post" action="cotizaciones_editar.php">
<input name="POL_MP" type="hidden" id="POL_MP" value="<?php echo LIST_U_MP();?>" />
<input name="POL_MB" type="hidden" id="POL_MB" value="<?php echo LIST_U_MB();?>" />
<input name="BIT_STOCK" type="hidden" id="BIT_STOCK" value="" />
<input name="BIT_STOCK_USU" type="hidden" id="BIT_STOCK_USU" value="" />
<input name="BIT_PRECIO" type="hidden" id="BIT_PRECIO" value="" />
<input name="BIT_PRECIO_USU" type="hidden" id="BIT_PRECIO_USU" value="" />

<center> 
<div Id="DivFRM"  class="centrarFrm" >
<div id="layer" style="position:absolute; left:31px; top: 22px; width: 246px; height: 24px;">
<div align="left" class="Estilo1">COTIZACIONES  EDITAR</div></div>
<div >
<input name="TxtId" type="hidden" id="TxtId" value="<?php echo $StrId;?>"/>
<input name="TxtI" type="hidden" id="TxtI" value="<?php echo $StrI;?>"/>
<input name="TxtH" type="hidden" id="TxtH" value="<?php echo $StrH;?>"/>
<input name="TxtM" type="hidden" id="TxtM" value="<?php echo $StrM;?>"/>
<input name="TxtAc" type="hidden" id="TxtAc" value="" />
<?php
$CLAVE=gs("valor","valores","dato='CLAVE_PRECIO'");
?>
<input name="TxtPAS" type="hidden" id="TxtPAS" value="<?php echo $CLAVE;?>"/>
</div>

<div id="layer6" style="position:absolute; left:31px; top: 47px; width: 465px;">
<div align="left" class="f2">COD : <?php 
$SEPARADOR=gs("aux_des","auxiliares","aux_tip='SIM'");
if(strlen($SEPARADOR)==0){$SEPARADOR="x";}

echo gs("COD","vproductos","ID=$IDPRO").$SEPARADOR.$StrUni;?></div></div>

<div id="layer6" style="position:absolute; left:31px; top: 71px; width: 515px;">
<div align="left" class="f2">DES : <?php echo gs("DESCRIPCION","vproductos","ID=$IDPRO");?></div></div>

<div id="layer6" style="position:absolute; left: 31px; top: 93px; width: 461px;">
<div align="left" class="f2">MAR : <?php echo gs("MARCA","vproductos","ID=$IDPRO");?></div></div>

<div id="layer6" style="position:absolute; left: 31px; top:115px; width: 134px;">
<div align="left" class="f2">PRECIO : <?php echo $StrPre;?></div></div>


<div id="layer6" style="position:absolute; left:188px; top:115px; width: 113px;">
<div align="left" class="f2">CANT : <?php echo $StrCan;?></div></div>


<div id="layer6" style="position:absolute; left:364px; top:115px; width: 154px;">
<div align="left" class="f2">PIEZAS : <?php echo $StrUni;?></div></div>


<div id="layer15" style="position:absolute; left: 32px; top: 174px; width: 68px;">
<div align="left" class="f2">PRECIO</div></div>
<input name="TxtCodO" type="hidden" 
id="TxtCodO" value="<?php echo gs("COD","vproductos","ID=$IDPRO");?>"/>

<input name="TxtPreO" type="hidden" 
id="TxtPreO" value="<?php echo $StrPre;?>"/>

<input name="TxtCanO" type="hidden" 
id="TxtCanO" value="<?php echo $StrCan;?>"/>

<input name="TxtUniO" type="hidden" 
id="TxtUniO" value="<?php echo $StrUni;?>"/>

<input name="TxtPre" type="text" id="TxtPre" 
style="position:absolute; left: 32px; top: 191px; width: 68px;"
value="<?php echo $StrPre;?>"  
<?php
	if(vbval($StrM)==1){		 
		echo " onkeydown=\"return SoloNum(this,event,'CmdB');\"	";	
	}else{
		echo  " onkeydown=\"return SoloNum(this,event,'TxtCan');\"	";	
	}
?>

onkeyup="fImporte();"
tabindex="1" />

<div id="layer6" style="position:absolute; left: 111px; top: 174px; width: 59px;">
<div align="left" class="f2">CANTIDAD</div></div>
<input name="TxtCan" type="text" id="TxtCan" 
style="position:absolute; left: 111px; top: 191px; width: 71px;"
value="<?php echo $StrCan;?>"  onfocus="this.select();"
onkeydown="return SoloNum(this,event,'TxtUni');" 
<?php
	if(vbval($StrM)==1){
		echo " readonly=\"readonly\"	";	 
	}
?>
onkeyup="fImporte();"
tabindex="2" />

<div id="layer6" style="position:absolute; left: 193px; top: 174px; width: 59px;">
<div align="left" class="f2">PIEZAS</div></div>
<input name="TxtUni" type="text" id="TxtUni" 
style="position:absolute; left: 192px; top: 191px; width: 58px;"
value="<?php echo $StrUni;?>"    onfocus="this.select();"
onkeydown="return SoloNum(this,event,'CmdB');"
<?php
	if(vbval($StrM)==1){
		echo " readonly=\"readonly\"	";	 
	}
?>
onkeyup="fImporte();"
tabindex="3" />

<div id="layer15" style="position:absolute; left: 264px; top: 174px; width: 52px;">
<div align="left" class="f2">IMPORTE</div></div>
<input name="TxtImp" type="text" id="TxtImp" 
style="position:absolute; left: 261px; top: 191px; width: 81px; background-color:#CCC; text-align:right"
value="<?php echo $StrImp;?>"  
onkeydown="return SoloNum(this,event,'CmdB');"
readonly="readonly" tabindex="4" />

<div style="position:absolute; left: 434px; top: 156px; width: 59px;">
<div align="left" class="f2">AUTORIZAR</div></div>

<div style="position:absolute; left: 434px; top: 174px; width: 59px;">
<div align="left" class="f2">USUARIO</div></div>
<input name="TxtUsu" type="text" id="TxtUsu" 
style="position:absolute; left:434px; top: 190px; width: 85px;"
tabindex="6" onkeydown="return SoloTextoEx(event,'TxtAut');" 
value="" maxlength="15" />

<div style="position:absolute; left: 530px; top: 174px; width: 59px;">
<div align="left" class="f2">PASSWORD</div></div>
<input name="TxtAut" type="password" id="TxtAut" 
style="position:absolute; left: 530px; top: 190px; width: 85px;"
tabindex="7" onkeydown="return SoloTextoEx(event,'CmdB');" 
value="" maxlength="15" />

<input name="CmdB" type="button" 
style="position:absolute; left: 354px; top: 191px; width: 72px;"
id="CmdB" value="Aceptar" onclick="Aceptar();" tabindex="5" />

</div>
</center>	
</form>
</body>
</html>
