Function NDW_WM_Out_Quick_Query_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_WM_Out_Quick_Query_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)
        12/12/22    djs     Created initial commuter module.

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

#pragma precomp SRP_PreCompiler
#Window NDW_WM_OUT_QUICK_QUERY

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

Declare subroutine  SRP_Show_Window, ErrMsg, Error_Services, Hold_Services, Labeling_Services
Declare function    Database_Services, WM_Out_Services, RGB, SRP_Array, MemberOf, Error_Services, Hold_Services
Declare function	Datetime, Printer_Select

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)

    WMOColumns       = ''
    WMOColumns<0, 1> = 'CUST_NAME'
    WMOColumns<0, 2> = 'WO_NO'
    WMOColumns<0, 3> = 'PROC_STEP_NO'
    WMOColumns<0, 4> = 'OUT_CASS_NO'
    WMOColumns<0, 5> = 'EPI_REACT_NO'
    WMOColumns<0, 6> = 'SUP_VER_SIG_DTM'
    WMOColumns<0, 7> = 'PART_NO'
    WMOColumns<0, 8> = 'HOLD'
    
    Swap @VM with @FM in CreateParam
    Begin Case
        Case RowExists('WO_LOG', CreateParam)
            ThisWorkOrderNo = CreateParam
            WMOOverrideList = ''
        Case RowExists('WM_OUT', CreateParam)
            ThisWorkOrderNo = ''
            WMOOverrideList = CreateParam
        Case Otherwise$
            ErrMsg('Unsupported CreateParam passed into NDW_WM_Out_Quick_Query_Events.')
            Send_Event(@Window, 'CLOSE')
    End Case
    
    WMOList    = WM_Out_Services('GetWMOData', ThisWorkOrderNo, WMOColumns, True$, WMOOverrideList)
    WMOList    = SRP_Array('SortRows', WMOList, 'AR2':@FM:'AR4', 'LIST')
    WMORptCtrl = @Window : '.OLE_RPT_WM_OUT_LIST'
    
    GoSub Setup_OLE_Controls
    
    // Turn off AutoPopulate for performance reasons
    Set_Property(WMORptCtrl, 'OLE.AutoPopulate', False$)
    
    SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)
        
    // Turn AutoPopulate back on
    Set_Property(WMORptCtrl, 'OLE.AutoPopulate', True$)
    Set_Property(WMORptCtrl, "OLE.AlwaysShowSelection", True$)
    Send_Message(WMORptCtrl, "OLE.UnselectAll")
    Send_Message(@Window, "SET_ZORDER", '')

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_OK.CLICK()

    Result     = ''
    SelWMOList = Get_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.SelList')
    SelWMOList = SRP_Array('Rotate', SelWMOList, @FM, @VM)
    SelWO      = SelWMOList<2>
    SelStep    = SelWMOList<3>
    SelCass    = SelWMOList<4>
    WMOKeys    = ''
    If SelWO NE '' then
        For each WONo in SelWO using @VM setting vPos
            WMOKeys<0, -1> = WONo:'*':SelStep<0, vPos>:'*':SelCass<0 , vPos>
        Next WONo
    end
    End_Dialog(@Window, WMOKeys)

end event


