COMPILE FUNCTION Reconcile_EPI_WO(WONo,ReturnArray)

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


$INSERT WO_MAT_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT WM_IN_EQUATES
$INSERT REACT_RUN_EQUATES

$INSERT RLIST_EQUATES
$INSERT POPUP_EQUATES
$INSERT LOGICAL

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


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,'*']
		Step	= WMInKey[COL2()+1,'*']
		CassNo	= WMInKey[COL2()+1,'*']
		LotNo	= XLATE('WO_MAT',WONo:'*':CassNo,WO_MAT_LOT_NO$,'X')
		SlotNos = WMIRec<WM_IN_SLOT_NO$>
		SlotCnt = COUNT(SlotNos,@VM) + (SlotNos NE '')
		
		FOR I = 1 TO SlotCnt
			WaferID  = WONo:'.':CassNo:'.':SlotNos<1,I>
			RunDest  = WMIRec<WM_IN_RDS_NO$,I>:'.'
			RunDest := WMIRec<WM_IN_POCKET_NO$,I>:'.' 
			RunDest := WMIRec<WM_IN_ZONE$,I>
			
			IF WMIRec<WM_IN_SLOT_NCR$,I> NE '' THEN
				WaferDest = 'NCR ':WMIRec<WM_IN_SLOT_NCR$,I>
			END ELSE
				WaferDest = RunDest
			END
			WaferArray<WAFER_ID$,-1> = WaferID
			WaferArray<LOT_NO$,-1> = LotNo
			WaferArray<LOADED_INTO$,-1> = WaferDest
		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$>
		
		SlotCnt = COUNT(InCassNos,@VM) + (InCassNos NE '')
		
		FOR I = 1 TO SlotCnt
			CassNo = InCassNos<1,I>
			SlotNo = InSlotNos<1,I>
			IF CassNo NE '' AND SlotNo NE '' THEN
				WaferID  = WONo:'.':CassNo:'.':SlotNo
				RDSPosID = RDSNo:'.':RRRec<REACT_RUN_POCKET$,I>:'.':RRRec<REACT_RUN_ZONE$,I>
				UnloadID = WONo:'.':RRRec<REACT_RUN_OUT_CASS_NO$,I>:'.':RRRec<REACT_RUN_OUT_SLOT_NO$,I>
				
				LOCATE WaferID IN WaferArray<WAFER_ID$> USING @VM SETTING Pos THEN
					IF WaferArray<LOADED_INTO$,Pos> = RDSPosID THEN
						WaferArray<RAN_IN$,Pos> = RDSPosID
					END ELSE
						WaferArray<RAN_IN$,Pos> = '*':RDSPosID:'*'
					END
					PocketChar = RRRec<REACT_RUN_POCKET_CHAR$,I>
					
					WaferArray<POCKET_CHAR$,Pos> = PocketChar
					IF PocketChar = '' THEN
						WaferArray<UNLOADED_INTO$,Pos> = UnloadID
					END
				END
			END
				
		
		
		NEXT I
	END	;* End of WMIRec 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,'*']
		SlotNos = WMORec<WM_OUT_SLOT_NO$>
		
		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$>
			
			DefWfrID	= WONo:'.':CassNo:'.':I
			UMWWaferID	= UMWCass[1,'.']:'.':UMWCass[-1,'B.']:'.':UMWSlot
			
			RDSPosID = RDSNo:'.':PktNo:'.':Zone
			
			BEGIN CASE
				CASE NCR NE ''
					Disposition = 'NCR - ':NCR
					
				CASE UMWCass NE ''
					Disposition = 'UMW - ':UMWWaferID
				
				CASE 1
					IF MWBox THEN
						Disposition = 'M/U - ':DefWfrID
					END ELSE
						Disposition = 'PROD - ':DefWfrID
					END
					
			END CASE
			LOCATE DefWfrID IN WaferArray<UNLOADED_INTO$> USING @VM SETTING Pos THEN
				IF WaferArray<WAFER_DISP$,Pos> NE '' THEN
					WaferArray<WAFER_DISP$,Pos> = WaferArray<WAFER_DISP$,Pos>:'***':Disposition
				END ELSE
					WaferArray<WAFER_DISP$,Pos> = Disposition
				END
			END ELSE
				IF RDSNo NE '' THEN
					ErrorLog<1,-1> = DefWfrID:' in WMO but not in RDS records. RDS.Pkt.Zone: ':RDSNo:'.':PktNo:'.':Zone' unload errors'
				END
			END

		NEXT I
	END	;* End of WMIRec read

REPEAT

IF ErrorLog NE '' THEN
	TypeOver = ''
	TypeOver<pDISPLAY$> = ErrorLog
	dummy = Popup(@WINDOW,TypeOver,'EPI_WO_RECON_ERRORS')
END

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



IF ReturnArray = 1 THEN

	Result = WaferArray
	
END ELSE

	FOR I = 1 TO WACnt

		LineOut  = WaferArray<WAFER_ID$,I>:@SVM
		LineOut := WaferArray<LOT_NO$,I>:@SVM
		LineOut := WaferArray<LOADED_INTO$,I>:@SVM
		LineOut := WaferArray<RAN_IN$,I>:@SVM
		LineOut := WaferArray<POCKET_CHAR$,I>:@SVM
		LineOut := WaferArray<UNLOADED_INTO$,I>:@SVM
		LineOut := WaferArray<WAFER_DISP$,I>

		Display<1,I> = LineOut

	NEXT I

	TypeOver = ''
	TypeOver<PDISPLAY$> = Display

	void = Popup(@WINDOW,TypeOver,'EPI_WO_RECONCILE')

	IF Get_Status(errCode) THEN DEBUG
END


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

RETURN Result



