COMPILE FUNCTION Convert_EPI_WO(WONo,ReturnArray)

DECLARE SUBROUTINE ErrMsg, RList, Set_Status, obj_WO_Wfr
DECLARE FUNCTION Set_Status, Popup, obj_WO_Mat_Wfr

$Insert WO_LOG_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT WM_IN_EQUATES
$INSERT REACT_RUN_EQUATES
$INSERT WO_WFR_EQUATES
$INSERT NCR_EQUATES
$INSERT RDS_EQUATES
$INSERT TW_USE_EQUATES
$INSERT RLIST_EQUATES


IF NOT(Assigned(ReturnArray)) THEN ReturnArray = ''

ErrorMsg = ''
Result = ''

IF NOT(ASSIGNED(WONo))	THEN ErrorMsg = 'Null parameter passed to routine.'
IF WONo = ''			THEN ErrorMsg = 'Null WONo passed to routine.'

WORec = XLATE('WO_LOG',WONo,'','X')

IF WORec = '' THEN ErrorMsg = 'WONo ':QUOTE(WOno):' is not on file!'

IF ErrorMsg NE '' THEN
	ErrMsg(ErrorMsg)
	GOTO Bail
END

OPEN 'WM_IN' TO WMITable ELSE
	ErrMsg('Unable to open WM_IN table.')
	GOTO Bail
END

OPEN 'WM_OUT' TO WMOTable ELSE
	ErrMsg('Unable to open WM_OUT table.')
	GOTO Bail
END

OPEN 'REACT_RUN' TO RRTable ELSE
	ErrMsg('Unable to open REACT_RUN table.')
	GOTO Bail
END

OPEN 'WO_WFR' TO WfrTable ELSE
	ErrMsg('Unable to open WO_WFR table.')
	DEBUG
	RETURN
END

OPEN 'NCR' TO NCRTable ELSE
	ErrMsg('Unable to open NCR table.')
	DEBUG
	RETURN
End


* * *   Fix the SlotNos fields in WO_LOG   * * *

OutWfrQty = XLATE('WO_LOG',WONo,WO_LOG_OUT_CASS_LOAD_QTY$,'X')

If OutWfrQty > 0 Then
	OutSlots = ''
	For I = 1 To OutWfrQty
		OutSlots<1,I> = 1
	Next I

End 

SelectSent = 'SELECT WM_IN WITH WO_NO = ':QUOTE(WONo):' BY IN_CASS_NO'

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

EQU WAFER_ID$		TO 1
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

WaferArray = ''

Done = 0
LOOP
	READNEXT WMInKey ELSE Done = 1
UNTIL Done
	
	READ WMIRec FROM WMITable,WMInKey THEN
	
		WONo	= WMInKey[1,'*']
		CassNo	= FIELD(WMInKey,'*',3)
		
		WOMatRec = XLATE('WO_MAT',WONo:'*':CassNo,'','X')
		
		SlotNos = WMIRec<WM_IN_SLOT_NO$>
	
		IF SlotNos = '' THEN
			
			WaferQty = WOMatRec<WO_MAT_WAFER_QTY$>
			FOR I = 1 TO WaferQty
				SlotNos<1,I> = I
			NEXT
			
			WMIRec<WM_IN_SLOT_NO$> = SlotNos
				
			WRITE WMIRec ON WMITable,WMInKey THEN
				Null
			END
		END
		
		SlotCnt = COUNT(SlotNos,@VM) + (SlotNos NE '')
		
		FOR I = 1 TO SlotCnt
			
			WfrID  = WONo:'*':CassNo:'*':SlotNos<1,I>
			
			/*
			* * *  Add REL event to Wafer Record  * * *
			
			RelDTM = WOMatRec<WO_MAT_REL_DTM$>
			RelBy = WOMatRec<WO_MAT_REL_BY$>
		
			owwParms  = WfrID:@RM						;* WfrID				
			owwParms := Oconv(RelDTM,'DT4/^S'):@RM		;* Event Dtm
			owwParms := RelBy:@RM						;* Event By
			owwParms := 'REL':@RM						;* Event
			owwParms := WfrID:@RM						;* NewSlotID
			owwParms := '':@RM							;* RunID (RPZ)
			owwParms := '':@RM							;* NCRNo
			owwParms := '':@RM							;* TwID
			owwParms := ''								;* CurrSlotID
				
			obj_WO_Wfr('AddEvent',owwParms)
			*/
			
			* * *  PreEPI Non Conforming Wafer  * * *
			
			NCRNo = WMIRec<WM_IN_NCR_NO$,I>
				
			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 on pre-epi wafer
					
		NEXT I
		
	END	;* End of WMIRec read

