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

    Name        :   NDW_MATERIAL_TRACK_PRO_REPORT_EVENTS

    Description :   Commuter module for the NDW_MATERIAL_TRACK_PRO_REPORT_EVENTS 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)
        10/29/20    djs     Original programmer.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_MATERIAL_TRACK_PRO_REPORT

Declare function   Form_Services, Database_Services, RTI_Task_Submit, RTI_Task_Status, MemberOf
Declare subroutine SRP_Show_Window, Send_Message, Set_Property, Database_Services, Material_Track, Report_Services

$Insert EVENT_SETUP
$Insert LOGICAL
$Insert REPORT_CONFIG_EQUATES

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 else
    // Event not implemented
end

Return EventFlow or 1

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

Event WINDOW.CREATE(CreateParam)
    
    RptConfigKey = 'MATERIAL_TRACK*':@User4
    If RowExists('REPORT_CONFIG', RptConfigKey) then
        UserSettings = Database_Services('ReadDataRow', 'REPORT_CONFIG', RptConfigKey)
        Set_Property(@Window:'.PUB_SAVE_SETTINGS', 'ENABLED', False$)
    end else
        UserSettings = ''
        Set_Property(@Window:'.PUB_SAVE_SETTINGS', 'ENABLED', True$)
    end
    
    If MemberOf(@User4, 'OI_ADMIN') then Set_Property(@Window:'.CHK_OLD_REPORT', 'VISIBLE', True$)
    
    GoSub Setup_OLE_Controls
    GoSub EnableGenerateReportButton
    
    FormSize = '' 
    SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)
    
End Event


Event PUB_GEN_REPORT.CLICK()
    
    // Gather settings from form
    ColFltrArray = Get_Property(@Window:'.OLE_EDT_COL_FILTER', 'OLE.ARRAY')
    LocFltrArray = Get_Property(@Window:'.OLE_EDT_LOC_FILTER', 'OLE.ARRAY')
    NoMatFlag    = Get_Property(@Window:'.CHK_NO_MAT_FOUND', 'CHECK')
    OldReport    = Get_Property(@Window:'.CHK_OLD_REPORT', 'CHECK')
    
    RptColumns   = ''
    RptLocations = ''
    
    Columns = ColFltrArray<1>
    Flags   = ColFltrArray<2>
    For each Column in Columns using @VM setting vPos
        ColumnEnabled = Flags<0, vPos>
        If ColumnEnabled then RptColumns<0, -1> = Column
    Next Column
    
    Locations = LocFltrArray<1>
    Flags     = LocFltrArray<3>
    For each Location in Locations using @VM setting vPos
        LocationEnabled = Flags<0, vPos>
        If LocationEnabled then RptLocations<0, -1> = Location
    Next Location
    
    Report_Services('PrintMaterialTrackReport', RptColumns, RptLocations, NoMatFlag, OldReport)

end event


Event PUB_SAVE_SETTINGS.CLICK()

    RptConfigKey = 'MATERIAL_TRACK*':@User4
    If RowExists('REPORT_CONFIG', RptConfigKey) then
        UserSettings = Database_Services('ReadDataRow', 'REPORT_CONFIG', RptConfigKey)
    end else
        UserSettings = ''
    end
    
    // Gather settings from form
    ColFltrArray   = Get_Property(@Window:'.OLE_EDT_COL_FILTER', 'OLE.ARRAY')
    LocFltrArray   = Get_Property(@Window:'.OLE_EDT_LOC_FILTER', 'OLE.ARRAY')
    NoMatFoundFlag = Get_Property(@Window:'.CHK_NO_MAT_FOUND', 'CHECK')
    RptColumns     = ''
    RptLocations   = ''
    
    Columns = ColFltrArray<1>
    Flags   = ColFltrArray<2>
    For each Column in Columns using @VM setting vPos
        ColumnEnabled = Flags<0, vPos>
        If ColumnEnabled then RptColumns<0, -1> = Column
    Next Column
    UserSettings<REPORT_CONFIG.REPORT_COLUMNS$> = Flags
    
    Locations = LocFltrArray<1>
    Flags     = LocFltrArray<3>
    For each Location in Locations using @VM setting vPos
        LocationEnabled = Flags<0, vPos>
        If LocationEnabled then RptLocations<0, -1> = Location
    Next Location
    UserSettings<REPORT_CONFIG.LOCATION_FILTER$> = Flags
    UserSettings<REPORT_CONFIG.NO_MAT_FOUND$>    = NoMatFoundFlag
    Database_Services('WriteDataRow', 'REPORT_CONFIG', RptConfigKey, UserSettings, True$, False$, True$)
    Set_Property(CtrlEntID, 'ENABLED', False$)   
    
