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

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports System.Linq
Imports DevExpress.Xpo.DB

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

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

    End Sub

    Private Sub aGenerateStorageDateClearingPrice_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGenerateStorageDateClearingPrice.Execute
        Try
            GLOBAL_HAS_Audit = False

            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
            Dim _StorageDateClearingPrice As StorageDateClearingPrice
            Dim _Products As Products
            Dim _CustomersFrom_Collection As New XPCollection(Of CustomersFrom)(_uow)

            Dim _Products_Collection As New XPCollection(Of Products)(_uow)
            RaiseEvent InitWork(1, 1, _Products_Collection.Count)
            For Each _Products In _Products_Collection
                RaiseEvent DoWork()

                Dim _StorageDateClearingPrice_Collection As New XPCollection(Of StorageDateClearingPrice)(_uow, CriteriaOperator.Parse("Products=?", _Products))
                _uow.Delete(_StorageDateClearingPrice_Collection)
                _uow.CommitChanges()


                '// Igazolt szállítólevél sorok
                Dim _DeliveryNoteInRRFC_Collection As XPCollection(Of DeliveryNoteInRRFC)
                Dim _DeliveryNoteInRRFC As DeliveryNoteInRRFC
                _DeliveryNoteInRRFC_Collection = New XPCollection(Of DeliveryNoteInRRFC)(_uow,
                                     CriteriaOperator.Parse("DeliveryNoteInRows.Products=?", _Products))

                _DeliveryNoteInRRFC_Collection.Sorting.Add(New SortProperty("GetDate(RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution)", DB.SortingDirection.Ascending))

                For Each _DeliveryNoteInRRFC In _DeliveryNoteInRRFC_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 0
                    _StorageDateClearingPrice.CustomersFrom = _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution.Date
                    _StorageDateClearingPrice.DeliveryNoteInRRFC = _DeliveryNoteInRRFC
                    _StorageDateClearingPrice.Products = _DeliveryNoteInRRFC.DeliveryNoteInRows.Products
                    _StorageDateClearingPrice.QTT_In = _DeliveryNoteInRRFC.QTT_Controlled
                    _uow.CommitChanges()
                Next

                'Kimenő számla sorok
                Dim _InvoiceRows As InvoiceRows
                Dim _InvoiceRows_Collection As New XPCollection(Of InvoiceRows)(_uow,
                                    CriteriaOperator.Parse("isnull(InvoiceHeader.GCRecord)=True and " +
                                                           "InvoiceHeader.IsEditable=False and " +
                                                           "InvoiceHeader.IsStorno=False and " +
                                                           "InvoiceHeader.DocumentNumber !='' and " +
                                                           "OrderInRows.Products.Oid=?", _Products.Oid))

                _InvoiceRows_Collection.Sorting.Add(New SortProperty("GetDate(InvoiceHeader.DateExecution)", DB.SortingDirection.Ascending))

                For Each _InvoiceRows In _InvoiceRows_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 1
                    _StorageDateClearingPrice.CustomersFrom = _InvoiceRows.InvoiceHeader.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _InvoiceRows.InvoiceHeader.DateExecution.Date
                    _StorageDateClearingPrice.InvoiceRows = _InvoiceRows
                    _StorageDateClearingPrice.Products = _InvoiceRows.OrderInRows.Products
                    _StorageDateClearingPrice.QTT_Out = _InvoiceRows.QTT
                    _uow.CommitChanges()
                Next


                '// Gyártásba kiadott tételek kitárolás !
                Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(_uow, CriteriaOperator.Parse("StorageOutRows.StorageOutHeader.StorageMoveType.MoveType in (5,3,4) " + _
                                                                            "AND StorageOutRows.StorageOutHeader.IsEditable = False AND StorageOutRows.StorageOutHeader.DocumentNumber !='' and StorageOutRows.Products.Oid=?", _Products.Oid))

                For Each _StorageOutRowsInRows As StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 1
                    _StorageDateClearingPrice.CustomersFrom = _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.CreateDate.Date
                    _StorageDateClearingPrice.Products = _StorageOutRowsInRows.StorageOutRows.Products
                    _StorageDateClearingPrice.QTT_Out = _StorageOutRowsInRows.QTT
                    _StorageDateClearingPrice.StorageOutRowsInRows = _StorageOutRowsInRows
                    _uow.CommitChanges()
                Next

                '// Gyártásból bevett tételek + vevői jóváírásból bevétel  betárolás !!
                Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(_uow, CriteriaOperator.Parse("StorageInHeader.StorageMoveType.MoveType in (5,3) " + _
                                                                                                    "AND StorageInHeader.IsEditable = False AND StorageInHeader.DocumentNumber !='' and Products.Oid=?", _Products.Oid))

                For Each _StorageInRows As StorageInRows In _StorageInRows_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 0
                    _StorageDateClearingPrice.CustomersFrom = _StorageInRows.StorageInHeader.Storage.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _StorageInRows.StorageInHeader.CreateDate.Date
                    _StorageDateClearingPrice.StorageInRows = _StorageInRows
                    _StorageDateClearingPrice.Products = _StorageInRows.Products
                    _StorageDateClearingPrice.QTT_In = _StorageInRows.QTT
                    _uow.CommitChanges()
                Next


                _uow.CommitChanges()

                For Each _CustomersFrom As CustomersFrom In _CustomersFrom_Collection
                    UpdateStorageDateClearingPrice(_Products, _CustomersFrom, False)
                Next
                _uow.CommitChanges()
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork()
        End Try

    End Sub
    Private Sub aGenerateStorageDateClearingPrice_Products_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aGenerateStorageDateClearingPrice_Products.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As New CollectionSource(_onew, GetType(Products))
        Dim _StorageDateClearingPrice As StorageDateClearingPrice
        Dim _SQL As String = ""

        For Each _StorageDateClearingPrice In View.SelectedObjects
            If _SQL = "" Then
                _SQL = " Oid in ('" & _StorageDateClearingPrice.Products.Oid.ToString & "'"
            Else
                _SQL += ",'" & _StorageDateClearingPrice.Products.Oid.ToString & "'"
            End If
        Next
        If _SQL <> "" Then
            _SQL += ")"
            _CS.BeginUpdateCriteria()
            _CS.Criteria.Clear()
            _CS.Criteria("ByProducts") = CriteriaOperator.Parse(_SQL)
            _CS.EndUpdateCriteria()
        End If

        e.View = Application.CreateListView("Products_ListView_Find", _CS, True)
    End Sub
    Private Sub aGenerateStorageDateClearingPrice_Products_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aGenerateStorageDateClearingPrice_Products.Execute
        Try
            GLOBAL_HAS_Audit = False

            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
            Dim _StorageDateClearingPrice As StorageDateClearingPrice
            Dim _Products As Products
            Dim _SQL As String = ""
            Dim _CustomersFrom_Collection As New XPCollection(Of CustomersFrom)(_uow)

            RaiseEvent InitWork(1, 1, e.PopupWindow.View.SelectedObjects.Count)
            For Each _Products In e.PopupWindow.View.SelectedObjects
                RaiseEvent DoWork()

                Dim _StorageDateClearingPrice_Collection As New XPCollection(Of StorageDateClearingPrice)(_uow, CriteriaOperator.Parse("Products.Oid=?", _Products.Oid))
                _uow.Delete(_StorageDateClearingPrice_Collection)
                _uow.CommitChanges()
                Dim _DeliveryNoteInRRFC_Collection As XPCollection(Of DeliveryNoteInRRFC)
                Dim _DeliveryNoteInRRFC As DeliveryNoteInRRFC
                _DeliveryNoteInRRFC_Collection = New XPCollection(Of DeliveryNoteInRRFC)(_uow,
                                     CriteriaOperator.Parse("DeliveryNoteInRows.Products.Oid=?", _Products.Oid))

                _DeliveryNoteInRRFC_Collection.Sorting.Add(New SortProperty("GetDate(RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution)", DB.SortingDirection.Ascending))

                For Each _DeliveryNoteInRRFC In _DeliveryNoteInRRFC_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 0
                    _StorageDateClearingPrice.CustomersFrom = _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution.Date
                    _StorageDateClearingPrice.DeliveryNoteInRRFC = _DeliveryNoteInRRFC
                    _StorageDateClearingPrice.Products = _DeliveryNoteInRRFC.DeliveryNoteInRows.Products
                    _StorageDateClearingPrice.QTT_In = _DeliveryNoteInRRFC.QTT_Controlled
                Next

                Dim _InvoiceRows As InvoiceRows
                Dim _InvoiceRows_Collection As New XPCollection(Of InvoiceRows)(_uow,
                                    CriteriaOperator.Parse("isnull(InvoiceHeader.GCRecord)=True and " +
                                                           "InvoiceHeader.IsEditable=False and " +
                                                           "InvoiceHeader.IsStorno=False and " +
                                                           "InvoiceHeader.DocumentNumber !='' and " +
                                                           "OrderInRows.Products.Oid=?", _Products.Oid))

                _InvoiceRows_Collection.Sorting.Add(New SortProperty("GetDate(InvoiceHeader.DateExecution)", DB.SortingDirection.Ascending))

                For Each _InvoiceRows In _InvoiceRows_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 1
                    _StorageDateClearingPrice.CustomersFrom = _InvoiceRows.InvoiceHeader.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _InvoiceRows.InvoiceHeader.DateExecution.Date
                    _StorageDateClearingPrice.InvoiceRows = _InvoiceRows
                    _StorageDateClearingPrice.Products = _InvoiceRows.OrderInRows.Products
                    _StorageDateClearingPrice.QTT_Out = _InvoiceRows.QTT
                Next


                '// Gyártásba kiadott tételek kitárolás !
                Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(_uow, CriteriaOperator.Parse("StorageOutRows.StorageOutHeader.StorageMoveType.MoveType in (5,3,4) " + _
                                                                            "AND StorageOutRows.StorageOutHeader.IsEditable = False AND StorageOutRows.StorageOutHeader.DocumentNumber !='' and StorageOutRows.Products.Oid=?", _Products.Oid))

                For Each _StorageOutRowsInRows As StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 1
                    _StorageDateClearingPrice.CustomersFrom = _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.CreateDate.Date
                    _StorageDateClearingPrice.Products = _StorageOutRowsInRows.StorageOutRows.Products
                    _StorageDateClearingPrice.QTT_Out = _StorageOutRowsInRows.QTT
                    _StorageDateClearingPrice.StorageOutRowsInRows = _StorageOutRowsInRows
                Next

                '// Gyártásból bevett tételek + vevői jóváírásból betárolás !!
                Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(_uow, CriteriaOperator.Parse("StorageInHeader.StorageMoveType.MoveType in (5,3) " + _
                                                                                                    "AND StorageInHeader.IsEditable = False AND StorageInHeader.DocumentNumber !='' and Products.Oid=?", _Products.Oid))

                For Each _StorageInRows As StorageInRows In _StorageInRows_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 0
                    _StorageDateClearingPrice.CustomersFrom = _StorageInRows.StorageInHeader.Storage.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _StorageInRows.StorageInHeader.CreateDate.Date
                    _StorageDateClearingPrice.StorageInRows = _StorageInRows
                    _StorageDateClearingPrice.Products = _StorageInRows.Products
                    _StorageDateClearingPrice.QTT_In = _StorageInRows.QTT
                Next

                _uow.CommitChanges()

                For Each _CustomersFrom As CustomersFrom In _CustomersFrom_Collection
                    UpdateStorageDateClearingPrice(_Products, _CustomersFrom, False)
                Next

            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork()
        End Try

    End Sub
    Private Sub aGenerateStorageDate_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGenerateStorageDate.Execute
        Try
            GLOBAL_HAS_Audit = False

            Dim _uow As New UnitOfWork(TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session.DataLayer)
            Dim _StorageDate_Collection As New XPCollection(Of StorageDate)(_uow)

            _uow.Delete(_StorageDate_Collection)
            _uow.CommitChanges()


            Dim _Products_Collection As New XPCollection(Of Products)(_uow)

            RaiseEvent InitWork(1, 1, _Products_Collection.Count)
            For Each _Products In _Products_Collection
                RaiseEvent DoWork()

                Dim _DateArray As ArrayList = GenerateDateCollection(New Date(2013, 1, 1), GetSQLTime(_uow))

                For Each _Date As Date In _DateArray
                    Dim _StorageDateClearingPrice_Collection As New XPCollection(Of StorageDateClearingPrice) _
                            (_uow, CriteriaOperator.Parse("Products.Oid=? and GetDate(DateExecution)<=? and RowIndex=1",
                                                          _Products.Oid, _Date))

                    Dim _StorageDateClearingPrice As StorageDateClearingPrice
                    _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("GetDate(DateExecution)", DB.SortingDirection.Descending))
                    _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("QTT_After", DB.SortingDirection.Ascending))

                    If _StorageDateClearingPrice_Collection.Count <= 0 Then
                        _StorageDateClearingPrice_Collection = New XPCollection(Of StorageDateClearingPrice) _
                        (_uow, CriteriaOperator.Parse("Products.Oid=? and GetDate(DateExecution)<=?  and RowIndex=0",
                                                      _Products.Oid, _Date))
                        _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("GetDate(DateExecution)", DB.SortingDirection.Descending))
                        _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("QTT_After", DB.SortingDirection.Descending))
                    End If

                    For Each _StorageDateClearingPrice In _StorageDateClearingPrice_Collection
                        Dim _StorageDate As New StorageDate(_uow)
                        _StorageDate.CustomersFrom = _StorageDateClearingPrice.CustomersFrom
                        _StorageDate.DateExecution = _Date
                        _StorageDate.Products = _StorageDateClearingPrice.Products
                        _StorageDate.QTT_Close = _StorageDateClearingPrice.QTT_After
                        _StorageDate.SumAveragePriceHUF = _StorageDateClearingPrice.AveragePriceHUF_After
                        _StorageDate.SumPriceHUF = _StorageDate.QTT_Close * _StorageDateClearingPrice.AveragePriceHUF_After
                        _uow.CommitChanges()
                        Exit For
                    Next
                Next


                _DateArray = GenerateDateCollection(New Date(2013, 1, 1), GetSQLTime(_uow), True)

                For Each _Date As Date In _DateArray
                    Dim _StorageDateClearingPrice_Collection As New XPCollection(Of StorageDateClearingPrice) _
                            (_uow, CriteriaOperator.Parse("Products.Oid=? and GetDate(DateExecution)<=? and RowIndex=1",
                                                          _Products.Oid, _Date))

                    Dim _StorageDateClearingPrice As StorageDateClearingPrice
                    _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("GetDate(DateExecution)", DB.SortingDirection.Descending))
                    _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("QTT_After", DB.SortingDirection.Ascending))

                    If _StorageDateClearingPrice_Collection.Count <= 0 Then
                        _StorageDateClearingPrice_Collection = New XPCollection(Of StorageDateClearingPrice) _
                        (_uow, CriteriaOperator.Parse("Products.Oid=? and GetDate(DateExecution)<=?  and RowIndex=0",
                                                      _Products.Oid, _Date))
                        _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("GetDate(DateExecution)", DB.SortingDirection.Descending))
                        _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("QTT_After", DB.SortingDirection.Descending))
                    End If

                    For Each _StorageDateClearingPrice In _StorageDateClearingPrice_Collection
                        If _uow.FindObject(Of StorageDate)(CriteriaOperator.Parse("CustomersFrom=? AND Products=? AND DateExecution=?", _StorageDateClearingPrice.CustomersFrom, _StorageDateClearingPrice.Products, _Date)) Is Nothing Then
                            Dim _StorageDate As New StorageDate(_uow)
                            _StorageDate.CustomersFrom = _StorageDateClearingPrice.CustomersFrom
                            _StorageDate.DateExecution = _Date
                            _StorageDate.Products = _StorageDateClearingPrice.Products
                            _StorageDate.QTT_Close = _StorageDateClearingPrice.QTT_After
                            _StorageDate.SumAveragePriceHUF = _StorageDateClearingPrice.AveragePriceHUF_After
                            _StorageDate.SumPriceHUF = _StorageDate.QTT_Close * _StorageDateClearingPrice.AveragePriceHUF_After
                            _uow.CommitChanges()
                            Exit For
                        End If
                    Next
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork()
        End Try
    End Sub
    Private Sub aStorageDateClearingPrice_ViewAttachments_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aStorageDateClearingPrice_ViewAttachments.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _StorageDateClearingPrice As StorageDateClearingPrice = TryCast(View.SelectedObjects(0), StorageDateClearingPrice)

        If _StorageDateClearingPrice.DeliveryNoteInRRFC IsNot Nothing Then
            ViewAttachments.ViewAttachments(_ocur, Frame, _StorageDateClearingPrice.DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.CustomersFrom, _
                                           _StorageDateClearingPrice.DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.Customers, _
                                           _StorageDateClearingPrice.DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.DocumentNumber)

        End If
        If _StorageDateClearingPrice.InvoiceRows IsNot Nothing Then
            ViewAttachments.ViewAttachments(_ocur, Frame, _StorageDateClearingPrice.InvoiceRows.InvoiceHeader.CustomersFrom, _
                                            _StorageDateClearingPrice.InvoiceRows.InvoiceHeader.Customers, _
                                            _StorageDateClearingPrice.InvoiceRows.InvoiceHeader.DocumentNumber)

        End If
    End Sub
    Private Sub aStorageDateClearingPrice_DateAndValueReCalculate_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aStorageDateClearingPrice_DateAndValueReCalculate.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _StorageDateClearingPrice_Selected As StorageDateClearingPrice = TryCast(View.SelectedObjects(0), StorageDateClearingPrice)

        UpdateStorageDateClearingPrice(_StorageDateClearingPrice_Selected.Products, _StorageDateClearingPrice_Selected.CustomersFrom, True)

        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RefreshController).RefreshAction.DoExecute()
    End Sub
    Public Sub UpdateStorageDateClearingPrice_SQL(ByVal _Products As Products, _CustomersFrom As CustomersFrom, Optional _NeedLine As Boolean = True)
        Dim _SelectedData As SelectedData = Nothing
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _SQL As String = ""


        _SQL = String.Format("select [] from [StorageDateClearingPrice] Where [CustomersFrom]='{0}' and [Products]='{1}'", _CustomersFrom.Oid.ToString, _Products.Oid.ToString)
        _SelectedData = _uow.ExecuteQuery(_SQL)

        For Each row As SelectStatementResultRow In _SelectedData.ResultSet(0).Rows
            'Console.WriteLine(String.Format("{0}" & Constants.vbTab & "{1}", row.Values(0), row.Values(1)))
        Next row
    End Sub
    Public Sub UpdateStorageDateClearingPrice(ByVal _Products As Products, _CustomersFrom As CustomersFrom, Optional _NeedLine As Boolean = True)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow_update As New UnitOfWork(_onew.Session.DataLayer)
        Dim _uow_xpcollection As New UnitOfWork(_onew.Session.DataLayer)
        Dim _uow_nothing As New UnitOfWork(_onew.Session.DataLayer)

        Dim _StorageDateClearingPrice As StorageDateClearingPrice = Nothing

        Dim _SQL As String = ""
        Dim _ItemIndex As Long = 0
        Dim _QTT_Before As Double = 0
        Dim _QTT_After As Double = 0
        Dim _AveragePriceHUF_After As Double = 0
        Dim _SumAveragePriceHUF_After As Double = 0

        Dim _StorageDateClearingPrice_Collection As New XPCollection(Of StorageDateClearingPrice)(_uow_xpcollection, CriteriaOperator.Parse("Products.Oid=? AND CustomersFrom.Oid=?", _
                                                                    _Products.Oid, _CustomersFrom.Oid))
        _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("DateExecution", DB.SortingDirection.Ascending))
        _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))
        '  Try
        'If _NeedLine Then RaiseEvent InitWork(1, 1, _StorageDateClearingPrice_Collection.Count)
        'If _StorageDateClearingPrice_Collection.Count > 0 Then

        For Each _StorageDateClearingPrice In _StorageDateClearingPrice_Collection
            'If _NeedLine Then RaiseEvent DoWork()
            _StorageDateClearingPrice = _uow_nothing.GetObjectByKey(Of StorageDateClearingPrice)(_StorageDateClearingPrice.Oid)

            If _ItemIndex > 0 Then
                _StorageDateClearingPrice.AveragePriceHUF_Before = _AveragePriceHUF_After
                _StorageDateClearingPrice.QTT_Before = _QTT_After
                _StorageDateClearingPrice.SumAveragePriceHUF_Before = Math.Round(_StorageDateClearingPrice.QTT_Before * _StorageDateClearingPrice.AveragePriceHUF_Before)


                If _StorageDateClearingPrice.QTT_In <> 0 Then
                    _QTT_After = _StorageDateClearingPrice.QTT_Before + _StorageDateClearingPrice.QTT_In
                    If _StorageDateClearingPrice.DeliveryNoteInRRFC IsNot Nothing Then
                        _StorageDateClearingPrice.SumAveragePriceHUF_After = _StorageDateClearingPrice.SumAveragePriceHUF_Before + (_StorageDateClearingPrice.DeliveryNoteInRRFC.RegistryRowsFinancialControlling.Amount - _StorageDateClearingPrice.DeliveryNoteInRRFC.BonusHUF)
                    ElseIf _StorageDateClearingPrice.StorageInRows IsNot Nothing Then
                        _StorageDateClearingPrice.SumAveragePriceHUF_After = _StorageDateClearingPrice.SumAveragePriceHUF_Before + (_StorageDateClearingPrice.StorageInRows.AveragePriceInHUF * _StorageDateClearingPrice.StorageInRows.QTT)
                    End If
                    If _QTT_After <> 0 Then
                        _StorageDateClearingPrice.AveragePriceHUF_After = Math.Round(_StorageDateClearingPrice.SumAveragePriceHUF_After / _QTT_After, 0, MidpointRounding.AwayFromZero)
                    Else
                        _StorageDateClearingPrice.AveragePriceHUF_After = 0
                    End If

                    _AveragePriceHUF_After = _StorageDateClearingPrice.AveragePriceHUF_After
                    _SumAveragePriceHUF_After = _StorageDateClearingPrice.SumAveragePriceHUF_After
                End If
                If _StorageDateClearingPrice.QTT_Out <> 0 Then
                    _QTT_After = _StorageDateClearingPrice.QTT_Before - _StorageDateClearingPrice.QTT_Out

                    If _QTT_After = 0 Then
                        _StorageDateClearingPrice.AveragePriceHUF_After = _AveragePriceHUF_After
                        _AveragePriceHUF_After = 0
                        _SumAveragePriceHUF_After = 0
                    Else
                        _StorageDateClearingPrice.AveragePriceHUF_After = _AveragePriceHUF_After
                    End If

                    _StorageDateClearingPrice.SumAveragePriceHUF_After = _StorageDateClearingPrice.AveragePriceHUF_After * _QTT_After
                End If
                _StorageDateClearingPrice.QTT_After = _QTT_After
            Else
                _StorageDateClearingPrice.QTT_Before = 0
                If _StorageDateClearingPrice.QTT_In <> 0 Then
                    _QTT_After = _StorageDateClearingPrice.QTT_Before + _StorageDateClearingPrice.QTT_In
                End If
                If _StorageDateClearingPrice.QTT_Out <> 0 Then
                    _QTT_After = _StorageDateClearingPrice.QTT_Before - _StorageDateClearingPrice.QTT_Out
                End If
                _StorageDateClearingPrice.QTT_After = _QTT_After

                _StorageDateClearingPrice.AveragePriceHUF_Before = 0
                _StorageDateClearingPrice.SumAveragePriceHUF_Before = 0
                '// Itt kell 2.
                If _StorageDateClearingPrice.DeliveryNoteInRRFC IsNot Nothing Then
                    _SumAveragePriceHUF_After = (_StorageDateClearingPrice.DeliveryNoteInRRFC.RegistryRowsFinancialControlling.Amount - _StorageDateClearingPrice.DeliveryNoteInRRFC.BonusHUF)
                    If _StorageDateClearingPrice.QTT_After <> 0 Then
                        _AveragePriceHUF_After = _SumAveragePriceHUF_After / _StorageDateClearingPrice.QTT_After
                    Else
                        _AveragePriceHUF_After = 0
                    End If


                    _StorageDateClearingPrice.SumAveragePriceHUF_After = _SumAveragePriceHUF_After
                    _StorageDateClearingPrice.AveragePriceHUF_After = _AveragePriceHUF_After

                ElseIf _StorageDateClearingPrice.StorageInRows IsNot Nothing Then
                    _SumAveragePriceHUF_After = _StorageDateClearingPrice.StorageInRows.AveragePriceInHUF * _StorageDateClearingPrice.StorageInRows.QTT
                    If _StorageDateClearingPrice.QTT_After <> 0 Then
                        _AveragePriceHUF_After = _SumAveragePriceHUF_After / _StorageDateClearingPrice.QTT_After
                    Else
                        _AveragePriceHUF_After = 0
                    End If


                    _StorageDateClearingPrice.SumAveragePriceHUF_After = _SumAveragePriceHUF_After
                    _StorageDateClearingPrice.AveragePriceHUF_After = _AveragePriceHUF_After
                Else
                    _StorageDateClearingPrice.SumAveragePriceHUF_After = 0
                    _StorageDateClearingPrice.AveragePriceHUF_After = 0

                    _SumAveragePriceHUF_After = 0
                    _AveragePriceHUF_After = 0
                End If

            End If
            _StorageDateClearingPrice.MainIndex = _ItemIndex

            _ItemIndex += 1

            Select Case GLOBAL_SQLType
                Case eSQLType.MSSQL
                    _SQL = "update StorageDateClearingPrice set "
                    _SQL = String.Format("{0} QTT_Before={1},", _SQL, SQLNum(_StorageDateClearingPrice.QTT_Before.ToString))
                    _SQL = String.Format("{0} QTT_After={1},", _SQL, SQLNum(_StorageDateClearingPrice.QTT_After.ToString))
                    _SQL = String.Format("{0} AveragePriceHUF_Before={1},", _SQL, SQLNum(_StorageDateClearingPrice.AveragePriceHUF_Before.ToString))
                    _SQL = String.Format("{0} SumAveragePriceHUF_Before={1},", _SQL, SQLNum(_StorageDateClearingPrice.SumAveragePriceHUF_Before.ToString))
                    _SQL = String.Format("{0} AveragePriceHUF_After={1},", _SQL, SQLNum(_StorageDateClearingPrice.AveragePriceHUF_After.ToString))
                    _SQL = String.Format("{0} SumAveragePriceHUF_After={1},", _SQL, SQLNum(_StorageDateClearingPrice.SumAveragePriceHUF_After.ToString))
                    _SQL = String.Format("{0} MainIndex={1}", _SQL, SQLNum(_StorageDateClearingPrice.MainIndex.ToString))
                    _SQL += String.Format(" Where Oid='{0}'", _StorageDateClearingPrice.Oid)
                Case eSQLType.MySQL
                    _SQL = "update StorageDateClearingPrice set "
                    _SQL = String.Format("{0} QTT_Before={1},", _SQL, SQLNum(_StorageDateClearingPrice.QTT_Before.ToString))
                    _SQL = String.Format("{0} QTT_After={1},", _SQL, SQLNum(_StorageDateClearingPrice.QTT_After.ToString))
                    _SQL = String.Format("{0} AveragePriceHUF_Before={1},", _SQL, SQLNum(_StorageDateClearingPrice.AveragePriceHUF_Before.ToString))
                    _SQL = String.Format("{0} SumAveragePriceHUF_Before={1},", _SQL, SQLNum(_StorageDateClearingPrice.SumAveragePriceHUF_Before.ToString))
                    _SQL = String.Format("{0} AveragePriceHUF_After={1},", _SQL, SQLNum(_StorageDateClearingPrice.AveragePriceHUF_After.ToString))
                    _SQL = String.Format("{0} SumAveragePriceHUF_After={1},", _SQL, SQLNum(_StorageDateClearingPrice.SumAveragePriceHUF_After.ToString))
                    _SQL = String.Format("{0} MainIndex={1}", _SQL, SQLNum(_StorageDateClearingPrice.MainIndex.ToString))
                    _SQL += String.Format(" Where Oid='{0}'", _StorageDateClearingPrice.Oid)
                Case eSQLType.PostgreSQL
                    _SQL = "update ""StorageDateClearingPrice"" set "
                    _SQL = String.Format("{0} ""QTT_Before""={1},", _SQL, SQLNum(_StorageDateClearingPrice.QTT_Before.ToString))
                    _SQL = String.Format("{0} ""QTT_After""={1},", _SQL, SQLNum(_StorageDateClearingPrice.QTT_After.ToString))
                    _SQL = String.Format("{0} ""AveragePriceHUF_Before""={1},", _SQL, SQLNum(_StorageDateClearingPrice.AveragePriceHUF_Before.ToString))
                    _SQL = String.Format("{0} ""SumAveragePriceHUF_Before""={1},", _SQL, SQLNum(_StorageDateClearingPrice.SumAveragePriceHUF_Before.ToString))
                    _SQL = String.Format("{0} ""AveragePriceHUF_After""={1},", _SQL, SQLNum(_StorageDateClearingPrice.AveragePriceHUF_After.ToString))
                    _SQL = String.Format("{0} ""SumAveragePriceHUF_After""={1},", _SQL, SQLNum(_StorageDateClearingPrice.SumAveragePriceHUF_After.ToString))
                    _SQL = String.Format("{0} ""MainIndex""={1}", _SQL, SQLNum(_StorageDateClearingPrice.MainIndex.ToString))
                    _SQL += String.Format(" Where ""Oid""='{0}'", _StorageDateClearingPrice.Oid)
            End Select
            _uow_update.ExecuteNonQuery(_SQL)

            If _StorageDateClearingPrice.InvoiceRows IsNot Nothing Then
                _SQL = ""
                Select Case GLOBAL_SQLType
                    Case eSQLType.MSSQL
                        _SQL = String.Format("update InvoiceRows set SumAveragePriceHUF=QTT*{0},", SQLNum(Math.Round(_StorageDateClearingPrice.AveragePriceHUF_After, 0, MidpointRounding.AwayFromZero).ToString))
                        _SQL = _SQL & " SumGrossMarginHUF=SumPriceHUF-SumAveragePriceHUF "
                        _SQL += String.Format(" Where Oid='{0}'", _StorageDateClearingPrice.InvoiceRows.Oid)
                    Case eSQLType.MySQL
                        _SQL = String.Format("update InvoiceRows set SumAveragePriceHUF=QTT*{0},", SQLNum(Math.Round(_StorageDateClearingPrice.AveragePriceHUF_After, 0, MidpointRounding.AwayFromZero).ToString))
                        _SQL = _SQL & " SumGrossMarginHUF=SumPriceHUF-SumAveragePriceHUF "
                        _SQL += String.Format(" Where Oid='{0}'", _StorageDateClearingPrice.InvoiceRows.Oid)
                    Case eSQLType.PostgreSQL
                        _SQL = String.Format("update ""InvoiceRows"" set ""SumAveragePriceHUF""=""QTT""*{0},", SQLNum(Math.Round(_StorageDateClearingPrice.AveragePriceHUF_After, 0, MidpointRounding.AwayFromZero).ToString))
                        _SQL = _SQL & " ""SumGrossMarginHUF""=""SumPriceHUF""-""SumAveragePriceHUF"" "
                        _SQL += String.Format(" Where ""Oid""='{0}'", _StorageDateClearingPrice.InvoiceRows.Oid)
                End Select
                _uow_update.ExecuteNonQuery(_SQL)
            End If
        Next
        'End If
        '_uow.CommitChanges()
        'Catch ex As Exception
        '    MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        'Finally
        'If _NeedLine Then RaiseEvent FinalWork
        'End Try
    End Sub
    Private Sub aStorageDateClearingPrice_ShowStorageOutDeliveryOutRows_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aStorageDateClearingPrice_ShowStorageOutDeliveryOutRows.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _StorageDateClearingPrice As StorageDateClearingPrice = _onew.GetObject(TryCast(View.SelectedObjects(0), StorageDateClearingPrice))

        If _StorageDateClearingPrice IsNot Nothing Then
            If _StorageDateClearingPrice.InvoiceRows.OrderInRows IsNot Nothing Then
                Dim _ShowStorageOutDeliveryOutRows_PopUp As New ShowStorageOutDeliveryOutRows_PopUp(_onew.Session)
                _ShowStorageOutDeliveryOutRows_PopUp.OrderInRows = _StorageDateClearingPrice.InvoiceRows.OrderInRows

                e.View = Application.CreateDetailView(_onew, "ShowStorageOutDeliveryOutRows_PopUp_DetailView", False, _ShowStorageOutDeliveryOutRows_PopUp)
            End If
        End If
    End Sub
    Private Sub aStorageDateClearingPrice_ShowStorageOutDeliveryOutRows_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aStorageDateClearingPrice_ShowStorageOutDeliveryOutRows.Execute
    End Sub
    Private Sub aStorageDateClearingPrice_GeneterateDate2Date_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aStorageDateClearingPrice_GeneterateDate2Date.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _StorageDateClearingPrice_GeneterateDate2Date_PopUp As New StorageDateClearingPrice_GeneterateDate2Date_PopUp(_onew.Session)

        e.View = Application.CreateDetailView(_onew, "StorageDateClearingPrice_GeneterateDate2Date_PopUp_DetailView", False, _StorageDateClearingPrice_GeneterateDate2Date_PopUp)
    End Sub
    Private Sub aStorageDateClearingPrice_GeneterateDate2Date_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aStorageDateClearingPrice_GeneterateDate2Date.Execute
        Try
            GLOBAL_HAS_Audit = False

            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
            Dim _StorageDateClearingPrice As StorageDateClearingPrice
            Dim _Products As Products
            Dim _CustomersFrom_Collection As New XPCollection(Of CustomersFrom)(_uow)
            Dim _StartDate As Date = TryCast(e.PopupWindow.View.SelectedObjects(0), StorageDateClearingPrice_GeneterateDate2Date_PopUp).StartDate
            Dim _EndDate As Date = TryCast(e.PopupWindow.View.SelectedObjects(0), StorageDateClearingPrice_GeneterateDate2Date_PopUp).EndDate

            Dim _Products_Collection As New XPCollection(Of Products)(_uow)
            RaiseEvent InitWork(1, 1, _Products_Collection.Count)
            For Each _Products In _Products_Collection
                RaiseEvent DoWork()

                Dim _StorageDateClearingPrice_Collection As New XPCollection(Of StorageDateClearingPrice)(_uow, CriteriaOperator.Parse("Products=? AND DateExecution>=? AND DateExecution<=?", _
                                                                                                                                      _Products, _StartDate, _EndDate))
                _uow.Delete(_StorageDateClearingPrice_Collection)
                _uow.CommitChanges()

                Dim _DeliveryNoteInRRFC_Collection As XPCollection(Of DeliveryNoteInRRFC)
                Dim _DeliveryNoteInRRFC As DeliveryNoteInRRFC
                _DeliveryNoteInRRFC_Collection = New XPCollection(Of DeliveryNoteInRRFC)(_uow,
                                     CriteriaOperator.Parse("DeliveryNoteInRows.Products=? AND RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution>=? AND RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution<=?", _
                                                                                                        _Products, _StartDate, _EndDate))

                _DeliveryNoteInRRFC_Collection.Sorting.Add(New SortProperty("GetDate(RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution)", DB.SortingDirection.Ascending))

                For Each _DeliveryNoteInRRFC In _DeliveryNoteInRRFC_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 0
                    _StorageDateClearingPrice.CustomersFrom = _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution.Date
                    _StorageDateClearingPrice.DeliveryNoteInRRFC = _DeliveryNoteInRRFC
                    _StorageDateClearingPrice.Products = _DeliveryNoteInRRFC.DeliveryNoteInRows.Products
                    _StorageDateClearingPrice.QTT_In = _DeliveryNoteInRRFC.QTT_Controlled
                Next

                Dim _InvoiceRows As InvoiceRows
                Dim _InvoiceRows_Collection As New XPCollection(Of InvoiceRows)(_uow,
                                    CriteriaOperator.Parse("isnull(InvoiceHeader.GCRecord)=True and " +
                                                           "InvoiceHeader.IsEditable=False and " +
                                                            "InvoiceHeader.IsStorno=False and " +
                                                           "InvoiceHeader.DocumentNumber !='' and " +
                                                           "OrderInRows.Products.Oid=? AND InvoiceHeader.DateExecution>=? AND InvoiceHeader.DateExecution<=?", _Products.Oid, _StartDate, _EndDate))

                _InvoiceRows_Collection.Sorting.Add(New SortProperty("GetDate(InvoiceHeader.DateExecution)", DB.SortingDirection.Ascending))

                For Each _InvoiceRows In _InvoiceRows_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 1
                    _StorageDateClearingPrice.CustomersFrom = _InvoiceRows.InvoiceHeader.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _InvoiceRows.InvoiceHeader.DateExecution.Date
                    _StorageDateClearingPrice.InvoiceRows = _InvoiceRows
                    _StorageDateClearingPrice.Products = _InvoiceRows.OrderInRows.Products
                    _StorageDateClearingPrice.QTT_Out = _InvoiceRows.QTT
                Next

                '// Gyártásba kiadott tételek kitárolás !
                Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(_uow, CriteriaOperator.Parse("StorageOutRows.StorageOutHeader.StorageMoveType.MoveType in (5,3,4) " + _
                                                                            "AND StorageOutRows.StorageOutHeader.IsEditable = False AND StorageOutRows.StorageOutHeader.DocumentNumber !='' and StorageOutRows.Products.Oid=? " + _
                                                                            "AND StorageOutRows.StorageOutHeader.CreateDate>=? AND StorageOutRows.StorageOutHeader.CreateDate<=?", _Products.Oid, _StartDate, _EndDate))

                For Each _StorageOutRowsInRows As StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 1
                    _StorageDateClearingPrice.CustomersFrom = _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.CreateDate.Date
                    _StorageDateClearingPrice.Products = _StorageOutRowsInRows.StorageOutRows.Products
                    _StorageDateClearingPrice.QTT_Out = _StorageOutRowsInRows.QTT
                    _StorageDateClearingPrice.StorageOutRowsInRows = _StorageOutRowsInRows
                Next

                '// Gyártásból bevett tételek + vevői jóváírásból betárolás !!
                Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(_uow, CriteriaOperator.Parse("StorageInHeader.StorageMoveType.MoveType in (5,3) " + _
                                                                                                    "AND StorageInHeader.IsEditable = False AND StorageInHeader.DocumentNumber !='' and Products.Oid=?" + _
                                                                                                    " and StorageInHeader.CreateDate>=? AND StorageInHeader.CreateDate<=?", _Products.Oid, _StartDate, _EndDate))

                For Each _StorageInRows As StorageInRows In _StorageInRows_Collection
                    _StorageDateClearingPrice = New StorageDateClearingPrice(_uow)
                    _StorageDateClearingPrice.RowIndex = 0
                    _StorageDateClearingPrice.CustomersFrom = _StorageInRows.StorageInHeader.Storage.CustomersFrom
                    _StorageDateClearingPrice.DateExecution = _StorageInRows.StorageInHeader.CreateDate.Date
                    _StorageDateClearingPrice.StorageInRows = _StorageInRows
                    _StorageDateClearingPrice.Products = _StorageInRows.Products
                    _StorageDateClearingPrice.QTT_In = _StorageInRows.QTT
                Next

                _uow.CommitChanges()

                For Each _CustomersFrom As CustomersFrom In _CustomersFrom_Collection
                    UpdateStorageDateClearingPrice(_Products, _CustomersFrom, False)
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork()
        End Try
    End Sub

    Private Sub aStorageDateClearingPrice_SetSIRPrice_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aStorageDateClearingPrice_SetSIRPrice.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _SetSIRPrice_PopUp As New SetSIRPrice_PopUp(_onew.Session)

        e.View = Application.CreateDetailView(_onew, "SetSIRPrice_PopUp_DetailView", False, _SetSIRPrice_PopUp)
    End Sub
    Private Sub aStorageDateClearingPrice_SetSIRPrice_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aStorageDateClearingPrice_SetSIRPrice.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _StorageDateClearingPrice As StorageDateClearingPrice = TryCast(View.SelectedObjects(0), StorageDateClearingPrice)
        Dim _SetSIRPrice_PopUp As SetSIRPrice_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), SetSIRPrice_PopUp)

        If _StorageDateClearingPrice.StorageInRows IsNot Nothing Then
            _StorageDateClearingPrice.StorageInRows.AveragePriceInHUF = _SetSIRPrice_PopUp.AveragePriceInHUF
        End If

        _ocur.CommitChanges()

        Frame.GetController(Of StorageDateVC).aStorageDateClearingPrice_DateAndValueReCalculate.DoExecute()
    End Sub

    Private Sub aSetBonusHUF_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aSetBonusHUF.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _DeliveryNoteInRRFC_Popup As DeliveryNoteInRRFC_Popup = _onew.CreateObject(Of DeliveryNoteInRRFC_Popup)()


        e.View = Application.CreateDetailView(_onew, "DeliveryNoteInRRFC_Popup_DetailView", False, _DeliveryNoteInRRFC_Popup)
    End Sub

    Private Sub aSetBonusHUF_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aSetBonusHUF.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInRRFC_Popup As DeliveryNoteInRRFC_Popup = e.PopupWindow.View.SelectedObjects(0)
        Dim _DeliveryNoteInRRFC As DeliveryNoteInRRFC
        Dim _TotalAmount As Double = 0

        For Each _DeliveryNoteInRRFC In View.SelectedObjects
            _TotalAmount += _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.Amount
        Next

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _DeliveryNoteInRRFC In View.SelectedObjects
            RaiseEvent DoWork()
            _DeliveryNoteInRRFC.BonusHUF = Math.Round(_DeliveryNoteInRRFC.RegistryRowsFinancialControlling.Amount * (_DeliveryNoteInRRFC_Popup.BonusHUF / _TotalAmount), 0, MidpointRounding.AwayFromZero)
            _ocur.CommitChanges()
            UpdateStorageDateClearingPrice(_DeliveryNoteInRRFC.DeliveryNoteInRows.Products, _DeliveryNoteInRRFC.RegistryRowsFinancialControlling.RegistryHeader.CustomersFrom, False)
        Next
        RaiseEvent FinalWork()
    End Sub


End Class
