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.Data.Filtering
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.SystemModule
Imports System.Linq
Imports System.Linq.Expressions
Imports DevExpress.Xpo

Public Class OrderInRowsVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "ORI_ChangeQTT_PopUp_DetailView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf ORI_ChangeQTT_PopUp_DetailView_AcceptAction_Executing
        End If

        If View.Id = "OrderInRowsAdvanceBallanceInfo_ListView" Then

            AddHandler Frame.GetController(Of RefreshController).RefreshAction.Executed, AddressOf OrderInRowsAdvanceBallanceInfo_ListView_RefreshAction_Executed
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()

        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "ORI_ChangeQTT_PopUp_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf ORI_ChangeQTT_PopUp_DetailView_AcceptAction_Executing
        End If
    End Sub
    Private Sub aMoveOrderInRowsToOtherOrderInHeader_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aMoveOrderInRowsToOtherOrderInHeader.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _OrderInRows As OrderInRows = TryCast(View.SelectedObjects(0), OrderInRows)
            Dim _OrderInHeader_Collectionsource As New CollectionSource(_onew, GetType(OrderInHeader))

            _OrderInHeader_Collectionsource.BeginUpdateCriteria()
            _OrderInHeader_Collectionsource.Criteria.Add("Criteria", CriteriaOperator.Parse("Customers=? AND IsEditable=True AND IsStorno=False", _onew.GetObject(_OrderInRows.OrderInHeader.Customers)))
            _OrderInHeader_Collectionsource.EndUpdateCriteria()

            e.View = Application.CreateListView("OrderInHeader_ListView_Select", _OrderInHeader_Collectionsource, True)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
    Private Sub aMoveOrderInRowsToOtherOrderInHeader_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aMoveOrderInRowsToOtherOrderInHeader.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OrderInHeader_New As OrderInHeader = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OrderInHeader))
            If _OrderInHeader_New Is Nothing Then Throw New Exception("*Nem lett kiválasztva az új bejövő megrendelés!")
            For Each _OrderInRows As OrderInRows In View.SelectedObjects
                _OrderInRows.OrderInHeader = _OrderInHeader_New
                _OrderInRows.RowIndex = _OrderInHeader_New.OrderInRows.Count
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
    Private Sub aMoveOrderInRowsOtherToOtherOrderInHeader_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aMoveOrderInRowsOtherToOtherOrderInHeader.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _OrderInRowsOther As OrderInRowsOther = TryCast(View.SelectedObjects(0), OrderInRowsOther)
            Dim _OrderInHeader_Collectionsource As New CollectionSource(_onew, GetType(OrderInHeader))

            _OrderInHeader_Collectionsource.BeginUpdateCriteria()
            _OrderInHeader_Collectionsource.Criteria.Add("Criteria", CriteriaOperator.Parse("Customers=? AND IsEditable=True AND IsStorno=False", _onew.GetObject(_OrderInRowsOther.OrderInHeader.Customers)))
            _OrderInHeader_Collectionsource.EndUpdateCriteria()

            e.View = Application.CreateListView("OrderInHeader_ListView_Select", _OrderInHeader_Collectionsource, True)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
    Private Sub aMoveOrderInRowsOtherToOtherOrderInHeader_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aMoveOrderInRowsOtherToOtherOrderInHeader.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OrderInHeader_New As OrderInHeader = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OrderInHeader))
            If _OrderInHeader_New Is Nothing Then Throw New Exception("*Nem lett kiválasztva az új bejövő megrendelés!")
            For Each _OrderInRowsOther As OrderInRowsOther In View.SelectedObjects
                _OrderInRowsOther.OrderInHeader = _OrderInHeader_New
                _OrderInRowsOther.RowIndex = _OrderInHeader_New.OrderInRowsOther.Count
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub

    Private Sub aORI_ChangeQTT_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aORI_ChangeQTT.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OrderInRows As OrderInRows = TryCast(View.SelectedObjects(0), OrderInRows)
        Dim _ORI_ChangeQTT_PopUp As New ORI_ChangeQTT_PopUp(_onew.Session)

        With _ORI_ChangeQTT_PopUp
            .QTT = _OrderInRows.QTT
            .QTT_Storaged = _OrderInRows.QTT_Storaged
            .QTT_Storaged_Will = _OrderInRows.QTT_Storaged_Will
            .OrderInRows = _onew.GetObject(_OrderInRows)
        End With

        e.View = Application.CreateDetailView(_onew, "ORI_ChangeQTT_PopUp_DetailView", False, _ORI_ChangeQTT_PopUp)
    End Sub
    Private Sub aORI_ChangeQTT_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aORI_ChangeQTT.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ORI_ChangeQTT_PopUp As ORI_ChangeQTT_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ORI_ChangeQTT_PopUp)
        Dim _OrderInRows As OrderInRows = TryCast(View.SelectedObjects(0), OrderInRows)
        _OrderInRows.QTT = _ORI_ChangeQTT_PopUp.QTT
        _ocur.CommitChanges()
    End Sub
    Private Sub ORI_ChangeQTT_PopUp_DetailView_AcceptAction_Executing(sender As Object, e As CancelEventArgs)
        Try
            Dim _ORI_ChangeQTT_PopUp As ORI_ChangeQTT_PopUp = TryCast(View.SelectedObjects(0), ORI_ChangeQTT_PopUp)
            If _ORI_ChangeQTT_PopUp IsNot Nothing Then
                If _ORI_ChangeQTT_PopUp.QTT < _ORI_ChangeQTT_PopUp.QTT_Storaged Then
                    e.Cancel = True
                    Throw New Exception("*A rendelt mennyiség kisebb, mint a már kitárolt mennyiség!")
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub


    Private Sub OrderInRowsAdvanceBallanceInfo_ListView_RefreshAction_Executed(sender As Object, e As ActionBaseEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderInRowsAdvanceBallanceInfo As OrderInRowsAdvanceBallanceInfo
        Dim _InvoiceRows_XPQuery As New XPQuery(Of InvoiceRows)(_ocur.Session)

        Dim query1 = From _InvoiceRows In _InvoiceRows_XPQuery
                     Where String.IsNullOrEmpty(_InvoiceRows.InvoiceHeader.DocumentNumber) = False And
                           _InvoiceRows.InvoiceHeader.IsEditable = False And
                           _InvoiceRows.ShortName.ToUpper.Contains("ELŐLEG") And
                           _InvoiceRows.InvoiceHeader IsNot Nothing
                     Group _InvoiceRows By _InvoiceRows.InvoiceHeader.Customers Into g = Group
                     Select New With {.Customers = Customers,
                                      .BallanceHUF_Advance = g.Sum(Function(inv) inv.SumPriceBruttoHUF)}


        RaiseEvent InitWork(1, 1, query1.Count)
        For Each item In query1
            RaiseEvent DoWork()
            If Math.Abs(Math.Round(item.BallanceHUF_Advance, 0, MidpointRounding.AwayFromZero)) > 100 Then
                _OrderInRowsAdvanceBallanceInfo = _ocur.CreateObject(Of OrderInRowsAdvanceBallanceInfo)()
                _OrderInRowsAdvanceBallanceInfo.Customers = item.Customers
                _OrderInRowsAdvanceBallanceInfo.BallanceHUF_Advance = item.BallanceHUF_Advance
                TryCast(View, ListView).CollectionSource.Add(_OrderInRowsAdvanceBallanceInfo)

                Dim _OrderInRows_Collection As New XPCollection(Of OrderInRows)(_ocur.Session, CriteriaOperator.Parse("IsNull(OrderInHeader.GCRecord)=True and IsNull(GCRecord)=True and OrderInHeader.Customers=? and OrderInHeader.IsEditable=True and IsNull(OrderInHeader)=False", item.Customers))
                Dim _OrderInRows As OrderInRows

                For Each _OrderInRows In _OrderInRows_Collection
                    _OrderInRowsAdvanceBallanceInfo = _ocur.CreateObject(Of OrderInRowsAdvanceBallanceInfo)()
                    _OrderInRowsAdvanceBallanceInfo.Customers = item.Customers
                    _OrderInRowsAdvanceBallanceInfo.BallanceHUF_Advance = 0
                    _OrderInRowsAdvanceBallanceInfo.OrderInRows = _OrderInRows
                    TryCast(View, ListView).CollectionSource.Add(_OrderInRowsAdvanceBallanceInfo)
                Next
            End If
        Next
        RaiseEvent FinalWork


    End Sub

End Class
