COMPILE FUNCTION Run_Stage(EntID,Event,Parm1,Parm2,Parm3,Parm4,Parm5)

/*
	Commuter module for Run_Stage window
	
	05/18/2016 - John C. Henry, J.C. Henry & Co., Inc.
*/

DECLARE SUBROUTINE Set_Property, End_Dialog, Set_Status,obj_WO_Wfr, obj_WO_Wfr_Wip, Post_Event
DECLARE SUBROUTINE ErrMsg, Send_Message, Set_Property, Send_Event, obj_AppWindow, Forward_Event, Send_Event
DECLARE SUBROUTINE End_Window,obj_React_Reads, obj_WO_Mat_Log, obj_WO_Mat, obj_Appwindow,obj_Run_Stage, obj_Post_Log

DECLARE FUNCTION Get_Property, Get_Status, Dialog_Box, Utility, Popup, Collect.Ixvals, obj_WO_Mat_QA, obj_Appwindow, obj_WO_Wfr, MemberOf
DECLARE FUNCTION Send_Message, Msg, Security_Check, obj_WO_Log, WO_Purge, Set_Property, Create_Dialog, obj_WO_Wfr_Wip, obj_React_Run

Declare function    Logging_Services, Environment_Services
Declare subroutine  Logging_Services

$INSERT REACT_RUN_EQUATES
$INSERT RUN_STAGE_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT WO_MAT_WFR_EQUATES
$INSERT WO_WFR_EQUATES
$INSERT PRS_STAGE_EQUATES
$INSERT LSL_USERS_EQUATES
$INSERT POPUP_EQUATES
$INSERT LOGICAL
$INSERT TOOL_EQUATES
$INSERT TOOL_CLASS_EQUATES
$INSERT RTI_STYLE_EQUATES

$INSERT MSG_EQUATES
$INSERT APPCOLORS

EQU COL$IA_ACTION	TO 1
EQU COL$IA_COMP_BY	TO 2
EQU COL$IA_COMP_DTM TO 3



EQU COL$POCKET		TO 1
EQU COL$USAGE		TO 2
EQU COL$ZONE		TO 3
EQU COL$PKT_SAT_ID	TO 4
EQU COL$WFR_TYPE	TO 5
EQU COL$IN_SLOT_ID	TO 6
EQU COL$WFR_ID		TO 7
EQU COL$OUT_SLOT_ID	TO 8


EQU COL$MET_TEST		TO 1		;* QA Metrology control
EQU COL$MET_TEST_DESC	TO 2
EQU COL$MET_SLOT		TO 3
EQU COL$MET_SLOT_DESC	TO 4
EQU COL$MET_WFR_QTY		TO 5
EQU COL$MET_MIN			TO 6
EQU COL$MET_MAX			TO 7
EQU COL$MET_RESULT		TO 8
EQU COL$MET_STD_MAX		TO 9
EQU COL$MET_STD_RESULT	TO 10
EQU COL$MET_SIG			TO 11
EQU COL$MET_SIG_DTM		TO 12

	
EQU COL$ACTION				TO 1	;* CLEAN_INSP control
EQU COL$SPEC_CLEAN_TOOL		TO 2
EQU COL$SPEC_MICROSCOPE		TO 3
EQU COL$SPEC_BRIGHTLIGHT	TO 4
EQU COL$SURFSCAN_RECIPE		TO 5
EQU COL$TOOL				TO 6
EQU COL$SIG					TO 7
EQU COL$SIG_DTM				TO 8


EQU COL$CARRIER_SLOT	    TO 1	;* CARRIER control
EQU COL$CARRIER_WFR_ID      TO 2
EQU COL$CARRIER_SCRIBE		TO 3
EQU COL$CARRIER_EDIT_SCRIBE	TO 4


EQU CRLF$	TO \0D0A\
EQU TAB$	TO \09\


ErrCode	 = ''
ErrorMsg = ''
ErrTitle = 'Error in Run_Stage commuter module'

Result = ''


BEGIN CASE

	CASE EntID = @WINDOW
		BEGIN CASE
			CASE Event = 'CREATE' 			; GOSUB Create
			CASE Event = 'CLEAR'			; GOSUB Clear
			CASE Event = 'CLOSE'			; GOSUB Close
			CASE Event = 'DELETE'			; GOSUB Delete
			CASE Event = 'READ'				; GOSUB Read
			CASE Event = 'WRITE'			; GOSUB Write
			CASE Event = 'VSCROLL'			; GOSUB VScroll
			CASE Event[1,3] = 'QBF'			; GOSUB Refresh
		END CASE
		
	CASE EntID = @WINDOW:'.TABCONTROL'		AND Event = 'CLICK'		; GOSUB Page
	CASE EntID = @WINDOW:'.SIGN_COMP'		AND Event = 'CLICK'		; GOSUB SignComp
	CASE EntID = @WINDOW:'.LU_TOOL_ID'		AND Event = 'CLICK'		; GOSUB LUToolID
	CASE EntID = @WINDOW:'.INV_ACTION'		AND Event = 'OPTIONS'	; GOSUB PerformIA
	CASE EntID = @WINDOW:'.SKIP_STAGE'		AND Event = 'CLICK'		; GOSUB SkipStage
	/*
	CASE EntID = @WINDOW:'.LU_WO' 			AND Event = 'CLICK'	; GOSUB LUWONo
	CASE EntID = @WINDOW:'.START_PURGE'		AND Event = 'CLICK'	; GOSUB StartPurge
	CASE EntID = @WINDOW:'.DONE'			AND Event = 'CLICK'	; GOSUB Close
	CASE EntID = @WINDOW:'.CLEAR'			AND Event = 'CLICK'	; GOSUB Clear
	
	CASE EntID = @WINDOW:'.WO_NO' AND Event = 'POSCHANGED'		; GOSUB WONoPC
	CASE EntID = @WINDOW:'.WO_NO' AND Event = 'CLICK'			; GOSUB WOClick
	CASE EntID = @WINDOW:'.WO_NO' AND Event = 'INSERTROW'		; GOSUB InsertRow
	CASE EntID = @WINDOW:'.WO_NO' AND Event = 'DELETEROW'		; GOSUB DeleteRow
	*/
	
	
	CASE 1
		ErrorMsg = 'Unknown Parameters ':EntID:' - ':Event:' passed to commuter'
		ErrMsg(ErrorMsg)
		
