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

Public Class ContractsVC
    Inherits DevExpress.ExpressApp.ViewController
    Protected _selection As ArrayList
    Dim _InvoiceRowsSelection As ArrayList = New ArrayList
    Protected _noselect As Boolean = False

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

        Frame.GetController(Of ContractsVC).aMarkAsReady.Active.SetItemValue("", False) ' -- Megjelölés Kész-nek
        Frame.GetController(Of ContractsVC).aSendBackToEdit.Active.SetItemValue("", False) ' -- Visszaküldés szerkeszthetőnek
        Frame.GetController(Of ContractsVC).aMarkAsSigned.Active.SetItemValue("", False) ' -- Megjelölés aláírtnak
        Frame.GetController(Of ContractsVC).aMarkAsClosed.Active.SetItemValue("", False) ' -- Megjelölés lezártnak
        Frame.GetController(Of ContractsVC).aMarkAsTerminating_Old.Active.SetItemValue("", False) ' -- Megjelölés megszakítottnak
        Frame.GetController(Of ContractsVC).aMarkAsWaiting.Active.SetItemValue("", False) ' -- Megjelölés várakozónak
        Frame.GetController(Of ContractsVC).aCloneContractsRows.Active.SetItemValue("", False) ' -- Kijelölt szerzodési sorok klónozása
        Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRows.Active.SetItemValue("", False) ' -- Számla készítése szerzodés sorokból
        Frame.GetController(Of ContractsVC).aCloneSelectedContracts.Active.SetItemValue("", False) ' -- Kijelölt szerzodések klónozása
        Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRowsCustomRate.Active.SetItemValue("", False) ' -- Kijelölt szerzodési sorokból Díjbekérot készít
        Frame.GetController(Of ContractsVC).aCreateFinalInvoiceFromContract.Active.SetItemValue("", False)
        Frame.GetController(Of ContractsVC)().aNewContractRows.Active.SetItemValue("", False)
        Frame.GetController(Of ContractsVC)().aMergeContractRows.Active.SetItemValue("", False)
        Frame.GetController(Of ContractsVC)().aDivideContractRows.Active.SetItemValue("", False)

        If View.Id Like "Contracts_ListView*" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If

        If View.Id = "ContractRows_ListView_Invoice_Advance" Then
            AddHandler Frame.GetController(Of DeleteObjectsViewController).DeleteAction.Executing, AddressOf ContractRows_ListView_Invoice_Advance_DeleteAction_Executing
        End If

        If View.Id Like "Contracts_ListView_*" Then
            Frame.GetController(Of ContractsVC).aCloneSelectedContracts.Active.SetItemValue("", True)
            AddHandler View.SelectionChanged, AddressOf view_SelectionChanged2
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteria = _
                "isnull(CertificateofContractRows)=True and isnull(RegistryRowsFinancialControlling)=True and isnull(InvoiceRows)=True and Contracts.State = 0"
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteriaMode = TargetObjectsCriteriaMode.TrueForAll
        End If
        If View.Id Like "*ContractRows_ListView*" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf DeleteAction_Executing
        End If

        '-------------------- LISTVIEW -------------------------------------------------------------------------------------------------------------------------
        If View.Id = "ContractRows_RegistryRowsFinancialControlling_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "ContractRows_ListView_Invoice_Advance" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", False)
            'Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC).aDivideContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aMergeContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", False)

            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aCreateFinalInvoiceFromContract.Active.SetItemValue("", False)

            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRowsCustomRate.Active.SetItemValue("", True) 'Ez is itt volt...
        End If
        If View.Id = "ContractRows_ListView_Invoice_CustomRate" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRows.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aDivideContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aMergeContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRowsCustomRate.Active.SetItemValue("", True)
        End If
        If View.Id = "ContractRows_ListView_Invoice_Not_MileStone" Or View.Id = "ContractRows_ListView_Invoice_MileStone" Then
            Frame.GetController(Of ContractsVC)().aGenerateInvoicefromContractsRows.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aCreateFinalInvoiceFromContract.Active.SetItemValue("", True)
        End If

        If View.Id = "ContractRows_ListView_Invoiced" Then
            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRows.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", False)
        End If
        If View.Id = "ContractRows_ListView_Invoice_IsProforma" Then
            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRows.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", False)
        End If
        If View.Id = "ContractRows_ListView_Invoice_Finalized" Then
            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRows.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", False)
        End If

        If View.Id = "Contracts_ListView_Signed" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "Contracts_ContractRows_ListView" Then
            Frame.GetController(Of ContractsVC)().aNewContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC)().aMergeContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC)().aDivideContractRows.Active.SetItemValue("", False)

            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aCloneContractsRows.Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)

            Dim nv As ListView = CType(View, ListView)
            Dim cs As PropertyCollectionSource = CType(nv.CollectionSource, PropertyCollectionSource)
            Dim _Contracts As Contracts = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Contracts)

            If _Contracts IsNot Nothing Then

                If _Contracts.ContractTemplate IsNot Nothing Then
                    If _Contracts.ContractTemplate.IsTechnical = True Then
                        Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
                        Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
                        Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
                        Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
                        Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
                    End If
                End If
            End If

            AddHandler cs.MasterObjectChanged, AddressOf cs_MasterObjectChanged
            AddHandler View.SelectionChanged, AddressOf ContractRowsListView_selection_changed
        End If
        If View.Id = "ContractProductRows_ListView_Invoice" Then
            Frame.GetController(Of ContractsVC)().aGenerateInvoicefromContractsRows.Active.SetItemValue("", True)
        End If
        If View.Id = "Contracts_ListView_Technical" Then
            Frame.GetController(Of ContractsVC).aCloneSelectedContracts.Active.SetItemValue("", False)
        End If

        If View.Id = "InvoiceHeader_DescriptionChanger_PopUp_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "Contracts_ListView_Subcontractor" Then
            Frame.GetController(Of ContractsVC).aCloneSelectedContracts.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aModifyContractData.Active.SetItemValue("", False)
        End If

        If View.Id = "Contracts_ListView_Support" Then
            Frame.GetController(Of ContractsVC).aCloneSelectedContracts.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aModifyContractData.Active.SetItemValue("", False)
        End If
        '-------------------- DETAILVIEW -----------------------------------------------------------------------------------------------------------------------
        If View.Id = "ContractRows_DetailView" Then
            If TryCast(View.SelectedObjects(0), ContractRows).InvoiceRows IsNot Nothing Then

                Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
                Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", False)
            End If
        End If
        If View.Id = "Contracts_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", True)

            Dim _Contracts = TryCast(View.SelectedObjects(0), Contracts)
            If _Contracts IsNot Nothing Then
                If _Contracts.State = State.Ready Or _Contracts.State = State.Signed Then
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
                End If
            End If
            If _Contracts.State = State.Preparation Then
                Frame.GetController(Of ContractsVC).aMarkAsReady.Active.SetItemValue("", True)
                Frame.GetController(Of ContractsVC).aSendBackToEdit.Active.SetItemValue("", False)
                Frame.GetController(Of ContractsVC).aMarkAsSigned.Active.SetItemValue("", False)
            End If
            If _Contracts.State = State.Ready Then
                Frame.GetController(Of ContractsVC).aMarkAsSigned.Active.SetItemValue("", True)
                Frame.GetController(Of ContractsVC).aSendBackToEdit.Active.SetItemValue("", True)
                Frame.GetController(Of ContractsVC).aMarkAsReady.Active.SetItemValue("", False)
            End If
            If _Contracts.State = State.Signed Then
                Frame.GetController(Of ContractsVC).aMarkAsReady.Active.SetItemValue("", False)
                Frame.GetController(Of ContractsVC).aSendBackToEdit.Active.SetItemValue("", False)
                Frame.GetController(Of ContractsVC).aMarkAsSigned.Active.SetItemValue("", False)
                Frame.GetController(Of ContractsVC).aMarkAsClosed.Active.SetItemValue("", True)
                Frame.GetController(Of ContractsVC).aMarkAsTerminating_Old.Active.SetItemValue("", True)
                Frame.GetController(Of ContractsVC).aMarkAsWaiting.Active.SetItemValue("", True)
            End If

            If _Contracts IsNot Nothing Then
                If _Contracts.ContractTemplate IsNot Nothing Then

                    If _Contracts.ContractTemplate.IsTechnical = True Then '! Technikai szerződés esetén !!!
                        Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
                        Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
                        Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
                        Frame.GetController(Of ContractsVC).aMarkAsClosed.Active.SetItemValue("", False)
                        Frame.GetController(Of ContractsVC).aMarkAsReady.Active.SetItemValue("", False)
                        Frame.GetController(Of ContractsVC).aMarkAsSigned.Active.SetItemValue("", False)
                        Frame.GetController(Of ContractsVC).aMarkAsTerminating_Old.Active.SetItemValue("", False)
                        Frame.GetController(Of ContractsVC).aMarkAsWaiting.Active.SetItemValue("", False)
                    End If
                End If
            End If
        End If
        If View.Id = "ContractRowsToFinalInvoice_DetailView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf ContractRowsToFinalInvoice_AcceptAction_Executing
        End If


        '-------------------- LookupListView -----------------------------------------------------------------------------------------------------------------------
        If View.Id = "ContractRows_LookupListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
        End If
        'A csoportos számlázás kijelölésváltozás figyelése, hogy csak azonos vállalattal rendelkezo sorokat lehessen kiválasztani
        AddHandler View.SelectionChanged, AddressOf view_SelectionChanged
        'Ha létezik proforma vagy véglegesített számla sor a szerzodéshez akkor ne lehessen törölni
    End Sub

    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
        If View.Id = "ContractRows_RegistryRowsFinancialControlling_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "Contracts_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "ContractRows_ListView_Invoice_Advance" Then
            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRowsCustomRate.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC).aCreateFinalInvoiceFromContract.Active.SetItemValue("", False)

            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", True)

            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC).aCreateFinalInvoiceFromContract.Active.SetItemValue("", True)


            Frame.GetController(Of ContractsVC).aDivideContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC).aMergeContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", True)

            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC).aCreateFinalInvoiceFromContract.Active.SetItemValue("", True)
        End If
        If View.Id = "ContractRows_ListView_Invoice_CustomRate" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC).aGenerateInvoicefromContractsRowsCustomRate.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC).aDivideContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC).aMergeContractRows.Active.SetItemValue("", True)
        End If
        If View.Id = "ContractRowsToFinalInvoice_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf ContractRowsToFinalInvoice_AcceptAction_Executing
        End If
        If View.Id = "ContractRows_ListView_Invoice_Advance" Then
            RemoveHandler Frame.GetController(Of DeleteObjectsViewController).DeleteAction.Executing, AddressOf ContractRows_ListView_Invoice_Advance_DeleteAction_Executing
        End If


    End Sub
    Sub ContractRowsToFinalInvoice_AcceptAction_Executing(sender As Object, e As System.ComponentModel.CancelEventArgs)
        _noselect = True
    End Sub
    Sub ContractRowsListView_selection_changed(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _ContractRows As ContractRows
        For Each _ContractRows In View.SelectedObjects
            If _ContractRows.InvoiceRows IsNot Nothing Then
                Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
            End If
        Next
    End Sub
    Private Sub cs_MasterObjectChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim _Contracts As Contracts = TryCast((CType(sender, PropertyCollectionSource)).MasterObject, Contracts)
        If _Contracts Is Nothing Then Exit Sub
        If _Contracts.State = State.Signed Or _Contracts.State = State.Ready Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
        End If
    End Sub
    Sub DeleteAction_Executing(sender As Object, e As System.ComponentModel.CancelEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractRows As ContractRows = TryCast(View.CurrentObject, ContractRows)
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling
        Dim _RegistryRowsFinancialControlling_Collection As XPCollection(Of RegistryRowsFinancialControlling)

        If _ContractRows IsNot Nothing Then

            If _ContractRows.Contracts.ContractTemplate.PartnerType = ePartnerType.ePayabels Then
                If _ContractRows.CertificateofContractRows IsNot Nothing Then
                    e.Cancel = True
                    Throw New Exception("*Ehhez a szerződési sorhoz aktív teljesítési igazolás van rendelve !")
                    Exit Sub
                End If
                If _ContractRows.RegistryRowsFinancialControlling IsNot Nothing Then
                    e.Cancel = True
                    Throw New Exception("*Ehhez a szerződési sorhoz aktív számla igazolás van rendelve !")
                    Exit Sub
                End If
            ElseIf _ContractRows.Contracts.ContractTemplate.PartnerType = ePartnerType.eReceivables Then
                If _ContractRows.InvoiceRows IsNot Nothing Then
                    e.Cancel = True
                    Throw New Exception("*Ehhez a szerződési sorhoz aktív számla van rendelve !")
                    Exit Sub
                End If
                If _ContractRows.InvoiceRowsAdvance IsNot Nothing Then
                    e.Cancel = True
                    Throw New Exception("*Ehhez a szerződési sorhoz aktív számla van rendelve !")
                    Exit Sub
                End If
                _RegistryRowsFinancialControlling_Collection = New XPCollection(Of RegistryRowsFinancialControlling) _
                    (_ocur.Session, CriteriaOperator.Parse("ReInvoice_ContractRows.Oid=?", _ContractRows.Oid))
                For Each _RegistryRowsFinancialControlling In _RegistryRowsFinancialControlling_Collection
                    _RegistryRowsFinancialControlling.ReInvoice_ContractRows = Nothing
                Next
            End If

        End If
    End Sub

    Private Sub view_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If View Is Nothing Then Exit Sub
        If View.Id = "ContractRows_ListView_Invoice_Not_MileStone" Or _
            View.Id = "ContractRows_ListView_Invoice_MileStone" Or _
            View.Id = "ContractRows_ListView_Invoice_Advance" Or _
            View.Id = "ContractRows_ListView_Invoice_CustomRate" Or _
            View.Id = "ContractProductRows_ListView_Invoice" Then

            Dim _ContractRows As ContractRows
            If _noselect = False Then
                _selection.Clear()
                For Each _ContractRows In View.SelectedObjects
                    _selection.Add(_ContractRows)
                Next
            End If

            'If View.SelectedObjects.Count > 0 Then
            '    Dim _FirstContractRowSelected As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)
            '    Dim _LastContractRowSelected As ContractRows = TryCast(View.SelectedObjects(View.SelectedObjects.Count - 1), ContractRows)

            '    If _FirstContractRowSelected.Contracts.CustomersFrom IsNot _LastContractRowSelected.Contracts.CustomersFrom Then
            '        Frame.GetController(Of ContractsVC)().aGenerateInvoicefromContractsRows.Active.SetItemValue("", False)
            '    Else
            '        Frame.GetController(Of ContractsVC)().aGenerateInvoicefromContractsRows.Active.SetItemValue("", True)
            '    End If
            'End If
        End If

        If View.Id Like "Contracts_ListView_*" Then
            Dim _Contracts As Contracts
            If _noselect = False Then
                _selection.Clear()
                For Each _Contracts In View.SelectedObjects
                    _selection.Add(_Contracts)
                Next
            End If
        End If
        If View.Id = "ContractRowsToFinalInvoice_InvoiceRows_ListView" Then
            Dim _InvoiceRows As InvoiceRows
            If _noselect = False Then
                _selection.Clear()
                For Each _InvoiceRows In View.SelectedObjects
                    _selection.Add(_InvoiceRows)
                Next
            End If
        End If

    End Sub
    Private Sub view_SelectionChanged2(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _Contracts As Contracts
        If View.Id Like "Contracts_ListView_*" And _noselect = False Then
            _selection.Clear()
            For Each _Contracts In View.SelectedObjects
                _selection.Add(_Contracts)
            Next
        End If
    End Sub

    Private Sub aCloneContractsRows_Cancel(ByVal sender As Object, ByVal e As System.EventArgs) Handles aCloneContractsRows.Cancel
        _noselect = False
    End Sub
    Private Sub aCloneContractsRows_CustomizePopupWindowParams(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCloneContractsRows.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractRowsCloneView As ContractRowsCloneView = _onew.CreateObject(Of ContractRowsCloneView)()

        _ContractRowsCloneView.DayOfExecution = eDayOfMonth.e1
        _ContractRowsCloneView.ActionFrequency = eActionFrequency.eMonthly
        _ContractRowsCloneView.DayOfExecutionInWeek = eDayOfWeek.eMonday
        _ContractRowsCloneView.ActionCount = 1

        e.View = Application.CreateDetailView(_onew, "ContractRowsCloneView_DetailView", False, _ContractRowsCloneView)
    End Sub
    Private Sub aCloneContractsRows_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCloneContractsRows.Execute
        '--- Az első kijelölt szerződési sor alapján klónoz sorokat--------------------------------
        Dim _ocur_original As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur_original.Session.DataLayer)

        Dim _ContractRows As ContractRows
        Dim _ContractRows_New As ContractRows
        Dim _ContractRows_Selected As ContractRows
        Dim _ContractRowsCloneView As ContractRowsCloneView = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractRowsCloneView)

        Dim _DateStart As Date
        Dim _DateCur As Date
        Dim _DiffDay As Long

        For Each _ContractRows_Selected In e.SelectedObjects
            _ContractRows = _uow.GetObjectByKey(Of ContractRows)(_ContractRows_Selected.Oid)
            For i = 1 To _ContractRowsCloneView.ActionCount
                _ContractRows_New = New ContractRows(_uow)
                If i = 1 Then _DateStart = _ContractRows.DateExecution
                _ContractRows_New.Contracts = _ContractRows.Contracts
                _ContractRows_New.Controlling = _ContractRows.Controlling
                _ContractRows_New.CurrencyName = _ContractRows.CurrencyName
                _ContractRows_New.CurrencyRate = _ContractRows.CurrencyRate
                _ContractRows_New.Description = _ContractRows.Description
                _ContractRows_New.NettoPriceDEV = _ContractRows.NettoPriceDEV
                _ContractRows_New.NettoPriceHUF = _ContractRows.NettoPriceHUF
                _ContractRows_New.PaymentOption = _ContractRows.PaymentOption
                _ContractRows_New.QTT = _ContractRows.QTT
                _ContractRows_New.QualityOption = _ContractRows.QualityOption
                _ContractRows_New.ShipmentOption = _ContractRows.ShipmentOption
                _ContractRows_New.ShortName = _ContractRows.ShortName
                _ContractRows_New.Units = _ContractRows.Units
                _ContractRows_New.ContractRowsType = _ContractRows.ContractRowsType
                _ContractRows_New.DateExecution = _ContractRows.DateExecution
                _ContractRows_New.MileStone = _ContractRows.MileStone
                _ContractRows_New.VAT = _ContractRows.VAT
                _ContractRows_New.UniqueObjects = _ContractRows.UniqueObjects
                _ContractRows_New.CostHolder = _ContractRows.CostHolder
                _ContractRows_New.CostPlace = _ContractRows.CostPlace
                _ContractRows_New.JobNumberObjects = _ContractRows.JobNumberObjects

                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eDaily Then
                    _DateCur = DateAdd(DateInterval.Day, i, _DateStart)
                End If
                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eMonthly Then
                    If _ContractRowsCloneView.DayOfExecution = eDayOfMonth.eLastDay Then
                        _DateCur = DateAdd(DateInterval.Month, i + 1, _DateStart)
                        _DateCur = DateAdd(DateInterval.Day, -1, CDate(Year(_DateCur) & "/" & Month(_DateCur) & "/01"))
                    Else
                        _DateCur = DateAdd(DateInterval.Month, i, _DateStart)
                        _DateCur = CDate(Year(_DateCur) & "/" & Month(_DateCur) & "/" & _ContractRowsCloneView.DayOfExecution)
                    End If
                End If
                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eWeekly Then
                    _DiffDay = _ContractRowsCloneView.DayOfExecutionInWeek - DatePart(DateInterval.Weekday, _DateStart)
                    _DiffDay += 1
                    _DateCur = DateAdd(DateInterval.WeekOfYear, i, _DateStart)
                    _DateCur = DateAdd(DateInterval.Day, _DiffDay, _DateCur)

                End If
                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eTwoWeekly Then
                    _DiffDay = _ContractRowsCloneView.DayOfExecutionInWeek - DatePart(DateInterval.Weekday, _DateStart)
                    _DiffDay += 1
                    _DateCur = DateAdd(DateInterval.WeekOfYear, i * 2, _DateStart)
                    _DateCur = DateAdd(DateInterval.Day, _DiffDay, _DateCur)
                End If
                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eQuarter Then
                    _DateCur = DateAdd(DateInterval.Quarter, i, _DateStart)
                    _DateCur = CDate(Year(_DateCur) & "/" & Month(_DateCur) & "/" & _ContractRowsCloneView.DayOfExecution)
                End If
                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eYear Then
                    _DateCur = DateAdd(DateInterval.Year, i, _DateStart)
                    _DateCur = CDate(Year(_DateCur) & "/" & Month(_DateCur) & "/" & _ContractRowsCloneView.DayOfExecution)
                End If
                _ContractRows_New.DateExecution = _DateCur
            Next
        Next
        _uow.CommitChanges()

        '-- NESTED FRAME REFRESH -----------------------------------------------------------------------------------------------------------------------
        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If
    End Sub


    Private Sub aGenerateInvoicefromContractsRows_Cancel(ByVal sender As Object, ByVal e As System.EventArgs) Handles aGenerateInvoicefromContractsRows.Cancel
        _noselect = False
    End Sub
    Private Sub aGenerateInvoicefromContractsRows_CustomizePopupWindowParams(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGenerateInvoicefromContractsRows.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractsRowsToInvoice As ContractsRowsToInvoice = _ocur.CreateObject(Of ContractsRowsToInvoice)()
        Dim _ContractsRowsToInvoice_New As ContractsRowsToInvoice

        _ContractsRowsToInvoice.Description = ""
        _ContractsRowsToInvoice.CurrencyRate = 1
        _ContractsRowsToInvoice.DateExecution = GetSQLTime(_ocur.Session)
        _ContractsRowsToInvoice.BankInformation = Nothing
        _ContractsRowsToInvoice.ContractRows = _selection(0)

        _noselect = True
        _ContractsRowsToInvoice.Save()
        _ocur.CommitChanges()

        _ContractsRowsToInvoice_New = _onew.GetObject(_ContractsRowsToInvoice)

        e.View = Application.CreateDetailView(_onew, "ContractsRowsToInvoice_DetailView", False, _ContractsRowsToInvoice_New)

    End Sub
    Private Sub aGenerateInvoicefromContractsRows_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGenerateInvoicefromContractsRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractRowsCollection As New XPCollection(Of ContractRows)(_ocur.Session, CriteriaOperator.Parse("1=2"))
        Dim _ContractsRowsToInvoice As ContractsRowsToInvoice = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractsRowsToInvoice)
        Dim _ContractRows As ContractRows
        Dim _ContractRows_Selection As ContractRows
        Dim _InvoiceRows_new As InvoiceRows
        Dim _InvoiceRows_Array As New ArrayList()

        On Error GoTo CheckError

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        ' -- létrehozunk egy kollekciót a kiválasztott elemek tömbjébol
        For Each _ContractRows_Selection In _selection
            _ContractRows = _ocur.GetObject(_ContractRows_Selection)
            _ContractRowsCollection.Add(_ContractRows)
        Next
        _InvoiceRows_Array.Clear()

        _noselect = False
        ' -- rendezési feltételeket adunk a létrehozott kollekciónak
        Dim sortCollection As SortingCollection = New SortingCollection()
        sortCollection.Add(New SortProperty("Contracts.CustomersFrom.Name", DB.SortingDirection.Ascending))
        sortCollection.Add(New SortProperty("Contracts.Customers.Name", DB.SortingDirection.Ascending))
        sortCollection.Add(New SortProperty("DateExecution", DB.SortingDirection.Ascending))
        sortCollection.Add(New SortProperty("CurrencyName.ShortName", DB.SortingDirection.Ascending))
        sortCollection.Add(New SortProperty("PaymentOption.ShortName", DB.SortingDirection.Ascending))
        _ContractRowsCollection.Sorting = sortCollection
        Dim i As Long = 0
        Dim _IsHeaderCreated As Boolean = False
        Dim _IsNextWhile As Boolean = True
        Dim _NG As NumberGenerator
        Do While _IsNextWhile
            Dim _InvoiceHeader As InvoiceHeader
            Dim _InvoiceRows As InvoiceRows = _ocur.CreateObject(Of InvoiceRows)()
            _InvoiceHeader = _ocur.CreateObject(GetType(InvoiceHeader))
            _InvoiceHeader.PaymentOption = _ContractRowsCollection(i).PaymentOption
            _InvoiceHeader.ShipmentOption = _ContractRowsCollection(i).ShipmentOption

            'Ha a sor típusa előleg akkor az előleg számla típusát kell vennünk a szerz.sablonnak egyébként a rendeset
            If _ContractRowsCollection(i).ContractRowsType = eContractRowsType.eAdvance Then
                _InvoiceHeader.InvoiceType = _ContractRowsCollection(i).Contracts.ContractTemplate.InvoiceTypeAdvanced
            Else
                _InvoiceHeader.InvoiceType = _ContractRowsCollection(i).Contracts.ContractTemplate.InvoiceType
            End If
            If _InvoiceHeader.InvoiceType.NumberGeneratorProforma IsNot Nothing Then
                _NG = _InvoiceHeader.InvoiceType.NumberGeneratorProforma
                _InvoiceHeader.DocumentNumberProforma = _NG.GetNewNumber(_NG)
            End If
            _InvoiceHeader.IsProforma = True
            _InvoiceHeader.IsEditable = False
            _InvoiceHeader.CustomersFrom = _ContractRowsCollection(i).Contracts.CustomersFrom
            _InvoiceHeader.Customers = _ContractRowsCollection(i).Contracts.Customers
            _InvoiceHeader.Description = _ocur.GetObject(_ContractsRowsToInvoice.Description)
            _InvoiceHeader.CurrencyName = _ContractRowsCollection(i).CurrencyName
            _InvoiceHeader.CurrencyRate = _ocur.GetObject(_ContractsRowsToInvoice.CurrencyRate)
            _InvoiceHeader.BankInformation = _ocur.GetObject(_ContractsRowsToInvoice.BankInformation)

            '-- Próbaszámla miatt el kell menteni a tényleges adatokat --------------------------------------------------------
            _InvoiceHeader.Saved_Customers_Name = _ContractRowsCollection(i).Contracts.Customers.Name.ToString
            _InvoiceHeader.Saved_Customers_Address_Country = _ContractRowsCollection(i).Contracts.Customers.Address1.Country.ToString
            _InvoiceHeader.Saved_Customers_Address_ZipPostal = _ContractRowsCollection(i).Contracts.Customers.Address1.ZipPostal.ToString
            _InvoiceHeader.Saved_Customers_Address_Street = _ContractRowsCollection(i).Contracts.Customers.Address1.Street.ToString
            _InvoiceHeader.Saved_Customers_Address_City = _ContractRowsCollection(i).Contracts.Customers.Address1.City.ToString
            _InvoiceHeader.Saved_Customers_Address_StateProvince = _ContractRowsCollection(i).Contracts.Customers.Address1.StateProvince.ToString
            _InvoiceHeader.Saved_Customers_VATNumber = _ContractRowsCollection(i).Contracts.Customers.VATNumber.ToString
            _InvoiceHeader.Saved_Customers_VATNumberEU = _ContractRowsCollection(i).Contracts.Customers.VATNumberEU.ToString

            _InvoiceHeader.Saved_CustomersFrom_Name = _ContractRowsCollection(i).Contracts.CustomersFrom.Name.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_ZipPostal = _ContractRowsCollection(i).Contracts.CustomersFrom.Address1.ZipPostal.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_Street = _ContractRowsCollection(i).Contracts.CustomersFrom.Address1.Street.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_City = _ContractRowsCollection(i).Contracts.CustomersFrom.Address1.City.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_StateProvince = _ContractRowsCollection(i).Contracts.CustomersFrom.Address1.StateProvince.ToString
            _InvoiceHeader.Saved_CustomersFrom_VATNumber = _ContractRowsCollection(i).Contracts.CustomersFrom.VATNumber.ToString
            _InvoiceHeader.Saved_CustomersFrom_VATNumberEU = _ContractRowsCollection(i).Contracts.CustomersFrom.VATNumberEU.ToString

            _InvoiceHeader.Saved_CustomersFrom_Bank_ShortName = _InvoiceHeader.BankInformation.FullBankAccount.ToString
            _InvoiceHeader.Saved_CustomersFrom_Bank_IBAN = _InvoiceHeader.BankInformation.IBAN.ToString
            _InvoiceHeader.Saved_CustomersFrom_Bank_SWIFT = _InvoiceHeader.BankInformation.SWIFT.ToString
            _InvoiceHeader.Saved_CustomersFrom_Bank_AccountNumber = _InvoiceHeader.BankInformation.AccountNumber.ToString
            '------------------------------------------------------------------------------------------------------------------
            If _ContractRowsCollection(i).ContractRowsType = eContractRowsType.eMileStone Then
                _InvoiceHeader.DateExecution = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractsRowsToInvoice).DateExecution
            Else
                _InvoiceHeader.DateExecution = _ContractRowsCollection(i).DateExecution
            End If
            If _InvoiceHeader.PaymentOption.PayMode = ePayMode.InContinuous Then
                _InvoiceHeader.DatePayment = _InvoiceHeader.DateExecution
            Else
                If _InvoiceHeader.PaymentOption.DatePaymentMode = eDatePaymentMode.eDateCreated Then
                    _InvoiceHeader.DatePayment = _InvoiceHeader.DateCreated.AddDays(_InvoiceHeader.PaymentOption.PayDay)
                Else
                    _InvoiceHeader.DatePayment = _InvoiceHeader.DateExecution.AddDays(_InvoiceHeader.PaymentOption.PayDay)
                End If
            End If
            _InvoiceHeader.CurrencyName = _ContractRowsCollection(i).CurrencyName
            '-- A következo sorok vizsgálata és hozzáadása a fenti fejléchez amíg az ezt engedélyezo feltételek teljesülnek
            Do
                _InvoiceRows_new = _ocur.CreateObject(Of InvoiceRows)()
                _InvoiceRows_new.Description = _ContractRowsCollection(i).Description
                _InvoiceRows_new.QTT = _ContractRowsCollection(i).QTT
                _InvoiceRows_new.QualityOption = _ContractRowsCollection(i).QualityOption
                _InvoiceRows_new.VAT = _ContractRowsCollection(i).VAT
                _InvoiceRows_new.Units = _ContractRowsCollection(i).Units
                _InvoiceRows_new.ShortName = _ContractRowsCollection(i).ShortName

                If Not _InvoiceHeader.CurrencyName.ShortName = "HUF" Then
                    _InvoiceRows_new.ListPriceHUF = Math.Round(_ContractRowsCollection(i).NettoPriceDEV * _ContractsRowsToInvoice.CurrencyRate, 2, MidpointRounding.AwayFromZero)
                Else
                    _InvoiceRows_new.ListPriceHUF = _ContractRowsCollection(i).NettoPriceHUF
                End If
                _InvoiceRows_new.ListPriceDEV = _ContractRowsCollection(i).NettoPriceDEV
                _InvoiceRows_new.ContractRows = _ContractRowsCollection(i)
                _InvoiceRows_new.InvoiceHeader = _InvoiceHeader
                _InvoiceRows_new.Save()
                _InvoiceRows_Array.Add(_InvoiceRows_new)
                If i = _ContractRowsCollection.Count - 1 Then
                    _IsNextWhile = False
                    Exit Do
                End If
                i += 1
            Loop While _ContractRowsCollection(i - 1).Contracts.CustomersFrom Is _ContractRowsCollection(i).Contracts.CustomersFrom And _
                    _ContractRowsCollection(i - 1).Contracts.Customers Is _ContractRowsCollection(i).Contracts.Customers And _
                    _ContractRowsCollection(i - 1).DateExecution.Date = _ContractRowsCollection(i).DateExecution.Date And _
                    _ContractRowsCollection(i - 1).CurrencyName Is _ContractRowsCollection(i).CurrencyName And _
                    _ContractRowsCollection(i - 1).PaymentOption Is _ContractRowsCollection(i).PaymentOption


            _InvoiceHeader.Save()
            _ocur.CommitChanges()
            _InvoiceHeader.RecalculateInvoice(_InvoiceHeader, _ocur)
            _ocur.CommitChanges()
        Loop

        For Each _InvoiceRows_new In _InvoiceRows_Array
            _ContractRows = _InvoiceRows_new.ContractRows
            _ContractRows.InvoiceRows = _InvoiceRows_new
            _ContractRows.Save()
        Next
        _ocur.CommitChanges()
        _noselect = False
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
        Exit Sub
CheckError:
        If Err.Number = 91 Then Resume Next
        If Err.Number = 13 Then Resume Next

    End Sub

    Private Sub aCloneSelectedContracts_Cancel(ByVal sender As Object, ByVal e As System.EventArgs) Handles aCloneSelectedContracts.Cancel
        _noselect = False
    End Sub
    Private Sub aCloneSelectedContracts_CustomizePopupWindowParams(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCloneSelectedContracts.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractsCloneView As ContractsCloneView = _ocur.CreateObject(Of ContractsCloneView)()
        Dim _ContractsCloneView_New As ContractsCloneView

        _noselect = True
        If _selection.Count > 1 Then
            _ContractsCloneView.IsMultipleSelection = True
        Else
            _ContractsCloneView.IsMultipleSelection = False
        End If
        _ContractsCloneView.Save()
        _ocur.CommitChanges()

        _ContractsCloneView_New = _onew.GetObject(_ContractsCloneView)

        e.View = Application.CreateDetailView(_onew, "ContractsCloneView_DetailView", False, _ContractsCloneView_New)

    End Sub
    Private Sub aCloneSelectedContracts_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCloneSelectedContracts.Execute
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False) ' A validáció ideiglenes letiltása az akció végrehajtásának idejére.
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractsCloneView As ContractsCloneView = e.PopupWindow.View.SelectedObjects(0)
        Dim _Contracts As Contracts
        Dim _ContractRows As ContractRows

        If _ContractsCloneView.ContractTemplate IsNot Nothing Then
            For I = 0 To _selection.Count - 1
                _Contracts = _ocur.GetObjectByKey(Of Contracts)((TryCast(_selection(I), Contracts)).Oid)
                Dim _Contracts_New As Contracts = _ocur.CreateObject(Of Contracts)()

                _Contracts_New.CustomersFrom = _ocur.GetObject(_ContractsCloneView.CustomersFrom)
                _Contracts_New.ContractTemplate = _ocur.GetObject(_ContractsCloneView.ContractTemplate)

                If _ContractsCloneView.Customers IsNot Nothing Then
                    _Contracts_New.Customers = _ocur.GetObject(_ContractsCloneView.Customers)
                Else
                    _Contracts_New.Customers = _ocur.GetObject(_Contracts.Customers)
                End If
                _Contracts_New.DateClosed = _ocur.GetObject(_Contracts.DateClosed)
                _Contracts_New.DateCreated = _ocur.GetObject(_Contracts.DateCreated)
                _Contracts_New.DateExecution = _ocur.GetObject(_Contracts.DateExecution)
                _Contracts_New.DateExpectClose = _ocur.GetObject(_Contracts.DateExpectClose)
                _Contracts_New.DatePreContractSigned = _ocur.GetObject(_Contracts.DatePreContractSigned)
                _Contracts_New.DateTerminating = _ocur.GetObject(_Contracts.DateTerminating)
                _Contracts_New.DateWaiting = _ocur.GetObject(_Contracts.DateWaiting)
                _Contracts_New.DocumentBody = _ocur.GetObject(_Contracts.DocumentBody)
                _Contracts_New.BankInformation = _ocur.GetObject(_Contracts.BankInformation)
                _Contracts_New.CurrencyName = _ocur.GetObject(_Contracts.CurrencyName)
                _Contracts_New.CurrencyName_Invoice = _ocur.GetObject(_Contracts.CurrencyName_Invoice)

                _Contracts_New.HRResponsible = _ocur.GetObject(_Contracts.HRResponsible)
                _Contracts_New.JobNumberObjects = _ocur.GetObject(_Contracts.JobNumberObjects)
                _Contracts_New.ShortName = _ocur.GetObject(_Contracts.ShortName)
                _Contracts_New.State = State.Preparation    '_ocur.GetObject(_Contracts.State)
                _Contracts_New.DocumentNumber = _Contracts_New.ContractTemplate.NumberGenerator.GetNewNumber(_Contracts.ContractTemplate.NumberGenerator)

                If _ContractsCloneView.AreContractRowsClone = True Then
                    Dim _ContractRowsCollection As XPCollection = _ocur.CreateCollection(GetType(ContractRows), CriteriaOperator.Parse("Contracts=?", _Contracts))
                    For Each _ContractRows In _ContractRowsCollection
                        'Csak a ki nem számlázott sorok átadása!
                        ' If _ContractRows.InvoiceRows Is Nothing And _ContractRows.InvoiceRowsAdvance Is Nothing Then
                        Dim _ContractRows_New As ContractRows = _ocur.CreateObject(Of ContractRows)()

                        _ContractRows_New.Contracts = _ocur.GetObject(_Contracts_New)
                        _ContractRows_New.ContractRowsType = _ocur.GetObject(_ContractRows.ContractRowsType)
                        _ContractRows_New.Controlling = _ocur.GetObject(_ContractRows.Controlling)
                        _ContractRows_New.CostHolder = _ocur.GetObject(_ContractRows.CostHolder)
                        _ContractRows_New.CostPlace = _ocur.GetObject(_ContractRows.CostPlace)
                        _ContractRows_New.CurrencyName = _ocur.GetObject(_ContractRows.CurrencyName)
                        _ContractRows_New.CurrencyRate = _ocur.GetObject(_ContractRows.CurrencyRate)
                        _ContractRows_New.DateExecution = _ocur.GetObject(_ContractRows.DateExecution)
                        _ContractRows_New.Description = _ocur.GetObject(_ContractRows.Description)
                        _ContractRows_New.MileStone = _ocur.GetObject(_ContractRows.MileStone)
                        _ContractRows_New.NettoPriceDEV = _ocur.GetObject(_ContractRows.NettoPriceDEV)
                        _ContractRows_New.NettoPriceHUF = _ocur.GetObject(_ContractRows.NettoPriceHUF)
                        _ContractRows_New.PaymentOption = _ocur.GetObject(_ContractRows.PaymentOption)
                        _ContractRows_New.QTT = _ocur.GetObject(_ContractRows.QTT)
                        _ContractRows_New.QualityOption = _ocur.GetObject(_ContractRows.QualityOption)
                        _ContractRows_New.ShipmentOption = _ocur.GetObject(_ContractRows.ShipmentOption)
                        _ContractRows_New.ShortName = _ocur.GetObject(_ContractRows.ShortName)
                        _ContractRows_New.UniqueObjects = _ocur.GetObject(_ContractRows.UniqueObjects)
                        _ContractRows_New.Units = _ocur.GetObject(_ContractRows.Units)
                        _ContractRows_New.VAT = _ocur.GetObject(_ContractRows.VAT)
                        _ContractRows_New.JobNumberObjects = _ocur.GetObject(_ContractRows.JobNumberObjects)
                        _ContractRows_New.InvoicePeriod = _ocur.GetObject(_ContractRows.InvoicePeriod)
                        'End If
                    Next
                End If
            Next I
            _ocur.CommitChanges()
        Else
            _ocur.Rollback()
            Throw New Exception("*Nincs megadva szerződés sablon!")
        End If

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub
    Private Sub aGenerateInvoicefromContractsRowsCustomRate_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aGenerateInvoicefromContractsRowsCustomRate.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractRowsCollection As New XPCollection(Of ContractRows)(_ocur.Session, CriteriaOperator.Parse("1=2"))
        Dim _ContractsRowsToInvoice As ContractsRowsToInvoice = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractsRowsToInvoice)
        Dim _ContractRows As ContractRows
        Dim _ContractRows_Selection As ContractRows
        Dim _InvoiceRows_new As InvoiceRows
        Dim _InvoiceRows_Array As New ArrayList()

        On Error GoTo CheckError

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        ' -- létrehozunk egy kollekciót a kiválasztott elemek tömbjébol
        For Each _ContractRows_Selection In _selection
            _ContractRows = _ocur.GetObject(_ContractRows_Selection)
            _ContractRowsCollection.Add(_ContractRows)
        Next
        _InvoiceRows_Array.Clear()

        _noselect = False
        ' -- rendezési feltételeket adunk a létrehozott kollekciónak
        Dim sortCollection As SortingCollection = New SortingCollection()
        sortCollection.Add(New SortProperty("Contracts.CustomersFrom.Name", DB.SortingDirection.Ascending))
        sortCollection.Add(New SortProperty("Contracts.Customers.Name", DB.SortingDirection.Ascending))
        sortCollection.Add(New SortProperty("DateExecution", DB.SortingDirection.Ascending))
        sortCollection.Add(New SortProperty("CurrencyName.ShortName", DB.SortingDirection.Ascending))
        sortCollection.Add(New SortProperty("PaymentOption.ShortName", DB.SortingDirection.Ascending))
        _ContractRowsCollection.Sorting = sortCollection
        Dim i As Long = 0
        Dim _IsHeaderCreated As Boolean = False
        Dim _IsNextWhile As Boolean = True
        Dim _NG As NumberGenerator
        Do While _IsNextWhile
            Dim _InvoiceHeader As InvoiceHeader
            '            Dim _InvoiceRows As InvoiceRows = _ocur.CreateObject(Of InvoiceRows)()
            _InvoiceHeader = _ocur.CreateObject(GetType(InvoiceHeader))
            _InvoiceHeader.PaymentOption = _ContractRowsCollection(i).PaymentOption
            _InvoiceHeader.ShipmentOption = _ContractRowsCollection(i).ShipmentOption

            If _ContractRowsCollection(i).ContractRowsType = eContractRowsType.eAdvance Then
                _InvoiceHeader.InvoiceType = _ContractRowsCollection(i).Contracts.ContractTemplate.InvoiceTypeAdvanced
            Else
                _InvoiceHeader.InvoiceType = _ContractRowsCollection(i).Contracts.ContractTemplate.InvoiceTypeCustomRate
            End If

            If _InvoiceHeader.InvoiceType.NumberGeneratorProforma IsNot Nothing Then
                _NG = _InvoiceHeader.InvoiceType.NumberGeneratorProforma
                _InvoiceHeader.DocumentNumberProforma = _NG.GetNewNumber(_NG)
            End If
            _InvoiceHeader.IsProforma = True
            _InvoiceHeader.IsEditable = False
            _InvoiceHeader.CustomersFrom = _ContractRowsCollection(i).Contracts.CustomersFrom
            _InvoiceHeader.Customers = _ContractRowsCollection(i).Contracts.Customers
            _InvoiceHeader.Description = _ocur.GetObject(_ContractsRowsToInvoice.Description)
            _InvoiceHeader.CurrencyName = _ContractRowsCollection(i).CurrencyName
            _InvoiceHeader.CurrencyRate = _ocur.GetObject(_ContractsRowsToInvoice.CurrencyRate)
            _InvoiceHeader.BankInformation = _ocur.GetObject(_ContractsRowsToInvoice.BankInformation)

            '-- Próbaszámla miatt el kell menteni a tényleges adatokat --------------------------------------------------------
            _InvoiceHeader.Saved_Customers_Name = _ContractRowsCollection(i).Contracts.Customers.Name.ToString
            _InvoiceHeader.Saved_Customers_Address_Country = _ContractRowsCollection(i).Contracts.Customers.Address1.Country.ToString
            _InvoiceHeader.Saved_Customers_Address_ZipPostal = _ContractRowsCollection(i).Contracts.Customers.Address1.ZipPostal.ToString
            _InvoiceHeader.Saved_Customers_Address_Street = _ContractRowsCollection(i).Contracts.Customers.Address1.Street.ToString
            _InvoiceHeader.Saved_Customers_Address_City = _ContractRowsCollection(i).Contracts.Customers.Address1.City.ToString
            _InvoiceHeader.Saved_Customers_Address_StateProvince = _ContractRowsCollection(i).Contracts.Customers.Address1.StateProvince.ToString
            _InvoiceHeader.Saved_Customers_VATNumber = _ContractRowsCollection(i).Contracts.Customers.VATNumber.ToString
            _InvoiceHeader.Saved_Customers_VATNumberEU = _ContractRowsCollection(i).Contracts.Customers.VATNumberEU.ToString

            _InvoiceHeader.Saved_CustomersFrom_Name = _ContractRowsCollection(i).Contracts.CustomersFrom.Name.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_ZipPostal = _ContractRowsCollection(i).Contracts.CustomersFrom.Address1.ZipPostal.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_Street = _ContractRowsCollection(i).Contracts.CustomersFrom.Address1.Street.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_City = _ContractRowsCollection(i).Contracts.CustomersFrom.Address1.City.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_StateProvince = _ContractRowsCollection(i).Contracts.CustomersFrom.Address1.StateProvince.ToString
            _InvoiceHeader.Saved_CustomersFrom_VATNumber = _ContractRowsCollection(i).Contracts.CustomersFrom.VATNumber.ToString
            _InvoiceHeader.Saved_CustomersFrom_VATNumberEU = _ContractRowsCollection(i).Contracts.CustomersFrom.VATNumberEU.ToString

            _InvoiceHeader.Saved_CustomersFrom_Bank_ShortName = _InvoiceHeader.BankInformation.FullBankAccount.ToString
            _InvoiceHeader.Saved_CustomersFrom_Bank_IBAN = _InvoiceHeader.BankInformation.IBAN.ToString
            _InvoiceHeader.Saved_CustomersFrom_Bank_SWIFT = _InvoiceHeader.BankInformation.SWIFT.ToString
            _InvoiceHeader.Saved_CustomersFrom_Bank_AccountNumber = _InvoiceHeader.BankInformation.AccountNumber.ToString
            '------------------------------------------------------------------------------------------------------------------
            If _ContractRowsCollection(i).ContractRowsType = eContractRowsType.eMileStone Then
                _InvoiceHeader.DateExecution = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractsRowsToInvoice).DateExecution
            Else
                _InvoiceHeader.DateExecution = _ContractRowsCollection(i).DateExecution
            End If
            If _InvoiceHeader.PaymentOption.PayMode = ePayMode.InContinuous Then
                _InvoiceHeader.DatePayment = _InvoiceHeader.DateExecution
            Else
                If _InvoiceHeader.PaymentOption.DatePaymentMode = eDatePaymentMode.eDateCreated Then
                    _InvoiceHeader.DatePayment = _InvoiceHeader.DateCreated.AddDays(_InvoiceHeader.PaymentOption.PayDay)
                Else
                    _InvoiceHeader.DatePayment = _InvoiceHeader.DateExecution.AddDays(_InvoiceHeader.PaymentOption.PayDay)
                End If

            End If
            _InvoiceHeader.CurrencyName = _ContractRowsCollection(i).CurrencyName
            '-- A következo sorok vizsgálata és hozzáadása a fenti fejléchez amíg az ezt engedélyező feltételek teljesülnek
            Do
                _InvoiceRows_new = _ocur.CreateObject(Of InvoiceRows)()
                _InvoiceRows_new.Description = _ContractRowsCollection(i).Description
                _InvoiceRows_new.QTT = _ContractRowsCollection(i).QTT
                _InvoiceRows_new.QualityOption = _ContractRowsCollection(i).QualityOption
                _InvoiceRows_new.VAT = _ContractRowsCollection(i).VAT
                _InvoiceRows_new.Units = _ContractRowsCollection(i).Units
                _InvoiceRows_new.ShortName = _ContractRowsCollection(i).ShortName

                If Not _InvoiceHeader.CurrencyName.ShortName = "HUF" Then
                    _InvoiceRows_new.ListPriceHUF = Math.Round(_ContractRowsCollection(i).NettoPriceDEV * _ContractsRowsToInvoice.CurrencyRate, 2, MidpointRounding.AwayFromZero)
                Else
                    _InvoiceRows_new.ListPriceHUF = _ContractRowsCollection(i).NettoPriceHUF
                End If
                _InvoiceRows_new.ListPriceDEV = _ContractRowsCollection(i).NettoPriceDEV
                _InvoiceRows_new.ContractRows = _ContractRowsCollection(i)
                _InvoiceRows_new.InvoiceHeader = _InvoiceHeader
                _InvoiceRows_new.CostHolder = _ocur.GetObject(_ContractRowsCollection(i).CostHolder)
                _InvoiceRows_new.Controlling = _ocur.GetObject(_ContractRowsCollection(i).Controlling)
                _InvoiceRows_new.CostPlace = _ocur.GetObject(_ContractRowsCollection(i).CostPlace)
                _InvoiceRows_new.JobNumberObjects = _ocur.GetObject(_ContractRowsCollection(i).JobNumberObjects)
                _InvoiceRows_new.UniqueObjects = _ocur.GetObject(_ContractRowsCollection(i).UniqueObjects)

                _InvoiceRows_Array.Add(_InvoiceRows_new)
                If i = _ContractRowsCollection.Count - 1 Then
                    _IsNextWhile = False
                    Exit Do
                End If
                i += 1
            Loop While _ContractRowsCollection(i - 1).Contracts.CustomersFrom Is _ContractRowsCollection(i).Contracts.CustomersFrom And _
                    _ContractRowsCollection(i - 1).Contracts.Customers Is _ContractRowsCollection(i).Contracts.Customers And _
                    _ContractRowsCollection(i - 1).DateExecution.Date = _ContractRowsCollection(i).DateExecution.Date And _
                    _ContractRowsCollection(i - 1).CurrencyName Is _ContractRowsCollection(i).CurrencyName And _
                    _ContractRowsCollection(i - 1).PaymentOption Is _ContractRowsCollection(i).PaymentOption


            _ocur.CommitChanges()
            _InvoiceHeader.RecalculateInvoice(_InvoiceHeader, _ocur)
            _ocur.CommitChanges()
        Loop

        For Each _InvoiceRows_new In _InvoiceRows_Array
            _ContractRows = _InvoiceRows_new.ContractRows
            _ContractRows.InvoiceRows = _InvoiceRows_new
        Next
        _ocur.CommitChanges()
        _noselect = False
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
        Exit Sub
