COMPILE FUNCTION obj_Tube(Method,Parms)

/*
	Methods for TUBE table
    
	09/30/2009 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Install(TubeID,ReactNo,DTM,ReactHrs,ReactWfrs,RL_Id)				;* Adds Reactor Installation line item to record
	Remove(TubeID,ReactNo,DTM,ReactHrs,ReactWfrs,RL_Id,RDSWfrCnt)		;* Adds Removal information to Reactor Installation line item
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, Send_Dyn, obj_Calendar
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, Send_Dyn
DECLARE SUBROUTINE ErrMsg

$INSERT MSG_EQUATES
$INSERT TUBE_EQUATES
$INSERT REACTOR_EQUATES
$INSERT REACT_READS_EQUATES



EQU CRLF$	TO \0D0A\
EQU TAB$	TO CHAR(9)


ErrTitle = 'Error in Stored Procedure "obj_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 = 'Install'			; GOSUB Install
	CASE Method = 'Remove'			; GOSUB Remove
	CASE Method = 'ClearRemove'		; GOSUB ClearRemove
	CASE Method = 'CurrStatus'		; GOSUB CurrStatus
	CASE Method = 'ServiceHrs'		; GOSUB ServiceHrs
	CASE Method = 'ServiceWfrs'		; GOSUB ServiceWfrs
	CASE Method = 'ServicePCRC'		; GOSUB ServicePCRC
		
	CASE 1
		ErrorMsg = 'Unknown Method ':QUOTE(Method):' passed to routine.'

END CASE

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

RETURN Result


* * * * * * *
Install:
* * * * * * *

TubeID		= Parms[1,@RM]
ReactNo		= Parms[COL2()+1,@RM]
ActionDTM	= Parms[COL2()+1,@RM]
ReactHrs 	= Parms[COL2()+1,@RM]
ReactWfrs	= Parms[COL2()+1,@RM]
RL_Id		= Parms[COL2()+1,@RM]

IF TubeID = ''			THEN ErrorMsg = 'Null Parameter "Tube ID" passed to routine. (':Method:')'
IF ReactNo = ''			THEN ErrorMsg = 'Null Parameter "ReactNo" passed to routine. (':Method:')'
IF ActionDTM = ''		THEN ErrorMsg = 'Null Parameter "ActionDTM" passed to routine. (':Method:')'
*IF ReactHrs = ''		THEN ErrorMsg = 'Null Parameter "ReactHrs" passed to routine. (':Method:')'
*IF ReactWfrs = ''		THEN ErrorMsg = 'Null Parameter "ReactWfrs" passed to routine. (':Method:')'
IF RL_Id = ''			THEN ErrorMsg = 'Null Parameter "RL_Id" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

thisActionDTM = ICONV(ActionDTM,'DT')

IF thisActionDTM = '' THEN
	ErrorMsg = 'Invalid parameter ActionDTM ':QUOTE(ActionDTM):' passed to routine. (':Method:')'
	RETURN
END

otParms = 'TUBE':@RM:TubeID

TubeRec = obj_Tables('ReadRec',otParms)

IF Get_Status(errCode) THEN
	Set_Status(0)
	TubeRec = ''
	TubeRec<TUBE_ENTER_BY$>		= 'System'
	TubeRec<TUBE_ENTER_DTM$>	= ICONV(obj_Calendar('CurrDTM'),'DT')
END


/*
IF TubeRec<TUBE_REACT_NO$,1> = ReactNo THEN
	ErrorMsg = 'Tube ID ':QUOTE(TubeID):' is already in service in reactor ':ReactNo:'.'
	obj_Tables('UnlockRec',otParms)
	RETURN
END
*/

* Locate stuff added 11/23/2013 JCH

LOCATE RL_Id IN TubeRec<TUBE_INST_RL_ID$> USING @VM SETTING Pos THEN
	TubeRec<TUBE_INST_REACT_HRS$,Pos> = ReactHrs
	TubeRec<TUBE_INST_REACT_WFRS$,Pos> = ReactWfrs
