COMPILE FUNCTION obj_RDS2(Method,Parms)
#pragma precomp SRP_PreCompiler

/*
	Methods for RDS table		;Original object (obj_RDS) is full with methods for original data structures
    
	05/12/2005 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Find()												;* Lookup RDS record
    Delete(RdsNos)										;* Delete RDS records (@VM'd list of keys)
    LockSet(RdsNos)										;* Returns TableVar if all records were locked
    UnlockSet(RdsNos,TableVar)							;* Uses TableVar returned from LockSet call
    ChangeWOStepProps(RSNos,CommitDts,HotLots,MUBoxes)	;* Changes RDS stuff 'en-mass' from WOStep window
    AdjStepWafersIn(
	GetMetKeys(
	MetConversion(
	LoadEpi(
	ReloadEpi(
	RemovePocketNCR(
*/


DECLARE FUNCTION   Get_Status, Msg, Utility, obj_Tables, NextKey,  obj_Prod_Spec, Dialog_Box, obj_RDS_Test
DECLARE FUNCTION   obj_React_Status, Logging_Services, Environment_Services, Database_Services, Error_Services
DECLARE FUNCTION   Material_Services, SRP_Array, Lot_Services
DECLARE SUBROUTINE Set_Status, Msg, obj_Tables, obj_WO_Step, obj_RDS_Layer, obj_RDS_Test,  obj_React_Status, obj_WO_Wfr
DECLARE SUBROUTINE RDS_React_Run, obj_WM_Out, obj_WM_In, ErrMsg, Send_Info, obj_Clean_Insp, Database_Services
DECLARE SUBROUTINE Logging_Services, Lot_Services

$INSERT MSG_EQUATES
$INSERT WO_VERIFY_EQU
$INSERT WO_LOG_EQU
$INSERT RDS_EQUATES
$INSERT RDS_MAKEUP_EQU
$INSERT RDS_LAYER_EQUATES
$INSERT RDS_TEST_EQUATES
$INSERT RDS_LAYER_INFO_EQU
$INSERT QUOTE_SPEC_EQU
$INSERT PROD_SPEC_EQUATES
$INSERT SCHEDULE_EQU
$INSERT WO_STEP_EQU
$INSERT REACT_RUN_EQUATES
$INSERT DICT_EQUATES
$INSERT LOGICAL
$INSERT PRS_LAYER_EQU	;* Used to return obj_Prod_Spec values


EQU SHEETRHO_SPEC_UNITS$	TO \EA2FDC\
EQU RES_SPEC_UNITS$			TO \EA2D636D\

* Equates used with LoadEpi and UnloadEpi (EpiPRO wafers)

EQU COL$POCKET		TO 1
EQU COL$USAGE		TO 2
EQU COL$ZONE		TO 3
EQU COL$IN_CASS		TO 4
EQU COL$IN_SLOT		TO 5
EQU COL$WAFER_TYPE	TO 6
EQU COL$OUT_CASS	TO 7
EQU COL$OUT_SLOT	TO 8
EQU COL$OUT_NCR		TO 9

equ RDS_TW_PPLUS$					to 102
equ RDS_TW_PMINUS$					to 103
equ RDS_TW_NPLUS$					to 104
equ RDS_TW_NMINUS$					to 105
equ RDS_TW_PPLUS_CODE$ 				to 106
equ RDS_TW_PMINUS_CODE$				to 107
equ RDS_TW_NPLUS_CODE$				to 108
equ RDS_TW_NMINUS_CODE$				to 109
equ RDS_TW_PPLUS_CNT$				to 110
equ RDS_TW_PMINUS_CNT$				to 111
equ RDS_TW_NPLUS_CNT$ 				to 112
equ RDS_TW_NMINUS_CNT$ 				to 113

equ RDS_TW_PROD_CODE$				to 164
equ RDS_TW_PROD_CNT$				to 165
equ RDS_TW_PPLUS_TOOL$ 				to 166
equ RDS_TW_PMINUS_TOOL$				to 167
equ RDS_TW_NPLUS_TOOL$ 				to 168
equ RDS_TW_NMINUS_TOOL$ 			to 169
equ RDS_TW_PROD_TOOL$				to 170
equ RDS_TW_PPLUS_THICK$				to 171
equ RDS_TW_PMINUS_THICK$			to 172
equ RDS_TW_NPLUS_THICK$ 			to 173
equ RDS_TW_NMINUS_THICK$			to 174
equ RDS_TW_PROD_THICK$ 				to 175
equ RDS_TW_PPLUS_RES$				to 176
equ RDS_TW_PMINUS_RES$				to 177
equ RDS_TW_NPLUS_RES$				to 178
equ RDS_TW_NMINUS_RES$ 				to 179
equ RDS_TW_PROD_RES$				to 180
equ RDS_TW_PPLUS_CON$				to 181
equ RDS_TW_PMINUS_CON$				to 182
equ RDS_TW_NPLUS_CON$				to 183
equ RDS_TW_NMINUS_CON$				to 184
equ RDS_TW_PROD_CON$				to 185
equ RDS_TW_PPLUS_STRESS$			to 186
equ RDS_TW_PMINUS_STRESS$			to 187
equ RDS_TW_NPLUS_STRESS$			to 188
equ RDS_TW_NMINUS_STRESS$			to 189

equ RDS_TW_RECLAIM$					to 240
equ RDS_TW_RECLAIM_CODE$			to 241
equ RDS_TW_RECLAIM_CNT$				to 242
equ RDS_TW_RECLAIM_CON$				to 243
equ RDS_TW_RECLAIM_RES$				to 244
equ RDS_TW_RECLAIM_RHO$				to 245
equ RDS_TW_RECLAIM_STRESS$			to 246
equ RDS_TW_RECLAIM_THICK$			TO 247
equ RDS_TW_RECLAIM_TOOL$			to 248
equ RDS_TW_RECLAIM_TRANS$			to 249

LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\EpiPro'
LogDate     = Oconv(Date(), 'D4/')
LogTime     = Oconv(Time(), 'MTS')
LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' Unload Log.csv'
Headers     = 'Logging DTM' : @FM : 'RDS Key ID' : @FM : 'Error'
objLog      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, Comma$, Headers, '', False$, False$)
LoggingDTM  = LogDate : ' ' : LogTime   ; // Logging DTM

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

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 = 'Find'				; GOSUB Find
	CASE Method = 'Delete'				; GOSUB Delete
	CASE Method = 'LockSet'				; GOSUB LockSet
	CASE Method = 'UnlockSet'			; GOSUB UnlockSet
	CASE Method = 'ChangeWOStepProps'	; GOSUB ChangeWOStepProps
	CASE Method = 'AdjStepWafersIn'		; GOSUB AdjStepWafersIn
	CASE Method = 'GetMetKeys'			; GOSUB GetMetKeys
	CASE Method = 'MetConversion'		; GOSUB MetConversion
	CASE Method = 'UnloadEpi'			; GOSUB UnloadEpi
	CASE Method = 'ReloadEpi'			; GOSUB ReloadEpi
	CASE Method = 'ClearEpiUnload'		; GOSUB ClearEpiUnload
	CASE Method = 'LoadMat'				; GOSUB LoadMat
	CASE Method = 'ReturnMat'			; GOSUB ReturnMat
	CASE Method = 'RemovePocketNCR'		; GOSUB RemovePocketNCR
	CASE Method = 'SetPartNo'			; GOSUB SetPartNo
	CASE Method = 'FieldsUsed'			; GOSUB FieldsUsed
	CASE 1

END CASE

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

RETURN Result



* * * * * * * 
FieldsUsed:
* * * * * * *

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

Fields = XLATE('DICT.RDS','%FIELDS%','','X')

KList = 2:@VM:3:@VM:4:@VM:6:@VM:8:@VM:9:@VM:10:@VM:12:@VM:14:@VM:34:@VM:31:@VM:35:@VM:36:@VM:37:@VM:38:@VM:39:@VM:40

