Function NDW_RDS_UPDATE_MET_LIMITS_Events(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from Infineon.

    Name        :   NDW_RDS_Update_Met_Limits_Events

    Description :   This function acts as a commuter module for all events related to this window.

    Notes       :   Commuter Modules are automatically called from the Promoted_Events function which is called by the
                    application-specific promoted event handler. This makes it possible to add QuickEvents that need to
                    execute Basic+ logic without having use the Form Designer to make the association, although this is
                    limited to the events which are currently promoted.

                    If the form needs to call the commuter module directly then the QuickEvent parameters should be
                    formatted like this:

                        '@SELF','@EVENT',['@PARAM1','@PARAMx']

    Parameters  :
        CtrlEntId   [in] -- The fully qualified name of the control calling the promoted event
        Event       [in] -- The event being executed. See the Notes section regarding "PRE" events
        Param1-15   [in] -- Additional event parameter holders
        EventFlow  [out] -- Set to 1 or 0 so the calling event knows whether or not to chain forward. See comments in
                            EVENT_SETUP insert

    History     :   (Date, Initials, Notes)
        08/22/22    djs     Created initial commuter module.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler
#Window NDW_RDS_UPDATE_MET_LIMITS

$insert APP_INSERTS
$insert EVENT_SETUP
$insert WO_MAT_EQUATES
$insert MSG_EQUATES

Declare subroutine  SRP_EditTable_Manager, SRP_Show_Window, Rds_Services, PSN_Services, ErrMsg
Declare function    SRP_EditTable_Manager, Database_Services, Material_Services, Rds_Services, RGB

SubclassInfo    = Form_Services('FindSubclassControl')
Subclass        = SubclassInfo<1>

// 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

Return EventFlow else EVENT_CONTINUE$


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Event WINDOW.CREATE(CreateParam)
    
    RDSColumns        = ''
    RDSColumns<0, 1>  = 'ABBREV_OR_CO_NAME'
    RDSColumns<0, 2>  = 'WO'
    RDSColumns<0, 3>  = 'WO_STEP'
    RDSColumns<0, 4>  = 'SEQ'
    RDSColumns<0, 5>  = 'REACTOR'
    RDSColumns<0, 6>  = 'RUN_ORDER_NUM'
    RDSColumns<0, 7>  = 'UNLOAD_THICK_TEST'
    RDSColumns<0, 8>  = 'HGCV_TEST'
    RDSColumns<0, 9>  = 'DATE_IN'
    RDSColumns<0, 10> = 'TIME_IN'
    RDSColumns<0, 11> = 'DATE_OUT'
    RDSColumns<0, 12> = 'TIME_OUT'
    RDSColumns<0, 13> = 'ELAPSED_HOURS'
    RDSColumns<0, 14> = 'PART_NUM'
    RDSColumns<0, 15> = 'LOT_NUM'
    RDSColumns<0, 16> = 'COMB_STATUS'
    RDSColumns<0, 17> = 'NCR_SUM'
    RDSColumns<0, 18> = 'NCR_REJ_CNT'
    RDSColumns<0, 19> = 'HOLD_STATUS'
    
    Swap @VM with @FM in CreateParam
    Begin Case
        Case RowExists('WO_LOG', CreateParam)
            ThisWorkOrderNo = CreateParam
            RDSOverrideList = ''
        Case RowExists('RDS', CreateParam)
            ThisWorkOrderNo = ''
            RDSOverrideList = CreateParam
        Case Otherwise$
            ErrMsg('Unsupported CreateParam passed into NDW_RDS_QUERY_EVENTS.')
            Send_Event(@Window, 'CLOSE')
    End Case
    
    RDSList         = RDS_Services('GetRDSData', ThisWorkOrderNo, RDSColumns, True$, RDSOverrideList)   
    RDSRptCtrl      = @Window : '.OLE_RPT_RDS_LIST'
    
    GoSub Setup_OLE_Controls
    
    // Turn off AutoPopulate for performance reasons
    Set_Property(@Window, RDSRptCtrl, 'OLE.AutoPopulate', False$)
    
    SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)
        
    // Turn AutoPopulate back on
    Set_Property(@Window, RDSRptCtrl, 'OLE.AutoPopulate', True$)

end event


Event WINDOW.CLOSE(CancelFlag)
    
    Result = ''
    // Perhaps add a savewarn prompt if list has been edited.
    End_Dialog(@Window, Result)
    
end event


Event PUB_CLOSE.CLICK()

    Result = ''
    End_Dialog(@Window, Result)

end event


