Imports System.Linq
Imports DevExpress.ExpressApp
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Xpo

' For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
Partial Public Class DBV_Dashboard_VC
    Inherits ViewController(Of DashboardView)

    Private _DBV_CustomersOrder_ListView As DashboardViewItem
    Private _DBV_Customers_ListView As DashboardViewItem
    Private _DBV_OfferOutRows_ListView As DashboardViewItem
    Private _DBV_OrderInRows_ListView As DashboardViewItem
    Private _DBV_Products_ListView As DashboardViewItem

    Private _CustomersOrder As CustomersOrder
    Private _Customers As Customers
    Public Sub New()
        InitializeComponent()
        ' Target required Views (via the TargetXXX properties) and create their Actions.
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        ' Perform various tasks depending on the target View.
        If View.Id = "DVB_Dashboard" Then
            _DBV_CustomersOrder_ListView = CType(View.FindItem("DBV_CustomersOrder_ListView"), DashboardViewItem)
            _DBV_Customers_ListView = CType(View.FindItem("DBV_Customers_ListView"), DashboardViewItem)
            _DBV_OfferOutRows_ListView = CType(View.FindItem("DBV_OfferOutRows_ListView"), DashboardViewItem)
            _DBV_OrderInRows_ListView = CType(View.FindItem("DBV_OrderInRows_ListView"), DashboardViewItem)
            _DBV_Products_ListView = CType(View.FindItem("DBV_Products_ListView"), DashboardViewItem)

            If _DBV_CustomersOrder_ListView IsNot Nothing Then
                AddHandler _DBV_CustomersOrder_ListView.ControlCreated, AddressOf DBV_CustomersOrder_ListView_ControlCreated
            End If
            If _DBV_Customers_ListView IsNot Nothing Then
                AddHandler _DBV_Customers_ListView.ControlCreated, AddressOf DBV_Customers_ListView_ControlCreated
            End If
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        ' Access and customize the target View control.
    End Sub
    Protected Overrides Sub OnDeactivated()
        ' Unsubscribe from previously subscribed events and release other references and resources.
        MyBase.OnDeactivated()
    End Sub

    Private Sub DBV_CustomersOrder_ListView_ControlCreated(sender As Object, e As EventArgs)

        Dim _DashboardViewItem As DashboardViewItem = DirectCast(sender, DashboardViewItem)
        Dim _DBV_CustomersOrder_ListView As ListView = TryCast(_DashboardViewItem.InnerView, ListView)

        If _DBV_CustomersOrder_ListView IsNot Nothing Then
            RemoveHandler _DBV_CustomersOrder_ListView.SelectionChanged, AddressOf DBV_CustomersOrder_ListView_SelectionChanged
            AddHandler _DBV_CustomersOrder_ListView.SelectionChanged, AddressOf DBV_CustomersOrder_ListView_SelectionChanged
        End If
    End Sub

    Private Sub DBV_CustomersOrder_ListView_SelectionChanged(sender As Object, e As EventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(TryCast(_DBV_CustomersOrder_ListView.InnerView, ListView).ObjectSpace, Xpo.XPObjectSpace)
        Dim _Criteria As String = ""
        Dim _Criteria2 As String = ""
        Dim _Criteria3 As String = ""


        If TryCast(_DBV_CustomersOrder_ListView.InnerView, ListView).SelectedObjects.Count > 0 Then
            _CustomersOrder = TryCast(_DBV_CustomersOrder_ListView.InnerView, ListView).SelectedObjects(0)
            Dim _Exists As Boolean = False
            Dim _CustomersOrderPriority_Collection As New XPCollection(Of CustomersOrderPriority)(_ocur.Session, CriteriaOperator.Parse("CustomersOrder.Oid=?", _CustomersOrder.Oid))

            For Each _CustomersOrderPriority As CustomersOrderPriority In _CustomersOrderPriority_Collection
                If _Criteria3 = "" Then
                    _Criteria3 = String.Format("Oid in ('{0}'", _CustomersOrderPriority.Products.Oid)
                Else
                    _Criteria3 += String.Format(",'{0}'", _CustomersOrderPriority.Products.Oid)
                End If

                _Exists = True
            Next
            If _Exists Then
                _Criteria3 += ")"
            Else
                _Criteria3 = "1=2"
            End If




            _Exists = False
            For Each _CustomersOrderPriority As CustomersOrderPriority In _CustomersOrderPriority_Collection
                Dim _OrderInRows_Collection As New XPCollection(Of OrderInRows)(_ocur.Session, CriteriaOperator.Parse("Products=?", _CustomersOrderPriority.Products))

                For Each _OrderInRows As OrderInRows In _OrderInRows_Collection
                    If _Criteria = "" Then
                        _Criteria = String.Format("Oid in ('{0}'", _OrderInRows.OrderInHeader.Customers.Oid)
                    Else
                        _Criteria += String.Format(",'{0}'", _OrderInRows.OrderInHeader.Customers.Oid)
                    End If
                    _Exists = True
                Next
            Next
            If _Exists Then
                _Criteria += ")"
            Else
                _Criteria = "1=2"
            End If

            _Exists = False

            For Each _CustomersOrderPriority As CustomersOrderPriority In _CustomersOrderPriority_Collection

                Dim _OfferOutRows_Collection As New XPCollection(Of OfferOutRows)(_ocur.Session, CriteriaOperator.Parse("Product=?", _CustomersOrderPriority.Products))

                For Each _OfferOutRows As OfferOutRows In _OfferOutRows_Collection
                    If _Criteria2 = "" Then
                        _Criteria2 = String.Format("Oid in ('{0}'", _OfferOutRows.OfferOutHeader.Customers.Oid)
                    Else
                        _Criteria2 += String.Format(",'{0}'", _OfferOutRows.OfferOutHeader.Customers.Oid)
                    End If
                    _Exists = True
                Next
            Next
            If _Exists Then
                _Criteria2 += ")"
            Else
                _Criteria2 = "1=2"
            End If

            _Criteria = String.Format("({0}) OR ({1})", _Criteria, _Criteria2)
        Else
            _CustomersOrder = Nothing
            _Criteria = "1=2"
        End If

        

        TryCast(_DBV_Customers_ListView.InnerView, ListView).CollectionSource.Criteria.Clear()
        TryCast(_DBV_Customers_ListView.InnerView, ListView).CollectionSource.BeginUpdateCriteria()
        TryCast(_DBV_Customers_ListView.InnerView, ListView).CollectionSource.Criteria("Criteria") = CriteriaOperator.Parse(_Criteria)
        TryCast(_DBV_Customers_ListView.InnerView, ListView).CollectionSource.EndUpdateCriteria()

        TryCast(_DBV_Products_ListView.InnerView, ListView).CollectionSource.Criteria.Clear()
        TryCast(_DBV_Products_ListView.InnerView, ListView).CollectionSource.BeginUpdateCriteria()
        TryCast(_DBV_Products_ListView.InnerView, ListView).CollectionSource.Criteria("Criteria") = CriteriaOperator.Parse(_Criteria3)
        TryCast(_DBV_Products_ListView.InnerView, ListView).CollectionSource.EndUpdateCriteria()


    End Sub

    Private Sub DBV_Customers_ListView_ControlCreated(sender As Object, e As EventArgs)
        Dim _DashboardViewItem As DashboardViewItem = DirectCast(sender, DashboardViewItem)
        Dim _DBV_Customers_ListView As ListView = TryCast(_DashboardViewItem.InnerView, ListView)

        If _DBV_Customers_ListView IsNot Nothing Then
            RemoveHandler _DBV_Customers_ListView.SelectionChanged, AddressOf DBV_Customers_ListView_SelectionChanged
            AddHandler _DBV_Customers_ListView.SelectionChanged, AddressOf DBV_Customers_ListView_SelectionChanged
        End If
    End Sub

    Private Sub DBV_Customers_ListView_SelectionChanged(sender As Object, e As EventArgs)
        If _CustomersOrder Is Nothing Then Exit Sub

        Dim _ocur As Xpo.XPObjectSpace = TryCast(TryCast(_DBV_Customers_ListView.InnerView, ListView).ObjectSpace, Xpo.XPObjectSpace)
        Dim _Criteria As String = ""
        Dim _Criteria2 As String = ""
        Dim _Exists As Boolean = False

        If TryCast(_DBV_Customers_ListView.InnerView, ListView).SelectedObjects.Count > 0 Then
            _Customers = TryCast(_DBV_Customers_ListView.InnerView, ListView).SelectedObjects(0)
        Else
            _Customers = Nothing
        End If

        If _Customers IsNot Nothing Then
            Dim _CustomersOrderPriority_Collection As New XPCollection(Of CustomersOrderPriority)(_ocur.Session, CriteriaOperator.Parse("CustomersOrder.Oid=?", _CustomersOrder.Oid))
            For Each _CustomersOrderPriority As CustomersOrderPriority In _CustomersOrderPriority_Collection
                Dim _OrderInRows_Collection As New XPCollection(Of OrderInRows)(_ocur.Session, CriteriaOperator.Parse("Products=?", _CustomersOrderPriority.Products))
                For Each _OrderInRows As OrderInRows In _OrderInRows_Collection
                    If _Criteria = "" Then
                        _Criteria = String.Format("Oid in ('{0}'", _OrderInRows.Oid)
                    Else
                        _Criteria += String.Format(",'{0}'", _OrderInRows.Oid)
                    End If
                    _Exists = True
                Next
            Next
            If _Exists Then
                _Criteria += ")"
            Else
                _Criteria = "1=2"
            End If
        Else
            _Criteria = "1=2"
        End If

        TryCast(_DBV_OrderInRows_ListView.InnerView, ListView).CollectionSource.Criteria.Clear()

        TryCast(_DBV_OrderInRows_ListView.InnerView, ListView).CollectionSource.BeginUpdateCriteria()
        TryCast(_DBV_OrderInRows_ListView.InnerView, ListView).CollectionSource.Criteria("Products") = CriteriaOperator.Parse(_Criteria)
        If _Customers IsNot Nothing Then TryCast(_DBV_OrderInRows_ListView.InnerView, ListView).CollectionSource.Criteria("Customers") = CriteriaOperator.Parse("OrderInHeader.Customers.Oid=?", _Customers.Oid)
        TryCast(_DBV_OrderInRows_ListView.InnerView, ListView).CollectionSource.EndUpdateCriteria()

        _Exists = False
        If _Customers IsNot Nothing Then
            Dim _CustomersOrderPriority_Collection As New XPCollection(Of CustomersOrderPriority)(_ocur.Session, CriteriaOperator.Parse("CustomersOrder.Oid=?", _CustomersOrder.Oid))
            For Each _CustomersOrderPriority As CustomersOrderPriority In _CustomersOrderPriority_Collection
                Dim _OfferOutRows_Collection As New XPCollection(Of OfferOutRows)(_ocur.Session, CriteriaOperator.Parse("Product=?", _CustomersOrderPriority.Products))
                For Each _OfferOutRows As OfferOutRows In _OfferOutRows_Collection
                    If _Criteria2 = "" Then
                        _Criteria2 = String.Format("Oid in ('{0}'", _OfferOutRows.Oid)
                    Else
                        _Criteria2 += String.Format(",'{0}'", _OfferOutRows.Oid)
                    End If
                    _Exists = True
                Next
            Next
            If _Exists Then
                _Criteria2 += ")"
            Else
                _Criteria2 = "1=2"
            End If
        Else
            _Criteria2 = "1=2"
        End If




        TryCast(_DBV_OfferOutRows_ListView.InnerView, ListView).CollectionSource.Criteria.Clear()
        TryCast(_DBV_OfferOutRows_ListView.InnerView, ListView).CollectionSource.BeginUpdateCriteria()
        TryCast(_DBV_OfferOutRows_ListView.InnerView, ListView).CollectionSource.Criteria("Products") = CriteriaOperator.Parse(_Criteria2)
        If _Customers IsNot Nothing Then TryCast(_DBV_OfferOutRows_ListView.InnerView, ListView).CollectionSource.Criteria("Customers") = CriteriaOperator.Parse("OfferOutHeader.Customers.Oid=?", _Customers.Oid)
        TryCast(_DBV_OfferOutRows_ListView.InnerView, ListView).CollectionSource.EndUpdateCriteria()

    End Sub

End Class
