COMPILE FUNCTION Comm_TW_Use(CtrlEntID,Event,Parm1, Parm2, Parm3, Parm4, Parm5)
#pragma precomp SRP_PreCompiler

/*
	Commuter module for TW_USE (Test Wafer Use) window
	
	12/12/2007 - John C. Henry, J.C. Henry & Co., Inc.
	
	12/02/2020 - DJS - Moved function calls in LU_WfrID GoSub to the Window's WRITE event to prevent data inconsistency
	                   issues when users cancel creating a TW_USE record.
	01/25/2021 - DJS - Moved function calls in Delete GoSub to after the user confirms the delete prompt.
*/

COMMON /CommTWUse/ Module@, Routine@, RoutineParams@

DECLARE SUBROUTINE Set_Property,  Set_Status, ErrMsg, Set_Property, obj_AppWindow, Forward_Event, Database_Services
DECLARE SUBROUTINE Send_Event, obj_TW_Use, Security_Err_Msg, Post_Event, Btree.Extract, ErrMsg, obj_WO_Mat
DECLARE SUBROUTINE Send_Message, obj_WM_IN, obj_WO_Wfr, obj_Notes, obj_WO_Wfr, Obj_Tables, Error_Services
DECLARE SUBROUTINE Get_EventStatus

DECLARE FUNCTION Get_Property, Get_Status, Popup, Send_Message, Msg, MemberOf, obj_WO_Wfr, RDS_Services, Function
DECLARE FUNCTION obj_Quote, Security_Check, obj_WO_Mat, obj_WM_In, Dialog_Box, Error_Services, Database_Services


$INSERT REACT_RUN_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT NOTIFICATION_EQU
$INSERT SECURITY_RIGHTS_EQU
$INSERT LSL_USERS_EQU
$INSERT MSG_EQUATES
$INSERT APPCOLORS
$INSERT POPUP_EQUATES
$INSERT LOGICAL

EQU CRLF$	TO \0D0A\

ErrTitle = 'Error in Comm_TW_Use'
ErrorMsg = ''

Result = ''

BEGIN CASE
	CASE CtrlEntID = @WINDOW AND Event = 'CREATE'			; GOSUB Create
	CASE CtrlEntID = @WINDOW AND Event = 'READ'				; GOSUB Read
	CASE CtrlEntID = @WINDOW AND Event = 'WRITE'			; GOSUB Write
	CASE CtrlEntID = @WINDOW AND Event = 'DELETE'			; GOSUB Delete
	CASE CtrlEntID = @WINDOW AND Event = 'CLEAR'            ; GOSUB Refresh    			 
	CASE CtrlEntID = @WINDOW AND Event = 'CLOSE'			; GOSUB Close
	CASE CtrlEntID = @WINDOW AND Event[1,3] = 'QBF'			; GOSUB Refresh
	
	CASE CtrlEntID = @WINDOW:'.WAFER_ID' AND Event = 'DELETEROW'	; GOSUB DeleteWaferID
	CASE CtrlEntID = @WINDOW:'.WAFER_ID' AND Event = 'INSERTROW'	; GOSUB InsertWaferID
	
	CASE CtrlEntID = @WINDOW:'.TW_QTY' AND Event = 'LOSTFOCUS'		; GOSUB TWQtyLF
	CASE CtrlEntID = @WINDOW:'.TW_CODE' AND Event = 'LOSTFOCUS'		; GOSUB Refresh
	CASE CtrlEntID = @WINDOW:'.TW_TYPE' AND Event = 'LOSTFOCUS'		; GOSUB TWTypeLF
	CASE CtrlEntID = @WINDOW:'.TW_TYPE' AND Event = 'GOTFOCUS'		; GOSUB TWTypeGF
	
	CASE CtrlEntID = @WINDOW:'.LU_TW_CODE' AND Event = 'CLICK'		; GOSUB LU_TWCode
	CASE CtrlEntID = @WINDOW:'.LU_TW_TYPE' AND Event = 'CLICK'		; GOSUB LU_TWType
	CASE CtrlEntID = @WINDOW:'.LU_TOOL' AND Event = 'CLICK'			; GOSUB LU_Tool
	CASE CtrlEntID = @WINDOW:'.LU_CUST_PART_NO' AND Event = 'CLICK'	; GOSUB LU_CustPart
	CASE CtrlEntID = @WINDOW:'.LU_WFR_ID' AND Event = 'CLICK'		; GOSUB LU_WfrID
	CASE CtrlEntID = @WINDOW:'.SIGN_BUTTON' AND Event = 'CLICK'		; GOSUB Sign
	CASE CtrlEntID = @WINDOW:'.WAFER_HELP' AND Event = 'CLICK'		; GOSUB WaferHelp
	
	CASE 1
	
		ErrorMsg = 'Unknown Instruction passed to routine':CRLF$:CtrlEntID:' - ':Event
