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

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Utils
Imports System.IO
Imports DevExpress.Persistent.BaseImpl

Public Class InventoryVC
    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()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of InventoryVC).aCreateInventoryHeader.Active.SetItemValue("", False)
        Frame.GetController(Of InventoryVC).aCloseInventoryHeader.Active.SetItemValue("", False)
        Frame.GetController(Of InventoryVC).aCreatePDACollection.Active.SetItemValue("", False)
        Frame.GetController(Of InventoryVC).aCreateDifferent.Active.SetItemValue("", False)

        If View.Id = "InventoryHeader_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            ' Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False) Teszt miatt ideiglenesen enedélyezett

            Frame.GetController(Of InventoryVC).aCreateInventoryHeader.Active.SetItemValue("", True)
            Frame.GetController(Of InventoryVC).aCloseInventoryHeader.Active.SetItemValue("", True)
        End If
        If View.Id = "InventoryHeader_InventorySC_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
            Frame.GetController(Of InventoryVC).aCreatePDACollection.Active.SetItemValue("", True)
        End If
        If View.Id = "InventoryHeader_InventorySIR_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
        End If
        If View.Id = "InventoryHeader_InventoryPDA_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
        End If
        If View.Id = "InventoryHeader_InventoryDifferent_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
            Frame.GetController(Of InventoryVC).aCreateDifferent.Active.SetItemValue("", True)
        End If
        If View.Id = "Storage_LookupListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "InventoryParameters_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        '--------------DeatilViewZ-------------
        If View.Id = "InventoryParameters_DetailView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "InventoryHeader_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)

            Frame.GetController(Of InventoryVC).aCreateInventoryHeader.Active.SetItemValue("", False)
            Frame.GetController(Of InventoryVC).aCloseInventoryHeader.Active.SetItemValue("", False)
        End If
        If View.Id = "Storage_LookupListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "InventoryHeader_InventorySC_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
            Frame.GetController(Of InventoryVC).aCreatePDACollection.Active.SetItemValue("", False)
        End If
        If View.Id = "InventoryHeader_InventorySIR_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
        End If
        If View.Id = "InventoryHeader_InventoryPDA_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
        End If
        If View.Id = "InventoryHeader_InventoryDifferent_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
            Frame.GetController(Of InventoryVC).aCreateDifferent.Active.SetItemValue("", False)
        End If
        If View.Id = "InventoryParameters_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        '--------------DeatilViewZ-------------
        If View.Id = "InventoryHeader_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "InventoryParameters_DetailView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aCreateInventoryHeader_Cancel(sender As Object, e As System.EventArgs) Handles aCreateInventoryHeader.Cancel
    End Sub
    Private Sub aCreateInventoryHeader_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateInventoryHeader.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CreateInventoryHeader_PopUp As CreateInventoryHeader_PopUp = _onew.CreateObject(Of CreateInventoryHeader_PopUp)()

        e.View = Application.CreateDetailView(_onew, "CreateInventoryHeader_PopUp_DetailView", False, _CreateInventoryHeader_PopUp)
    End Sub
    Private Sub aCreateInventoryHeader_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateInventoryHeader.Execute
        Try
            GLOBAL_HAS_Audit = False

            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _CreateInventoryHeader_PopUp As CreateInventoryHeader_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), CreateInventoryHeader_PopUp)
            Dim _InventoryHeader As InventoryHeader = _ocur.CreateObject(Of InventoryHeader)()
            With _InventoryHeader
                .Storage = _ocur.GetObject(_CreateInventoryHeader_PopUp.Storage)
                .DateClose = _CreateInventoryHeader_PopUp.DateClose
                .InventoryParameters = _ocur.GetObject(_CreateInventoryHeader_PopUp.InventoryParameters)
                .InventoryCreator = _ocur.GetObject(_CreateInventoryHeader_PopUp.InventoryCreator)
                .InventoryInspector = _ocur.GetObject(_CreateInventoryHeader_PopUp.InventoryInspector)
            End With

            Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(_ocur.Session, _
                                        CriteriaOperator.Parse("StorageInHeader.IsEditable=False AND " +
                                                               " StorageInHeader.Active=True AND " +
                                                               " StorageInHeader.Storage=? AND " +
                                                               " StorageInHeader.ObjectCreated<=?", _ocur.GetObject(_CreateInventoryHeader_PopUp.Storage), _CreateInventoryHeader_PopUp.DateClose))

            RaiseEvent InitWork(1, 1, _StorageInRows_Collection.Count)

            If _StorageInRows_Collection.Count > 0 Then
                For Each _StorageInRows As StorageInRows In _StorageInRows_Collection
                    Dim _InventorySIR As InventorySIR = _ocur.CreateObject(Of InventorySIR)()
                    With _InventorySIR
                        .InventoryHeader = _InventoryHeader
                        .StorageInRows = _StorageInRows
                        .QTT = Math.Round(_StorageInRows.QTT, 4, MidpointRounding.AwayFromZero)

                        Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(_ocur.Session, _
                             CriteriaOperator.Parse("StorageInRows=? and StorageOutRows.StorageOutHeader.ObjectCreated<=?", _StorageInRows, _CreateInventoryHeader_PopUp.DateClose))

                        For Each _StorageOutRowsInRows As StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                            .QTT -= Math.Round(_StorageOutRowsInRows.QTT, 4, MidpointRounding.AwayFromZero)
                        Next
                    End With
                    If _InventorySIR.QTT > 0 Then
                        Dim _InventorySC As InventorySC = _ocur.FindObject(Of InventorySC)(CriteriaOperator.Parse("InventoryHeader=? AND Products=?", _
                                                                                                                  _InventoryHeader, _InventorySIR.StorageInRows.Products))
                        If _InventorySC Is Nothing Then
                            _InventorySC = New InventorySC(_ocur.Session)
                            With _InventorySC
                                .InventoryHeader = _InventoryHeader
                                .Products = _InventorySIR.StorageInRows.Products
                                .QTT = Math.Round(_InventorySIR.QTT, 4, MidpointRounding.AwayFromZero)
                            End With
                        Else
                            _InventorySC.QTT += Math.Round(_InventorySIR.QTT, 4, MidpointRounding.AwayFromZero)
                        End If
                    End If
                    RaiseEvent DoWork()
                Next
            End If

            _ocur.CommitChanges()

            Dim _InventorySIR_DEL_Collection As New XPCollection(Of InventorySIR)(_ocur.Session, CriteriaOperator.Parse("InventoryHeader=? AND QTT=0", _InventoryHeader))

            If _InventorySIR_DEL_Collection.Count > 0 Then _ocur.Delete(_InventorySIR_DEL_Collection)
            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        Finally
            GLOBAL_HAS_Audit = True

            RaiseEvent FinalWork
        End Try
    End Sub
    Private Sub aCloseInventoryHeader_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCloseInventoryHeader.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _InventoryHeader As InventoryHeader = TryCast(View.SelectedObjects(0), InventoryHeader)
        _InventoryHeader.InventoryStatus = eInventoryStatus.eClosed
        _InventoryHeader.InventoryHeaderDateClose = GetSQLTime(_ocur.Session)

        _ocur.CommitChanges()
    End Sub
    Private Sub aCreatePDACollection_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreatePDACollection.Execute

        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _InventoryHeader As InventoryHeader = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject
            If _InventoryHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = _ocur.CreateObject(Of StoragePDACollectionHeader)()
            _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eOpen
            _StoragePDACollectionHeader.StoragePDAParameters = _ocur.FindObject(Of StoragePDAParameters)(CriteriaOperator.Parse("CustomersFrom=?", _InventoryHeader.Storage.CustomersFrom))


            Dim _InventoryPDA As InventoryPDA = _ocur.CreateObject(Of InventoryPDA)()
            _InventoryPDA.InventoryHeader = _InventoryHeader
            _InventoryPDA.StoragePDACollectionHeader = _StoragePDACollectionHeader

            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
    Private Sub aCreateDifferent_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateDifferent.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _InventoryHeader As InventoryHeader = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject
            If _InventoryHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            If _InventoryHeader.InventoryDifferent.Count > 0 Then
                Dim _InventoryDifferent_Collection As New XPCollection(Of InventoryDifferent)(_ocur.Session, CriteriaOperator.Parse("InventoryHeader=?", _InventoryHeader))

                _ocur.Delete(_InventoryDifferent_Collection)
            End If

            If _InventoryHeader.InventorySC.Count > 0 Then
                For Each _InventorySC As InventorySC In _InventoryHeader.InventorySC
                    Dim _InventoryDifferent As InventoryDifferent = _ocur.CreateObject(Of InventoryDifferent)()
                    _InventoryDifferent.InventoryHeader = _InventoryHeader
                    '_InventoryDifferent.Products = _InventorySC.StorageComputed.Products
                    '_InventoryDifferent.QTT_SC_Free = _InventorySC.StorageComputed.QTTFree
                    _InventoryDifferent.QTT_SC_Found = _InventorySC.QTT_Found
                Next
            End If

            If _InventoryHeader.InventoryPDA.Count > 0 Then
                For Each _InventoryPDA As InventoryPDA In _InventoryHeader.InventoryPDA
                    If _InventoryPDA.StoragePDACollectionHeader.StoragePDACollectionRows.Count > 0 Then
                        For Each _StoragePDACollectionRows As StoragePDACollectionRows In _InventoryPDA.StoragePDACollectionHeader.StoragePDACollectionRows
                            Dim _InventoryDifferent As InventoryDifferent = _ocur.FindObject(Of InventoryDifferent)(CriteriaOperator.Parse("Products.Oid=? AND InventoryHeader=?", _StoragePDACollectionRows.Products.Oid, _InventoryHeader), True)
                            If _InventoryDifferent IsNot Nothing Then
                                _InventoryDifferent.QTT_PDA = _StoragePDACollectionRows.QTT
                            End If
                        Next
                    End If
                Next
            End If

            If _InventoryHeader.InventorySIR.Count > 0 Then
                For Each _InventorySIR As InventorySIR In _InventoryHeader.InventorySIR
                    Dim _InventoryDifferent As InventoryDifferent = _ocur.FindObject(Of InventoryDifferent)(CriteriaOperator.Parse("Products.Oid=? AND InventoryHeader=?", _InventorySIR.StorageInRows.Products.Oid, _InventoryHeader), True)

                    Dim _StorageMoveEvents_IN_Collection As New XPCollection(Of StorageMoveEvents)(_ocur.Session, CriteriaOperator.Parse("StorageInRows.Oid=? AND StorageOutRows=?", _InventorySIR.StorageInRows.Oid, Nothing))
                    _StorageMoveEvents_IN_Collection.Sorting.Add(New SortProperty("ExecutionDate", DB.SortingDirection.Ascending))
                    _StorageMoveEvents_IN_Collection.Filter = CriteriaOperator.Parse("ExecutionDate>?", _InventoryHeader.ObjectCreated)
                    If _StorageMoveEvents_IN_Collection.Count > 0 Then
                        Dim _QTT_In As Double = 0
                        For Each _StorageMoveEvents As StorageMoveEvents In _StorageMoveEvents_IN_Collection
                            _QTT_In += _StorageMoveEvents.QTT
                        Next
                        _InventoryDifferent.QTT_In = _QTT_In
                    End If

                    Dim _StorageMoveEvents_OUT_Collection As New XPCollection(Of StorageMoveEvents)(_ocur.Session, CriteriaOperator.Parse("StorageInRows.Oid=? AND StorageOutRows=?", _InventorySIR.StorageInRows.Oid, Not Nothing))
                    _StorageMoveEvents_OUT_Collection.Sorting.Add(New SortProperty("ExecutionDate", DB.SortingDirection.Ascending))
                    _StorageMoveEvents_OUT_Collection.Filter = CriteriaOperator.Parse("ExecutionDate>?", _InventoryHeader.ObjectCreated)
                    If _StorageMoveEvents_OUT_Collection.Count > 0 Then
                        Dim _QTT_Out As Double = 0
                        For Each _StorageMoveEvents As StorageMoveEvents In _StorageMoveEvents_OUT_Collection
                            _QTT_Out += _StorageMoveEvents.QTT
                        Next
                        _InventoryDifferent.QTT_Out = _QTT_Out
                    End If
                Next
            End If
            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
    Private Sub aInventory_Print_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aInventory_Print.Execute
        Try
            Dim _InventoryHeader As InventoryHeader = TryCast(View.SelectedObjects(0), InventoryHeader)
            Dim _ReportData As ReportData = _InventoryHeader.Storage.ReportInventory
            If _ReportData Is Nothing Then Throw New Exception(String.Format("""{0}"" raktárhoz nincs beállítva letár nyomtatvány!", _InventoryHeader.Storage.ShortName))

            Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("InventoryHeader.Oid=?", _InventoryHeader.Oid), True)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aInventoryHeader_RecreateInventory_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aInventoryHeader_RecreateInventory.Execute
        Try
            GLOBAL_HAS_Audit = False

            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _InventoryHeader As InventoryHeader = TryCast(View.SelectedObjects(0), InventoryHeader)

            Dim _InventorySC_DELCollection As New XPCollection(Of InventorySC)(_ocur.Session, CriteriaOperator.Parse("InventoryHeader=?", _InventoryHeader))
            Dim _InventorySIR_DELCollection As New XPCollection(Of InventorySIR)(_ocur.Session, CriteriaOperator.Parse("InventoryHeader=?", _InventoryHeader))
            _ocur.Delete(_InventorySC_DELCollection)
            _ocur.Delete(_InventorySIR_DELCollection)

            Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(_ocur.Session, _
                                         CriteriaOperator.Parse("StorageInHeader.IsEditable=False AND " +
                                                                " StorageInHeader.Active=True AND " +
                                                                " StorageInHeader.Storage=? AND " +
                                                                " StorageInHeader.ObjectCreated<=?", _InventoryHeader.Storage, _InventoryHeader.DateClose))

            RaiseEvent InitWork(1, 1, _StorageInRows_Collection.Count)

            If _StorageInRows_Collection.Count > 0 Then
                For Each _StorageInRows As StorageInRows In _StorageInRows_Collection
                    Dim _InventorySIR As InventorySIR = _ocur.CreateObject(Of InventorySIR)()
                    With _InventorySIR
                        .InventoryHeader = _InventoryHeader
                        .StorageInRows = _StorageInRows
                        .QTT = Math.Round(_StorageInRows.QTT, 4, MidpointRounding.AwayFromZero)

                        Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(_ocur.Session, _
                             CriteriaOperator.Parse("StorageInRows=? AND StorageOutRows.StorageOutHeader.ObjectCreated<=?", _StorageInRows, _InventoryHeader.DateClose))

                        For Each _StorageOutRowsInRows As StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                            .QTT -= Math.Round(_StorageOutRowsInRows.QTT, 4, MidpointRounding.AwayFromZero)
                        Next
                        .QTT = Math.Round(.QTT, 4, MidpointRounding.AwayFromZero)
                    End With
                    If _InventorySIR.QTT > 0 Then
                        Dim _InventorySC As InventorySC = _ocur.FindObject(Of InventorySC)(CriteriaOperator.Parse("InventoryHeader=? AND Products=?", _
                                                                                                                  _InventoryHeader, _InventorySIR.StorageInRows.Products))
                        If _InventorySC Is Nothing Then
                            _InventorySC = New InventorySC(_ocur.Session)
                            With _InventorySC
                                .InventoryHeader = _InventoryHeader
                                .Products = _InventorySIR.StorageInRows.Products
                                .QTT = Math.Round(_InventorySIR.QTT, 4, MidpointRounding.AwayFromZero)
                            End With
                        Else
                            _InventorySC.QTT += Math.Round(_InventorySIR.QTT, 4, MidpointRounding.AwayFromZero)
                        End If
                    End If
                    RaiseEvent DoWork()
                Next
            End If

            _ocur.CommitChanges()

            Dim _InventorySIR_DEL_Collection As New XPCollection(Of InventorySIR)(_ocur.Session, CriteriaOperator.Parse("InventoryHeader=? AND QTT<=0", _InventoryHeader))

            If _InventorySIR_DEL_Collection.Count > 0 Then _ocur.Delete(_InventorySIR_DEL_Collection)
            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        Finally
            GLOBAL_HAS_Audit = True

            RaiseEvent FinalWork
        End Try
    End Sub
    Protected Sub aInventoryHeader_LoadSpreadFromParameters_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aInventoryHeader_LoadSpreadFromParameters.Execute
        'WIN esben kidolgozva!
    End Sub

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

            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _InventoryHeader As InventoryHeader = TryCast(View.SelectedObjects(0), InventoryHeader)
            Dim _StorageDateClearingPrice As StorageDateClearingPrice
            Dim _StorageDateClearingPrice_Collection As XPCollection(Of StorageDateClearingPrice)

            RaiseEvent InitWork(1, 1, _InventoryHeader.InventorySC.Count)
            For Each _InventorySC As InventorySC In _InventoryHeader.InventorySC
                RaiseEvent DoWork()
                _InventorySC.AveragePriceHUF = 0
                _StorageDateClearingPrice_Collection = New XPCollection(Of StorageDateClearingPrice)(_ocur.Session, CriteriaOperator.Parse("CustomersFrom=? and Products=? and GetDate(DateExecution)<=?", _InventoryHeader.Storage.CustomersFrom, _InventorySC.Products, _InventoryHeader.DateClose.Date))
                _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("GetDate(DateExecution)", DB.SortingDirection.Descending))
                _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))
                For Each _StorageDateClearingPrice In _StorageDateClearingPrice_Collection
                    _InventorySC.AveragePriceHUF = _StorageDateClearingPrice.AveragePriceHUF_After
                    _InventorySC.SumAveragePriceHUF = _InventorySC.AveragePriceHUF * _InventorySC.QTT

                    _InventorySC.SumAveragePriceHUF_Found = _InventorySC.AveragePriceHUF * _InventorySC.QTT_Found
                    _InventorySC.SumAveragePriceHUF_Different = _InventorySC.SumAveragePriceHUF_Found - _InventorySC.SumAveragePriceHUF

                    _InventorySC.SumAveragePriceHUF_Surplus = 0
                    _InventorySC.SumAveragePriceHUF_Want = 0

                    If _InventorySC.SumAveragePriceHUF_Different > 0 Then
                        _InventorySC.SumAveragePriceHUF_Surplus = _InventorySC.SumAveragePriceHUF_Different
                    Else
                        _InventorySC.SumAveragePriceHUF_Want = Math.Abs(_InventorySC.SumAveragePriceHUF_Different)
                    End If

                    Exit For
                Next
                For Each _InventorySIR As InventorySIR In _InventoryHeader.InventorySIR
                    If _InventorySC.Products.Oid = _InventorySIR.StorageInRows.Products.Oid Then
                        _InventorySIR.AveragePriceHUF = _InventorySC.AveragePriceHUF
                        _InventorySIR.SumAveragePriceHUF = _InventorySIR.AveragePriceHUF * _InventorySIR.QTT

                        _InventorySIR.SumAveragePriceHUF_Found = _InventorySIR.AveragePriceHUF * _InventorySIR.QTT_Found
                        _InventorySIR.SumAveragePriceHUF_Different = _InventorySIR.SumAveragePriceHUF_Found - _InventorySIR.SumAveragePriceHUF

                        _InventorySIR.SumAveragePriceHUF_Surplus = 0
                        _InventorySIR.SumAveragePriceHUF_Want = 0

                        If _InventorySIR.SumAveragePriceHUF_Different > 0 Then
                            _InventorySIR.SumAveragePriceHUF_Surplus = _InventorySIR.SumAveragePriceHUF_Different
                        Else
                            _InventorySIR.SumAveragePriceHUF_Want = Math.Abs(_InventorySIR.SumAveragePriceHUF_Different)
                        End If
                    End If
                Next
            Next
            _ocur.CommitChanges()

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