Event PUB_UPDATE_MET.CLICK()

    // Update RDS Metrology and QA Metrology
    SelList = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelList')
    NumRows = DCount(SelList, @FM)
    
    Def              = ""
    Def<MCAPTION$>   = "Updating metrology limits for selected RDS..."
    Def<MTYPE$>      = "GC"
    Def<MEXTENT$>    = NumRows
    Def<MTEXTWIDTH$> = 600
    MsgUp            = Msg(@WINDOW, Def) ;* Start gas guage message
    
    For each Row in SelList using @FM setting fPos
        Running = Msg(@WINDOW, MsgUp, fPos, MSGINSTUPDATE$) ;* Update message
        RDSNo   = Row<0, 4>
        If RDSNo NE '' then
            PSN_Services('UpdateRDSMetrologyLimits', RDSNo)
            ReactType = Xlate('RDS', RDSNo, 'REACTOR_TYPE', 'X')
            If ReactType EQ 'EPP' then
                WMOKeys   = Xlate('RDS', RDSNo, 'WM_OUT_KEY', 'X')
                If WMOKeys NE '' then
                    For each WMOKey in WMOKeys using @VM
                        WOMatKey = Xlate('WM_OUT', WMOKey, 'WO_MAT_KEY', 'X')
                        PSN_Services('UpdateAllQAMetrologyLimits', WOMatKey)
                    Next WMOKey
                end
            end else
                WOMatKey = Xlate('RDS', RDSNo, 'WO_MAT_KEY', 'X')
                If WOMatKey NE '' then
                    PSN_Services('UpdateAllQAMetrologyLimits', WOMatKey)
                end
            end
        end
    Next Row
    
    Msg(@WINDOW,MsgUp)
    
    GoSub RefreshRowColors
    
    Def            = ""
    Def<MICON$>    = '*'
    Def<MCAPTION$> = 'Success'
    Def<MTEXT$>    = "Metrology limits update complete!"
    Def<MTYPE$>    = "BO"
    MsgUp          = Msg(@window, Def)

end event


Event PUB_REMOVE.CLICK()

    SelList     = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelList')
    RDSList     = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.List')  
    
    // Insert empty string into selected rows' Supplement cell to clear it
    For each Row in SelList using @FM
        Locate Row in RDSList Using @FM Setting Pos then
            RDSList<Pos,20> = ''
        end
    Next Row
    
    // Clear any selected rows
    SelPos = ''
    Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos', SelPos)
    
*    GoSub RefreshRowColors

end event


Event PUB_SELECT_ALL.CLICK()
    
    RDSList = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.List')
    NumRows = DCount(RDSList, @FM)
    SelPos  = ''
    
    For Index = 1 to NumRows
        If Index LT NumRows then
            SelPos := Index:@FM
        end else
            SelPos := Index
        end
    Next Index
    
    Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos', SelPos)
    Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.Focus', True$)

end event


Event PUB_CLEAR.CLICK()

    // Clear any selected rows
    SelPos = ''
    Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos', SelPos)

end event


Event OLE_SUBCLASS.OnComboClick(CtrlId, Sel, Value)

    Send_Event(CtrlId, 'LOSTFOCUS')

end event


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