END CASE

IF ErrorMsg NE '' THEN
	ErrMsg(ErrTitle:@SVM:ErrorMsg)
END

RETURN Result



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

obj_Appwindow('Create',@WINDOW)

InvStyles = Send_Message(@WINDOW:'.INV_ACTION','COLSTYLE',0,'')

InvStyles<COL$IA_ACTION>	= BitOr(InvStyles<COL$IA_ACTION>,DTCS_OPTIONSBUTTON$)

void = Send_Message(@WINDOW:'.INV_ACTION','COLSTYLE',0,InvStyles)

GOSUB Refresh

RETURN


* * * * * * *
Read:
* * * * * * *


RDSNo	= Get_Property(@WINDOW:'.RDS_NO','DEFPROP')
ToolID	= Get_Property(@WINDOW:'.TOOL_ID','DEFPROP')
Stage	= Get_Property(@WINDOW:'.STAGE','DEFPROP')

ReactRunRec = XLATE('REACT_RUN',RDSNo,'','X')


IF Stage = 'DISP' OR MemberOf(@USER4, 'OI_SUPERUSER') THEN

	InvStyles = Send_Message(@WINDOW:'.INV_ACTION','COLSTYLE',0,'')
	
	InvStyles<COL$IA_COMP_BY>	= BitOr(InvStyles<COL$IA_COMP_BY>,DTCS_OPTIONSBUTTON$)
	
	void = Send_Message(@WINDOW:'.INV_ACTION','COLSTYLE',0,InvStyles)

END;* End of check for Disposition Stage



RunStages = ReactRunRec<REACT_RUN_RUN_STAGE_KEY$>

LOCATE RdsNo:'*':Stage IN RunStages USING @VM SETTING Pos ELSE NULL

IF Pos > 1 THEN
	PrevStage = RunStages<1,Pos - 1>
	PrevStageStatus = XLATE('RUN_STAGE',PrevStage,'CURR_STATUS','X')
	
	IF PrevStageStatus NE 'COMP' AND PrevStageStatus NE 'SKIP' THEN
		Set_Property(@WINDOW:'.INV_ACTION_GROUP','ENABLED',0)
		Set_Property(@WINDOW:'.SIGN_COMP','ENABLED',0)
		Set_Property(@WINDOW:'.SKIP_STAGE','ENABLED',0)
	END ELSE
		Set_Property(@WINDOW:'.INV_ACTION_GROUP','ENABLED',1)
		Set_Property(@WINDOW:'.SIGN_COMP','ENABLED',1)
		Set_Property(@WINDOW:'.SKIP_STAGE','ENABLED',1)
	END
END	;* End of check for First Stage


IF Stage = 'GROWTH' AND ToolID = '' THEN
	ReactNo = XLATE('REACT_RUN',RDSNo,REACT_RUN_REACTOR$,'X')
	IF ReactNo NE '' THEN
		ToolID = 'R':ReactNo
		obj_Appwindow('LUValReturn',ToolID:@RM:@WINDOW:'.TOOL_ID')		;* Default setting for ToolID in the GROWTH
	END
END
	

InvActArray = Get_Property(@WINDOW:'.INV_ACTION','DEFPROP')

TestArray = InvActArray<1>		;* Take first column (INV_ACTIONS)

CONVERT @VM TO '' IN TestArray

IF TestArray = '' THEN

	RDSNo = Get_Property(@WINDOW:'.RDS_NO','DEFPROP')
	Stage = Get_Property(@WINDOW:'.STAGE','DEFPROP')

	IF RDSNo NE '' THEN

		PSNo		= XLATE('REACT_RUN',RDSNo,'PS_NO','X')
		PRSStageRec	= XLATE('PRS_STAGE',PSNo:'*':Stage,'','X')
		
		Set_Property(@WINDOW:'.SPEC_TOOL_CLASS','DEFPROP',PRSStageRec<PRS_STAGE_PROC_TOOL_CLASS$>)
		
		IF InvActArray<COL$IA_ACTION,1> = '' THEN
			InvActArray<COL$IA_ACTION> = PRSStageRec<PRS_STAGE_PROC_INV_ACTION$>
			Set_Property(@WINDOW:'.INV_ACTION','ARRAY',InvActArray)					;* Loads Inv Action Spec Items if null
		END
	
	END

END

GOSUB Refresh

RETURN


* * * * * * *
Write:
* * * * * * *

