Compile function NDW_RELEASE_CASSETTES_EVENTS(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

    Name        :   NDW_Release_Cassettes_Events

    Description :   Commuter module for the NDW_Release_Cassettes form.

    Notes       :   Application errors should be logged using the Error Services module. There are a few methodological
                    assumptions built into way errors are managed which are important to understand in order to properly
                    work with Error Services:

                    - The term 'top' refers to the originating procedure of a call stack and the term 'bottom' refers to
                      the last routine (or the current routine) within a call stack. Within the OpenInsight Debugger
                      this will appear backwards since the originating procedure always appears at the bottom of the
                      list and the current routine appears at the top of the list. We are using this orientation because
                      it is common to refer to the process of calling other procedures as 'drilling down'.

                    - The reason for defining the orientation of the call stack is because Error_Services allows for
                      multiple error conditions to be appended to an original error. In most cases this will happen when
                      a procedure at the bottom of the stack generates an error condition and then returns to its
                      calling procedure. This higher level procedure can optionally add more information relevant to
                      itself. This continues as the call stack 'bubbles' its way back to the top to where the
                      originating procedure is waiting.

                    - Native OpenInsight commands that handle errors (e.g., Set_Status, Set_FSError, Set_EventStatus)
                      preserve their error state until explicitly cleared. This can hinder the normal execution of code
                      since subsequent procedures (usually SSPs) will fail if a pre-existing error condition exists.
                      Our philosophy is that error conditions should automatically be cleared before a new procedure
                      is executed to avoid this problem. However, the nature of Basic+ does not make this easy to
                      automate for any given stored procedure. Therefore, if a stored procedure wants to conform to our
                      philosophy then it should include a call into the 'Clear' service request at the top of the
                      program. Alternatively this can be done through a common insert (see SERVICE_SETUP for example.)

                    - Service modules will use the SERVICE_SETUP insert and therefore automatically clear out any
                      error conditions that were set before.

    Parameters  :
        Service         [in] -- Name of the service being requested
        Param1-10   [in/out] -- Additional request parameter holders
        Response       [out] -- Response to be sent back to the Controller (MCP) or requesting procedure

    Metadata    :

    History     :   (Date, Initials, Notes)
        05/11/2021    djs     Original programmer.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_RELEASE_CASSETTES

$Insert EVENT_SETUP
$Insert APP_INSERTS
$Insert MSG_EQUATES
$Insert POPUP_EQUATES
$Insert WO_LOG_EQUATES

Declare subroutine Errmsg, Btree.Extract, Set_Status, Logging_Services, Work_Order_Services, PlaceDialog
Declare function   obj_WO_Log, Work_Order_Services, Environment_Services, Logging_Services, Database_Services

LogPath       = Environment_Services('GetApplicationRootPath') : '\LogFiles\WO_LOG'
LogDate       = Oconv(Date(), 'D4/')
LogTime       = Oconv(Time(), 'MTS')
LogFileName   = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' Release Performance Log New.csv'
Headers       = 'Logging DTM':@FM:'WO':@FM:'User':@FM:'Cassettes (Qty)':@FM:'Release (Secs)':@FM:'Form Refresh (Secs)':@FM:'Total Duration (Secs)':@FM:'Seconds/Cassette'
objReleaseLog = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, COMMA$, Headers, '', False$, False$)

LoggingDTM    = LogDate : ' ' : LogTime   ; // Logging DTM

// Update the arguments so that the OpenInsight OLE event will treate the ActiveX event as a native event handler.
If Event EQ 'OLE' then
    Transfer Event to OIEvent
    Transfer Param1 to Event
    Transfer Param2 to Param1
    Transfer Param3 to Param2
    Transfer Param4 to Param3
    Transfer Param5 to Param4
    Transfer Param6 to Param5
    Transfer Param7 to Param6
    Transfer Param8 to Param7
end

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------


Event WINDOW.CREATE(CreateParam)
    
    PlaceDialog(-2, -2)
    
End Event


Event EDL_WO_NO.LOSTFOCUS(Flag, FocusID)
    
    DataValue = Get_Property(CtrlEntID, 'TEXT')
    If DataValue NE '' then
    
        If Index(DataValue,'.',1)  then
            * Try a BTREE lookup on PROD_ORD_NO
            
            Open 'DICT.WO_LOG' to DictVar then
                
                SearchString  = 'PROD_ORD_NO':@VM:DataValue:@FM
                WONos = ''
                Flag = ''
                Btree.Extract(SearchString, 'WO_LOG', DictVar, WONos, '', Flag)
                
                Begin Case
                    Case Get_Status(errCode)
                        ErrMsg(errCode)
                        
                    Case WONos EQ ''
                        ErrMsg('Value Entered "':DataValue:'" is not found.')
                        
                    Case Index(WONos,@VM,1)
                        ErrMsg('Multiple Work Orders ':QUOTE(DataValue):' for Prod Order entered.')
                        
                    Case RowExists('WO_LOG', DataValue)
                        Set_Property(CtrlEntID,'DEFPROP',WONos)
                        GoSub PopulateControls
                        
                    Case Otherwise$
                        ErrMsg(Quote(DataValue):' is not a valid work order.')
                        
                End Case
            end else
                ErrMsg('Unable to open "DICT.WO_LOG" for index lookup in COMM_SHIPMENT routine.')
            end
        end else
            If RowExists('WO_LOG', DataValue) then
                GoSub PopulateControls
            end else
                ErrMsg(Quote(DataValue):' is not a valid work order.')
            end
        end
    end
    
