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

Public Class ProductsVC
    Inherits DevExpress.ExpressApp.ViewController
    Dim _Selection As ArrayList
    Dim _NoSelect As Boolean = False

    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()
        _Selection = New ArrayList
        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
#Region "Overrides"
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        If View.Id = "Products_ListView" Then
            AddHandler View.SelectionChanged, AddressOf SelectionChanged
            AddHandler Frame.GetController(Of DeleteObjectsViewController).DeleteAction.Executing, AddressOf Products_ListView_DeleteAction_Executing
        End If
        If View.Id = "Products_LookupListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "Products_Products_BusinessDirectory_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "Products_ListView_EverUsed" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "Products_ListView_Deleted" Then
            If TryCast(View, ListView) IsNot Nothing Then
                If TryCast(View, ListView).CollectionSource IsNot Nothing Then
                    If TryCast(View, ListView).CollectionSource.Collection IsNot Nothing Then
                        If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection) IsNot Nothing Then
                            If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection IsNot Nothing Then
                                If TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, XPCollection) IsNot Nothing Then
                                    TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, XPCollection).SelectDeleted = True
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Products_ListView" Then
            RemoveHandler View.SelectionChanged, AddressOf SelectionChanged
        End If
        If View.Id = "Products_LookupListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "Products_Products_BusinessDirectory_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub
