COMPILE FUNCTION obj_WO_Mat_Met(Method,Parms)
/*
	Methods for WO_MAT_Met - WO_MAT metrology table
    
	05/5/2015 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Convert(WONo)							;* Move metrology data from WO_MAT to WO_MAT_MET table

	
*/


DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, ErrMsg, Btree.Extract

DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, Dialog_Box, obj_WO_Log, NextKey, obj_WO_Mat_Met
DECLARE FUNCTION obj_Prod_Spec, obj_RDS_Test


$INSERT MSG_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT PRS_LAYER_EQU


EQU CRLF$	TO \0D0A\

ErrTitle = 'Error in Stored Procedure "obj_RDS_Test"'
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 = ''

BEGIN CASE
	//CASE Method = 'Create'				; GOSUB Create
	CASE Method = 'GetQAMetKeys'		; GOSUB GetQAMetKeys
	CASE Method = 'AddQAMet'			; GOSUB AddQAMet
	CASE Method = 'RemQAMet'			; GOSUB RemQAMet
	//CASE Method = 'Convert'				; GOSUB Convert
	
	CASE 1
		ErrorMsg = 'Unknown Method ':QUOTE(Method):' passed to routine.'

END CASE

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

RETURN Result



* * * * * * *
GetQAMetKeys:
* * * * * * *

WOMatKey = Parms[1,@RM]
WOMatMetRec = Parms[COL2()+1,@RM]

IF WOMatKey = '' THEN RETURN
IF WOMatMetRec = '' THEN
	WOMatMetRec = XLATE('WO_MAT_MET',WOMatKey,'','X')
	IF WOMatMetRec = '' THEN RETURN
END

ProdVerNo = WOMatMetRec<WO_MAT_PROD_VER_NO$>

StepPSNos = XLATE('PROD_VER',ProdVerNo,PROD_VER_PROC_STEP_PSN$,'X')

psCnt = COUNT(StepPSNos,@VM) + (StepPSNos NE '')

LineCnt = 1

FOR I = 1 TO psCNT
	StepPSNo = StepPSNos<1,I>
	
	QAMetStruct = obj_Prod_Spec('GetQAMet',StepPSNo:@RM:'')
	
	msCnt = COUNT(QAMetStruct<COL$QA_MET_STAGE>,@VM) + (QAMetStruct<COL$QA_MET_STAGE> NE '')
	
	FOR N = 1 TO msCnt
		Result<1,LineCnt> = I:'*':QAMetStruct<COL$QA_MET_STAGE,N>:'*':QAMetStruct<COL$QA_MET_PROP,N>
		Result<2,LineCnt> = LineCnt
		LineCnt += 1
	NEXT N
	
NEXT I

RETURN



* * * * * * *
AddQAMet:
* * * * * * *

WONo 				= Parms[1,@RM]
WOStep				= Parms[COL2()+1,@RM]
CassNo				= Parms[COL2()+1,@RM]
Stage				= Parms[COL2()+1,@RM]
MetTest				= Parms[COL2()+1,@RM]
MetProp				= Parms[COL2()+1,@RM]
MetToolClass		= Parms[COL2()+1,@RM]
MetMin				= Parms[COL2()+1,@RM]
MetMax				= Parms[COL2()+1,@RM]
MetSlot				= Parms[COL2()+1,@RM]
MetRecipe			= Parms[COL2()+1,@RM]
MetRecipePattern	= Parms[COL2()+1,@RM]

IF WONo = ''		THEN ErrorMsg = 'Null Parm "WONo" passed to routine. (':Method:')'
IF WOStep = ''		THEN ErrorMsg = 'Null Parm "WOStep" passed to routine. (':Method:')'
IF CassNo = ''		THEN ErrorMsg = 'Null Parm "CassNo" passed to routine. (':Method:')'
IF Stage = ''		THEN ErrorMsg = 'Null Parm "Stage" passed to routine. (':Method:')'
IF MetProp = ''		THEN ErrorMsg = 'Null Parm "MetProp" passed to routine. (':Method:')'
 
IF ErrorMsg NE '' THEN RETURN

NewMetKey = WOStep:'*':Stage:'*':MetProp
						
WOMatKey 	= WONo:'*':CassNo
otParms		= 'WO_MAT_MET':@RM:WOMatKey
WOMatMetRec	= obj_Tables('ReadRec',otParms)

IF WOMatMetRec = '' THEN RETURN

AllQAMetKeysStruct	= obj_WO_Mat_Met('GetQAMetKeys',WOMatKey:@RM:WOMatMetRec) ;* All QAMetSort Keys Specified for this Work Order  Returns Keys in <1> and Sequence numbers = <2>

AllMetKeys	= AllQAMetKeysStruct<1>		;* Correctly sorted list of all possible QA Met keys built from the PRS_SPEC records for all WO Steps
AllLines	= AllQAMetKeysStruct<2>		;* List of position numbers for each of the met keys

CurrMetKeys = ''
CurrLines = ''
cmqCnt = COUNT(WOMatMetRec<WO_MAT_MET_PROFILE$>,@VM) + (WOMatMetRec<WO_MAT_MET_PROFILE$> NE '')

