COMPILE FUNCTION obj_React_Tube(Method,Parms)

/*
	Methods for REACT_TUBE (chambers table
    
	10/31/2005 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    TubeChange(Reactor,Date,ReactLogID)		;* Creates new Line item for Tube Change
	ReactUnload(Reactor,WafersOut)			;* Bumps Wafer Count on current (top) line item by WafersOut
	
*/


DECLARE FUNCTION Get_Status, Utility, obj_Tables
DECLARE SUBROUTINE  Set_Status, ErrMsg, obj_Tables, Send_Dyn, obj_Tube

$INSERT REACTOR_EQUATES
$INSERT REACT_TUBE_EQUATES
$INSERT REACT_READS_EQUATES

ErrTitle = 'Error in Stored Procedure "obj_React_Tube"'
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 = 'TubeChange'		; GOSUB TubeChange
	CASE Method = 'ReactUnload'		; GOSUB ReactUnload
	CASE Method = 'ServiceHrs'		; GOSUB ServiceHrs
	CASE Method = 'ServiceWfrCnt'	; GOSUB ServiceWfrCnt
	CASE Method = 'ServicePCRC'		; GOSUB ServicePCRC
	
	CASE 1
		ErrorMsg = 'Unknown Method ':Method:' passed to object routine.'

END CASE

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

RETURN Result


* * * * * * *
TubeChange:
* * * * * * *

* This method is called from the WRITE event on the REACTOR_LOG window 10/1/2009 JCH

ReactorNo	= Parms[1,@RM]
ChangeDtm	= Parms[COL2()+1,@RM]
ReactLogID	= Parms[COL2()+1,@RM]
TubeID		= Parms[COL2()+1,@RM]
ReactHrs	= Parms[COL2()+1,@RM]
ReactWfrCnt = Parms[COL2()+1,@RM]

IF ReactorNo = ''	THEN ErrorMsg = 'Null Parameter "ReactorNo" passed to routine. (':Method:')'
IF ChangeDtm = ''	THEN ErrorMsg = 'Null Parameter "ChangeDtm" passed to routine. (':Method:')'
IF ReactLogID = ''	THEN ErrorMsg = 'Null Parameter "ReactLogID" passed to routine. (':Method:')'
IF TubeID = ''		THEN ErrorMsg = 'Null Parameter "TubeID" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

thisChangeDtm = ICONV(ChangeDtm,'DT')
IF thisChangeDtm = '' THEN
	ErrorMsg = 'Invalid value ':QUOTE(ChangeDtm):' passed in parameter "ChangeDtm". (':Method:')'
	RETURN
END

OtParms = 'REACT_TUBE':@RM:ReactorNo
ReactTubeRec = obj_Tables('ReadRec',OtParms)		;* Read and lock REACT_TUBE record.

IF Get_Status(errCode) THEN
	obj_Tables('UnlockRec',OtParms)
	RETURN
END

* Added LOCATE stuff on 11/23/2013 JCH

* Close out tube being removed line item

LOCATE ReactLogID IN ReactTubeRec<REACT_TUBE_REM_RL_ID$> USING @VM SETTING Pos THEN

	* Already posted -> update data
	
	ReactTubeRec<REACT_TUBE_REM_REACT_HRS$,Pos>		= ReactHrs							;* Update Hrs
	ReactTubeRec<REACT_TUBE_REM_REACT_WFR_CNT$,Pos>	= ReactWfrCnt						;* Update WfrCnt
	
	InstRLId 	= ReactTubeRec<REACT_TUBE_INST_RL_ID$,Pos>
	InstDTM		= ReactTubeRec<REACT_TUBE_INST_DTM$,Pos>
	
