COMPILE FUNCTION REC_VER_MONITOR(Method,TableName,RecKey,NewRec,OrgRec)
/*
	Methods for the REC_VER_MFS
    
	09/05/2012 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Write(TableName,ID,NewRec,OrgRec)				;* New or changed records
   
*/


DECLARE SUBROUTINE obj_SAP, ErrMsg, obj_Post_Log, obj_Tables
DECLARE FUNCTION obj_NCR, obj_WM_Out, obj_WO_Mat


$INSERT WO_MAT_EQUATES
$INSERT NCR_EQUATES


ErrTitle = 'Error in Stored Procedure "SAP_Monitor"'
ErrorMsg = ''

IF NOT(ASSIGNED(Method))	THEN ErrorMsg = 'Unassigned parameter "Method" passed to subroutine'
IF NOT(ASSIGNED(Parms))		THEN Parms = ''

IF ErrorMsg NE '' THEN
	RETURN ''
END

Result = ''

BEGIN CASE
	CASE Method = 'Write'			; GOSUB Write
	
	
	CASE 1
		ErrMsg('Unknown Method ':QUOTE(Method):' passed to routine.')

END CASE

RETURN



* * * * * * *
Write:
* * * * * * * 

IF TableName = '' THEN RETURN
IF RecKey = '' THEN RETURN

BEGIN CASE

	CASE TableName = 'NCR'
		
		CurrVer	= OrgRec<NCR_REC_VER$>
		
		NewRejQty = obj_NCR('RejQty',RecKey:@RM:NewRec)
		OrgRejQty = obj_NCR('RejQty',RecKey:@RM:OrgRec)	
		
		IF NewRejQty NE OrgRejQty THEN
			CurrDTM = ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTHS'),'DT')
			NewRec<NCR_LAST_QTY_DTM$> = CurrDtm
		END
		
		IF CurrVer	= '' THEN CurrVer = 0
		
		NewRec<NCR_REC_VER$> = CurrVer + 1
		
		
		
	CASE TableName = 'WO_MAT' 
	
		CurrVer = OrgRec<WO_MAT_REC_VER$>
		
		IF CurrVer = '' THEN CurrVer = 0
		
		NewRec<WO_MAT_REC_VER$> = CurrVer + 1
		
		OrgSigProfile = OrgRec<WO_MAT_SIG_PROFILE$>
		NewSigProfile = NewRec<WO_MAT_SIG_PROFILE$>
		
		
		OrgSPCnt = COUNT(OrgSigProfile,@VM) + (OrgSigProfile NE '')
		NewSPCnt = COUNT(NewSigProfile,@VM) + (NewSigProfile NE '')
		
		IF NewSPCnt > OrgSPCnt THEN
			SPCnt = NewSPCnt
		END ELSE
			SPCnt = OrgSPCnt
		END
			
		SigLines = ''
			
		FOR I = 1 TO SPCnt
			OrgSigLine = OrgSigProfile<1,I>:OrgRec<WO_MAT_SIGNATURE$,I>:OrgRec<WO_MAT_SIG_DTM$,I>
			NewSigLine = NewSigProfile<1,I>:NewRec<WO_MAT_SIGNATURE$,I>:NewRec<WO_MAT_SIG_DTM$,I>
			
			IF NewSigLine NE OrgSigLine THEN
				CurrVer = OrgRec<WO_MAT_SIG_VER$,I>
				IF CurrVer = '' THEN CurrVer = 0
				
				NewRec<WO_MAT_SIG_VER$,I> = CurrVer + 1
			END
		
		NEXT SPCnt
			
	CASE 1
	
		NULL
	
		
END CASE


RETURN
