COMPILE FUNCTION obj_WO_React(Method,Parms)
/*
	Methods for the WO_React table
    
	03/31/2015 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    AddRDSNo
    RemRDSNo
    
	
*/

DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, NextKey, obj_Prod_Spec, obj_WO_Mat_QA, GetTickCount
Declare function Environment_Services, Database_Services
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, RList, ErrMsg, Btree.Extract, obj_WO_Mat_QA, Mona_Services

$INSERT MSG_EQUATES
$INSERT WO_REACT_EQUATES
$INSERT PRS_STAGE_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT RDS_EQUATES
$INSERT QA_MET_EQUATES			;* Used in GetQAMet data structure return variable
$Insert LOGICAL


ErrTitle = 'Error in Stored Procedure "obj_PRS_Prop"'
ErrorMsg = ''

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 = ''
Log = ''

IsProd = Environment_Services('IsProd')
If IsProd EQ True$ then
	MonaResource = 'GRP_OPENINSIGHT_MES_OP_FE_OBJWOREACT'
end else
	MonaResource = 'GRP_OPENINSIGHT_MES_OP_FE_DEV_OBJWOREACT'
end

BEGIN CASE
	CASE Method = 'AddRdsNo'	; GOSUB AddRdsNo
	CASE Method = 'RemRdsNo'	; GOSUB RemRdsNo
	
	CASE 1
		ErrorMsg = 'Unknown Method ':QUOTE(Method):' passed to object.'

END CASE

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

RETURN Result


* * * * * * *
AddRdsNo:
* * * * * * *

	StartTick  = GetTickCount()
	MetricName = 'AddRdsNo'

	WONo	= Parms[1,@RM]
	StepNo	= Parms[COL2()+1,@RM]
	ReactNo	= Parms[COL2()+1,@RM]
	RdsNo	= Parms[COL2()+1,@RM]
	CassNo	= Parms[COL2()+1,@RM]

	IF WONo		= '' THEN RETURN
	IF StepNo	= '' THEN RETURN
	IF ReactNo	= '' THEN RETURN
	IF RdsNo	= '' THEN RETURN
	IF CassNo	= '' THEN RETURN

	WOReactKey = WONo:'*':StepNo:'*':ReactNo

	otParms = 'WO_REACT':@RM:WOReactKey

	WOReactRec = obj_Tables('ReadOnlyRec',otParms)
	RDSNos	= WOReactRec<WO_REACT_RDS_NO$>
	CassNos = WOReactRec<WO_REACT_CASS_NO$>

	PSNos = XLATE('WO_LOG',WoNo,'PROD_VER_STEP_PSN','X')
	PSNo = PSNos<1,StepNo>

	QAMetData = obj_Prod_Spec('GetQAMet',PSNo:@RM:@RM:1)	;* * *   Get Reactor Scheduled QA Metrology   * * * 

	IF QAMetData = '' THEN RETURN		;* No reactor scheduled QA_MET specified

	Start		= QAMetData<COL$QA_MET_START>
	Interval	= QAMetData<COL$QA_MET_INTERVAL>
	Stage		= QAMetData<COL$QA_MET_STAGE>
	MetPropCd	= QAMetData<COL$QA_MET_PROP>
	GOSUB BuildAllTestPos		;* Set RDSNos, Start and Interval -> returns AllTestPos

	* * *   Find where the the RDS being added will be in the list.   * * *

	LOCATE RDSNo IN WOReactRec<WO_REACT_RDS_NO$> USING @VM SETTING InsPos THEN

		otParms = 'WO_REACT':@RM:WOReactKey
		WOReactRec = obj_Tables('UnlockRec',otParms)

		RETURN ;* RDSNo already in this list -> nothing to do.

	END ELSE

		LOCATE InsPos IN AllTestPos USING @VM SETTING TrimPos ELSE Null
		
		ChangePosList = FIELD(AllTestPos,@VM,TrimPos,SeqCnt)
		GOSUB RemSchedQA ;* Removes unsigned QAMet from cassettes scheduled past the box being removed
		
	END

	WOReactRec = INSERT(WOReactRec,WO_REACT_RDS_NO$,InsPos,0,RDSNo)
	WOReactRec = INSERT(WOReactRec,WO_REACT_CASS_NO$,InsPos,0,CassNo) ;* Inserts the passed in RDSNo & associated cassette number from the data fields

	RDSNos	= WOReactRec<WO_REACT_RDS_NO$>			;* List after insert
	CassNos = WOReactRec<WO_REACT_CASS_NO$>			;* List after insert

	* * *   Now build list for cassette	

	GOSUB BuildAllTestPos
	ChangePosList = FIELD(AllTestPos,@VM,TrimPos,SeqCnt)
	// Add QAMet spec data to cassettes now associated with the test
	// positions after removing the RDS & Cassno from the lists
	GOSUB AddSchedQA

	otParms = FIELDSTORE(otParms,@RM,4,0,WOReactRec)
	// Done with updates to the WO_REACT record
	obj_Tables('WriteOnlyRec',otParms)

	Result = Log

	EndTick = GetTickCount()
	Mona_Services('QueueLatencyAndCountMetrics', MonaResource, MetricName, StartTick, EndTick)