END CASE

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

RETURN Result


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

obj_Appwindow('Create',@WINDOW)

* Provides compatibility with the existing messaging attachment system

IF Parm1 NE '' THEN
	PassedKeys = FIELD(Parm1,'*',1)
	obj_Appwindow('ViewRelated',@WINDOW:@RM:PassedKeys)
END

IOOptions = Get_Property(@WINDOW,'IOOPTIONS')
IOOptions<10> = 1	;* Check for required fields on WRITE event
IOOptions<11> = 1	;* Create READ event on QBF load

Set_Property(@WINDOW,'IOOPTIONS',IOOptions)

GOSUB Refresh

RETURN


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

TestRec = Get_Property(@WINDOW,'RECORD')
CONVERT @FM TO '' IN TestRec
IF TestRec = '' THEN
	Set_Property(@WINDOW,'@NEW_RECORD',1)
END

GOSUB Refresh

Return


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

    Signed = Get_Property(@Window : '.SIGNATURE', 'TEXT')
    If Signed NE '' then
        Forward_Event()
        If Assigned(Module@) and Assigned(Routine@) and Assigned(RoutineParams@) then
            Handler = Module@
            Convert @Lower.Case to @Upper.Case in Handler
            Ans     = Function(@Handler(Routine@, RoutineParams@))
        end       
        Post_Event(@Window, 'CLOSE')
    end else
        Error_Services('Add', 'Signature required before saving.')
        ErrMsg('Signature required before saving.')
    end

Return


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

RETURN


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

obj_Appwindow('DetailReturn')
Forward_Event()

RETURN


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

IF MemberOf(@USER4, 'ENG_TECH') OR MemberOf( @USER4, 'SUPERVISOR' ) OR  MemberOf( @USER4, 'LEAD' ) ELSE
	MsgInfo = ''
	MsgInfo<micon$> = '!'
	MsgInfo<mtext$> = 'You must be a Supervisor, Lead or Eng. Tech to Remove a Test Wafer Use.'
	Void = Msg( '', MsgInfo )
	RETURN
END

Ctrls  = @WINDOW:'.MET_NO':@RM		; Props  = 'DEFPROP':@RM
Ctrls := @WINDOW:'.TW_NO':@RM		; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.WAFER_ID'		; Props := 'DEFPROP'

Vals = Get_Property(Ctrls,Props)

MetNo		= Vals[1,@RM]
TWNo		= Vals[COL2()+1,@RM]
WaferIDs	= Vals[COL2()+1,@RM]

LOOP
UNTIL WaferIDs[-1,1] NE @VM OR WaferIDs = ''
	WaferIDs[-1,1] = ''
REPEAT

IF WaferIDs NE '' THEN

	RDSNo	= Get_Property(@WINDOW:'.RDS_NO','DEFPROP')
	MetNo	= Get_Property(@WINDOW:'.MET_NO','DEFPROP')
	TWNo 	= Get_Property(@WINDOW:'.TW_NO','DEFPROP')

	TWUseKey = MetNo:'*':TWNo
	
	* * * *   Added 3/24/2016  JCH - wafer history   * * * * 
	
	wCnt = COUNT(WaferIDs,@VM) + (WaferIDs NE '')
	
	FOR W = 1 TO wCnt
		
		WfrID = WaferIDs<1,W>
		Event = 'TEST'
		
		obj_WO_Wfr('RemEvent',Parms)
		
	NEXT W
	
	* * * * *   End of update   * * * * *
	
	RRRec = XLATE('REACT_RUN',RDSNo,'','X')
	ReactType = XLATE('REACTOR',RRRec<REACT_RUN_REACTOR$>,1,'X')
	
	FirstWafer	= WaferIDs<1,1>
	WONo		= FirstWafer[1,'*']
	CassNo		= FirstWafer[COL2()+1,'*']
	
	WaferID = '' ;* Pass null to remove all references to this TW_USE record

	IF ReactType = 'EPP' THEN
	    
