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

/*
	Commuter module for PM (Preventive Maintenance) window
	
	4/4/2016 - John C. Henry, J.C. Henry & Co., Inc.
*/

DECLARE SUBROUTINE Set_Property, End_Dialog, Send_Event, Set_Status, obj_Tool_Log, obj_PM_Spec, PM_Services
DECLARE SUBROUTINE ErrMsg, Send_Message, Set_Property, Send_Event, Btree.Extract, obj_AppWindow, Error_Services
DECLARE SUBROUTINE obj_Notes, Security_Err_Msg, End_Window, Forward_Event, Start_Window, Create_Note, Post_Event

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

$INSERT LOGICAL
$INSERT MSG_EQUATES
$INSERT APPCOLORS
$INSERT LSL_USERS_EQUATES
$INSERT TOOL_EQUATES
$INSERT PM_SPEC_EQUATES

EQU CRLF$	TO \0D0A\
EQU TAB$	TO \09\

EQU COL$CAL_DT		TO 1
EQU COL$CAL_BY		TO 2
EQU COL$CAL_NOTE	TO 3

ErrTitle = 'Error in PM routine'
ErrorMsg = ''
ErrCode  = ''

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 = 'WRITE'			; GOSUB Write
            CASE Event[1,3] = 'QBF'			; GOSUB Refresh
        END CASE
        
    CASE EntID = @WINDOW:'.NEW' AND Event = 'CLICK'				; GOSUB New
    CASE EntID = @WINDOW:'.LU_PM_ID' AND Event = 'CLICK'		; GOSUB LUPMId
    CASE EntID = @WINDOW:'.LU_INTERVAL' AND Event = 'CLICK'		; GOSUB LUInterval
    CASE EntID = @WINDOW:'.LU_DT' AND Event = 'CLICK'			; GOSUB LUDate
    CASE EntID = @WINDOW:'.START_PM' AND Event = 'CLICK'		; GOSUB StartPM
    CASE EntID = @WINDOW:'.PM_COMP'  AND Event = 'CLICK'		; GOSUB PMComp
        
        
    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

RETURN


* * * * * * *
New:
* * * * * * *
    
    NextPMKey = NextKey('PM')
    
    obj_AppWindow('LoadFormKeys',@WINDOW:@RM:NextPMKey)
    
    
RETURN


* * * * * * *
LUPMId:
* * * * * * *
    
    PMId = Popup(@WINDOW,'','PM')
    
    IF PMId NE '' THEN
        obj_AppWindow('LoadFormKeys',@WINDOW:@RM:PMId)
    END
    
RETURN


* * * * * * *
LUInterval:
* * * * * * *
    
    PMInterval = Popup(@WINDOW,'','PM_INTERVAL')
    
    
    IF PMInterval NE '' THEN
        obj_AppWindow('LUValReturn',PMInterval:@RM:@WINDOW:'.CAL_INTVL')
    END
    
    
RETURN



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



* * * * * * *
Write:
* * * * * * *
    
    * Stub
    
RETURN

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

* * * * * * *
Clear:
* * * * * * *
RETURN