RETURN


* * * * * * *
RemRDSNo:
* * * * * * *

	StartTick  = GetTickCount()
	MetricName = 'RemRDSNo'

	WONo	= Parms[1,@RM]
	StepNo	= Parms[COL2()+1,@RM]
	ReactNo	= Parms[COL2()+1,@RM]
	RdsNo	= Parms[COL2()+1,@RM]
	CassNo	= Parms[COL2()+1,@RM]

	IF WONo		= '' THEN RETURN
	IF StepNo	= '' THEN RETURN
	IF ReactNo	= '' THEN RETURN
	IF RdsNo	= '' THEN RETURN
	IF CassNo	= '' THEN RETURN

	WOReactKey = WONo:'*':StepNo:'*':ReactNo

	otParms = 'WO_REACT':@RM:WOReactKey
	
	WOReactRec = obj_Tables('ReadOnlyRec',otParms)

	RDSNos	= WOReactRec<WO_REACT_RDS_NO$>
	CassNos = WOReactRec<WO_REACT_CASS_NO$>

	PSNos = XLATE('WO_LOG',WoNo,'PROD_VER_STEP_PSN','X')
	PSNo = PSNos<1,StepNo>

	QAMetData = obj_Prod_Spec('GetQAMet',PSNo:@RM:@RM:1)	;* * *   Get Reactor Scheduled QA Metrology   * * * 

	Start		= QAMetData<COL$QA_MET_START>
	Interval	= QAMetData<COL$QA_MET_INTERVAL>
	Stage		= QAMetData<COL$QA_MET_STAGE>
	MetPropCd	= QAMetData<COL$QA_MET_PROP>

	GOSUB BuildAllTestPos		;* Set RDSNos, Start and Interval -> returns AllTestPos

	IF AllTestPos NE '' THEN

		* * *   Find where the the RDS being removed is in the list.   * * *

		LOCATE RDSNo IN WOReactRec<WO_REACT_RDS_NO$> USING @VM SETTING RemPos THEN

			LOCATE RemPos IN AllTestPos USING @VM SETTING TrimPos ELSE Null
			
			ChangePosList = FIELD(AllTestPos,@VM,TrimPos,SeqCnt)
			
			GOSUB RemSchedQA ;* Removes unsigned QAMet from cassettes scheduled past the box being removed
		END ELSE
			
			otParms = 'WO_REACT':@RM:WOReactKey
			WOReactRec = obj_Tables('UnlockRec',otParms)

			RETURN		;* RDSNo is not in this list -> nothing to do.
		END
	END	;* End of check for AllTestPos null

	WOReactRec = DELETE(WOReactRec,WO_REACT_RDS_NO$,RemPos,0)
	WOReactRec = DELETE(WOReactRec,WO_REACT_CASS_NO$,RemPos,0) ;* Removes the passed in RDSNo & associated cassette number from the data fields

	RDSNos	= WOReactRec<WO_REACT_RDS_NO$>			;* List after removal
	CassNos = WOReactRec<WO_REACT_CASS_NO$>			;* List after removal

	* * *   Now build list for cassette	
			
	GOSUB BuildAllTestPos
	ChangePosList = FIELD(AllTestPos,@VM,TrimPos,SeqCnt)

	GOSUB AddSchedQA ;* Add QAMet spec data to cassettes now associated with the test positions after removing the RDS & Cassno from the lists
	otParms = FIELDSTORE(otParms,@RM,4,0,WOReactRec)

	obj_Tables('WriteOnlyRec',otParms) ;* Done with updates to the WO_REACT record

	Result = Log

	EndTick = GetTickCount()
	Mona_Services('QueueLatencyAndCountMetrics', MonaResource, MetricName, StartTick, EndTick)