CheckError:
        If Err.Number = 91 Then Resume Next
        If Err.Number = 13 Then Resume Next

    End Sub
    Private Sub aGenerateInvoicefromContractsRowsCustomRate_CustomizePopupWindowParams(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aGenerateInvoicefromContractsRowsCustomRate.CustomizePopupWindowParams
        _noselect = True
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

        Dim _ContractsRowsToInvoice_New As ContractsRowsToInvoice = _onew.CreateObject(Of ContractsRowsToInvoice)()

        _ContractsRowsToInvoice_New.Description = ""
        _ContractsRowsToInvoice_New.CurrencyRate = 1
        _ContractsRowsToInvoice_New.DateExecution = GetSQLTime(_onew.Session)
        _ContractsRowsToInvoice_New.BankInformation = Nothing
        _ContractsRowsToInvoice_New.ContractRows = _onew.GetObject(_selection(0))

        e.View = Application.CreateDetailView(_onew, "ContractsRowsToInvoice_DetailView", False, _ContractsRowsToInvoice_New)
    End Sub
    Private Sub aGenerateInvoicefromContractsRowsCustomRate_Cancel(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles aGenerateInvoicefromContractsRowsCustomRate.Cancel
        _noselect = False
    End Sub
    Private Sub aSendBackToEdit_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSendBackToEdit.Execute
        Dim _Contracts As Contracts = TryCast(View.CurrentObject, Contracts)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        _Contracts.State = State.Preparation
        _ocur.CommitChanges()
        View.Close()
    End Sub
    Private Sub aMarkAsReady_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aMarkAsReady.Execute
        Dim _Contracts As Contracts = TryCast(View.CurrentObject, Contracts)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        _Contracts.State = State.Ready
        If _Contracts.DocumentNumber = "" Then
            _Contracts.DocumentNumber = _Contracts.ContractTemplate.NumberGenerator.GetNewNumber(_Contracts.ContractTemplate.NumberGenerator)
        End If
        _ocur.CommitChanges()
        View.Close()
    End Sub
    Private Sub aMarkAsSigned_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aMarkAsSigned.Execute
        Dim _Contracts As Contracts = TryCast(View.CurrentObject, Contracts)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        _Contracts.State = State.Signed
        If _Contracts.DocumentNumber = "" Then
            _Contracts.DocumentNumber = _Contracts.ContractTemplate.NumberGenerator.GetNewNumber(_Contracts.ContractTemplate.NumberGenerator)
        End If
        _ocur.CommitChanges()
        View.Close()
    End Sub
    Private Sub aMarkAsClosed_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aMarkAsClosed.Execute
        Dim _Contracts As Contracts = TryCast(View.SelectedObjects(0), Contracts)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        _Contracts.State = State.Closed
        _Contracts.DateClosed = Now
        _ocur.CommitChanges()
        View.Close()
    End Sub
    Private Sub aMarkAsWaiting_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aMarkAsWaiting.Execute
        Dim _Contracts As Contracts = TryCast(View.SelectedObjects(0), Contracts)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        _Contracts.State = State.Waiting
        _Contracts.DateWaiting = Now
        _ocur.CommitChanges()
        View.Close()
    End Sub


    Private Sub aCreateFinalInvoiceFromContract_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateFinalInvoiceFromContract.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractRows As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)
        Dim _ContractRowsToFinalInvoice As ContractRowsToFinalInvoice = e.PopupWindow.View.SelectedObjects(0)
        Dim _ListView As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("InvoiceRows")
        Dim _InvoiceHeader As InvoiceHeader
        Dim _InvoiceRows_New As InvoiceRows
        Dim _NG As NumberGenerator
        _noselect = True

        On Error GoTo CheckError
        _InvoiceHeader = _ocur.CreateObject(GetType(InvoiceHeader))
        _InvoiceHeader.PaymentOption = _ContractRows.PaymentOption
        _InvoiceHeader.ShipmentOption = _ContractRows.ShipmentOption

        'Ha a sor típusa előleg akkor az előleg számla típusát kell vennünk a szerz.sablonnak egyébként a rendeset
        If _ContractRows.ContractRowsType = eContractRowsType.eAdvance Then
            _InvoiceHeader.InvoiceType = _ContractRows.Contracts.ContractTemplate.InvoiceTypeAdvanced
        Else
            _InvoiceHeader.InvoiceType = _ContractRows.Contracts.ContractTemplate.InvoiceType
        End If
        If _InvoiceHeader.InvoiceType.NumberGeneratorProforma IsNot Nothing Then
            _NG = _InvoiceHeader.InvoiceType.NumberGeneratorProforma
            _InvoiceHeader.DocumentNumberProforma = _NG.GetNewNumber(_NG)
        End If

        _InvoiceHeader.IsProforma = True
        _InvoiceHeader.IsEditable = False
        _InvoiceHeader.CustomersFrom = _ContractRows.Contracts.CustomersFrom
        _InvoiceHeader.Customers = _ContractRows.Contracts.Customers
        _InvoiceHeader.Description = _ocur.GetObject(_ContractRowsToFinalInvoice.Description)
        _InvoiceHeader.CurrencyName = _ContractRows.CurrencyName
        _InvoiceHeader.CurrencyRate = _ocur.GetObject(_ContractRowsToFinalInvoice.CurrencyRate)
        _InvoiceHeader.BankInformation = _ocur.GetObject(_ContractRowsToFinalInvoice.BankInformation)

        '-- Próbaszámla miatt el kell menteni a tényleges adatokat --------------------------------------------------------
        _InvoiceHeader.Saved_Customers_Name = _ContractRows.Contracts.Customers.Name.ToString
        _InvoiceHeader.Saved_Customers_Address_Country = _ContractRows.Contracts.Customers.Address1.Country.ToString
        _InvoiceHeader.Saved_Customers_Address_ZipPostal = _ContractRows.Contracts.Customers.Address1.ZipPostal.ToString
        _InvoiceHeader.Saved_Customers_Address_Street = _ContractRows.Contracts.Customers.Address1.Street.ToString
        _InvoiceHeader.Saved_Customers_Address_City = _ContractRows.Contracts.Customers.Address1.City.ToString
        _InvoiceHeader.Saved_Customers_Address_StateProvince = _ContractRows.Contracts.Customers.Address1.StateProvince.ToString
        _InvoiceHeader.Saved_Customers_VATNumber = _ContractRows.Contracts.Customers.VATNumber.ToString
        _InvoiceHeader.Saved_Customers_VATNumberEU = _ContractRows.Contracts.Customers.VATNumberEU.ToString

        _InvoiceHeader.Saved_CustomersFrom_Name = _ContractRows.Contracts.CustomersFrom.Name.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_ZipPostal = _ContractRows.Contracts.CustomersFrom.Address1.ZipPostal.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_Street = _ContractRows.Contracts.CustomersFrom.Address1.Street.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_City = _ContractRows.Contracts.CustomersFrom.Address1.City.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_StateProvince = _ContractRows.Contracts.CustomersFrom.Address1.StateProvince.ToString
        _InvoiceHeader.Saved_CustomersFrom_VATNumber = _ContractRows.Contracts.CustomersFrom.VATNumber.ToString
        _InvoiceHeader.Saved_CustomersFrom_VATNumberEU = _ContractRows.Contracts.CustomersFrom.VATNumberEU.ToString

        _InvoiceHeader.Saved_CustomersFrom_Bank_ShortName = _InvoiceHeader.BankInformation.FullBankAccount.ToString
        _InvoiceHeader.Saved_CustomersFrom_Bank_IBAN = _InvoiceHeader.BankInformation.IBAN.ToString
        _InvoiceHeader.Saved_CustomersFrom_Bank_SWIFT = _InvoiceHeader.BankInformation.SWIFT.ToString
        _InvoiceHeader.Saved_CustomersFrom_Bank_AccountNumber = _InvoiceHeader.BankInformation.AccountNumber.ToString
        '------------------------------------------------------------------------------------------------------------------
        If _ContractRows.ContractRowsType = eContractRowsType.eMileStone Then
            _InvoiceHeader.DateExecution = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractsRowsToInvoice).DateExecution
        Else
            _InvoiceHeader.DateExecution = _ContractRows.DateExecution
        End If
        If _InvoiceHeader.PaymentOption.PayMode = ePayMode.InContinuous Then
            _InvoiceHeader.DatePayment = _InvoiceHeader.DateExecution
        Else
            If _InvoiceHeader.PaymentOption.DatePaymentMode = eDatePaymentMode.eDateCreated Then
                _InvoiceHeader.DatePayment = _InvoiceHeader.DateCreated.AddDays(_InvoiceHeader.PaymentOption.PayDay)
            Else
                _InvoiceHeader.DatePayment = _InvoiceHeader.DateExecution.AddDays(_InvoiceHeader.PaymentOption.PayDay)
            End If
        End If
        _InvoiceHeader.CurrencyName = _ContractRows.CurrencyName

        _InvoiceRows_New = _ocur.CreateObject(Of InvoiceRows)()
        _InvoiceRows_New.Description = _ContractRows.Description
        _InvoiceRows_New.QTT = _ContractRows.QTT
        _InvoiceRows_New.QualityOption = _ContractRows.QualityOption
        _InvoiceRows_New.VAT = _ContractRows.VAT
        _InvoiceRows_New.Units = _ContractRows.Units
        _InvoiceRows_New.ShortName = _ContractRows.ShortName

        If Not _InvoiceHeader.CurrencyName.ShortName = "HUF" Then
            _InvoiceRows_New.ListPriceHUF = Math.Round(_ContractRows.NettoPriceDEV * _ContractRowsToFinalInvoice.CurrencyRate, 2, MidpointRounding.AwayFromZero)
        Else
            _InvoiceRows_New.ListPriceHUF = _ContractRows.NettoPriceHUF
        End If
        _InvoiceRows_New.ListPriceDEV = _ContractRows.NettoPriceDEV
        _InvoiceRows_New.ContractRows = _ContractRows
        _InvoiceRows_New.InvoiceHeader = _InvoiceHeader

        '-- Jelölő adatok
        _InvoiceRows_New.CostHolder = _ContractRows.CostHolder
        _InvoiceRows_New.CostPlace = _ContractRows.CostPlace
        _InvoiceRows_New.JobNumberObjects = _ContractRows.JobNumberObjects
        _InvoiceRows_New.UniqueObjects = _ContractRows.UniqueObjects
        _InvoiceRows_New.Controlling = _ContractRows.Controlling

        _ContractRows.InvoiceRows = _InvoiceRows_New

        For Each _InvoiceRows In _ListView.ListView.SelectedObjects
            _InvoiceRows_New = _ocur.CreateObject(Of InvoiceRows)()

            _InvoiceRows_New.InvoiceHeader = _InvoiceHeader
            _InvoiceRows_New.QTT = _InvoiceRows.QTT * -1
            _InvoiceRows_New.InvoiceRowsAdvance = _ocur.GetObject(_InvoiceRows)
            _InvoiceRows_New.ContractRows = _ocur.GetObject(_InvoiceRows.ContractRows)
            _InvoiceRows_New.CustomsTariffs = _ocur.GetObject(_InvoiceRows.CustomsTariffs)
            _InvoiceRows_New.Description = _InvoiceRows.InvoiceHeader.DocumentNumber & " " & Format(_ocur.GetObject(_InvoiceRows.Description), "")
            _InvoiceRows_New.DiscountPriceDEV = _ocur.GetObject(_InvoiceRows.DiscountPriceDEV)
            _InvoiceRows_New.DiscountPriceHUF = _ocur.GetObject(_InvoiceRows.DiscountPriceHUF)
            _InvoiceRows_New.InvoiceRowtype = _ocur.GetObject(_InvoiceRows.InvoiceRowtype)
            _InvoiceRows_New.ListPriceDEV = _ocur.GetObject(_InvoiceRows.ListPriceDEV)
            _InvoiceRows_New.ListPriceHUF = _ocur.GetObject(_InvoiceRows.ListPriceHUF)
            _InvoiceRows_New.QualityOption = _ocur.GetObject(_InvoiceRows.QualityOption)
            _InvoiceRows_New.ShortName = _ocur.GetObject(_InvoiceRows.ShortName)
            _InvoiceRows_New.Units = _ocur.GetObject(_InvoiceRows.Units)
            _InvoiceRows_New.VAT = _ocur.GetObject(_InvoiceRows.VAT)

            '-- Jelölő adatok
            _InvoiceRows_New.Controlling = _ocur.GetObject(_InvoiceRows.Controlling)
            _InvoiceRows_New.CostHolder = _ocur.GetObject(_InvoiceRows.CostHolder)
            _InvoiceRows_New.CostPlace = _ocur.GetObject(_InvoiceRows.CostPlace)
            _InvoiceRows_New.JobNumberObjects = _ocur.GetObject(_InvoiceRows.JobNumberObjects)
            _InvoiceRows_New.UniqueObjects = _ocur.GetObject(_InvoiceRows.UniqueObjects)
        Next
        _ocur.CommitChanges()
        _InvoiceRows_New.InvoiceHeader.RecalculateInvoice(_InvoiceRows_New.InvoiceHeader, _ocur)
        _ocur.CommitChanges()
        View.Close()
        Exit Sub