* This is called from a routine on the RUN_STAGE window 'WRITE' event as a PRE_EVENT *


SkipUpdate = Get_Property(@WINDOW,'@SKIP_WRITE_UPDATE')		;* Set by the Create NCR and NCRKeysDC events so the window doesn't close!

IF SkipUpdate THEN
	Forward_Event()					;* Performs the Write
	Result = 0
	Set_Property(@WINDOW,'@SKIP_WRITE_UPDATE','')
	RETURN
END

Result = 0						;* Set Flag to stop window POSTEVENT chain 

Forward_Event()					;* Performs the Write

Post_Event(@WINDOW,'CLOSE')


RETURN

* * * * * * *
Clear:
* * * * * * *


GOTO Refresh

RETURN

* * * * * * *
Close:
* * * * * * *

obj_Appwindow('DetailReturn')

RETURN


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

DEBUG


Result = 0	;* Stops the DELETE event chain 

RETURN


* * * * * * *
Page:
* * * * * * *

Page = Get_Property(@WINDOW:'.TABCONTROL', 'VALUE')

Set_Property(@WINDOW,'VPOSITION', Page)

RETURN


* * * * * * *
VScroll:
* * * * * * *

Page = Parm1

Set_Property(@WINDOW:'.TABCONTROL','VALUE', Page)


RETURN

* * * * * * *
Refresh:
* * * * * * *


* QBF buttons

Ctrls  = @WINDOW:'.QBF_FIRST_FIX':@RM		; Props  = 'ENABLED':@RM
Ctrls := @WINDOW:'.QBF_PREV_FIX':@RM		; Props := 'ENABLED':@RM
Ctrls := @WINDOW:'.QBF_ABS_FIX':@RM			; Props := 'ENABLED':@RM
Ctrls := @WINDOW:'.QBF_NEXT_FIX':@RM		; Props := 'ENABLED':@RM
Ctrls := @WINDOW:'.QBF_LAST_FIX':@RM		; Props := 'ENABLED':@RM
Ctrls := @WINDOW:'.QBF_STOP_FIX'			; Props := 'ENABLED'

IF Get_Property(@WINDOW,'QBFLIST') = '' THEN
	Vals = 0:@RM:0:@RM:0:@RM:0:@RM:0:@RM:0
END ELSE
	Vals = 1:@RM:1:@RM:1:@RM:1:@RM:1:@RM:1
END

Set_Property(Ctrls,Props,Vals)

* Turn edit table symbolic column backgrounds to green

ETSymbolics = Get_Property(@WINDOW,'@ET_SYMBOLICS')		;* Loaded during 'Create' in obj_Appwindow

ETCtrls	= ETSymbolics<1>
ETCols	= ETSymbolics<2>

FOR I = 1 TO COUNT(ETCtrls,@VM) + (ETCtrls NE '')
	ETCtrl	= ETCtrls<1,I>
	
	ETList = Get_Property(ETCtrl,'LIST')
	FOR Line = 1 TO COUNT(ETList,@FM) + (ETList NE '')
		IF ETList<Line,1> NE '' THEN
			FOR N = 1 TO COUNT(ETCols<1,I>,@SVM) + (ETCols<1,I> NE '')
				stat = Send_Message(ETCtrl,'COLOR_BY_POS',ETCols<1,I,N>,Line,GREEN$)
			NEXT N
		END
	NEXT Line
	
NEXT I


RETURN


* * * * * * *
SkipStage:
* * * * * * *

RDSNo 		= Get_Property(@WINDOW:'.RDS_NO','DEFPROP')
Stage 		= Get_Property(@WINDOW:'.STAGE','DEFPROP')

IF RDSNo = '' THEN RETURN
IF Stage = '' THEN RETURN


ToolClass 	= Get_Property(@WINDOW:'.SPEC_TOOL_CLASS','DEFPROP')
InvActions	= Get_Property(@WINDOW:'.INV_ACTION','DEFPROP')
CompSig		= Get_Property(@WINDOW:'.COMP_BY','DEFPROP')
CompDTM		= Get_Property(@WINDOW:'.COMP_DTM','DEFPROP')

IF InvActions<1,COL$IA_COMP_DTM> NE '' THEN
	Title	 = 'Inventory Action Completed'
	Message  = 'First Inventory Action Completed.  Stage may not be skipped.'
	Msg(@WINDOW,TypeOver,'OK','',Title:@FM:Message)
	RETURN
END


IF ToolClass NE '' THEN
	NextQueueID = obj_React_Run('StageUnloadQ',RDSNo:@RM:@RM:Stage)		;* * * * * *  JCH & FDR on 2/10/2017 
END ELSE
	NextQueueID = ''
END

IF ErrorMsg NE '' THEN RETURN

recWfrIDs = XLATE('REACT_RUN',RDSNo,REACT_RUN_CARR_WFR_ID$,'X')

WfrIDs	= ''
wCnt	= COUNT(recWfrIDs,@VM) + (recWfrIDs NE '')

FOR I = 1 TO wCnt
	recWfrID = recWfrIDs<1,I>
	IF recWfrID NE '' THEN
		WfrIDs<1,-1> = recWfrID		;* Remove any blank values from the carrier list  i.e. rejected wafers
	END
NEXT I


CurrLocs = obj_WO_Wfr('CurrLoc',WfrIDs<1,1>:@RM:'')	;* Added 9/27/2016 JCH
	