END ELSE
	LOCATE thisActionDTM IN TubeRec<TUBE_INST_DTM$> BY 'DR' USING @VM SETTING Pos ELSE
		TubeRec = INSERT(TubeRec,TUBE_REACT_NO$,Pos,0,ReactNo)
		TubeRec = INSERT(TubeRec,TUBE_INST_DTM$,Pos,0,thisActionDTM)
		TubeRec = INSERT(TubeRec,TUBE_INST_REACT_HRS$,Pos,0,ReactHrs)
		TubeRec = INSERT(TubeRec,TUBE_INST_REACT_WFRS$,Pos,0,ReactWfrs)
		TubeRec = INSERT(TubeRec,TUBE_INST_RL_ID$,Pos,0,RL_Id)
		TubeRec = INSERT(TubeRec,TUBE_RDS_WFR_CNT$,Pos,0,'')
		TubeRec = INSERT(TubeRec,TUBE_REM_DTM$,Pos,0,'')
		TubeRec = INSERT(TubeRec,TUBE_REM_REACT_HRS$,Pos,0,'')
		TubeRec = INSERT(TubeRec,TUBE_REM_REACT_WFRS$,Pos,0,'')
		TubeRec = INSERT(TubeRec,TUBE_REM_RL_ID$,Pos,0,'')
	END
END


otParms = FIELDSTORE(OtParms,@RM,4,0,TubeRec)

obj_Tables('WriteRec',otParms)

RETURN


* * * * * * *
Remove:
* * * * * * *

TubeID		= Parms[1,@RM]
ReactNo		= Parms[COL2()+1,@RM]
ActionDTM	= Parms[COL2()+1,@RM]
ReactHrs 	= Parms[COL2()+1,@RM]
ReactWfrs	= Parms[COL2()+1,@RM]
RL_Id		= Parms[COL2()+1,@RM]
RDSWfrCnt	= Parms[COL2()+1,@RM]
InstRLId	= Parms[COL2()+1,@RM]
InstDTM		= Parms[COL2()+1,@RM]	;* This is passed in internal format


IF TubeID = ''			THEN ErrorMsg = 'Null Parameter "Tube ID" passed to routine. (':Method:')'
IF ReactNo = ''			THEN ErrorMsg = 'Null Parameter "ReactNo" passed to routine. (':Method:')'
IF ActionDTM = ''		THEN ErrorMsg = 'Null Parameter "ActionDTM" passed to routine. (':Method:')'
IF RL_Id = ''			THEN ErrorMsg = 'Null Parameter "RL_Id" passed to routine. (':Method:')'

*IF ReactHrs = ''		THEN ErrorMsg = 'Null Parameter "ReactHrs" passed to routine. (':Method:')'
*IF ReactWfrs = ''		THEN ErrorMsg = 'Null Parameter "ReactWfrs" passed to routine. (':Method:')'
*IF RDSWfrCnt = ''		THEN ErrorMsg = 'Null Parameter "RDSWfrCnt" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

thisActionDTM = ICONV(ActionDTM,'DT')

IF thisActionDTM = '' THEN
	ErrorMsg = 'Invalid parameter ActionDTM ':QUOTE(ActionDTM):' passed to routine. (':Method:')'
	RETURN
END

otParms = 'TUBE':@RM:TubeID

TubeRec = obj_Tables('ReadRec',otParms)




IF Get_Status(errCode) THEN
	Set_Status(0)
	TubeRec = ''
	TubeRec<TUBE_ENTER_BY$>		= 'System'
	TubeRec<TUBE_ENTER_DTM$>	= ICONV(obj_Calendar('CurrDTM'),'DT')
END