CheckError:
        If Err.Number = 91 Then Resume Next
        If Err.Number = 13 Then Resume Next
        MsgBox(Err.Description)
    End Sub
    Private Sub aCreateFinalInvoiceFromContract_CustomizePopupWindowParams(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateFinalInvoiceFromContract.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractRowsToFinalInvoice_New As ContractRowsToFinalInvoice = _onew.CreateObject(Of ContractRowsToFinalInvoice)()

        _ContractRowsToFinalInvoice_New.ContractRows = _onew.GetObject(TryCast(View.SelectedObjects(0), ContractRows))
        _ContractRowsToFinalInvoice_New.DateExecution = GetSQLTime(_onew.Session)

        e.View = Application.CreateDetailView(_onew, "ContractRowsToFinalInvoice_DetailView", False, _ContractRowsToFinalInvoice_New)
    End Sub
    Private Sub aDivideContractRows_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aDivideContractRows.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractRows As ContractRows = View.SelectedObjects(0)

        Dim _ContractRowsDividePopup As ContractRowsDividePopup = _onew.CreateObject(Of ContractRowsDividePopup)()
        _ContractRowsDividePopup.ContractRows = _onew.GetObject(_ContractRows)

        _onew.CommitChanges()

        e.View = Application.CreateDetailView(_onew, _ContractRowsDividePopup)

    End Sub
    Private Sub aDivideContractRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aDivideContractRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _ContractRowsDividePopup As ContractRowsDividePopup = e.PopupWindow.View.SelectedObjects(0)
        Dim _ContractRows As ContractRows = _ocur.GetObjectByKey(Of ContractRows)(_ocur.GetObject(_ContractRowsDividePopup.ContractRows.Oid))
        Dim _ContractRows_New As ContractRows

        '-- új szerződési sor létrehozása és adatok átadása

        _ContractRows_New = _ocur.CreateObject(Of ContractRows)()
        _ContractRows_New.Contracts = _ContractRows.Contracts

        'Értékek számítása

        '-- ha százalékos arány lett megadva:
        If _ContractRowsDividePopup.Percent > 0 Then
            _ContractRows_New.NettoPriceHUF = Math.Round((_ContractRows.NettoPriceHUF * (_ContractRowsDividePopup.Percent / 100)), 0, MidpointRounding.AwayFromZero)
            If Not _ContractRows.CurrencyName.ShortName = "HUF" Then
                _ContractRows_New.NettoPriceDEV = Math.Round(_ContractRows.NettoPriceDEV * (_ContractRowsDividePopup.Percent / 100), 0, MidpointRounding.AwayFromZero)
                _ContractRows_New.NettoPriceHUF = Math.Round((_ContractRows.NettoPriceDEV * _ContractRows.CurrencyRate), 0, MidpointRounding.AwayFromZero)
            End If
        End If

        '-- ha konkrét összeg lett megadva:

        'forint összeg
        If _ContractRowsDividePopup.AmountHUF > 0 Then
            _ContractRows_New.NettoPriceHUF = _ContractRowsDividePopup.AmountHUF
        End If

        'deviza összeg
        If _ContractRowsDividePopup.AmountDEV > 0 Then
            If Not _ContractRows.CurrencyName.ShortName = "HUF" Then
                _ContractRows_New.NettoPriceDEV = _ContractRowsDividePopup.AmountDEV
                _ContractRows_New.NettoPriceHUF = _ContractRows_New.NettoPriceDEV * _ContractRows.CurrencyRate
            End If
        End If
        '--

        _ContractRows_New.ContractRowsType = _ContractRows.ContractRowsType
        _ContractRows_New.Controlling = _ContractRows.Controlling
        _ContractRows_New.CostHolder = _ContractRows.CostHolder
        _ContractRows_New.CostPlace = _ContractRows.CostPlace
        _ContractRows_New.CurrencyName = _ContractRows.CurrencyName
        _ContractRows_New.CurrencyRate = _ContractRows.CurrencyRate
        _ContractRows_New.DateExecution = _ContractRows.DateExecution
        _ContractRows_New.Description = _ContractRows.Description
        _ContractRows_New.JobNumberObjects = _ContractRows.JobNumberObjects
        _ContractRows_New.MileStone = _ContractRows.MileStone
        _ContractRows_New.PaymentOption = _ContractRows.PaymentOption
        _ContractRows_New.QTT = _ContractRows.QTT
        _ContractRows_New.QualityOption = _ContractRows.QualityOption
        _ContractRows_New.RowGroup1 = _ContractRows.RowGroup1
        _ContractRows_New.RowGroup2 = _ContractRows.RowGroup2
        _ContractRows_New.ShipmentOption = _ContractRows.ShipmentOption
        _ContractRows_New.ShortName = _ContractRows.ShortName
        _ContractRows_New.UniqueObjects = _ContractRows.UniqueObjects
        _ContractRows_New.Units = _ContractRows.Units
        _ContractRows_New.VAT = _ContractRows.VAT

        '-- eredeti sor módosítása (összegek csökkentése az új sor értékével)
        If _ContractRows.CurrencyName.ShortName = "HUF" Then
            _ContractRows.NettoPriceHUF -= _ContractRows_New.NettoPriceHUF
        Else
            _ContractRows.NettoPriceDEV -= _ContractRows_New.NettoPriceDEV
            ' -- jelenleg nincs kalkuláltatva a szerződés sorban a deviza forintosított összege így nem számoltatom vele mert jelenleg minden szerződési sor árfolyama 1 (2011.08.02.)
            '_ContractRows.NettoPriceHUF = _ContractRows_New.NettoPriceDEV * _ContractRows.CurrencyRate
        End If

        _ocur.CommitChanges()
    End Sub
    Private Sub aMergeContractRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aMergeContractRows.Execute
        Dim _ContractRows As ContractRows
        Dim _ContractRows_New As ContractRows
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _FirstSelectedContractRows As ContractRows
        Dim _SumAmountHUF As Double = 0
        Dim _SumAmountDEV As Double = 0
        Dim _RowsCanBeMerged As Boolean = True

        If View.SelectedObjects.Count > 0 Then
            _FirstSelectedContractRows = TryCast(View.SelectedObjects(0), ContractRows)

            '-- Ellenőrzés, hogy összevonhatóak-e a sorok (összes hasonlítása az elsővel)
            For Each _ContractRows In View.SelectedObjects
                If Not View.SelectedObjects(0) Is _ContractRows Then 'önmagával ne vizsgáljon (az action multiple selected required selectin dependency -re van állítva tehát legalább két elem lesz a tömbben)
                    If Not (_ContractRows.Contracts Is _FirstSelectedContractRows.Contracts And _
                        _ContractRows.Controlling Is _FirstSelectedContractRows.Controlling And _
                        _ContractRows.UniqueObjects Is _FirstSelectedContractRows.UniqueObjects And _
                        _ContractRows.CostPlace Is _FirstSelectedContractRows.CostPlace And _
                        _ContractRows.CostHolder Is _FirstSelectedContractRows.CostHolder And _
                        _ContractRows.JobNumberObjects Is _FirstSelectedContractRows.JobNumberObjects And _
                        _ContractRows.PaymentOption Is _FirstSelectedContractRows.PaymentOption And _
                        _ContractRows.VAT Is _FirstSelectedContractRows.VAT And _
                        _ContractRows.DateExecution = _FirstSelectedContractRows.DateExecution And _
                        _ContractRows.CurrencyName Is _FirstSelectedContractRows.CurrencyName) Then

                        _RowsCanBeMerged = False

                    End If
                End If
            Next
            '-- Amennyiben összevonhatóak átadjuk az első adatait és a kijelölt sorok összegét egy új sornak majd a kiválaszottakat töröljük

            ' További megfejtendő talányok:
            '- mi lesz a keletkezett sor leírása? (jelenleg az első kiválasztott elemének tulajdonságait kapja meg)
            '- a fenti kritériák elégségesek-e?

            If _RowsCanBeMerged Then
                For Each _ContractRows In View.SelectedObjects
                    _SumAmountDEV += _ContractRows.NettoPriceDEV
                    _SumAmountHUF += _ContractRows.NettoPriceHUF
                Next
                _ContractRows_New = _ocur.CreateObject(Of ContractRows)()
                _ContractRows_New.Contracts = _FirstSelectedContractRows.Contracts
                _ContractRows_New.NettoPriceDEV = _SumAmountDEV
                _ContractRows_New.NettoPriceHUF = _SumAmountHUF
                _ContractRows_New.ContractRowsType = _FirstSelectedContractRows.ContractRowsType
                _ContractRows_New.Controlling = _FirstSelectedContractRows.Controlling
                _ContractRows_New.CostHolder = _FirstSelectedContractRows.CostHolder
                _ContractRows_New.CostPlace = _FirstSelectedContractRows.CostPlace
                _ContractRows_New.CurrencyName = _FirstSelectedContractRows.CurrencyName
                _ContractRows_New.CurrencyRate = _FirstSelectedContractRows.CurrencyRate
                _ContractRows_New.DateExecution = _FirstSelectedContractRows.DateExecution
                _ContractRows_New.Description = _FirstSelectedContractRows.Description
                _ContractRows_New.JobNumberObjects = _FirstSelectedContractRows.JobNumberObjects
                _ContractRows_New.MileStone = _FirstSelectedContractRows.MileStone
                _ContractRows_New.PaymentOption = _FirstSelectedContractRows.PaymentOption
                _ContractRows_New.QTT = _FirstSelectedContractRows.QTT
                _ContractRows_New.QualityOption = _FirstSelectedContractRows.QualityOption
                _ContractRows_New.RowGroup1 = _FirstSelectedContractRows.RowGroup1
                _ContractRows_New.RowGroup2 = _FirstSelectedContractRows.RowGroup2
                _ContractRows_New.ShipmentOption = _FirstSelectedContractRows.ShipmentOption
                _ContractRows_New.ShortName = _FirstSelectedContractRows.ShortName
                _ContractRows_New.UniqueObjects = _FirstSelectedContractRows.UniqueObjects
                _ContractRows_New.Units = _FirstSelectedContractRows.Units
                _ContractRows_New.VAT = _FirstSelectedContractRows.VAT

                _ocur.Delete(View.SelectedObjects)
            Else
                _ocur.Rollback()
                Throw New Exception("*Nem lehet összevonni a kiválasztott sorokat!")
            End If
        End If
        _ocur.CommitChanges()
    End Sub
    Private Sub aOpenRegistryInvoiceFromContractRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenRegistryInvoiceFromContractRows.Execute
        On Error Resume Next

        Dim _Contractrows As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)

        Dim _CustomersFrom As CustomersFrom = _Contractrows.RegistryRowsFinancialControlling.RegistryHeader.CustomersFrom
        Dim _Customers As Customers = _Contractrows.RegistryRowsFinancialControlling.RegistryHeader.Customers
        Dim _DocumentNumber As String = _Contractrows.RegistryRowsFinancialControlling.RegistryHeader.DocumentNumber

        ViewAttachments.ViewAttachments(View.ObjectSpace, Frame, _CustomersFrom, _Customers, _DocumentNumber, "")
    End Sub
    Private Sub aOpenInvoiceFromContractRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenInvoiceFromContractRows.Execute
        On Error Resume Next

        Dim _Contractrows As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)

        Dim _CustomersFrom As CustomersFrom = _Contractrows.InvoiceRows.InvoiceHeader.CustomersFrom
        Dim _Customers As Customers = _Contractrows.InvoiceRows.InvoiceHeader.Customers
        Dim _DocumentNumber As String = _Contractrows.InvoiceRows.InvoiceHeader.DocumentNumber
        Dim _DocumentNumberProforma As String = _Contractrows.InvoiceRows.InvoiceHeader.DocumentNumberProforma

        ViewAttachments.ViewAttachments(View.ObjectSpace, Frame, _CustomersFrom, _Customers, _DocumentNumber, _DocumentNumberProforma)
    End Sub
    Private Sub aOpenCOCFromContractRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenCOCFromContractRows.Execute
        On Error Resume Next

        Dim _Contractrows As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CertificateofContractHeader As CertificateofContractHeader

        Dim _ReportData As ReportData
        _CertificateofContractHeader = _Contractrows.CertificateofContractRows.CertificateofContractHeader

        _ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _CertificateofContractHeader.Contracts.ContractTemplate.COC_ReportData.Oid), True)
        If _ReportData Is Nothing Or _CertificateofContractHeader Is Nothing Then
        Else
            Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("CertificateofContractHeader.DocumentNumber =?", _
                                                                                                                    _CertificateofContractHeader.DocumentNumber), True)
        End If


    End Sub

    Private Sub aNewContractRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aNewContractRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Contracts As Contracts = _ocur.GetObjectByKey(Of Contracts)(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject.Oid)

        Dim _ContractRows As ContractRows = _onew.CreateObject(Of ContractRows)()
        _ContractRows.Contracts = _onew.GetObject(_Contracts)
        If _Contracts.CurrencyName IsNot Nothing Then
            _ContractRows.CurrencyName = _onew.GetObject(_Contracts.CurrencyName)
        End If

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "ContractRows_DetailView", True, _ContractRows)
    End Sub

    Private Sub aBackToEditableSignedContract_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToEditableSignedContract.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _Contracts As Contracts = TryCast(View.SelectedObjects(0), Contracts)

            If _Contracts Is Nothing Then Throw New Exception("*Nics kiválasztott szerződés!")
            If _Contracts.State <> State.Signed Then Throw New Exception("*A kiválasztott szerződés nem aláírt állapotban van!")

            _Contracts.State = State.Preparation

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub

    Private Sub aModifyContractData_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aModifyContractData.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractModifyData As ContractModifyData = _onew.CreateObject(Of ContractModifyData)()

        e.View = Application.CreateDetailView(_onew, "ContractModifyData_DetailView", False, _ContractModifyData)
    End Sub
    Private Sub aModifyContractData_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aModifyContractData.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractModifyData As ContractModifyData = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractModifyData)
        Dim _Contracts As Contracts = Nothing


        For Each _Contracts In _selection

            If _ContractModifyData.IsContractGroup = True Then
                _Contracts.ContractGroup = _ocur.GetObject(_ContractModifyData.ContractGroup)
            End If

            If _ContractModifyData.IsCurrencyName_Invoice = True Then
                _Contracts.CurrencyName_Invoice = _ocur.GetObject(_ContractModifyData.CurrencyName_Invoice)
            End If

            If _ContractModifyData.IsCurrencyName = True Then
                _Contracts.CurrencyName = _ocur.GetObject(_ContractModifyData.CurrencyName)
            End If

            If _ContractModifyData.IsDateClosed = True Then
                _Contracts.DateClosed = _ContractModifyData.DateClosed
            End If

            If _ContractModifyData.IsDateCreated = True Then
                _ContractModifyData.DateCreated = _ContractModifyData.DateCreated
            End If

            If _ContractModifyData.IsHRResponsible = True Then
                _Contracts.HRResponsible = _ocur.GetObject(_ContractModifyData.HRResponsible)
            End If

            If _ContractModifyData.IsJobNumberObjects = True Then
                _Contracts.JobNumberObjects = _ocur.GetObject(_ContractModifyData.JobNumberObjects)
            End If

            If _ContractModifyData.IsUniqueObjects = True Then
                _Contracts.UniqueObjects = _ocur.GetObject(_ContractModifyData.UniqueObjects)
            End If

            If _ContractModifyData.IsState = True Then
                _Contracts.State = _ContractModifyData.State
            End If

            If _ContractModifyData.IsDefinite Then
                _Contracts.Definite = _ContractModifyData.Definite
            End If
            If _ContractModifyData.IsTerminateDay Then
                _Contracts.TerminateDay = _ContractModifyData.TerminateDay
            End If
        Next
        _ocur.CommitChanges()

    End Sub

    Private Sub ContractRows_ListView_Invoice_Advance_DeleteAction_Executing(sender As Object, e As CancelEventArgs)
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _ContractRows As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)

            If _ContractRows.Contracts.ContractRows.Count = 1 Then
                _ocur.Delete(_ContractRows.Contracts)
            End If
            Dim _CRMEstate_Opportunity_AdvanceMoney As CRMEstate_Opportunity_AdvanceMoney = _
                   _ocur.FindObject(Of CRMEstate_Opportunity_AdvanceMoney)(CriteriaOperator.Parse("ContractRows=?", _ContractRows))
            _ocur.Delete(_CRMEstate_Opportunity_AdvanceMoney)

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub

    Private Sub aDescriptionSelect_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aDescriptionSelect.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader_DescriptionChanger_PopUp As InvoiceHeader_DescriptionChanger_PopUp
        Dim _CS_Description As New CollectionSource(_onew, GetType(InvoiceHeader_DescriptionChanger_PopUp))

        Dim _InvoiceHeader_xpquery = New XPQuery(Of InvoiceHeader)(_ocur.Session)
        Dim query = From _InvoiceHeader In _InvoiceHeader_xpquery _
                    Where _InvoiceHeader.Description IsNot Nothing _
        Group _InvoiceHeader By _InvoiceHeader.Description Into g = Group _
        Select New With {Key .Description = Description}
        For Each item In query
            _InvoiceHeader_DescriptionChanger_PopUp = _onew.CreateObject(Of InvoiceHeader_DescriptionChanger_PopUp)()
            _InvoiceHeader_DescriptionChanger_PopUp.Description = item.Description
            _CS_Description.Add(_InvoiceHeader_DescriptionChanger_PopUp)
        Next
        e.View = Application.CreateListView("InvoiceHeader_DescriptionChanger_PopUp_ListView", _CS_Description, False)
    End Sub

    Private Sub aDescriptionSelect_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aDescriptionSelect.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractsRowsToInvoice As ContractsRowsToInvoice = TryCast(View.SelectedObjects(0), ContractsRowsToInvoice)
        For Each _Description In e.PopupWindow.View.SelectedObjects
            _ContractsRowsToInvoice.Description += vbNewLine & TryCast(_Description, InvoiceHeader_DescriptionChanger_PopUp).Description
        Next
    End Sub

    Private Sub aMarkAsTerminating_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aMarkAsTerminating.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractDatePopup As ContractDatePopup = _onew.CreateObject(Of ContractDatePopup)()

        e.View = Application.CreateDetailView(_onew, "ContractDatePopup_DetailView", True, _ContractDatePopup)

    End Sub
    Private Sub aMarkAsTerminating_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aMarkAsTerminating.Execute
        Dim _Contracts As Contracts = TryCast(View.SelectedObjects(0), Contracts)
        Dim _ContractDatePopup As ContractDatePopup = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractDatePopup)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        _Contracts.State = State.Terminating
        _Contracts.DateTerminating = _ContractDatePopup.DateTerminating
        _ocur.CommitChanges()
        View.Close()
    End Sub
End Class