CurrQueueID	= CurrLocs<1,4>							;* Added 9/27/2016 JCH


IF @USER4 = '' THEN @USER4 = 'BRYCE_BARB'

Valid = Dialog_Box('NDW_VERIFY_USER', @Window, @User4)
Valid = Valid<1>

IF NOT(Valid) THEN
	RETURN 0
END

CONVERT '.' TO '*' IN WfrIDs

IF CompSig NE '' THEN
	EventBy = CompSig
	EventDTM = CompDTM	;******** Check conversion 
END ELSE
	EventBy		= @USER4
	EventDTM	=  OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS')
END

IF WfrIDs NE '' AND NextQueueID NE '' THEN

	Parms  = WfrIDs	:@RM			;* mv
	Parms := EventDtm:@RM
	Parms := EventBy:@RM
	Parms := 'SKIP':@RM				;* Event
	Parms := '':@RM					;* mv NewSlotIDs
	Parms := '':@RM					;* mv	RunLocs
	Parms := '':@RM					;* NCRNo		
	Parms := '':@RM					;* TWUse		
	Parms := '':@RM					;* mv	CurrSlotIDs
	Parms := '':@RM					;* NewToolID	
	Parms := '':@RM					;* CurrToolID
	Parms := NextQueueID:@RM		;* NewInvLoc	
	Parms := CurrQueueID:@RM		;* CurrInvLoc
	Parms := 'O':@RM				;* WfrSide	
	Parms := '':@RM					;* NewCarrLocs			;* mv
	Parms := '':@RM					;* CurrCarrLocs			;* mv
	Parms := ''						;* Scribes				;* mv

	obj_WO_Wfr('TestAddEvent',Parms)

	IF Get_Status(errCode) THEN
		ErrMsg(ErrCode)
		RETURN
	END

END	;* End of check for wafers in the carrier

IF CompSig = '' THEN			
	Set_Property(@WINDOW:'.COMP_BY'  , 'DEFPROP' , EventBy)
	Set_Property(@WINDOW:'.COMP_DTM' , 'DEFPROP' , EventDTM)
END


RETURN
			
	

* * * * * * *
SignComp:
* * * * * * *

* PreChecks will go in this section with 
* error handlers

RDSNo 		= Get_Property(@WINDOW:'.RDS_NO','DEFPROP')
Stage 		= Get_Property(@WINDOW:'.STAGE','DEFPROP')

IF RDSNo = '' THEN RETURN
IF Stage = '' THEN RETURN

CtrlEntID = @WINDOW:'.INV_ACTION'
IAList = Get_Property(CtrlEntID,'LIST')

iaCnt = COUNT(IAList,@FM) + (IAList NE '')

FOR I = 1 TO iaCnt
	IF IAList<I,COL$IA_ACTION> NE '' THEN
		IF IAList<I,COL$IA_COMP_DTM> = '' THEN
			
			Title	 = 'Open Inventory Action'
			Message  = 'Inventory Action: ':QUOTE(IAList<I,COL$IA_ACTION>):' has not been signed'
			
			Msg(@WINDOW,TypeOver,'OK','',Title:@FM:Message)
			
			RETURN
			
		END	;* End of check for signature completed
	END	;* End of check for Inv Action
NEXT I

ReactRunRec = XLATE('REACT_RUN',RDSNo,'','X')

WONo	= ReactRunRec<REACT_RUN_WO_NO$>
WOStep	= ReactRunRec<REACT_RUN_WO_STEP$>
CassNo	= ReactRunRec<REACT_RUN_CASS_NO$>

WOMatKey = XLATE('REACT_RUN',RDSNo,'WO_MAT_KEY','X')

IF Stage = 'ETCH' THEN

	ToolID = Get_Property(@WINDOW:'.TOOL_ID','DEFPROP')
	
	IF ToolID = '' THEN
		Title	 = 'Missing Tool ID'
		Message  = 'Tool ID for Reactor Required for the "ETCH" stage.'
		
		Msg(@WINDOW,TypeOver,'OK','',Title:@FM:Message)
		
		RETURN
	END

	* Update TOOL record Cycle Count *

	NewCycleCnt = XLATE('TOOL',ToolID,TOOL_CYCLE_CNT$,'X') + 1

	obj_Post_Log('Create','TOOL':@RM:ToolID:@RM:TOOL_CYCLE_CNT$:@RM:NewCycleCnt)		;* Bumps the Cycle Count on the tool record by 1 upon UNLOAD event

END	;* End of check for Etch Stage


Valid = Dialog_Box('NDW_VERIFY_USER', @Window, @User4)
Valid = Valid<1>

IF NOT(Valid) THEN
	RETURN 0
END

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

LogFile	= 'WO_MAT'				; WOMLParms  = LogFile:@RM
LogDTM	= SigDTM				; WOMLParms := LogDTM:@RM
Action	= WOStep:Stage			; WOMLParms := Action:@RM
WhCd	= 'GRR'					; WOMLParms := WhCd:@RM
LocCd	= 'REACT_Q'				; WOMLParms := LocCd:@RM					;************************************************************ ????
WONos	= WONo					; WOMLParms := WONos:@RM
CassNos	= CassNo				; WOMLParms := CassNos:@RM
UserID	= @USER4				; WOMLParms := UserID:@RM
Tags	= ''					; WOMLParms := Tags:@RM
ToolID	= ''					; WOMLParms := ToolID