*		obj_WM_In('RepProdTW',WONo:@RM:CassNo:@RM:MetNo:'*':TWNo:@RM:WaferID)
        // Subroutine call moved to the end of this event after the user confirms the delete prompt.
        Module@        = 'OBJ_WM_IN'
        Routine@       = 'RepProdTW'
        RoutineParams@ = WONo:@RM:CassNo:@RM:MetNo:'*':TWNo:@RM:WaferID
	END ELSE
*		obj_WO_Mat('RepProdTW',WONo:@RM:CassNo:@RM:MetNo:'*':TWNo:@RM:WaferID)
        // Subroutine call moved to the end of this event after the user confirms the delete prompt.
        Module@        = 'OBJ_WO_MAT'
        Routine@       = 'RepProdTW'
        RoutineParams@ = WONo:@RM:CassNo:@RM:MetNo:'*':TWNo:@RM:WaferID
	END

END

Forward_Event()

EventCode  = ''
Get_EventStatus(EventCode)

If EventCode<0, 1> NE 'EV107' then
    // User pressed ok on delete prompt
    // Commit related delete events
    If Assigned(Module@) and Assigned(Routine@) and Assigned(RoutineParams@) then
        Handler = Module@
        Convert @Lower.Case to @Upper.Case in Handler
        Ans     = Function(@Handler(Routine@, RoutineParams@))
    end    
end

Result = 0

Send_Event(@WINDOW,'CLOSE')

RETURN



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

Ctrls  = @WINDOW:'.TW_QTY':@RM				; Props  = 'DEFPROP':@RM
Ctrls := @WINDOW:'.TW_CODE':@RM				; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.TOOL':@RM				; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.TW_TYPE':@RM				; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.CUST_TW_PART_NO':@RM		; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.WAFER_ID':@RM			; Props := 'ARRAY':@RM
Ctrls := @WINDOW:'.SIGNATURE'				; Props := 'DEFPROP'

Vals = Get_Property(Ctrls,Props)

TWQty			= Vals[1,@RM]
TWCode			= Vals[COL2()+1,@RM]
Tool			= Vals[COL2()+1,@RM]
TWType			= Vals[COL2()+1,@RM]
CustTWPartNo	= Vals[COL2()+1,@RM]
WaferIDs		= Vals[COL2()+1,@RM]
Signature		= Vals[COL2()+1,@RM]

LOOP
	WaferID = WaferIDs[-1,'B':@VM]
UNTIL WaferID NE '' OR WaferIDs = ''
	WaferIDs[-1,1] = ''
REPEAT

IF Signature = '' THEN

	OKToSign = 1
	TestWaferIDs = Get_Property(@WINDOW:'.WAFER_ID','DEFPROP')
	CONVERT @VM TO '' IN TestWaferIDs
	
	Ctrls  = @WINDOW:'.TOOL':@RM		; Props  = 'ENABLED':@RM
	Ctrls := @WINDOW:'.TW_TYPE':@RM		; Props := 'ENABLED':@RM
	Ctrls := @WINDOW:'.LU_TOOL':@RM		; Props := 'ENABLED':@RM
	Ctrls := @WINDOW:'.LU_TW_TYPE'		; Props := 'ENABLED'
	
	IF TWCode = 99 AND TWQty = 0 THEN	    
	    Vals = 0:@RM:0:@RM:0:@RM:0
	END ELSE
	    Vals = 1:@RM:1:@RM:1:@RM:1
	end

    Set_Property(Ctrls,Props,Vals)
	
	BEGIN CASE
		
		CASE (TWCode = '99' OR TWCode = '98') AND TWQty = 0		; NULL
		CASE (TWCode = '99' OR TWCode = '98') AND TWQty NE 0	; OKToSign = 0
		CASE TWQty = ''											; OKToSign = 0
		CASE TWCode = ''										; OKToSign = 0
		CASE TWType = ''										; OKToSign = 0
		CASE TWType = 'PROD' AND TestWaferIDs = '' 				; OKToSign = 0
			
	END CASE

	Set_Property(@WINDOW:'.SIG_GROUP','ENABLED',OKToSign)		;* Required parameters met


	TWType = Get_Property(@WINDOW:'.TW_TYPE','DEFPROP')

	IF TWType = 'CUST'  THEN
		Set_Property(@WINDOW:'.CUST_TW_GROUP','ENABLED',1)
	END ELSE
		Set_Property(@WINDOW:'.CUST_TW_GROUP','ENABLED',0)
	END

END

IF TWType = 'PROD' AND Signature = '' THEN
	Set_Property(@WINDOW:'.PROD_TEST_GROUP','ENABLED',1)
