COMPILE FUNCTION obj_WO_Wfr_Test(Method,Parms)

/*
	Methods for the WO_WFR (Wafer) Tables
	
	3/1/2016 JCH - Initial coding
	
	
*/

DECLARE FUNCTION Get_Status, Msg, obj_Tables, Send_Dyn, ErrMsg, obj_Tables, Reconcile_Epi_WO, obj_WO_Mat_Wfr
DECLARE SUBROUTINE ErrMsg, Set_Status, Send_Dyn, FieldStore, obj_Tables, obj_WO_Wfr, Clear_Table, obj_WO_Mat_Wfr, Set_Property
Declare Subroutine Rlist, obj_Tool_Wfr, obj_Location, obj_Notes

$INSERT WO_LOG_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT WM_IN_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT RDS_EQUATES
$INSERT WO_WFR_EQUATES
$Insert WO_MAT_WFR_EQUATES
$Insert TW_USE_EQUATES
$Insert NCR_EQUATES
$Insert RLIST_Equates

Equ CRLF$	To \0D0A\


EQU WAFER_ID$		TO 1		;* Columns in data structure returned by Reconcile_Epi_Pro
EQU LOT_NO$			TO 2
EQU LOADED_INTO$	TO 3
EQU RAN_IN$			TO 4
EQU POCKET_CHAR$	TO 5
EQU UNLOADED_INTO$	TO 6
EQU WAFER_DISP$		TO 7


ErrTitle = 'Error in Stored Procedure "obj_WO_Wfr"'
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 = 'CassRel'			; Gosub CassRel
	Case Method = 'AddEvent'		; Gosub AddEvent
	Case Method = 'RemEvent'		; Gosub RemEvent
	Case Method = 'CurrLoc'			; Gosub CurrLoc
	Case Method = 'GetET'			; Gosub GetET
	CASE Method = 'Convert'			; Gosub Convert
	CASE Method = 'Disp'			; Gosub Disp
	Case Method = 'CassDel'			; Gosub CassDel
	CASE Method = 'LoadFromSlot'	; Gosub LoadFromSlot
	CASE Method = 'UnloadIntoSlot'	; Gosub UnloadIntoSlot
	CASE Method = 'InSlotDisp'		; Gosub InSlotDisp
	CASE Method = 'OutSlotDisp'		; Gosub OutSlotDisp
	
	
	CASE 1
		NULL

END CASE

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

RETURN Result

* * * * * * *
CassRel:
* * * * * * *

WOMatKey = Parms[1,@RM]

If WOMatKey = '' Then Return

WfrQty	= Xlate('WO_MAT',WOMatKey,WO_MAT_WAFER_QTY$,'X')
CurrDTM =  OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS')

For I = 1 To WfrQty
	owwParms	 = WOMatKey:'*':I:@RM			;* WfrID
	owwParms	:= CurrDTM:@RM					;* EventDtm
	owwParms	:= @USER4:@RM					;* EventBy
	owwParms	:= 'REL':@RM					;* Event
	owwParms	:= WOMatKey:'*':I:@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	:= '':@RM						;* Curr Tool ID
	owwParms 	:= '':@RM						;* NewInvLoc
	owwParms	:= '':@RM						;* CurrInvLoc
	owwParms 	:= 'I'							;* Wfr Side
		
	obj_WO_Wfr('AddEvent',owwParms)
Next I

Return 


* * * * * * * 
AddEvent:
* * * * * * *

WfrIDs		= Parms[1,@RM]
EventDtm	= Parms[COL2()+1,@RM]
EventBy		= Parms[COL2()+1,@RM]
Event		= Parms[COL2()+1,@RM]
NewSlotID	= Parms[COL2()+1,@RM]
RunLoc		= Parms[COL2()+1,@RM]
NCRNo		= Parms[COL2()+1,@RM]
TWUse		= Parms[COL2()+1,@RM]
CurrSlotID	= Parms[COL2()+1,@RM]
NewToolID	= Parms[COL2()+1,@RM]
CurrToolID	= Parms[COL2()+1,@RM]
NewInvLoc	= Parms[COL2()+1,@RM]
CurrInvLoc	= Parms[COL2()+1,@RM]
WfrSide		= Parms[COL2()+1,@RM]
NewCarrLoc	= Parms[COL2()+1,@RM]	;* Added 6/20/2016 JCH
CurrCarrLoc	= Parms[COL2()+1,@RM]	;* Added 6/20/2016 JCH

