COMPILE FUNCTION obj_WO_Mat_Wfr(Method,Parms)

/*
	Methods for the WO_MAT_WFR (Wafer) Tables
	
	3/4/2016 JCH - Initial coding
	
	
*/

DECLARE FUNCTION Get_Status, Msg, obj_Tables, Send_Dyn, ErrMsg, obj_Tables, obj_WO_Wfr
DECLARE SUBROUTINE ErrMsg, Set_Status, FieldStore, obj_Tables, obj_WO_Wfr, RList, Send_Dyn

$Insert WO_MAT_WFR_EQUATES
$Insert WO_WFR_EQUATES
$Insert WO_MAT_EQUATES
$Insert WO_LOG_EQUATES
$Insert RLIST_EQUATES

ErrTitle = 'Error in Stored Procedure "obj_WO_Mat_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 = 'SetWfrID'		; Gosub SetWfrID
	Case Method = 'ClearWfrID'		; Gosub ClearWfrID
	CASE Method = 'GetWfrID'		; GOSUB GetWfrID
	Case Method = 'GetPreEpiWfrs'	; Gosub GetPreEpiWfrs
	Case Method = 'GetPostEpiSlots'	; Gosub GetPostEpiSlots
	Case Method = 'FixPrevWfr'		; Gosub FixPrevWfr
		
	CASE 1
		NULL

END CASE

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

RETURN Result


* * * * * * *
SetWfrID:
* * * * * * *

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

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

If ErrorMsg NE '' Then Return 

WOMatWfrKey = FIELD(SlotID,'*',1,2)
WOMatSlotNo	= FIELD(SlotID,'*',3)

otParms = 'WO_MAT_WFR':@RM:WOMatWfrKey
WOMatWfrRec = obj_Tables('ReadRec',otParms)


IF WfrSide = 'I' THEN
	WfrField		= WO_MAT_WFR_IN_WFR_ID$
	PrevWfrField	= WO_MAT_WFR_IN_PREV_WFR_ID$
END ELSE
	WfrField		= WO_MAT_WFR_OUT_WFR_ID$
	PrevWfrField	= WO_MAT_WFR_OUT_PREV_WFR_ID$
END

WOMatWfrRec<WfrField,WOMatSlotNo> = WfrID
WOMatWfrRec<PrevWfrField,WOMatSlotNo> = ''

otParms  = FieldStore(otParms,@RM,4,0,WOMatWfrRec)
obj_Tables('WriteRec',otParms)


Return



* * * * * * *
SetWfrIDs:
* * * * * * *

*** New 9/6/2016 JCH  * * * *

WOMatKey	= Parms[1,@RM]
SlotIDs		= Parms[COL2()+1,@RM]
WfrIDs		= Parms[COL2()+1,@RM]
WfrSide		= Parms[COL2()+1.@RM]

IF WOMatKey = '' 	THEN ErrorMsg = 'Null Parameter "WOMatKey" passed to routine. (':Method:')'
IF SlotIDs = '' 	THEN ErrorMsg = 'Null Parameter "SlotIDs" passed to routine. (':Method:')'
IF WfrIDs = ''		THEN ErrorMsg = 'Null Parameter "WfrIDs" passed to routine. (':Method:')'
IF WfrSide = ''		THEN ErrorMsg = 'Null Parameter "WfrSide" passed to routine. (':Method:')'


IF WfrSide = 'I' THEN
	WfrField		= WO_MAT_WFR_IN_WFR_ID$
	PrevWfrField	= WO_MAT_WFR_IN_PREV_WFR_ID$
END ELSE
	WfrField		= WO_MAT_WFR_OUT_WFR_ID$
	PrevWfrField	= WO_MAT_WFR_OUT_PREV_WFR_ID$
END

otParms = 'WO_MAT_WFR':@RM:WOMatWfrKey
WOMatWfrRec = obj_Tables('ReadRec',otParms)


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