END ELSE
	IF MemberOf(@USER4, 'OI_SUPERUSER') THEN
		Set_Property(@WINDOW:'.PROD_TEST_GROUP','ENABLED',1)
	END ELSE
		Set_Property(@WINDOW:'.PROD_TEST_GROUP','ENABLED',0)
	END
	
END

* 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


* * * * * * *
TWQtyLF:
* * * * * * *

Ctrls  = @WINDOW:'.TW_QTY':@RM			; Props  = 'DEFPROP':@RM
Ctrls := @WINDOW:'.TW_CODE'				; Props := 'DEFPROP'

Vals = Get_Property(Ctrls,Props)

TWQty 	= Vals[1,@RM]
TWType	= Vals[COL2()+1,@RM]

IF TWQty = 0 AND TWType = '' THEN 

	TypeOver = ''
	TypeOver<PDISPLAY$> = 'WITH SEQ = "99" "98" BY SEQ'
	
	TWCode = Popup(@WINDOW,TypeOver,'TW_CODES')
	
	IF TWCode = '' OR TWCode = CHAR(27) THEN RETURN
		
	Set_Property(@WINDOW:'.TW_CODE','DEFPROP',TWCode)
END

GOSUB Refresh

RETURN



* * * * * * *
TWTypeLF:
* * * * * * *

Ctrls  = @WINDOW:'.TW_QTY':@RM			; Props  = 'DEFPROP':@RM
Ctrls := @WINDOW:'.TW_CODE'				; Props := 'DEFPROP'

Vals = Get_Property(Ctrls,Props)

TWQty 		= Vals[1,@RM]
TWType		= Vals[COL2()+1,@RM]

IF TWQty NE 0 AND (TWType = 99 OR TWType = 98)THEN 
	ErrMsg('TW Qty must be "0" when using TW Type "99" or "98".')
	Set_Property(@WINDOW:'TW_CODE','FOCUS',1)
END

GOSUB Refresh

RETURN


* * * * * * *
TWTypeGF:
* * * * * * *

Ctrls  = @WINDOW:'.TW_TYPE':@RM			; Props  = 'DEFPROP':@RM
Ctrls := @WINDOW:'.WAFER_ID'			; Props := 'ARRAY'

Vals = Get_Property(Ctrls,Props)

TWType 		= Vals[1,@RM]
WaferIDs	= Vals[COL2()+1,@RM]

NoChange = 0

IF TWType = 'PROD' THEN
	CONVERT @FM:@VM TO '' IN WaferIDs
	IF WaferIDs NE '' THEN
		Msg(@WINDOW,'','TW_TYPE_CHANGE')
		NoChange = 1
	END
END

RETURN


* * * * * * *
LU_TWCode:
* * * * * * *

Result = Popup(@WINDOW,'','SHOW_TW_CODES')
		
IF Result NE '' THEN
	Parms = Result:@RM:@WINDOW:'.TW_CODE':@RM:''
	obj_Appwindow('LUValReturn',Parms )
END

GOSUB Refresh
		
RETURN


* * * * * * *
LU_TWType:
* * * * * * *

GOSUB TWTypeGF

IF NoChange = 1 THEN RETURN

Result = Popup(@WINDOW,'','TW_TYPES')
		
IF Result NE '' THEN
	Parms = Result:@RM:@WINDOW:'.TW_TYPE':@RM:''
	obj_Appwindow('LUValReturn',Parms )
END

GOSUB Refresh
		
RETURN


* * * * * * *
LU_Tool:
* * * * * * *

Result = Popup( @WINDOW, '', 'CHOOSE_TOOL' )
	
IF Result NE '' THEN
	Parms = Result:@RM:@WINDOW:'.TOOL':@RM:''
	obj_Appwindow('LUValReturn',Parms )
END

RETURN


* * * * * * *
LU_CustPart:
* * * * * * *

CustNo = Get_Property(@WINDOW:'.CUST_NO','DEFPROP')

IF CustNo NE '' THEN

	SearchString = 'CUST_NO':@VM:CustNo:@FM
	OPEN 'DICT.CUST_TW_PART' TO DictVar ELSE
		ErrMsg('Unable to open DICT.CUST_TW_PART for index lookup.')
		RETURN
	END

	Options = ''
	Flag = ''

	BTREE.EXTRACT(SearchString, 'CUST_TW_PART', DictVar, CustTWPartKeys, Options, flag)

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

	IF CustTWPartKeys = '' THEN
		ErrMsg('No TW Part Numbers on file for Customer ':QUOTE(CustNo):'.')
		RETURN
	END

	TypeOver = ''
	TypeOver<PDISPLAY$> = CustTWPartKeys
	TypeOver<PSELECT$>	= 1					;* Single select

	CustTWPartKey = Popup(@WINDOW,TypeOver,'CUST_TW_PART')
	CustTWPartNo = CustTWPartKey[-1,'B*']
	
	IF CustTWPartNo NE '' THEN
		Parms = CustTWPartNo:@RM:@WINDOW:'.CUST_TW_PART_NO':@RM:''
		obj_Appwindow('LUValReturn',Parms )
	END