END ELSE

	* Close out the active line
	
	Pos = 1
	
	
	ReactTubeRec<REACT_TUBE_REM_DTM$,Pos>			= thisChangeDTM
	ReactTubeRec<REACT_TUBE_REM_RL_ID$,Pos>			= ReactLogID
	ReactTubeRec<REACT_TUBE_REM_REACT_HRS$,Pos>		= ReactHrs
	ReactTubeRec<REACT_TUBE_REM_REACT_WFR_CNT$,Pos>	= ReactWfrCnt

	InstRLId 	= ReactTubeRec<REACT_TUBE_INST_RL_ID$,Pos>
	InstDTM		= ReactTubeRec<REACT_TUBE_INST_DTM$,Pos>
END	



IF ReactTubeRec<REACT_TUBE_TUBE_ID$,1> NE ''  THEN
	RemTubeID = ReactTubeRec<REACT_TUBE_TUBE_ID$,Pos>
	
	IF RemTubeID NE '' THEN
	
		Parms   = RemTubeID:@RM
		Parms  := ReactorNo:@RM
		Parms  := ChangeDTM:@RM
		Parms  := ReactHrs:@RM
		Parms  := ReactWfrCnt:@RM
		Parms  := ReactLogID:@RM
		Parms  := ReactTubeRec<REACT_TUBE_RDS_WAFER_CNT$,Pos>:@RM
		Parms  := InstRLId:@RM
		Parms  := InstDTM		;* DateTime passed in internal format

		Set_Status(0)
		obj_Tube('Remove',Parms)		;* Update the existing tube record

		IF Get_Status(errCode) THEN
			ErrMsg(errCode)
			obj_Tables('UnlockRec',OtParms)
			RETURN
		END
	END
END





Parms  = TubeID:@RM
Parms := ReactorNo:@RM
Parms := ChangeDTM:@RM
Parms := ReactHrs:@RM
Parms := ReactWfrCnt:@RM
Parms := ReactLogID

Set_Status(0)

obj_Tube('Install',Parms)

IF Get_Status(errCode) THEN
	ErrMsg(errCode)
	
	obj_Tube('ClearRemove',TubeID:@RM:ReactorNo)	;* Back out the Tube Remove already done

	obj_Tables('UnlockRec',OtParms)
	RETURN
END

* Added LOCATE stuff on 11/23/2013 JCH

LOCATE ReactLogID IN ReactTubeRec<REACT_TUBE_INST_RL_ID$> USING @VM SETTING Pos THEN

	ReactTubeRec<REACT_TUBE_INST_REACT_HRS$,Pos>		= ReactHrs
	ReactTubeRec<REACT_TUBE_INST_REACT_WFR_CNT$,Pos>	= ReactWfrCnt
	
END ELSE

	* Insert new line item at top of list for new tube record

	ReactTubeRec = INSERT(ReactTubeRec,REACT_TUBE_INST_DTM$,1,0,thisChangeDtm)
	ReactTubeRec = INSERT(ReactTubeRec,REACT_TUBE_TUBE_ID$,1,0,TubeID)
	ReactTubeRec = INSERT(ReactTubeRec,REACT_TUBE_INST_RL_ID$,1,0,ReactLogID)
	ReactTubeRec = INSERT(ReactTubeRec,REACT_TUBE_RDS_WAFER_CNT$,1,0,0)					;* Set Wafer Count to 0

	ReactTubeRec = INSERT(ReactTubeRec,REACT_TUBE_INST_REACT_HRS$,1,0,ReactHrs)
	ReactTubeRec = INSERT(ReactTubeRec,REACT_TUBE_INST_REACT_WFR_CNT$,1,0,ReactWfrCnt)

	ReactTubeRec = INSERT(ReactTubeRec,REACT_TUBE_REM_DTM$,1,0,'')
	ReactTubeRec = INSERT(ReactTubeRec,REACT_TUBE_REM_RL_ID$,1,0,'')
	ReactTubeRec = INSERT(ReactTubeRec,REACT_TUBE_REM_REACT_HRS$,1,0,'')
	ReactTubeRec = INSERT(ReactTubeRec,REACT_TUBE_INST_REACT_WFR_CNT$,1,0,'')
