COMPILE FUNCTION Sched(EntID,Event,Parm1,Parm2,Parm3,Parm4,Parm5)
#pragma precomp SRP_PreCompiler

/*
	Commuter module for SCHED (Reactor Schedule) window
	
	06/30/2014 - John C. Henry, J.C. Henry & Co., Inc.
*/

DECLARE SUBROUTINE Set_Property, End_Dialog, Send_Event, Set_Status,ErrMsg, obj_Appwindow

DECLARE FUNCTION Msg, obj_Appwindow, Get_Property, Send_Message, obj_Tables, Post_Event, MemberOf

$INSERT APPCOLORS
$INSERT SCHED_EQUATES
$INSERT SCHED_DET_EQUATES

EQU COL$SEQ			TO 1
EQU COL$WO_NO		TO 2
EQU COL$WO_STEP		TO 3
EQU COL_CUST_NAME	TO 4
EQU COL_DESC		TO 5


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



ErrTitle = 'Error in Sched Commuter'
ErrorMsg = ''

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 = 'WRITE'			; GOSUB Write
			CASE Event = 'READ'				; GOSUB Read
			CASE Event[1,3] = 'QBF'			; GOSUB Refresh
		END CASE
		
	
	CASE EntID = @WINDOW:'.NEW_BUTTON' AND Event = 'CLICK'	 		; GOSUB AddNew
	CASE EntID = @WINDOW:'.REORDER' AND Event = 'CLICK'				; GOSUB MoveUp
	
	
	CASE EntID = @WINDOW:'.SCHED_DETAIL'
		BEGIN CASE 
			CASE Event = 'POSCHANGED'		; GOSUB DetailPC
			CASE Event = 'DBLCLK'			; GOSUB DetailDC
		END CASE
		
		
	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)


RETURN


* * * * * * *
Read:
* * * * * * *

GOSUB Refresh

RETURN



* * * * * * *
Clear:
* * * * * * *

GOSUB Refresh

RETURN

* * * * * * *
Write:
* * * * * * *

*void = Post_Event(@WINDOW,'CLOSE')

RETURN 1



* * * * * * *
Close:
* * * * * * *


RETURN


* * * * * * *
Refresh:
* * * * * * *

CtrlEntID = @WINDOW:'.SCHED_DETAIL'
CurrPos = Get_Property(CtrlEntID,'SELPOS')
CurrCol = CurrPos<1>
CurrRow = CurrPos<2>

IF CurrRow > 1 THEN Prop = 1 ELSE Prop = 0
Set_Property(@WINDOW:'.REORDER','ENABLED',Prop)


* 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,I>,@SVM) + (ETCols<1,I> NE '')
				stat = Send_Message(ETCtrl,'COLOR_BY_POS',ETCols<1,I,N>,Line,GREEN$)
			NEXT N
		END
	NEXT Line
	
NEXT I

RETURN


* * * * * * *
DetailPC:
* * * * * * * 

CtrlEntID = @WINDOW:'.SCHED_DETAIL'
CurrPos = Get_Property(CtrlEntID,'SELPOS')
CurrCol = CurrPos<1>
CurrRow = CurrPos<2>

IF CurrRow > 1 THEN Prop = 1 ELSE Prop = 0
Set_Property(@WINDOW:'.REORDER','ENABLED',Prop)


RETURN