Ans = ''
FOR I = 1 TO 278
	IF RDSRec<I> NE '' THEN
		LOCATE I IN KList USING @VM SETTING Dummy ELSE
			LOCATE I IN Fields<FIELDS_FIELD_NO$> USING @VM SETTING Pos THEN
				Ans<1,-1> = I:' ':Fields<FIELDS_NAME$,Pos>
			END
		END
	
	END	;* End of check for field null
NEXT I

Result = Ans



RETURN



* * * * * * *
Find:
* * * * * * *


RDSKeys = Dialog_Box('DIALOG_RDS_QUERY',@WINDOW,'')

Result = RDSKeys
		
RETURN


* * * * * * *
Delete:
* * * * * * *

RdsNos		= Parms[1,@RM]
TableVar	= Parms[COL2()+1,@RM]

IF RdsNos = '' THEN RETURN

IF TableVar = '' THEN
	* This section of code should be removed after all calls to obj_RDS('Delete'....) have been checked
	Set_Status(0)
	RTParms = 'RDS'
	LockedRDSNos = ''

	FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
		RdsNo = RdsNos<1,I>
		RTParms = FieldStore(RTParms, @RM, 2, 1, RdsNo)
		obj_Tables('LockRec',RTParms)
		IF Get_Status(errCode) THEN
			FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
				RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
				obj_Tables('UnlockRec',RTParms)									;* Unlock everything locked up to here
			NEXT N
			ErrorMsg = 'Unable to lock RDS ':QUOTE(RdsNo):' for delete.'
			RETURN
		END ELSE
			LockedRDSNos<1,I> = RdsNo
		END
	NEXT I

	TableVar = FIELD(RTParms,@RM,3,1)
	
END



FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
	RdsNo	= RdsNos<1,I>
	RDSRec	= XLATE('RDS',RdsNo,'','X')
	
	LSKeys			= RDSRec<RDS_RDS_LAYER_KEYS$>
	NCRKeys			= RDSRec<RDS_NCR_KEYS$>
	SurfScanKeys	= RDSRec<RDS_SURFSCAN_KEYS$>
	CIKeys			= XLATE('REACT_RUN',RdsNo,REACT_RUN_CI_NO$,'X')
	
	MetKeys	= XLATE('RDS_LAYER',LSKeys,RDS_LAYER_RDS_TEST_KEYS$,'X')
	
	RTParms = 'RDS':@RM:RdsNo:@RM:TableVar:@RM
	obj_Tables('UnlockRec',RTParms)
	
	IF NCRKeys NE '' THEN
		OPEN 'NCR' TO NCRFile THEN
			NCRCnt = COUNT(NCRKeys,@VM)
			FOR NCR = 1 TO NCRCnt
				DELETE NCRFile,NCRKeys<1,NCR>
				
			NEXT NCR
		END
	END	;* End of NCR section
	
	IF SurfScanKeys NE '' THEN
		OPEN 'SURFACE_SCAN' TO SurfScanTable THEN
			ScanCnt = COUNT(SurfScanKeys,@VM) + (SurfScanKeys NE '')
			FOR Scan = 1 TO ScanCnt
				DELETE SurfScanTable,SurfScanKeys<1,Scan>
			NEXT Scan
		END	;* End of SURFACE_SCAN open
	END	;* End of SurfScan sectioin
	
	obj_RDS_Test('Delete',MetKeys)
	
	obj_RDS_Layer('Delete',LSKeys)
	
	obj_Clean_Insp('Delete',CIKeys)
	
	obj_Tables('DeleteRec','RDS':RTParms)				;* Deletes and removes the lock
NEXT I

RETURN


* * * * * * * 
LockSet:
* * * * * * * 

RdsNos 		= Parms[1,@RM]

IF RdsNos	= '' THEN RETURN

Set_Status(0)
RTParms = 'RDS'
LockedRDSNos = ''

FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
	RdsNo = RdsNos<1,I>
	RTParms = FieldStore(RTParms, @RM, 2, 1, RdsNo)
	obj_Tables('LockRec',RTParms)
	IF Get_Status(errCode) THEN
		FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
			RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
			obj_Tables('UnlockRec',RTParms)									;* Unlock everything locked up to here
		NEXT N
		ErrorMsg = 'Unable to lock RDS ':QUOTE(RdsNo):'.'
		RETURN
	END ELSE
		LockedRDSNos<1,I> = RdsNo
	END
NEXT I

TableVar = FIELD(RTParms,@RM,3,1)

Result = TableVar

RETURN


* * * * * * * 
UnlockSet:
* * * * * * * 

RdsNos 		= Parms[1,@RM]
TableVar	= Parms[COL2()+1,@RM]

IF RdsNos	= '' THEN RETURN
IF TableVar = '' THEN RETURN

Set_Status(0)
RTParms = 'RDS':@RM:@RM:TableVar
LockedRDSNos = ''

FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
	RdsNo = RdsNos<1,I>
	RTParms = FieldStore(RTParms, @RM, 2, 1, RdsNo)
	obj_Tables('UnlockRec',RTParms)
NEXT I

RETURN



* * * * * * *
ChangeWOStepProps:
* * * * * * *

RDSNos		= Parms[1,@RM]
CommitDts	= Parms[COL2()+1,@RM]
HotLots		= Parms[COL2()+1,@RM]
MUBoxes		= Parms[COL2()+1,@RM]

IF NOT(ASSIGNED(RDSNos))		THEN ErrorMsg = 'Unassigned Parm "RDSNos" passed to routine. (':Method:')'
IF NOT(ASSIGNED(CommitDts))		THEN CommitDts = ''
IF NOT(ASSIGNED(HotLots))		THEN HotLots = ''
IF NOT(ASSIGNED(MUBoxes))		THEN MUBoxes = ''

IF ErrorMsg NE '' THEN RETURN

IF RDSNos = '' THEN RETURN

Set_Status(0)
RTParms = 'RDS'
LockedRDSNos = ''

FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
	
	RdsNo = RdsNos<1,I>
	RTParms = FieldStore(RTParms, @RM, 2, 1, RdsNo)

	obj_Tables('LockRec',RTParms)
	IF Get_Status(errCode) THEN
		FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
			RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
			obj_Tables('UnlockRec',RTParms)		;* Unlock everything locked up to here
		NEXT N
		ErrorMsg = 'Unable to lock RDS ':QUOTE(RdsNo):' for update.'
		RETURN
	END ELSE
		LockedRDSNos<1,I> = RdsNo
	END
NEXT I

CommitDts	= ICONV(CommitDts,'D')		;* Move to internal format
HotLots		= ICONV(HotLots,'B')
MUBoxes		= ICONV(MUBoxes,'B')

RDSTableVar		= FIELD(RTParms,@RM,3)

FOR I = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
	LockedRDSNo = LockedRDSNos<1,I>
	READ RDSRec FROM RDSTableVar,LockedRDSNo THEN
		CommitDt	= CommitDts<1,I>
		HotLot		= HotLots<1,I>
		MUBox		= MUBoxes<1,I>
		
		IF CommitDt NE RDSRec<RDS_COMMIT_DATE_FINAL$>	THEN RDSRec<RDS_COMMIT_DATE_FINAL$>	= CommitDt
		*IF HotLot	NE RDSRec<RDS_HOT_LOT$>				THEN RDSRec<RDS_HOT_LOT$>			= HotLot	;* No longer used
		*IF MUBox NE RDSRec<RDS_MAKEUP_BOX$>			THEN RDSRec<RDS_MAKEUP_BOX$> 		= MUBox		;* No longer used
		
		RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNo)
		RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
		obj_Tables('WriteRec',RTParms)								;* Write and unlock RDS records
	END
NEXT I

RETURN


* * * * * * *
AdjStepWafersIn:
* * * * * * *

RETURN

* Dead as of 11/5/2008 JCH

StepCompRdsNos  = Parms[1,@RM]
AdjRdsNos		= Parms[COL2()+1,@RM]

IF StepCompRdsNos = ''	THEN ErrorMsg = 'Null parameter "StepCompRdsNos" passed to routine. (':Method:')'
IF AdjRdsNos = ''		THEN ErrorMsg = 'Null parameter "AdjRdsNos" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