END	;* End of check for null Customer number

RETURN


* * * * * * *
LU_WfrID:
* * * * * * *

Ctrls  = @WINDOW:'.MET_NO':@RM		; Props  = 'DEFPROP':@RM
Ctrls := @WINDOW:'.TW_NO':@RM		; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.RDS_NO':@RM		; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.TW_QTY':@RM		; Props := 'DEFPROP':@RM
Ctrls := @WINDOW:'.ZONE'			; Props := 'DEFPROP'

Vals = Get_Property(Ctrls,Props)

MetNo	= Vals[1,@RM]
TWNo	= Vals[COL2()+1,@RM]
RDSNo	= Vals[COL2()+1,@RM]
TWQty	= Vals[COL2()+1,@RM]
Zone	= Vals[COL2()+1,@RM]

IF MetNo	= '' THEN RETURN
IF TWNo		= '' THEN RETURN
IF RDSNo	= '' THEN RETURN

TWUseKey = MetNo:'*':TWNo

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


WONo	= RRRec<REACT_RUN_WO_NO$>
CassNo	= RRRec<REACT_RUN_CASS_NO$>
Pockets	= RRRec<REACT_RUN_POCKET$>
Zones	= RRRec<REACT_RUN_ZONE$>

PocketCnt = COUNT(Pockets,@VM) + (Pockets NE '')

ProdTestArray = ''

IF PocketCnt > 0 THEN

	* * * EpiPRO * * *
	Display = ''
	LineCnt = 0
	
	FOR I = 1 TO PocketCnt
	
		a1PktChar = RRRec<REACT_RUN_POCKET_CHAR$,I>
		a1WfrChar = RRRec<REACT_RUN_WAFER_CHAR$,I>
		a1WfrZone = RRRec<REACT_RUN_ZONE$,I>
		
		
		IF RRRec<REACT_RUN_POCKET_CHAR$,I> = 'TEST' AND RRRec<REACT_RUN_WAFER_CHAR$,I> = 'PROD' AND RRRec<REACT_RUN_ZONE$,I> = Zone THEN
			LineCnt += 1
			
			Display<1,LineCnt,1> = Pockets<1,I>
			Display<1,LineCnt,2> = RRRec<REACT_RUN_ZONE$,I>
			Display<1,LineCnt,3> = RRRec<REACT_RUN_POCKET_CHAR$,I>
			Display<1,LineCnt,4> = RRRec<REACT_RUN_WAFER_CHAR$,I>
			Display<1,LineCnt,5> = WONo:'.':RRRec<REACT_RUN_IN_CASS_NO$,I>:'.':RRRec<REACT_RUN_IN_SLOT_NO$,I>
			Display<1,LineCnt,6> = RRRec<REACT_RUN_OUT_CASS_NO$,I>:'.':RRRec<REACT_RUN_OUT_SLOT_NO$,I>
			
		END
	NEXT I
	
	IF Display = '' THEN
		ErrMsg('No Product Test Wafer configured in this zone during EpiPRO Load.')
		RETURN
	END
	
	TypeOver = ''
	TypeOver<PDISPLAY$> = Display
	
	LOOP
		WaferIDs = Popup(@WINDOW,TypeOver,'REACT_EPI_PRO_TW_LOAD')
		WaferIDCnt = COUNT(WaferIDs,@VM) + (WaferIDs NE '')
	UNTIL WaferIDCnt = TWQty
		Response = Msg(@WINDOW,'','PROD_TEST_QTY_ERROR','',WaferIDCnt:@FM:TWQty)
		IF Response = CHAR(27) THEN RETURN
	REPEAT
	
	
	IF WaferIDCnt > 0 THEN
		ProdTestArray = ''	
		
		
		RemDTM = OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS')
		
		TestWfrIDs = ''
		
		FOR N = 1 TO WaferIDCnt
		
			WaferID = WaferIDs<1,N>
			
			SrcCassNo	= Field(WaferID,'.',2)
			SlotNo		= Field(WaferID,'.',3)
			
			If SlotNo = '' Then
				Recipients		= XLATE('SEC_GROUPS', 'OI_ADMIN', 'USER', 'X')
				SentFrom		= @USER4
				Subject			= 'TW_USE - EpiPRO':TWUseKey
				Message			= 'WfrID = ':QUOTE(WONo:'*':CassNo:'*':SlotNo):CRLF$:WaferIDs
				AttachWindow	= 'TW_USE'
				AttachKey		= TWUseKEy
				SendToGroup		= ''   

				Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
				obj_Notes('Create',Parms)
			END
			
			
			
			* * * *   Added 3/24/2016  JCH - wafer history   * * * * 
			
			TestWfrID =  WONo:'*':SrcCassNo:'*':SlotNo
			
			CurrLocs = obj_WO_Wfr('CurrLoc',TestWfrID)
				
			WOMatLoc 	= CurrLocs<1,1>		;* Only 1 of these can be set others will be null
			ToolLoc		= CurrLocs<1,2>
			InvIDLoc	= CurrLocs<1,3>
				
			
			
			TestWfrIDs<1,-1> = WONo:'*':SrcCassNo:'*':SlotNo							;* 9/7/2016 JCH obj_WO_Wfr update
		
			Parms  = TestWfrID:@RM									;* WfrID
			Parms := RemDTM:@RM										;* EventDtm
			Parms := @USER4:@RM										;* EventBy
			Parms := 'TEST':@RM										;* Event
			Parms := '':@RM											;* NewSlotID
			Parms := '':@RM											;* RunLoc
			Parms := '':@RM											;* NCRNo
			Parms := TWUseKey:@RM									;* TWUse
			Parms := '':@RM											;* CurrSlotID
			Parms := '':@RM											;* NewToolID
			Parms := '':@RM											;* CurrToolID
			Parms := '':@RM											;* NewInvLoc
			Parms := '':@RM											;* CurrInvLoc
			Parms := 'O'											;* Inbound or Outbound Wafers
			
			obj_WO_Wfr('AddEvent',Parms)
			
			* * * * *   End of Update   * * * * * *
			
			Set_Status(0)
			
			
