COMPILE FUNCTION PM_Spec(EntID,Event,Parm1,Parm2,Parm3,Parm4,Parm5)

/*
	Commuter module for PM_SPEC (Preventive Maintenance Schedule) window
	
	8/24/2016 - John C. Henry, J.C. Henry & Co., Inc.
*/

DECLARE SUBROUTINE Set_Property, End_Dialog, Send_Event, Set_Status, obj_Tables, Post_Event
DECLARE SUBROUTINE ErrMsg, Send_Message, Set_Property, Send_Event, Btree.Extract, obj_AppWindow, Delete
DECLARE SUBROUTINE obj_Notes, Security_Err_Msg, End_Window, Forward_Event, Start_Window, Create_Note, Database_Services

DECLARE FUNCTION Get_Property, Get_Status, Dialog_Box, Utility, obj_Popup, Popup
DECLARE FUNCTION Send_Message, Msg, NextKey, Database_Services, Msg, Error_Services

$INSERT MSG_EQUATES
$INSERT APPCOLORS
$INSERT PM_EQUATES
$Insert PM_Spec_Equates



EQU CRLF$			TO \0D0A\
EQU TAB$			TO \09\
EQU PTYPE$			TO 14		;* From POPUP_EQUATES
EQU COL$PM_KEY		TO 1
EQU COL$EARLY_DTM	TO 2
EQU COL$SCHED_DTM	TO 3
EQU COL$LATE_DTM	TO 4

ErrCode  = ''
ErrorMsg = ''
ErrTitle = 'Error in PM_SPEC routine'
Result = ''

BEGIN CASE
    CASE EntID = @WINDOW
        BEGIN CASE
            CASE Event = 'CLEAR'			; GOSUB Clear
            CASE Event = 'CREATE' 			; GOSUB Create
            Case Event = 'CLOSE'			; GOSUB Close
            CASE Event = 'READ'				; GOSUB Read
            CASE Event[1,3] = 'QBF'			; GOSUB Refresh
        END CASE
        
    CASE EntID = @WINDOW:'.PM_KEYS' AND Event = 'DBLCLK'		; GOSUB PMKeyDC
    CASE EntID = @WINDOW:'.NEW_BUTTON' AND Event = 'CLICK'		; GOSUB New
    CASE EntID = @WINDOW:'.NEW_PM' AND Event = 'CLICK'			; GOSUB NewPM
    CASE EntID = @WINDOW:'.LU_PMS_ID' AND Event = 'CLICK'		; GOSUB LUPMSId
    CASE EntID = @WINDOW:'.LU_TOOL_ID' AND Event = 'CLICK'		; GOSUB LUToolID
    CASE EntID = @WINDOW:'.LU_TIME_CD' AND Event = 'CLICK'		; GOSUB LUInterval
    Case EntID = @Window:'.PUB_ADD_NOTIFY_GROUP' AND Event = 'CLICK'; GoSub AddNotificationGroup
    Case EntID = @Window:'.PUB_DELETE_NOTIFY_GROUP'AND Event = 'CLICK'; GoSub DeleteNotificationGroup
        
    CASE 1
        ErrorMsg = 'Unknown Parameters ':EntID:' - ':Event:' passed to commuter'
        ErrMsg(ErrorMsg)
END CASE

IF ErrorMsg NE '' THEN
    ErrMsg(ErrTitle:@SVM:ErrorMsg)
END

RETURN Result



* * * * * * *
Create:
* * * * * * *
    
    obj_Appwindow('Create',@WINDOW)
    
    GOSUB Refresh
    
RETURN


* * * * * * *
Clear:
* * * * * * *
    
    GOTO Refresh
    
RETURN


* * * * * * *
New:
* * * * * * *
    
    PMSId = NextKey('PM_SPEC')
    
    obj_Appwindow('LoadFormKeys',@WINDOW:@RM:PMSId)
    
RETURN


* * * * * * *
Close:
* * * * * * *
    
    Post_Event('NDW_SCHED_EVENTS','GOTFOCUS')
    
RETURN