Set_Status(0)
RTParms = 'RDS'
LockedRDSNos = ''

FOR I = 1 TO COUNT(AdjRdsNos,@VM) + (AdjRdsNos NE '')
	
	AdjRdsNo = AdjRdsNos<1,I>
		
	RTParms = FieldStore(RTParms, @RM, 2, 1, AdjRdsNo)

	obj_Tables('LockRec',RTParms)
	IF Get_Status(errCode) THEN
		FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
			RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
			obj_Tables('UnlockRec',RTParms)		;* Unlock everything locked up to here
		NEXT N
		ErrorMsg = 'Unable to lock RDS ':QUOTE(AdjRdsNo):' for update.'
		RETURN
	END ELSE
		LockedRDSNos<1,I> = AdjRdsNo
	END
NEXT I

RDSTableVar		= FIELD(RTParms,@RM,3)

FOR I = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
	LockedRDSNo = LockedRDSNos<1,I>
	
	READ RDSRec FROM RDSTableVar,LockedRDSNo THEN

		SCRdsNo						= StepCompRdsNos<1,I>
		SCWafersOut					= XLATE('RDS',SCRdsNo,'WFRS_OUT','X')		;* Wafers out from previous step
		RDSRec<RDS_CASS_WAFER_QTY$>	= SCWafersOut
			
		RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNo)
		RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
		obj_Tables('WriteRec',RTParms)											;* Write and unlock RDS records
	END ELSE
		status = Set_FSError()
		FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
			RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
			obj_Tables('UnlockRec',RTParms)		;* Unlock everything locked up to here
		NEXT N
		
	END
NEXT I

RETURN


* * * * * * *
GetMetKeys:
* * * * * * *

* Returns data structure of 'LSID*ZONE' - @VM in first column associated RDS_TEST numbers in 2nd Column.

RdsNo  = Parms[1,@RM]

IF RdsNo = '' THEN RETURN

SortedLSKeys = XLATE('RDS',RdsNo,'RDS_LS_SORTED','X')
CONVERT @VM TO @FM IN SortedLSKeys

IF SortedLSKeys NE '' THEN
	LSMetKeys = XLATE('RDS_LAYER',SortedLSKeys,RDS_LAYER_RDS_TEST_KEYS$,'X')
	RDSMetZones = XLATE('RDS_TEST',LSMetKeys,RDS_TEST_ZONE$,'X')
	ResultLine = 0
	FOR I = 1 TO COUNT(SortedLSKeys,@FM) + (SortedLSKeys NE '')
		SortedLSKey = SortedLSKeys<I>
		LSMetKey = LSMetKeys<I>
		
		FOR N = 1 TO COUNT(LSMetKey,@VM) + (LSMetKey NE '')
			ResultLine += 1
			Result<1,ResultLine> = FIELD(SortedLSKey,'*',2):'*':RDSMetZones<I,N>
			Result<2,ResultLine> = LSMetKeys<I,N>
		NEXT N
	NEXT I
END

RETURN


* * * * * * *
UnloadEpi:
* * * * * * * 

RdsNo			= Parms[1,@RM]
ReactorList		= Parms[COL2()+1,@RM]
AvailSlotKeys	= Parms[COL2()+1,@RM]

IF RdsNo		= ''	THEN ErrorMsg = 'Null parameter "RdsNo" passeed to routine.'
IF ReactorList	= ''	THEN ErrorMsg = 'Null parameter "ReactorList" passeed to routine.'
IF AvailSlotKeys = ''	THEN ErrorMsg = 'Null parameter "AvailSlotKeys" passeed to routine.'

IF ErrorMsg NE '' THEN RETURN

Send_Info('Reading RDS ':RdsNo:'...')

Set_Status(0)
RTParms = 'RDS':@RM:RdsNo
* RDSRec  = obj_Tables('ReadRec',RTParms)
RDSRec  = Database_Services('ReadDataRow', 'RDS', RDSNo)

IF Get_Status(errCode) THEN
	ErrorMsg = 'Unable to read RDS ':QUOTE(RdsNo):' for UnloadEpi update.'
	RETURN
END

Reactor = RDSRec<RDS_REACTOR$>

RLCnt = COUNT(ReactorList,@FM) + (ReactorList NE '')

LoadFailed = 0
LoadCnt = 0
OutCassettes = ''

CassIDs		= ''
OutSlotNos	= ''
RDSNos		= ''
PocketNos	= ''
Zones		= ''
InCassNos	= ''
InSlotNos	= ''

Send_Info('Building WM_OUT Load Data ...')

FOR I = 1 TO RLCnt
	
	IF ReactorList<I,COL$WAFER_TYPE> = 'PROD' AND ReactorList<I,COL$USAGE> = '' AND ReactorList<I,COL$OUT_CASS> = '' THEN
		AvailSlotKey = AvailSlotKeys[1,@VM]
		AvailSlotKeys[1,COL2()] = ''
		
		WONo		= AvailSlotKey[1,'*']
		WOStep		= AvailSlotKey[COL2()+1,'*']
		OutCassNo	= AvailSlotKey[COL2()+1,'*']
		OutSlotNo	= AvailSlotKey[COL2()+1,'*']
		PocketNo	= ReactorList<I,COL$POCKET>
		Zone		= ReactorList<I,COL$ZONE>
		InCass		= ReactorList<I,COL$IN_CASS>
		InSlot		= ReactorList<I,COL$IN_SLOT>
		
		LOCATE OutCassNo IN OutCassettes USING @VM SETTING Pos ELSE
			OutCassettes = INSERT(OutCassettes,1,Pos,0,OutCassNo)
		END
	
		CassID = WONo:'*':WOStep:'*':OutCassNo
		
		LOCATE CassID IN CassIDs USING @FM SETTING Pos THEN
			OutSlotNos	= INSERT(OutSlotNos,Pos,-1,0,OutSlotNo)
			RDSNos		= INSERT(RDSNos,Pos,-1,0,RDSNo)
			PocketNos	= INSERT(PocketNos,Pos,-1,0,PocketNo)
			Zones		= INSERT(Zones,Pos,-1,0,Zone)
			InCassNos	= INSERT(InCassNos,Pos,-1,0,InCass)
			InSlotNos	= INSERT(InSlotNos,Pos,-1,0,InSlot)
		END ELSE
			CassIDs		= INSERT(CassIDs,Pos,0,0,CassID)
			OutSlotNos	= INSERT(OutSlotNos,Pos,-1,0,OutSlotNo)
			RDSNos		= INSERT(RDSNos,Pos,-1,0,RDSNo)
			PocketNos	= INSERT(PocketNos,Pos,-1,0,PocketNo)
			Zones		= INSERT(Zones,Pos,-1,0,Zone)
			InCassNos	= INSERT(InCassNos,Pos,-1,0,InCass)
			InSlotNos	= INSERT(InSlotNos,Pos,-1,0,InSlot)
		END
		
		RDSRec<RDS_OUT_CASS_NO$,I> = OutCassNo
		RDSRec<RDS_OUT_SLOT_NO$,I> = OutSlotNo
		
		IF RDSRec<RDS_WAFER_CHAR$,I> = 'PROD' THEN LoadCnt += 1
		
	END
UNTIL LoadFailed
	
NEXT I

* RDSRec<RDS_OPERATOR_OUT$> = @USER4
* RDSRec<RDS_DATE_OUT$> = Date()
* RDSRec<RDS_TIME_OUT$> = Time()

UnloadDTM = OCONV(RDSRec<RDS_DATE_OUT$>, 'D4/' ):' ':OCONV( RDSRec<RDS_TIME_OUT$>, 'MTH' )
 	
RemCassIDs		= ''
RemOutSlotNos	= ''

UnloadFailed = 0

CassCnt = COUNT(CassIDs,@FM) + (CassIDs NE '')

Def               = ""
Def<MCAPTION$>    = "Unloading material... Please, do not interupt this process. This may take a while!"
Def<MTYPE$>       = "G"
Def<MEXTENT$>     = CassCnt
Def<MTEXTWIDTH$>  = 600
MsgUp             = Msg(@WINDOW, Def) ;* Start gas guage message

