COMPILE FUNCTION Comm_Dialog_SAP_Comm( Instruction, Parm1 )

/*
	Commuter Module for SAP Communications Window
	
	J.C. Henry, Inc. - John C. Henry
	
 */

DECLARE SUBROUTINE Center_Window,StatusLine,Set_Property,End_Dialog, Send_Event, Set_Status, Adios, obj_SAP, obj_Notes, obj_Post_Log, RList, SAP_Services
DECLARE SUBROUTINE ErrMsg, Send_Message, obj_Tables, Post_Event,  Send_INfo, ShowWindow, obj_Appwindow, Send_Dyn,ErrLog, obj_WO_Mat_Log, Sleepery, Yield, SRP_Stopwatch, SRP_TCPClient
DECLARE FUNCTION Get_Property, Get_Status,Dialog_Box, FindWindow, Utility, SAP_Services, Database_Services, SRP_TCPClient

$INSERT QUOTE_SIGS_EQU
$INSERT NOTIFICATION_EQU
$INSERT APPCOLORS
$INSERT LOGICAL

EQU CRLF$               TO \0D0A\
EQU InBoundLimit$       TO 20
EQU OutBoundLimit$      TO 10
EQU DEFAULT_INTERVAL$   TO 10 /* Seconds 	 */

$INSERT ENVIRON_CONSTANTS

ErrTitle = 'Error in Comm_Dialog_SAP_Comm routine'
ErrorMsg = ''

Instructions  = 'Create'
Instructions := @FM:'Timer'
Instructions := @FM:'StartStop'
Instructions := @FM:'CommStartStop'
Instructions := @FM:'Close'

RetVal = ''

BEGIN CASE
	CASE Instruction = 'Create'				                                        ; GOSUB Create
	CASE Instruction = 'Timer'				                                        ; GOSUB Timer
	CASE Instruction EQ @Window : '.LOG_ON' AND Parm1 EQ 'CLICK'                    ; GoSub LOG_ON.CLICK
	CASE Instruction EQ @Window : '.TRANS_QTY' AND Parm1 EQ 'LOSTFOCUS'             ; GoSub TRANS_QTY.LOSTFOCUS
	CASE Instruction EQ @Window : '.SAP_HOLD_START_DTM' AND Parm1 EQ 'LOSTFOCUS'    ; GoSub SAP_HOLD_START_DTM.LOSTFOCUS
	CASE Instruction EQ @Window : '.SAP_HOLD_STOP_DTM' AND Parm1 EQ 'LOSTFOCUS'     ; GoSub SAP_HOLD_STOP_DTM.LOSTFOCUS
	CASE Instruction = 'Close'				                                        ; GOSUB Close
	CASE 1
		ErrorMsg = 'Unknown Instruction ':QUOTE(Instruction):' passed to routine'
		
END CASE


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


RETURN RetVal


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

	obj_Appwindow('Create',@WINDOW)

	otParms = 'SYSOBJ':@RM:'SAP_COMM'
	obj_Tables('LockRec',otParms)	;* Places lock on fictional record

	IF Get_Status(errCode) THEN
		ErrorMsg = ErrTitle:@SVM:'Another SAP Communications Server is already running.'
		ErrMsg(ErrorMsg)
		Post_Event(@WINDOW,'CLOSE')
		RETURN
	END

	Set_Property(@WINDOW,'@LOCKED',1)				;* Set ad-hoc property '@LOCKED' to true
	Set_Property(@WINDOW,'TIMER','0':@FM:'1000')			;* Set timer event to start in 1000 msecs (1 second). Do not set for auto-repeat. Let the TIMER event handle this.

	Set_Property(@WINDOW:'.TRANS_QTY', 'INVALUE', SAP_Services('GetSAPTransactionLimit'))
	Set_Property(@WINDOW:'.SAP_HOLD_START_DTM', 'INVALUE', SAP_Services('GetSAPHoldStartDateTime'))
	Set_Property(@WINDOW:'.SAP_HOLD_STOP_DTM', 'INVALUE', SAP_Services('GetSAPHoldStopDateTime'))

RETURN


LOG_ON.CLICK:

    LoggingFlag = Get_Property(@Window : '.LOG_ON', 'CHECK')
    SAP_Services('SetTransactionLoggingFlag', LoggingFlag)

return


TRANS_QTY.LOSTFOCUS:

    TransactionLimit    = Get_Property(@Window : '.TRANS_QTY', 'TEXT')
    SAP_Services('SetSAPTransactionLimit', TransactionLimit)

return


SAP_HOLD_START_DTM.LOSTFOCUS:

    HoldStartDTM    = Get_Property(@Window : '.SAP_HOLD_START_DTM', 'INVALUE')
    SAP_Services('SetSAPHoldStartDateTime', HoldStartDTM)

return


SAP_HOLD_STOP_DTM.LOSTFOCUS:

    HoldStopDTM     = Get_Property(@Window : '.SAP_HOLD_STOP_DTM', 'INVALUE')
    SAP_Services('SetSAPHoldStopDateTime', HoldStopDTM)

return

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

	UNLOCK ALL
    NumWindows  = Utility('WINCOUNT')
    If NumWindows EQ 1 then Set_Property('SYSTEM','IDLEPROC','ADIOS')

RETURN