obj_WO_Mat_Log('Create',WOMLParms)															;* Add 'VER' event WO_MAT Event Log

IF Get_Status(errCode) THEN
	
	CALL ErrMsg(ErrCode)
	
	RETURN
	
END
	
Set_Property(@WINDOW:'.COMP_BY','DEFPROP',@USER4)
Set_Property(@WINDOW:'.COMP_DTM','DEFPROP',SigDTM)
	
RETURN



* * * * * * *
LUToolID:
* * * * * * * 

ToolClass = Get_Property(@WINDOW:'.SPEC_TOOL_CLASS','DEFPROP')

IF ToolClass = '' THEN RETURN


ToolIDs = XLATE('TOOL_CLASS',ToolClass,TOOL_CLASS_TOOL$,'X')

TypeOver = ''
TypeOver<PMODE$>	= 'K'
TypeOver<PDISPLAY$>	= ToolIDs

IF INDEX(ToolIDs,@VM,1) THEN
	ToolID = Popup(@WINDOW,TypeOver,'TOOLS')
END ELSE
	ToolID = ToolIDs
END

IF Get_Status(errCode) THEN DEBUG


IF ToolID = CHAR(27) OR ToolID = '' THEN RETURN


obj_AppWindow('LUValReturn',ToolID:@RM:@WINDOW:'.TOOL_ID')


RETURN


* * * * * * *
PerformIA:
* * * * * * *

CtrlEntID = @WINDOW:'.INV_ACTION'

IAList = Get_Property(CtrlEntID,'LIST')

CurrPos = Get_Property(CtrlEntID,'SELPOS')

CurrRow = CurrPos<2>
CurrCol = CurrPos<1>


* Section added 9/21/2016 JCH & DKK to prevent blowing through the signature steps.

LastIATm = Get_Property(@WINDOW,'@LAST_IA_TM')

IF LastIATm NE '' THEN

	IF Time() < LastIATm + 10 THEN				;* IF Time is less than 10 seconds after the previous IA time, make them wait
		Msg(@WINDOW,'','IA_INTERVAL')
		RETURN
	END
	
END


CurrAction = IAList<CurrRow,COL$IA_ACTION,1>

Ctrls  = @WINDOW:'.RDS_NO':@RM				; Props  = 'DEFPROP':@RM
Ctrls := @WINDOW:'.STAGE':@RM				; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.TOOL_ID':@RM				; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.SUSC_LOAD':@RM			; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.CARRIER':@RM				; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.SPEC_TOOL_CLASS':@RM		; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.CHAR_WFR_ID':@RM			; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.CARRIER'					; Props := 'DEFPROP'

Vals = Get_Property( Ctrls , Props )

RDSNo			= Vals[1,@RM]
Stage			= Vals[COL2()+1,@RM]
ToolID			= Vals[COL2()+1,@RM]
CurrentLoad 	= Vals[COL2()+1,@RM]
CurrCassLoad	= Vals[COL2()+1,@RM]
SpecToolClass	= Vals[COL2()+1,@RM]
CharWfrIDs		= Vals[COL2()+1,@RM]
CarrWfrIDs 		= Vals[COL2()+1,@RM]

IF RDSNo = '' THEN RETURN

* LogPath         = Environment_Services('GetApplicationRootPath') : '\LogFiles\GaNWIP'
* LogDate         = Oconv(Date(), 'D4/')
* LogTime         = Oconv(Time(), 'MTS')
* LogWfrID        = WfrID
* Convert '*' to '_' in LogWfrID
* LogFileName     = LogWfrID : '_' : LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : '.csv'
* Headers         = 'Logging DTM' : @FM : 'User' : @FM : 'Routine / Method' : @FM : 'Service Description'
* ColumnWidths    = 20 : @FM : 10 : @FM : 60 : @FM : 300
* objLog          = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, ' ', Headers, ColumnWidths, False$, False$)
* LoggingDTM      = LogDate : ' ' : LogTime   ; // Logging DTM



* Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 1. CurrAction : ' : CurrAction : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)

IF CurrCol = COL$IA_ACTION THEN
	
	* Perform Inv Action dispatcher ******

	IF SpecToolClass NE '' AND ToolID = '' THEN
		IF CurrAction NE 'SPLIT' AND CurrAction NE 'DISP' THEN
			ErrMsg('ToolID must be selected prior ton Inventory transactions.')
			RETURN
		END
	END
	
	Set_Property(@WINDOW,'@LAST_IA_TM', Time() )			;* Added 9/21/2016 JCH

	ToolClassRec = XLATE('TOOL_CLASS',SpecToolClass,'','X')

	QIn = ToolClassRec<TOOL_CLASS_QIN_WH$>:'*':ToolClassRec<TOOL_CLASS_QIN_LOC$>
	
	IF SpecToolClass NE '' THEN
*	    Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 2. CurrAction : ' : CurrAction : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
		QOut = obj_React_Run('StageUnloadQ',RDSNo:@RM:@RM:Stage)						;* * Added 2/10/2017 JCH & FDR - New method for finding unload Q from current stage.
	END ELSE
*	    Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 3. CurrAction : ' : CurrAction : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
		QOut = ''
	END

*    Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 4. CurrAction : ' : CurrAction : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
	Set_Property(@WINDOW,'@SKIP_WRITE_UPDATE',1)		;* 7/21/2016 JCH

	Send_Event(@WINDOW,'WRITE')							;* 7/21/2016 JCH