FOR N = 1 TO CassCnt

	CassID = CassIDs<N>
	
	Send_Info('Adding Wafers to WM_OUT ':CassID:'...')
	
	WONo	= CassID[1,'*']
	WOStep	= CassID[COL2()+1,'*']
	OutCass	= CassID[COL2()+1,'*']
	
	owParms  = WONo:@RM
	owParms := WOStep:@RM
	owParms := OutCass:@RM
	owParms := OutSlotNos<N>:@RM
	owParms := RDSNos<N>:@RM
	owParms := PocketNos<N>:@RM
	owParms := Zones<N>:@RM
	owParms := InCassNos<N>:@RM
	owParms := InSlotNos<N>

	Set_Status(0)
	obj_WM_Out('AddWafer',owParms)
	
	ErrorMessage = ''
	If Error_Services('HasError') then ErrorMessage = Error_Services('GetMessage')
	Status       = Get_Status(errCode)
	
	IF (Error_Services('HasError') or Get_Status(errCode) ) THEN
	
	    LogData = ''
	    LogData<1> = LoggingDTM
	    LogData<2> = @User4
	    LogData<3> = RDSNo
	    LogData<4> = 'Epi Unload Error. Error code: ':errCode:'.'
	    Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
	    
		UnloadFailed = 1
	
		RemCnt = COUNT(RemCassIDs,@FM) + (RemCassIDs NE '')
		
		FOR R = 1 TO RemCnt
			RemCassID = RemCassIDs<R>
			
			Send_Info('Epi Unload Error - Clearing Partial Unload data ':RemCassID:'...')
			
			WONo	= RemCassID[1,'*']
			WOStep	= RemCassID[COL2()+1,'*']
			OutCass	= RemCassID[COL2()+1,'*']
			
			OutSlotNos = RemOutSlotNos<R>
			
			Set_Status(0)
			
			obj_WM_Out('RemoveWafer',WONo:@RM:WOStep:@RM:OutCass:@RM:OutSlotNos)
		NEXT R
		
	END ELSE
		RemCassIDs<-1> = CassID
		RemOutSlotNos<-1> = OutSlotNos<N>
	END
Running = Msg(@WINDOW, MsgUp, N, MSGINSTUPDATE$) ;* Update message
UNTIL UnloadFailed
NEXT N

IF NOT(UnloadFailed) THEN

	Send_Info('Saving Updated RDS ':RdsNo:'...')
	
    Database_Services('WriteDataRow', 'RDS', RDSNo, RDSRec, True$, False$, True$)
    If Error_Services('HasError') then
        UnloadFailed = True$
        ErrorMsg     = Error_Services('GetMessage')
        LogData      = ''
        LogData<1>   = LoggingDTM
        LogData<2>   = @User4
        LogData<3>   = RDSNo
        LogData<4>   = ErrorMsg
        Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
        ErrMsg(ErrorMsg)
    end

	* Update Reactor Status with load data
	
	If UnloadFailed EQ False$ then
*        Send_Info('Updating Reactor ':Reactor:' Status...')
        
*        InCassettes = SRP_Array('Clean', InCassNos, 'TrimAndMakeUnique', @VM)
*        obj_React_Status('ReactorUnload',Reactor:@RM:WONo:@RM:InCassettes:@RM:UnloadDTM:@RM:RDSNo)

        IF Get_Status(errCode) THEN ErrMsg(errCode)
        
        Send_Info('Updating REACT_RUN record...')

        RDS_React_Run(RDSNo)		;* Conversion to REACT_RUN code 9/25/2008 JCH *******
    end
	
	// Verify RDS, REACT_RUN, and WM_OUT cassette & slot data match.
	DataMatch = Material_Services('VerifyEPPUnload', RDSNo)
	
	// If data does not match, then back out all changes and log the error.
	If ( (DataMatch EQ False$) or (UnloadFailed EQ True$) ) then
	    ErrorMsg   = Error_Services('GetMessage')
	    LogData    = ''
        LogData<1> = LoggingDTM
        LogData<2> = @User4
        LogData<3> = RDSNo
        LogData<4> = ErrorMsg
        Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
	    
	    // Clear WM_OUT data
	    UnloadFailed = 1
		RemCnt       = COUNT(RemCassIDs,@FM) + (RemCassIDs NE '')
		FOR R = 1 TO RemCnt
			RemCassID = RemCassIDs<R>
			Send_Info('Epi Unload Error - Clearing Partial Unload data ':RemCassID:'...')
			WONo	= RemCassID[1,'*']
			WOStep	= RemCassID[COL2()+1,'*']
			OutCass	= RemCassID[COL2()+1,'*']
			OutSlotNos = RemOutSlotNos<R>
			Set_Status(0)
			obj_WM_Out('RemoveWafer',WONo:@RM:WOStep:@RM:OutCass:@RM:OutSlotNos)
			If Error_Services('HasError') then
			    ErrorMsg   = Error_Services('GetMessage')
                LogData    = ''
                LogData<1> = LoggingDTM
                LogData<2> = @User4
                LogData<3> = WONo:'*':WOStep:'*':OutCass
                LogData<4> = ErrorMsg
                Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
			end
		NEXT R
	
        // Clear RDS Data
        RDSRec<RDS_OPERATOR_OUT$> = ''
        RDSRec<RDS_DATE_OUT$>     = ''
        RDSRec<RDS_TIME_OUT$>     = ''
        RDSRec<RDS_OUT_CASS_NO$>  = ''
        RDSRec<RDS_OUT_SLOT_NO$>  = ''
        Database_Services('WriteDataRow', 'RDS', RDSNo, RDSRec, True$, False$, False$)
        
        // Clear REACT_RUN data (copy cleared RDS data to REACT_RUN)
        RDS_React_Run(RDSNo)
        
    end else
        LotId = Lot_Services('GetLotIdByLegacyLotIdAndType', RDSNo, 'RDS')
        If Error_Services('NoError') then 
            Lot_Services('MoveOutLot', LotId, @User4) ; // Move out of POST_EPI operation
            Lot_Services('MoveInLot', LotId, @User4)  ; // Move into RDS_CLOSE operation
            Lot_Services('CloseLot', LotId)
        end
    end

END ELSE
	obj_Tables('UnlockRec',RTParms)
END

Msg(@WINDOW,MsgUp)      ;* Take message down

RETURN


* * * * * * *
ReloadEpi:
* * * * * * * 

RdsNo	= Parms[1,@RM]

IF RdsNo = ''	THEN
	ErrorMsg = 'Null parameter "RdsNo" passeed to routine.'
	RETURN
END

Send_Info('Reading RDS ':RdsNo:'...')

LocalLockFlag = 1
Set_Status(0)
RTParms = 'RDS':@RM:RdsNo
RDSRec = obj_Tables('ReadRec',RTParms)

IF Get_Status(errCode) THEN
	ErrorMsg = 'Unable to read RDS ':QUOTE(RdsNo):' for ReloadEpi update.'
	RETURN
END

WONo	= RDSRec<RDS_WO$>
WOStep	= RDSRec<RDS_WO_STEP_KEY$>[-1,'B*']
ReactNo	= RDSRec<RDS_REACTOR$>
ToolID	= 'R':ReactNo

RLCnt = COUNT(RDSRec<RDS_POCKET$>,@VM) + (RDSRec<RDS_POCKET$> NE '')

ReloadFailed = 0
LoadCnt = 0
RLWaferIDs = ''

OutCassIDs = ''
PocketNos	= ''
Zones		= ''
WaferChars	= ''
PocketChars	= ''
InCassNos	= ''
InSlotNos   = ''
OutSlots	= ''

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

Send_Info('Building WM_OUT Reload Data ...')

WfrIDs = ''					;* Added 9/7/2016 JCH	
RunLocs = ''				;* Added 9/7/2016 JCH
CurrSlotIDs = ''			;* Added 9/7/2016 JCH

