COMPILE FUNCTION obj_WO_Mat_Event(Method,Parms)
/*
	Methods for the Work Order Material Event (WO_MAT_EVENT) table
    
	11/22/2012 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Post(WONo,CassNo,InvDTMs, InvActions, Inv
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, Dialog_Box,NextKey, Popup, Get_Property, obj_RDS
DECLARE FUNCTION obj_WO_Mat, obj_WM_In, obj_WM_Out, obj_Prod_Spec, obj_Clean_Insp
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, Send_Dyn, Btree.Extract, ErrMsg, Send_Dyn, RList, obj_WO_Log, Send_Event, obj_RDS
DECLARE SUBROUTINE obj_notes, obj_Clean_Insp, obj_Post_Log, Send_Info

$INSERT MSG_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT WO_MAT_EVENT_EQUATES


EQU TARGET_ACTIVELIST$ TO 5

EQU CRLF$ TO \0D0A\

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

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

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END


Result = ''

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

END CASE

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

RETURN Result


* * * * * * *
Post:
* * * * * * *

WONo		= Parms[1,@RM]
CassNo		= Parms[COL2()+1,@RM]
InvDTMs		= Parms[COL2()+1,@RM]
InvActions	= Parms[COL2()+1,@RM]
InvWHs		= Parms[COL2()+1,@RM]
InvLocs		= Parms[COL2()+1,@RM]
InvUsers	= Parms[COL2()+1,@RM]
InvTags		= Parms[COL2()+1,@RM]
InvToolIDs	= Parms[COL2()+1,@RM]


IF WONo = ''		THEN ErrorMsg = 'Null parameter "WONo" passed to routine. (':Method:')'
IF CassNo = ''		THEN ErrorMsg = 'Null parameter "CassNo" passed to routine. (':Method:')'
IF InvDTMs = ''		THEN ErrorMsg = 'Null parameter "InvDTMs" passed to routine. (':Method:')'
IF InvActions = ''	THEN ErrorMsg = 'Null parameter "InvActions" passed to routine. (':Method:')'
IF InvWHs = ''		THEN ErrorMsg = 'Null parameter "InvWHs" passed to routine. (':Method:')'
IF InvLocs = ''		THEN ErrorMsg = 'Null parameter "InvLocs" passed to routine. (':Method:')'
IF InvUsers = ''	THEN ErrorMsg = 'Null parameter "InvUsers" passed to routine. (':Method:')'


IF ErrorMsg NE '' THEN
 	ErrorMsg := CRLF$:Parms
 	SWAP @RM WITH CRLF$ IN ErrorMsg
	RETURN
END

WOMEventRec = XLATE('WO_MAT_EVENT',WONo:'*':CassNo,'','X')

otParms	= 'WO_MAT_EVENT':@RM:WONo:'*':CassNo
Void	= obj_Tables('LockRec',otParms)
	
IF Get_Status(errCode) THEN
	ErrMsg(errCode)
	RETURN
END

LineCnt = COUNT(InvDTMs,@VM) + (InvDTMs NE '')

FOR I = 1 TO LineCnt

	thisInvDTM = ICONV(InvDTMs<1,I>,'DT')
	
	IF thisInvDTM = '' THEN
		ErrorMsg  = 'Invalid InvDTM ':QUOTE(InvDTSs<1,I>):' passed to routine in line ':I:'.'
		obj_Tables('UnlockRec',otParms)
		RETURN
	END

	LOCATE thisInvDTM IN WOMEventRec<WO_MAT_EVENT_INV_DTM$> BY 'AL' USING @VM SETTING Pos THEN
		ErrorMsg := CRLF$:'Event Not Logged - TimeStamp ':InvDTMs<1,I>:' Exists in Event Log.'
		obj_Tables('UnlockRec',otParms)
		RETURN
	END ELSE
		WOMEventRec = INSERT(WOMEventRec,WO_MAT_EVENT_INV_DTM$,Pos,0,thisInvDTM)
		WOMEventRec = INSERT(WOMEventRec,WO_MAT_EVENT_INV_ACTION$,Pos,0,InvActions<1,I>)
		WOMEventRec = INSERT(WOMEventRec,WO_MAT_EVENT_INV_WH$,Pos,0,InvWHs<1,I>)
		WOMEventRec = INSERT(WOMEventRec,WO_MAT_EVENT_INV_LOC$,Pos,0,InvLocs<1,I>)
		WOMEventRec = INSERT(WOMEventRec,WO_MAT_EVENT_INV_USER$,Pos,0,InvUsers<1,I>)
		WOMEventRec = INSERT(WOMEventRec,WO_MAT_EVENT_INV_TAG$,Pos,0,InvTags<1,I>)
		WOMEventRec = INSERT(WOMEventRec,WO_MAT_EVENT_INV_TOOL_ID$,Pos,0,InvToolIDs<1,I>)
	END
NEXT I

otParms = FieldStore(otParms,@RM,4,0,WOMEventRec)	;* Put record in 4th field of OtParms
obj_Tables('WriteRec',otParms)


RETURN