end event


Event PUB_RX.CLICK()
    
    WONo = Get_Property(@Window:'.EDL_WO_NO', 'TEXT')
    If WONo NE '' then
        Response = Dialog_Box('WO_REC', @Window, WONo)
        GoSub PopulateControls
    end
    
end event


Event PUB_LABELS.CLICK()

    WONo = Get_Property(@Window:'.EDL_WO_NO', 'TEXT')
    If WONo NE '' then
        Work_Order_Services('PrintLabels', WONo)
        If Error_Services('HasError') then
            ErrorMsg = Error_Services('GetMessage')
            ErrMsg(ErrorMsg)
        end
    end
    
end event


Event PUB_LU_WO.CLICK()
    
    Set_Status(0)
    WOKeys = obj_WO_Log('Find')	
    
    IF Get_Status(errCode) THEN ErrMsg(ErrCode)
    
    IF WOKeys NE '' THEN
        TypeOver = ''
        TypeOver<PDISPLAY$> = WOKeys
        TypeOver<PMODE$> = 'K'
        
        WOKeys = Popup(@WINDOW,TypeOver,'WO_LOG_QUERY')
        IF Get_Status(errCode) THEN
            ErrMsg(errCode)
        END
        
        IF WOKeys NE '' THEN
            Set_Property(@Window:'.EDL_WO_NO', 'TEXT', WOKeys)
            GoSub PopulateControls
        end
    END
    
end event


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


PopulateControls:
    
    WONo  = Get_Property(@Window:'.EDL_WO_NO', 'TEXT')
    If WONo NE '' then
        
        Set_Property(@Window, "REDRAW", False$)
        Ctrls = 'EDL_WO_QTY,EDL_RX_QTY,EDL_REL_QTY,EDL_UNREL_QTY,EDT_REL_STAMP,EDT_RX_STAMP'
        Cols  = 'WO_QTY,RX_QTY,REL_QTY,UNREL_QTY,REL_STAMP,RX_STAMP'
        
        NumCtrls = DCount(Ctrls, ',')
        
        Def              = ""
        Def<MCAPTION$>   = "Loading Work Order Details..."
        Def<MTYPE$>      = "GC"
        Def<MEXTENT$>    = NumCtrls
        Def<MTEXTWIDTH$> = 600
        Def<MCOL$>       = -2
        Def<MROW$>       = -2
        MsgUp            = Msg(@WINDOW, Def) ;* Start gas guage message
        
        Swap ',' with @VM in Ctrls
        Swap ',' with @VM in Cols
        
        WORec    = Database_Services('ReadDataRow', 'WO_LOG', WONo)
        WOQty    = WORec<WO_LOG_QTY$>
        // Pull these quantities using obj_WO_Log to ensure they are up-to-date.
        // The physical fields are updated via the PROC_QUEUE and may lag by a few seconds.
        RxQty    = obj_WO_Log('RxQty', WONo:@RM:WORec)
        RelQty   = obj_WO_Log('RelQty', WONo:@RM:WORec)
        UnRelQty = WOQty - RelQty
        
        For each Ctrl in Ctrls using @VM setting vPos
            
            Val     = Xlate('WO_LOG', WONo, Cols<0, vPos>, 'X')
            Set_Property(@Window:'.':Ctrl, 'DEFPROP', Val)
            Running = Msg(@WINDOW, MsgUp, vPos, MSGINSTUPDATE$) ;* Update message
            
        Next Ctrl
        
        Msg(@WINDOW,MsgUp)      ;* Take message down
        Set_Property(@Window, "REDRAW", True$)
    end
    GoSub EnableControls
    
return


EnableControls:
    
    Ctrls   = 'PUB_RX,PUB_RELEASE,PUB_LABELS'
    WONo    = Get_Property(@Window:'.EDL_WO_NO', 'TEXT')
    ValidWO = RowExists('WO_LOG', WONo) 
    For each Ctrl in Ctrls using ','
        Set_Property(@Window:'.':Ctrl, 'ENABLED', ValidWO)
    Next Ctrl
    
return

