COMPILE FUNCTION obj_Run_Stage(Method,Parms)

/*
	Methods for RUN_STAGE table
    
	07/21/2016 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    SetIASig		;* SetIA signature
	
	
*/

DECLARE SUBROUTINE Set_Status, Msg, obj_Tables, 
DECLARE SUBROUTINE obj_WO_Wfr, ErrMsg

DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, NextKey,  obj_Prod_Spec, Send_Dyn, obj_Clean_Insp, obj_Tables



$INSERT REACT_RUN_EQUATES
$INSERT RUN_STAGE_EQUATES
$INSERT RUN_STAGE_WFR_EQUATES
$INSERT PRS_STAGE_EQUATES

ErrCode  = ''
ErrorMsg = ''
ErrTitle = 'Error in Stored Procedure "obj_Run_Stage"'


IF NOT(ASSIGNED(Method))	THEN ErrorMsg = 'Unassigned parameter "Method" passed to subroutine'
IF NOT(ASSIGNED(Parms))		THEN Parms = ''

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END


Result = ''

BEGIN CASE

	CASE Method = 'Create'				; GOSUB Create
	CASE Method = 'SetIASig'			; GOSUB SetIASig
	CASE Method = 'CurrStatus'			; GOSUB CurrStatus
	
	CASE 1
		
END CASE

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
END

RETURN Result


* * * * * * *
Create:
* * * * * * *


RDSNo	= Parms[1,@RM]
Stage	= Parms[COL2()+1,@RM]
PSNo	= Parms[COL2()+1,@RM]


IF RdsNo = ''		THEN ErrorMsg = 'Null Parameter "RdsNo" passed to routine. (':Method:')'
IF Stage = ''		THEN ErrorMsg = 'Null Parameter "Stage" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

IF PSNo = '' THEN
	PSNo = XLATE('REACT_RUN',RDSNo,'PS_NO','X')
	IF PSNo = '' THEN
		ErrorMsg = 'Null Parameter "PSNo" passed to routine. (':Method:')'
		RETURN
	END
END

CurrDTM = ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')

PRSStageRec	= XLATE('PRS_STAGE',PSNo:'*':Stage,'','X')

StageRec = XLATE('RUN_STAGE',RDSNo:'*':Stage,'','X')		;* only need for initial test 8/11/2016 JCH

StageRec<RUN_STAGE_SPEC_TOOL_CLASS$>	= PRSStageRec<PRS_STAGE_PROC_TOOL_CLASS$>
StageRec<RUN_STAGE_SPEC_INV_ACTION$>	= PRSStageRec<PRS_STAGE_PROC_INV_ACTION$>
StageRec<RUN_STAGE_ENTER_DTM$>			= CurrDTM
StageRec<RUN_STAGE_STATUS$>             = 'INIT'

otParms = 'RUN_STAGE':@RM:RDSNo:'*':Stage:@RM:@RM:StageRec
RSRec = obj_Tables('WriteRec',otParms)

RETURN


* * * * * * *
SetIASig:
* * * * * * *

RDSNo		= Parms[1,@RM]
Stage		= Parms[COL2()+1,@RM]
InvAction	= Parms[COL2()+1,@RM]

IF RdsNo = ''		THEN ErrorMsg = 'Null Parameter "RdsNo" passed to routine. (':Method:')'
IF Stage = ''		THEN ErrorMsg = 'Null Parameter "Stage" passed to routine. (':Method:')'
IF InvAction = ''	THEN ErrorMsg = 'Null Parameter "InvAction" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

otParms = 'RUN_STAGE':@RM:RDSNo:'*':Stage
RSRec = obj_Tables('ReadRec',otParms)


LOCATE InvAction IN RSRec<RUN_STAGE_SPEC_INV_ACTION$> USING @VM SETTING Pos THEN

	CurrDTM = ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')
	
	RSRec<RUN_STAGE_SIA_COMP_BY$,Pos>	= @USER4
	RSRec<RUN_STAGE_SIA_COMP_DTM$,Pos>	= CurrDTM

	otParms = FIELDSTORE(otParms,@RM,4,0,RSRec)
	obj_Tables('WriteRec',otParms)

	IF Get_Status(errCode) THEN
		ErrMsg(errCode)
	END
	
END

RETURN



* * * * * * *
CurrStatus:
* * * * * * *

StageKey	= Parms[1,@RM]
StageRec	= Parms[COL2()+1,@RM]

IF StageKey = '' THEN RETURN

IF StageRec = '' THEN
	StageRec = XLATE('RUN_STAGE',StageKey,'','X')
	IF StageRec = '' THEN RETURN
END

SpecToolClass	= StageRec<RUN_STAGE_SPEC_TOOL_CLASS$>
SpecInvAct		= StageRec<RUN_STAGE_SPEC_INV_ACTION$>
CompBy			= StageRec<RUN_STAGE_COMP_BY$>
SIA_CompBy		= StageRec<RUN_STAGE_SIA_COMP_BY$>


IF SpecToolClass NE '' THEN
	IF SpecInvAct<1,1> NE '' AND SIA_CompBy<1,1> = '' AND CompBy NE '' THEN
		Result = 'SKIP'
		RETURN
	END
END


IAStart = ''
IAStop = ''
	
IF SpecInvAct NE '' THEN

	iaCnt = COUNT(SpecInvAct,@VM) + (SpecInvAct NE '')

	IF SIA_CompBy<1,iaCnt> NE ''	THEN IAStop = 1
	IF SIA_CompBy<1,1> NE ''		THEN IAStart = 1
	
END

BEGIN CASE
	CASE CompBy NE '' 			; Status = 'COMP'
	CASE IAStop		= 1			; Status = 'SIG'
	CASE IAStart	= 1			; Status = 'RUN'
	CASE 1						; Status = 'RDY'
END CASE
	
Result = Status
	

RETURN






