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

Public Class DeliveryNoteInVC
    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()

        Frame.GetController(Of DeliveryNoteInVC).aTotable_DeliveryNoteIn.Active.SetItemValue("", False) ' -- Sor hozzáadása (DeliveryNoteInHeader_DeliveryNoteInRows_ListView)
        Frame.GetController(Of DeliveryNoteInVC).aTotableD_DeliveryNoteIn.Active.SetItemValue("", False) ' -- Sor törlése (DeliveryNoteInHeader_DeliveryNoteInRows_ListView)
        Frame.GetController(Of DeliveryNoteInVC).aUp_DeliveryNoteIn.Active.SetItemValue("", False) ' kiválasztott sor fel
        Frame.GetController(Of DeliveryNoteInVC).aDown_DeliveryNoteIn.Active.SetItemValue("", False) ' kiválasztott sor le
        Frame.GetController(Of DeliveryNoteInVC).aFinalize_DeliveryNoteIn.Active.SetItemValue("", False) ' véglegesítés letiltása
        Frame.GetController(Of DeliveryNoteInVC).aStorno_DeliveryNoteIn.Active.SetItemValue("", False) ' sztorno letiltása
        Frame.GetController(Of DeliveryNoteInVC).aGenerateStorageIn_From_DeliveryNoteInRows.Active.SetItemValue("", False) ' Betárolás bejövő szállítólevél sorokból
        Frame.GetController(Of DeliveryNoteInVC).aFinalize_And_Storage_DeliveryNoteIn.Active.SetItemValue("", False)
        '------------------------- LISTVIEWS --------------------------------------------------------------------------------------------------------------------------------------

        If View.Id = "DeliveryNoteInHeader_DeliveryNoteInrows_ListView" Then ' -- A beágyazott ListView feletti toolbar
            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.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeliveryNoteInVC).aUp_DeliveryNoteIn.Active.SetItemValue("", True)
            Frame.GetController(Of DeliveryNoteInVC).aDown_DeliveryNoteIn.Active.SetItemValue("", True)
        End If
        If View.Id = "DeliveryNoteInHeader_ListView" Then
            Frame.GetController(Of DeliveryNoteInVC).aGenerateStorageIn_From_DeliveryNoteInRows.Active.SetItemValue("", True)
        End If
        If View.Id = "StorageInfromDeliveryNoteInRowsHeader_StorageInfromDeliveryNoteInRowsRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If
        If View.Id = "DeliveryNoteInFromOrderOutRowsHeader_DeliveryNoteInFromOrderOutRowsRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If
        '------------------------- DETAILVIEWS ------------------------------------------------------------------------------------------------------------------------------------
        If View.Id = "DeliveryNoteInHeader_DetailView" Then
            Frame.GetController(Of DeliveryNoteInVC).aFinalize_DeliveryNoteIn.Active.SetItemValue("", True)
            Frame.GetController(Of DeliveryNoteInVC).aStorno_DeliveryNoteIn.Active.SetItemValue("", True)
            Frame.GetController(Of DeliveryNoteInVC).aFinalize_And_Storage_DeliveryNoteIn.Active.SetItemValue("", True)
            Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = TryCast(View.SelectedObjects(0), DeliveryNoteInHeader)
            If _DeliveryNoteInHeader IsNot Nothing Then
                Frame.GetController(Of DeliveryNoteInVC).aTotable_DeliveryNoteIn.Active.SetItemValue("", True)
                Frame.GetController(Of DeliveryNoteInVC).aTotableD_DeliveryNoteIn.Active.SetItemValue("", True)
            End If
        End If
        If View.Id = "StorageInfromDeliveryNoteInRowsHeader_DetailView" Or View.Id = "DeliveryNoteInFromOrderOutRowsHeader_DetailView" Then ' Előleg bekérő készítő view
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "DeliveryNoteInHeader_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
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        '------------------------- LISTVIEWS --------------------------------------------------------------------------------------------------------------------------------------
        If View.Id = "DeliveryNoteInHeader_DeliveryNoteInRows_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.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
        If View.Id = "StorageInfromDeliveryNoteInRowsHeader_StorageInfromDeliveryNoteInRowsRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
        If View.Id = "DeliveryNoteInFromOrderOutRowsHeader_DeliveryNoteInFromOrderOutRowsRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
        '------------------------- DETAILVIEWS ------------------------------------------------------------------------------------------------------------------------------------
        If View.Id = "DeliveryNoteInHeader_DeatilView" Then
            Frame.GetController(Of DeliveryNoteInVC).aTotable_DeliveryNoteIn.Active.SetItemValue("", False)
            Frame.GetController(Of DeliveryNoteInVC).aTotableD_DeliveryNoteIn.Active.SetItemValue("", False)
        End If
        If View.Id = "StorageInfromDeliveryNoteInRowsHeader_DetailView" Or View.Id = "DeliveryNoteInFromOrderOutRowsHeader_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
    End Sub
    Private Sub AcceptAction_Executing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Validator.RuleSet.ValidateAll(View.ObjectSpace, View.SelectedObjects, DefaultContexts.Save)
    End Sub
    Private Sub aTotable_DeliveryNoteIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTotable_DeliveryNoteIn.Execute
        ' hozzáad egy új sort a fejléchez ------------------------

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = CType(e.SelectedObjects(0), DeliveryNoteInHeader)
        Dim _DeliveryNoteInRows As DeliveryNoteInRows = New DeliveryNoteInRows(_ocur.Session)
        Dim _OrderOutRows As OrderOutRows = _ocur.GetObject(_DeliveryNoteInHeader.row_OrderOutRows)
        Dim ItemToFocus As ViewItem = TryCast(View, DetailView).FindItem("row_Products")
        Try
            If ItemToFocus IsNot Nothing Then
                Dim ItemControl As Object
                ItemControl = ItemToFocus.Control
                ItemControl.Focus()
            End If

            If _OrderOutRows IsNot Nothing Then
                If _DeliveryNoteInHeader.row_QTT + _OrderOutRows.QTT_DeliveryNoteIn > _OrderOutRows.QTT Then
                    Throw New Exception("*Nem volt ennyi megrendelve ebből a tételből !")
                Else
                    _OrderOutRows.QTT_DeliveryNoteIn += _DeliveryNoteInHeader.row_QTT
                End If
            End If

            _DeliveryNoteInRows.AddRows(_DeliveryNoteInHeader)

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            _DeliveryNoteInHeader.row_Products = Nothing
            _DeliveryNoteInHeader.row_QTT = 0
            _DeliveryNoteInHeader.row_ListPriceHUF = 0
            _DeliveryNoteInHeader.row_ListPriceDEV = 0
            _DeliveryNoteInHeader.row_DiscountPercent = 0
            _DeliveryNoteInHeader.row_OrderOutRows = Nothing
            _DeliveryNoteInHeader.row_PacketNumber = ""
            _DeliveryNoteInHeader.row_Weight = 0
            _DeliveryNoteInHeader.row_CountryFrom = Nothing

            View.Refresh()
        End Try
    End Sub
    Private Sub aTotableD_DeliveryNoteIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTotableD_DeliveryNoteIn.Execute
        ' a kiválasztott sorokat törli -----------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = TryCast(e.SelectedObjects(0), DeliveryNoteInHeader)
        Dim _DeliveryNoteInRows_Collection As ArrayList = New ArrayList
        Dim _DeliveryNoteInRows_ListEditor As ListPropertyEditor

        _DeliveryNoteInRows_ListEditor = TryCast(View, DetailView).FindItem("DeliveryNoteInrows")
        For Each _DeliveryNoteInRows As DeliveryNoteInRows In _DeliveryNoteInRows_ListEditor.ListView.SelectedObjects
            Dim _DelivNoteInRows As DeliveryNoteInRows = _DeliveryNoteInRows
            For Each _DeliveryNoteInRows2 As DeliveryNoteInRows In _DeliveryNoteInHeader.DeliveryNoteInrows
                If _DeliveryNoteInRows2.RowIndex > _DelivNoteInRows.RowIndex Then
                    _DeliveryNoteInRows2.RowIndex -= 1
                End If
            Next
        Next

        For Each _DeliveryNoteInRows As DeliveryNoteInRows In _DeliveryNoteInRows_ListEditor.ListView.SelectedObjects
            If _DeliveryNoteInRows.OrderOutRows IsNot Nothing Then
                _DeliveryNoteInRows.OrderOutRows.QTT_DeliveryNoteIn -= _DeliveryNoteInRows.QTT
                _DeliveryNoteInRows.OrderOutRows.Save()
            End If
            _DeliveryNoteInRows_Collection.Add(_DeliveryNoteInRows)
        Next

        _ocur.Delete(_DeliveryNoteInRows_Collection)
        _ocur.CommitChanges()
        View.Refresh()
    End Sub
    Private Sub aUp_DeliveryNoteIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aUp_DeliveryNoteIn.Execute
        ' A kiválaszott sort egy sorral feljebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = TryCast(View.SelectedObjects(0), DeliveryNoteInRows).DeliveryNoteInHeader
        Dim _DeliveryNoteInRowsSelected As DeliveryNoteInRows = Nothing
        Dim _DeliveryNoteInRowsMinus As DeliveryNoteInRows = Nothing
        Dim _DeliveryNoteInRows As DeliveryNoteInRows = Nothing

        Try
            _DeliveryNoteInRowsSelected = TryCast(View.SelectedObjects(0), DeliveryNoteInRows) ' Az editor egy listview, kiveszi az első kijelölt sorát
        Catch
            ' TESZT MIATT!!!! ----------------------------------------------------------------
            Throw New Exception("*Nincs sor kiválasztva, vagy nem a sor kijelölése az aktív!")
        End Try

        If _DeliveryNoteInRowsSelected.RowIndex > 0 Then
            For Each _DeliveryNoteInRows In _DeliveryNoteInHeader.DeliveryNoteInrows
                If _DeliveryNoteInRows.RowIndex = _DeliveryNoteInRowsSelected.RowIndex - 1 Then
                    _DeliveryNoteInRowsMinus = _DeliveryNoteInRows
                    Exit For
                End If
            Next

            If _DeliveryNoteInRowsMinus IsNot Nothing Then
                _DeliveryNoteInRowsSelected.RowIndex -= 1
                _DeliveryNoteInRowsMinus.RowIndex += 1
            End If
            _DeliveryNoteInRowsMinus.Save()
            _DeliveryNoteInRowsSelected.Save()
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aDown_DeliveryNoteIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aDown_DeliveryNoteIn.Execute
        ' A kiválaszott sort egy sorral lejebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = TryCast(View.SelectedObjects(0), DeliveryNoteInRows).DeliveryNoteInHeader
        Dim _DeliveryNoteInRowsSelected As DeliveryNoteInRows
        Dim _DeliveryNoteInRowsPlus As DeliveryNoteInRows = Nothing
        Dim _DeliveryNoteInRows As DeliveryNoteInRows

        Try
            _DeliveryNoteInRowsSelected = TryCast(View.SelectedObjects(0), DeliveryNoteInRows) ' Kiveszi a kijelölt sorok közül az elsőt
        Catch
            ' TESZT MIATT!!!! ----------------------------------------------------------------
            Throw New Exception("*Nincs sor kiválasztva, vagy nem a sor kijelölése az aktív!")
        End Try

        If _DeliveryNoteInRowsSelected.RowIndex < _DeliveryNoteInHeader.DeliveryNoteInrows.Count - 1 Then
            For Each _DeliveryNoteInRows In _DeliveryNoteInHeader.DeliveryNoteInrows
                If _DeliveryNoteInRows.RowIndex = _DeliveryNoteInRowsSelected.RowIndex + 1 Then
                    _DeliveryNoteInRowsPlus = _DeliveryNoteInRows
                    Exit For
                End If
            Next

            If _DeliveryNoteInRowsPlus IsNot Nothing Then
                _DeliveryNoteInRowsSelected.RowIndex += 1
                _DeliveryNoteInRowsPlus.RowIndex -= 1
            End If
            _DeliveryNoteInRowsPlus.Save()
            _DeliveryNoteInRowsSelected.Save()
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aFinalizeDeliveryNoteIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalize_DeliveryNoteIn.Execute
        '---Az akció véglegesít egy bejövõ szállítólevelet------------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = TryCast(View.SelectedObjects(0), DeliveryNoteInHeader)

        If _DeliveryNoteInHeader.DeliveryNoteInrows.Count <= 0 Then
            Throw New Exception("*Nincs mit betárolni !")
        End If

        _DeliveryNoteInHeader.IsEditable = False

        If _DeliveryNoteInHeader.DocumentNumber = "" Then
            _DeliveryNoteInHeader.DocumentNumber = _DeliveryNoteInHeader.DeliveryNoteInType.NumberGenerator.GetNewNumber(_DeliveryNoteInHeader.DeliveryNoteInType.NumberGenerator)
        End If

        _DeliveryNoteInHeader.Save()
        _ocur.CommitChanges()
        View.Close()
    End Sub
    Private Sub aStorno_DeliveryNoteIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aStorno_DeliveryNoteIn.Execute
        '---Az akció sztornóz egy bejövõ szállítólevelet------------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = TryCast(View.SelectedObjects(0), DeliveryNoteInHeader)
        Dim _IsFound As Boolean = False

        Dim _StorageInRows_XPQuery As New XPQuery(Of StorageInRows)(_ocur.Session)
        Dim query = From _SIR In _StorageInRows_XPQuery
                       Where _SIR.DeliveryNoteInRows.DeliveryNoteInHeader Is _DeliveryNoteInHeader
                           Group _SIR By _SIR.DeliveryNoteInRows Into g = Group _
           Select New With {Key .DeliveryNoteInRows = DeliveryNoteInRows,
                            Key .QTT_Sum = g.Sum(Function(inv) inv.QTT)}


        For Each _DeliveryNoteInRows In _DeliveryNoteInHeader.DeliveryNoteInrows
            _DeliveryNoteInRows.OrderOutRows.QTT_DeliveryNoteIn -= _DeliveryNoteInRows.QTT
            _DeliveryNoteInRows.OrderOutRows = Nothing


            For Each item In query
                If item.DeliveryNoteInRows.Oid = _DeliveryNoteInRows.Oid And item.QTT_Sum <> 0 Then ' Ha talál olyan sort aminek azonos a szállítólevél sora az aktuálissal, akkor kiszállunk
                    _IsFound = True
                    Exit For
                End If
            Next
        Next

        If _IsFound = False Then

            _DeliveryNoteInHeader.IsStorno = True

            _DeliveryNoteInHeader.Save()
            _ocur.CommitChanges()
            View.Close()
        Else
            ThrowNewException("Erről a széllítólevélről már készült betárolás.") 'Teszt
        End If
    End Sub
    Private Sub aGenerateStorageIn_From_DeliveryNoteInRows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGenerateStorageIn_From_DeliveryNoteInRows.CustomizePopupWindowParams
        ' Egy kiválaszott végleges szállítólevél azon sorait hozza fel egy listában, ahol a mennyiség még több mint a már erről a betárolt össz mennyiség
        Dim _NOStorageInRows As New ArrayList
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
            Dim _DeliveryNoteInRows As DeliveryNoteInRows
            Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), DeliveryNoteInHeader))
            Dim _StorageInfromDeliveryNoteInRowsHeader As New StorageInfromDeliveryNoteInRowsHeader(_onew.Session)
            Dim _QTT_Sum As Double = 0


            _StorageInfromDeliveryNoteInRowsHeader.DeliveryNoteInHeader = _DeliveryNoteInHeader
            _StorageInfromDeliveryNoteInRowsHeader.DateCreation = GetSQLTime(_onew.Session)

            ' Az összes olyan betárolási sor keresése adott termékre, ami szállítólevéllről jött
            Dim _StorageInRows_XPQuery As New XPQuery(Of StorageInRows)(_onew.Session)
            Dim query = From _SIR In _StorageInRows_XPQuery
                            Where _SIR.DeliveryNoteInRows.DeliveryNoteInHeader Is _DeliveryNoteInHeader
                                Group _SIR By _SIR.DeliveryNoteInRows Into g = Group _
                Select New With {Key .DeliveryNoteInRows = DeliveryNoteInRows,
                                 Key .QTT_Sum = g.Sum(Function(inv) inv.QTT)}

            For Each _DeliveryNoteInRows In _DeliveryNoteInHeader.DeliveryNoteInrows
                _QTT_Sum = 0
                For Each item In query
                    If item.DeliveryNoteInRows.Oid = _DeliveryNoteInRows.Oid Then ' Ha talál olyan sort aminek azonos a szállítólevél sora az aktuálissal, akkor számolja hogy összesen mennyi van betárolva
                        _QTT_Sum = item.QTT_Sum
                    End If
                Next
                If _DeliveryNoteInRows.QTT > _QTT_Sum Then ' Csak azokat a sorokat hozza fel a ListView-ba amikre még lehet betárolást csinálni
                    Dim _StorageInfromDeliveryNoteInRowsRows As New StorageInfromDeliveryNoteInRowsRows(_onew.Session)

                    _StorageInfromDeliveryNoteInRowsRows.DeliveryNoteInRows = _DeliveryNoteInRows
                    _StorageInfromDeliveryNoteInRowsRows.StorageInfromDeliveryNoteInRowsHeader = _StorageInfromDeliveryNoteInRowsHeader
                    _StorageInfromDeliveryNoteInRowsRows.QTT_Process = _DeliveryNoteInRows.QTT - _QTT_Sum
                    _StorageInfromDeliveryNoteInRowsRows.QTT_Max = _StorageInfromDeliveryNoteInRowsRows.QTT_Process
                    If _StorageInfromDeliveryNoteInRowsRows.DeliveryNoteInRows.Products.Units.ShortName = "zsák" And _StorageInfromDeliveryNoteInRowsRows.DeliveryNoteInRows.Products.ProductGroups.Parent.Name = "VETŐ" Then
                        If _DeliveryNoteInRows.Weight = 0 Or String.IsNullOrEmpty(_DeliveryNoteInRows.PacketNumber) Then
                            _NOStorageInRows.Add(_DeliveryNoteInRows.Products)
                        End If
                    End If
                End If
            Next

            _onew.CommitChanges()

            e.View = Application.CreateDetailView(_onew, _StorageInfromDeliveryNoteInRowsHeader, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            If _NOStorageInRows.Count > 0 Then
                Dim _ErrorMessage As String = "A következő szállítólevélről az alábbi sorok nem tárolhatóak be :" + vbNewLine

                For Each _NOSIR As Products In _NOStorageInRows
                    _ErrorMessage += vbNewLine + _NOSIR.ProductNumber + "    /    " + _NOSIR.ShortName + vbNewLine
                Next

                _ErrorMessage += vbNewLine + "mert NINCS megadva a KÖTEGSZÁM vagy a SÚLY!"
                'MsgBox(_ErrorMessage, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
                Throw New UserFriendlyException(_ErrorMessage)
                View.Close()
            End If
        End Try
    End Sub
    Private Sub aGenerateStorageIn_From_DeliveryNoteInRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGenerateStorageIn_From_DeliveryNoteInRows.Execute
        ' A popup-on kiválasztott bej. szállítólevél sorokból a kézzel megadott szándékkal végleges bet. bizonylatot hoz létre (Sorokkal)

        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
        Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))
        Try
            Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), DeliveryNoteInHeader))

            If Not LockingModule.LockObject(_uow_lock, GetType(StorageComputed), "-", _User) Then
                Dim _LockingObject As LockingObject = _onew.FindObject(Of LockingObject)(CriteriaOperator.Parse("TableName=?", GetType(StorageComputed).Name + "-" + "-"))

                Dim _Prompt As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\StorageComputed", "CanNotStorageInByLocking")
                Dim _Title As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxError")
                MsgBox(String.Format(_Prompt, _LockingObject.User.FullName, _LockingObject.ObjectCreated), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, _Title)

                Exit Sub
            Else
                Dim _StorageInfromDeliveryNoteInRowsHeader As StorageInfromDeliveryNoteInRowsHeader = _onew.GetObjectByKey(Of StorageInfromDeliveryNoteInRowsHeader)(TryCast(e.PopupWindow.View.SelectedObjects(0), StorageInfromDeliveryNoteInRowsHeader).Oid)
                _StorageInfromDeliveryNoteInRowsHeader.Storage = _onew.GetObjectByKey(Of Storage)(TryCast(e.PopupWindow.View.SelectedObjects(0), StorageInfromDeliveryNoteInRowsHeader).Storage.Oid)
                Dim _StorageInfromDeliveryNoteInRowsRows_ListEditor As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("StorageInfromDeliveryNoteInRowsRows") ' Bekeresi az összes bejövõ megrendelési sort
                Dim _StorageInfromDeliveryNoteInRowsRows As StorageInfromDeliveryNoteInRowsRows
                Dim _MSGBoxNeed As Boolean = False

                For Each _StorageInfromDeliveryNoteInRowsRows In _StorageInfromDeliveryNoteInRowsRows_ListEditor.ListView.SelectedObjects
                    If _StorageInfromDeliveryNoteInRowsRows.QTT_Process > _StorageInfromDeliveryNoteInRowsRows.QTT_Max Then
                        _StorageInfromDeliveryNoteInRowsRows.QTT_Process = _StorageInfromDeliveryNoteInRowsRows.QTT_Max
                        _MSGBoxNeed = True
                        Exit For
                    End If
                Next

                If _MSGBoxNeed = True Then Throw New Exception("*Volt olyan szándék megadva ami több mint amit be lehet tárolni, így az értéke visszaállítódott a maximális értékre!")

                ' --- Létrehozzuk a bet. fejlécet ----
                Dim _StorageInHeader_New As New StorageInHeader(_onew.Session)
                With _StorageInHeader_New
                    .IsEditable = False
                    .Storage = _onew.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), StorageInfromDeliveryNoteInRowsHeader).Storage)
                    .ObjectCreated = GetSQLTime(_onew.Session)
                    .StorageMoveType = _onew.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), StorageInfromDeliveryNoteInRowsHeader).StorageMoveType)
                    .DocumentNumber = _StorageInHeader_New.Storage.NumberGeneratorIn.GetNewNumber(_StorageInHeader_New.Storage.NumberGeneratorIn)
                End With

                ' Létrehozzuk a bet. sorokat, minden kiválasztott bejövő szállítólevél sorokhoz ----------------------------------------------------------------------------------------------
                For i = 0 To _StorageInfromDeliveryNoteInRowsRows_ListEditor.ListView.SelectedObjects.Count - 1 ' A kiválasztott sorokon végigmegyünk és létrehozzuk a bet. sorokat.
                    Dim _StorageComputed As StorageComputed = _onew.Session.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _
                                _onew.GetObject(_StorageInfromDeliveryNoteInRowsRows_ListEditor.ListView.SelectedObjects(i).DeliveryNoteInRows.Products), _onew.GetObject(_StorageInfromDeliveryNoteInRowsHeader.Storage)))
                    Dim _QTT_Process As Double = 0
                    _QTT_Process = TryCast(_StorageInfromDeliveryNoteInRowsRows_ListEditor.ListView.SelectedObjects(i), StorageInfromDeliveryNoteInRowsRows).QTT_Process ' Kinyerjük az adott sorhoz tartozó szándékot

                    If _StorageComputed Is Nothing Then
                        _StorageComputed = _onew.CreateObject(Of StorageComputed)()
                        With _StorageComputed
                            .Storage = _StorageInfromDeliveryNoteInRowsHeader.Storage
                            .Products = _onew.GetObject(_StorageInfromDeliveryNoteInRowsRows_ListEditor.ListView.SelectedObjects(i).DeliveryNoteInRows.Products)
                        End With
                    End If

                    ' Új betárolási sorok létrehozása --------------------------------------------
                    Dim _StorageInRows_New As StorageInRows = _onew.CreateObject(Of StorageInRows)()
                    With _StorageInRows_New
                        .DeliveryNoteInRows = _onew.GetObject(_StorageInfromDeliveryNoteInRowsRows_ListEditor.ListView.SelectedObjects(i).DeliveryNoteInRows)
                        .StorageInHeader = _StorageInHeader_New
                        .Products = _onew.GetObject(_StorageInfromDeliveryNoteInRowsRows_ListEditor.ListView.SelectedObjects(i).DeliveryNoteInRows.Products)
                        .QTT_Will = _QTT_Process
                        .RowIndex = i + 1 ' Sorindexelés
                    End With
                    _StorageComputed.QTTReservedIn += _QTT_Process
                Next
                '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

                For Each _StorageInRows In _StorageInHeader_New.StorageInRows
                    Dim _StorageComputed As StorageComputed = _onew.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _StorageInRows.Products, _StorageInHeader_New.Storage))
                    Dim _StorageMoveEvents As New StorageMoveEvents(_onew.Session)
                    If _StorageInRows.QTT_Will > 0 Then
                        _StorageInRows.QTT = _StorageInRows.QTT_Will
                        _StorageComputed.QTTFree += _StorageInRows.QTT_Will
                        _StorageComputed.QTTReservedIn -= _StorageInRows.QTT_Will

                        _StorageMoveEvents.StorageInRows = _StorageInRows
                        _StorageMoveEvents.QTT = _StorageInRows.QTT
                        _StorageMoveEvents.QTT_Free_After = _StorageComputed.QTTFree
                        _StorageMoveEvents.QTT_Free_Before = _StorageMoveEvents.QTT_Free_After - _StorageMoveEvents.QTT
                        _StorageMoveEvents.MoveEventsType = eMoveEventsType.StorageInFinal
                        _StorageMoveEvents.ExecutionDate = _StorageInRows.StorageInHeader.CreateDate

                        _StorageInRows.QTT_Will = 0
                    End If
                Next

                _StorageInHeader_New.IsEditable = False
                _StorageInHeader_New.IsFinalized = True
                _DeliveryNoteInHeader.IsEditable = False
                _onew.CommitChanges()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            LockingModule.UnLockObject(_uow_lock, GetType(StorageComputed), "-", _User)
        End Try
    End Sub
    Private Sub aFinalize_And_Storage_DeliveryNoteIn_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aFinalize_And_Storage_DeliveryNoteIn.CustomizePopupWindowParams
        '---Létrehoz egy ListView-t ahol a raktárat kell kiválasztani a rendelésből kitárolási akcióhoz
        Dim _NOStorageInRows As New ArrayList
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _DeliveryNoteInToStorageInHeader As DeliveryNoteInToStorageInHeader = _onew.CreateObject(Of DeliveryNoteInToStorageInHeader)()

            Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), DeliveryNoteInHeader))
            For Each _DeliveryNoteInRows As DeliveryNoteInRows In _DeliveryNoteInHeader.DeliveryNoteInrows
                If _DeliveryNoteInRows.Products.Units.ShortName = "zsák" And _DeliveryNoteInRows.Products.ProductGroups.Parent.Name = "VETŐ" Then
                    If _DeliveryNoteInRows.Weight = 0 Or String.IsNullOrEmpty(_DeliveryNoteInRows.PacketNumber) Then
                        _NOStorageInRows.Add(_DeliveryNoteInRows.Products)
                    End If
                End If
            Next


            e.View = Application.CreateDetailView(_onew, "DeliveryNoteInToStorageInHeader_DetailView", False, _DeliveryNoteInToStorageInHeader)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            If _NOStorageInRows.Count > 0 Then
                Dim _ErrorMessage As String = "A következő szállítólevélről az alábbi sor(ok) nem tárolhatóak be :" + vbNewLine

                For Each _NOSIR As Products In _NOStorageInRows
                    _ErrorMessage += vbNewLine + _NOSIR.ProductNumber + "    /    " + _NOSIR.ShortName + vbNewLine
                Next

                _ErrorMessage += vbNewLine + "mert NINCS megadva a KÖTEGSZÁM vagy a SÚLY!"
                'MsgBox(_ErrorMessage, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
                Throw New UserFriendlyException(_ErrorMessage)
                View.Close()
            End If
        End Try
    End Sub
    Private Sub aFinalize_And_Storage_DeliveryNoteIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aFinalize_And_Storage_DeliveryNoteIn.Execute
        '---Az akció véglegesít egy bejövő szállítólevelet------------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
        Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))
        Dim _DeliveryNoteInHeader As DeliveryNoteInHeader = TryCast(View.SelectedObjects(0), DeliveryNoteInHeader)
        Dim _StorageInHeader As StorageInHeader
        Try
            Dim _DeliveryNoteInRows As DeliveryNoteInRows
            Dim _DeliveryNoteInToStorageInHeader As DeliveryNoteInToStorageInHeader = TryCast(e.PopupWindow.View.SelectedObjects(0), DeliveryNoteInToStorageInHeader)

            Dim _StorageInRows As StorageInRows

            If _DeliveryNoteInHeader.DeliveryNoteInrows.Count <= 0 Then Throw New Exception("*A szállítólevélnek nincs egyetlen sora sem!")
            Dim _DeliveryNoteInRows_Collection As New ArrayList
            For Each _DeliveryNoteInRows In _DeliveryNoteInHeader.DeliveryNoteInrows
                If _DeliveryNoteInRows.OrderOutRows IsNot Nothing Then
                    If _DeliveryNoteInRows.QTT > 0 And _DeliveryNoteInRows.QTT <= _DeliveryNoteInRows.OrderOutRows.QTT Then
                        _DeliveryNoteInRows_Collection.Add(_DeliveryNoteInRows)
                    End If
                Else
                    _DeliveryNoteInRows_Collection.Add(_DeliveryNoteInRows)
                End If
            Next

            If _DeliveryNoteInRows_Collection.Count = 0 Then Throw New Exception("*A szállítólevélnek nincs egyetlen olyan sora sem, melyet be lehetne tárolni!")

            'zárolás
            If Not LockingModule.LockObject(_uow_lock, GetType(StorageComputed), "-", _User) Then
                Dim _LockingObject As LockingObject = _onew.FindObject(Of LockingObject)(CriteriaOperator.Parse("TableName=?", GetType(StorageComputed).Name + "-" + "-"))

                Dim _Prompt As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\StorageComputed", "CanNotStorageInByLocking")
                Dim _Title As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxError")
                MsgBox(String.Format(_Prompt, _LockingObject.User.FullName, _LockingObject.ObjectCreated), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, _Title)

                Exit Sub
            Else

                _StorageInHeader = _ocur.CreateObject(Of StorageInHeader)()
                _StorageInHeader.Active = True
                _StorageInHeader.CreateDate = GetSQLTime(_ocur.Session)
                _StorageInHeader.Storage = _ocur.GetObjectByKey(Of Storage)(_DeliveryNoteInToStorageInHeader.Storage.Oid)
                _StorageInHeader.StorageMoveType = _ocur.GetObjectByKey(Of StorageMoveType)(_DeliveryNoteInToStorageInHeader.StorageMoveType.Oid)

                For Each _DeliveryNoteInRows In _DeliveryNoteInRows_Collection
                    _StorageInRows = _ocur.CreateObject(Of StorageInRows)()
                    With _StorageInRows
                        .Active = True
                        .DeliveryNoteInRows = _DeliveryNoteInRows
                        .PacketNumber = _DeliveryNoteInRows.PacketNumber
                        .Products = _DeliveryNoteInRows.Products
                        .QTT_Will = _DeliveryNoteInRows.QTT
                        .RowIndex = _StorageInHeader.StorageInRows.Count + 1
                        .SerialNumber = ""
                        .StorageInHeader = _StorageInHeader
                        .Storage = _StorageInHeader.Storage
                        .Weight = _DeliveryNoteInRows.Weight
                        .WeightMode = _DeliveryNoteInRows.WeightMode

                    End With

                    Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _
                                                                                                        _StorageInRows.Products, _StorageInHeader.Storage))
                    If _StorageComputed Is Nothing Then
                        _StorageComputed = _ocur.CreateObject(Of StorageComputed)()
                        With _StorageComputed
                            .Storage = _StorageInHeader.Storage
                            .Products = _DeliveryNoteInRows.Products
                            .QTTReservedIn = _DeliveryNoteInRows.QTT
                        End With
                    Else
                        _StorageComputed.QTTReservedIn += _DeliveryNoteInRows.QTT
                    End If
                Next

                '--------- Véglegesítjük a betárolást !
                For Each _StorageInRows In _StorageInHeader.StorageInRows
                    Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _StorageInRows.Products, _StorageInHeader.Storage))
                    Dim _StorageMoveEvents As New StorageMoveEvents(_ocur.Session)
                    If _StorageInRows.QTT_Will > 0 Then
                        _StorageInRows.QTT = _StorageInRows.QTT_Will
                        _StorageComputed.QTTFree += _StorageInRows.QTT_Will
                        _StorageComputed.QTTReservedIn -= _StorageInRows.QTT_Will

                        _StorageMoveEvents.StorageInRows = _StorageInRows
                        _StorageMoveEvents.QTT = _StorageInRows.QTT
                        _StorageMoveEvents.QTT_Free_After = _StorageComputed.QTTFree
                        _StorageMoveEvents.QTT_Free_Before = _StorageMoveEvents.QTT_Free_After - _StorageMoveEvents.QTT
                        _StorageMoveEvents.MoveEventsType = eMoveEventsType.StorageInFinal
                        _StorageMoveEvents.ExecutionDate = _StorageInRows.StorageInHeader.CreateDate


                        _StorageInRows.QTT_Will = 0
                    End If
                Next
                _StorageInHeader.IsEditable = False
                _StorageInHeader.IsFinalized = True
                _DeliveryNoteInHeader.IsEditable = False
                _ocur.CommitChanges()

                If _DeliveryNoteInToStorageInHeader.IsRegistry Then
                    Dim _RegistryHeader As New RegistryHeader(_ocur.Session)
                    With _RegistryHeader
                        .CustomersFrom = _DeliveryNoteInHeader.CustomersFrom
                        .Customers = _DeliveryNoteInHeader.Customers
                        .RegistryType = _DeliveryNoteInHeader.DeliveryNoteInType.RegistryType



                    End With

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

                    _ShowViewParameters.CreatedView = Application.CreateDetailView(_orenew, "RegistryHeader_DetailView", True, _orenew.GetObject(_RegistryHeader))
                    _ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild
                    _ShowViewParameters.TargetWindow = TargetWindow.NewWindow
                    Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)

                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_orenew, _RegistryHeader)
                End If

                Dim _ReportData As ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _StorageInHeader.Storage.ReportIn.Oid), True)
                If _ReportData Is Nothing Or _StorageInHeader Is Nothing Then
                Else
                    Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("StorageInHeader.DocumentNumber =?", _StorageInHeader.DocumentNumber), True)
                End If
                View.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            LockingModule.ReleaseAllLocking(_uow_lock, _User)
        End Try
    End Sub

    Private Sub aRecalculateDeliveryNoteInRowsSumPrice_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aRecalculateDeliveryNoteInRowsSumPrice.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInRRFC As DeliveryNoteInRRFC
        Dim _StorageComputed As StorageComputed

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _DeliveryNoteInRRFC In View.SelectedObjects
            RaiseEvent DoWork()
            If _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.F_CurrencyName.ShortName = "HUF" Then
                If _DeliveryNoteInRRFC.DeliveryNoteInRows.QTT <> 0 Then
                    _DeliveryNoteInRRFC.DeliveryNoteInRows.ListPriceHUF = Math.Round(_DeliveryNoteInRRFC.RegistryRowsFinancialControlling.Amount / _DeliveryNoteInRRFC.DeliveryNoteInRows.QTT, 2, MidpointRounding.AwayFromZero)
                    _DeliveryNoteInRRFC.DeliveryNoteInRows.ListPriceDEV = 0
                    _DeliveryNoteInRRFC.DeliveryNoteInRows.DiscountPriceDEV = 0
                    _DeliveryNoteInRRFC.DeliveryNoteInRows.DiscountPriceHUF = 0
                End If
            Else
                If _DeliveryNoteInRRFC.DeliveryNoteInRows.QTT <> 0 Then
                    If _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.F_CurrencyRate <> 0 Then
                        _DeliveryNoteInRRFC.DeliveryNoteInRows.ListPriceHUF = Math.Round(_DeliveryNoteInRRFC.RegistryRowsFinancialControlling.Amount / _DeliveryNoteInRRFC.DeliveryNoteInRows.QTT, 2, MidpointRounding.AwayFromZero)
                        _DeliveryNoteInRRFC.DeliveryNoteInRows.ListPriceDEV = Math.Round(_DeliveryNoteInRRFC.DeliveryNoteInRows.ListPriceHUF / _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.F_CurrencyRate, 2, MidpointRounding.AwayFromZero)
                        _DeliveryNoteInRRFC.DeliveryNoteInRows.DiscountPriceDEV = 0
                        _DeliveryNoteInRRFC.DeliveryNoteInRows.DiscountPriceHUF = 0
                    End If
                End If
            End If

            Dim _StorageComputed_Collection As New XPCollection(Of StorageComputed)(_ocur.Session, CriteriaOperator.Parse("Products=?", _DeliveryNoteInRRFC.DeliveryNoteInRows.Products))
            For Each _StorageComputed In _StorageComputed_Collection
                _StorageComputed.RecalculateAveragePrice()
                _StorageComputed.RecalculateMinimumPrice()
            Next

        Next
        _ocur.CommitChanges()
        RaiseEvent FinalWork
    End Sub

End Class