#End Region
    Private Sub SelectionChanged(sender As Object, e As System.EventArgs)

        If _NoSelect = False Then
            _Selection.Clear()
            For Each _Products As Products In View.SelectedObjects
                _Selection.Add(_Products)
            Next
        End If
    End Sub
    Private Sub aModifyProductsParameters_CustomizePopupWindowParams(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aModifyProductsParameters.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ModifyProductsParameters As ModifyProductsParameters = _onew.CreateObject(Of ModifyProductsParameters)()

        e.View = Application.CreateDetailView(_onew, _ModifyProductsParameters)
    End Sub
    ''' <summary>
    ''' Módosítja a Products objektum adatait csoportosan
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub aModifyProductsParameters_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aModifyProductsParameters.Execute
        Dim _ModifyProductsParameters As ModifyProductsParameters = TryCast(e.PopupWindow.View.SelectedObjects(0), ModifyProductsParameters)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        RaiseEvent InitWork(0, 1, View.SelectedObjects.Count)
        For Each _Products As Products In _Selection
            RaiseEvent DoWork()

            If _ModifyProductsParameters.IsParameter_01 Then
                _Products.Parameter_01 = _ModifyProductsParameters.Parameter_01
            End If

            If _ModifyProductsParameters.IsParameter_02 Then
                _Products.Parameter_02 = _ModifyProductsParameters.Parameter_02
            End If

            If _ModifyProductsParameters.IsParameter_03 Then
                _Products.Parameter_03 = _ModifyProductsParameters.Parameter_03
            End If

            If _ModifyProductsParameters.IsParameter_04 Then
                _Products.Parameter_04 = _ModifyProductsParameters.Parameter_04
            End If

            If _ModifyProductsParameters.IsParameter_05 Then
                _Products.Parameter_05 = _ModifyProductsParameters.Parameter_05
            End If

            If _ModifyProductsParameters.IsParameter_06 Then
                _Products.Parameter_06 = _ModifyProductsParameters.Parameter_06
            End If

            If _ModifyProductsParameters.IsParameter_07 Then
                _Products.Parameter_07 = _ModifyProductsParameters.Parameter_07
            End If

            If _ModifyProductsParameters.IsParameter_08 Then
                _Products.Parameter_08 = _ModifyProductsParameters.Parameter_08
            End If

            If _ModifyProductsParameters.IsParameter_09 Then
                _Products.Parameter_09 = _ModifyProductsParameters.Parameter_09
            End If

            If _ModifyProductsParameters.IsParameter_10 Then
                _Products.Parameter_10 = _ModifyProductsParameters.Parameter_10
            End If

            If _ModifyProductsParameters.IsUnits Then
                _Products.Units = _ocur.GetObjectByKey(Of Units)(_ModifyProductsParameters.Units.Oid)
            End If

            If _ModifyProductsParameters.IsVAT Then
                _Products.VAT = _ocur.GetObjectByKey(Of VAT)(_ModifyProductsParameters.VAT.Oid)
            End If

            If _ModifyProductsParameters.IsCurrencyName Then
                _Products.CurrencyName = _ocur.GetObjectByKey(Of CurrencyName)(_ModifyProductsParameters.CurrencyName.Oid)
            End If

            If _ModifyProductsParameters.IsCustomsTariffs Then
                _Products.CustomsTariff = _ocur.GetObjectByKey(Of CustomsTariffs)(_ModifyProductsParameters.CustomsTariffs.Oid)
            End If

            If _ModifyProductsParameters.IsCountryFrom Then
                _Products.CountryFrom = _ocur.GetObjectByKey(Of Country)(_ModifyProductsParameters.CountryFrom.Oid)
            End If
            If _ModifyProductsParameters.IsProductType Then
                _Products.ProductType = _ModifyProductsParameters.ProductType
            End If

            If _ModifyProductsParameters.ProductsGroups IsNot Nothing Then
                Dim _ProductsGroups As ProductsGroups = _ocur.GetObjectByKey(Of ProductsGroups)(_ModifyProductsParameters.ProductsGroups.Oid)
                If _ProductsGroups IsNot Nothing Then
                    _Products.ProductGroups = _ProductsGroups
                End If
            End If

            If _ModifyProductsParameters.IsCustomersOrder1 Then
                Dim _RowIndex1Avilable As Boolean = False
                For Each _CustomersOrderPriority In _Products.CustomersOrderPriority
                    If _CustomersOrderPriority.RowIndex = 1 Then
                        _RowIndex1Avilable = True
                        _CustomersOrderPriority.CustomersOrder = _ocur.GetObjectByKey(Of CustomersOrder)(_ModifyProductsParameters.CustomersOrder1.Oid)
                        Exit For
                    End If
                Next
                If _RowIndex1Avilable = False Then
                    Dim _CustomersOrderPriority_New As CustomersOrderPriority = _ocur.CreateObject(Of CustomersOrderPriority)()
                    _CustomersOrderPriority_New.RowIndex = 1
                    _CustomersOrderPriority_New.Products = _Products
                    _CustomersOrderPriority_New.CustomersOrder = _ocur.GetObjectByKey(Of CustomersOrder)(_ModifyProductsParameters.CustomersOrder1.Oid)
                    _CustomersOrderPriority_New.Save()
                End If
            End If

            If _ModifyProductsParameters.IsCustomersOrder2 Then
                Dim _RowIndex2Avilable As Boolean = False
                For Each _CustomersOrderPriority In _Products.CustomersOrderPriority
                    If _CustomersOrderPriority.RowIndex = 2 Then
                        _RowIndex2Avilable = True
                        _CustomersOrderPriority.CustomersOrder = _ocur.GetObjectByKey(Of CustomersOrder)(_ModifyProductsParameters.CustomersOrder2.Oid)
                        Exit For
                    End If
                Next
                If _RowIndex2Avilable = False Then
                    Dim _CustomersOrderPriority_New As CustomersOrderPriority = _ocur.CreateObject(Of CustomersOrderPriority)()
                    _CustomersOrderPriority_New.RowIndex = 2
                    _CustomersOrderPriority_New.Products = _Products
                    _CustomersOrderPriority_New.CustomersOrder = _ocur.GetObjectByKey(Of CustomersOrder)(_ModifyProductsParameters.CustomersOrder2.Oid)
                    _CustomersOrderPriority_New.Save()
                End If
            End If

            If _ModifyProductsParameters.IsCustomersOrder3 Then
                Dim _RowIndex3Avilable As Boolean = False
                For Each _CustomersOrderPriority In _Products.CustomersOrderPriority
                    If _CustomersOrderPriority.RowIndex = 3 Then
                        _RowIndex3Avilable = True
                        _CustomersOrderPriority.CustomersOrder = _ocur.GetObjectByKey(Of CustomersOrder)(_ModifyProductsParameters.CustomersOrder3.Oid)
                        Exit For
                    End If
                Next
                If _RowIndex3Avilable = False Then
                    Dim _CustomersOrderPriority_New As CustomersOrderPriority = _ocur.CreateObject(Of CustomersOrderPriority)()
                    _CustomersOrderPriority_New.RowIndex = 3
                    _CustomersOrderPriority_New.Products = _Products
                    _CustomersOrderPriority_New.CustomersOrder = _ocur.GetObjectByKey(Of CustomersOrder)(_ModifyProductsParameters.CustomersOrder3.Oid)
                    _CustomersOrderPriority_New.Save()
                End If
            End If

            If _ModifyProductsParameters.IsListPriceHUF = True Then
                _Products.ListPriceOutHUF = _Products.ListPriceOutHUF + (_Products.ListPriceOutHUF * (_ModifyProductsParameters.ListPricePercent / 100))
            End If
            If _ModifyProductsParameters.IsListPriceDEV = True Then
                _Products.ListPriceOutDEV = _Products.ListPriceOutDEV + (_Products.ListPriceOutDEV * (_ModifyProductsParameters.ListPricePercent / 100))
            End If

            If _ModifyProductsParameters.IsListPriceRegenerate Then
                Select Case _ModifyProductsParameters.PriceMode
                    Case ePriceMode.eDEVToHUF
                        _Products.ListPriceOutHUF = _Products.ListPriceOutDEV * _ModifyProductsParameters.CurrencyRate
                    Case (ePriceMode.eHUFToDEV)
                        _Products.ListPriceOutDEV = _Products.ListPriceOutHUF / _ModifyProductsParameters.CurrencyRate
                End Select
            End If
            If _ModifyProductsParameters.IsProducer Then 'Gyártó
                _Products.Producer = _ocur.GetObjectByKey(Of Customers)(_ModifyProductsParameters.Producer.Oid)
            End If

            If _ModifyProductsParameters.IsDecimalIn Then _Products.DecimalIn = _ModifyProductsParameters.DecimalIn
            If _ModifyProductsParameters.IsDecimalOut Then _Products.DecimalOut = _ModifyProductsParameters.DecimalOut


            If _ModifyProductsParameters.IsADRPackingUnit Then _Products.ADRPackingUnit = _ModifyProductsParameters.ADRPackingUnit

            If _ModifyProductsParameters.CloneDocumentations Then
                If _ModifyProductsParameters.ProductsSource IsNot Nothing Then
                    _ocur.Delete(_Products.ProductsDocumentations)
                    For Each _ProductsDocumentations As ProductsDocumentations In _ModifyProductsParameters.ProductsSource.ProductsDocumentations
                        Dim _ProductsDocumentations_New As ProductsDocumentations = _ocur.CreateObject(Of ProductsDocumentations)

                        _ProductsDocumentations_New.Products = _Products
                        _ProductsDocumentations_New.Description = _ProductsDocumentations.Description
                        _ProductsDocumentations_New.File = _ocur.GetObject(_ProductsDocumentations.File)
                        _ProductsDocumentations_New.FileCreated = _ProductsDocumentations.FileCreated
                        _ProductsDocumentations_New.FileModified = _ProductsDocumentations.FileModified

                        _ProductsDocumentations_New.ObjectCreated = _ProductsDocumentations.ObjectCreated
                        _ProductsDocumentations_New.RowGroup1 = _ProductsDocumentations.RowGroup1
                        _ProductsDocumentations_New.RowGroup2 = _ProductsDocumentations.RowGroup2
                        _ProductsDocumentations_New.ShortName = _ProductsDocumentations.ShortName
                        _ProductsDocumentations_New.UserCreated = _ocur.GetObject(_ProductsDocumentations.UserCreated)


                        _Products.ProductsDocumentations.Add(_ocur.GetObject(_ProductsDocumentations_New))
                    Next
                Else

                End If
            End If
            If _ModifyProductsParameters.CloneSentence Then
                If _ModifyProductsParameters.ProductsSource IsNot Nothing Then
                    _ocur.Delete(_Products.ProductsSentence)
                    For Each _ProductsSentence As ProductsSentence In _ModifyProductsParameters.ProductsSource.ProductsSentence
                        Dim _ProductsSentence_New As ProductsSentence = _ocur.CreateObject(Of ProductsSentence)

                        _ProductsSentence_New.Products = _Products
                        _ProductsSentence_New.Sentence = _ocur.GetObject(_ProductsSentence.Sentence)

                        _Products.ProductsSentence.Add(_ocur.GetObject(_ProductsSentence_New))
                    Next
                Else

                End If
            End If
            If _ModifyProductsParameters.CloneUNList Then
                If _ModifyProductsParameters.ProductsSource IsNot Nothing Then
                    _ocur.Delete(_Products.ProductsUNList)
                    For Each _ProductsUNList As ProductsUNList In _ModifyProductsParameters.ProductsSource.ProductsUNList
                        Dim _ProductsUNList_New As ProductsUNList = _ocur.CreateObject(Of ProductsUNList)

                        _ProductsUNList_New.Products = _Products
                        _ProductsUNList_New.UNList = _ocur.GetObject(_ProductsUNList.UNList)

                        _Products.ProductsUNList.Add(_ocur.GetObject(_ProductsUNList_New))
                    Next
                Else

                End If
                If _Products.ProductsUNList.Count > 0 Then
                    _Products.UNNumber = _Products.ProductsUNList(0).UNList.ShortName
                End If
            End If

            If _ModifyProductsParameters.IsPacketMode Then
                _Products.PacketMode = _ocur.GetObject(_ModifyProductsParameters.PacketMode)
            End If

            _Products.Save()
        Next
        _ocur.CommitChanges()
        RaiseEvent FinalWork()
    End Sub
    Private Sub aPrintSelectedProductsBarcode_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aPrintSelectedProductsBarcode.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _ReportData As ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Name=?", "Termékek - etikett"))
            If _ReportData Is Nothing Then Throw New Exception("*Nincs beállítva nyomtatvány!")
            If View.SelectedObjects.Count = 0 Then Throw New Exception("*Egy sort ki kell jelölni, hogy a raktár azonosítható legyen!")

            Dim _OidArray As New ArrayList

            For Each _Products As Products In View.SelectedObjects
                _OidArray.Add(_Products.Oid)
            Next

            Dim _InOperator As InOperator = New InOperator("Products.Oid", _OidArray)

            Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, _InOperator, True)
        Catch ext As Exception
            MsgBox(ext.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aViewStorageMoveInfo_Products_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewStorageMoveInfo_Products.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Products As Products = TryCast(View.SelectedObjects(0), Products)
        Dim _StorageMoveInfo As StorageMoveInfo
        If _Products IsNot Nothing Then
            _StorageMoveInfo = _onew.CreateObject(Of StorageMoveInfo)()
            _StorageMoveInfo.Products = _onew.GetObject(_Products)
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "StorageMoveInfo_DetailView", False, _StorageMoveInfo)
        End If
    End Sub
    Private Sub aProductsBarcodeGenerator_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aProductsBarcodeGenerator.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
            Dim _Products_Collection As New XPCollection(Of Products)(_uow, CriteriaOperator.Parse("IsNull(Barcode)=False AND Barcode!=''"))
            _Products_Collection.Sorting.Add(New SortProperty("Barcode", DB.SortingDirection.Descending))

            RaiseEvent InitWork(0, 1, View.SelectedObjects.Count)

            Dim _StartIndex As Double
            If _Products_Collection.Count = 0 Then
                _StartIndex = 400000100001
            Else
                _StartIndex = Convert.ToDouble(Mid(_Products_Collection(0).Barcode, 1, 12)) + 1
            End If

            For Each _Products As Products In View.SelectedObjects
                RaiseEvent DoWork()
                If _Products.Barcode Is Nothing Then
                    _Products.Barcode = EAN13Generator((_StartIndex).ToString)
                    _StartIndex += 1
                End If
            Next
            _uow.CommitChanges()
            RaiseEvent FinalWork()
        Catch ex As Exception

        End Try
    End Sub


    Private Sub aProductsDocumentations_ChangeGroup_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aProductsDocumentations_ChangeGroup.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ChangeFileAttachment_PopUp As ChangeFileAttachment_PopUp = _onew.CreateObject(Of ChangeFileAttachment_PopUp)()
            e.View = Application.CreateDetailView(_onew, "ChangeFileAttachment_PopUp_DetailView", True, _ChangeFileAttachment_PopUp)
            e.View.Tag = e.Action.Id
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Hiba")
        End Try
    End Sub
    Private Sub aProductsDocumentations_ChangeGroup_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aProductsDocumentations_ChangeGroup.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _ChangeFileAttachment_PopUp As ChangeFileAttachment_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ChangeFileAttachment_PopUp)
            For Each _ProductsDocumentations As ProductsDocumentations In View.SelectedObjects
                If _ChangeFileAttachment_PopUp.ChangeStatus = True Then
                    _ProductsDocumentations.Status = _ChangeFileAttachment_PopUp.Status
                End If
                If _ChangeFileAttachment_PopUp.ChangeRowGroup1 = True Then
                    _ProductsDocumentations.RowGroup1 = _ChangeFileAttachment_PopUp.RowGroup1
                End If
                If _ChangeFileAttachment_PopUp.ChangeRowGroup2 = True Then
                    _ProductsDocumentations.RowGroup2 = _ChangeFileAttachment_PopUp.RowGroup2
                End If
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Hiba")
        End Try
    End Sub

    Private Sub aProductsDocumentations_Remove_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProductsDocumentations_Remove.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _ListView As ListView = TryCast(View, ListView)
            Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)

            Dim _Products As Products = TryCast(_PropertyCollectionSource.MasterObject, Products)

            For Each _ProductsDocumentations As ProductsDocumentations In e.SelectedObjects
                If _Products IsNot Nothing Then
                    _Products.ProductsDocumentations.Remove(_ProductsDocumentations)
                End If
            Next
            _ocur.CommitChanges()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aProducts_Open_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProducts_Open.Execute
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Products As Products = _onew.GetObject(TryCast(View.SelectedObjects(0), Products))
            If _Products Is Nothing Then Throw New Exception("*Nem lehet azonosítani a terméket!")

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Products_DetailView", True, _Products)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub

    Private Sub Products_ListView_DeleteAction_Executing(sender As Object, e As CancelEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Products As Products
        'For Each _Products In View.SelectedObjects
        '    'For Each _Object In _ocur.Session.CollectReferencingObjects(_Products)
        '    '    e.Cancel = True

        '    '    '// Ide kell vaami model szöveg ...
        '    'Next
        'Next
    End Sub
    Private Sub aProducts_CreateBusinessDirectory_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProducts_CreateBusinessDirectory.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        For Each _Products As Products In View.SelectedObjects
            Dim _ProductsMasterBusinessDirectory As BusinessDirectory = Nothing
            Dim _ProductGroupsMasterBusinessDirectory As BusinessDirectory = Nothing

            If _Products.MasterBusinessDirectory Is Nothing Then

                If _Products.ProductGroups IsNot Nothing Then
                    If _Products.ProductGroups.MasterBusinessDirectory IsNot Nothing Then
                        _ProductGroupsMasterBusinessDirectory = _Products.ProductGroups.MasterBusinessDirectory
                    Else
                        Dim _ProductsBaseBusinessDirectory As BusinessDirectory = _ocur.FindObject(Of BusinessDirectory)(CriteriaOperator.Parse("ShortName='Termékek' AND IsNull(BusinessDirectory_Parent)=True"))

                        If _ProductsBaseBusinessDirectory Is Nothing Then
                            _ProductsBaseBusinessDirectory = New BusinessDirectory(_ocur.Session)
                            With _ProductsBaseBusinessDirectory
                                .ShortName = "Termékek"
                                .BusinessDirectory_Parent = Nothing
                            End With
                        End If
                        _ProductGroupsMasterBusinessDirectory = New BusinessDirectory(_ocur.Session)
                        With _ProductGroupsMasterBusinessDirectory
                            .ShortName = _Products.ProductGroups.ShortName
                            .BusinessDirectory_Parent = _ProductsBaseBusinessDirectory
                        End With
                        _Products.ProductGroups.MasterBusinessDirectory = _ProductGroupsMasterBusinessDirectory
                    End If
                    _ProductsMasterBusinessDirectory = New BusinessDirectory(_ocur.Session)
                    With _ProductsMasterBusinessDirectory
                        .ShortName = _Products.ShortName
                        .BusinessDirectory_Parent = _ProductGroupsMasterBusinessDirectory
                    End With
                End If

                _Products.MasterBusinessDirectory = _ProductsMasterBusinessDirectory

            End If
        Next
        _ocur.CommitChanges()
    End Sub

    Private Sub aProducts_GenerateNotUsedList_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProducts_GenerateNotUsedList.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Products_Collection As New XPCollection(Of Products)(_ocur.Session)

        RaiseEvent InitWork(1, 1, _Products_Collection.Count)
        For Each _Products As Products In _Products_Collection
            If Not _Products.IsStoragEverUsed Then
                Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(_ocur.Session, CriteriaOperator.Parse("Products=?", _Products))
                If _StorageInRows_Collection.Count > 0 Then
                    _Products.IsStoragEverUsed = True
                Else
                    Dim _OrderInRows_Collection As New XPCollection(Of OrderInRows)(_ocur.Session, CriteriaOperator.Parse("Products=?", _Products))
                    If _OrderInRows_Collection.Count > 0 Then
                        _Products.IsStoragEverUsed = True
                    Else
                        Dim _OrderOutRows_Collection As New XPCollection(Of OrderOutRows)(_ocur.Session, CriteriaOperator.Parse("Products=?", _Products))
                        If _OrderOutRows_Collection.Count > 0 Then
                            _Products.IsStoragEverUsed = True
                        Else
                            _Products.IsStoragEverUsed = False
                        End If
                    End If
                End If
            End If
            RaiseEvent DoWork()
        Next

        _ocur.CommitChanges()
        RaiseEvent FinalWork()
    End Sub
End Class