* * * * * * *
Refresh:
* * * * * * *
    
    Units = Get_Property(@WINDOW:'.UNITS','DEFPROP')
    
    DateVals = 0:@RM:0:@RM:0		; DateProps = 'ENABLED':@RM:'ENABLED':@RM:'ENABLED'
    TimeVals = 0:@RM:0				; TimeProps = 'ENABLED':@RM:'ENABLED'
    QtyVals  = 0:@RM:0				; QtyProps  = 'ENABLED':@RM:'ENABLED'
    
    IF Units = 'D' OR Units = 'T' OR Units EQ 'M'	THEN DateVals = 1:@RM:1:@RM:1
    IF Units = 'T'    				THEN TimeVals = 1:@RM:1
    IF Units = 'Q'					THEN QtyVals  = 1:@RM:1
    
    dtCtrls  = @WINDOW:'.SCHED_DT_LABEL':@RM
    dtCtrls	:= @WINDOW:'.SCHED_DT':@RM
    dtCtrls	:= @WINDOW:'.LU_DT'
    
    tCtrls	 = @WINDOW:'.SCHED_TM_LABEL':@RM
    tCtrls	:= @WINDOW:'.SCHED_TM'
    
    qCtrls	 = @WINDOW:'.SCHED_QTY_LABEL':@RM
    qCtrls	:= @WINDOW:'.SCHED_QTY'
    
    Set_Property( dtCtrls , DateProps , DateVals )
    Set_Property( tCtrls , TimeProps , TimeVals )
    Set_Property( qCtrls , QtyProps , QtyVals )
    
    * 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
    
    PMSId = Get_Property(@Window:'.PMS_ID', 'TEXT')
    If ( (PMSId EQ 653) or (PMSId EQ 654) ) then
        Set_Property(@Window:'.PASS_FAIL_LABEL', 'VISIBLE', True$)
        Set_Property(@Window:'.EDL_PASS_FAIL', 'VISIBLE', True$)
        PassFail = Get_Property(@Window:'.EDL_PASS_FAIL', 'TEXT')
        Begin Case
            Case PassFail EQ ''
                Set_Property(@Window:'.EDL_PASS_FAIL', 'BACKCOLOR', READONLY_GREEN$)
            Case PassFail EQ 'Pass'
                Set_Property(@Window:'.EDL_PASS_FAIL', 'BACKCOLOR', BGREEN$)
            Case PassFail EQ 'Fail'
                Set_Property(@Window:'.EDL_PASS_FAIL', 'BACKCOLOR', BRED$)
        End Case
    end else
        Set_Property(@Window:'.PASS_FAIL_LABEL', 'VISIBLE', False$)
        Set_Property(@Window:'.EDL_PASS_FAIL', 'VISIBLE', False$)
    end
    
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)
            Set_Property(@WINDOW:'.ENTER_BY','DEFPROP',@USER4)
        END
    END
    
    GOSUB Refresh
    
RETURN

LUDate:
    ReturnCtrl = @WINDOW:'.SCHED_DT'
    Set_Property(@Window, 'FOCUS', ReturnCtrl)
    ReturnVal = Popup_Month()
RETURN
* * * * * * *
StartPM:
* * * * * * *
    
    PMNo	= Get_Property(@WINDOW:'.PM_NO','DEFPROP')
    PMSId	= Get_Property(@WINDOW:'.PMS_ID','DEFPROP')
    
    ToolID = XLATE('PM_SPEC',PMSId,PM_SPEC_TOOL_ID$,'X')
    
    IF PMNo = '' OR PMSId = '' OR ToolID = '' THEN
        ErrMsg('Incomplete record in the window')
        RETURN
    END
    
    SigDTM       = OCONV( Date(), 'D4/' ):' ':OCONV( Time(), 'MTS' )
    
    PM_Services('StartPM', PMNo, @User4, IConv(SigDTM, 'DT'))
    
    If Error_Services('HasError') then
        Error_Services('DisplayError')
    end
    
    Post_Event(@Window, 'READ')
    
RETURN


* * * * * *
PMComp:
* * * * * *
    
    PMNo			= Get_Property(@WINDOW:'.PM_NO','DEFPROP')
    PMSId			= Get_Property(@WINDOW:'.PMS_ID','DEFPROP')
    ToolCycleCnt	= Get_Property(@WINDOW:'.TOOL_CYCLE_CNT','DEFPROP')
    
    IF ToolCycleCnt = '' THEN ToolCycleCnt = '0'
    
    ToolID = XLATE('PM_SPEC',PMSId,PM_SPEC_TOOL_ID$,'X')
    
    IF PMNo = '' OR PMSId = '' OR ToolID = '' THEN
        ErrMsg('Incomplete record in the window')
        RETURN
    END
    
    Valid = Dialog_Box('NDW_VERIFY_USER', @Window, @User4)
    Valid = Valid<1>
    
    IF NOT(Valid) THEN
        RETURN 0
    END

    SigDTM = OCONV( Date(), 'D4/' ):' ':OCONV( Time(), 'MTS' )

    PM_Services('CompletePM', PMNo, @User4, IConv(SigDTM, 'DT'))
    If Error_Services('HasError') then
        Error_Services('DisplayError')
    end
    
    Post_Event(@Window, 'READ')
    
RETURN