/*
IF TubeRec<TUBE_REACT_NO$,1> NE ReactNo THEN
	
	TubeRec = INSERT(TubeRec,TUBE_REACT_NO$,1,0,ReactNo)
	TubeRec = INSERT(TubeRec,TUBE_INST_DTM$,1,0,'')
	TubeRec = INSERT(TubeRec,TUBE_INST_REACT_HRS$,1,0,'')
	TubeRec = INSERT(TubeRec,TUBE_INST_REACT_WFRS$,1,0,'')
	TubeRec = INSERT(TubeRec,TUBE_INST_RL_ID$,1,0,'')
	TubeRec = INSERT(TubeRec,TUBE_RDS_WFR_CNT$,1,0,RDSWfrCnt)
	TubeRec = INSERT(TubeRec,TUBE_REM_DTM$,1,0,thisActionDTM)
	TubeRec = INSERT(TubeRec,TUBE_REM_REACT_HRS$,1,0,ReactHrs)
	TubeRec = INSERT(TubeRec,TUBE_REM_REACT_WFRS$,1,0,ReactWfrs)
	TubeRec = INSERT(TubeRec,TUBE_REM_RL_ID$,1,0,RL_Id)
	
	otParms = FIELDSTORE(OtParms,@RM,4,0,TubeRec)

	obj_Tables('WriteRec',otParms)

	*ErrorMsg = 'Tube ID ':QUOTE(TubeID):' is not in service in reactor ':ReactNo:'.'
	*obj_Tables('UnlockRec',otParms)
	RETURN
END
*/



LOCATE InstRLId IN TubeRec<TUBE_INST_RL_ID$> BY 'DR' USING @VM SETTING Pos THEN

	TubeRec<TUBE_RDS_WFR_CNT$,Pos>		= RDSWfrCnt
	TubeRec<TUBE_REM_DTM$,Pos>			= thisActionDTM
	TubeRec<TUBE_REM_REACT_HRS$,Pos>	= ReactHrs
	TubeRec<TUBE_REM_REACT_WFRS$,Pos>	= ReactWfrs
	TubeRec<TUBE_REM_RL_ID$,Pos>		= RL_Id
END


otParms = FIELDSTORE(OtParms,@RM,4,0,TubeRec)

obj_Tables('WriteRec',otParms)

RETURN



* * * * * * *
ClearRemove:
* * * * * * *

TubeID		= Parms[1,@RM]
ReactNo		= Parms[COL2()+1,@RM]

IF TubeID = ''			THEN ErrorMsg = 'Null Parameter "Tube ID" passed to routine. (':Method:')'
IF ReactNo = ''			THEN ErrorMsg = 'Null Parameter "ReactNo" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

otParms = 'TUBE':@RM:TubeID

TubeRec = obj_Tables('ReadRec',otParms)

IF TubeRec<TUBE_REACT_NO$,1> = ReactNo THEN
	TubeRec<TUBE_RDS_WFR_CNT$,1>		= ''
	TubeRec<TUBE_REM_DTM$,1>			= ''
	TubeRec<TUBE_REM_REACT_HRS$,1>		= ''
	TubeRec<TUBE_REM_REACT_WFRS$,1>	= ''
	TubeRec<TUBE_REM_RL_ID$,1>			= ''
END

otParms = FIELDSTORE(OtParms,@RM,4,0,TubeRec)

obj_Tables('WriteRec',otParms)

RETURN


* * * * * * *
CurrStatus:
* * * * * * *

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

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

IF TubeRec = '' THEN TubeRec = XLATE('GRAPHITE',TubeNo,'','X')

InstDTM = TubeRec<TUBE_INST_DTM$,1>
RemDTM	= TubeRec<TUBE_REM_DTM$,1>

RetireDt = TubeRec<TUBE_RETIRE_DT$>

BEGIN CASE
	CASE RetireDt NE ''						; Result = 'R'
	CASE InstDTM NE '' AND RemDTM = ''		; Result = 'I'
	CASE InstDTM NE '' AND RemDTM NE ''		; Result = 'U'
	CASE InstDTM = '' AND RemDTM = ''		; Result = 'N'
	CASE 1									; Result = '?'
END CASE
		
RETURN


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

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

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

IF TubeRec = '' THEN TubeRec = XLATE('TUBE',TubeID,'','X')

LastRemDtm = TubeRec<TUBE_REM_DTM$,1>

IF LastRemDTM = '' THEN 
	ReactNo = TubeRec<TUBE_REACT_NO$,1>
	LastReadDTM = XLATE('REACTOR',ReactNo,REACTOR_LAST_READ_HRS_DTM$,'X')
	LastReadHrs = XLATE('REACT_READS',ReactNo:'*':LastReadDTM,REACT_READS_HOURS$,'X')
END ELSE
	LastReadHrs = ''