REPEAT

SelectSent = 'SELECT REACT_RUN WITH WO_NO = ':QUOTE(WONo)

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


Done = 0
LOOP
	READNEXT RDSNo ELSE Done = 1
UNTIL Done
	READ RRRec FROM RRTable,RDSNo THEN
		
		WONo			= RRRec<REACT_RUN_WO_NO$>
		InCassNos		= RRRec<REACT_RUN_IN_CASS_NO$>
		InSlotNos		= RRRec<REACT_RUN_IN_SLOT_NO$>
		OutCassNos		= RRRec<REACT_RUN_OUT_CASS_NO$>
		OutSlotNos		= RRRec<REACT_RUN_OUT_SLOT_NO$>
		LoadSig			= RRRec<REACT_RUN_LOAD_SIG$>
		LoadSigDtm		= RRRec<REACT_RUN_LOAD_SIG_DTM$>
		UnloadSig		= RRRec<REACT_RUN_UNLOAD_SIG$>
		UnloadSigDtm	= RRRec<REACT_RUN_UNLOAD_SIG_DTM$>
		
		SlotCnt = COUNT(InCassNos,@VM) + (InCassNos NE '')
		
		FOR I = 1 TO SlotCnt
			CassNo		= InCassNos<1,I>
			SlotNo		= InSlotNos<1,I>
			OutCassNo	= OutCassNos<1,I>
			OutSlotNo	= OutSlotNos<1,I>

			
			IF CassNo NE '' AND SlotNo NE '' THEN
			
				* * *  Load Wafer into Reactor  * * *
			
				WfrID	= WONo:'*':CassNo:'*':SlotNo
				RPZ		= RDSNo:'*':RRRec<REACT_RUN_POCKET$,I>:'*':RRRec<REACT_RUN_ZONE$,I>
				PktChar	= RRRec<REACT_RUN_POCKET_CHAR$,I>
				WfrChar = RRRec<REACT_RUN_WAFER_CHAR$,I>
				
				
				CurrWfrRec = XLATE('WO_WFR',WfrID,'','X')
					
				LOCATE 'LOAD' IN CurrWfrRec<WO_WFR_LOC_EVENT$> SETTING cwPos ELSE
				
					owwParms  = WfrID:@RM						;* WfrID				
					owwParms := Oconv(LoadSigDTM,'DT4/^S'):@RM	;* Event Dtm
					owwParms := LoadSig:@RM						;* Event By
					owwParms := 'LOAD':@RM						;* Event
					owwParms := '':@RM							;* NewSlotID
					owwParms := RPZ:@RM							;* RunID (RPZ)
					owwParms := '':@RM							;* NCRNo
					owwParms := '':@RM							;* TwID
					owwParms := WfrID							;* CurrSlotID
												
					obj_WO_Wfr('AddEvent',owwParms)
					
				End	;* End of check for RPZ already in WO_WFR Record
				
				
				IF PktChar = 'TEST' AND WfrChar = 'PROD' THEN
				
					* * *  PTEST  Product Test Wfr  * * *
					
					WMIKey = WONo:'*1*':CassNo
					WMIRec = XLATE('WM_IN',WMIKey,'','X')
					
					TWUseKey = WMIRec<WM_IN_SLOT_MET_NO$,SlotNo> 
					
					LOCATE 'TEST' IN CurrWfrRec<WO_WFR_LOC_EVENT$> SETTING twPos ELSE
					
						IF TWUseKey NE '' THEN
						
							TWUseRec = XLATE('TW_USE',TWUseKey,'','X')
							
							TWSig		= TWUseRec<TW_USE_SIGNATURE$>
							TWSigDtm	= TWUseRec<TW_USE_SIG_DTM$>
							
						END ELSE
						
							TWUseKey 	= '***'				;* TW_USE record missing Wfr ID *
							TWSig		= UnloadSig
							TWSigDTM	= UnloadSigDtm
							
						END
						
						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
				
				IF PktChar = '' AND WfrChar = 'PROD' AND OutCassNo NE ''THEN
				
					LOCATE 'UNLOAD' IN CurrWfrRec<WO_WFR_LOC_EVENT$> SETTING Dummy ELSE
					
						* * *  UNLOAD product wafer  * * *
						
						UnloadSlotID = WONo:'*':OutCassNo:'*':OutSlotNo
						
						owwParms  = WfrID:@RM							;* WfrID				
						owwParms := Oconv(UnloadSigDTM,'DT4/^S'):@RM	;* Event Dtm
						owwParms := UnloadSig:@RM						;* Event By
						owwParms := 'UNLOAD':@RM						;* Event
						owwParms := UnloadSlotID:@RM					;* NewSlotID
						owwParms := '':@RM								;* RunID (RPZ)
						owwParms := '':@RM								;* NCRNo
						owwParms := '':@RM								;* TWUseKey
						owwParms := ''									;* CurrSlotID
									
						obj_WO_Wfr('AddEvent',owwParms)
						
					END	;* End of check for UNLOAD Event already in WO_WFR record.
					
				END
					
			END	;* End of check for Inbound Cass and Slot 
				
		
		
		NEXT I
	END	;* End of WMIRec read

