COMPILE SUBROUTINE WM_MFS(Code, BFS, Handle, Name, FMC, Record, Status)

DECLARE Subroutine Msg, FSMsg, Set_Status
DECLARE FUNCTION Set_Status, Get_Status, obj_WM_In, obj_WM_Out

COMMON /FILENAME/ Files_Array, Handles_Array

FS = DELETE(BFS,1,1,1)
NEXTFS = FS<1,1,1>
@FILE.ERROR = ''

$INSERT FILE.SYSTEM.ONGOSUB
$INSERT FILE.SYSTEM.EQUATES
$INSERT MSG_EQUATES
$INSERT DICT_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT WM_IN_EQUATES
$INSERT WO_MAT_EQUATES

EQU ReadRec$ TO 1

*EQU WO_MAT_WMI_CURR_STATUS$			TO 48
*EQU WO_MAT_WMO_CURR_STATUS$			TO 49
	
RETURN

/* Directly called functions - don't pass to next file system */

INSTALL:
FLUSH:
UNLOCK.ALL:
FLUSH.CACHE:

Status = 1
RETURN

/* Not available to MFS */
	
LOCK.SEMAPHORE:
UNLOCK.SEMAPHORE:
SET.USER.SEMAPHORE:

RETURN

/* Directory level calls */

CREATE.MEDIA:
OPEN.MEDIA:
CLOSE.MEDIA:
READ.MEDIA:
WRITE.MEDIA:
GROUP.NUMBER:
RECORD.COUNT:
CREATE.FILE:
RENAME.FILE:
MOVE.FILE:
DELETE.FILE:

Goto NEXT.FS

OPEN.FILE:
	
	* Call BFS in order to get file handle
	
	FS = DELETE(BFS,1,1,1)
	NEXTFS = FS<1,1,1>
	CALL @NEXTFS(Code, FS, Handle, Name, FMC, Record, Status)
	
	* Load handle and file name into labelled common
	
	IF Status THEN
		LOCATE Name IN Files_Array USING @FM SETTING POS THEN
			Handles_Array<POS> = Record
		END ELSE
			Files_Array<-1> = Name
			Handles_Array<-1> = Record
		END
	END
	RETURN

CLEARFILE:
REMAKE.FILE:
SELECT:
READNEXT:
CLEARSELECT:

READ.RECORD:
READO.RECORD:
	
	GOTO NEXT.FS
	