end event


Event OLE_EDT_COL_FILTER.OnCheckChanged(Cell, OldValue, NewValue)
    
    GoSub EnableSaveButton
    GoSub EnableGenerateReportButton
    
end event


Event OLE_EDT_LOC_FILTER.OnCheckChanged(Cell, OldValue, NewValue)
    
    GoSub EnableReportColumns
    GoSub EnableSaveButton
    GoSub EnableGenerateReportButton
    
end event


Event CHK_NO_MAT_FOUND.CLICK()
    
    GoSub EnableSaveButton
    GoSub EnableGenerateReportButton
    
end event


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

Setup_OLE_Controls:
    
    // Report Columns
    RptCols        = ''
    RptCols<1, 1>  = 'React No'
    RptCols<1, 2>  = 'React Type'
    RptCols<1, 3>  = 'WO No'
    RptCols<1, 4>  = 'SAP Prod No'
    RptCols<1, 5>  = 'Sub Part No'
    RptCols<1, 6>  = 'Epi Part No'
    RptCols<1, 7>  = 'WO Qty'
    RptCols<1, 8>  = 'RX Qty'
    RptCols<1, 9>  = 'UnRel Qty'
    RptCols<1, 10> = 'Kit Location'
    RptCols<1, 11> = 'Kit Qty'
    RptCols<1, 12> = '+/-'
    RptCols<1, 13> = 'Kit RO'
    RptCols<1, 14> = 'PTI RO'
    RptCols<1, 15> = 'Load'
    RptCols<1, 16> = 'Comments'
    
    RptColsEnabled        = ''
    If UserSettings NE '' then
        RptColsEnabled = UserSettings<REPORT_CONFIG.REPORT_COLUMNS$>
    end else
        For each Col in RptCols using @VM setting vPos
            RptColsEnabled<0, -1> = True$
        Next Col    
    end
    
    NoMatFoundFlag = UserSettings<REPORT_CONFIG.NO_MAT_FOUND$>
    Set_Property(@Window:'.CHK_NO_MAT_FOUND', 'CHECK', NoMatFoundFlag)

    RptColsArray = RptCols:@FM:RptColsEnabled
    
    // Location Filters
    LocFilters        = ''
    LocFilters<1, 1>  = 'SR*KTR]'
    LocFilters<1, 2>  = '1K*PTI'
    LocFilters<1, 3>  = 'CR*BE'
    LocFilters<1, 4>  = 'CR*BO'
    LocFilters<1, 5>  = 'CR*TUN'
    LocFilters<1, 6>  = 'CR*EPR'
    LocFilters<1, 7>  = 'CR*FE'
    LocFilters<1, 8>  = 'CR*FEH'
    LocFilters<1, 9>  = 'CR*FO'
    LocFilters<1, 10> = 'CR*FOH'
    
    LocDescriptions = ''
    LocsEnabled     = ''
    If UserSettings NE '' then
        LocsEnabled = UserSettings<REPORT_CONFIG.LOCATION_FILTER$>
        For each Loc in LocFilters using @VM setting vPos
            If vPos GT 1 then
                LocDescriptions<0, -1> = Xlate('LOCATION', Loc, 'DESC', 'X')
            end else
                LocDescriptions<0, -1> = 'All Kit Racks'
            end    
        Next Loc
    end else
        For each Loc in LocFilters using @VM setting vPos
            LocsEnabled<0, -1> = True$
            If vPos GT 1 then
                LocDescriptions<0, -1> = Xlate('LOCATION', Loc, 'DESC', 'X')
            end else
                LocDescriptions<0, -1> = 'All Kit Racks'
            end
        Next Loc    
    end

    LocFltrArray = LocFilters:@FM:LocDescriptions:@FM:LocsEnabled
    
    ColFltrCtrl   = @Window:'.OLE_EDT_COL_FILTER'
    LocFltrCtrl   = @Window:'.OLE_EDT_LOC_FILTER'
    
    // Qualify OLE events that we want to intercept
    Qualifier = ''
    Qualifier<1> = 1
    Qualifier<4> = 0 ; * process synchronously (i.e. immediately)
    Send_Message(ColFltrCtrl, 'QUALIFY_EVENT', 'OLE.OnCheckChanged', Qualifier)
    Send_Message(LocFltrCtrl, 'QUALIFY_EVENT', 'OLE.OnCheckChanged', Qualifier)
    
    NumColFltrCols   = 2
    NumColFltrRows   = DCount(RptCols, @VM)
    NumLocFltrCols   = 3
    NumLocFltrRows   = DCount(LocFilters, @VM)
    
    HeaderFontArray = 'Segoe UI':@SVM:8:@SVM:700
    ColFltrDimArray = NumColFltrCols : @FM : NumColFltrRows
    LocFltrDimArray = NumLocFltrCols : @FM : NumLocFltrRows
    DataColArray    = ''
    DataColArray<4> = True$ ; // Autosize column
    
    HeaderTitles    = 'Column':@VM:'Enabled'
    Set_Property(ColFltrCtrl, "OLE.TitleList", HeaderTitles)
    Set_Property(ColFltrCtrl, "OLE.CellFont[All; All]", 'Segoe UI':@SVM:8)
    Set_Property(ColFltrCtrl, "OLE.Dimension", ColFltrDimArray)
    Set_Property(ColFltrCtrl, "OLE.HeaderFont[All; 1]", HeaderFontArray)
    Set_Property(ColFltrCtrl, "OLE.HeaderColumn[1]", '':@FM:False$:@FM)
    Set_Property(ColFltrCtrl, "OLE.HeaderAlignment[All; 1]", "Top":@FM:"Center":@FM:"Center")
    Set_Property(ColFltrCtrl, "OLE.HeaderColors[All; 1]", @FM:"{200, 200, 200}")
    Set_Property(ColFltrCtrl, "OLE.CellType[2; All]", "Check Box")
    Set_Property(ColFltrCtrl, "OLE.CellAlignment[2; All]", "Top":@FM:"Center":@FM:"Center")   
    Set_Property(ColFltrCtrl, "OLE.DataColumn[1]", DataColArray)
    Set_Property(ColFltrCtrl, "OLE.ARRAY", RptColsArray)
    Set_Property(ColFltrCtrl, "OLE.CellCheckEnabled[2; 1-3]", False)
    CellColorArray = 'None':@FM:"{240, 240, 240}"
    Set_Property(ColFltrCtrl, "OLE.CellColors[All; 1-3]", CellColorArray)
    Set_Property(ColFltrCtrl, "OLE.CellProtection[All; 1-3]", 'FUL')
    
       
    HeaderTitles = 'Location':@VM:'Description':@VM:'Enabled'
    Set_Property(LocFltrCtrl, "OLE.TitleList", HeaderTitles)
    Set_Property(LocFltrCtrl, "OLE.CellFont[All; All]", 'Segoe UI':@SVM:8)
    Set_Property(LocFltrCtrl, "OLE.Dimension", LocFltrDimArray)
    Set_Property(LocFltrCtrl, "OLE.LIST", '')
    Set_Property(LocFltrCtrl, "OLE.HeaderFont[All; 1]", HeaderFontArray)
    Set_Property(LocFltrCtrl, "OLE.HeaderColumn[1]", '':@FM:False$:@FM)
    Set_Property(LocFltrCtrl, "OLE.HeaderAlignment[All; 1]", "Top":@FM:"Center":@FM:"Center")
    Set_Property(LocFltrCtrl, "OLE.HeaderColors[All; 1]", @FM:"{200, 200, 200}")
    Set_Property(LocFltrCtrl, "OLE.CellType[3; All]", "Check Box")
    Set_Property(LocFltrCtrl, "OLE.DataColumn[2]", DataColArray)
    Set_Property(LocFltrCtrl, "OLE.CellAlignment[3; All]", "Top":@FM:"Center":@FM:"Center")
    Set_Property(LocFltrCtrl, "OLE.ARRAY", LocFltrArray)
       