REPEAT

SelectSent = 'SELECT NCR WITH WO_NO = ':QUOTE(WONo):' BY WO_MAT_CASS_NO'

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

ErrorLog = ''
Done = 0
LOOP
	READNEXT NCRNo ELSE Done = 1
UNTIL Done
	READ NCRRec FROM NCRTable,NCRNo THEN
		
		CassNo = NCRRec<NCR_WO_MAT_CASS_NO$>
		
		If CassNo = '' Then
			CassNo = NCRRec<NCR_CASS_NO$,1>
		END
		
		WOMatKey = WONo:'*':CassNo
		
		WOMatRec = XLATE('WO_MAT', WOMatKey ,'','X')
		
		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>
		
			rejCnt = COUNT(NCRRec<NCR_OUT_CASS_NO$>,@VM) + (NCRRec<NCR_OUT_CASS_NO$> NE '')
			
			FOR I = 1 TO rejCnt
			
				WfrID = WONo:'*':NCRRec<NCR_CASS_NO$,I>:'*':NCRRec<NCR_SLOT_NO$,I>
				CurrSlotID = WONo:'*':NCRRec<NCR_OUT_CASS_NO$,I>:'*':NCRRec<NCR_OUT_SLOT_NO$,I>
				
				CurrWfrRec = XLATE('WO_WFR',WfrID,'','X')
				
				LOCATE 'NCR' IN CurrWfrRec<WO_WFR_LOC_EVENT$> SETTING Dummy ELSE
				
					NCRBy = WOMatRec<WO_MAT_NCR_FINAL_SIG$,NPos>
					NCRDTM = WOMatRec<WO_MAT_NCR_FINAL_SIG_DTM$,NPos>
					
					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 := CurrSlotID						;* CurrSlotID
								
					obj_WO_Wfr('AddEvent',owwParms)
					
				END	;* End of check for existing 'NCR' Event in WO_WFR record
			
			NEXT I
		END	;* End of NCR Final sig locate
		

	END	;* End of NCR record read
