COMPILE FUNCTION obj_React_Event(Method,Parms)
/*
	Methods for REACT_EVENT table
    
	01/10/2007 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Find()							;* Lookup Reactor Event key
   
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, Dialog_Box
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, Send_Dyn, Btree.Extract, ErrMsg
DECLARE SUBROUTINE  obj_Notes, Send_Info

$INSERT MSG_EQUATES
$INSERT REACT_EVENT_EQUATES
$INSERT REACT_MODE_EQUATES
$INSERT REACT_ESC_EQUATES
$INSERT REACTOR_LOG_EQUATES
$INSERT REACTOR_EQUATES
$INSERT REACT_READS_EQUATES


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

ErrTitle = 'Error in Stored Procedure "obj_React_Event"'
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 = 'Create'			; GOSUB Create
	CASE Method = 'SetStopDTM'		; GOSUB SetStopDTM
	CASE Method = 'EventTypeNote'	; GOSUB EventTypeNote
	
	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



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

ReactEventKeys = Dialog_Box( 'DIALOG_REACT_EVENT_QUERY', @WINDOW, '' )

Result = ReactEventKeys


RETURN


* * * * * * *
Create:
* * * * * * *

ReactNo		= Parms[1,@RM]
EventDTM	= Parms[COL2()+1,@RM]
EventType	= Parms[COL2()+1,@RM]
LoggedBy	= Parms[COL2()+1,@RM]
Comment		= Parms[COL2()+1,@RM]
NewMode		= Parms[COL2()+1,@RM]
EventMode	= Parms[COL2()+1,@RM]
	
IF ReactNo = ''		THEN ErrorMsg = 'Null parameter "ReactNo" passed to routine. (':Method:')'
IF EventDTM = ''	THEN ErrorMsg = 'Null parameter "EventDTM" passed to routine. (':Method:')'
IF EventType = ''	THEN ErrorMsg = 'Null parameter "EventType" passed to routine. (':Method:')'

IF LoggedBy = '' THEN LoggedBy = @USER4

thisEventDTM = ICONV(EventDTM,'DT')

IF thisEventDTM = '' THEN
	ErrorMsg = 'Ivalid Data ':QUOTE(EventDTM):' passed in "EventDTM" parameter. (':Method:')'
END

IF ErrorMsg NE '' THEN RETURN

ReactEventKey = ReactNo:'*':thisEventDTM

ReactEventRec = XLATE('REACT_EVENT',ReactEventKey,'','X')

EscHrs		= XLATE('REACTOR',ReactNo,'ACT_ESC_HRS','X')
WONo		= XLATE('CONFIG','WO_DAILY_SCHED':ReactNo,2,'X')
LastRdDTM	= XLATE('REACTOR',ReactNo,REACTOR_LAST_READ_WFRS_DTM$,'X')
LastWfrRead = XLATE('REACT_READS',ReactNo:'*':LastRdDTM,REACT_READS_WAFER_CNT$,'X')


WONo = WONo[1,' ']

IF NewMode NE '' THEN
	ReactMode = NewMode		;* Mode Change
END ELSE
	ReactMode = EventMode	;* Current Mode
END

ReactEventRec<REACT_EVENT_LOGGED_BY$>			= LoggedBy
ReactEventRec<REACT_EVENT_EVENT_CD$>			= EventType
ReactEventRec<REACT_EVENT_REACT_MODE$>			= ReactMode
ReactEventRec<REACT_EVENT_WO_NO$>				= WONo
ReactEventRec<REACT_EVENT_REACT_ESC_HRS$>		= EscHrs
ReactEventRec<REACT_EVENT_COMMENT$>				= Comment
ReactEventRec<REACT_EVENT_AUTO_LOG$>			= 1
ReactEventRec<REACT_EVENT_LAST_READ_WFRS_DTM$>	= LastRdDTM
ReactEventRec<REACT_EVENT_LAST_READ_WFRS_VAL$>	= LastWfrRead


OtParms = 'REACT_EVENT':@RM:ReactEventKey:@RM:@RM:ReactEventRec
obj_Tables('WriteRec',OtParms)


RETURN

* * * * * * *
SetStopDTM:
* * * * * * *

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



IF ReactNo = ''		THEN ErrorMsg = 'Null parameter "ReactNo" passed to routine. (':Method:')'
IF EventDTM = ''	THEN ErrorMsg = 'Null parameter "EventDTM" passed to routine. (':Method:')'
IF StopDTM = ''		THEN ErrorMsg = 'Null parameter "StopDTM" passed to routine. (':Method:')'


thisEventDTM = ICONV(EventDTM,'DT')

IF thisEventDTM = '' THEN
	ErrorMsg = 'Ivalid Data ':QUOTE(EventDTM):' passed in "EventDTM" parameter. (':Method:')'
END

thisStopDTM = ICONV(StopDTM,'DT')

IF thisStopDTM = '' THEN
	ErrorMsg = 'Ivalid Data ':QUOTE(StopDTM):' passed in "StopDTM" parameter. (':Method:')'
END

IF ErrorMsg NE '' THEN RETURN

ReactEventKey = ReactNo:'*':thisEventDTM

OtParms = 'REACT_EVENT':@RM:ReactEventKey
ReactEventRec = obj_Tables('ReadRec',OtParms)			;* Locks and reads record for update

IF Get_Status(errCode) THEN DEBUG

ReactEventRec<REACT_EVENT_STOP_DTM$> = thisStopDTM

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


RETURN


* * * * * * *
EventTypeNote:
* * * * * * *

ReactEventKey = Parms[1,@RM]
ReactEventRec = Parms[COL2()+1,@RM]

IF ReactEventKey = '' THEN RETURN

IF ReactEventRec = '' THEN
	ReactEventRec = XLATE('REACT_EVENT',ReactEventKey,'','X')
END


IF ReactEventRec = '' THEN RETURN

Types = ''
TypeUsers = ''
TypeNotes = ''

IF ReactEventRec<REACT_EVENT_EVENT_CD$> NE '' THEN
	Types 		= ReactEventRec<REACT_EVENT_EVENT_CD$>
	TypeUsers	= OCONV(ReactEventRec<REACT_EVENT_LOGGED_BY$>,'[XLATE_CONV,LSL_USERS*FIRST_LAST]')
	TypeNotes	= ReactEventRec<REACT_EVENT_COMMENT$>
	
	SWAP CRLF$ WITH @TM IN TypeNotes

	Result = Types:@FM:TypeNotes:@FM:TypeUsers

	RETURN
END


IF ReactEventRec<REACT_EVENT_REACT_MODE_KEY$>	NE '' THEN
	
	ModeKey		= ReactEventRec<REACT_EVENT_REACT_MODE_KEY$>
    ModeRec		= XLATE('REACT_MODE',ModeKey,'','X')
    Mode		= OCONV(ModeRec<REACT_MODE_MODE$ >,'[REACT_MODE_CONV]')
    StartNote	= ModeRec<REACT_MODE_START_NOTE$>
    
    StartUser	= ModeRec<REACT_MODE_START_USER$>
    StartUser	= OCONV(StartUser,'[XLATE_CONV,LSL_USERS*FIRST_LAST]')
    
    Note = 'Mode: ':Mode:@TM:OCONV(ModeRec<REACT_MODE_START_NOTE$>,'T#100')
	
	IF Types = '' THEN
		Types		= 'MODE'
		TypeUsers	= StartUser
		TypeNotes 	= Note
		
	END ELSE
		Types 		= 'COMB'
		TypeUsers	= StartUser
		TypeNotes  := @TM:STR('- ',20):@TM:Note
	END
			
END

IF ReactEventRec<REACT_EVENT_REACT_LOG_KEY$>	NE '' THEN
	LogKey	= ReactEventRec<REACT_EVENT_REACT_LOG_KEY$>
	
	LogUser = XLATE('REACTOR_LOG',LogKey,REACTOR_LOG_ENTRY_ID$,'X')
	LogUser = OCONV(LogUser,'[XLATE_CONV,LSL_USERS*FIRST_LAST]')
	
    Note	= XLATE('REACTOR_LOG',LogKey,REACTOR_LOG_NOTES$,'X')
    
    * Data coming in with CRLF$ delimiters, T# formatting doesn't deal well with this.
	* Divide into Items and text format them individually and reassemble it
	
	SWAP CRLF$ WITH @VM IN Note
	FormattedNote = ''
	ItemCnt = COUNT(Note,@VM) + (Note NE '')
	
	FOR N = 1 TO ItemCnt
		Item = Note<1,N>
		FormattedNote<1,N> = OCONV(Item,'T#100')
	NEXT N
	
  	CONVERT @VM TO @TM IN FormattedNote
  	Note = FormattedNote
    
    
    IF Types = '' THEN
		Types		= 'MTC'
		TypeUsers	= LogUser
		TypeNotes 	= Note
	END ELSE
		Types 		= 'COMB'
		TypeUsers	= LogUser
		TypeNotes  := @TM:STR('- ',20):@TM:Note
	END
END


IF ReactEventRec<REACT_EVENT_REACT_LOG_PD_KEY$>	NE '' THEN
	LogKey	= ReactEventRec<REACT_EVENT_REACT_LOG_PD_KEY$>
	
	LogUser = XLATE('REACTOR_LOG',LogKey,REACTOR_LOG_ENTRY_ID$,'X')
	LogUser = OCONV(LogUser,'[XLATE_CONV,LSL_USERS*FIRST_LAST]')
	
	Note = XLATE('REACTOR_LOG',LogKey,REACTOR_LOG_NOTES$,'X')
	
	* Data coming in with CRLF$ delimiters, T# formatting doesn't deal well with this.
	* Divide into Items and text format them individually and reassemble it
	
	SWAP CRLF$ WITH @VM IN Note
	FormattedNote = ''
	ItemCnt = COUNT(Note,@VM) + (Note NE '')
	
	FOR N = 1 TO ItemCnt
		Item = Note<1,N>
		FormattedNote<1,N> = OCONV(Item,'T#100')
	NEXT N
	
  	CONVERT @VM TO @TM IN FormattedNote
  	Note = FormattedNote
	
    
    IF Types = '' THEN
		Types		= 'PD'
		TypeUsers	= LogUser
		TypeNotes 	= Note
	END ELSE
		Types 		= 'COMB'
		TypeUsers	= LogUser
		TypeNotes  := @TM:STR('- ',20):@TM:Note
	END
END


IF ReactEventRec<REACT_EVENT_REACT_ESC_START_KEY$>	NE '' THEN
	EscKey	= ReactEventRec<REACT_EVENT_REACT_ESC_START_KEY$>
	
	EscUser = XLATE('REACT_ESC',EscKey,REACT_ESC_START_USER$,'X')
	EscUser = OCONV(EscUser,'[XLATE_CONV,LSL_USERS*FIRST_LAST]')
	
    Note	= XLATE('REACT_ESC',EscKey,REACT_ESC_START_REASON$,'X')
    
    SWAP CRLF$ WITH @VM IN Note
	FormattedNote = ''
	ItemCnt = COUNT(Note,@VM) + (Note NE '')
	
	FOR N = 1 TO ItemCnt
		Item = Note<1,N>
		FormattedNote<1,N> = OCONV(Item,'T#100')
	NEXT N
	
  	CONVERT @VM TO @TM IN FormattedNote
  	Note = FormattedNote
    
    IF Types = '' THEN
		Types		= 'ESTR'
		TypeUsers	= EscUser
		TypeNotes 	= Note
	END ELSE
		Types 		= 'COMB'
		TypeUsers	= EscUser
		TypeNotes  := @TM:STR('- ',20):@TM:Note
	END
END


IF ReactEventRec<REACT_EVENT_REACT_ESC_STOP_KEY$>	NE '' THEN
	EscKey	= ReactEventRec<REACT_EVENT_REACT_ESC_STOP_KEY$>
	
	EscUser = XLATE('REACT_ESC',EscKey,REACT_ESC_STOP_USER$,'X')
	EscUser = OCONV(EscUser,'[XLATE_CONV,LSL_USERS*FIRST_LAST]')
	
    Note	= XLATE('REACT_ESC',EscKey,REACT_ESC_STOP_REASON$,'X')
    
    SWAP CRLF$ WITH @VM IN Note
	FormattedNote = ''
	ItemCnt = COUNT(Note,@VM) + (Note NE '')
	
	FOR N = 1 TO ItemCnt
		Item = Note<1,N>
		FormattedNote<1,N> = OCONV(Item,'T#100')
	NEXT N
	
  	CONVERT @VM TO @TM IN FormattedNote
  	Note = FormattedNote
    
    IF Types = '' THEN
		Types		= 'ESTP'
		TypeUsers	= EscUser
		TypeNotes 	= Note
	END ELSE
		Types 		= 'COMB'
		TypeUsers	= EscUser
		TypeNotes  := @TM:STR('- ',20):@TM:Note
	END
END

Result = Types:@FM:TypeNotes:@FM:TypeUsers

RETURN


