COMPILE FUNCTION obj_WO_Mat_Log(Method,Parms)
/*
	Methods for the Work Order Material Log (WO_MAT_LOG) table.
	(Input buffer for bar code window, works like a !file)
    
	10/24/2006 JCH - Initial Coding
     
	Properties:
	
	Methods:
	
	Create(WONo,CassNo,......)									;* Create new WO Material entry
	Post(null)													;* Pass null - will attempt to post all transactions in the table
	
*/
#pragma precomp SRP_PreCompiler


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, Dialog_Box, NextKey, Popup, Get_Property, obj_RDS, Database_Services
Declare function    Logging_Services, Environment_Services, RTI_CreateGUID, Error_Services, Datetime, GetTickCount
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_WO_Mat, Send_Info, obj_Notes, Logging_Services, Database_Services, Lot_Event_Services, Mona_Services

$INSERT MSG_EQUATES
$INSERT WO_MAT_LOG_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT WM_IN_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT RDS_EQU
$Insert WO_LOG_EQUATES

$INSERT LSL_USERS_EQUATES
$INSERT SRPMail_Inserts
$INSERT EMAIL_BOX_EQUATES
$INSERT LOGICAL

Equ Tab$                    to \09\
Equ CRLF$                   to \0D0A\
Equ LF$                     to \0A\
Equ Comma$                  to ','

EQU TARGET_ACTIVELIST$ TO 5

LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\WO_MAT_LOG'
LogDate     = Oconv(Date(), 'D4/')
LogTime     = Oconv(Time(), 'MTS')
LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' Front-end Create Log.csv'
Headers     = 'Logging DTM'
objLog      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, Comma$, Headers, '', False$, False$)

LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\WO_MAT_LOG'
LogDate     = Oconv(Date(), 'D4/')
LogTime     = Oconv(Time(), 'MTS')
LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : '_Post_Log.csv'
Headers     = 'Logging DTM'
objLogPost      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, Comma$, Headers, '', False$, False$)

LoggingDTM  = LogDate : ' ' : LogTime   ; // Logging DTM

IsProd = Environment_Services('IsProd')
If IsProd EQ True$ then
	MonaResource = 'GRP_OPENINSIGHT_MES_OP_FE_OBJWOMATLOG'
end else
	MonaResource = 'GRP_OPENINSIGHT_MES_OP_FE_DEV_OBJWOMATLOG'
end

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

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

Result = ''

BEGIN CASE
    CASE Method = 'Create'			; GOSUB Create
    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