REPEAT

SelectSent = 'SELECT WM_OUT WITH WO_NO = ':QUOTE(WONo)

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

ErrorLog = ''
Done = 0
LOOP
	READNEXT WMOKey ELSE Done = 1
UNTIL Done
	READ WMORec FROM WMOTable,WMOKey THEN
		
		WONo		= WMOKey[1,'*']
		StepNo		= WMOKey[COL2()+1,'*']
		CassNo		= WMOKey[COL2()+1,'*']
		
		OutWfrQty = XLATE('WO_LOG',WONo,WO_LOG_OUT_CASS_LOAD_QTY$,'X')
		
		SlotNos = WMORec<WM_OUT_SLOT_NO$>
		
		IF SlotNos = '' THEN
			WaferQty = WOMatRec<WO_MAT_WAFER_QTY$>
			FOR I = 1 TO WaferQty
				SlotNos<1,I> = I
			NEXT
		END
		
		SlotCnt = COUNT(SlotNos,@VM) + (SlotNos NE '')
		
		FOR I = 1 TO SlotCnt
			RDSNo 		= WMORec<WM_OUT_RDS$,I>
			PktNo		= WMORec<WM_OUT_POCKET$,I>
			Zone		= WMORec<WM_OUT_ZONE$,I>
			InCassNo	= WMORec<WM_OUT_IN_CASS_NO$,I>
			InSlotNo	= WMORec<WM_OUT_IN_SLOT_NO$,I>
			NCR			= WMORec<WM_OUT_SLOT_NCR$,I>
			UMWCass		= WMORec<WM_OUT_UMW_CASS_ID$,I>
			UMWSlot		= WMORec<WM_OUT_UMW_SLOT_NO$,I>
			MWBox		= WMORec<WM_OUT_MAKEUP_BOX$>
			
			CurrSlotID = WONo:'*':CassNo:'*':SlotNos<1,I>
			
			If RDSNo = '' Then
				CurrWfrID = ''
			End ELSE
				InWONo = XLATE('RDS',RDSNo,RDS_WO$,'X')				
				CurrWfrID = InWONo:'*':InCassNo:'*':InSlotNo	;* CurrWfrID from RDS + Inbound Cass & Slot
			End
			
			MUWONo		= WMORec<WM_OUT_MU_WO_NO$,I>
			MUCassNo	= WMORec<WM_OUT_MU_CASS_NO$,I>
			MUSlot		= WMORec<WM_OUT_MU_SLOT_NO$,I>
			
			MUFromSlotID = MUWONo:'*':MUCassNo:'*':MUSlot			
		
			If MUWONo NE '' THEN
			
				CurrWfrRec = XLATE('WO_WFR',WfrID,'','X')
				
				LOCATE CurrSlotID IN CurrWfrRec<WO_WFR_SLOT_ID$> SETTING Dummy ELSE
			
			
					owwParms  = CurrWfrID:@RM					;* WfrID				
					owwParms := '':@RM							;* Event Dtm
					owwParms := '':@RM							;* Event By
					owwParms := 'MKUP':@RM						;* Event
					owwParms := CurrSlotID:@RM					;* NewSlotID
					owwParms := '':@RM							;* RunID (RPZ)
					owwParms := '':@RM							;* NCRNo
					owwParms := '':@RM							;* TwID
					owwParms := MUFromSlotID					;* CurrSlotID
								
					obj_WO_Wfr('AddEvent',owwParms)
					
				END	;* End of new Slot ID already in the existing WO_WFR Rec
					
			END
			
		NEXT I
	END	;* End of WMIRec readO

REPEAT

Display = ''
WACnt = COUNT(WaferArray<WAFER_ID$>,@VM) + (WaferArray<WAFER_ID$> NE '')





* * * * * * *
Bail:
* * * * * * * 

RETURN Result