If WfrIDs = ''		THEN ErrorMsg = 'Null Parameter "WfrIDs" passed to routine. (':Method:')'
*If EventDTM = ''	THEN ErrorMsg = 'Null Parameter "EventDTM" passed to routine. (':Method:')'
*If EventBy = ''	THEN ErrorMsg = 'Null Parameter "EventBy" passed to routine. (':Method:')'
If Event = ''		THEN ErrorMsg = 'Null Parameter "EventEvent" passed to routine. (':Method:')'

If EventDTM NE '' THEN
	thisEventDTM = ICONV(EventDTM,'DT')
	If thisEventDTM = '' THEN ErrorMsg = 'Invalid Parameter EventDTM ':Quote(EventDTM):' passed to routine. (':Method:')'

	If ErrorMsg NE '' Then RETURN
End

CONVERT '.' TO '*' IN NewSlotID
CONVERT '.' TO '*' IN CurrSlotID

IF NewSlotID NE '' THEN
	NewSlotIDCassNo = FIELD(NewSlotID,'*',2)

	IF NewSlotIDCassNo = '' THEN
		DEBUG
		RETURN
	END
END



otParms = 'WO_WFR':@RM:WfrID
WfrRec = obj_Tables('ReadRec',otParms)

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


If EventDTM = '' Then
	
	ExistDTMs = WfrRec<WO_WFR_LOC_DTM$>
	
	IF ExistDTMs = '' THEN
		
		thisEventDTM =  ICONV( OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT' )
		
	END ELSE
		Loop
			TestChar = ExistDTMs[-1,1]
		Until TestChar NE @VM Or ExistDTMs = ''
			ExistDTMs[-1,1] = ''
		Repeat 
		LastDTM = ExistDTMs[-1,'B':@VM]
		thisEventDTM = LastDTM + ( 1/24 )		;* Fake a time for this 1 hour after last transaction.
		
	END	;* End check for Existing DTMs
	
End 

If EventBy = '' Then 
	EventBy = 'OI'							;* Admin user
End 
	

Locate thisEventDTM In WfrRec<WO_WFR_LOC_DTM$> BY 'AR' SETTING Pos THEN

	obj_Tables('UnlockRec',otParms)
	
	IF Get_Status(ErrCode) THEN 
		ErrMsg(errCode)
		DEBUG
	END

END ELSE


	DestTest = XLATE('TOOl',NewToolID,'DEST_TEST','X')	;* Tool Start on destructive test
	
	IF DestTest THEN

		
		IF WfrRec<WO_WFR_BROKEN$> = 1 THEN
		
			obj_Tables('UnlockRec',otParms)								;* Wafer already flagged as broken
			
		END ELSE

			
			WfrRec<WO_WFR_BROKEN$> = 1									;* Flag the wafer as broken
		
			WfrRec = Insert(WfrRec,WO_WFR_LOC_DTM$,Pos,0,thisEventDTM)
			WfrRec = Insert(WfrRec,WO_WFR_LOC_BY$,Pos,0,EventBy)
			WfrRec = Insert(WfrRec,WO_WFR_LOC_EVENT$,Pos,0,'DEST')		;* Wafer flagged as destroyed
			WfrRec = Insert(WfrRec,WO_WFR_SLOT_ID$,Pos,0,NewSlotID)
			WfrRec = Insert(WfrRec,WO_WFR_RUN_LOC$,Pos,0,RunLoc)
			WfrRec = Insert(WfrRec,WO_WFR_NCR_NO$,Pos,0,NCRNo)
			WfrRec = Insert(WfrRec,WO_WFR_TW_USE$,Pos,0,TWUse)
			WfrRec = Insert(WfrRec,WO_WFR_TOOL_ID$,Pos,0,'')			;* Wafer not logged 
			WfrRec = Insert(WfrRec,WO_WFR_INV_LOC$,Pos,0,NewInvLoc)		;* This is blank in this case
			WfrRec = Insert(WfrRec,WO_WFR_WFR_SIDE$,Pos,0,WfrSide)		;* Added 5/28/2016 JCH
			WfrRec = Insert(WfrRec,WO_WFR_CARR_SLOT$,Pos,0,NewCarrLoc)	;* Added 6/20/2016 JCH
			
			otParms  = FieldStore(otParms,@RM,4,0,WfrRec)
			obj_Tables('WriteRec',otParms)
			
			IF Get_Status(ErrCode) THEN 
				ErrMsg(errCode)
				DEBUG
			END
			
		END
		
	
	END ELSE

		WfrRec = Insert(WfrRec,WO_WFR_LOC_DTM$,Pos,0,thisEventDTM)
		WfrRec = Insert(WfrRec,WO_WFR_LOC_BY$,Pos,0,EventBy)
		WfrRec = Insert(WfrRec,WO_WFR_LOC_EVENT$,Pos,0,Event)
		WfrRec = Insert(WfrRec,WO_WFR_SLOT_ID$,Pos,0,NewSlotID)
		WfrRec = Insert(WfrRec,WO_WFR_RUN_LOC$,Pos,0,RunLoc)
		WfrRec = Insert(WfrRec,WO_WFR_NCR_NO$,Pos,0,NCRNo)
		WfrRec = Insert(WfrRec,WO_WFR_TW_USE$,Pos,0,TWUse)
		WfrRec = Insert(WfrRec,WO_WFR_TOOL_ID$,Pos,0,NewToolID)
		WfrRec = Insert(WfrRec,WO_WFR_INV_LOC$,Pos,0,NewInvLoc)
		WfrRec = Insert(WfrRec,WO_WFR_WFR_SIDE$,Pos,0,WfrSide)		;* Added 5/28/2016 JCH
		WfrRec = Insert(WfrRec,WO_WFR_CARR_SLOT$,Pos,0,NewCarrLoc)	;* Added 6/20/2016 JCH
		
		otParms  = FieldStore(otParms,@RM,4,0,WfrRec)
		obj_Tables('WriteRec',otParms)
		
		IF Get_Status(ErrCode) THEN 
			ErrMsg(errCode)
			DEBUG
		END
		
	END	; * end of check for destructive test
	
	BEGIN CASE
		CASE NewSlotID = '' AND CurrSlotID NE ''
			obj_WO_Mat_Wfr('ClearWfrID',CurrSlotID:@RM:@RM:WfrSide)			;* Removes wafer from slot in WO_MAT_WFR record
			
		CASE NewSlotID NE '' AND CurrSlotID NE ''
			obj_WO_Mat_Wfr('SetWfrID',NewSlotID:@RM:WfrID:@RM:WfrSide)		;* Adds Wafer ID to NewSlotID in WO_MAT_WFR table
			obj_WO_Mat_Wfr('ClearWfrID',CurrSlotID:@RM:@RM:WfrSide)			;* Removes wafer from CurrSlotIDin WO_MAT_WFR record
			
		CASE NewSlotID NE '' AND CurrSlotID = ''
			obj_WO_Mat_Wfr('SetWfrID',NewSlotID:@RM:WfrID:@RM:WfrSide)		;* Adds Wafer ID to NewSlotID in WO_MAT_WFR table
			
		CASE 1
			NULL
			
	END Case
	
	
	BEGIN CASE
		CASE NewToolID = '' AND CurrToolID NE ''
			obj_Tool_Wfr('ClearWfrID',CurrToolID:@RM:WfrID)		;* Removes wafer from Tool_WFR record
			
		CASE NewToolID NE '' AND CurrToolID NE ''
			obj_Tool_Wfr('SetWfrID',NewToolID:@RM:WfrID)		;* Adds Wafer ID to NewToolID IN TOOL_WFR table
			obj_Tool_Wfr('ClearWfrID',CurrToolID)				;* Removes wafer from CurrToolID in TOOL_WFR record
			
		CASE NewToolID NE '' AND CurrToolID = ''
			obj_Tool_Wfr('SetWfrID',NewToolID:@RM:WfrID)		;* Adds Wafer ID to NewTOOLID IN TOOL_WFR table
			
		CASE 1
			NULL
			
	END Case
	
	
	BEGIN CASE
		CASE NewInvLoc = '' AND CurrInvLoc NE ''
			obj_Location('ClearWfrID',CurrInvLoc:@RM:WfrID)		;* Removes wafer from Location record
			
		CASE NewInvLoc NE '' AND CurrInvLoc NE ''
			obj_Location('SetWfrID',NewInvLoc:@RM:WfrID)		;* Adds Wafer ID to WFR_ID field IN LOCATION table
			obj_Location('ClearWfrID',CurrInvLoc)				;* Removes wafer from WFR_ID field in LOCATION record
			
		CASE NewInvLoc NE '' AND CurrInvLoc = ''
			obj_Location('SetWfrID',NewInvLoc:@RM:WfrID)		;* Adds Wafer ID to WFR_ID field IN LOCATION table
			
		CASE 1
			NULL
			
	END Case
	
		
			
END
	
Return


* * * * * * * 
RemEvent:
* * * * * * *

* * *   Removes NCR or TEST from history if they are the last event recorded   * * *

WfrID		= Parms[1,@RM]
Event		= Parms[COL2()+1,@RM]
EventID		= Parms[COL2()+1,@RM]

If WfrID = ''	THEN ErrorMsg = 'Null Parameter "WfrID" passed to routine. (':Method:')'
If Event = ''	THEN ErrorMsg = 'Null Parameter "Event" passed to routine. (':Method:')'

If ErrorMsg NE '' Then Return

If Event NE 'NCR' And Event NE 'TEST' Then
	ErrorMsg = 'Only NCR or TEST events may be removed. Event = ':QUOTE(Event)
	Return 
END
	
otParms = 'WO_WFR':@RM:WfrID
WfrRec = obj_Tables('ReadRec',otParms)

IF WfrRec = '' THEN 
	obj_Tables('UnlockRec',otParms)
	RETURN
END

BEGIN CASE

	CASE Event = 'NCR' 		; EventFieldNo = WO_WFR_NCR_NO$
	CASE Event = 'TEST'		; EventFieldNo = WO_WFR_TW_USE$
	CASE Event = ''
	
		*otParms = 'WO_WFR':@RM:WfrID
		obj_Tables('UnlockRec',otParms)
		RETURN
		
END CASE

Locate EventID In WfrRec<EventFieldNo> USING @VM SETTING Pos Then
	
	NewSlotID = WfrRec<WO_WFR_SLOT_ID$,Pos - 1>
	
	If NewSlotID NE '' THEN
		obj_WO_Mat_Wfr('SetWfrID',NewSlotID:@RM:WfrID)	;* Adds Wafer ID to NewSlotID in WO_MAT_WFR table
	End
	
	WfrRec = Delete(WfrRec,WO_WFR_LOC_DTM$,Pos,0)
	WfrRec = Delete(WfrRec,WO_WFR_LOC_BY$,Pos,0)
	WfrRec = Delete(WfrRec,WO_WFR_LOC_EVENT$,Pos,0)
	WfrRec = Delete(WfrRec,WO_WFR_SLOT_ID$,Pos,0)
	WfrRec = Delete(WfrRec,WO_WFR_RUN_LOC$,Pos,0)
	WfrRec = Delete(WfrRec,WO_WFR_NCR_NO$,Pos,0)
	WfrRec = Delete(WfrRec,WO_WFR_TW_USE$,Pos,0) 
	
	otParms = FieldStore(otParms,@RM,4,1,WfrRec)
	obj_Tables('WriteRec',otParms)
	
End Else

	*otParms = 'WO_WFR':@RM:WfrID
	obj_Tables('UnlockRec',otParms)

End

Return 


* * * * * * *
CurrLoc:
* * * * * * *

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

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

IF WfrRec = '' THEN WfrRec = XLATE('WO_WFR',WfrID,'','X')

WfrEvents = WfrRec<WO_WFR_LOC_EVENT$>

CurrLoc = ''
weCnt = Count(WfrEvents,@VM) + (WfrEvents NE '')

For M = WeCnt To 1 STEP -1
	WfrEvent	= WfrEvents<1,M>
	WOMatLoc	= WfrRec<WO_WFR_SLOT_ID$,M>
	ToolLoc		= WfrRec<WO_WFR_TOOL_ID$,M>
	InvIDLoc	= WfrRec<WO_WFR_INV_LOC$,M>
	
	If WOMatLoc NE '' Then CurrLoc<1,1>	= Field(WoMatLoc,'*',1,2)
	If ToolLoc NE '' Then CurrLoc<1,2>	= ToolLoc
	If InvIDLoc NE '' Then CurrLoc<1,3>	= InvIDLoc
	
While CurrLoc = ''	
Next M

Result = CurrLoc

RETURN


* * * * * * *
GetET:
* * * * * * *

* Returns time in Hours since last event happened

WfrID	= Parms[1,@RM]
WfrRec	= Parms[COL2()+1,@RM]

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

IF WfrRec = '' THEN WfrRec = XLATE('WO_WFR',WfrID,'','X')

LocDTMs = WfrRec<WO_WFR_LOC_DTM$>

LocCnt = COUNT(LocDTMs,@VM) + (LocDTMs NE '')

LastDTM = LocDTMs<1,LocCnt>

IF LastDTM = '' THEN RETURN		;* Bail

CurrDTM = OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTH')
CurrDTM = ICONV(CurrDTM , 'DT' )

IF CurrDTM > LastDTM THEN
	Result = OCONV(ICONV( (CurrDTM - LastDTM)*24 , 'MD1'), 'MD1')		;* Elapsed time in hours to 1 decimal precision
END

RETURN







* * * * * * *
Disp:
* * * * * * *

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

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

IF WfrRec = '' THEN WfrRec = XLATE('WO_WFR',WfrID,'','X')

WfrEvents = WfrRec<WO_WFR_LOC_EVENT$>

evCnt = COUNT(WfrEvents,@VM) + ( WfrEvents NE '' )


Loaded		= INDEX(WfrEvents,'LOAD',1)
Unloaded	= INDEX(WfrEvents,'UNLOAD',1)

BEGIN CASE
	
	CASE NOT(Loaded)
		EpiStage = 'Pre'
		
	CASE Loaded AND NOT(Unloaded)
		EpiStage ='React'
		
	CASE Loaded AND Unloaded
		EpiStage = 'Post'
		
END CASE

LastEvent = WfrRec<WO_WFR_LOC_EVENT$,evCnt>

BEGIN CASE

	CASE LastEvent = 'TEST'
		TWKey = WfrRec<WO_WFR_TW_USE$,evCnt>
		IF TWKey = '' THEN TWKey = '***'
		Disp = 'TEST -> ':TWKey
		
	CASE LastEvent = 'NCR'
		Disp = 'NCR':' - ':WfrRec<WO_WFR_NCR_NO$,evCnt>:' ':WfrRec<WO_WFR_SLOT_ID$,evCnt-1>
		
	CASE LastEvent = 'UNLOAD'
		Disp = 'UNLOAD -> ':WfrRec<WO_WFR_SLOT_ID$,evCnt>
		
	CASE LastEvent = 'LOAD'
		Disp = 'LOAD -> ':WfrRec<WO_WFR_RUN_LOC$,evCnt>
		
	CASE LastEvent = 'MKUP'
		Disp = 'MKUP -> ':WfrRec<WO_WFR_SLOT_ID$,evCnt>
		
	CASE LastEvent = 'MOVE'
		Disp = 'MOVE -> ':WfrRec<WO_WFR_SLOT_ID$,evCnt>
		
	CASE LastEvent = 'REPLACE'
		Disp = 'REPLACE -> ':WfrRec<WO_WFR_SLOT_ID$,evCnt>
		
	CASE LastEvent = 'RETURN'
		Disp = 'RETURN -> ':WfrRec<WO_WFR_RUN_LOC$,evCnt>
		
	CASE 1
		Disp = 'PROD - ':WfrID
	


END CASE

Result = Disp

RETURN


* * * * * * *
CassDel:
* * * * * * * 

WOMatKey = Parms[1,@RM]

If WOMatKey = '' Then Return

WfrQty = Xlate('WO_MAT',WOMatKey,WO_MAT_WAFER_QTY$,'X')

otParms = 'WO_MAT_WFR':@RM:WOMatKey
obj_Tables('DeleteRec',otParms)

For I = 1 To WfrQty
	otParms = 'WO_WFR':@RM:WOMatKey:'*':I
	obj_Tables('DeleteRec',otParms)
Next I

Return 


* * * * * * *
InSlotDisp:
* * * * * * *

WOWfrKey = Parms[1,@RM]
WOWfrRec = Parms[COL2()+1,@RM]


IF WOWfrKey = ''		THEN RETURN		;* Called from dictionary -> no messages

IF WOWfrRec = '' THEN
	WOWfrRec = XLATE('WO_WFR',WOWfrKey,'','X')
END

IF WOWfrRec = '' THEN RETURN

eCnt = COUNT(WOWfrRec<WO_WFR_LOC_EVENT$>,@VM) + (WOWfrRec<WO_WFR_LOC_EVENT$> NE '')

LoadPOs 	= ''
NCRPos		= ''
TestPos		= ''
UnloadPos	= ''

FOR I = eCnt TO 1 STEP -1
	
	Event = WOWfrRec<WO_WFR_LOC_EVENT$,I>
	IF Event = 'LOAD'	AND LoadPos = '' THEN LoadPos = I
	IF Event = 'NCR'	AND NCRPos = '' THEN NCRPos = I
	IF Event = 'TEST'	AND TestPos = '' THEN TestPos = I
	IF Event = 'UNLOAD' AND UnloadPos = '' THEN UnloadPos = I

UNTIL LoadPos AND NCRPos AND TestPos AND UnloadPos
		
NEXT I

BEGIN CASE
	CASE NCRPos AND LoadPos = ''	; Result = 'PreEpi NCR - ':WOWfrRec<WO_WFR_NCR_NO$,NCRPos>
	CASE TestPos NE '' 				; Result = 'Prod Test - ':WOWfrRec<WO_WFR_TW_USE$,TestPos>
	CASE LoadPos NE ''				; Result = 'Load - ':WOWfrREc<WO_WFR_TOOL_ID$,LoadPos>:' ':WOWfrRec<WO_WFR_RUN_LOC$,LoadPos>
	CASE 1
			NULL
END CASE


RETURN


* * * * * * *
OutSlotDisp:
* * * * * * *

WOWfrKey = Parms[1,@RM]
WOWfrRec = Parms[COL2()+1,@RM]


IF WOWfrKey = ''		THEN RETURN		;* Called from dictionary -> no messages

IF WOWfrRec = '' THEN
	WOWfrRec = XLATE('WO_WFR',WOWfrKey,'','X')
END

IF WOWfrRec = '' THEN RETURN

eCnt = COUNT(WOWfrRec<WO_WFR_LOC_EVENT$>,@VM) + (WOWfrRec<WO_WFR_LOC_EVENT$> NE '')

MkupPos 	= ''
NCRPos		= ''
UnloadPos	= ''

FOR I = eCnt TO 1 STEP -1
	
	Event = WOWfrRec<WO_WFR_LOC_EVENT$,I>
	IF Event = 'MKUP'	AND MkupPos = '' THEN MkupPos = I
	IF Event = 'NCR'	AND NCRPos = '' THEN NCRPos = I
	IF Event = 'UNLOAD' AND UnloadPos = '' THEN UnloadPos = I
	
UNTIL UnloadPos AND NCRPos AND MkupPos
		
NEXT I

BEGIN CASE
	CASE NCRPos AND UnLoadPos NE ''	; Result = 'NCR Post Epi - ':WOWfrRec<WO_WFR_NCR_NO$,NCRPos>
	CASE MkupPos NE '' 				; Result = 'UMW - ':WOWfrRec<WO_WFR_SLOT_ID$,MkupPos>
	CASE 1
			NULL
END CASE


RETURN




* * * * * * * 
Convert:
* * * * * * *

*CLEAR_TABLE('WO_WFR')
*CLEAR_TABLE('WO_MAT_WFR')

WONos = '161854'


wCnt = COUNT(WONos,@VM) + (WONos NE '')

FOR I = 1 TO wCnt
	WONo = WONos<1,I>
	EpiProFlag = Xlate('WO_LOG',WONo,'EPI_PRO_FLAG','X')
	
	If EpiProFlag THEN
		CALL Convert_Epi_WO(WONo)
	End Else
		Gosub ConvertASM
	End 
NEXT I

RETURN





* * * * * * *
ConvertASM:
* * * * * * *

Open 'WO_MAT' To WOMatTable Else
	ErrMsg(ErrorMsg)
	Return 
End 

SelectSent = 'SELECT WO_MAT WITH WO_NO = ':QUOTE(WONo):' BY CASS_NO'

Set_Status(0)
RList(SelectSent,TARGET_ACTIVELIST$,'','','')
IF Get_Status(errCode) THEN
	ErrMsg(errCode)
	Return
END

Done = 0
LOOP
	READNEXT WOMatKey ELSE Done = 1
UNTIL Done
	
	WOMatRec = Xlate('WO_MAT',WOMatKey,'','X')
	
	WONo	= WOMatKey[1,'*']
	CassNo	= WOMatKey[COL2()+1,'*']
	
	SlotNos = WOMatRec<WO_MAT_SLOT_NO$>
	sCnt = Count( SlotNos,@VM ) + ( SlotNos NE '' )
	
	For I = 1 To sCnt
		SlotNo		= SlotNos<1,I>
		NCRNo 		= WOMatRec<WO_MAT_SLOT_NCR$,I>
		TWUseKey	= WOMatRec<WO_MAT_SLOT_MET_NO$,I>
		RepWfrID	= WOMatRec<WO_MAT_SLOT_REP_WAFER_ID$,I>
		
		WfrID = WONo:'*':CassNo:'*':SlotNo
		CurrWfrRec = Xlate('WO_WFR',WfrID,'','X')
		
		If NCRNo NE '' Then
		
			Locate NCRNo In WOMatRec<WO_MAT_NCR_KEYS$> BY 'AR' SETTING NPos Then
				
				NCRBy = WOMatRec<WO_MAT_NCR_FINAL_SIG$,NPos>
				NCRDTM = WOMatRec<WO_MAT_NCR_FINAL_SIG_DTM$,NPos>
				
				CurrWfrRec = XLATE('WO_WFR',WfrID,'','X')
				
				LOCATE NCRNo IN CurrWfrRec<WO_WFR_NCR_NO$> SETTING cwPos ELSE
				
					owwParms  = WfrID:@RM						;* WfrID				
					owwParms := Oconv(NCRDtm,'DT4/^S'):@RM		;* Event Dtm
					owwParms := NCRBy:@RM						;* Event By
					owwParms := 'NCR':@RM						;* Event
					owwParms := '':@RM							;* NewSlotID
					owwParms := '':@RM							;* RunID (RPZ)
					owwParms := NCRNo:@RM						;* NCRNo
					owwParms := '':@RM							;* TwID
					owwParms := WfrID							;* CurrSlotID
								
					obj_WO_Wfr('AddEvent',owwParms)
					
				END 	;* End of check for NCRNo already in WO_WFR record
				
			End	;* End of locating NCRNo IN WOMatRec field
			
		End	;* End of check for NCR No
		
		If TWUseKey NE '' Then
				
			LOCATE 'TEST' IN CurrWfrRec<WO_WFR_LOC_EVENT$> SETTING twPos ELSE
					
				TWUseRec = XLATE('TW_USE',TWUseKey,'','X')
				
				TWSig		= TWUseRec<TW_USE_SIGNATURE$>
				TWSigDtm	= TWUseRec<TW_USE_SIG_DTM$>
					
				owwParms  = WfrID:@RM						;* WfrID				
				owwParms := Oconv(TWSigDTM,'DT4/^S'):@RM	;* Event Dtm
				owwParms := TWSig:@RM						;* Event By
				owwParms := 'TEST':@RM						;* Event
				owwParms := '':@RM							;* NewSlotID
				owwParms := '':@RM							;* RunID (RPZ)
				owwParms := '':@RM							;* NCRNo
				owwParms := TWUseKey:@RM					;* TWUseKey
				owwParms := WfrID							;* CurrSlotID
							
				obj_WO_Wfr('AddEvent',owwParms)
				
			END		;* End of check for 'TEST' Event already in WO_WFR Record
		End	;* End of check for TWUseKey
		
		If RepWfrID NE '' Then
			Convert '.' To '*' In RepWfrID
			
			LOCATE WfrID IN CurrWfrRec<WO_WFR_SLOT_ID$> SETTING Dummy ELSE
	
				owwParms  = RepWfrID:@RM					;* WfrID				
				owwParms := '':@RM							;* Event Dtm
				owwParms := '':@RM							;* Event By
				owwParms := 'MKUP':@RM						;* Event
				owwParms := WfrID:@RM						;* NewSlotID
				owwParms := '':@RM							;* RunID (RPZ)
				owwParms := '':@RM							;* NCRNo
				owwParms := '':@RM							;* TwID
				owwParms := RepWfrID						;* CurrSlotID
							
				obj_WO_Wfr('AddEvent',owwParms)
				
			END	;* End of new Slot ID already in the existing WO_WFR Rec
			
		End ;* End of check for RepWfrID
	
	Next I
		
REPEAT

RETURN





* * * * * * * 
LoadFromSlot:
* * * * * * *
DEBUG


RETURN



* * * * * * *
UnloadIntoSlot:
* * * * * * *
DEBUG

RETURN





