Imports System
Imports System.ComponentModel

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports System.Linq
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Utils

<DefaultClassOptions()> _
<NonPersistent()>
<CreatableItem(False)> _
<NavigationItem(False)> _
Public Class ProductsGant_CreateCustomersReport_PopUp
    Inherits BaseObject

    Private _Customers As Customers
    Private _CustomersFrom As CustomersFrom
    Private _Products As Products
    Private _ProductsGantRows As ProductsGantRows
    Private _DateExecution As Date

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
    End Sub

    <VisibleInDetailView(False)> _
    <VisibleInListView(False)> _
    Property Customers As Customers
        Get
            Return _Customers
        End Get
        Set(value As Customers)
            SetPropertyValue("Customers", _Customers, value)
        End Set
    End Property
    <VisibleInDetailView(False)> _
  <VisibleInListView(False)> _
    Property CustomersFrom As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    <VisibleInDetailView(False)> _
 <VisibleInListView(False)> _
    Property Products As Products
        Get
            Return _Products
        End Get
        Set(value As Products)
            SetPropertyValue("Products", _Products, value)
        End Set
    End Property
    Property ProductsGantRows As ProductsGantRows
        Get
            Return _ProductsGantRows
        End Get
        Set(value As ProductsGantRows)
            SetPropertyValue("ProductsGantRows", _ProductsGantRows, value)
        End Set
    End Property
    <ImmediatePostData()> _
    Property DateExecution As Date
        Get
            Return _DateExecution
        End Get
        Set(value As Date)
            SetPropertyValue("DateExecution", _DateExecution, value)
            If Not Session.IsObjectsSaving And Not Session.IsObjectsLoading Then
                '    ProductsGantCustomersReport.Reload()
            End If
        End Set
    End Property

    ReadOnly Property ProductsGantCustomersReport As XPCollection(Of ProductsGantCustomersReport)
        Get
            Dim _ProductsGantCustomersReport_Collection As New XPCollection(Of ProductsGantCustomersReport)(Session, CriteriaOperator.Parse("1=2"))

            Dim InvoiceRowsXPQ As New XPQuery(Of InvoiceRows)(Session)
            Dim IRXPQ = From _InvoiceRowsXPQ In InvoiceRowsXPQ
                        Where _InvoiceRowsXPQ.OrderInRows IsNot Nothing And
                                _InvoiceRowsXPQ.OrderInRows.Products IsNot Nothing And
                                _InvoiceRowsXPQ.InvoiceHeader.IsStorno = False And
                                _InvoiceRowsXPQ.InvoiceHeader.CustomersFrom.Oid = CustomersFrom.Oid And
                                _InvoiceRowsXPQ.InvoiceHeader.Customers.Oid = Customers.Oid
                        Group _InvoiceRowsXPQ By _InvoiceRowsXPQ.OrderInRows.Products Into g = Group _
                        Select New With {Key .Products = Products}


            If IRXPQ.Count > 0 Then
                For Each _IRXPQ_Item In IRXPQ
                    Dim _ProductsGantCustomersReport As New ProductsGantCustomersReport(Session)
                    With _ProductsGantCustomersReport
                        .Customers = Customers
                        .Products = _IRXPQ_Item.Products
                        .DateExecution = DateExecution.Date
                    End With

                    _ProductsGantCustomersReport_Collection.Add(_ProductsGantCustomersReport)

                Next
            End If

            Dim _ProductsGantCustomersReport_Existing_Collection As New XPCollection(Of ProductsGantCustomersReport) _
                (Session, CriteriaOperator.Parse("Customers=? AND Products=? AND GetDate(DateExecution)=?", Customers, Products, DateExecution.Date))

            If _ProductsGantCustomersReport_Existing_Collection.Count > 0 Then
                For Each _ProductsGantCustomersReport As ProductsGantCustomersReport In _ProductsGantCustomersReport_Existing_Collection
                    _ProductsGantCustomersReport_Collection.Add(_ProductsGantCustomersReport)
                Next
            End If

            Return _ProductsGantCustomersReport_Collection
        End Get
    End Property
End Class