* * * * * * *
Create:
* * * * * * *

	StartTick = GetTickCount()
    MetricName = 'Create'
	
    LogFile		= Parms[1,@RM]
    TransDTM	= Parms[COL2()+1,@RM]
    Action		= Parms[COL2()+1,@RM]
    WhCd		= Parms[COL2()+1,@RM]
    LocCd		= Parms[COL2()+1,@RM]
    WoNos		= Parms[COL2()+1,@RM]
    CassNos		= Parms[COL2()+1,@RM]
    UserID		= Parms[COL2()+1,@RM]
    Tags		= Parms[COL2()+1,@RM]
    ToolID		= Parms[COL2()+1,@RM]
    ScanEntry	= Parms[COL2()+1,@RM]
    
    // Log front-end transaction
    LogData     = ''
    LogData<1>  = LoggingDTM
    LogData<2>  = @User4
    LogData<3>  = LogFile
    LogData<4>  = TransDTM
    LogData<5>  = Action
    LogData<6>  = WhCd
    LogData<7>  = LocCd
    LogData<8>  = WONos
    LogData<9>  = CassNos
    LogData<10> = UserID
    LogData<11> = Tags
    LogData<12> = ToolID
    LogData<13> = ScanEntry
    Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
    
    IF LogFile = ''		THEN ErrorMsg = 'Null parameter "LogFile" passed to routine. (':Method:')'
    IF TransDTM = ''	THEN ErrorMsg = 'Null parameter "TransDTM" passed to routine. (':Method:')'
    IF Action = ''		THEN ErrorMsg = 'Null parameter "Action" passed to routine. (':Method:')'
    IF WhCd = ''		THEN ErrorMsg = 'Null parameter "WhCd" passed to routine. (':Method:')'
    IF LocCd = ''		THEN ErrorMsg = 'Null parameter "LocCd" passed to routine. (':Method:')'
    IF WoNos = ''		THEN ErrorMsg = 'Null parameter "WoNos" passed to routine. (':Method:')'
    IF CassNos = ''		THEN ErrorMsg = 'Null parameter "CassNos" passed to routine. (':Method:')'
    IF ScanEntry = ''   OR ScanEntry NE 1 THEN ScanEntry = False$
    
    
    thisTransDTM = ICONV(TransDTM,'DT')
    IF thisTransDTM = '' THEN
        ErrorMsg = 'Invalid TransDTM ':QUOTE(TransDTM):' passed to routine. (':Method:')'
    END
    
    IF ErrorMsg NE '' THEN
        CONVERT @RM TO '/' IN Parms
        ErrorMsg := CRLF$:Parms
    END
    
    IF ErrorMsg NE '' THEN
        RETURN
    END
    
    IF UserID = '' THEN
        UserID = @USER4
    END
    IF INDEX(UserID,@VM,1) THEN UserID = UserID[1,@VM]	;* We have bullshit records w/1000's of mv'd usernames being written
    
    UniqueID    = RTI_CreateGUID()
    WOMatLogKey = LogFile:'*':thisTransDTM:'*':Action:'*':WhCd:'*':LocCd:'*':UniqueID
    
    WOMatLogRec = ''
    
    WOMatLogRec<WO_MAT_LOG_WO_NO$>		= WONos
    WOMatLogRec<WO_MAT_LOG_CASS_NO$>	= CassNos
    WOMatLogRec<WO_MAT_LOG_USER_ID$>	= UserID
    WOMatLogRec<WO_MAT_LOG_TAG$>		= Tags
    WOMatLogRec<WO_MAT_LOG_TOOL_ID$>	= ToolID
    WOMatLogRec<WO_MAT_LOG_SCAN_ENTRY$> = ScanEntry
    
    Database_Services('WriteDataRow', 'WO_MAT_LOG', WOMatLogKey, WOMatLogRec, True$, False$, True$)
    If Error_Services('HasError') then ErrorMsg = Error_Services('GetMessage')
    
    EndTick = GetTickCount()
    Mona_Services('QueueLatencyAndCountMetrics', MonaResource, MetricName, StartTick, EndTick)
    
