Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.SystemModule
Imports System.Linq
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Utils

Public Class ProductsGant_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork

#Region "Overrides"
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "ProductsGant_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "ProductsGant_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub
#End Region

    Private Sub aProductsGant_GerenrateProductsGantRows_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProductsGant_GerenrateProductsGantRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _ProductsGant As ProductsGant = TryCast(View.SelectedObjects(0), ProductsGant)

        Dim _ProductsGantRows_DELCollection As New XPCollection(Of ProductsGantRows)(_uow, CriteriaOperator.Parse("ProductsGant.Oid=?", _ProductsGant.Oid))
        If _ProductsGantRows_DELCollection.Count > 0 Then

            Dim _ProductsGantRowsBar_DELCollection As New XPCollection(Of ProductsGantRowsBar)(_uow, CriteriaOperator.Parse("ProductsGantRows.ProductsGant.Oid=?", _ProductsGant.Oid))
            _uow.Delete(_ProductsGantRows_DELCollection)
            _uow.Delete(_ProductsGantRowsBar_DELCollection)

            _uow.CommitChanges()
        End If



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

        '                            _InvoiceRowsXPQ.InvoiceHeader.DateExecution.Date > #7/1/2013#

        Try
            RaiseEvent InitWork(1, 1, IRXPQ.Count)
            For Each item In IRXPQ
                RaiseEvent DoWork()
                Dim _ProductsGantRows As New ProductsGantRows(_ocur.Session)
                With _ProductsGantRows
                    .ProductsGant = _ProductsGant
                    .Customers = item.Customers
                    .Products = item.Products
                    .CalculatedRotation = 0
                    .ManualRotation = 0
                End With

                'Számlázott mennyiségekhez ProductsGantRowsBar
                Dim _InvoiceRows_Collection As New XPCollection(Of InvoiceRows)(_ocur.Session, CriteriaOperator.Parse("InvoiceHeader.Customers=? AND OrderInRows.Products=? AND InvoiceHeader.IsEditable=False AND " + _
                                                                                    "InvoiceHeader.IsStorno=False AND (IsNull(InvoiceHeader.DocumentNumber)=False OR InvoiceHeader.DocumentNumber<>'')", item.Customers, item.Products))

                If _InvoiceRows_Collection.Count > 0 Then
                    _InvoiceRows_Collection.Sorting.Add(New SortProperty("DateExecution", DB.SortingDirection.Ascending))
                    For Each _InvoiceRows As InvoiceRows In _InvoiceRows_Collection

                        Dim _ProductsGantRowsBar_OrderArrive As New ProductsGantRowsBar(_ocur.Session)
                        With _ProductsGantRowsBar_OrderArrive
                            .ProductsGantRows = _ProductsGantRows
                            .FromDate = New Date(_InvoiceRows.InvoiceHeader.DateExecution.Year, _InvoiceRows.InvoiceHeader.DateExecution.Month, _InvoiceRows.InvoiceHeader.DateExecution.Day, 0, 0, 0)
                            .ToDate = New Date(_InvoiceRows.InvoiceHeader.DateExecution.Year, _InvoiceRows.InvoiceHeader.DateExecution.Month, _InvoiceRows.InvoiceHeader.DateExecution.Day, 23, 59, 59)
                            .BarType = eBarType.eAccounted
                            .Caption = _InvoiceRows.QTT
                        End With
                    Next
                End If


                'Adott pillanatban leadott rendelés érkezése - dinamikos cucc, frissítésnél ezeket update!
                Dim _CustomersOrderPriority_Collection As New XPCollection(Of CustomersOrderPriority)(_ocur.Session, CriteriaOperator.Parse("Products=?", item.Products))

                If _CustomersOrderPriority_Collection.Count > 0 Then
                    _CustomersOrderPriority_Collection.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))

                    Dim _ProductsGantRowsBar_OrderArrive As New ProductsGantRowsBar(_ocur.Session)
                    With _ProductsGantRowsBar_OrderArrive
                        .ProductsGantRows = _ProductsGantRows
                        .FromDate = GetSQLTime(_ocur.Session)
                        If _CustomersOrderPriority_Collection(0).DeadlineWeek = 0 Then
                            .ToDate = .FromDate.AddDays(7)
                        Else
                            .ToDate = .FromDate.AddDays(_CustomersOrderPriority_Collection(0).DeadlineWeek * 7)
                        End If
                        .BarType = eBarType.eOrderArrive
                    End With
                End If

                'Jelentett készlet
                Dim _ProductsGantCustomersReport_Collection As New XPCollection(Of ProductsGantCustomersReport)(_ocur.Session, CriteriaOperator.Parse("Customers=? AND Products=?", item.Customers, item.Products))

                If _ProductsGantCustomersReport_Collection.Count > 0 Then
                    For Each _ProductsGantCustomersReport As ProductsGantCustomersReport In _ProductsGantCustomersReport_Collection
                        Dim _ProductsGantRowsBar_Reported As New ProductsGantRowsBar(_ocur.Session)
                        With _ProductsGantRowsBar_Reported
                            .ProductsGantRows = _ProductsGantRows
                            .FromDate = _ProductsGantCustomersReport.DateExecution
                            If .ProductsGantRows.Products.CustomersOrderPriority.Count > 0 Then
                                .ProductsGantRows.Products.CustomersOrderPriority.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))
                                .ToDate = .FromDate.AddDays(.ProductsGantRows.Products.CustomersOrderPriority(0).DeadlineWeek * 7)
                            Else
                                .ToDate = .FromDate.AddDays(_ProductsGantCustomersReport.QTT)
                            End If
                            .Caption = _ProductsGantCustomersReport.QTT.ToString
                            .BarType = eBarType.eReported
                        End With
                    Next
                End If


                'Rendelések
                Dim _OrderInRowsCollection As New XPCollection(Of OrderInRows)(_ocur.Session, CriteriaOperator.Parse("Products=? AND OrderInHeader.Customers=?", _ProductsGantRows.Products, _ProductsGantRows.Customers))

                If _OrderInRowsCollection.Count > 0 Then
                    For Each _OrderInRows As OrderInRows In _OrderInRowsCollection
                        Dim _ProductsGantRowsBar As New ProductsGantRowsBar(_ocur.Session)
                        With _ProductsGantRowsBar
                            .ProductsGantRows = _ProductsGantRows
                            .FromDate = New Date(_OrderInRows.OrderInHeader.DateExecution.Year, _OrderInRows.OrderInHeader.DateExecution.Month, _OrderInRows.OrderInHeader.DateExecution.Day, 0, 0, 0)
                            .ToDate = New Date(_OrderInRows.OrderInHeader.DateExecution.Year, _OrderInRows.OrderInHeader.DateExecution.Month, _OrderInRows.OrderInHeader.DateExecution.Day, 23, 59, 59)
                            .BarType = eBarType.eOrdered
                            .Caption = String.Format("{0}", _OrderInRows.QTT)
                        End With
                    Next
                End If
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            RaiseEvent FinalWork
        End Try
    End Sub

    Private Sub aProductsGant_UpdateProductsGantRows_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProductsGant_UpdateProductsGantRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _ProductsGant As ProductsGant = TryCast(View.SelectedObjects(0), ProductsGant)

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

        '                            _InvoiceRowsXPQ.InvoiceHeader.DateExecution.Date > #7/1/2013#
        Try
            RaiseEvent InitWork(1, 1, IRXPQ.Count)
            For Each item In IRXPQ
                RaiseEvent DoWork()
                Dim _ProductsGantRows As ProductsGantRows = _ocur.FindObject(Of ProductsGantRows)(CriteriaOperator.Parse("ProductsGant=? AND Customers=? AND Products=?", _ProductsGant, item.Customers, item.Products))

                If _ProductsGantRows Is Nothing Then
                    _ProductsGantRows = New ProductsGantRows(_ocur.Session)
                    With _ProductsGantRows
                        .ProductsGant = _ProductsGant
                        .Customers = item.Customers
                        .Products = item.Products
                        .CalculatedRotation = 0
                        .ManualRotation = 0
                    End With
                End If

                'Számlázott mennyiségekhez ProductsGantRowsBar
                Dim _InvoiceRows_Collection As New XPCollection(Of InvoiceRows)(_ocur.Session, CriteriaOperator.Parse("InvoiceHeader.Customers=? AND OrderInRows.Products=? AND InvoiceHeader.IsEditable=False AND " + _
                                                                                    "InvoiceHeader.IsStorno=False AND (IsNull(InvoiceHeader.DocumentNumber)=False OR InvoiceHeader.DocumentNumber<>'')", item.Customers, item.Products))
                If _InvoiceRows_Collection.Count > 0 Then
                    _InvoiceRows_Collection.Sorting.Add(New SortProperty("DateExecution", DB.SortingDirection.Ascending))
                    For Each _InvoiceRows As InvoiceRows In _InvoiceRows_Collection
                        Dim _FromDate As New Date(_InvoiceRows.InvoiceHeader.DateExecution.Year, _InvoiceRows.InvoiceHeader.DateExecution.Month, _InvoiceRows.InvoiceHeader.DateExecution.Day, 0, 0, 0)
                        Dim _ToDate = New Date(_InvoiceRows.InvoiceHeader.DateExecution.Year, _InvoiceRows.InvoiceHeader.DateExecution.Month, _InvoiceRows.InvoiceHeader.DateExecution.Day, 23, 59, 59)

                        Dim _ProductsGantRowsBar_InvoiceRows As ProductsGantRowsBar = _ocur.FindObject(Of ProductsGantRowsBar)(CriteriaOperator.Parse("ProductsGantRows=? AND FromDate=? AND ToDate=? AND BarType=0", _
                                                                                                                                                                _ProductsGantRows, _FromDate, _ToDate))
                        If _ProductsGantRowsBar_InvoiceRows Is Nothing Then
                            _ProductsGantRowsBar_InvoiceRows = New ProductsGantRowsBar(_ocur.Session)
                            With _ProductsGantRowsBar_InvoiceRows
                                .ProductsGantRows = _ProductsGantRows
                                .FromDate = _FromDate
                                .ToDate = _ToDate
                                .BarType = eBarType.eAccounted
                                .Caption = _InvoiceRows.QTT
                            End With
                        End If

                    Next
                End If


                'Adott pillanatban leadott rendelés érkezése - dinamikos cucc, frissítésnél ezeket update!
                Dim _CustomersOrderPriority_Collection As New XPCollection(Of CustomersOrderPriority)(_ocur.Session, CriteriaOperator.Parse("Products=?", item.Products))

                If _CustomersOrderPriority_Collection.Count > 0 Then
                    _CustomersOrderPriority_Collection.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))

                    Dim _ProductsGantRowsBar_OrderArrive As ProductsGantRowsBar = _ocur.FindObject(Of ProductsGantRowsBar)(CriteriaOperator.Parse("ProductsGantRows=? AND BarType=3", _ProductsGantRows))
                    If _ProductsGantRowsBar_OrderArrive Is Nothing Then
                        _ProductsGantRowsBar_OrderArrive = New ProductsGantRowsBar(_ocur.Session)
                        With _ProductsGantRowsBar_OrderArrive
                            .ProductsGantRows = _ProductsGantRows
                            .FromDate = GetSQLTime(_ocur.Session).Date
                            If _CustomersOrderPriority_Collection(0).DeadlineWeek = 0 Then
                                .ToDate = .FromDate.AddDays(7)
                            Else
                                .ToDate = .FromDate.AddDays(_CustomersOrderPriority_Collection(0).DeadlineWeek * 7)
                            End If
                            .BarType = eBarType.eOrderArrive
                        End With
                    End If

                End If

                'Jelentett készlet
                Dim _ProductsGantCustomersReport_Collection As New XPCollection(Of ProductsGantCustomersReport)(_ocur.Session, CriteriaOperator.Parse("Customers=? AND Products=?", item.Customers, item.Products))

                If _ProductsGantCustomersReport_Collection.Count > 0 Then
                    For Each _ProductsGantCustomersReport As ProductsGantCustomersReport In _ProductsGantCustomersReport_Collection

                        Dim _FromDate As Date = _ProductsGantCustomersReport.DateExecution
                        _ProductsGantRows.Products.CustomersOrderPriority.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))

                        Dim _ToDate As Date
                        If _ProductsGantRows.Products.CustomersOrderPriority.Count > 0 Then
                            _ToDate = _FromDate.AddDays(_ProductsGantRows.Products.CustomersOrderPriority(0).DeadlineWeek * 7)
                        Else
                            _ToDate = _FromDate.Date
                        End If


                        Dim _ProductsGantRowsBar_Reported As ProductsGantRowsBar = _ocur.FindObject(Of ProductsGantRowsBar)(CriteriaOperator.Parse("ProductsGantRows=? AND FromDate=? AND ToDate=? AND BarType=2", _
                                                                                                                                                   _ProductsGantRows, _FromDate, _ToDate))
                        If _ProductsGantRowsBar_Reported Is Nothing Then
                            _ProductsGantRowsBar_Reported = New ProductsGantRowsBar(_ocur.Session)
                            With _ProductsGantRowsBar_Reported
                                .ProductsGantRows = _ProductsGantRows
                                .FromDate = _FromDate
                                .ToDate = _ToDate
                                .Caption = _ProductsGantCustomersReport.QTT.ToString
                                .BarType = eBarType.eReported
                            End With
                        End If

                    Next
                End If


                'Rendelések
                Dim _OrderInRowsCollection As New XPCollection(Of OrderInRows)(_ocur.Session, CriteriaOperator.Parse("Products=? AND OrderInHeader.Customers=?", _ProductsGantRows.Products, _ProductsGantRows.Customers))

                If _OrderInRowsCollection.Count > 0 Then
                    For Each _OrderInRows As OrderInRows In _OrderInRowsCollection
                        Dim _FromDate As New Date(_OrderInRows.OrderInHeader.DateExecution.Year, _OrderInRows.OrderInHeader.DateExecution.Month, _OrderInRows.OrderInHeader.DateExecution.Day, 0, 0, 0)
                        Dim _ToDate As New Date(_OrderInRows.OrderInHeader.DateExecution.Year, _OrderInRows.OrderInHeader.DateExecution.Month, _OrderInRows.OrderInHeader.DateExecution.Day, 23, 59, 59)

                        Dim _ProductsGantRowsBar As ProductsGantRowsBar = _ocur.FindObject(Of ProductsGantRowsBar)(CriteriaOperator.Parse("ProductsGantRows=? AND FromDate=? AND ToDate=? AND BarType=1", _
                                                                                                                                          _ProductsGantRows, _FromDate, _ToDate))

                        If _ProductsGantRowsBar Is Nothing Then
                            _ProductsGantRowsBar = New ProductsGantRowsBar(_ocur.Session)
                            With _ProductsGantRowsBar
                                .ProductsGantRows = _ProductsGantRows
                                .FromDate = New Date(_OrderInRows.OrderInHeader.DateExecution.Year, _OrderInRows.OrderInHeader.DateExecution.Month, _OrderInRows.OrderInHeader.DateExecution.Day, 0, 0, 0)
                                .ToDate = New Date(_OrderInRows.OrderInHeader.DateExecution.Year, _OrderInRows.OrderInHeader.DateExecution.Month, _OrderInRows.OrderInHeader.DateExecution.Day, 23, 59, 59)
                                .BarType = eBarType.eOrdered
                                .Caption = String.Format("{0}", _OrderInRows.QTT)
                            End With
                        End If

                    Next
                End If
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            RaiseEvent FinalWork
        End Try
    End Sub
End Class