*********
DetailDC:
*********

    if (MemberOf(@USER4, "MASTER_SCHED") OR MemberOf(@USER4, "OI_ADMIN")) then
        
        CtrlEntID = @WINDOW:'.SCHED_DETAIL'
        CurrPos = Get_Property(CtrlEntID,'SELPOS')
        CurrCol = CurrPos<1>
        CurrRow = CurrPos<2>

        ReactNo = Get_Property(@WINDOW:'.REACT_NO','DEFPROP')
        SchedDt = ICONV(Get_Property(@WINDOW:'.SCHED_DT','TEXT'),'D')

        IF ((ReactNo NE '') AND (SchedDt NE '')) THEN

            SchedSeq = Get_Property(CtrlEntID,'CELLPOS',COL$SEQ:@FM:CurrRow)

            IF (SchedSeq NE '') THEN
                
                SchedDetKey = ReactNo:'*':SchedDt:'*':SchedSeq

                Send_Event(@WINDOW,'WRITE')

                DetWindow	= 'SCHED_DET'
                DetKeys		= SchedDetKey
                DefaultRec	= ''
                RetKey		= ReactNo:'*':SchedDt
                RetWin		= @WINDOW
                RetPage		= 1
                RetCtrl		= CtrlEntID
                RetPos		= CurrPos

                obj_Appwindow('ViewNewDetail',DetWindow:@RM:DetKeys:@RM:DefaultRec:@RM:RetKey:@RM:RetPage:@RM:RetCtrl:@RM:RetPos)

            end
        end
    end

RETURN


* * * * * * *
AddNew:
* * * * * * *

ReactNo = Get_Property(@WINDOW:'.REACT_NO','DEFPROP')
SchedDt = ICONV(Get_Property(@WINDOW:'.SCHED_DT','TEXT'),'D')

IF ReactNo = '' OR SchedDt = '' THEN RETURN

CtrlEntID = @WINDOW:'.SCHED_DETAIL'

CurrPos = Get_Property(CtrlEntID,'SELPOS')
CurrCol = CurrPos<1>
CurrRow = CurrPos<2>

SeqNos = Get_Property(CtrlEntID,'ARRAY')<COL$SEQ>

LOOP
UNTIL SeqNos[-1,1] NE @VM OR SeqNos = ''
	SeqNos[-1,1] = ''
REPEAT

LastSeq = SeqNos[-1,'B':@VM]

IF LastSeq = '' THEN LastSeq = 0

NextSeq = LastSeq + 1

SchedDetKey = ReactNo:'*':SchedDt:'*':NextSeq

Send_Event(@WINDOW,'WRITE')

DetWindow	= 'SCHED_DET'
DetKeys		= SchedDetKey
DefaultRec	= ''
RetKey		= ReactNo:'*':SchedDt
RetWin		= @WINDOW
RetPage		= 1
RetCtrl		= CtrlEntID
RetPos		= CurrPos

obj_Appwindow('ViewNewDetail',DetWindow:@RM:DetKeys:@RM:DefaultRec:@RM:RetKey:@RM:RetPage:@RM:RetCtrl:@RM:RetPos)

RETURN


* * * * * * *
MoveUp:
* * * * * * *

CtrlEntID = @WINDOW:'.SCHED_DETAIL'
CurrPos = Get_Property(CtrlEntID,'SELPOS')
CurrCol = CurrPos<1>
CurrRow = CurrPos<2>

ReactNo = Get_Property(@WINDOW:'.REACT_NO','DEFPROP')
SchedDt = ICONV(Get_Property(@WINDOW:'.SCHED_DT','TEXT'),'D')

IF ReactNo = '' OR SchedDt = '' THEN RETURN

CurrList = Get_Property(CtrlEntID,'LIST')

CurrSeq = CurrList<CurrRow,COL$SEQ>
PrevSeq = CurrList<CurrRow - 1,COL$SEQ>

CurrKey = ReactNo:'*':SchedDt:'*':CurrSeq
PrevKey = ReactNo:'*':SchedDt:'*':PrevSeq

Send_Event(@WINDOW,'WRITE')

otParms = 'SCHED_DET':@RM:CurrKey
CurrRec = obj_Tables('ReadRec',otParms)

otParms = 'SCHED_DET':@RM:PrevKey
PrevRec = obj_Tables('ReadRec',otParms)

otParms = 'SCHED_DET':@RM:PrevKey:@RM:@RM:CurrRec
void = obj_Tables('WriteRec',otParms)

otParms = 'SCHED_DET':@RM:CurrKey:@RM:@RM:PrevRec
void = obj_Tables('WriteRec',otParms)

obj_Appwindow('LoadFormKeys',@WINDOW:@RM:ReactNo:'*':SchedDt)


RETURN