return


EnableSaveButton:
    
    // Enable save settings button if necessary
    RptConfigKey = 'MATERIAL_TRACK*':@User4
    If RowExists('REPORT_CONFIG', RptConfigKey) then
        UserSettings = Database_Services('ReadDataRow', 'REPORT_CONFIG', RptConfigKey)
    end else
        UserSettings = ''
    end
    
    // Gather settings from form
    ColFltrArray      = Get_Property(@Window:'.OLE_EDT_COL_FILTER', 'OLE.ARRAY')
    LocFltrArray      = Get_Property(@Window:'.OLE_EDT_LOC_FILTER', 'OLE.ARRAY')
    
    NewColFlags  = ColFltrArray<2>
    CurrColFlags = UserSettings<REPORT_CONFIG.REPORT_COLUMNS$>
    
    NewLocFlags  = LocFltrArray<3>
    CurrLocFlags = UserSettings<REPORT_CONFIG.LOCATION_FILTER$>
    
    NewNoMatFoundFlag  = Get_Property(@Window:'.CHK_NO_MAT_FOUND', 'CHECK')
    CurrNoMatFoundFlag = UserSettings<REPORT_CONFIG.NO_MAT_FOUND$>
    
    ChangeDetected = ( (NewColFlags NE CurrColFlags) or (NewLocFlags NE CurrLocFlags) or (NewNoMatFoundFlag NE CurrNoMatFoundFlag) )
    Set_Property(@Window:'.PUB_SAVE_SETTINGS', 'ENABLED', ChangeDetected)
    