* * * * * * *
Timer:
* * * * * * *

	Set_Status(0)

	Set_Property(@WINDOW:'.CURR_TIME','TEXT',OCONV( Time(), 'MTHS' ))		;* Clock on window

	LastTime = Get_Property(@WINDOW,'@LAST_TIME')

	* * * Added 1/29/2015 JCH/DKK * * *

	*** Used to temporarily stop the SAP interface during specified time periods, like SAP-PI outage, EOM procesing, VM backups. 

	SAPOutage       = SAP_Services('GetSAPOutageFlag')
	IF SAPOutage THEN
		Set_Property(@WINDOW:'.BACKUP_HOLD','VISIBLE',1)
	END ELSE
		Set_Property(@WINDOW:'.BACKUP_HOLD','VISIBLE',0)
	END


    IF LastTime > Time() THEN
        Set_Property(@WINDOW,'@LAST_TIME', Time())				;* Midnight -> clock resets to 0
        LastTime = Time()
        // Call the SRPEngineService and pass it the command
        Command = 'RUN SAP_SERVICES "SendReconcile"'
        TCPServerSettings@  = Database_Services('ReadDataRow', 'APP_INFO', 'SQL_TCPIP_SETTINGS')
        TcpClientHandle     = 0
        If SRP_TcpClient(TcpClientHandle, 'CONNECT', TCPServerSettings@<1>, TCPServerSettings@<2>) then
            SRP_TcpClient(TcpClientHandle, 'SEND_SES', Command)
            SRP_TcpClient(TcpClientHandle, 'CLOSE_SES')
        end
*			obj_SAP('SendReconcile')								;* WIP Recom data sent to SAP once each day at midnight
    END

    CommOn		= Get_Property(@WINDOW,'@COMM_ACTIVE')
    CommOnTime	= Get_Property(@WINDOW,'@COMM_ACTIVE_TIME')
    
    IF CommOn THEN
        Send_Info('Comm Active...')
        IF Time() - CommOnTime > 22 THEN
            Set_Property(@WINDOW,'@COMM_ACTIVE',0)			;* FTP window timeout takes about 21 seconds
            Set_Property(@WINDOW,'@COMM_ACTIVE_TIME','')	;* Hold off retrying until 
        END
        
    END ELSE
        Interval = DEFAULT_INTERVAL$
        IF Time() >= (LastTime + Interval) Then				;* SAP_Comm cycle time setting = 10 sec - changed to 8 sec -dkk 1/14/15
        
            Set_Property(@WINDOW,'@LAST_TIME', Time())
            
        END	;* End of comm interval time check
        
    END	;* End of check for @COMM_ON flag


	*********************************************
	* Refresh the SAP Buffer Outbound Qty Count *
	*********************************************

    OutboundQty = SAP_Services('GetOutboundPending')

    Set_Property(@WINDOW:'.OUTBOUND_QTY','TEXT', OutboundQty)
    
    If (OutboundQty >= OutBoundLimit$) Then
        OutBoundMsgSent = Get_Property(@Window,'@OutBoundMsgSent')
        If (OutBoundMsgSent NE True$) Then
            Recipients	    = XLATE('NOTIFICATION', 'FI_SUPPORT', 'USER_ID', 'X')
            SentFrom		= 'OI Admin'
            Subject			= 'SAP Comm OUTBOUND Pending Buffer'
            Message			= 'SAP Comm OUTBOUND Pending Buffer is filling up with ':OutboundQty:' records not sent. Check SAP interface.'
            AttachWindow	= ''
            AttachKey		= ''   
            SendToGroup		= ''   

            Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
            obj_Notes('Create',Parms)

            Set_Property(@WINDOW,'@OutBoundMsgSent', True$)
        end

    end else
        Set_Property(@WINDOW,'@OutBoundMsgSent', False$)
    end
		

	************************************************
	* Refresh the SAP Buffer Inbound Pending Count *
	************************************************
	Set_Status(0)

    InboundPending = SAP_Services('GetInboundPending')
    If InboundPending < 0 then InboundPending = 0
    Set_Property(@WINDOW:'.INBOUND_PENDING','TEXT', InboundPending)
    
    If (InboundPending >= InBoundLimit$) Then

        InBoundMsgSent = Get_Property(@Window,'@InBoundMsgSent')
        If (InBoundMsgSent NE True$) Then
            Recipients		= XLATE('NOTIFICATION', 'FI_SUPPORT', 'USER_ID', 'X')
            SentFrom		= 'OI Admin'
            Subject			= 'SAP Comm INBOUND Pending Buffer'
            Message			= 'SAP Comm INBOUND Pending Buffer is filling up with ':InBoundPending:' records not received. Check SAP interface.'
            AttachWindow	= ''
            AttachKey		= ''   
            SendToGroup		= ''   

            Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
            obj_Notes('Create',Parms)

            Set_Property(@WINDOW,'@InBoundMsgSent', True$)
        end
        
    end else
        Set_Property(@WINDOW,'@InBoundMsgSent', False$)
    end

	Yield(); Yield(); Yield(); Yield(); Yield(); Yield(); Yield(); Yield(); Yield(); Yield()
	Sleepery(500)
	Set_Property(@WINDOW,'TIMER','0':@FM:'1000')	;* Set the next timer event to start in 1000 msecs (1 second).

RETURN