RETURN


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

    hSysLists       = Database_Services('GetTableHandle', 'SYSLISTS')
    ServiceKeyID    = 'Obj_WO_Mat_Log*Post'
    Lock hSysLists, ServiceKeyID then
        
        OPEN 'WO_MAT_LOG' TO FileIn ELSE
            ErrorMsg = 'Unable to Open WO_MAT_LOG table for Posting.'
            RETURN
        END
        ltParms = 'SYSOBJ':@RM:'WO_MAT_LOG_POST'
        obj_Tables('LockRec',ltParms)	;* Places lock on fictional record
        errCode = ''
        IF Get_Status(errCode) THEN
            Set_Status(0)
            RETURN
        END
        
        SELECT FileIn
        
        WMLKeys = ''
        Done = 0
        LOOP
            READNEXT WMLKey ELSE Done = 1
        UNTIL Done
            WMLKeys := WMLKey:@FM
        REPEAT
        
        WMLKeys[-1,1] = ''	;* Trim trailing @FM
        
        KeyCnt = COUNT(WMLKeys,@FM) + (WMLKeys NE '')
        
        LogRec = ''
        
        FOR N = 1 TO KeyCnt
            WMLKey = WMLKeys<N>
            
            OtParms = 'WO_MAT_LOG':@RM:WMLKey
            
            Send_Info(WMLKey)
            
            WMLRec = obj_Tables('ReadRec',OtParms)
            
            LogFile 		= WMLKey[1,'*']
            thisTransDTM	= WMLKey[COL2()+1,'*']
            Action			= WMLKey[COL2()+1,'*']
            WhCd			= WMLKey[COL2()+1,'*']
            LocCd			= WMLKey[COL2()+1,'*']
            
            BadKey = 0
            
            IF LogFile = ''			THEN BadKey = 1
            IF thisTransDTM = ''	THEN BadKey = 1
            IF Action = ''			THEN BadKey = 1
            IF WhCd =''				THEN BadKey = 1
            IF LocCd = ''			THEN BadKey = 1
            
            IF BadKey THEN
                WMLRec = ''
                Send_Info('Deleted Bad Log Record')
                
            END ELSE
                TransDTM = OCONV(thisTransDTM,'DT2/^HS')
                
                LockedWONos = ''
                LockedCassNos = ''
                LockedTags = ''
                LockedUserID = ''
                LockedToolID = ''
                LockedScanEntry = ''
                
                WONos		= WMLRec<WO_MAT_LOG_WO_NO$>
                ScanUserID	= WMLRec<WO_MAT_LOG_USER_ID$>
                ScanToolID	= WMLRec<WO_MAT_LOG_TOOL_ID$>
                ScanEntry	= WMLRec<WO_MAT_LOG_SCAN_ENTRY$>
                
                WOCnt = COUNT(WONos,@VM) + (WONos NE '')
                
                FOR I = 1 TO WOCnt
                    WONo	= WONos<1,I>
                    CassNo 	= WMLRec<WO_MAT_LOG_CASS_NO$,I>
                    
                    Tag		= WMLRec<WO_MAT_LOG_TAG$,I>
                    
                    aiParms		= LogFile:@RM:WONo:@RM:CassNo:@RM:WhCd:'*':LocCd:@RM:Action:@RM:TransDTM:@RM:ScanUserID:@RM:Tag:@RM:ScanToolID:@RM:ScanEntry
                    
                    Set_Status(0)
                    
                    obj_WO_Mat('AddInvTrans',aiParms)
                    
                    TransDTM = OCONV(thisTransDTM,'DT4/^S')
                    
                    IF Get_Status(errCode) THEN
                        
                        Set_Status(0)
                        LockedWONos<1,-1>	= WONo
                        LockedCassNos<1,-1>	= CassNo
                        LockedTags<1,-1>	= Tag
                        LockedUserID		= ScanUserID
                        LockedToolID		= ScanToolID
                        LockedScanEntry		= ScanEntry
                        
                        //Log the error
                        LogData     = ''
                        LogData<1>  = LoggingDTM
                        LogData<2>  = WONo
                        LogData<3>  = CassNo
                        LogData<4> = Tag
                        LogData<5> = ScanUserId
                        LogData<6> = ScanToolId
                        LogData<7> = ScanEntry
                        LogData<8> = 'Error reported from obj_wo_mat -> Add InvTrans. Check specific log.'
                        Logging_Services('AppendLog', objLogPost, LogData, @RM, @FM)
                    END  else
                        LogData     = ''
                        LogData<1>  = LoggingDTM
                        LogData<2>  = WONo
                        LogData<3>  = CassNo
                        LogData<4> = Tag
                        LogData<5> = ScanUserId
                        LogData<6> = ScanToolId
                        LogData<7> = ScanEntry
                        LogData<8> = 'Successfully process obj_wo_mat -> AddInvTrans. Check specific log file for more details.'
                        Logging_Services('AppendLog', objLogPost, LogData, @RM, @FM)
                    end
                NEXT I
                
                WMLRec<WO_MAT_LOG_WO_NO$>	= LockedWONos
                WMLRec<WO_MAT_LOG_CASS_NO$> = LockedCassNos
                WMLRec<WO_MAT_LOG_TAG$>		= LockedTags
                WMLRec<WO_MAT_LOG_USER_ID$> = LockedUserID
                WMLRec<WO_MAT_LOG_TOOL_ID$>	= LockedToolID
                WMLRec<WO_MAT_LOG_SCAN_ENTRY$> = LockedScanEntry
                
            END	;* End of check for Key complete
            
            OtParms = 'WO_MAT_LOG':@RM:WMLKey:@RM:@RM:WMLRec
            obj_Tables('WriteRec',OtParms)						;* If the record is null, this will delete the record from the table
            
            Send_Info(' ')
            
        NEXT N
        obj_Tables('UnlockRec',ltParms)	;* Places lock on fictional record
        
        Unlock hSysLists, ServiceKeyID else Null
    end
    
RETURN