FOR I = 1 TO RLCnt
	
	PocketNo	= RDSRec<RDS_POCKET$,I>
	Zone		= RDSRec<RDS_ZONE$,I>
	WaferChar	= RDSRec<RDS_WAFER_CHAR$,I>
	PocketChar	= RDSRec<RDS_POCKET_CHAR$,I>
	InCass		= RDSRec<RDS_IN_CASS_NO$,I>
	InSlot		= RDSRec<RDS_IN_SLOT_NO$,I>
	OutCass		= RDSRec<RDS_OUT_CASS_NO$,I>
	OutSlot		= RDSRec<RDS_OUT_SLOT_NO$,I>     

	IF WaferChar = 'PROD' AND PocketChar = '' AND OutCass NE '' THEN
		
		WfrIDs<1,-1>		= WONo:'*':InCass:'*':InSlot				;* Added 9/7/2016 JCH
		RunLocs<1,-1>		= RDSNo:'*':PocketNo:'*':Zone				;* Added 9/7/2016 JCH
		CurrSlotIDs<1,-1>	= WONo:'*':OutCass:'*':Outslot				;* Added 9/7/2016 JCH
		
		* * * *   Added 3/22/2016  JCH - wafer history   * * * * 
		
		Parms  = WONo:'*':InCass:'*':InSlot:@RM			;* WfrID
		Parms := CurrDTM:@RM							;* EventDtm
		Parms := @USER4:@RM								;* EventBy
		Parms := 'RELOAD':@RM							;* Event
		Parms := '':@RM									;* NewSlotID
		Parms := RDSNo:'*':PocketNo:'*':Zone:@RM		;* RunLoc
		Parms := '':@RM									;* NCRNo
		Parms := '':@RM									;* TWUse
		Parms := WONo:'*':OutCass:'*':Outslot:@RM		;* CurrSlotID
		Parms := ToolID:@RM								;* NewToolID (Load Tool)
		Parms := '':@RM									;* CurrToolID (Unload Tool)
		Parms := '':@RM									;* NewInvLoc
		Parms := '':@RM									;* CurrInvLoc
		Parms := 'O'									;* Wfr Side

		obj_WO_Wfr('AddEvent',Parms)
		
		
		OutCassID = WONo:'*':WOStep:'*':OutCass
		
		LOCATE OutCassID IN OutCassIDs USING @FM SETTING Pos THEN
			PocketNos	= INSERT(PocketNos,Pos,-1,0,PocketNo)
			Zones		= INSERT(Zones,Pos,-1,0,Zone)
			WaferChars	= INSERT(WaferChars,Pos,-1,0,WaferChar)
			PocketChars = INSERT(PocketChars,Pos,-1,0,PocketChar)
			InCassNos	= INSERT(InCassNos,Pos,-1,0,InCass)
			InSlotNos	= INSERT(InSlotNos,Pos,-1,0,InSlot)
			OutSlots	= INSERT(OutSlots,Pos,-1,0,OutSlot)
			
		END ELSE
			OutCassIDs	= INSERT(OutCassIDs,Pos,0,0,OutCassID)
			PocketNos	= INSERT(PocketNos,Pos,-1,0,PocketNo)
			Zones		= INSERT(Zones,Pos,-1,0,Zone)
			WaferChars	= INSERT(WaferChars,Pos,-1,0,WaferChar)
			PocketChars = INSERT(PocketChars,Pos,-1,0,PocketChar)
			InCassNos	= INSERT(InCassNos,Pos,-1,0,InCass)
			InSlotNos	= INSERT(InSlotNos,Pos,-1,0,InSlot)
			OutSlots	= INSERT(OutSlots,Pos,-1,0,OutSlot)
		END
		
		RDSRec<RDS_OUT_CASS_NO$,I> = ''
		RDSRec<RDS_OUT_SLOT_NO$,I> = ''
	
	END
UNTIL ReloadFailed
NEXT I




/*

* * * *   Added 9/7/2016  JCH - wafer history   * * * * 

IF WfrIDs NE '' THEN
		
		Parms  = WfrIDs:@RM								;* WfrIDs
		Parms := CurrDTM:@RM							;* EventDtm
		Parms := @USER4:@RM								;* EventBy
		Parms := 'RELOAD':@RM							;* Event
		Parms := '':@RM									;* NewSlotIDs
		Parms := RunLocs:@RM							;* RunLocs
		Parms := '':@RM									;* NCRNo
		Parms := '':@RM									;* TWUse
		Parms := CurrSlotIDs:@RM						;* CurrSlotIDs
		Parms := ToolID:@RM								;* NewToolID (Load Tool)
		Parms := '':@RM									;* CurrToolID (Unload Tool)
		Parms := '':@RM									;* NewInvLoc
		Parms := '':@RM									;* CurrInvLoc
		Parms := 'O'									;* Wfr Side

		obj_WO_Wfr('AddEvent',Parms)
		
END		

*/


RepCassIDs		= ''
RepOutSlotNos	= ''
RepRDSNos		= ''
RepPocketNos	= ''
RepZones		= ''
RepInCassNos	= ''
RepInSlotNos	= ''


ReloadFailed = 0

CassCnt = COUNT(OutCassIDs,@FM) + (OutCassIDs NE '')

FOR I = 1 TO CassCnt

	OutCassID = OutCassIDs<I>
	
	Send_Info('Removing Run Data from WM_OUT ':OutCassID:'...')
	
	WONo	= OutCassID[1,'*']
	WOStep	= OutCassID[COL2()+1,'*']
	OutCass	= OutCassID[COL2()+1,'*']
	
	owParms  = WONo:@RM
	owParms := WOStep:@RM
	owParms := OutCass:@RM
	owParms := OutSlots<I>
	
	Set_Status(0)
	obj_WM_Out('RemoveWafer',owParms)
	
	IF Get_Status(ErrCode) THEN
		ReloadFailed = 1
		RepCnt = COUNT(RepCassIDs,@FM) + (RepCassIDs NE '')
		
		FOR N = 1 TO RepCnt
			RepCassID	= RepCassIDs<N>
			
			Send_Info('Epi Reload Error - Restoring Unload Data ':RepCassID:'...')
			
			WONo	= RepCassID[1,'*']
			WOStep	= RepCassID[COL2()+1,'*']
			OutCass	= RepCassID[COL2()+1,'*']
			
			owParms  = WONo:@RM
			owParms := WOStep:@RM
			owParms := OutCass:@RM
			owParms := RepOutSlotNos<N>:@RM
			owParms := RepRDSNos<N>:@RM
			owParms := RepPocketNos<N>:@RM
			owParms := RepZones<N>:@RM
			owParms := RepInCassNos<N>:@RM
			owParms := RepInSlotNos<N>

			Set_Status(0)
			obj_WM_Out('AddWafer',owParms)

		NEXT N
		
	END ELSE
	
		RepCassIDs<-1> 		= OutCassID
		RepOutSlotNos<-1>	= OutSlots<I>
		RepRDSNos<-1>		= RDSNo
		RepPocketNos<-1>	= PocketNos<I>
		RepZones<-1>		= Zones<I>
		RepInCassNos<-1>	= InCassNos<I>
		RepInSlotNos<-1>	= InSlotNos<I>
	
	END
UNTIL ReloadFailed
NEXT I

IF NOT(ReloadFailed) THEN

	Send_Info('Saving RDS record ':RdsNo:'...')

	RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
	obj_Tables('WriteRec',RTParms)
			
	Send_Info('Updating REACT_RUN record...')
		
	RDS_React_Run(RDSNo)		;* Conversion to REACT_RUN code 9/25/2008 JCH **********************************************
END ELSE
	obj_Tables('UnlockRec',RTParms)
END


RETURN


* * * * * * *
ClearEpiUnload:
* * * * * * * 

* Clears unload destination data - used when RDS & WM_OUT get completely out of sync


RdsNo	= Parms[1,@RM]

IF RdsNo = ''	THEN
	ErrorMsg = 'Null parameter "RdsNo" passeed to routine.'
	RETURN
END

Send_Info('Reading RDS ':RdsNo:'...')

Set_Status(0)
RTParms = 'RDS':@RM:RdsNo
RDSRec = obj_Tables('ReadRec',RTParms)