Event PUB_ON_HOLD.CLICK()

	SelPos           = Get_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.SelPos')
	SelCount         = Dcount(SelPos, @VM)
    WMOutKeys  = ''
	SelWMOList = Get_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.SelList')
    SelWMOList = SRP_Array('Rotate', SelWMOList, @FM, @VM)
    SelWO      = SelWMOList<2>
    SelStep    = SelWMOList<3>
    SelCass    = SelWMOList<4>
	WOMatKeys  = ''
	If SelWO NE '' then
		For each WONo in SelWO using @VM setting vPos
			WOMatKeys<0, -1> = WONo:'*':SelCass<0 , vPos>
			WMOutKeys<0, -1> = WONo:'*':SelStep<0 , vPos> :'*': SelCass<0 , vPos>
        Next WONo
    end
    Swap @VM with @FM in WMOutKeys
    Swap @VM with @FM in WOMAtKeys
	SelCount         = DCount(WMOutKeys, @FM)
	Transition       = False$
	HoldEntity		 = 'WM_OUT'
    HoldType         = 'HOLD'
    HoldData         = ''
    HoldData         = Dialog_Box('DIALOG_HOLD',@WINDOW,Transition:@FM:@FM:HoldType)
    If HoldData NE 'Cancel' then
		Hold_Services('EnableMultipleHolds', WOMatKeys, HoldEntity, WMOutKeys, 'NDW_WM_OUT_QUICK_QUERY', '', HoldData)
		IF Error_Services("HasError") THEN
            ErrCode = Error_Services("GetMessage")
            ErrMsg(errCode)
        end else
            MsgInfo = ''
            MsgInfo<MTYPE$> = 'BNY'
            MsgInfo<MTEXT$> = 'Hold Successful. Would you like to print label(s)?'
            MsgInfo<MICON$> = '!'
            PrintLabel = Msg(@WINDOW,MsgInfo,'')
            HoldBy = HoldData<1>
            Reason = HoldData<2>
            Stage = HoldData<4>
            Interrupted = HoldData<5>
            DTM = Datetime()
            If PrintLabel EQ True$ then
                PrinterId = Printer_Select(PrinterID)
            end
            Reactor = 'EPP'
            For L = 1 to SelCount
                ThisPos = SelPos<L>
				Set_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.ItemValue[8;' : ThisPos : ']', 'On Hold')
				PSN = Xlate('WM_OUT', WMOutKeys<L>, 'PS_NO', 'X')
            	If PrintLabel EQ True$ then
                   Labeling_Services('PrintHoldLabel', HoldEntity, WMOutKeys<L>, Stage, Reason, HoldBy, DTM, PSN, Reactor, Interrupted, PrinterID)
                end
            Next L

		HoldOffFlag = True$
		HoldOnFlag  = False$
		Gosub CheckSelectedForHolds
		end
	end

end event


Event PUB_OFF_HOLD.CLICK()
	
	SelPos   = Get_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.SelPos')
	SelCount = Dcount(SelPos, @VM)
    WMOutKeys  = ''
	SelWMOList = Get_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.SelList')
    SelWMOList = SRP_Array('Rotate', SelWMOList, @FM, @VM)
    SelWO      = SelWMOList<2>
    SelStep    = SelWMOList<3>
    SelCass    = SelWMOList<4>
	WOMatKeys  = ''
	If SelWO NE '' then
		For each WONo in SelWO using @VM setting vPos
			WOMatKeys<0, -1> = WONo:'*':SelCass<0 , vPos>
			WMOutKeys<0, -1> = WONo:'*':SelStep<0 , vPos> :'*': SelCass<0 , vPos>
        Next WONo
    end
    Swap @VM with @FM in WMOutKeys
    Swap @VM with @FM in WOMAtKeys
	SelCount         = DCount(WMOutKeys, @FM)
	Transition       = True$
    HoldType         = 'HOLD'
    HoldData         = ''
    HoldData         = Dialog_Box('DIALOG_HOLD',@WINDOW,Transition:@FM:@FM:HoldType)
    If HoldData NE 'Cancel' then
		Hold_Services('DisableMultipleHolds', WOMatKeys, 'WM_OUT', WMOutKeys, 'NDW_WM_OUT_QUICK_QUERY', '', HoldData)
		For L = 1 to SelCount
			ThisPos = SelPos<L>
			Set_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.ItemValue[8;' : ThisPos : ']', 'Off Hold')
		Next L
		HoldOffFlag = False$
		HoldOnFlag  = True$
		Gosub CheckSelectedForHolds
	end
	
end event



Event PUB_CANCEL.CLICK()

    Result = ''
    // Perhaps add a savewarn prompt if list has been edited.
    End_Dialog(@Window, Result)

end event


Event PUB_SELECT_ALL.CLICK()
    
    Send_Message(@Window : '.OLE_RPT_WM_OUT_LIST', "OLE.SelectAll")
    Set_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.Focus', True$)
    Send_Event(@Window:'.OLE_RPT_WM_OUT_LIST', 'OLE', 'OnSelChange')

end event


Event PUB_CLEAR.CLICK()

    Send_Message(@Window : '.OLE_RPT_WM_OUT_LIST', "OLE.UnselectAll")
    Send_Event(@Window:'.OLE_RPT_WM_OUT_LIST', 'OLE', 'OnSelChange')

end event