* * * * * * *
Refresh:
* * * * * * *
    
    * QBF buttons
    
    Ctrls  = @WINDOW:'.QBF_FIRST_FIX':@RM		; Props  = 'ENABLED':@RM
    Ctrls := @WINDOW:'.QBF_PREV_FIX':@RM		; Props := 'ENABLED':@RM
    Ctrls := @WINDOW:'.QBF_ABS_FIX':@RM			; Props := 'ENABLED':@RM
    Ctrls := @WINDOW:'.QBF_NEXT_FIX':@RM		; Props := 'ENABLED':@RM
    Ctrls := @WINDOW:'.QBF_LAST_FIX':@RM		; Props := 'ENABLED':@RM
    Ctrls := @WINDOW:'.QBF_STOP_FIX'			; Props := 'ENABLED'
    
    IF Get_Property(@WINDOW,'QBFLIST') = '' THEN
        Vals = 0:@RM:0:@RM:0:@RM:0:@RM:0:@RM:0
    END ELSE
        Vals = 1:@RM:1:@RM:1:@RM:1:@RM:1:@RM:1
    END
    
    Set_Property(Ctrls,Props,Vals)
    
    * Turn edit table symbolic column backgrounds to green
    
    ETSymbolics = Get_Property(@WINDOW,'@ET_SYMBOLICS')		;* Loaded during 'Create' in obj_Appwindow
    
    ETCtrls	= ETSymbolics<1>
    ETCols	= ETSymbolics<2>
    
    FOR I = 1 TO COUNT(ETCtrls,@VM) + (ETCtrls NE '')
        ETCtrl	= ETCtrls<1,I>
        ETList = Get_Property(ETCtrl,'LIST')
        FOR Line = 1 TO COUNT(ETList,@FM) + (ETList NE '')
            IF ETList<Line,1> NE '' THEN
                FOR N = 1 TO COUNT(ETCols<1,Line>,@SVM) + (ETCols<1,Line> NE '')
                    stat = Send_Message(ETCtrl,'COLOR_BY_POS',ETCols<1,Line,N>,Line,GREEN$)
                NEXT N
            END
        NEXT Line
    NEXT I
    ThisPMSpecRec        = Database_Services('ReadDataRow', 'PM_SPEC', @ID, 1, 0, 0)
    ADNotificationGroups = ThisPMSpecRec<PM_SPEC_AD_NOTIFICATION_GROUPS$>
    Set_Property(@Window : '.EDT_NOTIFICATION_GROUPS', 'ARRAY', ADNotificationGroups)
    
    
RETURN


* * * * * * *
Read:
* * * * * * *
    
    PMId = Get_Property(@WINDOW:'.PM_ID','DEFPROP')
    
    IF RowExists('PM',PMId) ELSE
        IF Get_Property(@WINDOW:'.ENTER_DTM','DEFPROP') = '' THEN
            CurrDTM = OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTH')
            Set_Property(@WINDOW:'.ENTER_DTM','DEFPROP',CurrDTM)
        END
    END
    
    GOSUB Refresh
    
RETURN


* * * * * * *
LUPMSId:
* * * * * * *
    
    PMSId = Popup(@WINDOW,'','PM_SPEC')
    
    IF PMSId = '' THEN RETURN
    
    
    IF INDEX(PMSId,@VM,1) THEN
        CONVERT @VM TO @FM IN PMSId
        Send_Event(@WINDOW,'QBFINIT')
        Set_Property(@WINDOW,'QBFLIST',PMSId)
        Send_Event(@WINDOW,'QBFFIRST')
    END ELSE
        obj_AppWindow('LoadFormKeys',@WINDOW:@RM:PMSId)
    END
    
RETURN


* * * * * * *
LUToolID:
* * * * * * *
    
    ToolID = Popup(@WINDOW,'','TOOLS')
    IF ToolID = '' THEN RETURN
    
    obj_Appwindow('LUValReturn',ToolID:@RM:@WINDOW:'.TOOL_ID')
    
RETURN


* * * * * * *
LUInterval:
* * * * * * *
    
    Typeover = ''
    Typeover<PTYPE$> = 'E'		;* Returns entire row(s)
    
    PMInterval = Popup(@WINDOW,Typeover,'PM_INTERVAL')
    
    IF PMInterval NE '' THEN
        TimeCode = PMInterval<1,1>
        IntDays	= PMInterval<1,3>
        obj_AppWindow('LUValReturn',TimeCode:@RM:@WINDOW:'.INT_TIME_CD')
        obj_AppWindow('LUValReturn',IntDays:@RM:@WINDOW:'.INTERVAL_TIME')
    END
    
RETURN