*			ProdTestArray<1,N> = obj_WM_IN('RemProdTW',WONo:@RM:SrcCassNo:@RM:TWUseKey:@RM:SlotNo)	;* Add metrology number to each selected slot

            // Function call moved to WINDOW WRITE event.
            Module@        = 'OBJ_WM_IN'
            Routine@       = 'RemProdTW'
            RoutineParams@ = WONo:@RM:SrcCassNo:@RM:TWUseKey:@RM:SlotNo
            ProdTestArray  = TestWfrIDs
			
			IF Get_Status(errCode) THEN
				ErrMsg(errCode)
				* Back out logic here
			END
			
		NEXT N
		
		
		/*
		
		* * * *   Added 9/7/2016  JCH - update   * * * * 
		
		IF TestWfrIDs NE '' THEN
		
			Parms  = TestWfrIDs:@RM				;* WfrIDs
			Parms := RemDTM:@RM					;* EventDtm
			Parms := @USER4:@RM					;* EventBy
			Parms := 'TEST':@RM					;* Event
			Parms := '':@RM						;* NewSlotIDs
			Parms := '':@RM						;* RunLocs
			Parms := '':@RM						;* NCRNo
			Parms := TWUseKey:@RM				;* TWUse
			Parms := '':@RM						;* CurrSlotIDs
			Parms := '':@RM						;* NewToolID
			Parms := '':@RM						;* CurrToolID
			Parms := '':@RM						;* NewInvLoc
			Parms := '':@RM						;* CurrInvLoc
			Parms := 'O'						;* Inbound or Outbound Wafers
			
			obj_WO_Wfr('AddEvent',Parms)
		END
		
		* * * * *   End of Update   * * * * * *
		
		*/
	
	END
	
