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

DECLARE Subroutine Msg, FSMsg, Set_Status, obj_SAP
DECLARE FUNCTION Set_Status, Get_Status, obj_NCR

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 NCR_EQUATES
$INSERT WO_LOG_EQUATES

EQU ReadRec$ TO 1

	
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 NCR_MFS!|"
		mesg := handle
		MSG('',mesg)
		GOTO NEXT.FS
	END
	
	WONo		= RECORD<NCR_WO_NO$>
	CassNo      = RECORD<NCR_CASS_NO$>
	SAPOrderNO	= XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')

	IF SAPOrderNo = '' THEN GOTO NEXT.FS
	
	
	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
	
	NCRNo = Name
	
	NewQty = obj_NCR('RejQty',NCRNo:@RM:RECORD)
	
	IF OrgRecord = '' THEN
		TransQty = NewQty
	END ELSE
		OrgQty = obj_NCR('RejQty',NCRNo:@RM:OrgRecord)
		TransQty = NewQty - OrgQty
	END
	
	IF TransQty = 0 THEN GOTO NEXT.FS
	
	obj_SAP('AddTransaction','WFR_REJECT':@RM:SAPOrderNo:@RM:TransQty:@RM:WONo:@RM:CassNo)
		
	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
	
	WONo		= RECORD<NCR_WO_NO$>
	SAPOrderNO	= XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')

	IF SAPOrderNo = '' THEN GOTO NEXT.FS
	
	
	FileName = Files_Array<POS>[1,'*']		;* Extract corresponding file name
	
	NCRNo = Name
	
	NewQty = obj_NCR('RejQty',NCRNo:@RM:RECORD)
	
	IF OrgRecord = '' THEN
		TransQty = NewQty
	END ELSE
		OrgQty = obj_NCR('RejQty',NCRNo:@RM:OrgRecord)
		TransQty = 0 - OrgQty
	END
	
	IF TransQty = 0 THEN GOTO NEXT.FS
	
	
	
	obj_SAP('AddTransaction','WFR_REJECT':@RM:SAPOrderNo:@RM:TransQty)
	
	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