*    Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 5. CurrAction : ' : CurrAction : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)

	BEGIN CASE

		CASE CurrAction = '' 
			ErrMsg('No Action Required')
		
			
		CASE CurrAction = 'LOAD'
		
			WfrIDs = CurrentLoad<COL$WFR_ID>

			CONVERT @VM TO '' IN WfrIDs

			IF WfrIDs NE '' THEN
				ErrMsg('Wafers have already been loaded on the susceptor.')
				RETURN
			END
			
			Reactor = ToolID
			CONVERT 'R' TO '' IN Reactor

			ReactorType = XLATE('REACTOR',Reactor,1,'X')

			IF ReactorType NE 'EPP' AND ReactorType NE 'GAN' THEN
				ErrMsg('Reactor ':Reactor:' must be an EpiPRO or GaN process.')
				RETURN
			END
			
			LoadComplete = Dialog_Box('DIALOG_LOAD_SUSCEPTOR', @WINDOW, RDSNo:@FM:Stage:@FM:ToolID:@FM:CurrentLoad)		;* Added LoadComplete flag 10/6/2016 JCH
			
			Set_Property('DIALOG_LOAD_SUSCEPTOR','@RETURN_KEY',RDSNo:'*':Stage)
			
			IF Get_Status(errCode) THEN
				ErrMsg(errCode)
				RETURN
			END
			
			* IASig now is in the DIALOG routine
		
			
		CASE CurrAction = 'UNLOAD'
		
			IF QOut = '' THEN
				ErrMsg('Unable to find Unload Queue for RDS No: ':RDSNo:' Stage: ':Stage)
				RETURN
			END
		
			InSlotIds = CurrentLoad<COL$IN_SLOT_ID>

			CONVERT @VM TO '' IN InSlotIDs
			
			IF InSlotIDs = '' THEN
				ErrMsg('Wafers have not been loaded onto the susceptor.')
				RETURN
			END
			
			Reactor = ToolID
			CONVERT 'R' TO '' IN Reactor

			ReactorType = XLATE('REACTOR',Reactor,1,'X')

			IF ReactorType NE 'EPP' AND ReactorType NE 'GAN' THEN
				ErrMsg('Reactor ':Reactor:' must be an EpiPRO or GaN process.')
				RETURN
			END
			
			UnloadComplete = ''
			
			IF ReactorType = 'EPP' THEN
				UnloadComplete = Dialog_Box('DIALOG_UNLOAD_SUSCEPTOR', @WINDOW, RDSNo:@FM:Stage:@FM:ToolID:@FM:CurrentLoad)
				Set_Property('DIALOG_UNLOAD_SUSCEPTOR','@RETURN_KEY',RDSNo:'*':Stage)
			END 
			
			
			IF ReactorTYpe = 'GAN' THEN
				UnloadComplete = Dialog_Box('DIALOG_UNLOAD_GAN', @WINDOW, RDSNo:@FM:Stage:@FM:ToolID:@FM:CurrentLoad)
				Set_Property('DIALOG_UNLOAD_GAN','@RETURN_KEY',RDSNo:'*':Stage)
			END
			
			IF Get_Status(errCode) THEN
				ErrMsg(errCode)
				RETURN
			END
			
			* IASig now is in the DIALOG routine
		
		
		CASE CurrAction = 'START'
		
			* Move Material from Queue to Tool
			
			StartComplete = ''
					
			CarrSlotIDs = ''
			
			IF CharWfrIDs = '' THEN
			
				* All of the Wfrs in the Carrier are being processed together *
				* CarrSlotID is kept on the WO_WFR rec and just the tool added/removed *
				
				RRunRec = XLATE('REACT_RUN',RDSNo,'','X')
				WfrIDs		= RRunRec<REACT_RUN_CARR_WFR_ID$>
				CarrSlotIDs	= RRunRec<REACT_RUN_CARR_SLOT_ID$>
			END
			
			WfrQty = COUNT(CarrSlotIDs,@VM) + (CarrSlotIDs NE '')
			
			CurrDTM =  OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS')
			
			owwWfrIDs		= ''		;* Added 9/7/2016 JCH
			owwCarrSlotIDs	= ''		;* Added 9/7/2016 JCH

			For I = 1 To WfrQty
			
				WfrID = WfrIDs<1,I>
				
				IF WfrID NE '' THEN
				
					CarrSlotID	= CarrSlotIDs<1,I>
				
					owwWfrIDs<1,-1>			= WfrID				;* Added 9/7/2016 JCH
					owwCarrSlotIDs<1,-1>	= CarrSlotID		;* Added 9/7/2016 JCH
					
					CurrLocs = obj_WO_Wfr('CurrLoc',WfrID:@RM:'')	;* Added 9/27/2016 JCH
					
					CurrSlotID	= CurrLocs<1,2>						;* Added 9/27/2016 JCH
					CurrTool	= CurrLocs<1,3>						;* Added 9/27/2016 JCH
					CurrInvLoc	= CurrLocs<1,4>						;* Added 9/27/2016 JCH
					
					owwParms	 = WfrID:@RM			;* WfrID
					owwParms	:= CurrDTM:@RM			;* EventDtm
					owwParms	:= @USER4:@RM			;* EventBy
					owwParms	:= 'START':@RM			;* Event
					owwParms	:= '':@RM				;* New Slot ID
					owwParms	:= '':@RM				;* RPZ (RDSNo*Pocket*Zone)
					owwParms	:= '':@RM				;* NCRNo
					owwParms	:= '':@RM				;* TWUse ID 
					owwParms	:= '':@RM				;* Current Slot ID
					owwParms	:= ToolID:@RM			;* New ToolID
					owwParms	:= '':@RM				;* Curr Tool ID
					owwParms 	:= '':@RM				;* NewInvLoc
					owwParms	:= CurrInvLoc:@RM		;* CurrInvLoc
					owwParms 	:= 'O':@RM				;* Wfr Side
					owwParms	:= CarrSlotID:@RM		;* New Carrier SlotID
					owwParms	:= ''					;* CurrCarrier SlotID