END ELSE
	
	WOMatRec = XLATE('WO_MAT',WONo:'*':CassNo,'','X')
	
	SlotMetNos 		= WOMatRec<WO_MAT_SLOT_MET_NO$>
	SlotNCRNos		= WOMatRec<WO_MAT_SLOT_NCR$>
	SlotWaferIDs 	= obj_WO_Mat('SlotWaferIDs',WONo:'*':CassNo)									;* Get list of available wafers in cassette
	
	WaferIDCnt = COUNT(SlotWaferIDs,@VM) + (SlotWaferIDs NE '')
	
	PopupDisplay = ''
	CurrTWUseCnt = 0

	FOR N = 1 TO WaferIDCnt
        PopupDisplay<1,N,1> = N
        PopupDisplay<1,N,2> = SlotWaferIDs<1,N>
        PopupDisplay<1,N,3> = SlotNCRNos<1,N>
        PopupDisplay<1,N,4> = SlotMetNos<1,N>
        IF SlotMetNos<1,N> = MetNo:'*':TWNo THEN CurrTWUseCnt += 1	;* Test Wafers already pulled from the box for this TW_USE
	NEXT N
	
	TypeOver = ''
	TypeOver<PDISPLAY$> = PopupDisplay
	TypeOver<PFIELD$>	= 2
	
	LOOP
		WaferIDs	= Popup(@WINDOW,TypeOver,'SELECT_PROD_TEST')
		
		IF WaferIDs = '' OR WaferIDs = CHAR(27) THEN RETURN
		
		WaferIDCnt	= COUNT(WaferIDs,@VM) + (WaferIDs NE '')
		
	UNTIL WaferIDCnt + CurrTWUseCnt = TWQty
	
		Response	= Msg(@WINDOW,'','PROD_TEST_QTY_ERROR','',WaferIDCnt:@FM:TWQty)
		
		IF Response = CHAR(27) THEN RETURN
	REPEAT

	SlotNos = ''
 	FOR N = 1 TO WaferIDCnt
		WaferID = WaferIDs<1,N>
		IF WaferID = '' THEN 
			ErrMsg('Select slots must contain a Wafer ID.')
			RETURN
		END ELSE
			SlotNos<1,-1> = Field(WaferID,'.',3)
		END
		Locate WaferID in SlotWaferIDs using @VM setting SlotNo then
            If SlotNCRNos<1, SlotNo> NE '' then
                ErrMsg('Selected Wafer ID# ' : WaferID : ' is not eligible for Metrology.')
                RETURN		    
            end
        end
		
 	NEXT N
	
	* * * *   Added 3/24/2016  JCH - wafer history   * * * * 
	
	RemDTM = OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS')
	
	TestWfrIDs = ''
	
	sCnt = COUNT(SlotNos,@VM) + (SlotNos NE '')
	
	FOR S = 1 TO Scnt
	
		SlotNo = SlotNos<1,S>
		
		If SlotNo = '' Then
			Recipients		=  XLATE('SEC_GROUPS', 'OI_ADMIN', 'USER', 'X')
			SentFrom		= @USER4
			Subject			= 'TW_USE non EpiPRO':TWUseKey
			Message			= 'WfrID = ':QUOTE(WONo:'*':CassNo:'*':SlotNo):CRLF$:WaferIDs
			AttachWindow	= 'TW_USE'
			AttachKey		= TWUseKEy
			SendToGroup		= ''   

			Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
			obj_Notes('Create',Parms)
		END
		
		TestWfrIDs<1,-1> = WONo:'*':CassNo:'*':SlotNo
		
		Parms  = WONo:'*':CassNo:'*':SlotNo:@RM				;* WfrIDs
		Parms := RemDTM:@RM									;* EventDtm
		Parms := @USER4:@RM									;* EventBy
		Parms := 'TEST':@RM									;* Event
		Parms := '':@RM										;* NewSlotIDs
		Parms := '':@RM										;* RunLocs
		Parms := '':@RM										;* NCRNo
		Parms := TWUseKey:@RM								;* TWUse
		Parms := ''											;* CurrSlotID

		obj_WO_Wfr('AddEvent',Parms)
		
	NEXT S
	
	
	Set_Status(0)

* 	ProdTestArray = obj_WO_Mat('RemProdTW',WONo:@RM:CassNo:@RM:TWUseKey:@RM:SlotNos:@RM:'1')
    // Function call moved to WINDOW WRITE event.
 	Module@        = 'OBJ_WO_MAT'
 	Routine@       = 'RemProdTW'
 	RoutineParams@ = WONo:@RM:CassNo:@RM:TWUseKey:@RM:SlotNos:@RM:'1'
    ProdTestArray  = TestWfrIDs   

	IF Get_Status(errCode) THEN
		ErrMsg(errCode)
		* Back out logic here
	END

END	;* End of check for EpiPRO pocket load data

CurrProdTestArray = Get_Property(@WINDOW:'.WAFER_ID','ARRAY')

NewProdTestArray = ''

Position = 1
Flag  = ""
LOOP
    REMOVE CurrWfrID FROM CurrProdTestArray AT Position SETTING FLAG