END

OtParms = FieldStore(OtParms,@RM,4,0,ReactTubeRec)		;* Put record in 4th field of OtParms
obj_Tables('WriteRec',OtParms)

IF Get_Status(errCode) THEN
	obj_Tables('UnlockRec',OtParms)
END

RETURN



* * * * * * *
ReactUnload:
* * * * * * *

* This method is called from the Unload Signature Button Click event on the RDS window

ReactorNo	= Parms[1,@RM]
WaferQty	= Parms[COL2()+1,@RM]

IF ReactorNo = ''	THEN ErrorMsg = 'Null Parameter "ReactorNo" passed to routine. (':Method:')'
IF WaferQty = ''	THEN RETURN

IF ErrorMsg NE '' THEN RETURN

IF NOT(NUM(WaferQty)) THEN
	ErrorMsg = 'Non-Numeric data ':QUOTE(WafeQty):' passed in parameter "WaferQty". (':Method:')'
	RETURN
END

OtParms = 'REACT_TUBE':@RM:ReactorNo
ReactTubeRec = obj_Tables('ReadRec',OtParms)			;* Read and lock configuration record

IF Get_Status(errCode) THEN
	obj_Tables('UnlockRec',OtParms)
	RETURN
END

IF ReactTubeRec<REACT_TUBE_RDS_WAFER_CNT$,1> = '' THEN ReactTubeRec<REACT_TUBE_RDS_WAFER_CNT$,1> = 0	;* Just in case stuff happens

ReactTubeRec<REACT_TUBE_RDS_WAFER_CNT$,1> = ReactTubeRec<REACT_TUBE_RDS_WAFER_CNT$,1> + WaferQty

OtParms = FieldStore(OtParms,@RM,4,0,ReactTubeRec)		;* Put record in 4th field of OtParms
obj_Tables('WriteRec',OtParms)

IF Get_Status(errCode) THEN
	obj_Tables('UnlockRec',OtParms)
END

RETURN



* * * * * * *
ServiceHrs:
* * * * * * *

IF NOT(ASSIGNED(ReactNo))		THEN ReactNo	= Parms[1,@RM]
IF NOT(ASSIGNED(ReactTubeRec))	THEN ReactTubeRec	= Parms[COL2()+1,@RM]

IF ReactNo = '' THEN RETURN			;* This is used in the dictionary -> don't throw an error for a null parmeter

IF ReactTubeRec = '' THEN ReactTubeRec = XLATE('REACT_TUBE',ReactNo,'','X')

ReactNo = @ID
LastReadDTM = XLATE('REACTOR',ReactNo,25,'X')
LastReadHrs = XLATE('REACT_READS',ReactNo:'*':LastReadDTM,1,'X')

Ans = ''

IRCnt = COUNT(ReactTubeRec<REACT_TUBE_INST_REACT_HRS$>,@VM) + (ReactTubeRec<REACT_TUBE_INST_REACT_HRS$> NE '')

FOR I = 1 TO IRCnt

	RemReactHrs = ReactTubeRec<REACT_TUBE_REM_REACT_HRS$,I>
	InstReactHrs = ReactTubeRec<REACT_TUBE_INST_REACT_HRS$,I>
	
	IF I = 1 THEN
		RemReactHrs = LastReadHrs
	END
	
	IF RemReactHrs > InstReactHrs AND NUM(InstReactHrs) AND NUM(RemReactHrs) THEN
		Ans<1,I> = RemReactHrs - InstReactHrs
	END ELSE
		Ans<1,I> = ''
	END
NEXT I

Result = Ans

RETURN





* * * * * * *
ServiceWfrCnt:
* * * * * * *

IF NOT(ASSIGNED(ReactNo))		THEN ReactNo	= Parms[1,@RM]
IF NOT(ASSIGNED(ReactTubeRec))	THEN ReactTubeRec	= Parms[COL2()+1,@RM]