*					Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 5. CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
					obj_WO_Wfr('AddEvent',owwParms)
*					Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 6. CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
					
					IF Get_Status(errCode) THEN
*					    Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 6.1. Error : ' : ErrCode : ', CurrAction : ' : CurrAction : 'WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
						ErrMsg(errCode)
					END ELSE
*					    Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 7. CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
						StartComplete = 1
					END
				END
			Next I
			
			
			/*
			
			IF owwWfrIDs NE '' THEN
			
				* * * * *   Added 9/7/2016 JCH    * * * * *
				owwParms	 = owwWfrIDs:@RM			;* WfrIDs - mv
				owwParms	:= CurrDTM:@RM				;* EventDtm
				owwParms	:= @USER4:@RM				;* EventBy
				owwParms	:= 'START':@RM				;* Event
				owwParms	:= '':@RM					;* New Slot ID - mv
				owwParms	:= '':@RM					;* RPZ (RDSNo*Pocket*Zone) - mv
				owwParms	:= '':@RM					;* NCRNo
				owwParms	:= '':@RM					;* TWUse ID 
				owwParms	:= '':@RM					;* Current Slot ID - mv
				owwParms	:= ToolID:@RM				;* New ToolID
				owwParms	:= '':@RM					;* Curr Tool ID
				owwParms 	:= '':@RM					;* NewInvLoc
				owwParms	:= QIn:@RM					;* CurrInvLoc
				owwParms 	:= 'O':@RM					;* Wfr Side
				owwParms	:= owwCarrSlotIDs:@RM		;* New Carrier SlotID - mv
				owwParms	:= ''						;* CurrCarrier SlotID - mv
					
				obj_WO_Wfr('AddEvent',owwParms)
			
			*/
			
			IF StartComplete = 1 THEN
*			    Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 8. CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
				obj_Run_Stage('SetIASig',RDSNo:@RM:Stage:@RM:CurrAction)
*				Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : ' 9. CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
			END
		
			obj_Appwindow('LoadFormKeys',@WINDOW:@RM:RDSNo:'*':Stage)
			
			
		CASE CurrAction = 'STOP'
		
			IF QOut = '' THEN
				ErrMsg('Unable to find Unload Queue for RDS No: ':RDSNo:' Stage: ':Stage)
				RETURN
			END
		
			* Move Material next Queue
			
			StopComplete = ''
			
			CarrSlotIDs = ''
			
			IF CharWfrIDs = '' THEN
			
				* All of the Wfrs in the Carrier are being processed together *
				* CarrSlotID is kept on the WO_WFR rec and just the tool added/removed * 
				
				RRunRec = XLATE('REACT_RUN',RDSNo,'','X')
				WfrIDs		= RRunRec<REACT_RUN_CARR_WFR_ID$>		;* changed to WfrIDs in the carrier 10/21/2016
				CarrSlotIDs	= RRunRec<REACT_RUN_CARR_SLOT_ID$>
			END
			
			WfrQty = COUNT(CarrSlotIDs,@VM) + (CarrSlotIDs NE '')
			
			CurrDTM =  OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS')
			
			owwWfrIDs		= ''		;* Added 9/7/2016 JCH
			owwCarrSlotIDs	= ''		;* Added 9/7/2016 JCH
									
						
			* Update TOOL record Cycle Count *

			NewCycleCnt = XLATE('TOOL',ToolID,TOOL_CYCLE_CNT$,'X') + 1

			obj_Post_Log('Create','TOOL':@RM:ToolID:@RM:TOOL_CYCLE_CNT$:@RM:NewCycleCnt)		;* Bumps the Cycle Count on the tool record by 1 upon UNLOAD event

	
			For I = 1 To WfrQty
			
				WfrID		= WfrIDs<1,I>
				CarrSlotID	= CarrSlotIDs<1,I>
				
				IF WfrID NE '' THEN
				
					owwWfrIDs<1,-1>			= WfrID				;* Added 9/7/2016 JCH
					owwCarrSlotIDs<1,-1>	= CarrSlotID		;* Added 9/7/2016 JCH
					
					CurrLocs = obj_WO_Wfr('CurrLoc',WfrID:@RM:'')	;* Added 9/27/2016 JCH
					
					CurrSlotID	= CurrLocs<1,2>						;* Added 9/27/2016 JCH
					CurrTool	= CurrLocs<1,3>						;* Added 9/27/2016 JCH
					CurrInvLoc	= CurrLocs<1,4>						;* Added 9/27/2016 JCH
					
					owwParms	 = WfrID:@RM			;* WfrID
					owwParms	:= CurrDTM:@RM			;* EventDtm
					owwParms	:= @USER4:@RM			;* EventBy
					owwParms	:= 'STOP':@RM			;* Event
					owwParms	:= '':@RM				;* New Slot ID
					owwParms	:= '':@RM				;* RPZ (RDSNo*Pocket*Zone)
					owwParms	:= '':@RM				;* NCRNo
					owwParms	:= '':@RM				;* TWUse ID 
					owwParms	:= '':@RM				;* Current Slot ID
					owwParms	:= '':@RM				;* New ToolID
					owwParms	:= CurrTool:@RM			;* Curr Tool ID
					owwParms 	:= QOut:@RM				;* NewInvLoc
					owwParms	:= CurrInvLoc:@RM		;* CurrInvLoc
					owwParms 	:= 'O':@RM				;* Wfr Side
					owwParms	:= CarrSlotID:@RM		;* New Carrier SlotID
					owwParms	:= ''					;* CurrCarrier SlotID
