<?php
//------------------------------------------------
	$StrAlm=V($_POST['CboAlm']);
	$StrCod=V($_POST['TxtCod']);
	$T_ID=V($_POST['T_ID']);
	$StrCan=V($_POST['TxtCan']);
	
	//ACTIVAR DESDE OTRA PAGINA
	if(strlen($StrId)==0){
		$StrId=V($_GET['Id']);
		if(is_numeric($StrId)==false){
			$StrId="";
		}else{
			$StrCmd="AR";
			$LStrPag=V($_GET['Pag']);
			if(!is_numeric($LStrPag)){
				$LStrPag="";
			}
		}
	}
//------------------------------------------------	
	//FUNCION PARA ACTIVAR UN REGISTRO
	if(strlen($StrId)>0 && $StrCmd=="AR"){
		$Id=$StrId;
		$StrAlm=gs("ALM","vis_stockm","ID=$Id");
		$StrCod=gs("COD","vis_stockm","ID=$Id");
		$StrDes=gs("DES","vis_stockm","ID=$Id");
		$StrMar=gs("MAR","vis_stockm","ID=$Id");
		$StrCan=gs("CAN","vis_stockm","ID=$Id");	
		$LStrFoc="document.getElementById('CboAlm').focus();";		
	}
	//------------------------------------------------	
	if($StrCmd=="G" || $StrCmd=="M"){	
			$StrAlm_=gn("id_almacen","almacenes","alm_nombre='$StrAlm'");
			$StrIDP_=gn("id_producto","productos","pro_codigo='$StrCod'");
	}
	//------------------------------------------------
	//FUNCIONES DE ACTUALIZACION
if($StrCmd=="G"){
		$S_Id=gn("ID","vis_stockm","ALM='$StrAlm' AND COD='$StrCod'");
		if($S_Id>0){			
			$s="DELETE FROM stockm WHERE id_stockm=$S_Id";	
			ejt($s);
			$Msm="Stock minimo Actualizado";
		}
		
		if($StrIDP_==0){
			$LStrMsg="Codigo de producto no encontrado";
			$LStrFoc="document.getElementById('TxtCod').focus();";						
			$StrGT="S";
		}
		//
		if(strlen($StrGT)==0){
			$Id=GenId("id_stockm","stockm");
			$s="INSERT INTO stockm VALUES ($Id,$StrAlm_,$StrIDP_,$StrCan)";
			ejt($s);
			//
			if($LStrMM=="S")	$LStrMsg="Stock Minimo Guardado";
			$LStrMsg=$Msm;
			$LStrFoc="document.getElementById('CboAlm').focus();";
			$StrCmd="L";
		}
}
//------------------------------------------------
if($StrCmd=="M"){
		$S_Id=gn("ID","vis_stockm","ALM='$StrAlm' AND COD='$StrCod'");
		if($S_Id>0){			
			$s="DELETE FROM stockm WHERE id_stockm=$S_Id";	
			ejt($s);
			$Msm="Stock minimo Actualizado";
		}
		//
		if(strlen($StrGT)==0){
			$Id=GenId("id_stockm","stockm");
			$s="INSERT INTO stockm VALUES ($Id,$StrAlm_,$StrIDP_,$StrCan)";
			ejt($s);

			if(strlen($Msm)==0){$Msm="Stock Minimo Guardado";}
			$LStrMsg=$Msm;
			$LStrFoc="document.getElementById('CboAlm').focus();";
			$StrCmd="L";
		}
}
//------------------------------------------------
if($StrCmd=="E"){
	if(strlen($StrGT)==0){
		$s="DELETE FROM stockm WHERE id_stockm=$StrId";	
		ejt($s);

		if($LStrMM=="S")	$LStrMsg="Registro Eliminado";
		$LStrFoc="document.getElementById('CboAlm').focus();";
		$StrCmd="L";
	}		
}
//------------------------------------------------
if($StrCmd=="L"){
	$StrId="";
	$StrCod="";
	$StrDes="";
	$StrMar="";
	$StrCan="";
	$StrGT="S";	
}
	//////////////////////////////////
	if($StrGT=="S"){
		if(strlen($LStrMsg)>0){
			if(strlen($LStrFoc)==0){
				$LStrMsg="onload=\"alert('$LStrMsg');\"";
			}else{
				$LStrMsg="onload=\"alert('$LStrMsg');$LStrFoc;\"";
			}
		}else{
			if(strlen($LStrFoc)>0){
				$LStrMsg="onload=\"$LStrFoc;\"";
			}
		}
	}else{
		$LStrFoc="document.getElementById('CboAlm').focus();";
		$LStrMsg="onload=\"$LStrFoc;\"";
	}

?>