IF ReactNo = '' THEN RETURN			;* This is used in the dictionary -> don't throw an error for a null parmeter

IF ReactTubeRec = '' THEN ReactTubeRec = XLATE('REACT_TUBE',ReactNo,'','X')

ReactNo = @ID
LastReadDTM = XLATE('REACTOR',ReactNo,REACTOR_LAST_READ_WFRS_DTM$,'X')
LastReadWfrCnt = XLATE('REACT_READS',ReactNo:'*':LastReadDTM,REACT_READS_WAFER_CNT$,'X')

Ans = ''

IRCnt = COUNT(ReactTubeRec<REACT_TUBE_INST_REACT_WFR_CNT$>,@VM) + (ReactTubeRec<REACT_TUBE_INST_REACT_WFR_CNT$> NE '')


IF IRCnt = 0 THEN
	Ans = LastReadWfrCnt
END ELSE

	FOR I = 1 TO IRCnt

		RemReactWfrCnt = ReactTubeRec<REACT_TUBE_REM_REACT_WFR_CNT$,I>
		InstReactWfrCnt = ReactTubeRec<REACT_TUBE_INST_REACT_WFR_CNT$,I>
		
		IF I = 1 THEN
			RemReactWfrCnt = LastReadWfrCnt
		END
		
		IF RemReactWfrCnt > InstReactWfrCnt AND NUM(InstReactWfrCnt) AND NUM(RemReactWfrCnt) THEN
			Ans<1,I> = RemReactWfrCnt - InstReactWfrCnt
		END ELSE
			Ans<1,I> = ''
		END
		
	NEXT I
END

Result = Ans

RETURN Result






* * * * * * *
ServicePCRC:
* * * * * * *

IF NOT(ASSIGNED(ReactNo))		THEN ReactNo	= Parms[1,@RM]
IF NOT(ASSIGNED(ReactTubeRec))	THEN ReactTubeRec	= Parms[COL2()+1,@RM]

IF ReactNo = '' THEN RETURN			;* This is used in the dictionary -> don't throw an error for a null parmeter

IF ReactTubeRec = '' THEN ReactTubeRec = XLATE('REACT_TUBE',ReactNo,'','X')

ReactNo = @ID
LastReadDTM = XLATE('REACTOR',ReactNo,REACTOR_LAST_READ_WFRS_DTM$,'X')
LastReadWfrCnt = XLATE('REACT_READS',ReactNo:'*':LastReadDTM,REACT_READS_WAFER_CNT$,'X')

Ans = ''

IRCnt = COUNT(ReactTubeRec<REACT_TUBE_INST_REACT_WFR_CNT$>,@VM) + (ReactTubeRec<REACT_TUBE_INST_REACT_WFR_CNT$> NE '')

IF IRCnt = 0 THEN
	Ans = LastReadWfrCnt - ReactTubeRec<REACT_TUBE_RDS_WAFER_CNT$,1>
END ELSE
	FOR I = 1 TO IRCnt

		RemReactWfrCnt = ReactTubeRec<REACT_TUBE_REM_REACT_WFR_CNT$,I>
		InstReactWfrCnt = ReactTubeRec<REACT_TUBE_INST_REACT_WFR_CNT$,I>		;* This should always be set to zero
		
		IF I = 1 THEN
			RemReactWfrCnt = LastReadWfrCnt
		END
		
		IF RemReactWfrCnt > InstReactWfrCnt AND NUM(InstReactWfrCnt) AND NUM(RemReactWfrCnt) THEN
			Ans<1,I> = RemReactWfrCnt - InstReactWfrCnt - ReactTubeRec<REACT_TUBE_RDS_WAFER_CNT$,I>
		END ELSE
			Ans<1,I> = ''
		END
		
	NEXT I
END

Result = Ans

RETURN