*					Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : '10. CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
					obj_WO_Wfr('AddEvent',owwParms)
*					Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : '11. CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
					
					IF Get_Status(errCode) THEN
*                        Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : '11.1. Error : ' : ErrCode : ', CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
						ErrMsg(errCode)
					END ELSE
*                        Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : '12. CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
						StopComplete = 1
					END
				END
			Next I
			
			
			/*
			
			
			* * * * *   Added 9/7/2016 JCH   * * * * *
			
			IF owwWfrIDs NE '' THEN
			
				owwParms	 = owwWfrIDs:@RM			;* WfrID - mv
				owwParms	:= CurrDTM:@RM				;* EventDtm
				owwParms	:= @USER4:@RM				;* EventBy
				owwParms	:= 'STOP':@RM				;* Event
				owwParms	:= '':@RM					;* New Slot ID - mv
				owwParms	:= '':@RM					;* RPZ (RDSNo*Pocket*Zone) - mv
				owwParms	:= '':@RM					;* NCRNo
				owwParms	:= '':@RM					;* TWUse ID 
				owwParms	:= '':@RM					;* Current Slot ID - mv
				owwParms	:= '':@RM					;* New ToolID
				owwParms	:= ToolID:@RM				;* Curr Tool ID
				owwParms 	:= QOut:@RM					;* NewInvLoc
				owwParms	:= '':@RM					;* CurrInvLoc
				owwParms 	:= 'O':@RM					;* Wfr Side
				owwParms	:= owwCarrSlotIDs:@RM		;* New Carrier SlotID - mv
				owwParms	:= ''						;* CurrCarrier SlotID - mv
					
				obj_WO_Wfr('AddEvent',owwParms)
			
			END
			
			*/
			
			IF StopComplete = 1 THEN
*                Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : '13. CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
				obj_Run_Stage('SetIASig',RDSNo:@RM:Stage:@RM:CurrAction)
*				Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: 'RUN_STAGE / PerformIA' : @FM : '14. CurrAction : ' : CurrAction : ', WfrID : ' : WfrID : ', RDSNo : ' : RDSNo : ', Stage : ' : Stage, @RM, @FM)
			END
			
			obj_Appwindow('LoadFormKeys',@WINDOW:@RM:RDSNo:'*':Stage)
			
		
		CASE CurrAction = 'SPLIT'
			
			TestCarrWfrIDs = CarrWfrIDs
			
			CONVERT @VM TO '' IN TestCarrWfrIDS

			IF TestCarrWfrIDs = '' THEN
				ErrMsg('Wafers have not been unloaded from the reactor.')
				RETURN
			END
			

			SplitComplete = Dialog_Box('DIALOG_SPLIT_WAFER', @WINDOW, RDSNo:@FM:Stage:@FM:ToolID:@FM:CarrWfrIDs)
			
			Set_Property('DIALOG_SPLIT_WAFER','@RETURN_KEY',RDSNo:'*':Stage)
			
			IF Get_Status(errCode) THEN
				ErrMsg(errCode)
				RETURN
			END
			
			
		
		
		CASE CurrAction = 'DISP'
			
			DetWindow	= 'REACT_RUN_DISP'
			DetKeys		= RDSNo
			DefaultRec	= ''
			RetKey		= RDSNo:'*':Stage
			RetPage		= 1
			RetCtrl		= ''
			RetPos		= ''
		
			oAParms = DetWindow:@RM:DetKeys:@RM:DefaultRec:@RM:RetKey:@RM:RetPage:@RM:RetCtrl:@RM:RetPos

			obj_AppWindow('ViewNewDetail',oAParms)
			
			IF Get_Status(errCode) THEN
				ErrMsg(errCode)
			END
			
			*obj_Run_Stage('SetIASig',RDSNo:@RM:Stage:@RM:CurrAction)
		
		
		CASE 1
			ErrMsg('Unknown Inventory Action, contact support')
			obj_Appwindow('LoadFormKeys',@WINDOW:@RM:RDSNo:'*':Stage)
			

	END CASE	

END


IF CurrCol = COL$IA_COMP_BY THEN

	Set_Property(@WINDOW,'@SKIP_WRITE_UPDATE',1)		;* 7/21/2016 JCH
	Send_Event(@WINDOW,'WRITE')							;* 7/21/2016 JCH
	
	obj_Run_Stage('SetIASig',RDSNo:@RM:Stage:@RM:CurrAction)
		
	obj_Appwindow('LoadFormKeys',@WINDOW:@RM:RDSNo:'*':Stage)

END	;* End of check for Signature column
	
RETURN