* * * * * * *
NewPM:
* * * * * * *
    
    PMSId = Get_Property(@WINDOW:'.PMS_ID','DEFPROP')
    
    Send_Event(@WINDOW,'WRITE')								;* Write current PM_SPEC record
    
    PMNo                = NextKey('PM')								;* Next PMKey
    PMRec               = ''
    PMRec<PM_PMS_ID$>   = PMSId								;* New PMRec
    StartTime           = Get_Property(@Window:'.EDL_PM_START_TIME', 'TEXT')
    PMRec<PM_SCHED_TM$> = IConv(StartTime, 'MTS')
    otParms             = 'PM':@RM:PMNo:@RM:@RM:PMRec
    obj_Tables('WriteRec',otParms)							;* Write new PM record
    
    IF Get_Status(errCode) THEN
        ErrMsg(errCode)
        RETURN
    END
    
    obj_Appwindow('ViewRelated','PM':@RM:PMNo:@RM:@RM)		;* View new related PM record
    
    Set_Property('PM','@RETURN_KEY',PMSId)
    
    
    
RETURN


* * * * * * *
PMKeyDC:
* * * * * * *
    
    CtrlEntID = @WINDOW:'.PM_KEYS'
    
    CurrPos = Get_Property(CtrlEntID,'SELPOS')
    CurrCol = CurrPos<1>
    CurrRow = CurrPos<2>
    
    PMKeyList = Get_Property(CtrlEntID,'LIST')
    
    IF PMKeyList<CurrRow,COL$PM_KEY> NE '' THEN
        PMKey = PMKeyList<CurrRow,COL$PM_KEY>
        obj_Appwindow('ViewRelated','PM':@RM:PMKey:@RM:@RM)
        
    END
    
RETURN

AddNotificationGroup:
    Def = ""
    Def<MTEXT$> = "Enter in an existing Distribution List Name:"
    Def<MTYPE$> = "RE"
    Def<MICON$> = "?"
    Group = Msg(@window, Def)
    
    If Group NE '' then
        //Save any uncommited changes
        UpdatedRec = Get_Property (@Window, "ATRECORD")
        Locate Group in UpdatedRec<PM_SPEC_AD_NOTIFICATION_GROUPS$> using @VM setting gPos then
            Text = "Distribution list is already assigned."
            Msg(@window, Text)
        end else
            UpdatedRec<PM_SPEC_AD_NOTIFICATION_GROUPS$, -1> = Group
            Database_Services('WriteDataRow', 'PM_SPEC', @ID, UpdatedRec, 1, 0, 1)
            if Error_Services('NoError') then
                Set_Property (@Window, "ATRECORD", UpdatedRec)
                Set_Property(@Window, 'SAVEWARN', 0)
                Text = "Distribution list has been added."
                Msg(@window, Text)
                GoSub   Refresh
            end else
                ErrMsg = Error_Services('GetMessage')
                Text   = "Error while adding to PM Spec record. " : ErrMsg
                Msg(@window, Text)
            end
        end     
    end
return

DeleteNotificationGroup:
    PMSpecID      = @ID
    NotifArray    = Get_Property(@Window:'.EDT_NOTIFICATION_GROUPS', 'ARRAY')
    SelectedPos   = Get_Property(@Window: '.EDT_NOTIFICATION_GROUPS', "SELPOS")<2>
    GroupToDelete = NotifArray<1,SelectedPos>
    If GroupToDelete NE '' then
        UpdatedRec                                      = Get_Property (@Window, "ATRECORD")
        Locate GroupToDelete in UpdatedRec<PM_SPEC_AD_NOTIFICATION_GROUPS$> using @VM setting gPos then
            UpdatedRec = Delete(UpdatedRec, PM_SPEC_AD_NOTIFICATION_GROUPS$, gPos, 0)
            Database_Services('WriteDataRow', 'PM_SPEC', PMSpecID, UpdatedRec, 1, 0, 1)
            if Error_Services('NoError') then
                Set_Property (@Window, "ATRECORD", UpdatedRec)
                Set_Property(@Window, 'SAVEWARN', 0)
                Text       = "Distribution list has been removed."
                Msg(@window, Text)
                GoSub   Refresh
            end else
                ErrMsg = Error_Services('GetMessage')
                Text   = "Error while deleting notification group PM Spec record. " : ErrMsg
                Msg(@window, Text)
            end
        end    
    end
return