* * * * * * *
WRITE.RECORD:
* * * * * * *

	* Get original record and compare with RECORD being written
	LOCATE Handle IN Handles_Array USING @FM SETTING POS ELSE
		mesg = "Missing file handle in WM_MFS!|"
		mesg := handle
		MSG('',mesg)
		GOTO NEXT.FS
	END
	
	FileName = Files_Array<POS>[1,'*']		;* Extract corresponding file name
		
	Write_Flag	= 0
	OrgRecord	= ''
	NextFS 		= BFS<1,1,2>
	
	CALL @NextFS(ReadRec$, FS, Handle, Name, FMC, OrgRecord, Status)  ;* Read old record
	
	OPEN 'WO_MAT' TO WOMatFile ELSE
		MSG('',"Unable to open 'WO_MAT' table in WM_MFS")
		GOTO NEXT.FS
	END
	
	IF FileName = 'WM_IN' THEN

		WONo	= Name[1,'*']
		WOStep	= Name[COL2()+1,'*']
		CassNo	= Name[COL2()+1,'*']
		
		Set_Status(0)
		NewStatus = obj_WM_In('CurrStatus',Name:@RM:RECORD)
		
		IF Get_Status(errCode) THEN
			NewStatus = 'ERR'
		END
		
		WOMatKey = WONo:'*':CassNo
		READ WOMatRec FROM WOMatFile,WOMatKey THEN
		
			SlotCnt = COUNT(RECORD<WM_IN_SLOT_NO$>,@VM) + (RECORD<WM_IN_SLOT_NO$> NE '')
			
			FOR I = 1 TO SlotCnt
				WOMatRec<WO_MAT_SLOT_MET_NO$,I> = RECORD<WM_IN_SLOT_MET_NO$,I>
			NEXT I
		
			IF WOMatRec<WO_MAT_WMI_CURR_STATUS$> NE NewStatus THEN
				WOMatRec<WO_MAT_WMI_CURR_STATUS$> = NewStatus
			END
			
			WRITE WOMatRec ON WOMatFile,WOMatKey ELSE Null
			
		END ELSE
			*MSG('',"Unable to read 'WO_MAT' record ":QUOTE(WOMatKey):" in WM_MFS")
			GOTO NEXT.FS
		END
			

	END	;* End of check for WM_IN table
	
	
	IF FileName = 'WM_OUT' THEN
		
		WONo	= Name[1,'*']
		WOStep	= Name[COL2()+1,'*']
		CassNo	= Name[COL2()+1,'*']
		
		Set_Status(0)
		NewStatus = obj_WM_Out('CurrStatus',Name:@RM:RECORD)
		
		IF Get_Status(errCode) THEN
			NewStatus = 'ERR'
		END
		
		WOMatKey = WONo:'*':CassNo
		
		READ WOMatRec FROM WOMatFile,WOMatKey ELSE WOMatRec = ''
		
		* Smaller outbound boxes won't have WO_MAT records when the are being created so it happens here.
			
			WOMatRec<WO_MAT_WMO_CURR_STATUS$> = NewStatus		;* Original code
			
			SlotCnt = COUNT(RECORD<WM_OUT_SLOT_NO$>,@VM) + (RECORD<WM_OUT_SLOT_NO$> NE '')
			
			FOR I = 1 TO Slotcnt
				*WOMatRec<WO_MAT_EPO_SLOT$,I> = RECORD<WM_OUT_SLOT_NO$,I>
				
				WOMatRec<WO_MAT_EPO_SLOT$,I> = I								;* 8/4/2011 JCH Keep all slot numbers for EPOS
				
				InCassNo = RECORD<WM_OUT_IN_CASS_NO$,I>
				InSlotNo = RECORD<WM_OUT_IN_SLOT_NO$,I>
				
				IF InCassNo NE '' AND InSlotNo NE '' THEN
					WOMatRec<WO_MAT_EPOS_WFR_IN$,I> = WONo:'.':InCassNo:'.':InSlotNo
				END ELSE
					WOMatRec<WO_MAT_EPOS_WFR_IN$,I> = ''
				END
				
				RDSNo = RECORD<WM_OUT_RDS$,I>
				
				IF RDSNo NE '' THEN
					WOMatRec<WO_MAT_EPOS_RUN_ID$,I>			= RDSNo:'.':RECORD<WM_OUT_POCKET$,I>:'.':RECORD<WM_OUT_ZONE$,I>
				END ELSE
					WOMatRec<WO_MAT_EPOS_RUN_ID$,I> = ''
				END
				
				WOMatRec<WO_MAT_EPOS_NCR$,I>			= RECORD<WM_OUT_SLOT_NCR$,I>
				WOMatRec<WO_MAT_EPOS_MET_NO$,I>			= ''	;* Not used
				
				MUWONo = RECORD<WM_OUT_MU_WO_NO$,I>
				
				IF MUWONo NE '' THEN
					WOMatRec<WO_MAT_EPOS_REP_WAFER_ID$,I>	= MUWONo:'.':RECORD<WM_OUT_MU_CASS_NO$,I>:'.':RECORD<WM_OUT_MU_SLOT_NO$,I>
				END ELSE
					WOMatRec<WO_MAT_EPOS_REP_WAFER_ID$,I> = ''
				END
				
				MuWfrID = RECORD<WM_OUT_UMW_CASS_ID$,I>
				
				IF MuWfrID NE '' THEN
					WOMatRec<WO_MAT_EPOS_MOVED_TO$,I> = MuWfrID[1,'.']:'.':FIELD(MuWfrID,'.',3):'.':RECORD<WM_OUT_UMW_SLOT_NO$,I>
				END ELSE
					WOMatRec<WO_MAT_EPOS_MOVED_TO$,I> = ''
				END
				
			NEXT I
			
			
			WRITE WOMatRec ON WOMatFile,WOMatKey ELSE Null
			
		
	END	;* End of check for WM_OUT table
	
	
	
	GOTO NEXT.FS
	
	
* * * * * * *
DELETE.RECORD:
* * * * * * *

	LOCATE Handle IN Handles_Array USING @FM SETTING POS ELSE
		mesg = "Missing file handle in DUEIN_MFS!|"
		mesg := handle
		MSG('',mesg)
		GOTO NEXT.FS
	END
	
	FileName = Files_Array<POS>[1,'*']		;* Extract corresponding file name
	
	OPEN 'WO_MAT' TO WOMatFile ELSE
		MSG('',"Unable to Open 'WO_MAT' table in DueIn_MFS")
		GOTO NEXT.FS
	END

	OrgRecord	= ''
	NextFS		= BFS<1,1,2>

	CALL @NextFS(ReadRec$, FS, Handle, Name, FMC, OrgRecord, Status)  ;* Read old record
	
	WONo	= Name[1,'*']
	WOStep	= Name[COL2()+1,'*']
	CassNo	= Name[COL2()+1,'*']
	
	WOMatKey = WONo:'*':CassNo
	
	READ WOMatRec FROM WOMatFile,WOMatKey THEN
		IF FileName = 'WM_IN' THEN
			WOMatRec<WO_MAT_WMI_CURR_STATUS$> = ''
		END
		
		IF FileName = 'WM_OUT' THEN
			WOMatRec<WO_MAT_WMO_CURR_STATUS$> = ''
		END
		
		WRITE WOMatRec ON WOMatFile,WOMatKey ELSE Null
	END ELSE
		*MSG('',"Unable to read 'WO_MAT' record ":QUOTE(WOMatKey):" in WM_MFS")
		GOTO NEXT.FS
	END
	
	GOTO NEXT.FS

/*   Unused calls  */
LOCK.RECORD:
UNLOCK.RECORD:
RESERVED:
OMNI.SCRIPT:

CREATE.INDEX:
DELETE.INDEX:
SELECT.INDEX:
UPDATE.INDEX:
READNEXT.INDEX:

NEXT.FS:

	FS = DELETE(BFS,1,1,1)
	NEXTFS = FS<1,1,1>
	CALL @NEXTFS(Code, FS, Handle, Name, FMC, Record, Status)
	RETURN