IF Get_Status(errCode) THEN
	ErrorMsg = 'Unable to read RDS ':QUOTE(RdsNo):' for ClearEpiUnload process.'
	RETURN
END

RLCnt = COUNT(RDSRec<RDS_POCKET$>,@VM) + (RDSRec<RDS_POCKET$> NE '')

FOR I = 1 TO RLCnt
		RDSRec<RDS_OUT_CASS_NO$,I> = ''
		RDSRec<RDS_OUT_SLOT_NO$,I> = ''
NEXT I

RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
obj_Tables('WriteRec',RTParms)
			
Send_Info('Updating REACT_RUN record...')
		
RDS_React_Run(RDSNo)		;* Conversion to REACT_RUN code 9/25/2008 JCH **********************************************


RETURN


* * * * * * *
LoadMat:
* * * * * * *

RdsNo		= Parms[1,@RM]
ReactorList	= Parms[COL2()+1,@RM]	;* Contains susceptor load configuration 
WMIKeys		= Parms[COL2()+1,@RM]	;* Contains list of locked WMI slots to use

IF RdsNo		= ''	THEN ErrorMsg = 'Null parameter "RdsNo" passeed to routine.'
IF ReactorList	= ''	THEN ErrorMsg = 'Null parameter "ReactorList" passeed to routine.'
IF WMIKeys 		= ''	THEN ErrorMsg = 'Null parameter "AvailSlotKeys" passeed to routine.'

IF ErrorMsg NE '' THEN RETURN

Send_Info('Reading RDS ':RdsNo:'...')

Set_Status(0)
RTParms = 'RDS':@RM:RdsNo
RDSRec = obj_Tables('ReadRec',RTParms)

IF Get_Status(errCode) THEN
	ErrorMsg = 'Unable to read RDS ':QUOTE(RdsNo):' for UnloadEpi update.'
	RETURN
END

// Set BLOCK_LOAD flag until the process completes
RDSRec<RDS_BLOCK_LOAD$> = True$
Database_Services('WriteDataRow', 'RDS', RDSNo, RDSRec, True$, False$, True$)

Reactor = RDSRec<RDS_REACTOR$>

RLCnt = COUNT(ReactorList,@FM) + (ReactorList NE '')

LOOP
	LastLine = ReactorList[-1,'B':@FM]
UNTIL LastLine<1,COL$POCKET> NE '' OR ReactorList = ''
	ReactorList[COL1(),99] = ''
	RLCnt -= 1
REPEAT


LoadFailed	= 0
LoadCnt		= 0
InCassettes	= ''

CassIDs			= ''
CassSlots		= ''
CassSlotRDSNos	= ''
CassSlotPkts	= ''
CassSlotZones	= ''

Send_Info('Building Cassette Unload Data ...')

FOR I = 1 TO RLCnt
	
	IF ReactorList<I,COL$IN_CASS> = '' AND ReactorList<I,COL$WAFER_TYPE> = '' AND ReactorList<I,COL$USAGE> NE 'DUMMY' THEN
		WMIKey = WMIKeys[1,@VM]
		
		WMIKeys[1,COL2()] = ''
		
		WONo	= WMIKey[1,'*']
		WOStep	= WMIKey[COL2()+1,'*']
		InCass	= WMIKey[COL2()+1,'*']
		InSlot	= WMIKey[COL2()+1,'*']
		
		PocketNo	= ReactorList<I,COL$POCKET>
		Usage		= ReactorList<I,COL$USAGE>
		Zone		= ReactorList<I,COL$ZONE>
		
		CassID = WONo:'*':WOStep:'*':InCass

		LOCATE CassID IN CassIDs USING @FM SETTING Pos THEN
			CassSlots		= INSERT(CassSlots,Pos,-1,0,InSlot)
			CassSlotRDSNos	= INSERT(CassSlotRDSNos,Pos,-1,0,RDSNo)
			CassSlotPkts	= INSERT(CassSlotPkts,Pos,-1,0,PocketNo)
			CassSlotZones	= INSERT(CassSlotZones,Pos,-1,0,Zone)
		END ELSE
			CassIDs			= INSERT(CassIDs,Pos,0,0,CassID)
			CassSlots		= INSERT(CassSlots,Pos,-1,0,InSlot)
			CassSlotRDSNos	= INSERT(CassSlotRDSNos,Pos,-1,0,RDSNo)
			CassSlotPkts	= INSERT(CassSlotPkts,Pos,-1,0,PocketNo)
			CassSlotZones	= INSERT(CassSlotZones,Pos,-1,0,Zone)
		END
		
		RDSRec<RDS_POCKET$,I>		= PocketNo
		RDSRec<RDS_ZONE$,I>			= Zone
		RDSRec<RDS_POCKET_CHAR$,I>	= Usage
		RDSRec<RDS_IN_CASS_NO$,I>	= InCass
		RDSRec<RDS_IN_SLOT_NO$,I>	= InSlot
		RDSRec<RDS_WAFER_CHAR$,I>	= 'PROD'
		
		LoadCnt += 1
		
		LOCATE InCass IN InCassettes USING @VM SETTING Pos ELSE
			InCassettes = INSERT(InCassettes,1,Pos,0,InCass)
		END
		
	END ELSE
		RDSRec<RDS_POCKET$,I>		= ReactorList<I,COL$POCKET>
		RDSRec<RDS_ZONE$,I>			= ReactorList<I,COL$ZONE>
		RDSRec<RDS_POCKET_CHAR$,I>	= ReactorList<I,COL$USAGE>
		RDSRec<RDS_WAFER_CHAR$,I>	= ReactorList<I,COL$WAFER_TYPE>
	END
UNTIL LoadFailed
NEXT I

LoadFailed = 0

CassIDCnt = COUNT(CassIDs,@FM) + (CassIDs NE '')

RepCassIDs = ''
RepCassSlots = ''

Def               = ""
Def<MCAPTION$>    = "Loading material... Please, do not interupt this process. This may take a while!"
Def<MTYPE$>       = "G"
Def<MEXTENT$>     = CassIDCnt
Def<MTEXTWIDTH$>  = 600
MsgUp             = Msg(@WINDOW, Def) ;* Start gas guage message

FOR I = 1 TO CassIDCnt
	CassID	= CassIDs<I>
	
	Send_Info('Pulling Material from WM_IN ':CassID:'...')
	
	WONo	= CassID[1,'*']
	WOStep	= CassID[COL2()+1,'*']
	InCass	= CassID[COL2()+1,'*']
	
	owParms  = WONo:@RM
	owParms := WOStep:@RM
	owParms := InCass:@RM
	owParms := CassSlots<I>:@RM
	owParms := CassSlotRDSNos<I>:@RM
	owParms := CassSlotPkts<I>:@RM
	owParms := CassSlotZones<I>:@RM
			
	Set_Status(0)
	obj_WM_In('RemoveWafer',owParms)
	
	IF Get_Status(errCode) THEN
	
		ErrorMsg = 'Unable to read RDS ':QUOTE(RdsNo):' for UnloadEpi update.'

		LoadFailed = 1
		RepCnt = COUNT(RepCassIDs,@FM) + (RepCassIDs NE '')
		
		FOR N = 1 TO RepCnt
			RepCassID = RepCassIDs<N>
			
			Send_Info('Replacing Material in WM_IN ':RepCassID:'...')
	
			WONo 	= RepCassID[1,'*']
			WOStep	= RepCassID[COL2()+1,'*']
			InCass	= RepCassID[COL2()+1,'*']
			
			owParms  = WONo:@RM
			owParms := WOStep:@RM
			owParms := InCass:@RM
			owParms := RepCassSlots<N>:@RM
		
			obj_WM_In('ReplaceWafer',owParms)
			
		NEXT N
	END	ELSE
		RepCassIDs<-1> = CassID
		RepCassSlots<-1> = CassSlots<I>
	END
	Running = Msg(@WINDOW, MsgUp, I, MSGINSTUPDATE$) ;* Update message
UNTIL LoadFailed
NEXT I


