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

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Editors

Public Class ImmovablesCostRecordExcelVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of ImmovablesCostRecordExcelVC).aCreateContractRows_Costs.Active.SetItemValue("", False)
        Frame.GetController(Of ImmovablesCostRecordExcelVC).aRemoveContractRows_Costs.Active.SetItemValue("", False)

        If View.Id = "ImmovablesCostRecordEvents_ListView" Then
            Frame.GetController(Of ImmovablesCostRecordExcelVC).aCreateContractRows_Costs.Active.SetItemValue("", True)
            Frame.GetController(Of ImmovablesCostRecordExcelVC).aRemoveContractRows_Costs.Active.SetItemValue("", True)
        End If

        If View.Id = "ImmovablesCostRecordExcel_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If

        If View.Id = "ImmovablesCostRecordExcel_DetailView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "ImmovablesCostRecordExcel_ImmovablesCostRecordExcelMainGroup_ListView" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If
        If View.Id = "ImmovablesCostRecordExcel_ImmovablesCostRecordExcelImmovables_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
    End Sub

    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "ImmovablesCostRecordExcel_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If

        If View.Id = "ImmovablesCostRecordExcel_DetailView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If

        If View.Id = "ImmovablesCostRecordExcel_ImmovablesCostRecordExcelMainGroup_ListView" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
        End If

    End Sub

    Private Sub aCreateContractRows_Costs_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateContractRows_Costs.Execute
        '// Szerződési sor ImmovablesCostRecordEvents-ből
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ImmovablesCostRecordEvents As ImmovablesCostRecordEvents
        Dim _ContractRows_Collection As XPCollection(Of ContractRows)
        Dim _ContractRows_New As ContractRows

        For Each _ImmovablesCostRecordEvents In View.SelectedObjects
            If _ImmovablesCostRecordEvents.ContractRows Is Nothing Then

                _ContractRows_Collection = New XPCollection(Of ContractRows)(_ocur.Session, _
                    CriteriaOperator.Parse("Controlling=? and UniqueObjects=? and Contracts.State in ('Ready','Signed')", _ImmovablesCostRecordEvents.ImmovablesCostRecord.ImmovablesCostRecordGroup.Controlling, _
                                           _ImmovablesCostRecordEvents.ImmovablesCostRecord.Immovables.UniqueObjects))
                _ContractRows_Collection.Sorting.Add(New SortProperty("DateExecution", DB.SortingDirection.Descending))

                'Dim _ContractRows As ContractRows = _ocur.FindObject(Of ContractRows)(CriteriaOperator.Parse("Controlling=? and UniqueObjects=? and CostHolder=?", _ImmovablesCostRecordEvents.ImmovablesCostRecord.ImmovablesCostRecordGroup.Controlling, _
                '                           _ImmovablesCostRecordEvents.ImmovablesCostRecord.Immovables.UniqueObjects, _ImmovablesCostRecordEvents.ImmovablesCostRecord.ImmovablesCostRecordGroup.CostHolder))
                If _ContractRows_Collection.Count > 0 Then


                    _ContractRows_New = _ocur.CreateObject(Of ContractRows)()
                    _ContractRows_New.Contracts = _ContractRows_Collection(0).Contracts
                    _ContractRows_New.ContractRowsType = _ContractRows_Collection(0).ContractRowsType
                    _ContractRows_New.Controlling = _ContractRows_Collection(0).Controlling
                    _ContractRows_New.CostHolder = _ContractRows_Collection(0).CostHolder
                    _ContractRows_New.CostPlace = _ContractRows_Collection(0).CostPlace
                    _ContractRows_New.CurrencyName = _ContractRows_Collection(0).CurrencyName
                    _ContractRows_New.CurrencyRate = _ContractRows_Collection(0).CurrencyRate
                    _ContractRows_New.DateExecution = _ImmovablesCostRecordEvents.ReadDate
                    _ContractRows_New.Description = _ImmovablesCostRecordEvents.Description_ContractRows
                    _ContractRows_New.MileStone = _ContractRows_Collection(0).MileStone
                    _ContractRows_New.NettoPriceDEV = 0
                    _ContractRows_New.NettoPriceHUF = _ImmovablesCostRecordEvents.ReadedValueSumPrice
                    _ContractRows_New.PaymentOption = _ContractRows_Collection(0).PaymentOption
                    _ContractRows_New.QTT = _ContractRows_Collection(0).QTT
                    _ContractRows_New.QualityOption = _ContractRows_Collection(0).QualityOption
                    _ContractRows_New.ShipmentOption = _ContractRows_Collection(0).ShipmentOption
                    _ContractRows_New.ShortName = _ContractRows_Collection(0).ShortName
                    _ContractRows_New.UniqueObjects = _ContractRows_Collection(0).UniqueObjects
                    _ContractRows_New.Units = _ContractRows_Collection(0).Units
                    _ContractRows_New.VAT = _ContractRows_Collection(0).VAT
                    _ContractRows_New.JobNumberObjects = _ContractRows_Collection(0).JobNumberObjects
                    _ContractRows_New.InvoicePeriod = _ImmovablesCostRecordEvents.InvoicePeriod

                    _ContractRows_New.ImmovablesCostRecordEvents = _ImmovablesCostRecordEvents

                    _ImmovablesCostRecordEvents.ContractRows = _ContractRows_New
                    _ImmovablesCostRecordEvents.Save()
                    _ocur.CommitChanges()
                End If


            End If

        Next

        View.Refresh()

    End Sub

    Private Sub aRemoveContractRows_Costs_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRemoveContractRows_Costs.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ImmovablesCostRecordEvents As ImmovablesCostRecordEvents

        For Each _ImmovablesCostRecordEvents In View.SelectedObjects
            If _ImmovablesCostRecordEvents.ContractRows IsNot Nothing Then
                If _ImmovablesCostRecordEvents.ContractRows.InvoiceRows Is Nothing Then
                    _ImmovablesCostRecordEvents.ContractRows.ImmovablesCostRecordEvents = Nothing
                    _ImmovablesCostRecordEvents.ContractRows.Save()
                    _ImmovablesCostRecordEvents.ContractRows = Nothing
                    _ImmovablesCostRecordEvents.Save()
                    _ocur.CommitChanges()
                End If
            End If
        Next
    End Sub

    Private Sub aImportImmovables_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aImportImmovables.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ImmovablesPriceListHeader As ImmovablesPriceListHeader
        Dim _ImmovablesPriceListRowsA As ImmovablesPriceListRowsA
        Dim _ImmovablesPriceListRowsG As ImmovablesPriceListRowsG
        Dim _ImmovablesPriceListRowsO As ImmovablesPriceListRowsO
        Dim _ImmovablesPriceListRowsC As ImmovablesPriceListRowsC


        Dim _Apartments_Collection As New XPCollection(Of Apartments)(_onew.Session)
        Dim _Apartments As Apartments

        Dim _Garages_Collection As New XPCollection(Of Garages)(_onew.Session)
        Dim _Garages As Garages

        Dim _Container_Collection As New XPCollection(Of Container)(_onew.Session)
        Dim _Container As Container

        Dim _Offices_Collection As New XPCollection(Of Offices)(_onew.Session)
        Dim _Offices As Offices

        _ImmovablesPriceListHeader = _onew.CreateObject(Of ImmovablesPriceListHeader)()
        For Each _Apartments In _Apartments_Collection
            _ImmovablesPriceListRowsA = _onew.CreateObject(Of ImmovablesPriceListRowsA)()
            _ImmovablesPriceListRowsA.ImmovablesPriceListHeader = _ImmovablesPriceListHeader
            _ImmovablesPriceListRowsA.Apartments = _Apartments
            _ImmovablesPriceListHeader.ImmovablesPriceListRowsA.Add(_ImmovablesPriceListRowsA)
        Next

        For Each _Garages In _Garages_Collection
            _ImmovablesPriceListRowsG = _onew.CreateObject(Of ImmovablesPriceListRowsG)()
            _ImmovablesPriceListRowsG.ImmovablesPriceListHeader = _ImmovablesPriceListHeader
            _ImmovablesPriceListRowsG.Garages = _Garages
            _ImmovablesPriceListHeader.ImmovablesPriceListRowsG.Add(_ImmovablesPriceListRowsG)
        Next

        For Each _Offices In _Offices_Collection
            _ImmovablesPriceListRowsO = _onew.CreateObject(Of ImmovablesPriceListRowsO)()
            _ImmovablesPriceListRowsO.ImmovablesPriceListHeader = _ImmovablesPriceListHeader
            _ImmovablesPriceListRowsO.Offices = _Offices
            _ImmovablesPriceListHeader.ImmovablesPriceListRowsO.Add(_ImmovablesPriceListRowsO)
        Next

        For Each _Container In _Container_Collection
            _ImmovablesPriceListRowsC = _onew.CreateObject(Of ImmovablesPriceListRowsC)()
            _ImmovablesPriceListRowsC.ImmovablesPriceListHeader = _ImmovablesPriceListHeader
            _ImmovablesPriceListRowsC.Container = _Container
            _ImmovablesPriceListHeader.ImmovablesPriceListRowsC.Add(_ImmovablesPriceListRowsC)
        Next


        e.View = Application.CreateDetailView(_onew, "ImmovablesPriceListHeader_DetailView_Finder", False, _ImmovablesPriceListHeader)
    End Sub

    Private Sub aImportImmovables_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aImportImmovables.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ImmovablesCostRecordExcel As ImmovablesCostRecordExcel = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, ImmovablesCostRecordExcel)
        Dim _ImmovablesCostRecordExcelImmovables As ImmovablesCostRecordExcelImmovables
        Dim _DetailView As DetailView = TryCast(e.PopupWindow.View, DetailView)
        Dim _ListView As ListView = Nothing
        Dim _ListPropertyEditor As ListPropertyEditor

        Dim _ImmovablesPriceListRowsA As ImmovablesPriceListRowsA
        Dim _ImmovablesPriceListRowsG As ImmovablesPriceListRowsG
        Dim _ImmovablesPriceListRowsO As ImmovablesPriceListRowsO
        Dim _ImmovablesPriceListRowsC As ImmovablesPriceListRowsC

        If _ImmovablesCostRecordExcel IsNot Nothing Then
            If _DetailView IsNot Nothing Then
                _ListPropertyEditor = _DetailView.FindItem("ImmovablesPriceListRowsA")
                If _ListPropertyEditor IsNot Nothing Then
                    _ListView = TryCast(_ListPropertyEditor.ListView, ListView)
                    If _ListView IsNot Nothing Then
                        For Each _ImmovablesPriceListRowsA In _ListView.SelectedObjects
                            _ImmovablesCostRecordExcelImmovables = _ocur.CreateObject(Of ImmovablesCostRecordExcelImmovables)()
                            _ImmovablesCostRecordExcelImmovables.ImmovablesCostRecordExcel = _ImmovablesCostRecordExcel
                            _ImmovablesCostRecordExcelImmovables.Immovables = TryCast(_ocur.GetObjectByKey(Of Immovables)(_ImmovablesPriceListRowsA.Apartments.Oid), Immovables)
                            _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelImmovables.Add(_ImmovablesCostRecordExcelImmovables)
                        Next
                    End If
                End If

                _ListPropertyEditor = _DetailView.FindItem("ImmovablesPriceListRowsG")
                If _ListPropertyEditor IsNot Nothing Then
                    _ListView = TryCast(_ListPropertyEditor.ListView, ListView)
                    If _ListView IsNot Nothing Then
                        For Each _ImmovablesPriceListRowsG In _ListView.SelectedObjects
                            _ImmovablesCostRecordExcelImmovables = _ocur.CreateObject(Of ImmovablesCostRecordExcelImmovables)()
                            _ImmovablesCostRecordExcelImmovables.ImmovablesCostRecordExcel = _ImmovablesCostRecordExcel
                            _ImmovablesCostRecordExcelImmovables.Immovables = TryCast(_ocur.GetObjectByKey(Of Immovables)(_ImmovablesPriceListRowsG.Garages.Oid), Immovables)
                            _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelImmovables.Add(_ImmovablesCostRecordExcelImmovables)
                        Next
                    End If
                End If

                _ListPropertyEditor = _DetailView.FindItem("ImmovablesPriceListRowsO")
                If _ListPropertyEditor IsNot Nothing Then
                    _ListView = TryCast(_ListPropertyEditor.ListView, ListView)
                    If _ListView IsNot Nothing Then
                        For Each _ImmovablesPriceListRowsO In _ListView.SelectedObjects
                            _ImmovablesCostRecordExcelImmovables = _ocur.CreateObject(Of ImmovablesCostRecordExcelImmovables)()
                            _ImmovablesCostRecordExcelImmovables.ImmovablesCostRecordExcel = _ImmovablesCostRecordExcel
                            _ImmovablesCostRecordExcelImmovables.Immovables = TryCast(_ocur.GetObjectByKey(Of Immovables)(_ImmovablesPriceListRowsO.Offices.Oid), Immovables)
                            _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelImmovables.Add(_ImmovablesCostRecordExcelImmovables)
                        Next
                    End If
                End If
                _ListPropertyEditor = _DetailView.FindItem("ImmovablesPriceListRowsC")
                If _ListPropertyEditor IsNot Nothing Then
                    _ListView = TryCast(_ListPropertyEditor.ListView, ListView)
                    If _ListView IsNot Nothing Then
                        For Each _ImmovablesPriceListRowsC In _ListView.SelectedObjects
                            _ImmovablesCostRecordExcelImmovables = _ocur.CreateObject(Of ImmovablesCostRecordExcelImmovables)()
                            _ImmovablesCostRecordExcelImmovables.ImmovablesCostRecordExcel = _ImmovablesCostRecordExcel
                            _ImmovablesCostRecordExcelImmovables.Immovables = TryCast(_ocur.GetObjectByKey(Of Immovables)(_ImmovablesPriceListRowsC.Container.Oid), Immovables)
                            _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelImmovables.Add(_ImmovablesCostRecordExcelImmovables)
                        Next
                    End If
                End If
            End If
        End If
        e.PopupWindow.View.ObjectSpace.Rollback()

        _ocur.CommitChanges()

    End Sub

    Private Sub aCreateContractRows_Costs_FromContract_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateContractRows_Costs_FromContract.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ImmovablesCostRecordContactRow As ImmovablesCostRecordContactRow
        Dim _ImmovablesCostRecordEvents As ImmovablesCostRecordEvents = TryCast(View.SelectedObjects(0), ImmovablesCostRecordEvents)

        If _ImmovablesCostRecordEvents IsNot Nothing Then
            If _ImmovablesCostRecordEvents.ImmovablesCostRecord IsNot Nothing Then
                If _ImmovablesCostRecordEvents.ImmovablesCostRecord.ImmovablesCostRecordGroup IsNot Nothing Then

                    _ImmovablesCostRecordContactRow = _onew.CreateObject(Of ImmovablesCostRecordContactRow)()

                    If _ImmovablesCostRecordEvents.ImmovablesCostRecord.ImmovablesCostRecordGroup.Controlling IsNot Nothing Then
                        _ImmovablesCostRecordContactRow.Controlling = _onew.GetObjectByKey(Of Controlling)(_ImmovablesCostRecordEvents.ImmovablesCostRecord.ImmovablesCostRecordGroup.Controlling.Oid)
                    End If
                    If _ImmovablesCostRecordEvents.ImmovablesCostRecord.ImmovablesCostRecordGroup.CostHolder IsNot Nothing Then
                        _ImmovablesCostRecordContactRow.CostHolder = _onew.GetObjectByKey(Of CostHolder)(_ImmovablesCostRecordEvents.ImmovablesCostRecord.ImmovablesCostRecordGroup.CostHolder.Oid)
                    End If
                    If _ImmovablesCostRecordEvents.ImmovablesCostRecord.ImmovablesCostRecordGroup.CostPlace IsNot Nothing Then
                        _ImmovablesCostRecordContactRow.CostPlace = _onew.GetObjectByKey(Of CostPlace)(_ImmovablesCostRecordEvents.ImmovablesCostRecord.ImmovablesCostRecordGroup.CostPlace.Oid)
                    End If

                    If _ImmovablesCostRecordEvents.ImmovablesCostRecord.Immovables.UniqueObjects IsNot Nothing Then
                        _ImmovablesCostRecordContactRow.UniqueObjects = _onew.GetObjectByKey(Of UniqueObjects)(_ImmovablesCostRecordEvents.ImmovablesCostRecord.Immovables.UniqueObjects.Oid)
                    End If

                    e.View = Application.CreateDetailView(_onew, "ImmovablesCostRecordContactRow_DetailView", True, _ImmovablesCostRecordContactRow)

                End If
            End If
        End If
    End Sub

    Private Sub aCreateContractRows_Costs_FromContract_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateContractRows_Costs_FromContract.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ImmovablesCostRecordContactRow As ImmovablesCostRecordContactRow = TryCast(e.PopupWindow.View.CurrentObject, ImmovablesCostRecordContactRow)
        Dim _ImmovablesCostRecordEvents As ImmovablesCostRecordEvents = View.SelectedObjects(0)
        Dim _ContractRows_New As ContractRows
        Dim _CurrencyName As CurrencyName = _ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
        Dim _Units As Units = _ocur.FindObject(Of Units)(CriteriaOperator.Parse("DefaultForBusinnes=True"))
        Dim _VAT As VAT = _ocur.FindObject(Of VAT)(CriteriaOperator.Parse("DefaultForBusinnes=True"))

        Dim _Contracts_ListEditor As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("Contracts")
        If _Contracts_ListEditor.ListView.SelectedObjects.Count > 0 Then
            If _Contracts_ListEditor.ListView.SelectedObjects(0) IsNot Nothing Then
                Dim _Contracts As Contracts = _ocur.GetObjectByKey(Of Contracts)(TryCast(_Contracts_ListEditor.ListView.SelectedObjects(0), Contracts).Oid)

                _ContractRows_New = _ocur.CreateObject(Of ContractRows)()
                _ContractRows_New.Contracts = _Contracts
                _ContractRows_New.ContractRowsType = eContractRowsType.eFix
                If _ImmovablesCostRecordContactRow.Controlling IsNot Nothing Then
                    _ContractRows_New.Controlling = _ocur.GetObjectByKey(Of Controlling)(_ImmovablesCostRecordContactRow.Controlling.Oid)
                End If
                If _ImmovablesCostRecordContactRow.CostHolder IsNot Nothing Then
                    _ContractRows_New.CostHolder = _ocur.GetObjectByKey(Of CostHolder)(_ImmovablesCostRecordContactRow.CostHolder.Oid)
                End If
                If _ImmovablesCostRecordContactRow.CostPlace IsNot Nothing Then
                    _ContractRows_New.CostPlace = _ocur.GetObjectByKey(Of CostPlace)(_ImmovablesCostRecordContactRow.CostPlace.Oid)
                End If
                If _ImmovablesCostRecordContactRow.JobNumberObjects IsNot Nothing Then
                    _ContractRows_New.JobNumberObjects = _ocur.GetObjectByKey(Of JobNumberObjects)(_ImmovablesCostRecordContactRow.JobNumberObjects.Oid)
                End If
                If _ImmovablesCostRecordContactRow.PaymentOption IsNot Nothing Then
                    _ContractRows_New.PaymentOption = _ocur.GetObjectByKey(Of PaymentOption)(_ImmovablesCostRecordContactRow.PaymentOption.Oid)
                End If
                _ContractRows_New.CurrencyName = _CurrencyName
                _ContractRows_New.CurrencyRate = 1
                _ContractRows_New.DateExecution = _ImmovablesCostRecordEvents.ReadDate
                _ContractRows_New.NettoPriceDEV = 0
                _ContractRows_New.NettoPriceHUF = _ImmovablesCostRecordEvents.ReadedValueSumPrice
                _ContractRows_New.QTT = 1
                _ContractRows_New.ShortName = _ImmovablesCostRecordContactRow.ShortName
                _ContractRows_New.UniqueObjects = _ImmovablesCostRecordEvents.ImmovablesCostRecord.Immovables.UniqueObjects
                _ContractRows_New.Units = _Units
                _ContractRows_New.VAT = _VAT
                _ContractRows_New.InvoicePeriod = _ImmovablesCostRecordContactRow.InvoicePeriod

                _ContractRows_New.ImmovablesCostRecordEvents = _ImmovablesCostRecordEvents

                _ImmovablesCostRecordEvents.ContractRows = _ContractRows_New
                _ImmovablesCostRecordEvents.Save()
                _ocur.CommitChanges()

            End If
        End If

    End Sub


End Class