FOR I = 1 TO wCnt
	SlotID = SlotIDs<1,I>
	WfrID	= WfrIDs<1,I>
	
	WOMatSlotNo	= FIELD(SlotID,'*',3)
	
	WOMatWfrRec<WfrField,WOMatSlotNo> = WfrID
	WOMatWfrRec<PrevWfrField,WOMatSlotNo> = ''
	
NEXT I

otParms  = FieldStore(otParms,@RM,4,0,WOMatWfrRec)
obj_Tables('WriteRec',otParms)



RETURN


* * * * * * *
ClearWfrID:
* * * * * * *

SlotID	= Parms[1,@RM]
WfrID	= Parms[COL2()+1,@RM]	;* Not used here
WfrSide	= Parms[COL2()+1,@RM]


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

If ErrorMsg NE '' Then Return 

Convert '.' To '*' In SlotID

WOMatWfrKey = Field(SlotID,'*',1,2)
WOMatSlotNo = Field(SlotID,'*',3,1)

otParms = 'WO_MAT_WFR':@RM:WOMatWfrKey
WOMatWfrRec = obj_Tables('ReadRec',otParms)


IF WfrSide = 'I' THEN
	WfrField		= WO_MAT_WFR_IN_WFR_ID$
	PrevWfrField	= WO_MAT_WFR_IN_PREV_WFR_ID$
END ELSE
	WfrField		= WO_MAT_WFR_OUT_WFR_ID$
	PrevWfrField	= WO_MAT_WFR_OUT_PREV_WFR_ID$
END

WOMatWfrRec<PrevWfrField,WOMatSlotNo> = WOMatWfrRec<WfrField,WOMatSlotNo>
WOMatWfrRec<WfrField,WOMatSlotNo> = ''

otParms  = FieldStore(otParms,@RM,4,0,WOMatWfrRec)
obj_Tables('WriteRec',otParms)

Return 



* * * * * * *
ClearWfrIDs:
* * * * * * *

*** New 9/6/2016 JCH  * * * *

WOMatKey	= Parms[1,@RM]
SlotIDs		= Parms[COL2()+1,@RM]
WfrIDs		= Parms[COL2()+1,@RM]
WfrSide		= Parms[COL2()+1.@RM]

IF WOMatKey = '' 	THEN ErrorMsg = 'Null Parameter "WOMatKey" passed to routine. (':Method:')'
IF SlotIDs = '' 	THEN ErrorMsg = 'Null Parameter "SlotIDs" passed to routine. (':Method:')'
IF WfrIDs = ''		THEN ErrorMsg = 'Null Parameter "WfrIDs" passed to routine. (':Method:')'
IF WfrSide = ''		THEN ErrorMsg = 'Null Parameter "WfrSide" passed to routine. (':Method:')'

Convert '.' To '*' In SlotIDs

IF WfrSide = 'I' THEN
	WfrField		= WO_MAT_WFR_IN_WFR_ID$
	PrevWfrField	= WO_MAT_WFR_IN_PREV_WFR_ID$
END ELSE
	WfrField		= WO_MAT_WFR_OUT_WFR_ID$
	PrevWfrField	= WO_MAT_WFR_OUT_PREV_WFR_ID$
END

otParms = 'WO_MAT_WFR':@RM:WOMatWfrKey
WOMatWfrRec = obj_Tables('ReadRec',otParms)


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

FOR I = 1 TO wCnt
	SlotID = SlotIDs<1,I>
	WfrID	= WfrIDs<1,I>
	
	WOMatSlotNo	= FIELD(SlotID,'*',3)
		
	WOMatWfrRec<PrevWfrField,WOMatSlotNo> = WOMatWfrRec<WfrField,WOMatSlotNo>
	WOMatWfrRec<WfrField,WOMatSlotNo> = ''
	
NEXT I

otParms  = FieldStore(otParms,@RM,4,0,WOMatWfrRec)
obj_Tables('WriteRec',otParms)


RETURN


* * * * * * * 
GetWfrID:
* * * * * * *

SlotID	= Parms[1,@RM]

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

If ErrorMsg NE '' Then Return 

Convert '.' To '*' In SlotID