Setup_OLE_Controls:

    Qualify     = ''
    Qualify<1>  = 1
    Qualify<4>  = 0

    RDSRptCtrl          = @Window : '.OLE_RPT_RDS_LIST'
    RDSPicCtrl          = @Window : '.OLE_PIC_RDS_LIST_DIV'

    ColumnList          = ''
    ColumnList<1>       = 'Customer' : @VM : 'TEXT' : @VM : 150 : @VM : @VM : @VM : @VM : @VM : @VM : 50 : @VM : 'Left' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<2>       = 'WO No' : @VM : 'NUMBER' : @VM : 50 : @VM : @VM : @VM : @VM : @VM : @VM : 50 : @VM : 'Center' : @VM : @VM : '#######' : @VM : @VM : 'Segoe UI,9'
    ColumnList<3>       = 'Step' : @VM : 'NUMBER' : @VM : 30 : @VM : @VM : @VM : @VM : @VM : @VM : 30 : @VM : 'Center' : @VM : @VM : '##': @VM : @VM : 'Segoe UI,9'
    ColumnList<4>       = 'RDS' : @VM : 'NUMBER' : @VM : 75 : @VM : @VM : @VM : @VM : @VM : @VM : 30 : @VM : 'Center' : @VM : @VM : '###' : @VM : @VM : 'Segoe UI,9'
    ColumnList<5>       = 'Rx' : @VM : 'NUMBER' : @VM : 30 : @VM : @VM : @VM : @VM : @VM : @VM : 50 : @VM : 'Center' : @VM : @VM : '#######' : @VM : @VM : 'Segoe UI,9'
    ColumnList<6>       = 'Run' : @VM : 'NUMBER' : @VM : 30 : @VM : @VM : @VM : @VM : @VM : @VM : 25 : @VM : 'Center' : @VM : @VM :'##': @VM : @VM : 'Segoe UI,9'
    ColumnList<7>       = 'UL Thk' : @VM : 'BOOL' : @VM : 55 : @VM : @VM : @VM : @VM : @VM : @VM : 40 : @VM : 'Center' : @VM : @VM : 'Yes' : @VM : @VM : 'Segoe UI,9'
    ColumnList<8>       = 'HgCv' : @VM : 'BOOL' : @VM : 50 : @VM : @VM : @VM : @VM : @VM : @VM : 40 : @VM : 'Center' : @VM : @VM : 'Yes' : @VM : @VM : 'Segoe UI,9'
    ColumnList<9>       = 'Date In' : @VM : 'DATE' : @VM : 55 : @VM : @VM : @VM : @VM : @VM : @VM : 60 : @VM : 'Center' : @VM : @VM : 'MM/DD/YY' : @VM : @VM : 'Segoe UI,9'
    ColumnList<10>      = 'Time In' : @VM : 'TIME' : @VM : 55 : @VM : @VM : @VM : @VM : @VM : @VM : 60 : @VM : 'Center' : @VM : @VM : 'hh:mmAA' : @VM : @VM : 'Segoe UI,9'
    ColumnList<11>      = 'Date Out' : @VM : 'DATE' : @VM : 55 : @VM : @VM : @VM : @VM : @VM : @VM : 60 : @VM : 'Center' : @VM : @VM : 'MM/DD/YY': @VM : @VM : 'Segoe UI,9'
    ColumnList<12>      = 'Time Out' : @VM : 'TIME' : @VM : 55 : @VM : @VM : @VM : @VM : @VM : @VM : 60 : @VM : 'Center' : @VM : @VM : 'hh:mmAA': @VM : @VM : 'Segoe UI,9'
    ColumnList<13>      = 'Elapsed Hours' : @VM : 'TEXT' : @VM : 75 : @VM : @VM : @VM : @VM : @VM : @VM : 50 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<14>      = 'Cust Part No' : @VM : 'TEXT' : @VM : 75 : @VM : @VM : @VM : @VM : @VM : @VM : 75 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<15>      = 'Lot Number' : @VM : 'TEXT' : @VM : 100 : @VM : @VM : @VM : @VM : @VM : @VM : 75 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<16>      = 'Combined Status' : @VM : 'TEXT' : @VM : 125 : @VM : @VM : @VM : @VM : @VM : @VM : 125 : @VM : 'Left' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<17>      = 'NCR Status' : @VM : 'TEXT' : @VM : 75 : @VM : @VM : @VM : @VM : @VM : @VM : 30 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<18>      = 'NCR Wfr Cnt' : @VM : 'NUMBER' : @VM : 75 : @VM : @VM : @VM : @VM : @VM : @VM : 30 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<19>      = 'Hold Status' : @VM : 'TEXT' : @VM : 75 : @VM : @VM : @VM : @VM : @VM : @VM : 40 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    
    Set_Property(RDSRptCtrl, 'OLE.ColumnList', ColumnList)
    Set_Property(RDSRptCtrl, 'OLE.MultiSelect', 2)  ; // 2 = Multiselect Toggle Mode

    // Get the design time form size. This will be adjusted based on the number of RDS records.
    FormSize            = Get_Property(@Window, 'SIZE')
    RDSRptCtrlSize      = Get_Property(RDSRptCtrl, 'SIZE')
    NumRDS              = DCount(RDSList, @FM)
    
    // Adjust the form and ReportTable control size based on the number of RDS records in the work order.
    AdditionalRowsAdj   = 0
    AdditionalRowsAdj += (NumRDS - 1) * 20
   
    // Ensure window and controls are sized within client machine's max resolution.
    ProposedNewHeight = FormSize<4> + AdditionalRowsAdj
    SystemWindowSize  = Get_Property('SYSTEM', 'SIZE')
    MaxClientHeight   = SystemWindowSize<4>
    If ProposedNewHeight > MaxClientHeight then
        FormSize<4> = Oconv(MaxClientHeight * '0.9', 'MD0')
    end else
        FormSize<4> = FormSize<4> + AdditionalRowsAdj
    end
    MaxClientWidth    = SystemWindowSize<3>
    If FormSize<3> > MaxClientWidth then
        FormSize<3> = Oconv(MaxClientWidth * '0.9', 'MD0')
    end
    Set_Property(@Window, '@ORIGSIZE', FormSize)      
    // Pull then save original list after formatting for savewarn purposes
    Set_Property(RDSRptCtrl, 'OLE.List', RDSList)
    RDSList  = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.List')
    Set_Property(@Window, '@OrigList', RDSList)
    GoSub RefreshRowColors
    
return
    
    
RefreshRowColors:
    If Assigned(RDSList) then
    	RDSRptCtrl = @Window : '.OLE_RPT_RDS_LIST'
		NumRDS     = DCount(RDSList, @FM)
			   
		// Color every other cell to increase row distinguishability
		Colors = ''
		If NumRDS GT 1 then
			For RowNo = 1 To NumRDS
				If Mod(RowNo, 2) EQ 1 then Colors<RowNo> = RGB(231, 243, 254) else Null ; // light blue       
			Next RowNo
			Set_Property(RDSRptCtrl, 'OLE.RecordColors', Colors)
		end
    end
    // Clear any selected rows
    SelPos = ''
    Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos', SelPos)
    
return