Event PUB_ENG_OPTIONS.CLICK()
    
    SelOpt = Popup(@Window, '', 'ENG_OPTIONS')
    
    If SelOpt NE '' then
        
        SelWMOList = Get_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.SelList')
        SelWMOList = SRP_Array('Rotate', SelWMOList, @FM, @VM)
        SelWO      = SelWMOList<2>
        SelCass    = SelWMOList<4>
        WOMatKeys  = ''
        If SelWO NE '' then
            For each WONo in SelWO using @VM setting vPos
                WOMatKeys<0, -1> = WONo:'*':SelCass<0 , vPos>
            Next WONo
        end
        
        NumRows = DCount(WOMatKeys, @VM)
        
        Begin Case
            
            Case SelOpt EQ 'CLEAN_INSP'
                
                Caption        = "Updating Clean & Insp specs for selected WM Outbound cassettes..."
                ServiceModules = 'CLEAN_INSP_SERVICES'
                Services       = 'UpdateAllCleanInsp'
                 
            Case SelOpt EQ 'QA_MET'
                
                Caption        = "Updating QA Metrology specs for selected WM Outbound cassettes..."
                ServiceModules = 'WO_MAT_QA_SERVICES'
                Services       = 'UpdateQAMet'
                
            Case SelOpt EQ 'CLEAN_INSP':@VM:'QA_MET'
                Caption        = "Updating Clean & Insp and QA Metrology specs for selected WM Outbound cassettes..."
                ServiceModules = 'CLEAN_INSP_SERVICES':@VM:'WO_MAT_QA_SERVICES'
                Services       = 'UpdateAllCleanInsp':@VM:'UpdateQAMet'
                
        End Case
        
        If ServiceModules NE '' then
            Def              = ""
            Def<MCAPTION$>   = Caption
            Def<MTYPE$>      = "GC"
            Def<MEXTENT$>    = NumRows
            Def<MTEXTWIDTH$> = 600
            MsgUp            = Msg(@WINDOW, Def) ;* Start gas guage message
            For each WOMatKey in WOMatKeys using @VM setting vPos
                Running = Msg(@WINDOW, MsgUp, vPos, MSGINSTUPDATE$) ;* Update gas guage
                For each ServiceModule in ServiceModules using @VM setting sPos
                    Service = Services<0, sPos>
                    Ans     = Function(@ServiceModule(Service, WOMatKey))
                    If Error_Services('HasError') then
                        Error_Services('DisplayError')
                    end
                Next ServiceModule
            Next WOMatKey
            Msg(@WINDOW,MsgUp) ;* Take gas guage down
        end
        
        If Error_Services('NoError') then
            Def            = ""
            Def<MICON$>    = '*'
            Def<MCAPTION$> = 'Success'
            Def<MTEXT$>    = "Update complete!"
            Def<MTYPE$>    = "BO"
            MsgUp          = Msg(@window, Def)
        end
    end
    
end event


Event OLE_SUBCLASS.OnComboClick(CtrlId, Sel, Value)

    Send_Event(CtrlId, 'LOSTFOCUS')

end event


Event OLE_RPT_WM_OUT_LIST.OnSelChange()
    
    If MemberOf(@User4, 'ENGINEERING') then
        SelWMOList = Get_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.SelList')
        Enabled    = (SelWMOList NE '')
        Set_Property(@Window:'.PUB_ENG_OPTIONS', 'ENABLED', Enabled)
    end
    HoldOnFlag  = False$
    HoldOffFlag = False$
    Gosub CheckSelectedForHolds
    
end event


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

