Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Xpo.Session
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Persistent.Validation
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Utils

Public Class StorageTransactionsOutVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of StorageTransactionsOutVC)().aFinalizeAndNew_StorageOut.Active.SetItemValue("", False) ' -- Kitárolási biz. véglegesítése és új nyitása
        Frame.GetController(Of StorageTransactionsOutVC)().aStorageOutStorno.Active.SetItemValue("", False) ' -- Kitárolási biz. stornírozása
        Frame.GetController(Of StorageTransactionsOutVC)().aToTable_StorageOut.Active.SetItemValue("", False) ' -- Kitárolási biz. sor hozzáadása
        Frame.GetController(Of StorageTransactionsOutVC)().aBackToEdit_StorageOut.Active.SetItemValue("", False) ' -- Véglegesített Kit. biz. szerkeszthetőre állítása
        Frame.GetController(Of StorageTransactionsOutVC)().aToTableD_StorageOut.Active.SetItemValue("", False) ' -- Sor törlése kitárolási biz ról
        Frame.GetController(Of StorageTransactionsOutVC)().aCreateDeliveryNote.Active.SetItemValue("", False) ' -- Szállítólevél készítése a kiválasztott kit sorokból
        Frame.GetController(Of StorageTransactionsOutVC)().aDown_StorageOut.Active.SetItemValue("", False) ' -- Kitárolási sor lejebb
        Frame.GetController(Of StorageTransactionsOutVC)().aUp_StorageOut.Active.SetItemValue("", False) ' -- Kitárolási sor feljebb
        Frame.GetController(Of StorageTransactionsOutVC).aBackToStorageOutFromOrderIn.Active.SetItemValue("", False)
        Frame.GetController(Of StorageTransactionsOutVC)().aStorageOutFromPdaCollection.Active.SetItemValue("", False)
        Frame.GetController(Of StorageTransactionsOutVC).aCreatePDACollectionFromStorageOut.Active.SetItemValue("", False)
        '------------------------------------------ LISTVIEWS --------------------------------------------------------------------------------------------------------
        'If View.Id = "StorageOutHeader_ListView" Or View.Id = "StorageOutHeader_ListView_Editable" Then
        '    AddHandler View.SelectionChanged, AddressOf SelectionChanged
        'End If
        If View.Id = "StorageOutHeader_ListView" Then
            Frame.GetController(Of StorageTransactionsOutVC)().aBackToEdit_StorageOut.Active.SetItemValue("", True)
            Frame.GetController(Of StorageTransactionsOutVC)().aStorageOutStorno.Active.SetItemValue("", True)
        End If

        If View.Id = "StorageOutHeader_StorageOutRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of StorageTransactionsOutVC)().aUp_StorageOut.Active.SetItemValue("", True)
            Frame.GetController(Of StorageTransactionsOutVC)().aDown_StorageOut.Active.SetItemValue("", True)
            Frame.GetController(Of StorageTransactionsOutVC).aCreatePDACollectionFromStorageOut.Active.SetItemValue("", True)
        End If
        If View.Id = "StorageOutHeader_ListView_Editable" Then
            Frame.GetController(Of StorageTransactionsOutPrint)().aStorageTransactionsOutPrint.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "StorageOutHeader_ListView" Then
            Frame.GetController(Of StorageTransactionsOutPrint)().aStorageTransactionsOutPrint.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "StorageOutRows_ListView_Order" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of StorageTransactionsOutVC).aBackToStorageOutFromOrderIn.Active.SetItemValue("", True)
        End If
        If View.Id = "StorageOutRows_ListView_DeliveryNote" Then
            Frame.GetController(Of StorageTransactionsOutVC)().aCreateDeliveryNote.Active.SetItemValue("", True)
        End If
        If View.Id = "StorageOutRowsFromPDACollectionPopup_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        '------------------------------------------ DETAILVIEWS ------------------------------------------------------------------------------------------------------
        If View.Id = "StorageOutHeader_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)

            Dim _StorageOutHeader As StorageOutHeader = TryCast(View.SelectedObjects(0), StorageOutHeader)
            If _StorageOutHeader.IsEditable = False And _StorageOutHeader.IsStorno = False Then
                Frame.GetController(Of StorageTransactionsOutVC)().aBackToEdit_StorageOut.Active.SetItemValue("", True)
            End If
        End If

        If View.Id = "StorageOutHeader_DetailView" Then
            Dim _StorageOutHeader As StorageOutHeader = TryCast(View.SelectedObjects(0), StorageOutHeader)
            Dim _IsFromOrderIn As Boolean = False

            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController)().Active.SetItemValue("", True)

            If _StorageOutHeader IsNot Nothing Then
                For Each _StorageOutRows As StorageOutRows In _StorageOutHeader.StorageOutRows ' Végigmegy a kit biz sorain és ha talál olyan sort ahol van hozzá megrendelési sor akkor nem engedi stornózni
                    If _StorageOutRows.OrderInRows IsNot Nothing Then
                        _IsFromOrderIn = True
                    End If
                Next
                If (_StorageOutHeader.IsStorno = True Or _StorageOutHeader.IsEditable = True) Or _IsFromOrderIn = True Then
                    Frame.GetController(Of StorageTransactionsOutVC)().aStorageOutStorno.Active.SetItemValue("", False)
                End If
                If _StorageOutHeader.IsStorno = False And _StorageOutHeader.IsEditable = False And _IsFromOrderIn = False Then
                    If _IsFromOrderIn = False Then
                        Frame.GetController(Of StorageTransactionsOutVC)().aStorageOutStorno.Active.SetItemValue("", True)
                    End If
                End If
                If _StorageOutHeader.IsEditable = False Then
                    Frame.GetController(Of StorageTransactionsOutVC)().aFinalize_StorageOut.Active.SetItemValue("", False)
                    Frame.GetController(Of StorageTransactionsOutVC)().aFinalizeAndNew_StorageOut.Active.SetItemValue("", False)
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController)().Active.SetItemValue("", False)
                Else
                    Frame.GetController(Of StorageTransactionsOutVC)().aFinalizeAndNew_StorageOut.Active.SetItemValue("", True)
                    Frame.GetController(Of StorageTransactionsOutVC)().aFinalize_StorageOut.Active.SetItemValue("", True)
                    Frame.GetController(Of StorageTransactionsOutVC)().aToTable_StorageOut.Active.SetItemValue("", True)
                    Frame.GetController(Of StorageTransactionsOutVC)().aToTableD_StorageOut.Active.SetItemValue("", True)
                    Frame.GetController(Of StorageTransactionsOutVC)().aStorageOutFromPdaCollection.Active.SetItemValue("", True)
                    Frame.GetController(Of StorageTransactionsOutVC)().aCreatePDACollectionFromStorageOut.Active.SetItemValue("", True)
                End If
            End If


        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        '------------------------------------------ LISTVIEWS --------------------------------------------------------------------------------------------------------
        If View.Id = "StorageOutHeader_StorageOutRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            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 = "StorageOutHeader_ListView" Then
            Frame.GetController(Of StorageTransactionsOutVC)().aBackToEdit_StorageOut.Active.SetItemValue("", False)
            Frame.GetController(Of StorageTransactionsOutVC)().aStorageOutStorno.Active.SetItemValue("", False)
        End If

        If View.Id = "StorageOutHeader_StorageOutRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of StorageTransactionsOutVC)().aUp_StorageOut.Active.SetItemValue("", False)
            Frame.GetController(Of StorageTransactionsOutVC)().aDown_StorageOut.Active.SetItemValue("", False)
        End If
        If View.Id = "StorageOutHeader_ListView_Editable" Then
            Frame.GetController(Of StorageTransactionsOutPrint)().aStorageTransactionsOutPrint.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "StorageOutHeader_ListView" Then
            Frame.GetController(Of StorageTransactionsOutPrint)().aStorageTransactionsOutPrint.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            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 = "StorageOutRows_ListView_Order" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of StorageTransactionsOutVC).aBackToStorageOutFromOrderIn.Active.SetItemValue("", False)
        End If
        If View.Id = "StorageOutRows_ListView_DeliveryNote" Then
            Frame.GetController(Of StorageTransactionsOutVC)().aCreateDeliveryNote.Active.SetItemValue("", False)
        End If
        If View.Id = "StorageOutRowsFromPDACollectionPopup_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        '------------------------------------------ DETAILVIEWS ------------------------------------------------------------------------------------------------------
        If View.Id = "StorageOutHeader_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)

            Dim _StorageOutHeader As StorageOutHeader = TryCast(View.SelectedObjects(0), StorageOutHeader)
            If _StorageOutHeader.IsEditable = False And _StorageOutHeader.IsStorno = False Then
                Frame.GetController(Of StorageTransactionsOutVC)().aBackToEdit_StorageOut.Active.SetItemValue("", False)
            End If
        End If

        If View.Id = "StorageOutHeader_DetailView" Then
            Dim _StorageOutHeader As StorageOutHeader = TryCast(View.SelectedObjects(0), StorageOutHeader)
            Dim _IsFromOrderIn As Boolean = False

            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController)().Active.SetItemValue("", False)

            If _StorageOutHeader IsNot Nothing Then
                For Each _StorageOutRows As StorageOutRows In _StorageOutHeader.StorageOutRows ' Végigmegy a kit biz sorain és ha talál olyan sort ahol van hozzá megrendelési sor akkor nem engedi stornózni
                    If _StorageOutRows.OrderInRows IsNot Nothing Then
                        _IsFromOrderIn = True
                    End If
                Next
                If (_StorageOutHeader.IsStorno = True Or _StorageOutHeader.IsEditable = True) Or _IsFromOrderIn = True Then
                    Frame.GetController(Of StorageTransactionsOutVC)().aStorageOutStorno.Active.SetItemValue("", True)
                End If
                If _StorageOutHeader.IsStorno = False And _StorageOutHeader.IsEditable = False And _IsFromOrderIn = False Then
                    If _IsFromOrderIn = False Then
                        Frame.GetController(Of StorageTransactionsOutVC)().aStorageOutStorno.Active.SetItemValue("", False)
                    End If
                End If
                If _StorageOutHeader.IsEditable = False Then
                    Frame.GetController(Of StorageTransactionsOutVC)().aFinalize_StorageOut.Active.SetItemValue("", False)
                    Frame.GetController(Of StorageTransactionsOutVC)().aFinalizeAndNew_StorageOut.Active.SetItemValue("", False)
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController)().Active.SetItemValue("", False)
                Else
                    Frame.GetController(Of StorageTransactionsOutVC)().aFinalizeAndNew_StorageOut.Active.SetItemValue("", False)
                    Frame.GetController(Of StorageTransactionsOutVC)().aFinalize_StorageOut.Active.SetItemValue("", False)
                    Frame.GetController(Of StorageTransactionsOutVC)().aToTable_StorageOut.Active.SetItemValue("", False)
                    Frame.GetController(Of StorageTransactionsOutVC)().aToTableD_StorageOut.Active.SetItemValue("", False)
                    Frame.GetController(Of StorageTransactionsOutVC)().aCreatePDACollectionFromStorageOut.Active.SetItemValue("", False)
                End If
            End If
        End If
    End Sub
    Private Sub aStorageOutStorno_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aStorageOutStorno.Execute
        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 _StorageOutHeader As StorageOutHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), StorageOutHeader))
            If _StorageOutHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _StorageOutHeader.IsStorno = True Then Throw New Exception(String.Format("A {0} bizonylat már stornózva lett!", _StorageOutHeader.DocumentNumber) _
                                                                        + vbNewLine + "Kérem, ellenőrizze!")

            Dim _StorageOutRowsDelivered As StorageOutRows = Nothing
            For Each _StorageOutRows As StorageOutRows In _StorageOutHeader.StorageOutRows
                If _StorageOutRows.QTT_To_DeliveryOut > 0 Then
                    _StorageOutRowsDelivered = _StorageOutRows
                    Exit For
                End If
            Next
            If _StorageOutRowsDelivered IsNot Nothing Then Throw New Exception(String.Format("A {0} termékből a {1} sorszámú kitárolási soron {2}{3} kiszállításar került!", _
                            _StorageOutRowsDelivered.Products.ShortName, _StorageOutRowsDelivered.RowIndex, _StorageOutRowsDelivered.QTT_To_DeliveryOut, _StorageOutRowsDelivered.Products.Units.ShortName) _
                                  + vbNewLine + "Kérem, ellenőrizze!")

            '--==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
                Dim _StorageOutHeaderNew As New StorageOutHeader(_onew.Session)
                With _StorageOutHeaderNew
                    .ConnectInfo = _StorageOutHeader.DocumentNumber
                    .Storage = _StorageOutHeader.Storage
                    .StorageMoveType = _StorageOutHeader.StorageMoveType
                    .CustomersFrom = _StorageOutHeader.CustomersFrom
                    .Customers = _StorageOutHeader.Customers
                    .Active = False
                    .IsStorno = True
                    .IsEditable = False
                End With
                _StorageOutHeader = _onew.GetObject(_StorageOutHeader)
                _StorageOutHeader.IsEditable = False
                _StorageOutHeader.IsStorno = True
                _StorageOutHeader.Active = False

                For Each _StorageOutRows As StorageOutRows In _StorageOutHeader.StorageOutRows
                    Dim _StorageOutRowsNew As New StorageOutRows(_onew.Session)
                    With _StorageOutRowsNew
                        .RowIndex = _StorageOutRows.RowIndex
                        .StorageOutHeader = _StorageOutHeaderNew
                        .StorageComputed = _StorageOutRows.StorageComputed
                        .OrderInRows = _StorageOutRows.OrderInRows
                        .QTT = _StorageOutRows.QTT * (-1)
                        .QTT_Will = 0
                        .QTT_To_DeliveryOut = 0
                        .Products = _StorageOutRows.Products
                        .OnStorno = True
                    End With
                    '----Vissza raktárba + mozgástábla
                    '____________________________________________________
                    Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                     CriteriaOperator.Parse("StorageOutRows=?", _StorageOutRows))

                    For Each _StorageOutRowsInRows As StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                        Dim _StorageComputed As StorageComputed = _onew.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? AND Products=?", _StorageOutHeader.Storage, _StorageOutRows.Products))
                        Dim _StorageMoveEvents_Collection As New XPCollection(Of StorageMoveEvents)(_onew.Session, CriteriaOperator.Parse("StorageInRows=? AND StorageOutRows=?", _
                                                                                                    _StorageOutRowsInRows.StorageInRows, _StorageOutRows))
                        _StorageMoveEvents_Collection.Sorting.Add(New SortProperty("ObjectCreated", DB.SortingDirection.Descending))

                        _StorageComputed.QTTFree += _StorageOutRowsInRows.QTT

                        Dim _StorageOutRowsInRowsNew As New StorageOutRowsInRows(_onew.Session)
                        With _StorageOutRowsInRowsNew
                            .StorageInRows = _StorageOutRowsInRows.StorageInRows
                            .StorageOutRows = _StorageOutRowsNew
                            .QTT = _StorageOutRowsInRows.QTT * (-1)
                            .QTT_Will = 0
                            .QTT_To_DeliveryOut = 0
                            .StorageInRows.QTT_Out -= _StorageOutRowsInRows.QTT
                        End With


                        If _StorageOutRowsInRows.StorageOutRows.OrderInRows IsNot Nothing Then
                            _StorageOutRowsInRows.StorageOutRows.OrderInRows.QTT_Storaged -= _StorageOutRowsInRows.QTT
                        End If

                        Dim _StorageMoveEvents As New StorageMoveEvents(_onew.Session)
                        _StorageMoveEvents.QTT = _StorageOutRowsInRows.QTT
                        _StorageMoveEvents.StorageOutRows = _StorageOutRows
                        _StorageMoveEvents.StorageInRows = _StorageOutRowsInRows.StorageInRows
                        _StorageMoveEvents.QTT_Free_Before = _StorageMoveEvents_Collection(0).QTT_Free_After
                        _StorageMoveEvents.QTT_Free_After = _StorageMoveEvents.QTT_Free_Before + _StorageMoveEvents.QTT
                        _StorageMoveEvents.MoveEventsType = eMoveEventsType.StorageOutStorno
                        _StorageMoveEvents.ExecutionDate = _StorageOutRows.StorageOutHeader.CreateDate

                        _StorageOutRows.QTT = 0
                    Next
                Next

                _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 aToTable_StorageOut_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_StorageOut.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
        Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))

        Dim _StorageOutHeader_Original As StorageOutHeader = TryCast(View.SelectedObjects(0), StorageOutHeader)

        '--1. Levesszük a Nonpersistent propertyket az aktuális objektumról --------------------------------------------------------------------------------------------------
        Dim _row_QTT_Will As Double = _StorageOutHeader_Original.row_QTT_Will
        Dim _row_OutMode As eOutMode = _StorageOutHeader_Original.row_OutMode
        Dim _row_StorageComputed_Oid As System.Guid
        If _row_OutMode = eOutMode.eProducts Then
            _row_StorageComputed_Oid = _StorageOutHeader_Original.row_StorageComputed.Oid
        End If

        Dim _row_StorageInRows_Oid As System.Guid
        If _StorageOutHeader_Original.row_OutMode <> eOutMode.eProducts Then
            _row_StorageInRows_Oid = _StorageOutHeader_Original.row_StorageInRows.Oid

        End If
        '---------------------------------------------------------------------------------------------------------------------------------------------------------------------

        Dim _StorageOutHeader As StorageOutHeader = _onew.GetObjectByKey(Of StorageOutHeader)(TryCast(View.SelectedObjects(0), StorageOutHeader).Oid)

        Dim _StorageOutRowsInRows As StorageOutRowsInRows
        Dim _StorageOutRows As StorageOutRows
        Dim _StorageComputed As StorageComputed

        Dim _row_StorageComputed As StorageComputed = _onew.GetObjectByKey(Of StorageComputed)(_row_StorageComputed_Oid)
        Dim _row_StorageInRows As StorageInRows = _onew.GetObjectByKey(Of StorageInRows)(_row_StorageInRows_Oid)
        Dim _StorageInRows As StorageInRows
        Dim _StorageInRows_Collection As XPCollection(Of StorageInRows)

        Dim _StorageOutRows_R As StorageOutRows
        Dim _StorageOutRows_Collection_R As XPCollection(Of StorageOutRows)

        Dim _QTT_Mod As Double = 0
        Dim _QTT_Free As Double = 0
        Dim _QTT_ReservedOut As Double = 0
        Dim _QTT_Will_Out As Double = 0

        Try
            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
                If _row_OutMode = eOutMode.eProducts Then
                    If _row_StorageComputed.QTTFree < _row_QTT_Will Then
                        Throw New Exception("*Ebből a termékből ebben a raktárban ennyi nem áll rendellkezésre!")
                    End If
                    _StorageOutRows = _onew.FindObject(Of StorageOutRows) _
                                              (CriteriaOperator.Parse("StorageOutHeader=? and Products=?", _
                                              _StorageOutHeader, _row_StorageComputed.Products), True)
                    If _StorageOutRows Is Nothing Then
                        _StorageOutRows = New StorageOutRows(_onew.Session)
                        _StorageOutRows.StorageOutHeader = _StorageOutHeader
                        _StorageOutRows.Products = _row_StorageComputed.Products

                    End If

                    _StorageInRows_Collection = New XPCollection(Of StorageInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                               CriteriaOperator.Parse("StorageInHeader.Storage=? and StorageInHeader.IsEditable=False and StorageInHeader.IsStorno=False and Products=?", _
                               _onew.GetObject(_StorageOutHeader.Storage), _onew.GetObject(_row_StorageComputed.Products)))
                    _StorageInRows_Collection.Sorting.Add(New SortProperty("StorageInHeader.CreateDate", DB.SortingDirection.Ascending))

                    _QTT_Mod = _row_QTT_Will
                    For Each _StorageInRows In _StorageInRows_Collection
                        _QTT_Free = _StorageInRows.QTT - _StorageInRows.QTT_Out - _StorageInRows.QTT_Out_Will
                        If _QTT_Free > 0 Then
                            If _QTT_Free < _QTT_Mod Then
                                _StorageInRows.QTT_Out_Will += _QTT_Free
                                _StorageOutRows.QTT_Will += _QTT_Free
                                _StorageInRows.Save()
                                _QTT_Mod -= _QTT_Free
                                '------- FIFO sor beszúrása
                                _StorageOutRowsInRows = _onew.FindObject(Of StorageOutRowsInRows)(CriteriaOperator.Parse("StorageInRows=? and StorageOutRows=?", _
                                                                                _onew.GetObject(_StorageInRows), _onew.GetObject(_StorageOutRows)))
                                If _StorageOutRowsInRows IsNot Nothing Then
                                    _StorageOutRowsInRows.QTT += _QTT_Free
                                Else
                                    _StorageOutRowsInRows = New StorageOutRowsInRows(_onew.Session)
                                    _StorageOutRowsInRows.StorageInRows = _onew.GetObject(_StorageInRows)
                                    _StorageOutRowsInRows.StorageOutRows = _onew.GetObject(_StorageOutRows)
                                    _StorageOutRowsInRows.QTT = _QTT_Free
                                End If

                            Else
                                _StorageInRows.QTT_Out_Will += _QTT_Mod
                                _StorageOutRows.QTT_Will += _QTT_Mod
                                _StorageInRows.Save()
                                '------- FIFO sor beszúrása
                                _StorageOutRowsInRows = _onew.FindObject(Of StorageOutRowsInRows)(CriteriaOperator.Parse("StorageInRows=? and StorageOutRows=?", _
                                                                                                    _onew.GetObject(_StorageInRows), _onew.GetObject(_StorageOutRows)))
                                If _StorageOutRowsInRows IsNot Nothing Then
                                    _StorageOutRowsInRows.QTT += _QTT_Mod
                                Else
                                    _StorageOutRowsInRows = New StorageOutRowsInRows(_onew.Session)
                                    _StorageOutRowsInRows.StorageInRows = _onew.GetObject(_StorageInRows)
                                    _StorageOutRowsInRows.StorageOutRows = _onew.GetObject(_StorageOutRows)
                                    _StorageOutRowsInRows.QTT = _QTT_Mod
                                End If

                                Exit For
                            End If
                        End If
                    Next
                    If _StorageOutRows.RowIndex = -1 Then
                        If _StorageOutHeader.StorageOutRows.Count = 0 Then
                            _StorageOutRows.RowIndex = 1
                        Else
                            _StorageOutRows.RowIndex = _StorageOutHeader.StorageOutRows.Count
                        End If
                    End If
                    _StorageOutRows.Save()


                    _StorageComputed = _onew.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? and Products=?", _
                                                            _onew.GetObject(_StorageOutHeader.Storage), _onew.GetObject(_row_StorageComputed.Products)))
                    _QTT_ReservedOut = _StorageComputed.QTTReservedOut
                    _StorageOutRows_Collection_R = New XPCollection(Of StorageOutRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                                            CriteriaOperator.Parse("QTT_Will>0 and StorageOutHeader.Storage=? and Products=?", _onew.GetObject(_StorageOutHeader.Storage), _onew.GetObject(_row_StorageComputed.Products)))
                    For Each _StorageOutRows_R In _StorageOutRows_Collection_R
                        _QTT_Will_Out += _StorageOutRows_R.QTT_Will
                    Next

                    _StorageComputed.QTTFree = _StorageComputed.QTTFree - (_QTT_Will_Out - _QTT_ReservedOut)
                    _StorageComputed.QTTReservedOut = _QTT_Will_Out

                    _onew.CommitChanges()
                Else
                    _StorageOutRows = _onew.FindObject(Of StorageOutRows)(CriteriaOperator.Parse("StorageOutHeader=? and Products=?", _
                                                          _onew.GetObject(_StorageOutHeader), _onew.GetObject(_row_StorageInRows.Products)), True)
                    If _StorageOutRows Is Nothing Then
                        _StorageOutRows = New StorageOutRows(_onew.Session)
                        _StorageOutRows.StorageOutHeader = _onew.GetObject(_StorageOutHeader)
                        _StorageOutRows.Products = _onew.GetObject(_row_StorageInRows.Products)

                    End If

                    _StorageInRows_Collection = New XPCollection(Of StorageInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                               CriteriaOperator.Parse("Oid=?", _row_StorageInRows.Oid))
                    _StorageInRows_Collection.Sorting.Add(New SortProperty("StorageInHeader.CreateDate", DB.SortingDirection.Ascending))

                    _QTT_Mod = _row_QTT_Will
                    For Each _StorageInRows In _StorageInRows_Collection
                        _QTT_Free = _StorageInRows.QTT - _StorageInRows.QTT_Out - _StorageInRows.QTT_Out_Will

                        If _QTT_Free < _QTT_Mod Then
                            _StorageInRows.QTT_Out_Will += _QTT_Free
                            _StorageOutRows.QTT_Will += _QTT_Free
                            _QTT_Mod -= _QTT_Free
                            '------- FIFO sor beszúrása
                            _StorageOutRowsInRows = _onew.FindObject(Of StorageOutRowsInRows)(CriteriaOperator.Parse("StorageInRows=? and StorageOutRows=?", _
                                                                    _onew.GetObject(_StorageInRows), _onew.GetObject(_StorageOutRows)))
                            If _StorageOutRowsInRows IsNot Nothing Then
                                _StorageOutRowsInRows.QTT += _QTT_Free
                            Else
                                _StorageOutRowsInRows = New StorageOutRowsInRows(_onew.Session)
                                _StorageOutRowsInRows.StorageInRows = _onew.GetObject(_StorageInRows)
                                _StorageOutRowsInRows.StorageOutRows = _onew.GetObject(_StorageOutRows)
                                _StorageOutRowsInRows.QTT = _QTT_Free
                            End If

                        Else
                            _StorageInRows.QTT_Out_Will += _QTT_Mod
                            _StorageOutRows.QTT_Will += _QTT_Mod
                            '------- FIFO sor beszúrása
                            _StorageOutRowsInRows = _onew.FindObject(Of StorageOutRowsInRows)(CriteriaOperator.Parse("StorageInRows=? and StorageOutRows=?", _
                                                                                    _onew.GetObject(_StorageInRows), _onew.GetObject(_StorageOutRows)), True)
                            If _StorageOutRowsInRows IsNot Nothing Then
                                _StorageOutRowsInRows.QTT += _QTT_Mod
                            Else
                                _StorageOutRowsInRows = New StorageOutRowsInRows(_onew.Session)
                                _StorageOutRowsInRows.StorageInRows = _onew.GetObject(_StorageInRows)
                                _StorageOutRowsInRows.StorageOutRows = _onew.GetObject(_StorageOutRows)
                                _StorageOutRowsInRows.QTT = _QTT_Mod
                            End If

                            Exit For
                        End If
                    Next
                    If _StorageOutRows.RowIndex = -1 Then
                        If _StorageOutHeader.StorageOutRows.Count = 0 Then
                            _StorageOutRows.RowIndex = 1
                        Else
                            _StorageOutRows.RowIndex = _StorageOutHeader.StorageOutRows.Count
                        End If
                    End If
                    _StorageOutRows.Save()

                    _StorageComputed = _onew.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? and Products=?", _
                                                                    _onew.GetObject(_StorageOutHeader.Storage), _onew.GetObject(_row_StorageInRows.Products)), True)
                    _QTT_ReservedOut = _StorageComputed.QTTReservedOut
                    _StorageOutRows_Collection_R = New XPCollection(Of StorageOutRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                                            CriteriaOperator.Parse("QTT_Will>0 and StorageOutHeader.Storage=? and Products=?", _onew.GetObject(_StorageOutHeader.Storage), _onew.GetObject(_row_StorageInRows.Products)))
                    For Each _StorageOutRows_R In _StorageOutRows_Collection_R
                        _QTT_Will_Out += _StorageOutRows_R.QTT_Will
                    Next

                    _StorageComputed.QTTFree = _StorageComputed.QTTFree - (_QTT_Will_Out - _QTT_ReservedOut)
                    _StorageComputed.QTTReservedOut = _QTT_Will_Out

                    _onew.CommitChanges()

                    If _StorageOutHeader.StorageMoveType.MoveType = eMoveType.InventoryTransfer Then
                        CheckInventoryTransferStorageInRows(_uow, _StorageOutHeader.StorageInRows)
                    End If

                End If
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            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 aFinalizeAndNew_StorageOut_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalizeAndNew_StorageOut.Execute, aFinalize_StorageOut.Execute
        '--Az eljárás egy kitárolást véglegesít és nyit egy újat
        'Ide lett irányítva a aFinalize_StorageOut_Execute is!!!!!!!!!! Ez így jó, vagy szét kell szedni külön?
        '_______________________________________________________________________________________________
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
        Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))

        Try
            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 _StorageOutHeader As StorageOutHeader = _onew.GetObjectByKey(Of StorageOutHeader)(TryCast(View.SelectedObjects(0), StorageOutHeader).Oid)
                If _StorageOutHeader.Storage Is Nothing Then Throw New Exception("*Nincs beállítva raktár!")
                If _StorageOutHeader.StorageOutRows.Count = 0 Then Throw New Exception("*Nincs egyetlen kitárolási sor sem!")

                For Each _StorageOutRows In _StorageOutHeader.StorageOutRows
                    Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                      CriteriaOperator.Parse("StorageOutRows=?", _onew.GetObject(_StorageOutRows)))

                    For Each _StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                        If _StorageOutRowsInRows.QTT > 0 Then
                            Dim _StorageMoveEvents_Collection As New XPCollection(Of StorageMoveEvents)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                                                                                CriteriaOperator.Parse("StorageInRows=?", _onew.GetObject(_StorageOutRowsInRows.StorageInRows)))
                            _StorageMoveEvents_Collection.Sorting.Add(New SortProperty("ObjectCreated", DB.SortingDirection.Descending))

                            _StorageOutRowsInRows.StorageInRows.QTT_Out += _StorageOutRowsInRows.QTT
                            _StorageOutRowsInRows.StorageInRows.QTT_Out_Will -= _StorageOutRowsInRows.QTT

                            Dim _StorageMoveEvents As New StorageMoveEvents(_onew.Session)
                            _StorageMoveEvents.StorageInRows = _onew.GetObject(_StorageOutRowsInRows.StorageInRows)
                            _StorageMoveEvents.StorageOutRows = _onew.GetObject(_StorageOutRowsInRows.StorageOutRows)
                            If _StorageMoveEvents_Collection.Count > 0 Then
                                _StorageMoveEvents.QTT_Free_Before = _StorageMoveEvents_Collection(0).QTT_Free_After
                            Else
                                _StorageMoveEvents.QTT_Free_Before = 0
                            End If
                            _StorageMoveEvents.QTT = (-1) * _StorageOutRowsInRows.QTT
                            _StorageMoveEvents.QTT_Free_After = _StorageMoveEvents.QTT_Free_Before + _StorageMoveEvents.QTT
                            _StorageMoveEvents.MoveEventsType = eMoveEventsType.StorageOutFinal
                            _StorageMoveEvents.ExecutionDate = _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.CreateDate
                        End If
                    Next

                    _StorageOutRows.QTT += _StorageOutRows.QTT_Will

                    If _StorageOutRows.OrderInRows IsNot Nothing Then
                        _StorageOutRows.OrderInRows.QTT_Storaged += _StorageOutRows.OrderInRows.QTT_Storaged_Will
                        _StorageOutRows.OrderInRows.QTT_Storaged_Will = 0
                    End If
                    Dim _StorageComputed As StorageComputed = _onew.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _onew.GetObject(_StorageOutRows.Products), _
                                                                                                                          _onew.GetObject(_StorageOutRows.StorageOutHeader.Storage)), True)
                    _StorageComputed.QTTReservedOut -= _StorageOutRows.QTT_Will
                    _StorageOutRows.QTT_Will = 0
                Next

                _StorageOutHeader.IsEditable = False
                _StorageOutHeader.IsFinalized = True


                '//Ha 9-es tipusú a mozgás akkor be kell állítani bejövő szállítólevelenként a visszárúzott szumma mennyiséget !!!



                If _StorageOutHeader.StorageMoveType.MoveType = eMoveType.ReturnBack Then
                    Dim _StorageOutRows As StorageOutRows
                    Dim _StorageOutRowsInRows As StorageOutRowsInRows
                    Dim _StorageOutRowsInRows2 As StorageOutRowsInRows

                    For Each _StorageOutRows In _StorageOutHeader.StorageOutRows
                        For Each _StorageOutRowsInRows In _StorageOutRows.StorageOutRowsInRows
                            If _StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows IsNot Nothing Then
                                _StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.QTT_Returned = 0
                            End If
                        Next
                    Next

                    For Each _StorageOutRows In _StorageOutHeader.StorageOutRows
                        For Each _StorageOutRowsInRows In _StorageOutRows.StorageOutRowsInRows
                            If _StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows IsNot Nothing Then
                                Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(_onew.Session,
                                        CriteriaOperator.Parse("StorageOutRows.StorageOutHeader.StorageMoveType.MoveType='ReturnBack' and " +
                                                               " StorageOutRows.StorageOutHeader.IsEditable=False and " +
                                                               " StorageInRows.DeliveryNoteInRows.Oid=?", _StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.Oid))
                                Dim _QTT_Returned As Double = 0
                                For Each _StorageOutRowsInRows2 In _StorageOutRowsInRows_Collection
                                    _QTT_Returned += _StorageOutRowsInRows2.QTT
                                Next
                                _StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.QTT_Returned += _QTT_Returned + _StorageOutRowsInRows.QTT
                            End If
                        Next
                    Next
                End If

                _onew.CommitChanges()
                If _StorageOutHeader.StorageMoveType.MoveType = eMoveType.InventoryTransfer Then
                    CheckInventoryTransferStorageInRows(_uow, _StorageOutHeader.StorageInRows)
                End If

                If sender.id = "aFinalizeAndNew_StorageOut" Then
                    Frame.GetController(Of NewObjectViewController)().NewObjectAction.DoExecute(Frame.GetController(Of NewObjectViewController)().NewObjectAction.Items(0))
                End If

                Dim _ReportData As ReportData = _onew.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _StorageOutHeader.Storage.ReportOut.Oid), True)
                If _ReportData IsNot Nothing Then
                    Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("StorageOutRows.StorageOutHeader.DocumentNumber=?", _StorageOutHeader.DocumentNumber), True)
                Else
                    MsgBox("Nincs beállítva nyomtatvány!")
                End If
                View.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba")
        Finally
            LockingModule.UnLockObject(_uow_lock, GetType(StorageComputed), "-", _User)
        End Try
    End Sub
    Private Sub aSOFinList_Order_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSOFinList_Order.Execute
        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
            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 _StorageOutRows As StorageOutRows
                Dim _StorageOutHeader As StorageOutHeader = Nothing
                Dim _IsHeaderCreated As Boolean = False
                Dim _StorageOutHeader_Collection As New ArrayList
                Dim _StorageOutHeader_Exists As Boolean = False

                _StorageOutHeader_Collection.Clear()

                For Each _StorageOutRows In e.SelectedObjects
                    _StorageOutRows = _onew.GetObject(_StorageOutRows)
                    _StorageOutHeader_Exists = False
                    For Each _StorageOutHeader In _StorageOutHeader_Collection
                        If _StorageOutRows.StorageOutHeader.Oid = _StorageOutHeader.Oid Then
                            _StorageOutHeader_Exists = True
                            Exit For
                        End If
                    Next
                    If _StorageOutHeader_Exists = False Then
                        _StorageOutHeader_Collection.Add(_StorageOutRows.StorageOutHeader)
                    End If
                Next

                For Each _StorageOutHeader In _StorageOutHeader_Collection
                    Dim _StorageComputed As StorageComputed
                    Dim _StorageOutRowsInRows_Collection As XPCollection(Of StorageOutRowsInRows)
                    Dim _StorageOutRowsInRows As StorageOutRowsInRows
                    Dim _QTT_Free_Before As Double = 0
                    Dim _QTT_Free_After As Double = 0

                    For Each _StorageOutRows In _StorageOutHeader.StorageOutRows
                        _StorageOutRowsInRows_Collection = New XPCollection(Of StorageOutRowsInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                              CriteriaOperator.Parse("StorageOutRows=?", _StorageOutRows))

                        For Each _StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                            If _StorageOutRowsInRows.QTT > 0 Then

                                Dim _StorageMoveEvents_Collection As New XPCollection(Of StorageMoveEvents)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                                    CriteriaOperator.Parse("StorageInRows=?", _StorageOutRowsInRows.StorageInRows))

                                _StorageMoveEvents_Collection.Sorting.Add(New SortProperty("ObjectCreated", DB.SortingDirection.Descending))

                                _StorageOutRowsInRows.StorageInRows.QTT_Out += _StorageOutRowsInRows.QTT
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will -= _StorageOutRowsInRows.QTT

                                Dim _StorageMoveEvents As StorageMoveEvents = _onew.CreateObject(Of StorageMoveEvents)()
                                _StorageMoveEvents.StorageInRows = _StorageOutRowsInRows.StorageInRows
                                _StorageMoveEvents.StorageOutRows = _StorageOutRowsInRows.StorageOutRows
                                If _StorageMoveEvents_Collection.Count > 0 Then
                                    _StorageMoveEvents.QTT_Free_Before = _StorageMoveEvents_Collection(0).QTT_Free_After
                                Else
                                    _StorageMoveEvents.QTT_Free_Before = 0
                                End If
                                _StorageMoveEvents.QTT = (-1) * _StorageOutRowsInRows.QTT
                                _StorageMoveEvents.QTT_Free_After = _StorageMoveEvents.QTT_Free_Before + _StorageMoveEvents.QTT
                                _StorageMoveEvents.MoveEventsType = eMoveEventsType.StorageOutFinal
                                _StorageMoveEvents.ExecutionDate = _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.CreateDate
                                _StorageMoveEvents.Save()
                            End If

                        Next

                        _StorageOutRows.QTT += _StorageOutRows.QTT_Will

                        If _StorageOutRows.OrderInRows IsNot Nothing Then
                            _StorageOutRows.OrderInRows.QTT_Storaged += _StorageOutRows.OrderInRows.QTT_Storaged_Will
                            _StorageOutRows.OrderInRows.QTT_Storaged_Will = 0
                        End If
                        _StorageComputed = _onew.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _StorageOutRows.Products, _StorageOutRows.StorageOutHeader.Storage), True)
                        _StorageComputed.QTTReservedOut -= _StorageOutRows.QTT_Will
                        _StorageOutRows.QTT_Will = 0
                        _StorageOutHeader.IsEditable = False
                        _StorageOutHeader.IsFinalized = True
                        _StorageOutHeader.CreateDate = GetSQLTime(_onew.Session)
                    Next
                Next
                _onew.CommitChanges() 'Véglegesítjük az újonnan létrehozott fejlécet ezáltal a sorai kitárolt állapotba kerülnek

            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 aBackToEdit_StorageOut_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToEdit_StorageOut.Execute
        '-- Az akció egy véglegesített kitárolási bizonylatot állít vissza szerkeszthetőre -------------------------
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
        Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))
        Dim _StorageOutHeader As StorageOutHeader = _onew.GetObjectByKey(Of StorageOutHeader)(TryCast(View.SelectedObjects(0), StorageOutHeader).Oid)
        Try
            If _StorageOutHeader.IsEditable = False And _StorageOutHeader.IsStorno = False And _StorageOutHeader.Active = True Then
                Dim _AllowBackToEdit = False
                For Each _StorageOutRows As StorageOutRows In _StorageOutHeader.StorageOutRows
                    If _StorageOutRows.QTT_To_DeliveryOut = 0 And _StorageOutRows.QTT_Will = 0 Then
                        _AllowBackToEdit = True
                        Exit For
                    End If
                Next

                If _AllowBackToEdit = True Then
                    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
                        _StorageOutHeader.IsEditable = True

                        For Each _StorageOutRows As StorageOutRows In _StorageOutHeader.StorageOutRows
                            _StorageOutRows.QTT_Will = _StorageOutRows.QTT
                            _StorageOutRows.Save()

                            Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                             CriteriaOperator.Parse("StorageOutRows=?", _StorageOutRows))

                            For Each _StorageOutRowsInRows As StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                                Dim _StorageComputed As StorageComputed = _onew.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? AND Products=?", _
                                                                    _onew.GetObject(_StorageOutHeader.Storage), _onew.GetObject(_StorageOutRows.Products)))
                                Dim _StorageMoveEvents_Collection As New XPCollection(Of StorageMoveEvents)(_onew.Session, CriteriaOperator.Parse("StorageInRows=? AND StorageOutRows=?", _
                                                                                            _onew.GetObject(_StorageOutRowsInRows.StorageInRows), _onew.GetObject(_StorageOutRows)))

                                _StorageComputed.QTTReservedOut += _StorageOutRowsInRows.QTT

                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will += _StorageOutRowsInRows.QTT
                                _StorageOutRowsInRows.StorageInRows.QTT_Out -= _StorageOutRowsInRows.QTT

                                If _StorageOutRowsInRows.StorageOutRows.OrderInRows IsNot Nothing Then
                                    _StorageOutRowsInRows.StorageOutRows.OrderInRows.QTT_Storaged -= _StorageOutRowsInRows.QTT
                                    _StorageOutRowsInRows.StorageOutRows.OrderInRows.QTT_Storaged_Will += _StorageOutRowsInRows.QTT
                                End If

                                Dim _StorageMoveEvents As New StorageMoveEvents(_onew.Session)
                                _StorageMoveEvents.QTT = _StorageOutRowsInRows.QTT
                                _StorageMoveEvents.StorageOutRows = _onew.GetObject(_StorageOutRows)
                                _StorageMoveEvents.StorageInRows = _onew.GetObject(_StorageOutRowsInRows.StorageInRows)
                                _StorageMoveEvents.QTT_Free_Before = _StorageMoveEvents_Collection(0).QTT_Free_After
                                _StorageMoveEvents.QTT_Free_After = _StorageMoveEvents.QTT_Free_Before + _StorageMoveEvents.QTT
                                _StorageMoveEvents.MoveEventsType = eMoveEventsType.StorageOutBackToEdit
                                _StorageMoveEvents.ExecutionDate = _StorageOutRows.StorageOutHeader.CreateDate

                                _StorageOutRows.QTT = 0
                            Next

                        Next
                        _onew.CommitChanges()
                    End If
                End If
            End If
            If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba")
        Finally
            LockingModule.UnLockObject(_uow_lock, GetType(StorageComputed), "-", _User)
        End Try
    End Sub
    Private Sub aToTableD_StorageOut_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_StorageOut.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
        Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))

        Try
            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 _StorageOutHeader As StorageOutHeader = _onew.GetObjectByKey(Of StorageOutHeader)(TryCast(View.SelectedObjects(0), StorageOutHeader).Oid)
                Dim _StorageOutHeader_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("StorageOutRows")

                Dim _StorageOutRows As StorageOutRows

                Dim _StorageOutRows_R As StorageOutRows
                Dim _StorageOutRows_Collection_R As XPCollection(Of StorageOutRows)

                Dim _StorageComputed As StorageComputed
                Dim _Storage As Storage = Nothing

                Dim _StorageOutRowsInRows As StorageOutRowsInRows
                Dim _StorageOutRowsInRows_Collection As XPCollection(Of StorageOutRowsInRows)

                Dim _QTT_Process As Double = 0
                Dim _QTT_Mod As Double = 0

                Dim _QTT_Mod_In As Double = 0

                Dim _QTT_ReservedOut As Double = 0
                Dim _QTT_Will_Out As Double = 0

                Dim _StorageOutRows_Original As StorageOutRows

                For Each _StorageOutRows_Original In _StorageOutHeader_ListEditor.ListView.SelectedObjects

                    _StorageOutRows = _onew.GetObjectByKey(Of StorageOutRows)(_StorageOutRows_Original.Oid)

                    _QTT_Mod = Math.Round(_StorageOutRows.QTT_Will, 4, MidpointRounding.AwayFromZero)
                    _Storage = _StorageOutRows.StorageOutHeader.Storage
                    _StorageOutRowsInRows_Collection = New XPCollection(Of StorageOutRowsInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                        CriteriaOperator.Parse("StorageOutRows=? and QTT>0", _onew.GetObject(_StorageOutRows)))
                    If _QTT_Mod <= _StorageOutRows.QTT_Will Then
                        _StorageOutRows.QTT_Will -= _QTT_Mod
                        '  _StorageOutRows.Save()
                        _StorageOutRows.Delete()
                        '--- FIFO sor !!!
                        _QTT_Mod_In = _QTT_Mod
                        For Each _StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                            If _QTT_Mod_In <= _StorageOutRowsInRows.StorageInRows.QTT_Out_Will Then
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will -= _QTT_Mod_In
                                _StorageOutRowsInRows.QTT -= _QTT_Mod_In
                                Exit For
                            Else
                                _QTT_Mod_In -= _StorageOutRowsInRows.StorageInRows.QTT_Out_Will
                                _StorageOutRowsInRows.QTT = 0
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will = 0
                            End If
                        Next
                    Else
                        _QTT_Mod -= _StorageOutRows.QTT_Will
                        _StorageOutRows.QTT_Will = 0
                        _StorageOutRows.Delete()
                        '--- FIFO sor
                        _QTT_Mod_In = _QTT_Mod
                        For Each _StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                            If _QTT_Mod_In <= _StorageOutRowsInRows.StorageInRows.QTT_Out_Will Then
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will -= _QTT_Mod_In
                                _StorageOutRowsInRows.QTT -= _QTT_Mod_In
                                Exit For
                            Else
                                _QTT_Mod_In -= _StorageOutRowsInRows.StorageInRows.QTT_Out_Will
                                _StorageOutRowsInRows.QTT = 0
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will = 0
                            End If
                        Next
                    End If
                    '--- StorageComputed újraszámolása

                    _StorageComputed = _onew.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? and Products=?", _onew.GetObject(_Storage), _onew.GetObject(_StorageOutRows.Products)), True)
                    _QTT_ReservedOut = _StorageComputed.QTTReservedOut
                    _StorageOutRows_Collection_R = New XPCollection(Of StorageOutRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                                            CriteriaOperator.Parse("QTT_Will>0 and StorageOutHeader.Storage=? and Products=?", _onew.GetObject(_Storage), _onew.GetObject(_StorageOutRows.Products)))
                    For Each _StorageOutRows_R In _StorageOutRows_Collection_R
                        _QTT_Will_Out += Math.Round(_StorageOutRows_R.QTT_Will, 2, MidpointRounding.AwayFromZero)
                    Next

                    _StorageComputed.QTTFree = Math.Round(_StorageComputed.QTTFree - (Math.Round(_QTT_Will_Out, 2, MidpointRounding.AwayFromZero) - Math.Round(_QTT_ReservedOut, 2, MidpointRounding.AwayFromZero)), 2, MidpointRounding.AwayFromZero)
                    _StorageComputed.QTTReservedOut = Math.Round(_QTT_Will_Out, 2, MidpointRounding.AwayFromZero)
                Next
                _onew.CommitChanges()

                If _StorageOutHeader.StorageMoveType.MoveType = eMoveType.InventoryTransfer Then
                    CheckInventoryTransferStorageInRows(_uow, _StorageOutHeader.StorageInRows)
                End If
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba")
        Finally
            LockingModule.UnLockObject(_uow_lock, GetType(StorageComputed), "-", _User)
        End Try
    End Sub
    Private Sub aUp_StorageOut_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aUp_StorageOut.Execute
        ' A kiválaszott sort egy sorral feljebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _StorageOutHeader As StorageOutHeader = TryCast(View.SelectedObjects(0), StorageOutRows).StorageOutHeader
        Dim _StorageOutRowsSelected As StorageOutRows
        Dim _StorageOutRowsMinus As StorageOutRows = Nothing
        Dim _StorageOutRows As StorageOutRows

        Try
            _StorageOutRowsSelected = TryCast(View.SelectedObjects(0), StorageOutRows) ' 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 _StorageOutRowsSelected.RowIndex > 0 Then
            For Each _StorageOutRows In _StorageOutHeader.StorageOutRows
                If _StorageOutRows.RowIndex = _StorageOutRowsSelected.RowIndex - 1 Then
                    _StorageOutRowsMinus = _StorageOutRows
                    Exit For
                End If
            Next

            If _StorageOutRowsMinus IsNot Nothing Then
                _StorageOutRowsSelected.RowIndex -= 1
                _StorageOutRowsMinus.RowIndex += 1
            End If

            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aDown_StorageOut_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aDown_StorageOut.Execute
        ' A kiválaszott sort egy sorral lejebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _StorageOutHeader As StorageOutHeader = TryCast(View.SelectedObjects(0), StorageOutRows).StorageOutHeader
        Dim _StorageOutRowsSelected As StorageOutRows
        Dim _StorageOutRowsPlus As StorageOutRows = Nothing
        Dim _StorageOutrows As StorageOutRows

        Try
            _StorageOutRowsSelected = TryCast(View.SelectedObjects(0), StorageOutRows) ' 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 _StorageOutRowsSelected.RowIndex < _StorageOutHeader.StorageOutRows.Count - 1 Then
            For Each _StorageOutrows In _StorageOutHeader.StorageOutRows
                If _StorageOutrows.RowIndex = _StorageOutRowsSelected.RowIndex + 1 Then
                    _StorageOutRowsPlus = _StorageOutrows
                    Exit For
                End If
            Next

            If _StorageOutRowsPlus IsNot Nothing Then
                _StorageOutRowsSelected.RowIndex += 1
                _StorageOutRowsPlus.RowIndex -= 1
            End If
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aCreateDeliveryNote_CustomizePopupWindowParams(ByVal sender As Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateDeliveryNote.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _DeliveryNoteView As DeliveryNoteView = _onew.CreateObject(Of DeliveryNoteView)()

        e.View = Application.CreateDetailView(_onew, "DeliveryNoteView_DetailView", False, _DeliveryNoteView)
    End Sub
    Private Sub aCreateDeliveryNote_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateDeliveryNote.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteView As DeliveryNoteView = TryCast(e.PopupWindow.View.SelectedObjects(0), DeliveryNoteView)
        Dim _DeliveryNoteOutHeader_New As DeliveryNoteOutHeader = _ocur.CreateObject(Of DeliveryNoteOutHeader)()
        Dim _StorageOutRows As StorageOutRows
        Dim _StorageOutRowsInRows As StorageOutRowsInRows
        Dim i As Integer = 0

        _DeliveryNoteOutHeader_New.CurrencyName = _ocur.GetObject(_DeliveryNoteView.CurrencyName)
        _DeliveryNoteOutHeader_New.Customers = _ocur.GetObject(_DeliveryNoteView.Customers)
        _DeliveryNoteOutHeader_New.CustomersFrom = _ocur.GetObject(_DeliveryNoteView.CustomersFrom)
        _DeliveryNoteOutHeader_New.DateCreated = GetSQLTime(_ocur.Session)
        _DeliveryNoteOutHeader_New.DateExecution = _ocur.GetObject(_DeliveryNoteView.DateExecution)
        _DeliveryNoteOutHeader_New.Note = _ocur.GetObject(_DeliveryNoteView.Note)
        _DeliveryNoteOutHeader_New.QualityOption = _ocur.GetObject(_DeliveryNoteView.QualityOption)
        _DeliveryNoteOutHeader_New.ShipmentOption = _ocur.GetObject(_DeliveryNoteView.ShipmentOption)
        _DeliveryNoteOutHeader_New.TransportAddress = _ocur.GetObject(_DeliveryNoteView.TransportAddress)

        For Each _StorageOutRows In View.SelectedObjects
            Dim _DeliveryNoteOutRows As DeliveryNoteOutRows = _ocur.CreateObject(Of DeliveryNoteOutRows)()

            _DeliveryNoteOutRows.DeliveryNoteOutHeader = _DeliveryNoteOutHeader_New
            _DeliveryNoteOutRows.QTT = _StorageOutRows.QTT_Will

            For Each _StorageOutRowsInRows In _StorageOutRows.StorageOutRowsInRows
                _DeliveryNoteOutRows.StorageOutRowsInRows = _StorageOutRowsInRows
            Next

            _DeliveryNoteOutRows.RowIndex = i
            i += 1
        Next
        _ocur.CommitChanges()
    End Sub
    Private Sub aBackToStorageOutFromOrderIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToStorageOutFromOrderIn.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
        Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))

        Try
            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 _StorageOutRows As StorageOutRows = _onew.GetObject(TryCast(View.SelectedObjects(0), StorageOutRows))
                Dim _OrderInRows As OrderInRows = _onew.GetObjectByKey(Of OrderInRows)(_StorageOutRows.OrderInRows.Oid)
                Dim _StorageComputed As StorageComputed

                Dim _QTT_Mod As Double = 0
                Dim _QTT_Mod_In As Double = 0
                Dim _QTT_ReservedOut As Double = 0
                Dim _QTT_Process As Double = 0
                Dim _QTT_Will_Out As Double = 0

                Dim _StorageOutRows_Collection As XPCollection(Of StorageOutRows)
                Dim _StorageOutRowsInRows_Collection As XPCollection(Of StorageOutRowsInRows)
                Dim _StorageOutRows_Collection_R As XPCollection(Of StorageOutRows)

                Dim _Storage As Storage



                '-- FIFO láb alapján vissza !
                _QTT_Process = _StorageOutRows.QTT_Will
                _QTT_Mod = _StorageOutRows.QTT_Will
                _StorageOutRows_Collection = New XPCollection(Of StorageOutRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                   CriteriaOperator.Parse("Oid=?", _StorageOutRows.Oid))
                For Each _StorageOutRows In _StorageOutRows_Collection
                    _Storage = _onew.GetObject(_StorageOutRows.StorageOutHeader.Storage)
                    _StorageOutRowsInRows_Collection = New XPCollection(Of StorageOutRowsInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                        CriteriaOperator.Parse("StorageOutRows=? and QTT>0", _onew.GetObject(_StorageOutRows)))
                    If _QTT_Mod <= _StorageOutRows.QTT_Will Then
                        _StorageOutRows.QTT_Will -= _QTT_Mod
                        _StorageOutRows.Save()
                        '--- FIFO sor !!!
                        _QTT_Mod_In = _QTT_Mod
                        For Each _StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                            If _QTT_Mod_In <= _StorageOutRowsInRows.StorageInRows.QTT_Out_Will Then
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will -= _QTT_Mod_In
                                _StorageOutRowsInRows.QTT -= _QTT_Mod_In
                                Exit For
                            Else
                                _QTT_Mod_In -= _StorageOutRowsInRows.StorageInRows.QTT_Out_Will
                                _StorageOutRowsInRows.QTT = 0
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will = 0
                            End If
                        Next
                    Else
                        _QTT_Mod -= _StorageOutRows.QTT_Will
                        _StorageOutRows.QTT_Will = 0
                        _StorageOutRows.Save()
                        '--- FIFO sor
                        _QTT_Mod_In = _QTT_Mod
                        For Each _StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                            If _QTT_Mod_In <= _StorageOutRowsInRows.StorageInRows.QTT_Out_Will Then
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will -= _QTT_Mod_In
                                _StorageOutRowsInRows.QTT -= _QTT_Mod_In
                                Exit For
                            Else
                                _QTT_Mod_In -= _StorageOutRowsInRows.StorageInRows.QTT_Out_Will
                                _StorageOutRowsInRows.QTT = 0
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will = 0
                            End If
                        Next
                    End If
                    _OrderInRows.QTT_Storaged_Will -= _QTT_Process
                    '--- StorageComputed újraszámolása

                    _StorageComputed = _onew.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? and Products=?", _onew.GetObject(_Storage), _onew.GetObject(_OrderInRows.Products)), True)
                    _QTT_ReservedOut = _StorageComputed.QTTReservedOut
                    _StorageOutRows_Collection_R = New XPCollection(Of StorageOutRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _onew.Session, _
                                            CriteriaOperator.Parse("QTT_Will>0 and StorageOutHeader.Storage=? and Products=?", _onew.GetObject(_Storage), _onew.GetObject(_OrderInRows.Products)))
                    For Each _StorageOutRows_R In _StorageOutRows_Collection_R
                        _QTT_Will_Out += _StorageOutRows_R.QTT_Will
                    Next

                    _StorageComputed.QTTFree = _StorageComputed.QTTFree - (_QTT_Will_Out - _QTT_ReservedOut)
                    _StorageComputed.QTTReservedOut = _QTT_Will_Out
                Next
                _onew.CommitChanges()
                If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                    Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba")
        Finally
            LockingModule.UnLockObject(_uow_lock, GetType(StorageComputed), "-", _User)
        End Try
    End Sub
    Private Sub sis_SOFinal(ByVal _StorageOutHeader As StorageOutHeader, ByVal _ocur As Xpo.XPObjectSpace)
        Dim _StorageComputed As StorageComputed
        Dim _StorageOutRows As StorageOutRows
        Dim _StorageOutRowsInRows_Collection As XPCollection(Of StorageOutRowsInRows)
        Dim _StorageOutRowsInRows As StorageOutRowsInRows
        Dim _QTT_Free_Before As Double = 0
        Dim _QTT_Free_After As Double = 0
        For Each _StorageOutRows In _StorageOutHeader.StorageOutRows
            _StorageOutRowsInRows_Collection = New XPCollection(Of StorageOutRowsInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                  CriteriaOperator.Parse("StorageOutRows=?", _StorageOutRows))

            For Each _StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                _StorageOutRowsInRows.StorageInRows.QTT_Out += _StorageOutRowsInRows.StorageInRows.QTT_Out_Will
                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will = 0
                _StorageOutRowsInRows.StorageInRows.QTT_Will = 0

                '--- Esemény beszúrása !!!!!


            Next

            _StorageOutRows.QTT += _StorageOutRows.QTT_Will

            If _StorageOutRows.OrderInRows IsNot Nothing Then
                _StorageOutRows.OrderInRows.QTT_Storaged += _StorageOutRows.OrderInRows.QTT_Storaged_Will
                _StorageOutRows.OrderInRows.QTT_Storaged_Will = 0
            End If
            _StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _StorageOutRows.Products, _StorageOutRows.StorageOutHeader.Storage), True)
            _StorageComputed.QTTReservedOut -= _StorageOutRows.QTT_Will
            _StorageOutRows.QTT_Will = 0
        Next
        _ocur.CommitChanges()
    End Sub
    Private Sub aStorageOutFromPdaCollection_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aStorageOutFromPdaCollection.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _StoragePDACollectionHeader_Collection As New XPCollection(Of StoragePDACollectionHeader)(_onew.Session)

        Dim _CS As New CollectionSource(_onew, GetType(StorageOutRowsFromPDACollectionPopup))

        For Each _StoragePDACollectionHeader As StoragePDACollectionHeader In _StoragePDACollectionHeader_Collection
            Dim _StorageInRowsFromPDACollectionPopup As StorageOutRowsFromPDACollectionPopup = _onew.CreateObject(Of StorageOutRowsFromPDACollectionPopup)()
            _StorageInRowsFromPDACollectionPopup.StoragePDACollectionHeader = _StoragePDACollectionHeader

            _CS.Add(_StorageInRowsFromPDACollectionPopup)
        Next

        e.View = Application.CreateListView("StorageOutRowsFromPDACollectionPopup_ListView", _CS, False)
    End Sub
    Private Sub aStorageOutFromPdaCollection_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aStorageOutFromPdaCollection.Execute
        Try
            If e.PopupWindow.View.SelectedObjects.Count = 0 Then Throw New Exception("*Nincs kiválasztva egyetlen kollekció sem!" + vbNewLine _
                                                                                    + "A művelet megszakadt.")
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _StorageOutHeader As StorageOutHeader = TryCast(View.SelectedObjects(0), StorageOutHeader)
            For Each _StorageOutRowsFromPDACollectionPopup As StorageOutRowsFromPDACollectionPopup In e.PopupWindow.View.SelectedObjects
                If _StorageOutRowsFromPDACollectionPopup.StoragePDACollectionHeader.StoragePDACollectionRows.Count > 0 Then
                    Dim _StorageComputed As StorageComputed
                    For Each _StoragePDACollectionRows As StoragePDACollectionRows In _StorageOutRowsFromPDACollectionPopup.StoragePDACollectionHeader.StoragePDACollectionRows
                        _StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? AND Products=?", _StorageOutHeader.Storage, _
                                                                                                                              _ocur.GetObjectByKey(Of Products)(_StoragePDACollectionRows.Products.Oid)))

                        If _StorageComputed Is Nothing Then Throw New Exception("*Egy vagy több termékhez nem tartozik betárolás!" + vbNewLine + "A művelet megszakadt.")

                        _StorageOutHeader.row_StorageComputed = _StorageComputed
                        _StorageOutHeader.row_QTT_Will = _StoragePDACollectionRows.QTT
                        Frame.GetController(Of StorageTransactionsOutVC).aToTable_StorageOut.DoExecute()
                        _StorageComputed = Nothing
                    Next
                End If
            Next
            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message, MsgBoxStyle.OkOnly, "Hiba!")
        End Try
    End Sub
    Private Sub aCreatePDACollectionFromStorageOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreatePDACollectionFromStorageOut.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _StorageOutHeader As StorageOutHeader = TryCast(View.SelectedObjects(0), StorageOutHeader)
            If _StorageOutHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a kellemetlenségért elnézést kérünk! A folyamat megszakadt.")
            If _StorageOutHeader.StorageOutRows.Count = 0 Then Throw New Exception("*Nincs egy kitárolási sor sem, így nem készíthető PDA kollekció sem!")
            Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = _ocur.CreateObject(Of StoragePDACollectionHeader)()
            _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eOpen
            _StoragePDACollectionHeader.StoragePDAParameters = _ocur.FindObject(Of StoragePDAParameters) _
                                                            (CriteriaOperator.Parse("CustomersFrom=?", _StorageOutHeader.CustomersFrom))
            _StoragePDACollectionHeader.StorageCollectionType = eStoragePDACollectionType.eIsLocationOut
            For Each _StorageOutRows As StorageOutRows In _StorageOutHeader.StorageOutRows
                If _StorageOutRows.QTT_Will > 0 Then
                    Dim _StoragePDACollectionRows As StoragePDACollectionRows = _ocur.CreateObject(Of StoragePDACollectionRows)()
                    _StoragePDACollectionRows.StoragePDACollectionHeader = _StoragePDACollectionHeader
                    _StoragePDACollectionRows.Products = _StorageOutRows.Products
                    _StoragePDACollectionRows.QTT = _StorageOutRows.QTT_Will
                End If
            Next
            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message, MsgBoxStyle.OkOnly, "Hiba!")
        End Try
    End Sub

    Private Sub aGenerateStorageOutPDACollection_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGenerateStorageOutPDACollection.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _GenerateStorageInPDALocationCollection As GenerateStorageInPDALocationCollection = _onew.CreateObject(Of GenerateStorageInPDALocationCollection)()
            e.View = Application.CreateDetailView(_onew, "GenerateStorageInPDALocationCollection_DetailView", False, _GenerateStorageInPDALocationCollection)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aGenerateStorageOutPDACollection_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGenerateStorageOutPDACollection.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _StorageOutHeader As StorageOutHeader = TryCast(View.SelectedObjects(0), StorageOutHeader)
            Dim _GenerateStorageInPDALocationCollection As GenerateStorageInPDALocationCollection = TryCast(e.PopupWindow.View.SelectedObjects(0), GenerateStorageInPDALocationCollection)
            If _StorageOutHeader Is Nothing Then Throw New Exception("*Nincs kijelölve egyetlen véglegesített betárolás sem!")
            Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = _ocur.CreateObject(Of StoragePDACollectionHeader)()
            _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eOpen
            _StoragePDACollectionHeader.StorageCollectionType = eStoragePDACollectionType.eIsLocationOut
            _StoragePDACollectionHeader.StoragePDAParameters = _ocur.GetObjectByKey(Of StoragePDAParameters)(_GenerateStorageInPDALocationCollection.StoragePDAParameters.Oid)
            If _StorageOutHeader.StorageOutRows.Count > 0 Then
                For Each _StorageOutRows As StorageOutRows In _StorageOutHeader.StorageOutRows
                    If _StorageOutRows.Products.ProductType = eProductType.ePacketNumber Or _
                        _StorageOutRows.Products.ProductType = eProductType.eSerialNumber Then
                        Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(_ocur.Session, CriteriaOperator.Parse("StorageOutRows=?", _StorageOutRows))
                        Dim _StorageComputedLocations_Collection As XPCollection(Of StorageComputedLocations)

                        For Each _StorageOutRowsInRows As StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                            If _StorageOutRowsInRows.StorageInRows.SerialNumber Is Nothing Or _StorageOutRowsInRows.StorageInRows.SerialNumber = "" Then
                                _StorageComputedLocations_Collection = New XPCollection(Of StorageComputedLocations)(_ocur.Session, CriteriaOperator.Parse("PacketNumber=?", _StorageOutRowsInRows.StorageInRows.PacketNumber))
                            End If
                            If _StorageOutRowsInRows.StorageInRows.PacketNumber Is Nothing Or _StorageOutRowsInRows.StorageInRows.PacketNumber = "" Then
                                _StorageComputedLocations_Collection = New XPCollection(Of StorageComputedLocations)(_ocur.Session, CriteriaOperator.Parse("SerialNumber=?", _StorageOutRowsInRows.StorageInRows.SerialNumber))
                            End If

                            If _StorageComputedLocations_Collection.Count > 0 Then Exit For
                        Next

                        If _StorageComputedLocations_Collection.Count = 0 Then Throw New Exception("*Nincs a raktárban elhelyezve a termék!")
                        Dim _QTT As Double = _StorageOutRows.QTT

                        For Each _StorageComputedLocations As StorageComputedLocations In _StorageComputedLocations_Collection
                            Dim _StoragePDACollectionRows As StoragePDACollectionRows = _ocur.CreateObject(Of StoragePDACollectionRows)()
                            _StoragePDACollectionRows.StoragePDACollectionHeader = _StoragePDACollectionHeader
                            _StoragePDACollectionRows.Products = _StorageOutRows.Products
                            _StoragePDACollectionRows.StorageLocation = _StorageComputedLocations.StorageLocation
                            _StoragePDACollectionRows.DateModified = GetSQLTime(_ocur.Session)
                            _StoragePDACollectionRows.SerialNumber = _StorageComputedLocations.SerialNumber
                            _StoragePDACollectionRows.PacketNumber = _StorageComputedLocations.PacketNumber
                            _StoragePDACollectionHeader.StorageOutHeader = _StorageOutHeader

                            If _StorageComputedLocations.QTT >= _QTT Then
                                _StoragePDACollectionRows.QTT = _QTT
                                _StorageComputedLocations.QTT -= _QTT
                                _QTT = 0
                            Else
                                _StoragePDACollectionRows.QTT += _StorageComputedLocations.QTT
                                _QTT -= _StorageComputedLocations.QTT
                                _StorageComputedLocations.QTT = 0
                            End If
                            If _QTT = 0 Then Exit For
                        Next
                    Else
                        Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? AND Storage=?", _StorageOutRows.Products, _StorageOutHeader.Storage))
                        Dim _StorageComputedLocations_Collection As New XPCollection(Of StorageComputedLocations)(_ocur.Session, CriteriaOperator.Parse("StorageComputed=?", _StorageComputed))
                        If _StorageComputedLocations_Collection.Count = 0 Then Throw New Exception("*Nincs elhelyezve termék!")
                        Dim _QTT As Double = _StorageOutRows.QTT

                        For Each _StorageComputedLocations As StorageComputedLocations In _StorageComputedLocations_Collection
                            Dim _StoragePDACollectionRows As StoragePDACollectionRows = _ocur.CreateObject(Of StoragePDACollectionRows)()
                            _StoragePDACollectionRows.StoragePDACollectionHeader = _StoragePDACollectionHeader
                            _StoragePDACollectionRows.Products = _StorageOutRows.Products
                            _StoragePDACollectionRows.StorageLocation = _StorageComputedLocations.StorageLocation
                            _StoragePDACollectionRows.DateModified = GetSQLTime(_ocur.Session)
                            _StoragePDACollectionHeader.StorageOutHeader = _StorageOutHeader

                            If _StorageComputedLocations.QTT >= _QTT Then
                                _StoragePDACollectionRows.QTT = _QTT
                                _StorageComputedLocations.QTT -= _QTT
                                _QTT = 0
                            Else
                                _StoragePDACollectionRows.QTT += _StorageComputedLocations.QTT
                                _QTT -= _StorageComputedLocations.QTT
                                _StorageComputedLocations.QTT = 0
                            End If
                            If _QTT = 0 Then Exit For
                        Next
                    End If
                Next
            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
End Class