WOMatWfrKey = Field(SlotID,'*',1,2)
WOMatSlotNo = Field(SlotID,'*',3,1)

WOMatWfrRec = XLATE('WO_MAT_WFR',WOMatWfrKey,'','X')

Result = WOMatWfrRec<WO_MAT_WFR_WFR_ID$,WOMatSlotNo>

Return


* * * * * * *
GetPreEpiWfrs:
* * * * * * * 

WONo		= Parms[1,@RM]
CassNos		= Parms[COL2()+1,@RM]

IF WONo = ''		THEN ErrorMsg = 'Null Parameter "WONo" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

IF CassNos = '' THEN
	CassNos = XLATE('WO_LOG',WONo,'WO_MAT_CASS_NO','X')
END

CassCnt = COUNT(CassNos,@VM) + (CassNos NE '')

* Start Counting at the last box and work backward

FOR I = CassCnt TO 1 STEP -1

	CassNo = CassNos<1,I>
	
	WOMatKey 	= WONo:'*':CassNo
	WOMatRec	= XLATE('WO_MAT' , WOMatKey , '' , 'X' )
	SlotCnt		= WOMatRec<WO_MAT_WAFER_QTY$>
	
	IF WOMatRec<WO_MAT_HOLD$> = 1  THEN
		NULL		;* Box is on hold - skip it
	END Else
		WOWfrIDs	= Xlate('WO_MAT_WFR',WONo:'*':CassNo,WO_MAT_WFR_IN_WFR_ID$,'X')
		
		
		FOR N = SlotCnt TO 1 STEP -1
		
			SlotID = WONo:'*':CassNo:'*':N
			
			If WOWfrIDs<1,N> = '' Then
				WfrID = ''
			End Else 
				WfrID = WOWfrIDs<1,N>
				Result = INSERT(Result,1,0,0,SlotID:@VM:WfrID)	;* Plate-o-lator (LIFO) style
			End	
			
		Next N
	END

NEXT I

RETURN



* * * * * * *
GetPostEpiSlots:
* * * * * * *

WONo		= Parms[1,@RM]
CassNos		= Parms[COL2()+1,@RM]

IF WONo = ''		THEN ErrorMsg = 'Null Parameter "WONo" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

IF CassNos = '' THEN
	CassNos = XLATE('WO_LOG',WONo,'WO_MAT_CASS_NO','X')
END

CassCnt = COUNT(CassNos,@VM) + (CassNos NE '')

* Start Counting at the last box and work backward

LoadQty = Xlate('WO_LOG',WONo,WO_LOG_OUT_CASS_LOAD_QTY$,'X')

FOR I = CassCnt TO 1 STEP -1
	
	CassNo = CassNos<1,I>
	
	WOMatKey 	= WONo:'*':CassNo
	WOMatRec	= XLATE('WO_MAT' , WOMatKey , '' , 'X' )
	SlotCnt		= WOMatRec<WO_MAT_CASS_SHIP_QTY$>
	
	If SlotCnt = '' THEN
		SlotCnt = WOMatRec<WO_MAT_WAFER_QTY$>
	END
	
	WOMatWfrRec = XLATE('WO_MAT_WFR',WONo:'*':CassNo,'','X')
	WfrKeys		= WOMatWfrRec<WO_MAT_WFR_OUT_WFR_ID$>
	PrevWfrKeys = WOMatWfrRec<WO_MAT_WFR_OUT_PREV_WFR_ID$>
	
	FOR N = SlotCnt TO 1 STEP -1
	
		SlotID = WOMatKey:'*':N
		
		If WfrKeys<1,N> = '' Then
			PrevDisp = obj_WO_WFr('Disp',WOMatWfrRec<WO_MAT_WFR_OUT_PREV_WFR_ID$,N>)[1,' ']	;* Just need the 'EVENT' at the front of the text
		
			IF PrevDisp NE 'NCR' AND PrevDisp NE 'MKUP' THEN
				*IF NOT(RowExists('WMO_WFRS',WONo:'*':WOStep:'*':CassNo:'*':SlotNo)) THEN			;* Check for 'LOCK' in WMO_WFRS table
				Result = INSERT(Result,1,0,0,SlotID)	;* Plate-o-lator (LIFO) style	
			END
		END
		
	NEXT N
