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

Public Class OrderOutVC
    Inherits DevExpress.ExpressApp.ViewController
    Private _OrderOutRows_Corr As OrderOutRows
    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()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of OrderOutVC)().aUp_OrderOut.Active.SetItemValue("", False) ' -- Kimenõ megrendelés sorainak a row indexét csökkentjük
        Frame.GetController(Of OrderOutVC)().aDown_OrderOut.Active.SetItemValue("", False) ' -- Kimenõ megrendelés sorainak a row indexét növeljük
        Frame.GetController(Of OrderOutVC)().aFinalizeOrderOut.Active.SetItemValue("", False) ' -- Véglegesít egy bejövõ megrendelést
        Frame.GetController(Of OrderOutVC)().aStornoOrderOut.Active.SetItemValue("", False) ' -- Stornírozz egy bejövõ megrendelést
        Frame.GetController(Of OrderOutVC)().aToTable_OrderOut.Active.SetItemValue("", False) ' -- Sor hozzáadása
        Frame.GetController(Of OrderOutVC)().aToTableD_OrderOut.Active.SetItemValue("", False) ' -- Sor törlése
        Frame.GetController(Of OrderOutVC)().aGenerateDeliveryNoteIn_From_OrderOut.Active.SetItemValue("", False) ' -- Létrehoz egy bejövő szállítólevelet kimenő megrendelésből
        Frame.GetController(Of OrderOutVC)().aGenerateDeliveryNoteIn_From_OrderOutForControlListView.Active.SetItemValue("", False)
        Frame.GetController(Of OrderOutVC)().aBackToRowEdit_OrderOut.Active.SetItemValue("", False)
        '--------- LISTVIEWS -------------------------------------------------------------------------------------------------------------------------------
        If View.Id = "OrderOutHeader_OrderOutRows_ListView" Then '--- Kimenõ megrendelések sorai
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of OrderOutVC)().aUp_OrderOut.Active.SetItemValue("", True) ' -- Kimenõ megrendelés sorainak a row indexét csökkentjük
            Frame.GetController(Of OrderOutVC)().aDown_OrderOut.Active.SetItemValue("", True) ' -- Kimenõ megrendelés sorainak a row indexét növeljük
        End If
        If View.Id = "OrderOutHeader_ListView_Editable" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OrderOutHeader_ListView" Then
            Frame.GetController(Of OrderOutVC)().aGenerateDeliveryNoteIn_From_OrderOut.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OrderOutRows_ListView_Table" Then
            Frame.GetController(Of OrderOutVC)().aGenerateDeliveryNoteIn_From_OrderOut.Active.SetItemValue("", True)
        End If
        If View.Id = "OrderOutHeader_OrderOutRows_ListView_Corrigendum" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OrderOutHeader_OrderInTo_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If

        '--------- DETAILVIEWS -------------------------------------------------------------------------------------------------------------------------------
        If View.Id = "OrderOutHeader_DetailView" Then
            Frame.GetController(Of OrderOutVC)().aToTable_OrderOut.Active.SetItemValue("", True) ' -- Sor hozzáadása
            Frame.GetController(Of OrderOutVC)().aToTableD_OrderOut.Active.SetItemValue("", True) ' -- Sor törlése
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)

            Dim _OrderOutHeader As OrderOutHeader = TryCast(View.SelectedObjects(0), OrderOutHeader)

            If _OrderOutHeader.IsEditable = True And _OrderOutHeader.IsStorno = False Then
                Frame.GetController(Of OrderOutVC)().aFinalizeOrderOut.Active.SetItemValue("", True)
                Frame.GetController(Of OrderOutVC)().aStornoOrderOut.Active.SetItemValue("", False)
            End If
            If _OrderOutHeader.IsEditable = False Then
                Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
                Frame.GetController(Of OrderOutVC)().aFinalizeOrderOut.Active.SetItemValue("", False)
                Frame.GetController(Of OrderOutVC)().aToTable_OrderOut.Active.SetItemValue("", False)
                Frame.GetController(Of OrderOutVC)().aToTableD_OrderOut.Active.SetItemValue("", False)
            End If
            If _OrderOutHeader.IsStorno = False AndAlso _OrderOutHeader.IsEditable = False Then
                Frame.GetController(Of OrderOutVC)().aStornoOrderOut.Active.SetItemValue("", True)
                Frame.GetController(Of OrderOutVC)().aFinalizeOrderOut.Active.SetItemValue("", False)
            End If
        End If

        If View.Id = "OrderOutHeader_DetailView_Corrigendum" Then
            Frame.GetController(Of OrderOutVC)().aToTable_OrderOut.Active.SetItemValue("", True)
            Frame.GetController(Of OrderOutVC)().aToTableD_OrderOut.Active.SetItemValue("", True)
            Frame.GetController(Of OrderOutVC)().aBackToRowEdit_OrderOut.Active.SetItemValue("", True)
            Frame.GetController(Of OrderOutVC)().aFinalizeOrderOut.Active.SetItemValue("", True)

        End If
        If View.Id = "OrderOutHeader_ListView_Deleted" Then
            If TryCast(View, ListView) IsNot Nothing Then
                If TryCast(View, ListView).CollectionSource IsNot Nothing Then
                    If TryCast(View, ListView).CollectionSource.Collection IsNot Nothing Then
                        If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection) IsNot Nothing Then
                            If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection IsNot Nothing Then
                                If TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, XPCollection) IsNot Nothing Then
                                    TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, XPCollection).SelectDeleted = True
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
        If View.Id = "OrderOutRows_ListView_Control" Then
            Frame.GetController(Of OrderOutVC)().aGenerateDeliveryNoteIn_From_OrderOutForControlListView.Active.SetItemValue("", True)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        If View.Id = "OrderOutHeader_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)

        End If

        If View.Id = "OrderOutHeader_OrderOutRows_ListView" Then '--- Kimenõ megrendelések sorai
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        End If

        If View.Id = "OrderOutHeader_ListView" Then
            Frame.GetController(Of OrderOutVC)().aGenerateDeliveryNoteIn_From_OrderOut.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OrderOutHeader_OrderOutRows_ListView_Corrigendum" Then
            '    RemoveHandler View.SelectionChanged, AddressOf OrderOutHeader_OrderOutRows_ListView_Corrigendum_SelectionChanged
        End If

        If View.Id = "OrderOutHeader_OrderOutRows_ListView_Corrigendum" Then
            Frame.GetController(Of OrderOutVC)().aToTable_OrderOut.Active.SetItemValue("", False)
            Frame.GetController(Of OrderOutVC)().aToTableD_OrderOut.Active.SetItemValue("", False)
            Frame.GetController(Of OrderOutVC)().aBackToRowEdit_OrderOut.Active.SetItemValue("", False)
            Frame.GetController(Of OrderOutVC)().aFinalizeOrderOut.Active.SetItemValue("", False)
        End If
        If View.Id = "OrderOutHeader_OrderOutRows_ListView_Corrigendum" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OrderOutHeader_ListView_Editable" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OrderOutHeader_OrderInTo_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
    End Sub
    '--------- Kimenõ megrendelések akciói ---------------------------------------------------------------------------------------------------------------------------------------
    Private Sub aToTableD_OrderOut_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_OrderOut.Execute
        Dim _OrderOutRows As OrderOutRows
        Dim _OrderOutHeader As OrderOutHeader = TryCast(View.SelectedObjects(0), OrderOutHeader)
        Dim _OrderOutRows_ListEditor As ListPropertyEditor

        _OrderOutRows_ListEditor = TryCast(View, DetailView).FindItem("OrderOutRows")
        For Each _OrderOutRows In _OrderOutRows_ListEditor.ListView.SelectedObjects
            _OrderOutRows.RemoveRows(_OrderOutHeader)
        Next
        _OrderOutHeader.Save()

        View.Refresh()
    End Sub
    Private Sub aToTable_OrderOut_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_OrderOut.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderOutHeader As OrderOutHeader = CType(e.SelectedObjects(0), OrderOutHeader)
        Dim _OrderOutRows As OrderOutRows = _ocur.CreateObject(Of OrderOutRows)()
        Dim _OrderOutRows_Item As ViewItem = TryCast(View, DetailView).FindItem("@OrderOutHeader.OrderOutRows")

        If View.Id = "OrderOutHeader_DetailView_Corrigendum" Then
            If _OrderOutRows_Item IsNot Nothing Then
                If TryCast(_OrderOutRows_Item, ListPropertyEditor) IsNot Nothing Then
                    If TryCast(_OrderOutRows_Item, ListPropertyEditor).ListView.SelectedObjects.Count > 0 Then
                        _OrderOutRows_Corr = TryCast(_OrderOutRows_Item, ListPropertyEditor).ListView.SelectedObjects(0)
                    End If
                End If
            End If
            If _OrderOutRows_Corr Is Nothing Then
                ThrowNewException("Nincs kiválasztva a helyesbítendő sor !")
                Exit Sub
            End If
        End If
        _OrderOutRows.AddRows(_OrderOutHeader, _OrderOutRows_Corr)
        If _OrderOutRows.OrderOutRows Is Nothing And _OrderOutHeader.row_QTT <= 0 Then
            ThrowNewException("A mennyiség értéke nem lehet nulla!")
        Else
            Dim ItemToFocus As ViewItem = TryCast(View, DetailView).FindItem("row_Products")
            If ItemToFocus IsNot Nothing Then
                Dim ItemControl As Object
                ItemControl = ItemToFocus.Control
                ItemControl.Focus()
            End If


            _ocur.CommitChanges()
            _OrderOutRows_Corr = Nothing

            _OrderOutHeader.row_DiscountPercent = 0
            _OrderOutHeader.row_ListPriceDEV = 0
            _OrderOutHeader.row_ListPriceHUF = 0
            _OrderOutHeader.row_Products = Nothing
            _OrderOutHeader.row_QTT = 0

        End If

        View.Refresh()
    End Sub
    Private Sub aDown_OrderOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aDown_OrderOut.Execute
        ' A kiválaszott sort egy sorral lejebb helyezi
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OrderOutRowsSelected As OrderOutRows = TryCast(View.SelectedObjects(0), OrderOutRows)
            If _OrderOutRowsSelected Is Nothing Then Throw New Exception("*Nem lett kiválasztva egyetlen sor sem!")
            Dim _OrderOutHeader As OrderOutHeader = _OrderOutRowsSelected.OrderOutHeader
            If _OrderOutHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            If _OrderOutRowsSelected.RowIndex = 0 Then
                Dim _AllIndexIsZero As Boolean = True
                For Each _OrderOutRows As OrderOutRows In _OrderOutHeader.OrderOutRows
                    If _OrderOutRows.RowIndex <> 0 Then
                        _AllIndexIsZero = False
                        Exit For
                    End If
                Next

                If _AllIndexIsZero Then
                    Dim _RowIndex As Long = 1
                    For Each _OrderOutRows As OrderOutRows In _OrderOutHeader.OrderOutRows
                        _OrderOutRows.RowIndex = _RowIndex
                        _RowIndex += 1
                    Next
                Else
                    _OrderOutHeader.OrderOutRows.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))
                    Dim _RowIndex As Long = 1
                    For Each _OrderOutRows As OrderOutRows In _OrderOutHeader.OrderOutRows
                        _OrderOutRows.RowIndex = _RowIndex
                        _RowIndex += 1
                    Next
                End If
            Else
                If _OrderOutRowsSelected.RowIndex <> _OrderOutHeader.OrderOutRows.Count Then
                    Dim _OrderOutRowsNext As OrderOutRows = _ocur.FindObject(Of OrderOutRows)(CriteriaOperator.Parse("OrderOutHeader=? AND RowIndex=?", _OrderOutHeader, _OrderOutRowsSelected.RowIndex + 1))
                    If _OrderOutRowsNext IsNot Nothing Then
                        _OrderOutRowsSelected.RowIndex += 1
                        _OrderOutRowsNext.RowIndex -= 1
                    End If
                Else
                    Throw New Exception("*A kiválasztott sor a legutolsó helyen van!")
                End If

            End If
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aUp_OrderOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aUp_OrderOut.Execute
        ' A kiválaszott sort egy sorral feljebb helyezi
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OrderOutRowsSelected As OrderOutRows = TryCast(View.SelectedObjects(0), OrderOutRows)
            If _OrderOutRowsSelected Is Nothing Then Throw New Exception("*Nem lett kiválasztva egyetlen sor sem!")
            Dim _OrderOutHeader As OrderOutHeader = _OrderOutRowsSelected.OrderOutHeader
            If _OrderOutHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            If _OrderOutRowsSelected.RowIndex = 0 Then
                Dim _AllIndexIsZero As Boolean = True
                For Each _OrderOutRows As OrderOutRows In _OrderOutHeader.OrderOutRows
                    If _OrderOutRows.RowIndex <> 0 Then
                        _AllIndexIsZero = False
                        Exit For
                    End If
                Next

                If _AllIndexIsZero Then
                    Dim _RowIndex As Long = 1
                    For Each _OrderOutRows As OrderOutRows In _OrderOutHeader.OrderOutRows
                        _OrderOutRows.RowIndex = _RowIndex
                        _RowIndex += 1
                    Next
                Else
                    _OrderOutHeader.OrderOutRows.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))
                    Dim _RowIndex As Long = 1
                    For Each _OrderOutRows As OrderOutRows In _OrderOutHeader.OrderOutRows
                        _OrderOutRows.RowIndex = _RowIndex
                        _RowIndex += 1
                    Next
                End If
            Else
                If _OrderOutRowsSelected.RowIndex <> 1 Then
                    Dim _OrderOutRowsPrew As OrderOutRows = _ocur.FindObject(Of OrderOutRows)(CriteriaOperator.Parse("OrderOutHeader=? AND RowIndex=?", _OrderOutHeader, _OrderOutRowsSelected.RowIndex - 1))
                    If _OrderOutRowsPrew IsNot Nothing Then
                        _OrderOutRowsSelected.RowIndex -= 1
                        _OrderOutRowsPrew.RowIndex += 1
                    End If
                Else
                    Throw New Exception("*A kiválasztott sor a legelső helyen van!")
                End If

            End If
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aFinalizeOrderOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalizeOrderOut.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderOutHeader As OrderOutHeader = TryCast(View.SelectedObjects(0), OrderOutHeader)

        _OrderOutHeader.IsEditable = False

        If _OrderOutHeader.DocumentNumber = "" Then
            _OrderOutHeader.DocumentNumber = _OrderOutHeader.CustomersOrderDocuments.NumberGenerator.GetNewNumber(_OrderOutHeader.CustomersOrderDocuments.NumberGenerator)
        End If

        If _OrderOutHeader.CurrencyName.ShortName = "HUF" Then
            _OrderOutHeader.TotalNettoHUF = 0
            For Each _actOrderOutRows As OrderOutRows In _OrderOutHeader.OrderOutRows
                Dim _actPrice As Double = 0
                ' -- mennyiség X egységár
                _actPrice = _actOrderOutRows.ListPriceHUF * _actOrderOutRows.QTT
                ' -- Kedvezmény mértéke érvényesítése
                If _actOrderOutRows.DiscountPercent > 0 Then
                    _actPrice = _actPrice - _actPrice * (_actOrderOutRows.DiscountPercent / 100)
                End If
                ' -- Totál Netto HUF számítás
                _OrderOutHeader.TotalNettoHUF += _actPrice
            Next
        Else
            _OrderOutHeader.TotalNettoDEV = 0
            For Each _actOrderOutRows As OrderOutRows In _OrderOutHeader.OrderOutRows
                Dim _actPrice As Double = 0
                ' -- mennyiség X egységár
                _actPrice = _actOrderOutRows.ListPriceDEV * _actOrderOutRows.QTT
                ' -- Kedvezmény mértéke érvényesítése
                If _actOrderOutRows.DiscountPercent > 0 Then
                    _actPrice = _actPrice - _actPrice * (_actOrderOutRows.DiscountPercent / 100)
                End If
                ' -- Tolál Netto DEV számítás
                _OrderOutHeader.TotalNettoDEV += _actPrice
            Next
        End If

        _OrderOutHeader.Save()
        _ocur.CommitChanges()
        View.Refresh()
        View.Close()
    End Sub
    Private Sub aStornoOrderOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aStornoOrderOut.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderInHeader As OrderOutHeader = TryCast(View.SelectedObjects(0), OrderOutHeader)

        _OrderInHeader.IsStorno = True

        _ocur.CommitChanges()
        View.Refresh()
    End Sub
    Private Sub aCorrOrderOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCorrOrderOut.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OrderOutHeader_New As OrderOutHeader

        Dim _OrderOutHeader As OrderOutHeader = TryCast(View.SelectedObjects(0), OrderOutHeader)

        If _OrderOutHeader IsNot Nothing Then
            _OrderOutHeader_New = _onew.CreateObject(Of OrderOutHeader)()
            _OrderOutHeader_New.OrderOutHeader = _onew.GetObject(_OrderOutHeader)
            'Új OrderOutheader feltöltése
            _OrderOutHeader_New.CustomersFrom = _onew.GetObject(_OrderOutHeader.CustomersFrom)
            _OrderOutHeader_New.CustomersOrder = _onew.GetObject(_OrderOutHeader.CustomersOrder)
            _OrderOutHeader_New.CustomersOrderDocuments = _onew.GetObject(_OrderOutHeader.CustomersOrderDocuments)
            _OrderOutHeader_New.Contacts = _onew.GetObject(_OrderOutHeader.Contacts)

            'Új bizonylatazám...
            If _OrderOutHeader.OrderOutHeader IsNot Nothing Then
                Dim _actNum As Int32 = Convert.ToInt32(_OrderOutHeader.DocumentNumber.Remove(0, InStr(_OrderOutHeader.DocumentNumber, "->") + 1))
                _OrderOutHeader_New.DocumentNumber = _OrderOutHeader.DocumentNumber.Remove(InStr(_OrderOutHeader.DocumentNumber, "->"), _OrderOutHeader.DocumentNumber.Length - InStr(_OrderOutHeader.DocumentNumber, "->")) + ">" + (_actNum + 1).ToString()
            Else 'Még nem volt módoítva
                _OrderOutHeader_New.DocumentNumber = _OrderOutHeader.DocumentNumber + "->1"
            End If

            'Új View...
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "OrderOutHeader_DetailView_Corrigendum", True, _OrderOutHeader_New)
            ' e.ShowViewParameters.TargetWindow = TargetWindow.Default
        End If
    End Sub
    Private Sub aOrderOutPrint_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOrderOutPrint.Execute
        Dim _ReportData As ReportData
        Dim _OrderOutHeader As OrderOutHeader
        If e.SelectedObjects.Count > 0 Then
            _OrderOutHeader = TryCast(e.SelectedObjects.Item(0), OrderOutHeader)
            If _OrderOutHeader IsNot Nothing Then
                _ReportData = _OrderOutHeader.CustomersOrderDocuments.ReportData

                If _ReportData IsNot Nothing Or _OrderOutHeader Is Nothing Then
                    Dim _inoperator As New InOperator("OrderOutHeader", e.SelectedObjects)
                    Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("OrderOutHeader=?", _OrderOutHeader), True)
                Else
                    ThrowNewException("Nincs meg a nyomtatvány!")
                End If
            End If
        End If
    End Sub
    Private Sub aGenerateDeliveryNoteIn_From_OrderOut_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGenerateDeliveryNoteIn_From_OrderOut.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
            Dim _OrderOutRows As OrderOutRows
            Dim _OrderOutHeader As OrderOutHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), OrderOutHeader))
            If _OrderOutHeader Is Nothing Then _
                _OrderOutHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), OrderOutRows).OrderOutHeader)
            If _OrderOutHeader Is Nothing Then Throw New Exception("*Nem lehet azonosítani a kimenő megrendelés fejlécet!")
            Dim _DeliveryNoteInFromOrderOutRowsHeader As New DeliveryNoteInFromOrderOutRowsHeader(_onew.Session)
            Dim _QTT_Sum As Double = 0

            _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader = _OrderOutHeader
            _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.DateCreated = GetSQLTime(_onew.Session)

            Dim _OrderOutRows_XPQuery As New XPQuery(Of DeliveryNoteInRows)(_onew.Session)
            Dim query = From _DNIR In _OrderOutRows_XPQuery
                            Where _DNIR.OrderOutRows.OrderOutHeader Is _OrderOutHeader
                                Group _DNIR By _DNIR.OrderOutRows Into g = Group _
                Select New With {Key .OrderOutRows = OrderOutRows,
                                 Key .QTT_Sum = g.Sum(Function(inv) inv.QTT)}

            For Each _OrderOutRows In _OrderOutHeader.OrderOutRows
                _QTT_Sum = 0
                For Each item In query
                    If item.OrderOutRows.Oid = _OrderOutRows.Oid Then
                        _QTT_Sum = item.QTT_Sum
                    End If
                Next
                If _OrderOutRows.QTT > _QTT_Sum Then
                    Dim _DeliveryNoteInFromOrderOutRowsRows As New DeliveryNoteInFromOrderOutRowsRows(_onew.Session)

                    _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows = _OrderOutRows
                    _DeliveryNoteInFromOrderOutRowsRows.DeliveryNoteInFromOrderOutRowsHeader = _DeliveryNoteInFromOrderOutRowsHeader
                    _DeliveryNoteInFromOrderOutRowsRows.QTT_Process = _OrderOutRows.QTT - _QTT_Sum
                    _DeliveryNoteInFromOrderOutRowsRows.Save()
                End If
            Next

            _DeliveryNoteInFromOrderOutRowsHeader.Save()
            _onew.CommitChanges()

            e.View = Application.CreateDetailView(_onew, _DeliveryNoteInFromOrderOutRowsHeader, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aGenerateDeliveryNoteIn_From_OrderOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGenerateDeliveryNoteIn_From_OrderOut.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInHeader_New As New DeliveryNoteInHeader(_ocur.Session)
        Dim _DeliveryNoteInFromOrderOutRowsHeader As DeliveryNoteInFromOrderOutRowsHeader = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), DeliveryNoteInFromOrderOutRowsHeader))
        Dim _DeliveryNoteInFromOrderOutRowsRows_ListEditor As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("DeliveryNoteInFromOrderOutRowsRows")
        Dim _DeliveryNoteInType As DeliveryNoteInType = _ocur.GetObjectByKey(Of DeliveryNoteInType)(TryCast(e.PopupWindow.View.SelectedObjects(0), DeliveryNoteInFromOrderOutRowsHeader).DeliveryNoteInType.Oid)

        ' --- Létrehozzuk a bej. szállítólevél fejlécet ----------------------------------------------------------------------------------------------------------------
        _DeliveryNoteInHeader_New.CurrencyName = _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.CurrencyName
        _DeliveryNoteInHeader_New.CustomersFrom = _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.CustomersFrom
        _DeliveryNoteInHeader_New.PaymentOption = _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.PaymentOption
        _DeliveryNoteInHeader_New.QualityOption = _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.QualityOption
        _DeliveryNoteInHeader_New.Customers = TryCast(_DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.CustomersOrder, Customers)
        _DeliveryNoteInHeader_New.DeliveryNoteInType = _DeliveryNoteInType
        '-------------------------------------------------------------------------------------------------------------------------------------------------
        Try
            ' Létrehozzuk a bej. szállítólevél sorait, minden kiválasztott kimenő megrenedlési sorhoz ----------------------------------------------------------------------------------------------
            For i = 0 To _DeliveryNoteInFromOrderOutRowsRows_ListEditor.ListView.SelectedObjects.Count - 1 ' A kiválasztott sorokon végigmegyünk és létrehozzuk a bej. szállítólevél sorokat.
                Dim _DeliveryNoteInRows_New As DeliveryNoteInRows = _ocur.CreateObject(Of DeliveryNoteInRows)()
                Dim _QTT_Process As Double = 0
                Dim _DeliveryNoteInFromOrderOutRowsRows As DeliveryNoteInFromOrderOutRowsRows = _ocur.GetObject(TryCast(_DeliveryNoteInFromOrderOutRowsRows_ListEditor.ListView.SelectedObjects(i), DeliveryNoteInFromOrderOutRowsRows))
                _QTT_Process = TryCast(_DeliveryNoteInFromOrderOutRowsRows_ListEditor.ListView.SelectedObjects(i), DeliveryNoteInFromOrderOutRowsRows).QTT_Process ' Kinyerjük az adott sorhoz tartozó szándékot

                _DeliveryNoteInRows_New.DeliveryNoteInHeader = _DeliveryNoteInHeader_New
                _DeliveryNoteInRows_New.OrderOutRows = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows
                _DeliveryNoteInRows_New.Products = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.Products
                If _QTT_Process > _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.QTT - _DeliveryNoteInFromOrderOutRowsRows.QTT_DeliveryNoteIn Then
                    Throw New Exception("*Nem volt ennyi megrendelve a tételből !")
                Else
                    _DeliveryNoteInRows_New.QTT = _QTT_Process
                    _DeliveryNoteInRows_New.OrderOutRows.QTT_DeliveryNoteIn += _QTT_Process
                End If
                _DeliveryNoteInRows_New.controlled_DiscountPercent = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.DiscountPercent
                _DeliveryNoteInRows_New.NoteRows = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.NoteRows
                _DeliveryNoteInRows_New.VAT = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.Products.VAT
                _DeliveryNoteInRows_New.Units = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.Products.Units
                _DeliveryNoteInRows_New.ListPriceHUF = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceHUF
                _DeliveryNoteInRows_New.ListPriceDEV = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceDEV
                _DeliveryNoteInRows_New.DiscountPriceHUF = Math.Round((_DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.DiscountPercent / 100) * _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceHUF, 4, MidpointRounding.AwayFromZero)
                _DeliveryNoteInRows_New.DiscountPriceDEV = Math.Round((_DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.DiscountPercent / 100) * _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceDEV, 4, MidpointRounding.AwayFromZero)

                _DeliveryNoteInRows_New.RowIndex = i

                If _DeliveryNoteInHeader_New.DeliveryNoteInType.IsPrice = True Then
                    _DeliveryNoteInRows_New.ListPriceHUF = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceHUF - _DeliveryNoteInRows_New.DiscountPriceHUF
                    _DeliveryNoteInRows_New.ListPriceDEV = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceDEV - _DeliveryNoteInRows_New.ListPriceDEV
                End If

                _DeliveryNoteInRows_New.Save()
            Next
            '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            _DeliveryNoteInHeader_New.Save()
            _ocur.CommitChanges()

            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ShowViewParameters As New ShowViewParameters
            Dim _ShowViewSource As New ShowViewSource(Frame, Nothing)

            _ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "DeliveryNoteInHeader_DetailView", True, _onew.GetObject(_DeliveryNoteInHeader_New))
            _ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild
            _ShowViewParameters.TargetWindow = TargetWindow.NewWindow
            Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try

    End Sub
    Private Sub aCloneOrderOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
        ' Az akció egy kiválasztott kimenő renderlést klónoz,a hozzá tartozó sorokkal, módosíthatóan
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OrderOutHeader As OrderOutHeader = _onew.GetObject(TryCast(e.SelectedObjects(0), OrderOutHeader))
        Dim _OrderOutHeader_New As OrderOutHeader = _onew.CreateObject(Of OrderOutHeader)()
        Dim _OrderOutRows As OrderOutRows

        'Fejléc adatokból csak a Saját cég kell, a többi nem!
        _OrderOutHeader_New.CustomersFrom = _OrderOutHeader.CustomersFrom
        _OrderOutHeader_New.IsEditable = True
        _OrderOutHeader_New.IsStorno = False
        _OrderOutHeader_New.Save()

        'Sorok átvétele
        For Each _OrderOutRows In _OrderOutHeader.OrderOutRows
            Dim _OrderOutRows_New As OrderOutRows = _onew.CreateObject(Of OrderOutRows)()
            _OrderOutRows_New.OrderOutHeader = _OrderOutHeader_New
            _OrderOutRows_New.RowIndex = _OrderOutRows.RowIndex
            _OrderOutRows_New.ShipmentOption = _OrderOutRows.ShipmentOption
            _OrderOutRows_New.QualityOption = _OrderOutRows.QualityOption
            _OrderOutRows_New.UrgentOption = _OrderOutRows.UrgentOption
            _OrderOutRows_New.Products = _OrderOutRows.Products
            _OrderOutRows_New.QTT = _OrderOutRows.QTT
            _OrderOutRows_New.ListPriceHUF = _OrderOutRows.ListPriceHUF
            _OrderOutRows_New.ListPriceDEV = _OrderOutRows.ListPriceDEV
            _OrderOutRows_New.DiscountPercent = _OrderOutRows.DiscountPercent
            _OrderOutRows_New.NoteRows = _OrderOutRows.NoteRows
            _OrderOutRows_New.NoteRowsPrivate = _OrderOutRows.NoteRowsPrivate
            _OrderOutRows_New.OrderOutRows = _OrderOutRows.OrderOutRows

            _OrderOutRows_New.Save()
        Next
        ' muti-muti mit csináltam!
        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, _OrderOutHeader_New)
    End Sub
    Private Sub aCloneOrderOutNEW_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCloneOrderOutNEW.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OrderOutHeader As OrderOutHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), OrderOutHeader))
        Dim _OrderOutHeader_Clone_View As OrderOutHeader_Clone_View = _onew.CreateObject(Of OrderOutHeader_Clone_View)()

        _OrderOutHeader_Clone_View.CustomersFrom = _OrderOutHeader.CustomersFrom
        _OrderOutHeader_Clone_View.CustomersOrder = Nothing
        _OrderOutHeader_Clone_View.CustomersOrderDocuments = Nothing
        _OrderOutHeader_Clone_View.Contacts = Nothing

        e.View = Application.CreateDetailView(_onew, "OrderOutHeader_Clone_View_DetailView", False, _OrderOutHeader_Clone_View)
    End Sub
    Private Sub aCloneOrderOutNEW_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCloneOrderOutNEW.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderOutHeader As OrderOutHeader = TryCast(View.SelectedObjects(0), OrderOutHeader)
        Dim _OrderOutHeader_Clone_View As OrderOutHeader_Clone_View = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OrderOutHeader_Clone_View))

        If _OrderOutHeader_Clone_View.CustomersOrderDocuments.CurrencyName.ShortName = _OrderOutHeader.CustomersOrderDocuments.CurrencyName.ShortName Then
            Dim _OrderOutHeader_New As OrderOutHeader = _ocur.CreateObject(Of OrderOutHeader)()
            'Fejléc adatok átvétele
            _OrderOutHeader_New.CustomersFrom = _ocur.GetObject(_OrderOutHeader_Clone_View.CustomersFrom)
            _OrderOutHeader_New.CustomersOrder = _ocur.GetObject(_OrderOutHeader_Clone_View.CustomersOrder)
            _OrderOutHeader_New.CustomersOrderDocuments = _ocur.GetObject(_OrderOutHeader_Clone_View.CustomersOrderDocuments)
            _OrderOutHeader_New.Contacts = _ocur.GetObject(_OrderOutHeader_Clone_View.Contacts)
            _OrderOutHeader_New.CurrencyName = _ocur.GetObject(_OrderOutHeader.CurrencyName)
            'Sorok átvétele
            For Each _OrderOutRows As OrderOutRows In _OrderOutHeader.OrderOutRows
                Dim _OrderOutRows_New As OrderOutRows = _ocur.CreateObject(Of OrderOutRows)()
                _OrderOutRows_New.OrderOutHeader = _OrderOutHeader_New
                _OrderOutRows_New.RowIndex = _OrderOutRows.RowIndex
                _OrderOutRows_New.ShipmentOption = _OrderOutRows.ShipmentOption
                _OrderOutRows_New.QualityOption = _OrderOutRows.QualityOption
                _OrderOutRows_New.UrgentOption = _OrderOutRows.UrgentOption
                _OrderOutRows_New.Products = _OrderOutRows.Products
                _OrderOutRows_New.QTT = _OrderOutRows.QTT
                _OrderOutRows_New.ListPriceHUF = _OrderOutRows.ListPriceHUF
                _OrderOutRows_New.ListPriceDEV = _OrderOutRows.ListPriceDEV
                _OrderOutRows_New.DiscountPercent = _OrderOutRows.DiscountPercent
                _OrderOutRows_New.NoteRows = _OrderOutRows.NoteRows
                _OrderOutRows_New.NoteRowsPrivate = _OrderOutRows.NoteRowsPrivate
                '_OrderOutRows_New.OrderOutRows = _OrderOutRows.OrderOutRows

                _OrderOutRows_New.Save()
            Next
            _OrderOutHeader_New.Save()
            'e.ShowViewParameters.CreatedView = Application.CreateDetailView(_ocur, "OrderOutHeader_DetailView", False, _OrderOutHeader_New)
            _ocur.CommitChanges()

        Else
            ThrowNewException("Nem azonosak a valuta nemek, nem klónozható a kiválasztott sor a jelenlegi beállításokkal!")
            Return
        End If
    End Sub
    Private Sub aBackToRowEdit_OrderOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToRowEdit_OrderOut.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        'Dim _OrderOutHeader As OrderOutHeader = _ocur.CreateObject(Of OrderOutHeader)()
        Dim _OrderOutHeader As OrderOutHeader = View.SelectedObjects(0) 'Kijelölt elem fejlécének betöltése

        Dim _ListView As ListPropertyEditor = TryCast(View, DetailView).FindItem("@OrderOutHeader.OrderOutRows") 'Sorok összeszedése
        If _ListView.ListView.SelectedObjects.Count > 0 Then

            Dim _OrderOutRows As OrderOutRows = _ListView.ListView.SelectedObjects(0)  'Régi sor változóba töltése

            _OrderOutHeader.row_Products = _OrderOutRows.Products
            _OrderOutHeader.row_DiscountPercent = _OrderOutRows.DiscountPercent
            _OrderOutHeader.row_ListPriceDEV = _OrderOutRows.ListPriceDEV
            _OrderOutHeader.row_ListPriceHUF = _OrderOutRows.ListPriceHUF
            _OrderOutHeader.row_QTT = _OrderOutRows.QTT

            _OrderOutRows_Corr = _OrderOutRows
        End If
    End Sub
    Private Sub aCheckOrderOutRows_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aCheckOrderOutRows.Execute
        '// Ellenőrzi a megjött tételeket
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderOutRows As OrderOutRows

        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)

        Dim _OrderOutRows_Collection_Total As New XPCollection(Of OrderOutRows)(_ocur.Session, CriteriaOperator.Parse("1=1"))

        Dim _OrderOutRows_Collection_IsCorrigendum As New XPCollection(Of OrderOutRows)(_ocur.Session, New InOperator("OrderOutRows", _OrderOutRows_Collection_Total))
        RaiseEvent InitWork(1, 1, _OrderOutRows_Collection_IsCorrigendum.Count)
        For Each _OrderOutRows In _OrderOutRows_Collection_IsCorrigendum
            RaiseEvent DoWork()
            If _OrderOutRows.IsCorrigendum = False Then
                _OrderOutRows.IsCorrigendum = True
            End If
        Next
        RaiseEvent FinalWork
        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _OrderOutRows In View.SelectedObjects
            RaiseEvent DoWork()
            Dim _DeliveryNoteInRows_Collection As New XPCollection(Of DeliveryNoteInRows)(_ocur.Session, CriteriaOperator.Parse("OrderOutRows=?", _OrderOutRows))
            Dim _DeliveryNoteInRows As DeliveryNoteInRows
            _OrderOutRows.QTT_DeliveryNoteIn = 0
            For Each _DeliveryNoteInRows In _DeliveryNoteInRows_Collection
                _OrderOutRows.QTT_DeliveryNoteIn += _DeliveryNoteInRows.QTT
            Next

            If _OrderOutRows.OrderOutRows IsNot Nothing Then
                _OrderOutRows.QTT_DeliveryNoteIn += _OrderOutRows.OrderOutRows.QTT_DeliveryNoteIn
            End If

            _ocur.CommitChanges()

        Next
        RaiseEvent FinalWork


    End Sub
    Private Sub aGenerateDeliveryNoteIn_From_OrderOutForControlListView_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aGenerateDeliveryNoteIn_From_OrderOutForControlListView.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
            Dim _OrderOutRows As OrderOutRows
            Dim _OrderOutHeader As OrderOutHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), OrderOutHeader))
            If _OrderOutHeader Is Nothing Then _
                _OrderOutHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), OrderOutRows).OrderOutHeader)
            If _OrderOutHeader Is Nothing Then Throw New Exception("*Nem lehet azonosítani a kimenő megrendelés fejlécet!")
            Dim _DeliveryNoteInFromOrderOutRowsHeader As New DeliveryNoteInFromOrderOutRowsHeader(_onew.Session)
            Dim _QTT_Sum As Double = 0

            _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader = _OrderOutHeader
            _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.DateCreated = GetSQLTime(_onew.Session)

            Dim _OrderOutRows_XPQuery As New XPQuery(Of DeliveryNoteInRows)(_onew.Session)
            Dim query = From _DNIR In _OrderOutRows_XPQuery
                            Where _DNIR.OrderOutRows.OrderOutHeader Is _OrderOutHeader
                                Group _DNIR By _DNIR.OrderOutRows Into g = Group _
                Select New With {Key .OrderOutRows = OrderOutRows,
                                 Key .QTT_Sum = g.Sum(Function(inv) inv.QTT)}

            For Each _OrderOutRows In _OrderOutHeader.OrderOutRows
                _QTT_Sum = 0
                For Each item In query
                    If item.OrderOutRows.Oid = _OrderOutRows.Oid Then
                        _QTT_Sum = item.QTT_Sum
                    End If
                Next
                If _OrderOutRows.QTT > _QTT_Sum Then
                    Dim _DeliveryNoteInFromOrderOutRowsRows As New DeliveryNoteInFromOrderOutRowsRows(_onew.Session)

                    _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows = _OrderOutRows
                    _DeliveryNoteInFromOrderOutRowsRows.DeliveryNoteInFromOrderOutRowsHeader = _DeliveryNoteInFromOrderOutRowsHeader
                    _DeliveryNoteInFromOrderOutRowsRows.QTT_Process = _OrderOutRows.QTT - _QTT_Sum
                    _DeliveryNoteInFromOrderOutRowsRows.Save()
                End If
            Next

            _DeliveryNoteInFromOrderOutRowsHeader.Save()
            _onew.CommitChanges()

            e.View = Application.CreateDetailView(_onew, _DeliveryNoteInFromOrderOutRowsHeader, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aGenerateDeliveryNoteIn_From_OrderOutForControlListView_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aGenerateDeliveryNoteIn_From_OrderOutForControlListView.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInHeader_New As New DeliveryNoteInHeader(_ocur.Session)
        Dim _DeliveryNoteInFromOrderOutRowsHeader As DeliveryNoteInFromOrderOutRowsHeader = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), DeliveryNoteInFromOrderOutRowsHeader))
        Dim _DeliveryNoteInFromOrderOutRowsRows_ListEditor As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("DeliveryNoteInFromOrderOutRowsRows")
        Dim _DeliveryNoteInType As DeliveryNoteInType = _ocur.GetObjectByKey(Of DeliveryNoteInType)(TryCast(e.PopupWindow.View.SelectedObjects(0), DeliveryNoteInFromOrderOutRowsHeader).DeliveryNoteInType.Oid)

        ' --- Létrehozzuk a bej. szállítólevél fejlécet ----------------------------------------------------------------------------------------------------------------
        _DeliveryNoteInHeader_New.CurrencyName = _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.CurrencyName
        _DeliveryNoteInHeader_New.CustomersFrom = _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.CustomersFrom
        _DeliveryNoteInHeader_New.PaymentOption = _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.PaymentOption
        _DeliveryNoteInHeader_New.QualityOption = _DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.QualityOption
        _DeliveryNoteInHeader_New.Customers = TryCast(_DeliveryNoteInFromOrderOutRowsHeader.OrderOutHeader.CustomersOrder, Customers)
        _DeliveryNoteInHeader_New.DeliveryNoteInType = _DeliveryNoteInType
        '-------------------------------------------------------------------------------------------------------------------------------------------------
        Try
            ' Létrehozzuk a bej. szállítólevél sorait, minden kiválasztott kimenő megrenedlési sorhoz ----------------------------------------------------------------------------------------------
            For i = 0 To _DeliveryNoteInFromOrderOutRowsRows_ListEditor.ListView.SelectedObjects.Count - 1 ' A kiválasztott sorokon végigmegyünk és létrehozzuk a bej. szállítólevél sorokat.
                Dim _DeliveryNoteInRows_New As DeliveryNoteInRows = _ocur.CreateObject(Of DeliveryNoteInRows)()
                Dim _QTT_Process As Double = 0
                Dim _DeliveryNoteInFromOrderOutRowsRows As DeliveryNoteInFromOrderOutRowsRows = _ocur.GetObject(TryCast(_DeliveryNoteInFromOrderOutRowsRows_ListEditor.ListView.SelectedObjects(i), DeliveryNoteInFromOrderOutRowsRows))
                _QTT_Process = TryCast(_DeliveryNoteInFromOrderOutRowsRows_ListEditor.ListView.SelectedObjects(i), DeliveryNoteInFromOrderOutRowsRows).QTT_Process ' Kinyerjük az adott sorhoz tartozó szándékot

                _DeliveryNoteInRows_New.DeliveryNoteInHeader = _DeliveryNoteInHeader_New
                _DeliveryNoteInRows_New.OrderOutRows = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows
                _DeliveryNoteInRows_New.Products = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.Products
                If _QTT_Process > _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.QTT - _DeliveryNoteInFromOrderOutRowsRows.QTT_DeliveryNoteIn Then
                    Throw New Exception("*Nem volt ennyi megrendelve a tételből !")
                Else
                    _DeliveryNoteInRows_New.QTT = _QTT_Process
                    _DeliveryNoteInRows_New.OrderOutRows.QTT_DeliveryNoteIn += _QTT_Process
                End If
                _DeliveryNoteInRows_New.controlled_DiscountPercent = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.DiscountPercent
                _DeliveryNoteInRows_New.NoteRows = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.NoteRows
                _DeliveryNoteInRows_New.VAT = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.Products.VAT
                _DeliveryNoteInRows_New.Units = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.Products.Units
                _DeliveryNoteInRows_New.ListPriceHUF = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceHUF
                _DeliveryNoteInRows_New.ListPriceDEV = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceDEV
                _DeliveryNoteInRows_New.DiscountPriceHUF = Math.Round((_DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.DiscountPercent / 100) * _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceHUF, 4, MidpointRounding.AwayFromZero)
                _DeliveryNoteInRows_New.DiscountPriceDEV = Math.Round((_DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.DiscountPercent / 100) * _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceDEV, 4, MidpointRounding.AwayFromZero)

                _DeliveryNoteInRows_New.RowIndex = i

                If _DeliveryNoteInHeader_New.DeliveryNoteInType.IsPrice = True Then
                    _DeliveryNoteInRows_New.ListPriceHUF = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceHUF - _DeliveryNoteInRows_New.DiscountPriceHUF
                    _DeliveryNoteInRows_New.ListPriceDEV = _DeliveryNoteInFromOrderOutRowsRows.OrderOutRows.ListPriceDEV - _DeliveryNoteInRows_New.ListPriceDEV
                End If

                _DeliveryNoteInRows_New.Save()
            Next
            '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            _DeliveryNoteInHeader_New.Save()
            _ocur.CommitChanges()

            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ShowViewParameters As New ShowViewParameters
            Dim _ShowViewSource As New ShowViewSource(Frame, Nothing)

            _ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "DeliveryNoteInHeader_DetailView", True, _onew.GetObject(_DeliveryNoteInHeader_New))
            _ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild
            _ShowViewParameters.TargetWindow = TargetWindow.NewWindow
            Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try

    End Sub


End Class