END

Ans = ''

IRCnt = COUNT(TubeRec<TUBE_INST_REACT_HRS$>,@VM) + (TubeRec<TUBE_INST_REACT_HRS$> NE '')

FOR I = 1 TO IRCnt

	RemReactHrs = TubeRec<TUBE_REM_REACT_HRS$,I>
	InstReactHrs = TubeRec<TUBE_INST_REACT_HRS$,I>
	
	IF I = 1 AND LastReadHrs NE '' THEN
		ReactNo = TubeRec<TUBE_REACT_NO$,I>

		LastReadDTM = XLATE('REACTOR',ReactNo,25,'X')
		LastReadHrs = XLATE('REACT_READS',ReactNo:'*':LastReadDTM,1,'X')
	
		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


* * * * * * *
ServiceWfrs:
* * * * * * *

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

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

IF TubeRec = '' THEN TubeRec = XLATE('TUBE',TubeID,'','X')

LastRemDtm = TubeRec<TUBE_REM_DTM$,1>

IF LastRemDTM = '' THEN 
	ReactNo = TubeRec<TUBE_REACT_NO$,1>
	LastReadDTM = XLATE('REACTOR',ReactNo,REACTOR_LAST_READ_WFRS_DTM$,'X')
	LastReadWfrs = XLATE('REACT_READS',ReactNo:'*':LastReadDTM,REACT_READS_WAFER_CNT$,'X')
END ELSE
	LastReadWfrs = ''
END

Ans = ''

IRCnt = COUNT(TubeRec<TUBE_INST_REACT_WFRS$>,@VM) + (TubeRec<TUBE_INST_REACT_WFRS$> NE '')


IF IRCnt = 0 THEN
	Ans = LastReadWfrs
END ELSE

	FOR I = 1 TO IRCnt

		RemReactWfrs = TubeRec<TUBE_REM_REACT_WFRS$,I>
		InstReactWfrs = TubeRec<TUBE_INST_REACT_WFRS$,I>
		
		IF I = 1 AND LastReadWfrs NE '' THEN
			RemReactWfrs = LastReadWfrs
		END
		
		IF RemReactWfrs > InstReactWfrs AND NUM(InstReactWfrs) AND NUM(RemReactWfrs) THEN
			Ans<1,I> = RemReactWfrs - InstReactWfrs
		END ELSE
			Ans<1,I> = ''
		END
		
	NEXT I
END

Result = Ans

RETURN Result



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

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

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

IF TubeRec = '' THEN TubeRec = XLATE('TUBE',TubeID,'','X')

LastRemDtm = TubeRec<TUBE_REM_DTM$,1>

IF LastRemDTM = '' THEN 
	ReactNo 	= TubeRec<TUBE_REACT_NO$,1>
	LastReadDTM = XLATE('REACTOR',ReactNo,REACTOR_LAST_READ_WFRS_DTM$,'X')
	LastReadWfrs = XLATE('REACT_READS',ReactNo:'*':LastReadDTM,REACT_READS_WAFER_CNT$,'X')
END ELSE
	LastReadWfrs = ''
END

Ans = ''


IRCnt = COUNT(TubeRec<TUBE_INST_REACT_WFRS$>,@VM) + (TubeRec<TUBE_INST_REACT_WFRS$> NE '')

IF IRCnt = 0 THEN
	Ans = LastReadWfrs
END ELSE
	FOR I = 1 TO IRCnt

		RemReactWfrs = TubeRec<TUBE_REM_REACT_WFRS$,I>
		InstReactWfrs = TubeRec<TUBE_INST_REACT_WFRS$,I>		;* This should always be set to zero
		
		IF I = 1 AND LastReadWfrs NE ''THEN
			RemReactWfrs = LastReadWfrs
		END
		
		IF RemReactWfrs > InstReactWfrs AND NUM(InstReactWfrs) AND NUM(RemReactWfrs) THEN
			Ans<1,I> = RemReactWfrs - InstReactWfrs - TubeRec<TUBE_RDS_WFR_CNT$,I>
		END ELSE
			Ans<1,I> = ''
		END
		
	NEXT I
END

Result = Ans

RETURN





