COMPILE FUNCTION obj_PRS_STAGE(Method,Parms)
/*
	Methods for the PRS_STAGE table
    
	05/23/2011 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Convert()						;* Creates and overwrites PRS_STAGE records based on PROD_SPEC record
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, NextKey
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, RList, ErrMsg, Btree.Extract, Send_Dyn

$INSERT MSG_EQUATES
$INSERT PRS_STAGE_EQUATES
$INSERT PROD_SPEC_EQUATES
$INSERT QUOTE_SPEC_EQU
 

ErrTitle = 'Error in Stored Procedure "obj_PRS_STAGE"'
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 = 'Convert'		; GOSUB Convert
	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


* * * * * * *
Convert:
* * * * * * *


RETURN

* Done on 16 March 2014 JCH
* new section added on 5 March 2015 by JCH -> moving QA_MET to PRS_STAGE records out of PROD_SPEC

* * * * * * *   T A K E   T H I S   O U T   W H E N   D O N E   CUTTING OVER!   * * * * * *** * * * *

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

	IF PSNo = '' THEN RETURN
	IF PSRec = '' THEN
		PSRec = XLATE('PROD_SPEC',PSNo,'','X')
		IF PSRec = '' THEN
			RETURN
		END
	END
	


	MetTests = PSRec<PROD_SPEC_MET_TEST$>
			
	mtCnt = COUNT(MetTests,@VM) + (MetTests NE '')

	FOR I = 1 TO mtCnt

		MetStage = PSRec<PROD_SPEC_MET_RUN_STEP$,I>
		
		StageKey = PSNo:'*':MetStage
		
		otParms = 'PRS_STAGE':@RM:StageKey
		StageRec = obj_Tables('ReadRec',otParms)
		
				
		MetTest = PSRec<PROD_SPEC_MET_TEST$,I>
		
		LOCATE MetTest IN StageRec<PRS_STAGE_MET_TEST$> USING @VM SETTING Pos ELSE NULL
		
		StageRec<PRS_STAGE_MET_TEST$,Pos>			= PSRec<PROD_SPEC_MET_TEST$,I>
		StageRec<PRS_STAGE_MET_INTERVAL$,Pos>		= PSRec<PROD_SPEC_MET_INTERVAL$,I>
		StageRec<PRS_STAGE_MET_START$,Pos>			= PSRec<PROD_SPEC_MET_START$,I>
		StageRec<PRS_STAGE_MET_SLOT$,Pos>			= PSRec<PROD_SPEC_MET_SLOT$,I>
		StageRec<PRS_STAGE_MET_MIN$,Pos>			= PSRec<PROD_SPEC_MET_MIN$,I>
		StageRec<PRS_STAGE_MET_MAX$	,Pos>			= PSRec<PROD_SPEC_MET_MAX$,I>
		StageRec<PRS_STAGE_MET_RECIPE$,Pos>			= PSRec<PROD_SPEC_MET_RECIPE$,I>
		StageRec<PRS_STAGE_MET_RECIPE_PATTERN$,Pos>	= PSRec<PROD_SPEC_MET_RECIPE_PATTERN$,I>
			
		
		otParms = FIELDSTORE(otParms,@RM,4,0,StageRec)
		obj_Tables('WriteRec',otParms)
		
		IF Get_Status(errCode) THEN ErrMsg(errCode)
		
	NEXT I

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

RETURN

* * * * * * * * * * * * * * * * * * * *

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

IF PSNo = '' THEN RETURN
IF PSRec = '' THEN
	PSRec = XLATE('PROD_SPEC',PSNo,'','X')
	IF PSRec = '' THEN
		RETURN
	END
END

* Pre Epi

PreKey = PSNo:'*PRE'
PreRec = ''

PreRec<PRS_STAGE_INST$>	= PSRec<PROD_SPEC_PRE_EPI_VER_INST$>

SCSubOxide			= PSRec<PROD_SPEC_SPEC_SUBSTRATE$,QSSubOxide$>							;* Cleans
SCOxideAngstroms	= PSRec<PROD_SPEC_SPEC_SUBSTRATE$,QSSubOxideAngstroms$>					;* Cleans	
SCTool				= PSRec<PROD_SPEC_SPEC_SUBSTRATE$,QSSubPreClean$>						;* Cleans
SCToolRecipe		= PSRec<PROD_SPEC_SPEC_SUBSTRATE$,QSSubPreAkrionRecipe$>				;* Cleans
	
CTool = TRIM(SCTool)

CONVERT @LOWER_CASE TO @UPPER_CASE in CTool
SWAP 'AKRION 1' WITH 'AKRION1' in CTool

IF CTool[1,2] _EQC 'NO' THEN CTool = ''
IF CTool[1,2] _EQC 'IF' THEN CTool = ''

SWAP ' OR ' WITH @VM IN CTool
SWAP 'SC1+SC2' WITH 'CB3' IN CTool
SWAP 'ONLY' WITH '' IN CTool
SWAP 'HF+ SC1' WITH 'CB3' IN CTool
SWAP 'SC1' WITH 'CB3' IN CTool

intCTools = TRIM(CTool)

nCnt = COUNT(intCTools,@VM) + (intCTools NE '')

Fixed = 0
FOR N = 1 TO nCnt
	intCTool = intCTools<1,N>
	IF IntCTool = 'AKRION' THEN
		intCTools<1,N> = 'AKRION1':@VM:'AKRION2'
		Fixed = 1
	END
UNTIL Fixed
NEXT N

SCTool = intCTools



IF SCSubOxide = 1 OR SCTool NE '' OR SCToolRecipe NE ''  THEN
	PreRec<PRS_STAGE_CLEAN_SUBOXIDE$>			= SCSubOxide
	PreRec<PRS_STAGE_CLEAN_OXIDE_ANGSTROMS$>	= SCOxideAngstroms
	PreRec<PRS_STAGE_CLEAN_TOOL$>				= SCTool
	PreRec<PRS_STAGE_CLEAN_RECIPE$>				= SCToolRecipe
END

SBrightLight	= PSRec<PROD_SPEC_PRE_BRIGHTLIGHT$>
SMicroscope		= PSRec<PROD_SPEC_PRE_MICROSCOPE$>

IF SBrightLight = 1 OR SMicroscope = 1 THEN
	PreRec<PRS_STAGE_BRIGHTLIGHT$>				= SBrightLight
	PreRec<PRS_STAGE_MICROSCOPE$>				= SMicroscope
	PreRec<PRS_STAGE_PITS$>						= PSRec<PROD_SPEC_PRE_PITS$>
	PreRec<PRS_STAGE_MOUNDS$>					= PSRec<PROD_SPEC_PRE_MOUNDS$>
	PreRec<PRS_STAGE_BL_DEFECTS$>				= PSRec<PROD_SPEC_PRE_BL_DEFECTS$>
	PreRec<PRS_STAGE_SPOTS$>					= PSRec<PROD_SPEC_PRE_SPOTS$>
	PreRec<PRS_STAGE_FOV$>						= PSRec<PROD_SPEC_PRE_FOV$>
	PreRec<PRS_STAGE_SCRATCHES$>				= PSRec<PROD_SPEC_PRE_SCRATCHES$>
	PreRec<PRS_STAGE_SCRATCH_LEN$>				= PSRec<PROD_SPEC_PRE_SCRATCH_LEN$>
	PreRec<PRS_STAGE_LPD$>						= PSRec<PROD_SPEC_PRE_LPD$>
END	

SSurfscan		= PSRec<PROD_SPEC_PRE_SURFSCAN$>
SSurfscanRecipe	= PSRec<PROD_SPEC_PRE_SURFSCAN_RECIPE$>
	
IF SSurfscan = 1 OR SSurfscanRecipe NE '' THEN
	PreRec<PRS_STAGE_SURFSCAN$>					= SSurfscan
	PreRec<PRS_STAGE_SURFSCAN_RECIPE$>			= SSurfscanRecipe
	PreRec<PRS_STAGE_SURF_HAZE$>				= PSRec<PROD_SPEC_PRE_SURF_HAZE$>
	PreRec<PRS_STAGE_SURF_DEFECTS$>				= PSRec<PROD_SPEC_PRE_SURF_DEFECTS$>
END

TestRec = PreRec
CONVERT @FM:@VM TO '':'' IN TestRec

IF TestRec NE '' THEN
	obj_Tables('WriteRec','PRS_STAGE':@RM:PreKey:@RM:@RM:PreRec)
END

* Load

LoadRec = ''
LoadKey = PSNo:'*UNLOAD'

LoadRec<PRS_STAGE_INST$> = PSRec<PROD_SPEC_LOAD_VER_INST$>

obj_Tables('WriteRec','PRS_STAGE':@RM:LoadKey:@RM:@RM:LoadRec)

* Wafer

WfrRec = ''

Stage = PSRec<PROD_SPEC_POST_FWI_LWI$>

IF Stage = '' THEN
	*DEBUG
	Send_Dyn(PSNo:' No WFR stage')
END ELSE

	WfrKey = PSNo:'*':Stage

	PreRec<PRS_STAGE_INST$>	= PSRec<PROD_SPEC_FIRST_VER_INST$>

	SBrightLight	= PSRec<PROD_SPEC_FIRST_BRIGHTLIGHT$>
	SMicroscope		= PSRec<PROD_SPEC_FIRST_MICROSCOPE$>
	SSurfscan		= PSRec<PROD_SPEC_FIRST_SURFSCAN$>
	SSurfscanRecipe	= PSRec<PROD_SPEC_FIRST_SURFSCAN_RECIPE$>
		
	IF SBrightLight = 1 OR SMicroscope = 1 THEN
		WfrRec<PRS_STAGE_BRIGHTLIGHT$>			= SBrightLight								;* Visual Inspection
		WfrRec<PRS_STAGE_MICROSCOPE$>			= SMicroscope								;* Visual Inspection
		WfrRec<PRS_STAGE_PITS$>					= PSRec<PROD_SPEC_POST_PITS$>				;* Visual Inspection
		WfrRec<PRS_STAGE_MOUNDS$>				= PSRec<PROD_SPEC_POST_MOUNDS$>				;* Visual Inspection
		WfrRec<PRS_STAGE_BL_DEFECTS$>			= PSRec<PROD_SPEC_POST_BL_DEFECTS$>			;* Visual Inspection
		WfrRec<PRS_STAGE_SPOTS$>				= PSRec<PROD_SPEC_POST_SPOTS$>				;* Visual Inspection
		WfrRec<PRS_STAGE_FOV$>					= PSRec<PROD_SPEC_POST_FOV$>				;* Visual Inspection
		WfrRec<PRS_STAGE_SCRATCHES$>			= PSRec<PROD_SPEC_POST_SCRATCHES$>			;* Visual Inspection
		WfrRec<PRS_STAGE_SCRATCH_LEN$>			= PSRec<PROD_SPEC_POST_SCRATCH_LEN$>		;* Visual Inspection
		WfrRec<PRS_STAGE_LPD$>					= PSRec<PROD_SPEC_POST_LPD$>				;* Visual Inspection
		WfrRec<PRS_STAGE_STACK_FAULTS$>			= PSRec<PROD_SPEC_POST_STACK_FAULTS$>		;* Visual Inspection
		WfrRec<PRS_STAGE_SPIKES$>				= PSRec<PROD_SPEC_POST_SPIKES$>				;* Visual Inspection
		WfrRec<PRS_STAGE_FWI_LWI$>				= PSRec<PROD_SPEC_POST_FWI_LWI$>			;* Visual Inspection	
	END

	IF SSurfscan = 1 OR SSurfscanRecipe NE '' THEN
		WfrRec<PRS_STAGE_SURFSCAN$>				= SSurfscan									;* Surface Scan
		WfrRec<PRS_STAGE_SURFSCAN_RECIPE$>		= SSurfscanRecipe							;* Surface Scan
		WfrRec<PRS_STAGE_SURF_HAZE$>			= PSRec<PROD_SPEC_POST_SURF_HAZE$>			;* Surface Scan
		WfrRec<PRS_STAGE_SURF_DEFECTS$>			= PSRec<PROD_SPEC_POST_SURF_DEFECTS$>		;* Surface Scan
	END

	TestRec = WfrRec
	CONVERT @FM:@VM TO '':'' IN TestRec

	IF TestRec NE '' THEN
		obj_Tables('WriteRec','PRS_STAGE':@RM:WfrKey:@RM:@RM:WfrRec)
	END
END

* Unload

UnloadRec = ''
UnloadKey = PSNo:'*UNLOAD'

UnloadRec<PRS_STAGE_INST$> = PSRec<PROD_SPEC_UNLOAD_VER_INST$>

obj_Tables('WriteRec','PRS_STAGE':@RM:UnloadKey:@RM:@RM:UnloadRec)

* Post

PostRec = ''
PostKey = PSNo:'*POST'

SCTool			= PSRec<PROD_SPEC_SPEC_SUBSTRATE$,QSSubPostClean$>
SCToolRecipe	= PSRec<PROD_SPEC_SPEC_SUBSTRATE$,QSSubPostAkrionRecipe$>
SBrightLight 	= PSRec<PROD_SPEC_POST_CLEAN_BRIGHTLIGHT$>
SSurfscan		= PSRec<PROD_SPEC_POST_CLEAN_SURFSCAN$>
SSurfscanRecipe	= PSRec<PROD_SPEC_POST_CLEAN_SURFSCAN_RECIPE$>

CTool = TRIM(SCTool)

CONVERT @LOWER_CASE TO @UPPER_CASE in CTool
SWAP 'AKRION 1' WITH 'AKRION1' in CTool

IF CTool[1,2] _EQC 'NO' THEN CTool = ''
IF CTool[1,2] _EQC 'IF' THEN CTool = ''

SWAP ' OR ' WITH @VM IN CTool
SWAP 'SC1+SC2' WITH 'CB3' IN CTool
SWAP 'ONLY' WITH '' IN CTool
SWAP 'HF+ SC1' WITH 'CB3' IN CTool
SWAP 'SC1' WITH 'CB3' IN CTool

intCTools = TRIM(CTool)

nCnt = COUNT(intCTools,@VM) + (intCTools NE '')

Fixed = 0
FOR N = 1 TO nCnt
	intCTool = intCTools<1,N>
	IF IntCTool = 'AKRION' THEN
		intCTools<1,N> = 'AKRION1':@VM:'AKRION2'
		Fixed = 1
	END
UNTIL Fixed
NEXT N

SCTool = intCTools

IF SCTool NE '' OR SCToolRecipe NE '' THEN
	PostRec<PRS_STAGE_CLEAN_TOOL$>			= SCTool								;* Cleans
	PostRec<PRS_STAGE_CLEAN_RECIPE$>		= SCToolRecipe							;* Cleans
END

IF SBrightLight = 1 THEN
	PostRec<PRS_STAGE_BRIGHTLIGHT$>		= SBrightLight								;* Visual Inspection
	PostRec<PRS_STAGE_PITS$>			= PSRec<PROD_SPEC_POST_PITS$>				;* Visual Inspection
	PostRec<PRS_STAGE_MOUNDS$>			= PSRec<PROD_SPEC_POST_MOUNDS$>				;* Visual Inspection
	PostRec<PRS_STAGE_BL_DEFECTS$>		= PSRec<PROD_SPEC_POST_BL_DEFECTS$>			;* Visual Inspection
	PostRec<PRS_STAGE_SPOTS$>			= PSRec<PROD_SPEC_POST_SPOTS$>				;* Visual Inspection
	PostRec<PRS_STAGE_FOV$>				= PSRec<PROD_SPEC_POST_FOV$>				;* Visual Inspection
	PostRec<PRS_STAGE_SCRATCHES$>		= PSRec<PROD_SPEC_POST_SCRATCHES$>			;* Visual Inspection
	PostRec<PRS_STAGE_SCRATCH_LEN$>		= PSRec<PROD_SPEC_POST_SCRATCH_LEN$>		;* Visual Inspection
	PostRec<PRS_STAGE_LPD$>				= PSRec<PROD_SPEC_POST_LPD$>				;* Visual Inspection
	PostRec<PRS_STAGE_STACK_FAULTS$>	= PSRec<PROD_SPEC_POST_STACK_FAULTS$>		;* Visual Inspection
	PostRec<PRS_STAGE_SPIKES$>			= PSRec<PROD_SPEC_POST_SPIKES$>				;* Visual Inspection
END

IF SSurfscan = 1 OR SSurfscanRecipe NE '' THEN
	PostRec<PRS_STAGE_SURFSCAN$>		= SSurfscan										;* Surface Scan
	PostRec<PRS_STAGE_SURFSCAN_RECIPE$>	= SSurfscanRecipe								;* Surface Scan
	PostRec<PRS_STAGE_SURF_HAZE$>		= PSRec<PROD_SPEC_POST_CLEAN_SURF_HAZE$>		;* Surface Scan
	PostRec<PRS_STAGE_SURF_DEFECTS$>	= PSRec<PROD_SPEC_POST_CLEAN_SURF_DEFECTS$>		;* Surface Scan
	PostRec<PRS_STAGE_SS_SAMP_QTY$>		= PSRec<PROD_SPEC_POST_CLEAN_SS_SAMP_QTY$>		;* Surface Scan
END

TestRec = PostRec
CONVERT @FM:@VM TO '':'' IN TestRec

IF TestRec NE '' THEN
	obj_Tables('WriteRec','PRS_STAGE':@RM:PostKey:@RM:@RM:PostRec)
END

* QA

QARec = ''
QAKey = PSNo:'*QA'

QARec<PRS_STAGE_INST$> = PSRec<PROD_SPEC_FINAL_VER_INST$>

obj_Tables('WriteRec','PRS_STAGE':@RM:QAKey:@RM:@RM:QARec)


RETURN