return


EnableGenerateReportButton:
    
    // Gather settings from form
    LocFltrArray   = Get_Property(@Window:'.OLE_EDT_LOC_FILTER', 'OLE.ARRAY')
    NoMaterialFlag = Get_Property(@Window:'.CHK_NO_MAT_FOUND', 'CHECK')
    LocFlags       = LocFltrArray<3>    
    ButtonEnabled  = ( (Sum(LocFlags) GT 0) or (NoMaterialFlag EQ True$) )
    Set_Property(@Window:'.PUB_GEN_REPORT', 'ENABLED', ButtonEnabled)
        
return


EnableReportColumns:
    
    ColFltrCtrl   = @Window:'.OLE_EDT_COL_FILTER'
    LocFltrCtrl   = @Window:'.OLE_EDT_LOC_FILTER'
    
    // Gather settings from form
    LocFltrArray = Get_Property(LocFltrCtrl, 'OLE.ARRAY')

    LocFlags               = LocFltrArray<3>
    KitRackFlag            = LocFlags<0, 1>
    PTIRackFlag            = LocFlags<0, 2>
    LoadedFlags            = Field(LocFlags, @VM , 3, 8)
    LoadedColEnabled       = (Sum(LoadedFlags) GT 0)
    EnabledCellColorArray  = 'None':@FM:'None'
    DisabledCellColorArray = 'None':@FM:"{240, 240, 240}"
    If KitRackFlag EQ True$ then
        Set_Property(ColFltrCtrl, "OLE.CellCheck[2; 10-13]", True$)
        Set_Property(ColFltrCtrl, "OLE.CellColors[All; 10-13]", EnabledCellColorArray)
        Set_Property(ColFltrCtrl, "OLE.CellProtection[All; 10-13]", 'SEL')
        Set_Property(ColFltrCtrl, "OLE.CellCheckEnabled[2; 10-13]", True$)
    end else
        Set_Property(ColFltrCtrl, "OLE.CellCheck[2; 10-13]", False$)
        Set_Property(ColFltrCtrl, "OLE.CellColors[All; 10-13]", DisabledCellColorArray)
        Set_Property(ColFltrCtrl, "OLE.CellProtection[All; 10-13]", 'FUL')
        Set_Property(ColFltrCtrl, "OLE.CellCheckEnabled[2; 10-13]", False$)
    end
    If PTIRackFlag EQ True$ then
        Set_Property(ColFltrCtrl, "OLE.CellCheck[2; 14]", True$)
        Set_Property(ColFltrCtrl, "OLE.CellColors[All; 14]", EnabledCellColorArray)
        Set_Property(ColFltrCtrl, "OLE.CellProtection[All; 14]", 'SEL')
        Set_Property(ColFltrCtrl, "OLE.CellCheckEnabled[2; 14]", True$)  
    end else
        Set_Property(ColFltrCtrl, "OLE.CellCheck[2; 14]", False$)
        Set_Property(ColFltrCtrl, "OLE.CellColors[All; 14]", DisabledCellColorArray)
        Set_Property(ColFltrCtrl, "OLE.CellProtection[All; 14]", 'FUL')
        Set_Property(ColFltrCtrl, "OLE.CellCheckEnabled[2; 14]", False$)
    end
    If LoadedColEnabled EQ True$ then
        Set_Property(ColFltrCtrl, "OLE.CellCheck[2; 15]", True$)
        Set_Property(ColFltrCtrl, "OLE.CellColors[All; 15]", EnabledCellColorArray)
        Set_Property(ColFltrCtrl, "OLE.CellProtection[All; 15]", 'SEL')
        Set_Property(ColFltrCtrl, "OLE.CellCheckEnabled[2; 15]", True$)  
    end else
        Set_Property(ColFltrCtrl, "OLE.CellCheck[2; 15]", False$)
        Set_Property(ColFltrCtrl, "OLE.CellColors[All; 15]", DisabledCellColorArray)
        Set_Property(ColFltrCtrl, "OLE.CellProtection[All; 15]", 'FUL')
        Set_Property(ColFltrCtrl, "OLE.CellCheckEnabled[2; 15]", False$)
    end
    
return