WHILE Flag
	IF CurrWfrID NE '' THEN
		LOCATE CurrWfrID IN ProdTestArray BY 'AR' USING @VM  SETTING Pos ELSE
			ProdTestArray = INSERT(ProdTestArray,1,Pos,0,CurrWfrID)
		END
	END		
REPEAT

IF ProdTestArray NE '' THEN Set_Property(@WINDOW:'.WAFER_ID','ARRAY',ProdTestArray:@VM:@VM:@VM)


GOSUB Refresh

RETURN


* * * * * * *
InsertWaferID:
* * * * * * *

RowIndex = Parm1

void = Send_Message(CtrlEntID, 'DELETE', RowIndex)

ErrMsg('Use "Select Wafers" button on window to add additional wafers.')


RETURN


* * * * * * * 
DeleteWaferID:
* * * * * * *

RowIndex	= Parm1
WaferID		= Parm2

MsgHead	= 'Replace Product Test Wafer in Cassette'
MsgText	= 'Are you sure you wish to place wafer ':WaferID:' back into the product cassette?'

OK = Msg(@WINDOW,'','YESNO','',MsgHead:@FM:MsgText)

IF OK THEN

	RDSNo	= Get_Property(@WINDOW:'.RDS_NO','DEFPROP')
	MetNo	= Get_Property(@WINDOW:'.MET_NO','DEFPROP')
	TWNo 	= Get_Property(@WINDOW:'.TW_NO','DEFPROP')

	TWUseKey = MetNo:'*':TWNo
	
	RRRec = XLATE('REACT_RUN',RDSNo,'','X')
	ReactType = XLATE('REACTOR',RRRec<REACT_RUN_REACTOR$>,1,'X')
	
	WONo	= WaferID[1,'.']
	CassNo	= WaferID[COL2()+1,'.']
	
	
	* * * *   Added 3/24/2016  JCH - wafer history   * * * * 
	
	WfrID = WaferID
	CONVERT '.' TO '*' IN WfrID
	
	Parms  = WfrID:@RM
	Parms := 'TEST'
	
	obj_WO_Wfr('RemEvent',Parms)
	
	* * * * *   End of wafer history update   * * * * *
	

	IF ReactType = 'EPP' THEN
		* * * EpiPRO Box  * * *
									***********************************************************
		obj_WM_In('RepProdTW',WONo:@RM:CassNo:@RM:MetNo:'*':TWNo:@RM:WaferID)

	END ELSE
		obj_WO_Mat('RepProdTW',WONo:@RM:CassNo:@RM:MetNo:'*':TWNo:@RM:WaferID)
	END

END ELSE
	Send_Message(CtrlEntID,'INSERT',RowIndex,WaferID)
END

RETURN


* * * * * * *
Sign:
* * * * * * *

Response    = Dialog_Box('NDW_VERIFY_USER', @WINDOW)
Valid       = Response<1>                  

IF Valid THEN

	CurrDTM = OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS')
	
	Set_Property(@WINDOW:'.SIGNATURE','DEFPROP',@USER4)
	Set_Property(@WINDOW:'.SIG_DTM','DEFPROP',CurrDTM)

	/*
	* From the RDS_TEST window code - save just in case they want it back! *
	CurrDTM			= ICONV(CurrDTM,'DT')
	ReactNo			= Get_Property(@WINDOW:'.REACTOR','DEFPROP')
	OutOfProdDTM	= XLATE('REACT_STATUS',ReactNo,REACT_STATUS_OUT_OF_PROD_DTM$,'X')
	HrsSinceProd 	= OCONV((CurrDTM - OutOfProdDTM)*24,'MD2')
	
	Parms = HrsSinceProd:@RM:CtrlEntId:@RM:ColPointer+2:@FM:LinePointer		;* Hours Since Production
	obj_Appwindow('LUValReturn',Parms )
	
	EscHrs = XLATE('REACTOR',ReactNo,'ACT_ESC_HRS','X')
	IF EscHrs = '' THEN EscHrs = 0
	EscElapsedTime	= OCONV(EscHrs,'MD2')
	
	Parms = EscElapsedTime:@RM:CtrlEntId:@RM:ColPointer+3:@FM:LinePointer	;* Hours in Escalation
	obj_Appwindow('LUValReturn',Parms )
	*/
END	

GOSUB Refresh

RETURN


* * * * * * *
WaferHelp:
* * * * * * *

Msg(@WINDOW,'','TW_USE_WAFER_HELP')

RETURN