IF NOT(LoadFailed) THEN

	* Update Reactor Status with load data
	
	*Send_Info('Updating Reactor ':Reactor:' status...')

	LoadDTM = OCONV( Date(), 'D2/' ):' ':OCONV( Time(), 'MTH' )
	*IdleTime = obj_React_Status('ReactorLoad',Reactor:@RM:WONo:@RM:InCassettes:@RM:LoadDTM)

	IF Get_Status(errCode) THEN ErrMsg(errCode)

	RDSRec<RDS_WAFERS_IN$>			= LoadCnt
	RDSRec<RDS_VERIFY_QTY$>			= LoadCnt
	RDSRec<RDS_CASS_WAFER_QTY$>		= LoadCnt
	*RDSRec<RDS_REACT_IDLE_TIME$>	= ICONV(IdleTime,'MD2')	;* hours to 2 decimal places
	
	Send_Info('Saving Updated RDS ':RDSNo:'...')

* 	RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
	
	// Remove BLOCK_LOAD flag since the process has completed
    RDSRec<RDS_BLOCK_LOAD$> = False$
* 	obj_Tables('WriteRec',RTParms)
    Database_Services('WriteDataRow', 'RDS', RDSNo, RDSRec, True$, False$, True$)
    Database_Services('ReleaseKeyIDLock', 'RDS', RDSNo)
	Send_Info('Updating REACT_RUN record...')

	RDS_React_Run(RDSNo)		;* Conversion to REACT_RUN code 9/25/2008 JCH **********************************************
	
END ELSE
	RDSRec = obj_Tables('UnlockRec',RTParms)
END

Msg(@WINDOW,MsgUp)      ;* Take message down

RETURN


* * * * * * *
ReturnMat:
* * * * * * *

RdsNo = Parms[1,@RM]

IF RdsNo = '' THEN ErrorMsg = 'Null parameter "RdsNo" passeed to routine.'

IF ErrorMsg NE '' THEN RETURN

Send_Info('Reading RDS Record ':RdsNo:'...')

Set_Status(0)
RTParms = 'RDS':@RM:RdsNo
RDSRec = obj_Tables('ReadRec',RTParms)

IF Get_Status(errCode) THEN
	ErrorMsg = 'Unable to read RDS ':QUOTE(RdsNo):' for ReturnMat update.'
	RETURN
END

WONo	= RDSRec<RDS_WO$>
WOStep	= RDSRec<RDS_WO_STEP_KEY$>[-1,'B*']

RLCnt = COUNT(RDSRec<RDS_POCKET$>,@VM) + (RDSRec<RDS_POCKET$> NE '')

ReloadFailed = 0
LoadCnt = 0
RLWaferData = ''

CassIDs			= ''
CassSlots		= ''
CassSlotRDSNos	= ''
CassSlotPkts	= ''
CassSlotZones	= ''

Send_Info('Building Cassette Material Return data...')

FOR I = 1 TO RLCnt
	
	PocketNo	= RDSRec<RDS_POCKET$,I>
	Zone		= RDSRec<RDS_ZONE$,I>
	WaferChar	= RDSRec<RDS_WAFER_CHAR$,I>
	PocketChar	= RDSRec<RDS_POCKET_CHAR$,I>
	InCass		= RDSRec<RDS_IN_CASS_NO$,I>
	InSlot		= RDSRec<RDS_IN_SLOT_NO$,I>
	OutCass		= RDSRec<RDS_OUT_CASS_NO$,I>
	OutSlot		= RDSRec<RDS_OUT_SLOT_NO$,I>     

	IF WaferChar = 'PROD' AND InCass NE '' THEN
							
		CassID = WONo:'*':WOStep:'*':InCass
		
		LOCATE CassID IN CassIDs USING @FM SETTING Pos THEN
			CassSlots		= INSERT(CassSlots,Pos,-1,0,InSlot)
			CassSlotRDSNos	= INSERT(CassSlotRDSNos,Pos,-1,0,RdsNo)
			CassSlotPkts	= INSERT(CassSlotPkts,Pos,-1,0,PocketNo)
			CassSlotZones	= INSERT(CassSlotZones,Pos,-1,0,Zone)
		END ELSE
			CassIDs			= INSERT(CassIDs,Pos,0,0,CassID)
			CassSlots		= INSERT(CassSlots,Pos,-1,0,InSlot)
			CassSlotRDSNos	= INSERT(CassSlotRDSNos,Pos,-1,0,RDSNo)
			CassSlotPkts	= INSERT(CassSlotPkts,Pos,-1,0,PocketNo)
			CassSlotZones	= INSERT(CassSlotZones,Pos,-1,0,Zone)
		END
		
		RDSRec<RDS_IN_CASS_NO$,I>	= ''
		RDSRec<RDS_IN_SLOT_NO$,I>	= ''
		RDSRec<RDS_POCKET_CHAR$,I>	= ''
		RDSRec<RDS_WAFER_CHAR$,I>	= ''
		
	END	;* End of test for PROD wafers
	
	
UNTIL ReloadFailed
NEXT I

RepCassIDs			= ''
RepCassSlots		= ''
RepCassSlotRDSNos	= ''
RepCassSlotPkts		= ''
RepCassSlotZones	= '' 

ReloadFailed = 0

CassIDCnt = COUNT(CassIDs,@FM) + (CassIDs NE '')

FOR I = 1 TO CassIDCnt
	CassID = CassIDs<I>
	
	Send_Info('Returning Material to WM_IN ':CassID:'...')
	
	WONo	= CassID[1,'*']
	WOStep	= CassID[COL2()+1,'*']
	InCass	= CassID[COL2()+1,'*']
	
	owParms  = WONo:@RM
	owParms := WOStep:@RM
	owParms := InCass:@RM
	owParms := CassSlots<I>
	
	Set_Status(0)
	obj_WM_In('ReplaceWafer',owParms)			;* Puts wafer back in the WM_IN record
	
	IF Get_Status(errCode) THEN
		ReloadFailed = 1
		RepCnt = COUNT(RepCassIDs,@FM) + (RepCassIDs NE '')
		
		FOR N = 1 TO RepCnt
			RepCassID = RepCassIDs<N>
			
			Send_Info('Restoring WM_IN record ':RepCassID:'...')
			
			WONo	= RepCassID[1,'*']
			WOStep	= RepCassID[COL2()+1,'*']
			InCass	= RepCassID[COL2()+1,'*']
			
			owParms  = WONo:@RM
			owParms := WOStep:@RM
			owParms := InCass:@RM
			owParms := RepCassSlots<N>
			owParms := RepCassSlotRDSNos<N>
			owParms := RepCassSlotPkts<N>
			owParms := RepCassSlotZones<N>
			
			obj_WM_In('ReplaceWafer',owParms)
		NEXT N
		
	END ELSE
		RepCassIDs<-1>			= CassIDs<I>
		RepCassSlots<-1>		= CassSlots<I>
		RepCassSlotRDSNos<-1>	= CassSlotRDSNos<I>
		RepCassSlotPkts<-1>		= CassSlotPkts<I>
		RepCassSlotZones<-1>	= CassSlotZones<I>
	END
UNTIL ReloadFailed
NEXT I


IF NOT(ReloadFailed) THEN

	Send_Info('Saving RDS record ':RdsNo:'...')
	
	RDSRec<RDS_WAFERS_IN$>		= 0
	RDSRec<RDS_VERIFY_QTY$>		= 0
	RDSRec<RDS_CASS_WAFER_QTY$>	= 0

	RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
	obj_Tables('WriteRec',RTParms)
	
	Send_Info('Updating REACT_RUN record...')
	
	RDS_React_Run(RDSNo)		;* Conversion to REACT_RUN code 9/25/2008 JCH **********************************************
	
END ELSE
	obj_Tables('UnlockRec',RTParms)
END
	
RETURN



* * * * * * *
RemovePocketNCR:
* * * * * * *

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

IF RDSNo = '' THEN RETURN
IF NCRNo = '' THEN RETURN