NEXT I

RETURN



* * * * * * *
FixPrevWfr:
* * * * * * *

/*

OPEN 'WO_WFR' TO WOWfrTable ELSE
	DEBUG
	RETURN
END

OPEN 'WO_MAT_WFR' TO WOMatWfrTable ELSE
	DEBUG
	RETURN
END


*RList('SELECT WO_WFR WITH REACT_TYPE = "EPP"',TARGET_ACTIVELIST$)

RList('SELECT WO_WFR WITH WO_NO 162188 AND WITH CASS_NO >= 10',TARGET_ACTIVELIST$)


IF Get_Status(errCode) THEN DEBUG


Done = 0
RecNo = 0
FixCnt = 0
LOOP
	READNEXT WOWfrKey ELSE Done = 1
UNTIL Done 
	READ WOWfrRec FROM WOWfrTable,WOWfrKey THEN
		RecNo += 1
		
		Events = WOWfrRec<WO_WFR_LOC_EVENT$>
		eCnt = COUNT(Events,@VM) + (Events NE '')
		
		
		FOR I = 1 TO eCnt
		
			Event = Events<1,I>
			
			IF Event = 'LOAD' OR Event = 'TEST' OR Event = 'REPLACE' THEN
				WfrSide = 'I'
			END ELSE
				WfrSide = 'O'
			END
			
			
			Parms  = WOWfrKey:@RM										;* WfrID
			Parms := WOWfrRec<WO_WFR_LOC_DTM$,I>:@RM					;* EventDtm
			Parms := WOWfrRec<WO_WFR_LOC_BY$,I>:@RM						;* EventBy
			Parms := WOWfrRec<WO_WFR_LOC_EVENT$,I>:@RM					;* Event
			Parms := WOWfrRec<WO_WFR_SLOT_ID$,I>:@RM					;* NewSlotID
			Parms := WOWfrRec<WO_WFR_RUN_LOC$,I:@RM						;* RunLoc
			Parms := WOWfrRec<WO_WFR_NCR_NO$,I>:@RM						;* NCRNo
			Parms := WOWfrRec<WO_WFR_TW_USE$,I>:@RM						;* TWUse
			Parms := WOWfrRec<WO_WFR_TOOL_ID$,I>:@RM					;* CurrSlotID
			Parms := WOWfrRec<WO_WFR_INV_LOC$,I>:ReactNo:@RM			;* NewToolID (Load Tool)
			Parms := WOWfrRec<,I:@RM									;* CurrToolID
			Parms := WOWfrRec<,I:@RM									;* NewInvLoc
			Parms := WOWfrRec<,I:@RM									;* CurrInvLoc
			Parms := WfrSide											;* Wfr Side


		*obj_WO_Wfr('AddEvent',Parms)
			
			
			
			
			
		
		
		NEXT I
		
				*otParms = 'WO_MAT_WFR':@RM:WOMatWfrKey
				*WOMatWfrRec = obj_Tables('ReadRec',otParms)
				
				*IF WOMatWfrRec<WO_MAT_WFR_WFR_ID$,WOMatSlotNo> = '' THEN
				*	WOMatWfrRec<WO_MAT_WFR_PREV_WFR_ID$,WOMatSlotNo> = PrevWfrID
				*	FixCnt += 1
				*END
				

				*otParms  = FieldStore(otParms,@RM,4,0,WOMatWfrRec)
				*obj_Tables('WriteRec',otParms)
				
				Send_Dyn(RecNo:' ':FixCnt:' ':WOMatWfrKey:' ':WOMatSlotNo:' -> ':PrevWfrID:' ':LastEvent)
				
			END
			
		END	;* End of check for multiple lines	
		
	End	;* End of record read
	
Repeat
*/
RETURN