FOR M = 1 to cmqCnt
	CurrMetKey			= WOMatMetRec<WO_MAT_MET_PROFILE$,M>[1,1]:'*':WOMatMetRec<WO_MAT_MET_RUN_STEP$,M>:'*':WOMatMetRec<WO_MAT_MET_PROP$,M>
	CurrMetKeys<1,M>	= CurrMetKey
	
	LOCATE CurrMetKey IN AllMetKeys USING @VM SETTING AllPos THEN
		CurrLines<1,M> = AllPos													;* Builds a list of CurrLines for the Met Keys on the existing WO_MAT records
	END
NEXT M

LOCATE NewMetKey IN AllMetKeys USING @VM SETTING SpecLine THEN

	LOCATE SpecLine IN CurrLines BY 'AR' USING @VM SETTING InsertPos THEN
	
		obj_Tables('UnlockRec',otParms)
		
	END ELSE
	
		CurrLines = INSERT(CurrLines,1,InsertPos,0,SpecLine)
		
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_PROFILE$, InsertPos, 0, WOStep:MetTest)
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_PROP$,InsertPos,0,MetProp)
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_TOOL_CLASS$, InsertPos, 0, MetToolClass)
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_RUN_STEP$, InsertPos, 0, Stage)
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_MIN$, InsertPos, 0, MetMin)
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_MAX$, InsertPos, 0, MetMax)
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_SLOT$, InsertPos, 0, MetSlot)
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_RECIPE$, InsertPos, 0, MetRecipe)
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_RECIPE_PATTERN$, InsertPos, 0, MetRecipePattern)
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_SIG$, InsertPos, 0, '')
		WOMatMetRec = INSERT(WOMatMetRec, WO_MAT_MET_SIG_DTM$, InsertPos, 0, '')
		
		otParms = FIELDSTORE(otParms,@RM,4,0,WOMatMetRec)
		obj_Tables('WriteRec',otParms)
		
	END		;* End of SpecLine LOCATE
	
END	ELSE

	obj_Tables('UnlockRec',otParms)
	
END	;* End of NewMetKey LOCATE




RETURN



* * * * * * *
RemQAMet:
* * * * * * *

WONo 	= Parms[1,@RM]
WOStep	= Parms[COL2()+1,@RM]
CassNo	= Parms[COL2()+1,@RM]
Stage	= Parms[COL2()+1,@RM]
PropCd	= Parms[COL2()+1,@RM]

IF WONo = ''		THEN ErrorMsg = 'Null Parm "WONo" passed to routine. (':Method:')'
IF WOStep = ''		THEN ErrorMsg = 'Null Parm "WOStep" passed to routine. (':Method:')'
IF CassNo = ''		THEN ErrorMsg = 'Null Parm "CassNo" passed to routine. (':Method:')'
IF Stage = ''		THEN ErrorMsg = 'Null Parm "Stage" passed to routine. (':Method:')'
IF PropCd = ''		THEN ErrorMsg = 'Null Parm "PropCd" passed to routine. (':Method:')'
 
IF ErrorMsg NE '' THEN RETURN

WOMatKey 	= WONo:'*':CassNo
otParms		= 'WO_MAT_MET':@RM:WOMatKey
WOMatMetRec	= obj_Tables('ReadRec',otParms)

CurrMetKeys = ''

cmqCnt = COUNT(WOMatMetRec<WO_MAT_MET_PROFILE$>,@VM) + (WOMatMetRec<WO_MAT_MET_PROFILE$> NE '')

FOR M = 1 to cmqCnt
	CurrMetKey			= WOMatMetRec<WO_MAT_MET_PROFILE$,M>[1,1]:'*':WOMatMetRec<WO_MAT_MET_RUN_STEP$,M>:'*':WOMatMetRec<WO_MAT_MET_PROP$,M>
	CurrMetKeys<1,M>	= CurrMetKey
NEXT M

DelMetKey = WOStep:'*':Stage:'*':PropCd

LOCATE DelMetKey IN CurrMetKeys USING @VM SETTING DelPos THEN

	IF WOMatMetRec<WO_MAT_MET_SIG$,DelPos> = '' THEN
		
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_PROFILE$, DelPos, 0)
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_PROP$,DelPos,0)
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_TOOL_CLASS$, DelPos, 0)
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_RUN_STEP$, DelPos, 0)
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_MIN$, DelPos, 0)
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_MAX$, DelPos, 0)
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_SLOT$, DelPos, 0)
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_RECIPE$, DelPos, 0)
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_RECIPE_PATTERN$, DelPos, 0)
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_SIG$, DelPos, 0)
		WOMatMetRec = DELETE(WOMatMetRec, WO_MAT_MET_SIG_DTM$, DelPos, 0)
		
		otParms = FIELDSTORE(otParms,@RM,4,0,WOMatMetRec)
		obj_Tables('WriteRec',otParms)

	END ELSE
		obj_Tables('UnlockRec',otParms)
	END
	
END ELSE
	obj_Tables('UnlockRec',otParms)
END


RETURN