IF RDSNo = ''	THEN ErrorMsg = 'Null parameter "RDSNo" passed to routine. (':Method:')'
IF NCRNo = ''	THEN ErrorMsg = 'Null parameter "NCRNo" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

Set_Status(0)
RTParms = 'RDS':@RM:RdsNo
RDSRec  = Database_Services('ReadDataRow', 'RDS', RDSNo)
* RDSRec  = obj_Tables('ReadRec',RTParms)

IF Get_Status(errCode) THEN
	ErrorMsg = 'Unable to read RDS ':QUOTE(RdsNo):' for LoadEpi update.'
	RETURN
END

OutNCRs = RDSRec<RDS_OUT_NCR$>

Done = 0
LOOP
	LOCATE NCRNo IN RDSRec<RDS_OUT_NCR$> USING @VM SETTING Pos THEN
		RDSRec<RDS_OUT_NCR$,Pos> = ''
	END ELSE
		Done = 1
	END
UNTIL Done
REPEAT

* RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
* obj_Tables('WriteRec',RTParms)
Database_Services('WriteDataRow', 'RDS', RDSNo, RDSRec, True$, False$, True$)

RETURN


* * * * * * *
MetConversion:
* * * * * * * 

* Updates entered data into RDS_LAYER and RDS_TEST records from RDS record


RETURN		;* Dead 4/16/2006 with installation of metrology and NCR updates



* * * * * * * 
LoadTestWafers:
* * * * * * *

TWLine = 0

RDSTestRec = ''

IF RDSRec<RDS_TW_NMINUS_CNT$> NE '' THEN
	TWType = 'N-'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_NMINUS_CODE$>,@VM) + (RDSRec<RDS_TW_NMINUS_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_NMINUS_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_NMINUS_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_NMINUS_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_NMINUS_CNT$,T>
		TWCon		= RDSRec<RDS_TW_NMINUS_CON$,T>
		TWRes		= RDSRec<RDS_TW_NMINUS_RES$,T>
		TWRho		= RDSRec<RDS_TW_NMINUS_RHO$,T>
		TWStress	= RDSRec<RDS_TW_NMINUS_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_NMINUS_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

IF RDSRec<RDS_TW_NPLUS_CNT$> NE '' THEN
	TWType = 'N+'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_NPLUS_CODE$>,@VM) + (RDSRec<RDS_TW_NPLUS_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_NPLUS_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_NPLUS_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_NPLUS_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_NPLUS_CNT$,T>
		TWCon		= RDSRec<RDS_TW_NPLUS_CON$,T>
		TWRes		= RDSRec<RDS_TW_NPLUS_RES$,T>
		TWRho		= RDSRec<RDS_TW_NPLUS_RHO$,T>
		TWStress	= RDSRec<RDS_TW_NPLUS_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_NPLUS_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

IF RDSRec<RDS_TW_PMINUS_CNT$> NE '' THEN
	TWType = 'P-'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_PMINUS_CODE$>,@VM) + (RDSRec<RDS_TW_PMINUS_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_PMINUS_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_PMINUS_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_PMINUS_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_PMINUS_CNT$,T>
		TWCon		= RDSRec<RDS_TW_PMINUS_CON$,T>
		TWRes		= RDSRec<RDS_TW_PMINUS_RES$,T>
		TWRho		= RDSRec<RDS_TW_PMINUS_RHO$,T>
		TWStress	= RDSRec<RDS_TW_PMINUS_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_PMINUS_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

IF RDSRec<RDS_TW_PPLUS_CNT$> NE '' THEN
	TWType = 'P+'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_PPLUS_CODE$>,@VM) + (RDSRec<RDS_TW_PPLUS_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_PPLUS_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_PPLUS_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_PPLUS_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_PPLUS_CNT$,T>
		TWCon		= RDSRec<RDS_TW_PPLUS_CON$,T>
		TWRes		= RDSRec<RDS_TW_PPLUS_RES$,T>
		TWRho		= RDSRec<RDS_TW_PPLUS_RHO$,T>
		TWStress	= RDSRec<RDS_TW_PPLUS_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_PPLUS_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

IF RDSRec<RDS_TW_PROD_CNT$> NE '' THEN
	TWType = 'Prod'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_PROD_CODE$>,@VM) + (RDSRec<RDS_TW_PROD_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_PROD_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_PROD_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_PROD_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_PROD_CNT$,T>
		TWCon		= RDSRec<RDS_TW_PROD_CON$,T>
		TWRes		= RDSRec<RDS_TW_PROD_RES$,T>
		TWRho		= RDSRec<RDS_TW_PROD_RHO$,T>
		TWStress	= RDSRec<RDS_TW_PROD_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_PROD_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

IF RDSRec<RDS_TW_RECLAIM_CNT$> NE '' THEN
	TWType = 'Reclaim'
	FOR T = 1 TO COUNT(RDSRec<RDS_TW_RECLAIM_CODE$>,@VM) + (RDSRec<RDS_TW_RECLAIM_CODE$> NE '')
		
		TWTool		= RDSRec<RDS_TW_RECLAIM_TOOL$,T>
		TWCode		= RDSRec<RDS_TW_RECLAIM_CODE$,T>
		TWTrans		= RDSRec<RDS_TW_RECLAIM_TRANS$,T>
		TWCnt		= RDSRec<RDS_TW_RECLAIM_CNT$,T>
		TWCon		= RDSRec<RDS_TW_RECLAIM_CON$,T>
		TWRes		= RDSRec<RDS_TW_RECLAIM_RES$,T>
		TWRho		= RDSRec<RDS_TW_RECLAIM_RHO$,T>
		TWStress	= RDSRec<RDS_TW_RECLAIM_STRESS$,T>
		TWThick		= RDSRec<RDS_TW_RECLAIM_THICK$,T>

		GOSUB LoadTWLine
		
	NEXT T
END

RETURN


* * * * * * *
LoadTWLine:
* * * * * * *

TWLine += 1

RDSTestRec<RDS_TEST_TW_TYPE$,TWLine>	= TWType
RDSTestRec<RDS_TEST_TW_TOOL$,TWLine>	= TWTool
RDSTestRec<RDS_TEST_TW_CODE$,TWLine>	= TWCode
RDSTestRec<RDS_TEST_TW_TRANS$,TWLine>	= TWTrans
RDSTestRec<RDS_TEST_TW_CNT$,TWLine>		= TWCnt
RDSTestRec<RDS_TEST_TW_CON$,TWLine>		= TWCon
RDSTestRec<RDS_TEST_TW_RES$,TWLine>		= TWRes
RDSTestRec<RDS_TEST_TW_RHO$,TWLine>		= TWRho
RDSTestRec<RDS_TEST_TW_STRESS$,TWLine>	= TWStress
RDSTestRec<RDS_TEST_TW_THICK$,TWLine>	= TWThick

RETURN


* * * * * * *
SetPartNo:
* * * * * * *

RdsNos  	= Parms[1,@RM]
NewPartNo	= Parms[COL2()+1,@RM]
RDSTableVar	= Parms[COL2()+1,@RM]

IF RDSNos = ''		THEN ErrorMsg = 'Null parameter "RDSNos" passed to routine. (':Method:')'
IF NewPartNo = ''	THEN ErrorMsg = 'Null parameter "NewPartNo" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

IF RDSTableVar = '' THEN

	rtParms = 'RDS':@RM:RdsNos

	RDSTableVar = obj_Tables('LockSet',rtParms)

	IF Get_Status(errCode) THEN RETURN
END

RdsCnt = COUNT(RdsNos,@VM) + (RdsNos NE '')

FOR I = 1 TO RdsCnt
	RdsNo = RdsNos<1,I>
	READ RDSRec FROM RDSTableVar,RdsNo THEN
	
		RDSRec<RDS_PART_NUM$> = NewPartNo

		rtParms = 'RDS':@RM:RDSNo:@RM:RDSTableVar:@RM:RDSRec
		obj_Tables('WriteRec',rtParms)							;* Writes and unlocks the RDS records					
		Send_Info(' RDS record ':RDSNo:' Updated. ':I:'/':RdsCnt)
	END
NEXT I

RETURN




