COMPILE FUNCTION obj_Tool_Log(Method,Parms)
/*
	Methods for TOOL_LOG table
    
	10/22/2007 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    SetMode()		;* Sets Mode for specified Tool
    
*/
#pragma precomp SRP_PreCompiler


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, FieldStore, Database_Services
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, Btree.Extract, ErrMsg, Database_Services
DECLARE SUBROUTINE  obj_Notes, Send_Info

$INSERT RLIST_EQUATES
$INSERT TOOL_LOG_EQUATES
$INSERT TOOL_EQUATES
$INSERT MSG_EQUATES
$INSERT PM_EQUATES

ErrTitle = 'Error in Stored Procedure "obj_Tool_Log"'
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 = 'SetMode'		; GOSUB SetMode
    CASE Method = 'InitialLog'	; GOSUB InitialLog
        
    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


* * * * * * *
InitialLog:
* * * * * * *
    
    ToolID		= Parms[1,@RM]
    
    IF ToolID = '' THEN ErrorMsg = 'Null Parameter "ToolID" passed to object. (':Method:')'
    
    IF ErrorMsg NE '' THEN RETURN
    
    NewModeDTM	= ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')
    CurrUser	= @USER4
    
    
    NewLogRec = ''
    NewLogRec<TOOL_LOG_START_USER$>		= CurrUser
    NewLogRec<TOOL_LOG_START_REASON$>	= 'Initial Log Entry'
    NewLogRec<TOOL_LOG_TOOL_MODE$>		= 'OUT'
    
    ntParms = 'TOOL_LOG':@RM:ToolID:'*':NewModeDTM:@RM:@RM:NewLogRec
    obj_Tables('WriteRec',ntParms)										;* Write new record
    
    IF Get_Status(errCode) THEN 
        ErrorMsg = 'Unable to write NewLogRec.'					
        obj_Tables('UnlockRec',ctParms)									;* Otherwise unlock old one
        RETURN
    END
    
RETURN



* * * * * * *
SetMode:
* * * * * * *
    
    ToolID		= Parms[1,@RM]
    CurrModeDTM	= Parms[COL2()+1,@RM]
    NewMode		= Parms[COL2()+1,@RM]
    NewReason	= Parms[COL2()+1,@RM]
    
    IF ToolID = ''		THEN ErrorMsg = 'Null Parameter "ToolID" passed to object. (':Method:')'
    IF NewMode = ''		THEN ErrorMsg = 'Null Parameter "New" passed to object. (':Method:')'
    
    IF NewReason = ''	THEN NewReason = 'No Reason Entered'
    
    IF ErrorMsg NE '' THEN RETURN
    
    IF CurrModeDTM = '' THEN CurrModeDTM = OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS')
    
    thisCurrDTM = ICONV(CurrModeDTM,'DT')
    IF thisCurrDTM = '' THEN
        ErrorMsg = 'Invalid parameter CurrModeDTM ':QUOTE(CurrModeDTM):' passed to object. (':Method:')'
        IF ErrorMsg NE '' THEN RETURN
    END
    
    
    NewModeDTM	= ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')
    CurrUser	= @USER4
    
    ctParms = 'TOOL_LOG':@RM:ToolID:'*':thisCurrDTM
    obj_Tables('LockRec', ctParms)										;* Lock the currently active log record
    
    IF Get_Status(errCode) THEN 
        ErrorMsg = 'Unable to set current status.'
        RETURN
    END
    
    NewLogRec = ''
    NewLogRec<TOOL_LOG_START_USER$>		= CurrUser
    NewLogRec<TOOL_LOG_START_REASON$>	= NewReason
    NewLogRec<TOOL_LOG_TOOL_MODE$>		= NewMode
    
    ntParms = 'TOOL_LOG':@RM:ToolID:'*':NewModeDTM:@RM:@RM:NewLogRec
    obj_Tables('WriteRec', ntParms)										;* Write new record
    
    IF Get_Status(errCode) THEN 
        ErrorMsg = 'Unable to write NewLogRec.'					
        obj_Tables('UnlockRec',ctParms)									;* Otherwise unlock old one
        RETURN
    END
    
    CurrLogRec = obj_Tables('ReadOnlyRec', ctParms)
    
    CurrLogRec<TOOL_LOG_STOP_DTM$> = NewModeDTM						;* Put stop time in current log
    CurrLogRec<TOOL_LOG_STOP_USER$> = CurrUser
    
    ctParms = FieldStore(ctParms,@RM,4,0,CurrLogRec)
    
    obj_Tables('WriteRec', ctParms)									;* Write and unlock the record
    
    If NewMode EQ 'OUT' then
        Open 'PM' to PMHandle then
            // Remove any scheduled PM records for this TOOL ID
            NewModeDt = NewModeDTM[1, 'F.']
            NewModeTm = (NewModeDTM[COL2()+1, 999] * 86400)
            NewModeTm = NewModeTm[1, 'F.']
            Query = 'SELECT PM WITH TOOL_ID EQ "':ToolID:'" AND WITH STATUS EQ "SCHED" AND WITH SCHED_DT GE ':NewModeDt
            
            RList(Query, TARGET_ACTIVELIST$, '', '', '' )
            
            Done = 0
            LOOP
                READNEXT PmID ELSE Done = 1
            UNTIL Done
                READ PmRec FROM PMHandle,PmID THEN
                    SchedTm = PmRec<PM_SCHED_TM$>
                    If ( ( (SchedTm NE '') and (SchedTm GE NewModeTm) ) or (SchedTm EQ '') ) then
                        // Remove it                        
                        Database_Services('DeleteDataRow', 'PM', PmID, True$, False$)
                    end
                    
                END	;* End of PmRec read   
            REPEAT
        end
    end
    
RETURN