RETURN


**************   Internal Methods   *************

* * * * * * * 
BuildAllTestPos:
* * * * * * *

	AllTestPos = ''

	RDSCnt = COUNT(RDSNos,@VM) + (RDSNos NE '')
	SeqCnt = 0


	FOR TestSeq = 1 TO RDSCnt
		IF REM((TestSeq - Start),Interval) = 0 THEN
			AllTestPos<1,-1> = TestSeq											;* Build list of list positions to have QAMet scheduled
			SeqCnt += 1
		END
	NEXT TestSeq

	Log := 'AllTestPos: ':AllTestPos:@FM

RETURN


* * * * * * *
RemSchedQA:
* * * * * * *

	chgCnt = COUNT(ChangePosList,@VM) + (ChangePosList NE '')

	FOR I = 1 TO chgCnt
		RemCassNo = CassNos<1,ChangePoslist<1,I>>
		Log:= 'RemSched on CassNo: ':RemCassNo:@FM
		
		owmParms = WONo:@RM:StepNo:@RM:RemCassNo:@RM:Stage:@RM:MetPropCd
		
		obj_WO_Mat_QA('RemQAMet',owmParms)											;* Unschedule test boxes before list change
		
		IF Get_Status(errCode) THEN
			ErrMsg(errCode)
		END
		
	NEXT I

RETURN


* * * * * * *
AddSchedQA:
* * * * * * *

	chgCnt = COUNT(ChangePosList,@VM) + (ChangePosList NE '')

	FOR I = 1 TO chgCnt
		AddCassNo = CassNos<1,ChangePoslist<1,I>>
		
		Log:= 'AddSched on CassNo: ':AddCassNo:@FM
		
		owmParms  = WONo:@RM
		owmParms := StepNo:@RM
		owmParms := AddCassNo:@RM
		owmParms := QAMetData<COL$QA_MET_STAGE>:@RM
		owmParms := QAMetData<COL$QA_MET_TEST>:@RM
		owmParms := QAMetData<COL$QA_MET_PROP>:@RM
		owmParms := QAMetData<COL$QA_MET_TOOL_CLASS>:@RM
		owmParms := QAMetData<COL$QA_MET_MIN>:@RM
		owmParms := QAMetData<COL$QA_MET_MAX>:@RM
		owmParms := QAMetData<COL$QA_MET_SLOT>:@RM
		owmParms := QAMetData<COL$QA_MET_RECIPE>:@RM
		owmParms := QAMetData<COL$QA_MET_RECIPE_PATTERN>:@RM
		owmParms := QAMetData<COL$QA_MET_WFR_QTY>:@RM
		owmParms := QAMetData<COL$QA_MET_WFR_TYPE>:@RM
		owmParms := QAMetData<COL$QA_MET_REACT_SCHED>:@RM
		owmParms := QAMetData<COL$QA_MET_SHIP_DOC>:@RM
		owmParms := QAMetData<COL$QA_MET_PHASE_MIN> ; // 10/15/18 - djs - Added Phase Min Spec for HgCV
		
		
		obj_WO_Mat_QA('AddQAMet', owmParms)					;* Schedule test boxes after list change
		
		IF Get_Status(errCode) THEN
			ErrMsg(errCode)
		END
		
	NEXT I

RETURN

