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

Public Class OffersInVC
    Inherits DevExpress.ExpressApp.ViewController
    Protected _selection As New ArrayList
    Protected _noselect As Boolean = False
    Public Sub New()
        MyBase.New()

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

    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of OffersInVC).aTotable_OfferIn.Active.SetItemValue("", False)
        Frame.GetController(Of OffersInVC).aTotableD_OfferIn.Active.SetItemValue("", False)
        Frame.GetController(Of OffersInVC).aUp_OfferIn.Active.SetItemValue("", False)
        Frame.GetController(Of OffersInVC).aDown_OfferIn.Active.SetItemValue("", False)
        Frame.GetController(Of OffersInVC).aOffersInFinal.Active.SetItemValue("", False)
        Frame.GetController(Of OffersInVC).aOffersIn_ChangeRows.Active.SetItemValue("", False)
        Frame.GetController(Of OffersInVC).aInsertServicesToOfferIN.Active.SetItemValue("", False)
        Frame.GetController(Of OffersInVC).aOfferIn_ChangeState.Active.SetItemValue("", False)

        If View.Id = "OfferInHeader_ListView_Editable" Then
            Frame.GetController(Of OffersInVC).aOffersInFinal.Active.SetItemValue("", True)
        End If
        If View.Id = "OfferInHeader_ListView_NotEditable" Or _
            View.Id = "CRM_Opportunities_OfferInHeader_ListView" Then
            Frame.GetController(Of OffersInVC).aOfferIn_ChangeState.Active.SetItemValue("", True)
            AddHandler View.SelectionChanged, AddressOf OfferInHeader_OfferInRows_ListView_SelectionChanged
        End If

        If View.Id = "OfferInHeader_OfferInRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of OffersInVC).aUp_OfferIn.Active.SetItemValue("", True)
            Frame.GetController(Of OffersInVC).aDown_OfferIn.Active.SetItemValue("", True)
            Frame.GetController(Of OffersInVC).aOffersIn_ChangeRows.Active.SetItemValue("", True)
            Frame.GetController(Of OffersInVC).aInsertServicesToOfferIN.Active.SetItemValue("", True)
            AddHandler View.SelectionChanged, AddressOf OfferInHeader_OfferInRows_ListView_SelectionChanged
        End If
        If View.Id = "OfferInHeader_DetailView" Then
            Dim _OfferInHeader As OfferInHeader = TryCast(View.SelectedObjects(0), OfferInHeader)
            If _OfferInHeader IsNot Nothing Then
                If _OfferInHeader.IsEditable = False Then
                    Frame.GetController(Of OffersInVC).aOffersInFinal.Active.SetItemValue("", False)
                    Frame.GetController(Of OffersInVC).aTotable_OfferIn.Active.SetItemValue("", False)
                    Frame.GetController(Of OffersInVC).aTotableD_OfferIn.Active.SetItemValue("", False)
                    Frame.GetController(Of OffersInVC).aBackToRowEdit_OfferIn.Active.SetItemValue("", False)
                Else
                    Frame.GetController(Of OffersInVC).aOffersInFinal.Active.SetItemValue("", True)
                    Frame.GetController(Of OffersInVC).aTotable_OfferIn.Active.SetItemValue("", True)
                    Frame.GetController(Of OffersInVC).aTotableD_OfferIn.Active.SetItemValue("", True)
                    Frame.GetController(Of OffersInVC).aBackToRowEdit_OfferIn.Active.SetItemValue("", True)
                End If
            End If

        End If
        If View.Id = "OfferInHeader_ListView_Subcontractor" Then
            Frame.GetController(Of OffersInVC)().aPrintOfferIn.Active.SetItemValue("", False)
            Frame.GetController(Of OffersInVC)().aCloneOfferIn.Active.SetItemValue("", False)
            Frame.GetController(Of OffersInVC)().aOfferIn_CreateDeliveryNoteIn.Active.SetItemValue("", False)
        End If
        If View.Id = "DeliveryNoteInFromOffersInRowsHeader_DeliveryNoteInFromOffersInRowsRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OfferOutHeader_ListView_OfferOutHeaderInHeader" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "OfferInHeader_ListView_Editable" Then
            Frame.GetController(Of OffersInVC).aOffersInFinal.Active.SetItemValue("", False)
        End If
        If View.Id = "OfferInHeader_ListView" Then
            Frame.GetController(Of OffersInVC).aOfferIn_ChangeState.Active.SetItemValue("", False)
            RemoveHandler View.SelectionChanged, AddressOf OfferInHeader_OfferInRows_ListView_SelectionChanged
        End If
        If View.Id = "OfferInHeader_OfferInRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of OffersInVC).aOfferIn_ChangeState.Active.SetItemValue("", True)
            Frame.GetController(Of OffersInVC).aUp_OfferIn.Active.SetItemValue("", False)
            Frame.GetController(Of OffersInVC).aDown_OfferIn.Active.SetItemValue("", False)
            Frame.GetController(Of OffersInVC).aOffersIn_ChangeRows.Active.SetItemValue("", False)
            RemoveHandler View.SelectionChanged, AddressOf OfferInHeader_OfferInRows_ListView_SelectionChanged

        End If
        If View.Id = "OfferInHeader_DetailView" Then
            Dim _OfferInHeader As OfferInHeader = TryCast(View.SelectedObjects(0), OfferInHeader)
            If _OfferInHeader IsNot Nothing Then
                If _OfferInHeader.IsEditable = True Then
                    Frame.GetController(Of OffersInVC).aOffersInFinal.Active.SetItemValue("", False)
                    Frame.GetController(Of OffersInVC).aTotable_OfferIn.Active.SetItemValue("", False)
                    Frame.GetController(Of OffersInVC).aTotableD_OfferIn.Active.SetItemValue("", False)
                    Frame.GetController(Of OffersInVC).aBackToRowEdit_OfferIn.Active.SetItemValue("", False)
                End If
            End If
        End If
        If View.Id = "OfferInHeader_ListView_Subcontractor" Then
            Frame.GetController(Of OffersInVC)().aPrintOfferIn.Active.SetItemValue("", True)
            Frame.GetController(Of OffersInVC)().aCloneOfferIn.Active.SetItemValue("", True)
            Frame.GetController(Of OffersInVC)().aOfferIn_CreateDeliveryNoteIn.Active.SetItemValue("", True)
        End If
        If View.Id = "DeliveryNoteInFromOffersInRowsHeader_DeliveryNoteInFromOffersInRowsRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OfferOutHeader_ListView_OfferOutHeaderInHeader" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub OfferInHeader_OfferInRows_ListView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If View Is Nothing Then Exit Sub
        If _noselect = False Then
            If View.SelectedObjects.Count > 0 Then
                _selection.Clear()
                For Each _OfferOutRows In View.SelectedObjects
                    _selection.Add(_OfferOutRows)
                Next
            End If
        End If
    End Sub
    Private Sub aTotable_OfferIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTotable_OfferIn.Execute
        'hozzáadás fejléchez
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferInHeader As OfferInHeader = CType(View.SelectedObjects(0), OfferInHeader)
        Dim _OfferInRows As OfferInRows


        _OfferInRows = _ocur.CreateObject(Of OfferInRows)()
        _OfferInRows.OfferInHeader = _OfferInHeader

        Dim ItemToFocus As ViewItem = TryCast(View, DetailView).FindItem("row_Products")
        If ItemToFocus IsNot Nothing Then
            Dim ItemControl As Object
            ItemControl = ItemToFocus.Control
            ItemControl.Focus()
        End If

        _OfferInRows.AddRows(_OfferInHeader)
        _ocur.CommitChanges()

        _OfferInHeader.row_Installation = Nothing
        _OfferInHeader.row_Products = Nothing
        _OfferInHeader.row_Units = Nothing
        _OfferInHeader.row_VAT = Nothing
        _OfferInHeader.row_ShortName = ""
        _OfferInHeader.row_QTT = 0
        _OfferInHeader.row_Description = ""
        _OfferInHeader.row_FinalPriceDEV = 0
        _OfferInHeader.row_FinalPriceHUF = 0
        _OfferInHeader.row_RowGroup1 = ""
        _OfferInHeader.row_RowGroup2 = ""

        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub aTotableD_OfferIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTotableD_OfferIn.Execute
        'sor törlése
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferInHeader As OfferInHeader = TryCast(e.SelectedObjects(0), OfferInHeader)
        Dim _OfferInRows_Collection As ArrayList = New ArrayList
        Dim _OfferInRows_ListPropertyEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("OfferInRows")

        For Each _OfferInRows In _OfferInRows_ListPropertyEditor.ListView.SelectedObjects
            Dim _OfferInRows_Act As OfferInRows = _OfferInRows
            For Each _OfferInRows_ActHeader As OfferInRows In _OfferInHeader.OfferInRows
                If _OfferInRows_ActHeader.RowIndex > _OfferInRows_Act.RowIndex Then
                    _OfferInRows_ActHeader.RowIndex -= 1
                End If
            Next
        Next

        For Each _OfferInRows As OfferInRows In _OfferInRows_ListPropertyEditor.ListView.SelectedObjects
            _OfferInRows_Collection.Add(_OfferInRows)
        Next

        _ocur.Delete(_OfferInRows_Collection)
        _ocur.CommitChanges()

        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub aUp_OfferIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aUp_OfferIn.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferInHeader As OfferInHeader
        Dim _OfferInRowsSelected As OfferInRows
        Dim _OfferInRowsMinus As OfferInRows
        Dim _OfferInRows As OfferInRows

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

        If _OfferInRowsSelected.RowIndex > 0 Then
            For Each _OfferInRows In _OfferInHeader.OfferInRows
                If _OfferInRows.RowIndex = _OfferInRowsSelected.RowIndex - 1 Then
                    _OfferInRowsMinus = _OfferInRows
                    Exit For
                End If
            Next

            If _OfferInRowsMinus IsNot Nothing Then
                _OfferInRowsSelected.RowIndex -= 1
                _OfferInRowsMinus.RowIndex += 1
            End If
            _OfferInRowsMinus.Save()
            _OfferInRowsSelected.Save()
            _ocur.CommitChanges()
            'If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            '    Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            'End If
        End If
    End Sub
    Private Sub aDown_OfferIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aDown_OfferIn.Execute
        'kiválasztott sort lejebb rendzei
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferInHeader As OfferInHeader
        Dim _OfferInRowsSelected As OfferInRows
        Dim _OfferInRowsMinus As OfferInRows
        Dim _OfferInRows As OfferInRows

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

        If _OfferInRowsSelected.RowIndex < _OfferInHeader.OfferInRows.Count - 1 Then
            For Each _OfferInRows In _OfferInHeader.OfferInRows
                If _OfferInRows.RowIndex = _OfferInRowsSelected.RowIndex + 1 Then
                    _OfferInRowsMinus = _OfferInRows
                    Exit For
                End If
            Next

            If _OfferInRowsMinus IsNot Nothing Then
                _OfferInRowsSelected.RowIndex += 1
                _OfferInRowsMinus.RowIndex -= 1
            End If
            _OfferInRowsMinus.Save()
            _OfferInRowsSelected.Save()
            _ocur.CommitChanges()
            'If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            '    Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            'End If
        End If

    End Sub
    'Private Sub aCloneOfferIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCloneOfferIn.Execute
    '    'kivélasztott bejövõ megrendelés klónozása sorokkal...
    '    Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
    '    Dim _OfferInHeader As OfferInHeader = _onew.GetObject(TryCast(e.SelectedObjects(0), OfferInHeader))
    '    Dim _OfferInHeader_NEW As OfferInHeader = _onew.CreateObject(Of OfferInHeader)()
    '    Dim _OfferInRows As OfferInRows

    '    'Fejlés

    '    _OfferInHeader_NEW.Contact = _OfferInHeader.Contact
    '    _OfferInHeader_NEW.CRM_Opportunities = _OfferInHeader.CRM_Opportunities
    '    _OfferInHeader_NEW.CurrencyName = _OfferInHeader.CurrencyName
    '    _OfferInHeader_NEW.Customers = _OfferInHeader.Customers
    '    _OfferInHeader_NEW.CustomersFrom = _OfferInHeader.CustomersFrom
    '    _OfferInHeader_NEW.DateCreated = _OfferInHeader.DateCreated
    '    _OfferInHeader_NEW.DateExecution = _OfferInHeader.DateExecution
    '    _OfferInHeader_NEW.DatePayment = _OfferInHeader.DatePayment
    '    _OfferInHeader_NEW.Description = _OfferInHeader.Description
    '    _OfferInHeader_NEW.DocumentBody = _OfferInHeader.DocumentBody
    '    _OfferInHeader_NEW.OfferLOSTReasonDescription = _OfferInHeader.OfferLOSTReasonDescription
    '    _OfferInHeader_NEW.PaymentOption = _OfferInHeader.PaymentOption
    '    _OfferInHeader_NEW.QualityOption = _OfferInHeader.QualityOption
    '    _OfferInHeader_NEW.DocumentNumber = ""
    '    _OfferInHeader_NEW.IsEditable = True
    '    _OfferInHeader_NEW.IsStorno = False

    '    _OfferInHeader_NEW.Save()

    '    'sorok átpakolása
    '    For Each _OfferInRows In _OfferInHeader.OfferInRows
    '        Dim _OfferInRows_NEW As OfferInRows = _onew.CreateObject(Of OfferInRows)()

    '        _OfferInRows_NEW.OfferInHeader = _OfferInHeader_NEW
    '        _OfferInRows_NEW.Description = _OfferInRows.Description
    '        _OfferInRows_NEW.DescriptionPrivate = _OfferInRows.DescriptionPrivate
    '        _OfferInRows_NEW.FinalPriceDEV = _OfferInRows.FinalPriceDEV
    '        _OfferInRows_NEW.FinalPriceHUF = _OfferInRows.FinalPriceHUF
    '        _OfferInRows_NEW.Installation = _OfferInRows.Installation
    '        _OfferInRows_NEW.Product = _OfferInRows.Product
    '        _OfferInRows_NEW.QTT = _OfferInRows.QTT
    '        _OfferInRows_NEW.QualityOption = _OfferInRows.QualityOption
    '        _OfferInRows_NEW.RowIndex = _OfferInRows.RowIndex
    '        _OfferInRows_NEW.RowType = _OfferInRows.RowType
    '        _OfferInRows_NEW.ShortName = _OfferInRows.ShortName
    '        _OfferInRows_NEW.SumPriceDEV = _OfferInRows.SumPriceDEV
    '        _OfferInRows_NEW.SumPriceHUF = _OfferInRows.SumPriceHUF

    '        _OfferInRows_NEW.Save()
    '    Next

    '    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, _OfferInHeader_NEW)
    'End Sub 'Egykori CLONE action - R.IP
    Private Sub aOffersInFinal_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOffersInFinal.Execute
        'Véglegesítés
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferInHeader As OfferInHeader = TryCast(View.SelectedObjects(0), OfferInHeader)

        If _OfferInHeader IsNot Nothing Then
            If _OfferInHeader.DocumentNumber = "" Then
                _OfferInHeader.DocumentNumber = _OfferInHeader.OfferInParameters.NumberGenerator.GetNewNumber(_OfferInHeader.OfferInParameters.NumberGenerator)
            End If

            _OfferInHeader.IsEditable = False
            _OfferInHeader.Save()
            _ocur.CommitChanges()
        Else
            Throw New UserFriendlyException("Nincs kiválasztott sor!")
        End If
        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub aOffersIn_ChangeRows_Cancel(sender As Object, e As System.EventArgs) Handles aOffersIn_ChangeRows.Cancel
        _noselect = False
    End Sub
    Private Sub aOffersIn_ChangeRows_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOffersIn_ChangeRows.CustomizePopupWindowParams
        _noselect = True
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferInRows_ChangeGroup As OfferInRows_ChangeGroup = _onew.CreateObject(Of OfferInRows_ChangeGroup)()
        If View.SelectedObjects.Count > 0 Then
            Dim _OfferInRows As OfferInRows = TryCast(View.SelectedObjects(0), OfferInRows)
            _OfferInRows_ChangeGroup.ShortName = _onew.GetObject(_OfferInRows.ShortName)
            _OfferInRows_ChangeGroup.Description = _onew.GetObject(_OfferInRows.Description)
            _OfferInRows_ChangeGroup.DescriptionPrivate = _onew.GetObject(_OfferInRows.DescriptionPrivate)
            _OfferInRows_ChangeGroup.QualityOption = _onew.GetObject(_OfferInRows.QualityOption)
            _OfferInRows_ChangeGroup.Units = _onew.GetObject(_OfferInRows.Units)
            _OfferInRows_ChangeGroup.VAT = _onew.GetObject(_OfferInRows.VAT)

            e.View = Application.CreateDetailView(_onew, "OfferInRows_ChangeGroup_DetailView", False, _OfferInRows_ChangeGroup)
        Else
            Throw New UserFriendlyException("Nincs kijelölt elem!")
        End If
    End Sub
    Private Sub aOffersIn_ChangeRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOffersIn_ChangeRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferInRows_ChangeGroup As OfferInRows_ChangeGroup = TryCast(e.PopupWindow.View.SelectedObjects(0), OfferInRows_ChangeGroup)

        For Each _OfferInRows As OfferInRows In _selection
            If _OfferInRows_ChangeGroup.Change_Description Then
                _OfferInRows.Description = _OfferInRows_ChangeGroup.Description
            End If
            If _OfferInRows_ChangeGroup.Change_DescriptionPrivate Then
                _OfferInRows.DescriptionPrivate = _OfferInRows_ChangeGroup.DescriptionPrivate
            End If
            If _OfferInRows_ChangeGroup.Change_QualityOption Then
                _OfferInRows.QualityOption = _ocur.GetObjectByKey(Of QualityOption)(_OfferInRows_ChangeGroup.QualityOption.Oid)
            End If
            If _OfferInRows_ChangeGroup.Change_ShortName Then
                _OfferInRows.ShortName = _OfferInRows_ChangeGroup.ShortName
            End If
            If _OfferInRows_ChangeGroup.Change_Units Then
                _OfferInRows.Units = _ocur.GetObjectByKey(Of Units)(_OfferInRows_ChangeGroup.Units.Oid)
            End If
            If _OfferInRows_ChangeGroup.Change_VAT Then
                _OfferInRows.VAT = _ocur.GetObjectByKey(Of VAT)(_OfferInRows_ChangeGroup.VAT.Oid)
            End If
            If _OfferInRows_ChangeGroup.Change_RowGroup1 Then
                _OfferInRows.RowGroup1 = _OfferInRows_ChangeGroup.RowGroup1
            End If
            If _OfferInRows_ChangeGroup.Change_RowGroup2 Then
                _OfferInRows.RowGroup2 = _OfferInRows_ChangeGroup.RowGroup2
            End If
        Next
        _ocur.CommitChanges()
        _noselect = False
    End Sub
    Private Sub aInsertServicesToOfferIN_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aInsertServicesToOfferIN.CustomizePopupWindowParams
        Dim _OfferInHeader As OfferInHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferInHeader)

        If _OfferInHeader.IsEditable Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(Services))

            e.View = Application.CreateListView("Services_ListView_Browse", _CollectionSource, False)
        Else
            '----------TESZT MIATT-----------------------------------------
            Throw New Exception("*A bejövő árajánlat végleges vagy stornó!")
        End If
    End Sub
    Private Sub aInsertServicesToOfferIN_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aInsertServicesToOfferIN.Execute
        'A popup-on kiválasztott szolgáltatásokból létrehoz bejövő árajánlati sorokat
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferInHeader As OfferInHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferInHeader)
        Dim _Services As Services
        Dim _OfferInRows_New As OfferInRows

        For Each _Services In e.PopupWindow.View.SelectedObjects
            _OfferInRows_New = _ocur.CreateObject(Of OfferInRows)()

            _OfferInRows_New.RowType = eRowType.IsOther
            _OfferInRows_New.OfferInHeader = _OfferInHeader
            _OfferInRows_New.ShortName = _Services.ShortName
            _OfferInRows_New.Description = _Services.Description
            _OfferInRows_New.Units = _ocur.GetObject(_Services.Units)
            _OfferInRows_New.FinalPriceDEV = _Services.ListPriceDEV
            _OfferInRows_New.FinalPriceHUF = _Services.ListPriceHUF
            _OfferInRows_New.SumPriceDEV = _Services.ListPriceDEV
            _OfferInRows_New.SumPriceHUF = _Services.ListPriceHUF
            _OfferInRows_New.VAT = _ocur.GetObject(_Services.VAT)
            _OfferInRows_New.QTT = 1

            'Indexelés
            If _OfferInHeader.OfferInRows.Count = 0 Then
                _OfferInRows_New.RowIndex = 0
            Else
                _OfferInRows_New.RowIndex = _OfferInHeader.OfferInRows.Count - 1
            End If

            ' _OfferInRows_New.Save()
        Next
        _ocur.CommitChanges()
    End Sub
    Private Sub aBackToRowEdit_OfferIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToRowEdit_OfferIn.Execute
        Dim _OfferInHeader As OfferInHeader = TryCast(View.SelectedObjects(0), OfferInHeader)
        Dim _ListView As ListPropertyEditor = TryCast(View, DetailView).FindItem("OfferInRows") 'Sorok összeszedése
        If _ListView IsNot Nothing Then
            If _ListView.ListView.SelectedObjects.Count > 0 Then
                Dim _OfferInRows As OfferInRows = _ListView.ListView.SelectedObjects(0)

                _OfferInHeader.row_Description = _OfferInRows.Description
                _OfferInHeader.row_FinalPriceDEV = _OfferInRows.FinalPriceDEV
                _OfferInHeader.row_FinalPriceHUF = _OfferInRows.FinalPriceHUF
                _OfferInHeader.row_Installation = _OfferInRows.Installation
                _OfferInHeader.row_Products = _OfferInRows.Product
                _OfferInHeader.row_QTT = _OfferInRows.QTT
                _OfferInHeader.row_RowGroup1 = _OfferInRows.RowGroup1
                _OfferInHeader.row_RowGroup2 = _OfferInRows.RowGroup2
                _OfferInHeader.row_RowType = _OfferInRows.RowType
                _OfferInHeader.row_ShortName = _OfferInRows.ShortName
                _OfferInHeader.row_Units = _OfferInRows.Units
                _OfferInHeader.row_VAT = _OfferInRows.VAT

                View.Refresh()
            Else
                Throw New Exception("*Nincs kiválasztott sor!")
            End If
        End If
    End Sub
    Private Sub aOfferIn_ChangeState_Cancel(sender As Object, e As System.EventArgs) Handles aOfferIn_ChangeState.Cancel
        _noselect = False
    End Sub
    Private Sub aOfferIn_ChangeState_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOfferIn_ChangeState.CustomizePopupWindowParams
        _noselect = True
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferIn_ChangeState_PopUp As OfferIn_ChangeState_PopUp = _onew.CreateObject(Of OfferIn_ChangeState_PopUp)()

        e.View = Application.CreateDetailView(_onew, "OfferIn_ChangeState_PopUp_DetailView", False, _OfferIn_ChangeState_PopUp)
    End Sub
    Private Sub aOfferIn_ChangeState_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOfferIn_ChangeState.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferIn_ChangeState_PopUp As OfferIn_ChangeState_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), OfferIn_ChangeState_PopUp)
        For Each _OfferInHeader As OfferInHeader In _selection
            _OfferInHeader.OfferInState = _ocur.GetObject(_OfferIn_ChangeState_PopUp.OfferInState)
            If _OfferIn_ChangeState_PopUp.OfferInState = eOfferInState.eClosedLOST Then
                _OfferInHeader.OfferLOSTReasonDescription = _OfferIn_ChangeState_PopUp.OfferLOSTReasonDescription
                _OfferInHeader.OfferLOSTReason = _ocur.GetObject(_OfferIn_ChangeState_PopUp.OfferLOSTReason)
            End If
        Next
        _ocur.CommitChanges()
        _noselect = False
    End Sub

    Private Sub aCloneOfferIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCloneOfferIn.Execute
        '//Bejövő ajánlat klónozása a soraival együtt + DetailView benyitása mentés nélkül _onew-en
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferInHeader_Base As OfferInHeader = TryCast(View.SelectedObjects(0), OfferInHeader)
        Dim _OfferInHeader As OfferInHeader
        Dim _OfferInRows As OfferInRows
        Dim _OfferInRows_Base As OfferInRows

        If _OfferInHeader_Base IsNot Nothing Then

            '//Fejléc klónozása
            _OfferInHeader = _onew.CreateObject(Of OfferInHeader)()
            _OfferInHeader.Contact = _onew.GetObject(_OfferInHeader_Base.Contact)
            _OfferInHeader.CRM_Opportunities = _onew.GetObject(_OfferInHeader_Base.CRM_Opportunities)
            _OfferInHeader.CurrencyName = _onew.GetObject(_OfferInHeader_Base.CurrencyName)
            _OfferInHeader.Customers = _onew.GetObject(_OfferInHeader_Base.Customers)
            _OfferInHeader.CustomersFrom = _onew.GetObject(_OfferInHeader_Base.CustomersFrom)

            _OfferInHeader.DateDue = _OfferInHeader_Base.DateDue
            _OfferInHeader.DateExecution = _OfferInHeader_Base.DateExecution
            _OfferInHeader.DatePayment = _OfferInHeader_Base.DatePayment
            _OfferInHeader.Description = _OfferInHeader_Base.Description
            _OfferInHeader.IsEditable = True
            _OfferInHeader.IsStorno = False

            _OfferInHeader.OfferInParameters = _onew.GetObject(_OfferInHeader_Base.OfferInParameters)
            _OfferInHeader.OfferInState = eOfferInState.eInProgress
            _OfferInHeader.PaymentOption = _onew.GetObject(_OfferInHeader_Base.PaymentOption)

            For Each _OfferInRows_Base In _OfferInHeader_Base.OfferInRows
                _OfferInRows = _onew.CreateObject(Of OfferInRows)()
                _OfferInRows.Description = _OfferInRows_Base.Description
                _OfferInRows.DescriptionPrivate = _OfferInRows_Base.DescriptionPrivate
                _OfferInRows.FinalPriceDEV = _OfferInRows_Base.FinalPriceDEV
                _OfferInRows.FinalPriceHUF = _OfferInRows_Base.FinalPriceHUF
                _OfferInRows.Installation = _onew.GetObject(_OfferInRows_Base.Installation)
                _OfferInRows.OfferInHeader = _OfferInHeader
                _OfferInRows.Product = _onew.GetObject(_OfferInRows_Base.Product)
                _OfferInRows.QTT = _OfferInRows_Base.QTT
                _OfferInRows.QualityOption = _onew.GetObject(_OfferInRows_Base.QualityOption)
                _OfferInRows.RowGroup1 = _OfferInRows_Base.RowGroup1
                _OfferInRows.RowGroup2 = _OfferInRows_Base.RowGroup2
                _OfferInRows.RowIndex = _OfferInRows_Base.RowIndex
                _OfferInRows.RowType = _OfferInRows_Base.RowType
                _OfferInRows.ShortName = _OfferInRows_Base.ShortName
                _OfferInRows.Units = _onew.GetObject(_OfferInRows_Base.Units)
                _OfferInRows.VAT = _onew.GetObject(_OfferInRows_Base.VAT)
            Next

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "OfferInHeader_DetailView", True, _OfferInHeader)
        End If
    End Sub

    Private Sub aPrintOfferIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aPrintOfferIn.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferInHeader As OfferInHeader
        Dim _ReportData As ReportData

        If e.SelectedObjects.Count > 0 Then
            _OfferInHeader = TryCast(e.SelectedObjects(0), OfferInHeader)

            If _OfferInHeader IsNot Nothing Then
                _ReportData = _OfferInHeader.OfferInParameters.ReportData
                If _ReportData IsNot Nothing Then
                    Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("OfferInHeader=?", _OfferInHeader), True)
                Else
                    Throw New Exception("*Nincs nyomtatvány beállítva !")
                End If
            End If
        End If
    End Sub

    Private Sub aOfferIn_CreateDeliveryNoteIn_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOfferIn_CreateDeliveryNoteIn.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
            Dim _OfferInRows As OfferInRows
            Dim _OfferInHeader As OfferInHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), OfferInHeader))
            Dim _DeliveryNoteInFromOffersInRowsHeader As New DeliveryNoteInFromOffersInRowsHeader(_onew.Session)
            Dim _QTT_Sum As Double = 0

            _DeliveryNoteInFromOffersInRowsHeader.OfferInHeader = _OfferInHeader
            _DeliveryNoteInFromOffersInRowsHeader.OfferInHeader.DateCreated = GetSQLTime(_onew.Session)

            Dim _OfferInRows_XPQuery As New XPQuery(Of DeliveryNoteInRows)(_onew.Session)
            Dim query = From _DNIR In _OfferInRows_XPQuery
                            Where _DNIR.OfferInRows.OfferInHeader Is _OfferInHeader
                                Group _DNIR By _DNIR.OfferInRows Into g = Group _
                Select New With {Key .OfferInRows = OfferInRows,
                                 Key .QTT_Sum = g.Sum(Function(inv) inv.QTT)}

            For Each _OfferInRows In _OfferInHeader.OfferInRows
                _QTT_Sum = 0
                For Each item In query
                    If item.OfferInRows.Oid = _OfferInRows.Oid Then
                        _QTT_Sum = item.QTT_Sum
                    End If
                Next

                If _OfferInRows.QTT > _QTT_Sum Then
                    Dim _DeliveryNoteInFromOffersInRowsRows As New DeliveryNoteInFromOffersInRowsRows(_onew.Session)

                    _DeliveryNoteInFromOffersInRowsRows.OfferInRows = _OfferInRows
                    _DeliveryNoteInFromOffersInRowsRows.DeliveryNoteInFromOrderOutRowsHeader = _DeliveryNoteInFromOffersInRowsHeader
                    _DeliveryNoteInFromOffersInRowsRows.QTT_Process = _OfferInRows.QTT - _QTT_Sum
                    _DeliveryNoteInFromOffersInRowsRows.Save()
                End If
            Next

            _DeliveryNoteInFromOffersInRowsHeader.Save()
            _onew.CommitChanges()

            e.View = Application.CreateDetailView(_onew, _DeliveryNoteInFromOffersInRowsHeader, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aOfferIn_CreateDeliveryNoteIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOfferIn_CreateDeliveryNoteIn.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _DeliveryNoteInHeader As New DeliveryNoteInHeader(_ocur.Session)
            Dim _DeliveryNoteInFromOffersInRowsHeader As DeliveryNoteInFromOffersInRowsHeader = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), DeliveryNoteInFromOffersInRowsHeader))
            Dim _DeliveryNoteInFromOffersInRowsRows_ListEditor As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("DeliveryNoteInFromOffersInRowsRows")
            Dim _DeliveryNoteInType As DeliveryNoteInType = _ocur.GetObjectByKey(Of DeliveryNoteInType)(TryCast(e.PopupWindow.View.SelectedObjects(0), DeliveryNoteInFromOffersInRowsHeader).DeliveryNoteInType.Oid)

            _DeliveryNoteInHeader.CurrencyName = _DeliveryNoteInFromOffersInRowsHeader.OfferInHeader.CurrencyName
            _DeliveryNoteInHeader.CustomersFrom = _DeliveryNoteInFromOffersInRowsHeader.OfferInHeader.CustomersFrom
            _DeliveryNoteInHeader.PaymentOption = _DeliveryNoteInFromOffersInRowsHeader.OfferInHeader.PaymentOption
            _DeliveryNoteInHeader.QualityOption = _DeliveryNoteInFromOffersInRowsHeader.OfferInHeader.QualityOption
            _DeliveryNoteInHeader.Customers = _DeliveryNoteInFromOffersInRowsHeader.OfferInHeader.Customers
            _DeliveryNoteInHeader.DeliveryNoteInType = _DeliveryNoteInType


            For _ik = 0 To _DeliveryNoteInFromOffersInRowsRows_ListEditor.ListView.SelectedObjects.Count - 1
                Dim _DeliveryNoteInRows As DeliveryNoteInRows = _ocur.CreateObject(Of DeliveryNoteInRows)()
                Dim _QTT_Process As Double = 0
                Dim _DeliveryNoteInFromOffersInRowsRows As DeliveryNoteInFromOffersInRowsRows = _ocur.GetObject(TryCast(_DeliveryNoteInFromOffersInRowsRows_ListEditor.ListView.SelectedObjects(_ik), DeliveryNoteInFromOffersInRowsRows))
                _QTT_Process = TryCast(_DeliveryNoteInFromOffersInRowsRows_ListEditor.ListView.SelectedObjects(_ik), DeliveryNoteInFromOffersInRowsRows).QTT_Process 'szándék

                _DeliveryNoteInRows.DeliveryNoteInHeader = _DeliveryNoteInHeader
                _DeliveryNoteInRows.OfferInRows = _DeliveryNoteInFromOffersInRowsRows.OfferInRows
                _DeliveryNoteInRows.Products = _DeliveryNoteInFromOffersInRowsRows.OfferInRows.Product
                _DeliveryNoteInRows.QTT = _QTT_Process

                _DeliveryNoteInRows.VAT = _DeliveryNoteInFromOffersInRowsRows.OfferInRows.Product.VAT
                _DeliveryNoteInRows.Units = _DeliveryNoteInFromOffersInRowsRows.OfferInRows.Product.Units


                _DeliveryNoteInRows.ListPriceHUF = _DeliveryNoteInFromOffersInRowsRows.OfferInRows.FinalPriceHUF
                _DeliveryNoteInRows.ListPriceDEV = _DeliveryNoteInFromOffersInRowsRows.OfferInRows.FinalPriceDEV

                _DeliveryNoteInRows.DiscountPriceHUF = 0
                _DeliveryNoteInRows.DiscountPriceDEV = 0

                _DeliveryNoteInRows.RowIndex = _ik + 1

                '_DeliveryNoteInRows.Save()
            Next
            '_DeliveryNoteInHeader.Save()
            _ocur.CommitChanges()

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

            _ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "DeliveryNoteInHeader_DetailView", False, _onew.GetObject(_DeliveryNoteInHeader))
            _ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild
            _ShowViewParameters.TargetWindow = TargetWindow.NewWindow
            Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub

    Private Sub aOfferIn_RegenerateDEV_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOfferIn_RegenerateDEV.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferIn_RegenerateDEV_Popup As OfferIn_RegenerateDEV_Popup = _onew.CreateObject(Of OfferIn_RegenerateDEV_Popup)()
        Dim _OfferInHeader As OfferInHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), OfferInHeader))

        Dim _CurrencyRate_Collection As New XPCollection(Of CurrencyRate)(_onew.Session, CriteriaOperator.Parse("CurrencyName=? and IsMNB=True", _OfferInHeader.CurrencyName))
        Dim _CurrencyRate As CurrencyRate
        _CurrencyRate_Collection.Sorting.Add(New SortProperty("DateValid", DB.SortingDirection.Descending))

        _OfferIn_RegenerateDEV_Popup.CurrencyName = _OfferInHeader.CurrencyName
        _OfferIn_RegenerateDEV_Popup.PriceMode = ePriceMode.eDEVToHUF

        For Each _CurrencyRate In _CurrencyRate_Collection
            _OfferIn_RegenerateDEV_Popup.CurrencyRate = _CurrencyRate.RateAverage
            _OfferIn_RegenerateDEV_Popup.DateExecution = _CurrencyRate.DateValid
            Exit For
        Next

        e.View = Application.CreateDetailView(_onew, "OfferIn_RegenerateDEV_Popup_DetailView", False, _OfferIn_RegenerateDEV_Popup)

    End Sub

    Private Sub aOfferIn_RegenerateDEV_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOfferIn_RegenerateDEV.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferInHeader As OfferInHeader = _ocur.GetObject(TryCast(View.SelectedObjects(0), OfferInHeader))
        Dim _OfferIn_RegenerateDEV_Popup As OfferIn_RegenerateDEV_Popup = TryCast(e.PopupWindow.View.SelectedObjects(0), OfferIn_RegenerateDEV_Popup)
        Dim _OfferInRows As OfferInRows

        For Each _OfferInRows In _OfferInHeader.OfferInRows
            If _OfferIn_RegenerateDEV_Popup.PriceMode = ePriceMode.eDEVToHUF Then
                _OfferInRows.FinalPriceHUF = Math.Round(_OfferInRows.FinalPriceDEV * _OfferIn_RegenerateDEV_Popup.CurrencyRate, 2, MidpointRounding.AwayFromZero)
            Else
                If _OfferIn_RegenerateDEV_Popup.CurrencyRate <> 0 Then
                    _OfferInRows.FinalPriceDEV = Math.Round(_OfferInRows.FinalPriceHUF / _OfferIn_RegenerateDEV_Popup.CurrencyRate, 2, MidpointRounding.AwayFromZero)
                End If
            End If
        Next
        _ocur.CommitChanges()
    End Sub
    Private Sub aOfferInHeader_AddOfferOutHeader_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aOfferInHeader_AddOfferOutHeader.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferInHeader As OfferInHeader = TryCast(View.SelectedObjects(0), OfferInHeader)

        Dim _OfferOutHeader_CollectionSource As New CollectionSource(_onew, GetType(OfferOutHeader))

        _OfferOutHeader_CollectionSource.BeginUpdateCriteria()
        _OfferOutHeader_CollectionSource.Criteria.Add("CRM", CriteriaOperator.Parse("CRM_Opportunities=?", _onew.GetObject(_OfferInHeader.CRM_Opportunities)))
        _OfferOutHeader_CollectionSource.EndUpdateCriteria()

        e.View = Application.CreateListView("OfferOutHeader_ListView_OfferOutHeaderInHeader", _OfferOutHeader_CollectionSource, False)
    End Sub
    Private Sub aOfferInHeader_AddOfferOutHeader_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aOfferInHeader_AddOfferOutHeader.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferInHeader As OfferInHeader = TryCast(View.SelectedObjects(0), OfferInHeader)

        For Each _OfferOutHeader As OfferOutHeader In e.PopupWindow.View.SelectedObjects
            Dim _OfferOutHeaderInHeader As New OfferOutHeaderInHeader(_ocur.Session)
            With _OfferOutHeaderInHeader
                .OfferInHeader = _OfferInHeader
                .OfferOutHeader = _ocur.GetObject(_OfferOutHeader)
            End With
        Next

        _ocur.CommitChanges()
    End Sub
End Class