Setup_OLE_Controls:

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

    WMORptCtrl = @Window : '.OLE_RPT_WM_OUT_LIST'
    
    // Qualify OLE events that we want to intercept
    Qualifier = ''
    Qualifier<1> = 1
    Qualifier<4> = 0 ; * process synchronously (i.e. immediately)
    Send_Message(WMORptCtrl, 'QUALIFY_EVENT', 'OLE.OnSelChange', Qualifier)
    
    ColumnList    = ''
    ColumnList<1> = 'Customer' : @VM : 'TEXT' : @VM : 30 : @VM : @VM : @VM : @VM : @VM : @VM : 20 : @VM : 'Left' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<2> = 'WO No' : @VM : 'NUMBER' : @VM : 15 : @VM : @VM : @VM : @VM : @VM : @VM : 15 : @VM : 'Center' : @VM : @VM : '#######' : @VM : @VM : 'Segoe UI,9'
    ColumnList<3> = 'Step' : @VM : 'NUMBER' : @VM : 10 : @VM : @VM : @VM : @VM : @VM : @VM : 10 : @VM : 'Center' : @VM : @VM : '##': @VM : @VM : 'Segoe UI,9'
    ColumnList<4> = 'Out Cass No' : @VM : 'NUMBER' : @VM : 15 : @VM : @VM : @VM : @VM : @VM : @VM : 15 : @VM : 'Center' : @VM : @VM : '###' : @VM : @VM : 'Segoe UI,9'
    ColumnList<5> = 'Rx' : @VM : 'NUMBER' : @VM : 10 : @VM : @VM : @VM : @VM : @VM : @VM : 10 : @VM : 'Center' : @VM : @VM : '#######' : @VM : @VM : 'Segoe UI,9'
    ColumnList<6> = 'FQA Sig' : @VM : 'TEXT' : @VM : 30 : @VM : @VM : @VM : @VM : @VM : @VM : 20 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<7> = 'Cust Part No' : @VM : 'TEXT' : @VM : 30 : @VM : @VM : @VM : @VM : @VM : @VM : 20 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<8> = 'Hold Status' : @VM : 'TEXT' : @VM : 30 : @VM : @VM : @VM : @VM : @VM : @VM : 20 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    
    Set_Property(WMORptCtrl, 'OLE.ColumnList', ColumnList)
    Set_Property(WMORptCtrl, 'OLE.MultiSelect', 2)  ; // 2 = Multiselect Toggle Mode
    
    NumCols = DCount(ColumnList, @FM)
    For ColIndex = 1 to NumCols
        Set_Property(WMORptCtrl, "OLE.ColumnHeaderAlignment[":ColIndex:"]", 'C')
    Next ColIndex
    
    // Get the design time form size. This will be adjusted based on the number of WMO records.
    FormSize            = Get_Property(@Window, 'SIZE')
    WMORptCtrlSize      = Get_Property(WMORptCtrl, 'SIZE')
    NumWMO              = DCount(WMOList, @FM)
    
    // Adjust the form and ReportTable control size based on the number of WMO records in the work order.
    AdditionalRowsAdj   = 0
    AdditionalRowsAdj += (NumWMO - 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)
    Gosub CheckSelectedForHolds
    // Pull then save original list after formatting for savewarn purposes
    Set_Property(WMORptCtrl, 'OLE.List', WMOList)
    WMOList  = Get_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.List')
    Set_Property(@Window, '@OrigList', WMOList)
    GoSub RefreshRowColors
    
return
    
    
RefreshRowColors:
        
    WMORptCtrl  = @Window : '.OLE_RPT_WM_OUT_LIST'
    NumWMO      = DCount(WMOList, @FM)
    
    // Fill report table with WMO data
    Set_Property(WMORptCtrl, 'OLE.List', WMOList)
           
    // Color every other cell to increase row distinguishability
    Colors = ''
    If NumWMO GT 1 then
        For RowNo = 1 To NumWMO
            If Mod(RowNo, 2) EQ 1 then Colors<RowNo> = RGB(231, 243, 254) else Null ; // light blue       
        Next RowNo
        Set_Property(WMORptCtrl, 'OLE.RecordColors', Colors)
    end
    
    // Clear any selected rows
    SelPos = ''
    Set_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.SelPos', SelPos)
    
return


CheckSelectedForHolds:
	
	If Unassigned(HoldOnFlag) then  HoldOnFlag = False$
    If Unassigned(HoldOffFlag) then  HoldOffFlag = False$
	SelWMOList = Get_Property(@Window : '.OLE_RPT_WM_OUT_LIST', 'OLE.SelList')
	If SelWMOList NE '' then
		SelWMOList = SRP_Array('Rotate', SelWMOList, @FM, @VM)
		HoldStatuses = SelWMOList<8>
		Swap @VM with @FM in HoldStatuses
		SelCount     = DCount(HoldStatuses, @FM)
		
		For K = 1 to SelCount
			HoldText = HoldStatuses<K>
			If HoldText = 'On Hold' then
				HoldCheck = True$
			end else
				HoldCheck = False$
			end
			If HoldCheck EQ True$ AND HoldOffFlag EQ False$ then
				HoldOffFlag = True$
			end
			If HoldCheck EQ False$ AND HoldOnFlag EQ False$ then
				HoldOnFlag = True$
			end
		Until HoldOnFlag EQ True$ and HoldOffFlag  EQ True$ 
		Next K
    end
    if MemberOf(@USER4, 'ENG_TECH') OR MemberOf(@USER4, 'LEAD') OR MemberOf(@USER4, 'SUPERVISOR') then
    	// IF ANY SELECTED LOTS ARE ON HOLD, ENABLE PUB_OFF_HOLD
    	Set_Property(@Window:'.PUB_OFF_HOLD', 'ENABLED', HoldOffFlag)
    end else
    	Set_Property(@Window:'.PUB_OFF_HOLD', 'ENABLED', False$)
    end
    // IF ANY SELECTED LOTS ARE NOT ON HOLD, ENABLE PUB_ON_HOLD 
	Set_Property(@Window:'.PUB_ON_HOLD', 'ENABLED', HoldOnFlag)
	
return

