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

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

Public Class CRMEstateVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event CreateReservationReminder(ByVal _CRMEstate_Leads_Immovables_Reservation As CRMEstate_Leads_Immovables_Reservation)
    Public Sub New()
        MyBase.New()

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

    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        If View.Id = "CRMEstate_Leads_Offices_ListView" Or _
            View.Id = "CRMEstate_Leads_Garages_ListView" Or _
            View.Id = "CRMEstate_Leads_Container_ListView" Or _
            View.Id = "CRMEstate_Leads_StudentHostel_ListView" Or _
            View.Id = "CRMEstate_Opportunity_ListView" Or _
            View.Id = "CRMEstate_Leads_Apartments_ListView" Or _
            View.Id = "CRMEstate_Opportunity_InvoiceHeader_ListView" Or _
            View.Id = "CRMEstate_Opportunity_Contracts_ListView" Or _
            View.Id = "CRMEstate_Opportunity_PCIGroup_ListView" Or _
            View.Id = "CRMEstate_Leads_Selected_Apartments_ListView" Or _
            View.Id = "CRMEstate_Leads_Selected_Container_ListView" Or _
            View.Id = "CRMEstate_Leads_Selected_Garages_ListView" Or _
            View.Id = "CRMEstate_Leads_Selected_Offices_ListView" Or _
            View.Id = "CRMEstate_Leads_Selected_StudentHostel_ListView" Or _
            View.Id = "CRMEstate_Opportunity_Immovables_ListView_Navigation" Then

            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)

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


        If View.Id Like "CRMEstate_Leads_CRMEstate_Leads_Immovables_ListView*" Or _
           View.Id Like "CRMEstate_Leads_CRMEstate_Opportunity_ListView*" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf CRMEstate_Leads_CRMEstate_Opportunity_ListView_DeleteAction_Executing
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_AdvanceMoney_ListView" Or _
           View.Id = "CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_Customers_ListView" Or _
           View.Id = "CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_Payment_Forecast_ListView" Or _
           View.Id = "CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_Rent_ListView" Then
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
        End If
        If View.Id = "CRMEstate_Leads_Immovables_Reservation_Popup_DetailView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "CRMEstate_Leads_Immovables_Reservation_Popup_CollectionOfReservation_ListView" Then
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_AdvanceMoney_ListView" Then
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            AddHandler View.SelectionChanged, AddressOf Selectionchanged_CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_AdvanceMoney_ListView
        End If
        If View.Id = "CRMEstate_Opportunity_AdvanceMoney_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
        End If
        If View.Id = "CRMEstate_Leads_DetailView" Or _
            View.Id = "CRMEstate_Leads_ListView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf CRMEstate_Leads_DetailView_DeleteAction_Executing
        End If
        If View.Id = "CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_Payment_Forecast_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)

            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, _
                AddressOf CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_Payment_Forecast_ListView_DeleteAction_Executing
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "CRMEstate_Leads_Immovables_Reservation_Popup_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "CRMEstate_Leads_Immovables_Reservation_Popup_CollectionOfReservation_ListView" Then
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_AdvanceMoney_ListView" Then
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "CRMEstate_Opportunity_DetailView" Then
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If

        If View.Id Like "CRMEstate_Leads_CRMEstate_Leads_Immovables_ListView*" Or _
          View.Id Like "CRMEstate_Leads_CRMEstate_Opportunity_ListView*" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "CRMEstate_Opportunity_AdvanceMoney_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
        End If
        If View.Id = "CRMEstate_Leads_DetailView" Or _
           View.Id = "CRMEstate_Leads_ListView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf CRMEstate_Leads_DetailView_DeleteAction_Executing
        End If

        If View.Id = "CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_Payment_Forecast_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)

            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, _
                AddressOf CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_Payment_Forecast_ListView_DeleteAction_Executing
        End If
    End Sub

    Private Sub Selectionchanged_CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_AdvanceMoney_ListView()
        '--Ebben a nézetben csak egy példány lehet tehát ha már van egy akkor letiljuk az új létrehozására szolgáló action-t
        If View.SelectedObjects.Count > 0 Then
            Frame.GetController(Of CRMEstateVC).aCreateNewCRMEstate_Opportunity_AdvanceMoney.Active.SetItemValue("", False)
        Else
            Frame.GetController(Of CRMEstateVC).aCreateNewCRMEstate_Opportunity_AdvanceMoney.Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub AcceptAction_Executing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Validator.RuleSet.ValidateAll(View.ObjectSpace, View.SelectedObjects, DefaultContexts.Save)
    End Sub
    Private Sub aCreateOpportunitySold_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateOpportunitySold.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _CRMEstate_Leads As CRMEstate_Leads = TryCast(View.SelectedObjects(0), CRMEstate_Leads)
            Dim _CRMEstate_Opportunity As CRMEstate_Opportunity = Nothing
            Dim _CRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables
            Dim _IsExists As Boolean = False
            Dim _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables
            Dim _DocumentNumber As String = ""
            Dim _CRMEstate_Leads_Immovables_ListEditor As ListPropertyEditor

            _CRMEstate_Leads_Immovables_ListEditor = TryCast(View, DetailView).FindItem("CRMEstate_Leads_Immovables")

            If _CRMEstate_Leads_Immovables_ListEditor.ListView.SelectedObjects.Count < 0 Then Throw New Exception("*Nincs kiválasztva ingatlan !")

            _CRMEstate_Leads_Immovables_ListEditor.ListView.CollectionSource.Sorting.Add(New SortProperty("Immovables.CustomersOwner.FullName", DB.SortingDirection.Descending))

            Dim _CRMEstate_Leads_Immovables_Prew As CRMEstate_Leads_Immovables = Nothing

            For Each _CRMEstate_Leads_Immovables In _CRMEstate_Leads_Immovables_ListEditor.ListView.SelectedObjects
                If _CRMEstate_Leads_Immovables_Prew Is Nothing Then
                    _CRMEstate_Opportunity = _ocur.CreateObject(Of CRMEstate_Opportunity)()
                    _CRMEstate_Opportunity.OpportunityType = eOpportunityType.eSold

                    '_CRMEstate_Opportunity.DocumentNumber = _DocumentNumber

                    _CRMEstate_Opportunity.CRMEstate_Leads = _CRMEstate_Leads
                    _CRMEstate_Opportunity.HRPerson = _CRMEstate_Leads.HRPerson
                    _CRMEstate_Opportunity.ObjectCreated = GetSQLTime(_ocur.Session)

                    _CRMEstate_Leads_Immovables_Prew = _CRMEstate_Leads_Immovables
                ElseIf _CRMEstate_Leads_Immovables_Prew.Oid <> _CRMEstate_Leads_Immovables.Oid Then
                    _CRMEstate_Opportunity = _ocur.CreateObject(Of CRMEstate_Opportunity)()
                    _CRMEstate_Opportunity.OpportunityType = eOpportunityType.eSold

                    '_CRMEstate_Opportunity.DocumentNumber = _DocumentNumber

                    _CRMEstate_Opportunity.CRMEstate_Leads = _CRMEstate_Leads
                    _CRMEstate_Opportunity.HRPerson = _CRMEstate_Leads.HRPerson
                    _CRMEstate_Opportunity.ObjectCreated = GetSQLTime(_ocur.Session)

                    _CRMEstate_Leads_Immovables_Prew = _CRMEstate_Leads_Immovables
                End If

                _CRMEstate_Opportunity_Immovables = _ocur.CreateObject(Of CRMEstate_Opportunity_Immovables)()
                _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity = _CRMEstate_Opportunity
                _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables = _CRMEstate_Leads_Immovables

            Next

            _ocur.CommitChanges()
            View.Refresh()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aCreateContacts_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateContacts.Execute
        'Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        'Dim _CRMEstate_Leads As CRMEstate_Leads = TryCast(View.SelectedObjects(0), CRMEstate_Leads)
        'Dim _Contacts As Contacts
        'Dim _PhoneNumber As PhoneNumber
        '_Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("FirstName=? and LastName=? and (EMail=? OR Mobile=?)", _
        '            _CRMEstate_Leads.FirstName, _CRMEstate_Leads.LastName, _CRMEstate_Leads.EMail, _CRMEstate_Leads.Mobile))

        'If _Contacts IsNot Nothing Then
        '    _CRMEstate_Leads.Contacts = _Contacts
        'Else
        '    _Contacts = _ocur.CreateObject(Of Contacts)()
        '    _Contacts.FirstName = _CRMEstate_Leads.FirstName
        '    _Contacts.LastName = _CRMEstate_Leads.LastName
        '    _Contacts.Email = _CRMEstate_Leads.EMail
        '    If _CRMEstate_Leads.Mobile <> "" Then
        '        _PhoneNumber = _ocur.CreateObject(Of PhoneNumber)()
        '        _PhoneNumber.Number = _CRMEstate_Leads.Mobile
        '        _PhoneNumber.Save()
        '        _Contacts.PhoneNumbers.Add(_PhoneNumber)
        '    End If
        '    _Contacts.Save()
        '    _ocur.CommitChanges()
        'End If
    End Sub
    'Private Sub aCreateContractRent_old_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
    '    Try

    '    Catch ex As Exception
    '        MsgBox(ex.Message, MsgBoxStyle.OkOnly)
    '    End Try
    'End Sub
    'Private Sub aCreateContractSold_OLD_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
    '    Beep()
    '    'Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
    '    'Dim _CRMEstate_Opportunity As CRMEstate_Opportunity = TryCast(View.SelectedObjects(0), CRMEstate_Opportunity)
    '    'Dim _Contracts As Contracts
    '    'Dim _ContractRows As ContractRows
    '    'Dim _CRMEstate_Offer_Template As CRMEstate_Offer_Template = _ocur.FindObject(Of CRMEstate_Offer_Template)(CriteriaOperator.Parse("Oid=Oid"))
    '    'Dim _CRMEstate_Opportunity_Immovables_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("CRMEstate_Opportunity_Immovables")
    '    'Dim _CRMEstate_Offer_Template_CustomersFrom As CRMEstate_Offer_Template_CustomersFrom

    '    ''Ellenőrzések 
    '    'For Each _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables In _CRMEstate_Opportunity_Immovables_ListEditor.ListView.SelectedObjects

    '    '    ' -- Szándéknyilatkozat ellenőrzése
    '    '    If _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_AdvanceMoney.Count = 0 Then
    '    '        Throw New Exception("*Nincs kitötltött szándéknyilatkozat a(z) (" & _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects.Name & ") ingatlanra! Kérjük ellenőrizze!")
    '    '    End If
    '    '    ' --

    '    '    ' -- Tulajdoni hányadok ellenőrzése
    '    '    Dim _RigthQuota As Int32 = 0
    '    '    For Each _CRMEstate_Opportunity_Customers As CRMEstate_Opportunity_Customers In _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_Customers
    '    '        _RigthQuota += _CRMEstate_Opportunity_Customers.RightQuota
    '    '    Next
    '    '    If Not _RigthQuota = 100 Then
    '    '        Throw New Exception("*A tulajdoni hányadok nem adják ki a 100%-ot kérjük ellenőrizze!")
    '    '    End If
    '    '    ' --

    '    '    ' -- Meglévő szerződések ellenőrzése

    '    '    Dim _ExistingContractsCollection As xpcollection = _ocur.CreateCollection(GetType(Contracts), CriteriaOperator.Parse("CRMEstate_Opportunity_Immovables=?", _CRMEstate_Opportunity_Immovables))

    '    '    If _ExistingContractsCollection.Count > 1 Then 'Ha a szándéknyilatkozat szerződésén kívül létezik szerződés az adott ügyhöz akkor azok az adásvételi szerződések!
    '    '        Throw New Exception("*A kiválasztott ingatlanhoz már elkészültek az adásvételi szerződések! Újak generálásához törölje a jelenlegieket!")
    '    '    End If

    '    '    ' --

    '    '    ' -- Fizetési ütemezés ellenőrzése

    '    '    Dim _ForeCastSUM As Int32 = 0

    '    '    For Each _CRMEstate_Opportunity_Payment_Forecast As CRMEstate_Opportunity_Payment_Forecast In _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_Payment_Forecast
    '    '        _ForeCastSUM += _CRMEstate_Opportunity_Payment_Forecast.SumPriceNettoHUF
    '    '    Next
    '    '    If TryCast(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables, Apartments) IsNot Nothing Then
    '    '        If Not _ForeCastSUM = Math.Round((TryCast(_CRMEstate_Opportunity_Immovables.Immovables, Apartments).AreaNettoForSale * _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.AreaPriceNettoHUF), 0, MidpointRounding.AwayFromZero) Then
    '    '            Throw New Exception("*A fizetési ütemezés nem fedi a teljes vételárat! Kérjük ellenőrizze!")
    '    '        End If
    '    '    End If
    '    '    If TryCast(_CRMEstate_Opportunity_Immovables.Immovables, Offices) IsNot Nothing Then
    '    '        If Not _ForeCastSUM = Math.Round((TryCast(_CRMEstate_Opportunity_Immovables.Immovables, Offices).AreaNettoForSale * _CRMEstate_Opportunity_Immovables.AreaPriceNettoHUF), 0, MidpointRounding.AwayFromZero) Then
    '    '            Throw New Exception("*A fizetési ütemezés nem fedi a teljes vételárat! Kérjük ellenőrizze!")
    '    '        End If
    '    '    End If
    '    '    If TryCast(_CRMEstate_Opportunity_Immovables.Immovables, Garages) IsNot Nothing Then
    '    '        If Not _ForeCastSUM = TryCast(_CRMEstate_Opportunity_Immovables.Immovables, Garages).PriceNettoHUF Then
    '    '            Throw New Exception("*A fizetési ütemezés nem fedi a teljes vételárat! Kérjük ellenőrizze!")
    '    '        End If
    '    '    End If
    '    '    If TryCast(_CRMEstate_Opportunity_Immovables.Immovables, Container) IsNot Nothing Then
    '    '        If Not _ForeCastSUM = TryCast(_CRMEstate_Opportunity_Immovables.Immovables, Container).PriceNettoHUF Then
    '    '            Throw New Exception("*A fizetési ütemezés nem fedi a teljes vételárat! Kérjük ellenőrizze!")
    '    '        End If
    '    '    End If

    '    '    ' --
    '    'Next
    '    ''--

    '    ''Szerződések elkészítése
    '    'For Each _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables In _CRMEstate_Opportunity_Immovables_ListEditor.ListView.SelectedObjects

    '    '    _CRMEstate_Offer_Template_CustomersFrom = _ocur.FindObject(Of CRMEstate_Offer_Template_CustomersFrom)(CriteriaOperator.Parse("CustomersFrom=?", _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_AdvanceMoney(0).CustomersFrom))

    '    '    For Each _CRMEstate_Opportunity_Payment_Forecast As CRMEstate_Opportunity_Payment_Forecast In _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_Payment_Forecast

    '    '        For Each _CRMEstate_Opportunity_Customers As CRMEstate_Opportunity_Customers In _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_Customers

    '    '            _Contracts = _ocur.FindObject(Of Contracts)(CriteriaOperator.Parse("CRMEstate_Opportunity_Immovables=? AND Customers=?", _CRMEstate_Opportunity_Immovables, _CRMEstate_Opportunity_Customers.Customers))
    '    '            If _Contracts Is Nothing Then
    '    '                _Contracts = _ocur.CreateObject(Of Contracts)()
    '    '                _Contracts.ContractTemplate = _CRMEstate_Offer_Template_CustomersFrom.ContractTemplate_Final
    '    '                _Contracts.State = State.Ready
    '    '                _Contracts.DocumentNumber = _Contracts.ContractTemplate.NumberGenerator.GetNewNumber(_Contracts.ContractTemplate.NumberGenerator)
    '    '                _Contracts.CRMEstate_Opportunity_Immovables = _CRMEstate_Opportunity_Immovables
    '    '                _Contracts.ContractTemplate = _CRMEstate_Offer_Template_CustomersFrom.ContractTemplate_Final
    '    '                'csak egy szándéknyilatkozat szabad legyen és annak kiállítása megelőzi a szerződések elkészítését így nyugodtan vehetjük a tömb első elemét
    '    '                _Contracts.CustomersFrom = _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_AdvanceMoney(0).CustomersFrom
    '    '                _Contracts.Customers = _CRMEstate_Opportunity_Customers.Customers
    '    '                _Contracts.ShortName = _CRMEstate_Opportunity_Immovables.Immovables.UniqueObjects.Name & " adásvételi szerződés"
    '    '                _Contracts.Save()
    '    '            End If

    '    '            _ContractRows = _ocur.CreateObject(Of ContractRows)()
    '    '            _ContractRows.Contracts = _Contracts
    '    '            _ContractRows.JobNumberObjects = _CRMEstate_Opportunity_Immovables.Immovables.ImmovableGroups.JobNumberObjects
    '    '            _ContractRows.DateExecution = GetSQLTime(_ocur.Session)
    '    '            _ContractRows.ContractRowsType = eContractRowsType.eAdvance
    '    '            _ContractRows.Controlling = _CRMEstate_Offer_Template.Controlling_Final
    '    '            _ContractRows.CostHolder = _CRMEstate_Offer_Template.CostHolder
    '    '            _ContractRows.CostPlace = _CRMEstate_Offer_Template.CostPlace
    '    '            _ContractRows.NettoPriceHUF = Math.Round((_CRMEstate_Opportunity_Payment_Forecast.SumPriceNettoHUF * (_CRMEstate_Opportunity_Customers.RightQuota / 100)), 0, MidpointRounding.AwayFromZero)
    '    '            _ContractRows.VAT = _CRMEstate_Offer_Template.VAT
    '    '            _ContractRows.UniqueObjects = _CRMEstate_Opportunity_Immovables.Immovables.UniqueObjects
    '    '            _ContractRows.PaymentOption = _CRMEstate_Offer_Template.PaymentOption
    '    '            _ContractRows.ShortName = _CRMEstate_Opportunity_Immovables.Immovables.UniqueObjects.Name & " előleg"
    '    '            _ContractRows.Save()

    '    '        Next
    '    '    Next
    '    'Next
    '    ''--

    '    '_ocur.CommitChanges()
    '    'View.Refresh()

    'End Sub
    Private Sub aCreateImmovablesReservation_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateImmovablesReservation.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _CRMEstate_Leads_Immovables_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("CRMEstate_Leads_Immovables")
            If _CRMEstate_Leads_Immovables_ListEditor.ListView.SelectedObjects.Count = 0 Then Throw New Exception("*A foglaláshoz ki kell választani ingatlant!")

            Dim _CRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables = _CRMEstate_Leads_Immovables_ListEditor.ListView.SelectedObjects(0)

            Dim _CRMEstate_Leads_Immovables_Reservation_Popup As CRMEstate_Leads_Immovables_Reservation_Popup = _onew.CreateObject(Of CRMEstate_Leads_Immovables_Reservation_Popup)()
            Dim _CollectionOfReservation As XPCollection = _onew.CreateCollection(GetType(CRMEstate_Leads_Immovables_Reservation), CriteriaOperator.Parse("CRMEstate_Leads_Immovables.Immovables=?", _onew.GetObject(_CRMEstate_Leads_Immovables.Immovables)))
            Dim _CRMEstate_Leads_Immovables_Reservation As CRMEstate_Leads_Immovables_Reservation

            _CRMEstate_Leads_Immovables_Reservation_Popup.CRMEstate_Leads_Immovables = _onew.GetObject(_CRMEstate_Leads_Immovables)

            If _CollectionOfReservation.Count > 0 Then
                Dim sortCollection As SortingCollection = New SortingCollection()
                sortCollection.Add(New SortProperty("DateFrom", DB.SortingDirection.Ascending))
                _CollectionOfReservation.Sorting = sortCollection

                For Each _CRMEstate_Leads_Immovables_Reservation In _CollectionOfReservation
                    _CRMEstate_Leads_Immovables_Reservation_Popup.CollectionOfReservation.Add(_CRMEstate_Leads_Immovables_Reservation)
                Next

                Dim _LastReservation As CRMEstate_Leads_Immovables_Reservation = _CollectionOfReservation(_CollectionOfReservation.Count - 1)

                _CRMEstate_Leads_Immovables_Reservation_Popup.DateFrom = _LastReservation.DateTo
                _CRMEstate_Leads_Immovables_Reservation_Popup.DateTo = _CRMEstate_Leads_Immovables_Reservation_Popup.DateFrom.AddHours(72)
            Else
                _CRMEstate_Leads_Immovables_Reservation_Popup.DateFrom = GetSQLTime(_onew.Session)
                _CRMEstate_Leads_Immovables_Reservation_Popup.DateTo = _CRMEstate_Leads_Immovables_Reservation_Popup.DateFrom.AddHours(72)
            End If

            e.View = Application.CreateDetailView(_onew, "CRMEstate_Leads_Immovables_Reservation_Popup_DetailView", False, _CRMEstate_Leads_Immovables_Reservation_Popup)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aCreateImmovablesReservation_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateImmovablesReservation.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CRMEstate_Leads_Immovables_Reservation_Popup As CRMEstate_Leads_Immovables_Reservation_Popup = TryCast(e.PopupWindow.View.SelectedObjects(0), CRMEstate_Leads_Immovables_Reservation_Popup)
        Dim _CRMEstate_Leads_Immovables_Reservation_New As CRMEstate_Leads_Immovables_Reservation = _ocur.CreateObject(Of CRMEstate_Leads_Immovables_Reservation)()
        Dim _CRMEstate_Leads_Immovables_Reservation As CRMEstate_Leads_Immovables_Reservation
        Dim _DateTo As Date = _CRMEstate_Leads_Immovables_Reservation_Popup.DateTo
        Dim _DateFrom As Date = _CRMEstate_Leads_Immovables_Reservation_Popup.DateFrom
        Dim _CRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables = _CRMEstate_Leads_Immovables_Reservation_Popup.CRMEstate_Leads_Immovables
        Dim _DateTo_New As New Date(_DateTo.Year, _DateTo.Month, _DateTo.Day, 12, 0, 0)
        Dim _DateFrom_New As New Date(_DateFrom.Year, _DateFrom.Month, _DateFrom.Day, 12, 0, 0)
        Dim _CheckDate As Boolean = True

        If _CRMEstate_Leads_Immovables.CRMEstate_Leads_Immovables_Reservation.Count > 0 Then
            Dim _LastReservation As CRMEstate_Leads_Immovables_Reservation = _CRMEstate_Leads_Immovables.CRMEstate_Leads_Immovables_Reservation(_CRMEstate_Leads_Immovables.CRMEstate_Leads_Immovables_Reservation.Count - 1)

            _CRMEstate_Leads_Immovables_Reservation_New.DateFrom = _DateFrom_New
            _CRMEstate_Leads_Immovables_Reservation_New.DateTo = _DateTo_New
            _CRMEstate_Leads_Immovables_Reservation_New.CRMEstate_Leads_Immovables = _ocur.GetObject(_CRMEstate_Leads_Immovables)
            _CRMEstate_Leads_Immovables_Reservation_New.ObjectCreated = GetSQLTime(_ocur.Session)
            _CRMEstate_Leads_Immovables_Reservation_New.UserCreated = _ocur.GetObject(SecuritySystem.CurrentUser)

            For Each _CRMEstate_Leads_Immovables_Reservation In _CRMEstate_Leads_Immovables.CRMEstate_Leads_Immovables_Reservation
                If _CRMEstate_Leads_Immovables_Reservation_New.DateFrom < _CRMEstate_Leads_Immovables_Reservation.DateTo And _CRMEstate_Leads_Immovables_Reservation_New.DateTo > _CRMEstate_Leads_Immovables_Reservation.DateFrom Then
                    _CheckDate = False
                End If
            Next
            If _CheckDate = False Then
                ' -------- TESZT MIATT --------------------------------------------------------------------------------------------
                Throw New Exception("*Hibás foglalási időtartamot adott meg, valószínű van már foglalás erre az intervallumra!")
            End If

            _CRMEstate_Leads_Immovables_Reservation_New.Save()
            _ocur.CommitChanges()
            RaiseEvent CreateReservationReminder(_CRMEstate_Leads_Immovables_Reservation_New)
        Else
            _CRMEstate_Leads_Immovables_Reservation_New.DateFrom = _DateFrom_New
            _CRMEstate_Leads_Immovables_Reservation_New.DateTo = _DateTo_New
            _CRMEstate_Leads_Immovables_Reservation_New.CRMEstate_Leads_Immovables = _ocur.GetObject(_CRMEstate_Leads_Immovables)
            _CRMEstate_Leads_Immovables_Reservation_New.ObjectCreated = GetSQLTime(_ocur.Session)
            _CRMEstate_Leads_Immovables_Reservation_New.UserCreated = _ocur.GetObject(SecuritySystem.CurrentUser)

            _CRMEstate_Leads_Immovables_Reservation_New.Save()
            _ocur.CommitChanges()
            RaiseEvent CreateReservationReminder(_CRMEstate_Leads_Immovables_Reservation_New)
        End If
        Dim _HRPersonTasks As New HRPersonTasks(_ocur.Session)
        With (_HRPersonTasks)
            .AlertPeriod = _ocur.GetObject(_CRMEstate_Leads_Immovables_Reservation_Popup.AlertPeriod)
            .HRPerson = _ocur.FindObject(Of HRPerson)(CriteriaOperator.Parse("User=?", _ocur.GetObject(TryCast(SecuritySystem.CurrentUser, User))))
            .Subject = _CRMEstate_Leads_Immovables.Immovables.DisplayName
            .StartDate = _CRMEstate_Leads_Immovables_Reservation_Popup.DateFrom
            .DueDate = _CRMEstate_Leads_Immovables_Reservation_Popup.DateTo
        End With
        _ocur.CommitChanges()
    End Sub
    Private Sub aCreateOpportunityRent_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateOpportunityRent.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CRMEstate_Leads As CRMEstate_Leads = TryCast(View.SelectedObjects(0), CRMEstate_Leads)
        Dim _CRMEstate_Opportunity As CRMEstate_Opportunity
        Dim _CRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables
        Dim _IsExists As Boolean = False
        Dim _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables

        Dim _DocumentNumber As String = ""
        Dim _CRMEstate_Leads_Immovables_ListEditor As ListPropertyEditor

        _CRMEstate_Leads_Immovables_ListEditor = TryCast(View, DetailView).FindItem("CRMEstate_Leads_Immovables")

        For Each _CRMEstate_Leads_Immovables In _CRMEstate_Leads_Immovables_ListEditor.ListView.SelectedObjects
            'If _CRMEstate_Leads_Immovables.Immovables.ImmovableGroups.NumberGenerator_Opportunity IsNot Nothing Then
            '    _NumberGenerator_Opportunity = _CRMEstate_Leads_Immovables.Immovables.ImmovableGroups.NumberGenerator_Opportunity
            '    _DocumentNumber = _NumberGenerator_Opportunity.GetNewNumber(_NumberGenerator_Opportunity)
            'Else
            '    Throw New UserFriendlyException("Nincs beállítva az ügy sorszám generálása !")
            'End If

            _IsExists = True
            Exit For
        Next

        If _IsExists Then
            _CRMEstate_Opportunity = _ocur.CreateObject(Of CRMEstate_Opportunity)()
            _CRMEstate_Opportunity.OpportunityType = eOpportunityType.eRent
            '_CRMEstate_Opportunity.DocumentNumber = _DocumentNumber
            _CRMEstate_Opportunity.CRMEstate_Leads = _CRMEstate_Leads
            _CRMEstate_Opportunity.HRPerson = _CRMEstate_Leads.HRPerson
            _CRMEstate_Opportunity.ObjectCreated = GetSQLTime(_ocur.Session)
            _CRMEstate_Opportunity.Save()
            For Each _CRMEstate_Leads_Immovables In _CRMEstate_Leads_Immovables_ListEditor.ListView.SelectedObjects
                _CRMEstate_Opportunity_Immovables = _ocur.CreateObject(Of CRMEstate_Opportunity_Immovables)()
                _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity = _CRMEstate_Opportunity
                _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables = _CRMEstate_Leads_Immovables
                _CRMEstate_Opportunity_Immovables.Save()
            Next

            _ocur.CommitChanges()
            View.Refresh()
        Else
            Throw New UserFriendlyException("Nincs kiválasztva ingatlan !")
        End If
    End Sub
    Private Sub aCreateNewCRMEstate_Opportunity_AdvanceMoney_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateNewCRMEstate_Opportunity_AdvanceMoney.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ListView As ListView = TryCast(View, ListView)
            Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
            Dim _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables = TryCast(_PropertyCollectionSource.MasterObject, CRMEstate_Opportunity_Immovables)
            Dim _CRMEstate_Opportunity_AdvanceMoney As CRMEstate_Opportunity_AdvanceMoney = _onew.CreateObject(Of CRMEstate_Opportunity_AdvanceMoney)()
            Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _onew.FindObject(Of ImmovablesGroupsParameters)(CriteriaOperator.Parse("ImmovablesGroups=?", _
                                                                                    _onew.GetObjectByKey(Of ImmovablesGroups)(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups.Oid)))
            If _ImmovablesGroupsParameters Is Nothing Then Throw New Exception("*Nincs ingatlan csoport paraméter a " + _
                                                        _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups.ShortName + " céghez!")

            If _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.CanForSale = False Then _
                Throw New Exception("*A kijelölt ingatlan (" & _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects.Name & ") már nincs eladható státuszban!")

            _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables = TryCast(_onew.GetObject(_CRMEstate_Opportunity_Immovables), CRMEstate_Opportunity_Immovables)
            _CRMEstate_Opportunity_AdvanceMoney.DateCreated = GetSQLTime(_ocur.Session)
            _CRMEstate_Opportunity_AdvanceMoney.PaymentOption = _ImmovablesGroupsParameters.PaymentOption

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, _CRMEstate_Opportunity_AdvanceMoney, True)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aFinalCRMEstate_Opportunity_AdvanceMoney_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalCRMEstate_Opportunity_AdvanceMoney.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Contracts As Contracts
        Dim _ContractRows As ContractRows
        Dim _ContractTemplate_AdvanceMoney = Nothing

        Dim _CRMEstate_Opportunity_AdvanceMoney As CRMEstate_Opportunity_AdvanceMoney = TryCast(View.SelectedObjects(0), CRMEstate_Opportunity_AdvanceMoney)

        Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _ocur.FindObject(Of ImmovablesGroupsParameters)(CriteriaOperator.Parse("ImmovablesGroups=?", _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups))

        If _ImmovablesGroupsParameters IsNot Nothing Then
            'If _ImmovablesGroupsParameters.ContractTemplate_AdvanceMoney IsNot Nothing Then
            '    _ContractTemplate_AdvanceMoney = _ImmovablesGroupsParameters.ContractTemplate_AdvanceMoney
            'Else
            '    Throw New Exception("*A(z) " & _CRMEstate_Opportunity_AdvanceMoney.CustomersFrom.Name & " céghez nincs megfelelő szándéknyilatkozat sablon az ajánlati sablonban! Kérjük ellenőrizze!")
            'End If
        Else
            Throw New Exception("*A(z) " & _CRMEstate_Opportunity_AdvanceMoney.CustomersFrom.Name & " céghez nincs megfelelő sablon az ajánlati sablonban! Kérjük ellenőrizze!")
        End If

        'Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController).Active.SetItemValue("", False)
        '-- A tárgyban forgó ingatlanról levesszük az eladható státuszt
        _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.CanForSale = False
        _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.Save()

        'Létrehozzuk a szerződést a foglalószámlához és a foglaló sort

        'Szerződés fejléc létrehozása
        _Contracts = _ocur.CreateObject(Of Contracts)()
        _Contracts.ContractTemplate = _ContractTemplate_AdvanceMoney
        _Contracts.State = State.Ready
        _Contracts.CustomersFrom = _CRMEstate_Opportunity_AdvanceMoney.CustomersFrom
        _Contracts.Customers = _CRMEstate_Opportunity_AdvanceMoney.Customers
        _Contracts.DateCreated = GetSQLTime(_ocur.Session)
        _Contracts.CRMEstate_Opportunity_Immovables = _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables
        _Contracts.HRResponsible = _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity.HRPerson
        _Contracts.ShortName = _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity.Description
        _Contracts.JobNumberObjects = _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups.JobNumberObjects
        _Contracts.ShortName = "Ingatlan értékesítés"
        _Contracts.Save()

        'Szerződési sor hozzáadása
        _ContractRows = _ocur.CreateObject(Of ContractRows)()
        _ContractRows.Contracts = _Contracts
        _ContractRows.Controlling = _ImmovablesGroupsParameters.Controlling_AdvanceMoney
        _ContractRows.CostHolder = _ImmovablesGroupsParameters.CostHolder
        _ContractRows.CostPlace = _ImmovablesGroupsParameters.CostPlace
        _ContractRows.JobNumberObjects = _Contracts.JobNumberObjects
        _ContractRows.DateExecution = _CRMEstate_Opportunity_AdvanceMoney.DateCreated
        _ContractRows.PaymentOption = _CRMEstate_Opportunity_AdvanceMoney.PaymentOption
        _ContractRows.ContractRowsType = eContractRowsType.eAdvance
        _ContractRows.CurrencyName = _CRMEstate_Opportunity_AdvanceMoney.CurrencyName
        _ContractRows.CurrencyRate = 1
        _ContractRows.ShortName = "Foglaló"
        _ContractRows.UniqueObjects = _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects
        _ContractRows.Units = _ocur.FindObject(Of Units)(CriteriaOperator.Parse("ShortName = 'DB'"))
        _ContractRows.VAT = _ImmovablesGroupsParameters.VAT
        _ContractRows.QTT = 1

        _ContractRows.NettoPriceHUF = _CRMEstate_Opportunity_AdvanceMoney.AmountHUF - (_CRMEstate_Opportunity_AdvanceMoney.AmountHUF / (100 + _ImmovablesGroupsParameters.VAT.KeyValuePercent) _
                                                                                       * _ImmovablesGroupsParameters.VAT.KeyValuePercent)
        _ContractRows.NettoPriceDEV = _CRMEstate_Opportunity_AdvanceMoney.AmountDEV - (_CRMEstate_Opportunity_AdvanceMoney.AmountDEV / (100 + _ImmovablesGroupsParameters.VAT.KeyValuePercent) _
                                                                                       * _ImmovablesGroupsParameters.VAT.KeyValuePercent)
        '_ContractRows.NettoPriceHUF = _CRMEstate_Opportunity_AdvanceMoney.AmountHUF
        '_ContractRows.NettoPriceDEV = _CRMEstate_Opportunity_AdvanceMoney.AmountDEV
        _ContractRows.Save()

        _CRMEstate_Opportunity_AdvanceMoney.ContractRows = _ContractRows
        _CRMEstate_Opportunity_AdvanceMoney.Save()


        _ocur.CommitChanges()
        'Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController).Active.SetItemValue("", True)
        View.Close()
    End Sub
    Private Sub aDeleteCRMEstate_Opportunity_AdvanceMoney_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aDeleteCRMEstate_Opportunity_AdvanceMoney.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CRMEstate_Opportunity_AdvanceMoney As CRMEstate_Opportunity_AdvanceMoney = TryCast(e.SelectedObjects(0), CRMEstate_Opportunity_AdvanceMoney)
        Dim _ContractsCollection As XPCollection = _ocur.CreateCollection(GetType(Contracts), CriteriaOperator.Parse("CRMEstate_Opportunity_Immovables=?", _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables))
        Dim _ContractRowsCollection As xpcollection
        Dim _InOperator As New InOperator

        ' -- Szerződésekről kiállított számlák ellenőrzése
        For Each _Contracts As Contracts In _ContractsCollection
            For Each _ContractRows As ContractRows In _Contracts.ContractRows
                If _ContractRows.InvoiceRows IsNot Nothing Then
                    Throw New Exception("*Az értékesítési szerződésről már készült számla (" & _ContractRows.InvoiceRows.InvoiceHeader.DocumentNumber & ")! A szándéknyilatkozat törléséhez ezt stornózni kell!")
                End If
            Next
        Next

        ' -- A szándéknyilatkozatról kiállított számla ellenőrzése
        If _CRMEstate_Opportunity_AdvanceMoney.ContractRows IsNot Nothing Then
            'Ha számlázva lett akkor előbb le kell stornózni addig nem törölhető!
            If _CRMEstate_Opportunity_AdvanceMoney.ContractRows.InvoiceRows IsNot Nothing Then
                Throw New Exception("*A nyilatkozat alapján már ki lett állítva egy számla (" & _CRMEstate_Opportunity_AdvanceMoney.ContractRows.InvoiceRows.InvoiceHeader.DocumentNumber & ") kérjük stornózza a számlát mielőtt törli a nyilatkozatot!")
            Else
                _CRMEstate_Opportunity_AdvanceMoney.ContractRows.Contracts.Delete()
                _CRMEstate_Opportunity_AdvanceMoney.ContractRows.Delete()
                _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.CanForSale = True
                _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.Save()
                _CRMEstate_Opportunity_AdvanceMoney.Delete()
            End If
        Else
            _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.CanForSale = True
            _CRMEstate_Opportunity_AdvanceMoney.CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.Save()
            _CRMEstate_Opportunity_AdvanceMoney.Delete()
        End If

        ' -- Szerződési sorok összegyűjtése, ha nem volt ex akkor ezeket is törölni kell

        _ContractRowsCollection = _ocur.CreateCollection(GetType(ContractRows), New InOperator("Contracts", _ContractsCollection))

        '--

        _ocur.Delete(_ContractRowsCollection) ' -- sorok törlése
        _ocur.Delete(_ContractsCollection) ' -- szerződések törlése
        _ocur.CommitChanges()

        If TryCast(View, DetailView) IsNot Nothing Then
            View.Close(False)
        Else
            View.Refresh()
        End If
    End Sub
    Private Sub aSelectImmovables_Apartments_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSelectImmovables_Apartments.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ListView As ListView = TryCast(View, ListView)
        Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
        Dim _CRMEstate_Leads = TryCast(_PropertyCollectionSource.MasterObject, CRMEstate_Leads)
        Dim _CRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables
        Dim _ImmovablesPriceListRowsA As ImmovablesPriceListRowsA
        Dim _ExistingCRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables 'segédváltozó aminek akkor van értéke ha már létezik ingatlan lehetőség az aktuális ingatlanra

        For Each _ImmovablesPriceListRowsA In View.SelectedObjects
            _ExistingCRMEstate_Leads_Immovables = _ocur.FindObject(Of CRMEstate_Leads_Immovables)(CriteriaOperator.Parse("Immovables=? AND CRMEstate_Leads=?", TryCast(_ImmovablesPriceListRowsA.Apartments, Immovables), _CRMEstate_Leads))
            If _ExistingCRMEstate_Leads_Immovables Is Nothing Then
                _CRMEstate_Leads_Immovables = _ocur.CreateObject(Of CRMEstate_Leads_Immovables)()
                _CRMEstate_Leads_Immovables.CRMEstate_Leads = _CRMEstate_Leads

                Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _ocur.FindObject(Of ImmovablesGroupsParameters) _
                                                (CriteriaOperator.Parse("ImmovablesGroups=? AND CustomersFrom.Oid=?" _
                                                    , _ImmovablesPriceListRowsA.Apartments.ImmovableGroups, _ImmovablesPriceListRowsA.Apartments.CustomersOwner.Oid))
                If _ImmovablesGroupsParameters IsNot Nothing Then
                    _CRMEstate_Leads_Immovables.VAT = _ImmovablesGroupsParameters.VAT
                Else
                    _CRMEstate_Leads_Immovables.VAT = _ocur.FindObject(Of VAT)(CriteriaOperator.Parse("DefaultForBusinnes='True'"))
                End If

                _CRMEstate_Leads_Immovables.Immovables = TryCast(_ImmovablesPriceListRowsA.Apartments, Immovables)
                _CRMEstate_Leads_Immovables.AreaPriceDivide = 30

                _CRMEstate_Leads_Immovables.AreaListPriceBruttoDEV = _ImmovablesPriceListRowsA.AreaListPriceBruttoDEV
                _CRMEstate_Leads_Immovables.AreaListPriceBruttoDEVRent = _ImmovablesPriceListRowsA.AreaListPriceBruttoDEVRent
                _CRMEstate_Leads_Immovables.AreaListPriceBruttoHUF = _ImmovablesPriceListRowsA.AreaListPriceBruttoHUF
                _CRMEstate_Leads_Immovables.AreaListPriceBruttoHUFRent = _ImmovablesPriceListRowsA.AreaListPriceBruttoHUFRent
                _CRMEstate_Leads_Immovables.AreaListPriceNettoDEV = _ImmovablesPriceListRowsA.AreaListPriceNettoDEV
                _CRMEstate_Leads_Immovables.AreaListPriceNettoDEVRent = _ImmovablesPriceListRowsA.AreaListPriceNettoDEVRent
                _CRMEstate_Leads_Immovables.AreaListPriceNettoHUF = _ImmovablesPriceListRowsA.AreaListPriceNettoHUF
                _CRMEstate_Leads_Immovables.AreaListPriceNettoHUFRent = _ImmovablesPriceListRowsA.AreaListPriceNettoHUFRent

                _CRMEstate_Leads_Immovables.ListPriceBruttoDEV = _ImmovablesPriceListRowsA.ListPriceBruttoDEV
                _CRMEstate_Leads_Immovables.ListPriceBruttoDEVRent = _ImmovablesPriceListRowsA.ListPriceBruttoDEVRent
                _CRMEstate_Leads_Immovables.ListPriceBruttoHUF = _ImmovablesPriceListRowsA.ListPriceBruttoHUF
                _CRMEstate_Leads_Immovables.ListPriceBruttoHUFRent = _ImmovablesPriceListRowsA.ListPriceBruttoHUFRent
                _CRMEstate_Leads_Immovables.ListPriceNettoDEV = _ImmovablesPriceListRowsA.ListPriceNettoDEV
                _CRMEstate_Leads_Immovables.ListPriceNettoDEVRent = _ImmovablesPriceListRowsA.ListPriceNettoDEVRent
                _CRMEstate_Leads_Immovables.ListPriceNettoHUF = _ImmovablesPriceListRowsA.ListPriceNettoHUF
                _CRMEstate_Leads_Immovables.ListPriceNettoHUFRent = _ImmovablesPriceListRowsA.ListPriceNettoHUFRent

                If _CRMEstate_Leads_Immovables.Immovables IsNot Nothing Then
                    If TryCast(_CRMEstate_Leads_Immovables.Immovables, Apartments) IsNot Nothing Then
                        _CRMEstate_Leads_Immovables.ParcelPriceNettoHUF = Math.Round((Math.Round((TryCast(_CRMEstate_Leads_Immovables.Immovables, Apartments).AreaPriceNettoHUF * TryCast(_CRMEstate_Leads_Immovables.Immovables, Apartments).AreaNettoForSale), 0, MidpointRounding.AwayFromZero) * (_CRMEstate_Leads_Immovables.AreaPriceDivide / 100)), 0, MidpointRounding.AwayFromZero)
                        _CRMEstate_Leads_Immovables.SuperstructurePriceNettoHUF = (TryCast(_CRMEstate_Leads_Immovables.Immovables, Apartments).AreaPriceNettoHUF * TryCast(_CRMEstate_Leads_Immovables.Immovables, Apartments).AreaNettoForSale) - _CRMEstate_Leads_Immovables.ParcelPriceNettoHUF
                    End If
                    If TryCast(_CRMEstate_Leads_Immovables.Immovables, Offices) IsNot Nothing Then
                        _CRMEstate_Leads_Immovables.ParcelPriceNettoHUF = Math.Round((Math.Round((TryCast(_CRMEstate_Leads_Immovables.Immovables, Offices).AreaPriceNettoHUF * TryCast(_CRMEstate_Leads_Immovables.Immovables, Offices).AreaNettoForSale), 0, MidpointRounding.AwayFromZero) * (_CRMEstate_Leads_Immovables.AreaPriceDivide / 100)), 0, MidpointRounding.AwayFromZero)
                        _CRMEstate_Leads_Immovables.SuperstructurePriceNettoHUF = (TryCast(_CRMEstate_Leads_Immovables.Immovables, Offices).AreaPriceNettoHUF * TryCast(_CRMEstate_Leads_Immovables.Immovables, Offices).AreaNettoForSale) - _CRMEstate_Leads_Immovables.ParcelPriceNettoHUF
                    End If
                    If TryCast(_CRMEstate_Leads_Immovables.Immovables, Container) IsNot Nothing Then
                        _CRMEstate_Leads_Immovables.ParcelPriceNettoHUF = Math.Round(((TryCast(_CRMEstate_Leads_Immovables.Immovables, Container).PriceNettoHUF) * (_CRMEstate_Leads_Immovables.AreaPriceDivide / 100)), 0, MidpointRounding.AwayFromZero)
                        _CRMEstate_Leads_Immovables.SuperstructurePriceNettoHUF = TryCast(_CRMEstate_Leads_Immovables.Immovables, Container).PriceNettoHUF - _CRMEstate_Leads_Immovables.ParcelPriceNettoHUF
                    End If
                    If TryCast(_CRMEstate_Leads_Immovables.Immovables, Garages) IsNot Nothing Then
                        _CRMEstate_Leads_Immovables.ParcelPriceNettoHUF = Math.Round(((TryCast(_CRMEstate_Leads_Immovables.Immovables, Garages).PriceNettoHUF) * (_CRMEstate_Leads_Immovables.AreaPriceDivide / 100)), 0, MidpointRounding.AwayFromZero)
                        _CRMEstate_Leads_Immovables.SuperstructurePriceNettoHUF = TryCast(_CRMEstate_Leads_Immovables.Immovables, Garages).PriceNettoHUF - _CRMEstate_Leads_Immovables.ParcelPriceNettoHUF
                    End If
                End If

                _CRMEstate_Leads_Immovables.Save()
            End If
        Next

        _ocur.CommitChanges()
        View.Refresh()
    End Sub
    Private Sub aSelectImmovables_Offices_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSelectImmovables_Offices.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ListView As ListView = TryCast(View, ListView)
        Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
        Dim _CRMEstate_Leads = TryCast(_PropertyCollectionSource.MasterObject, CRMEstate_Leads)
        Dim _CRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables
        Dim _ImmovablesPriceListRowsO As ImmovablesPriceListRowsO
        Dim _ExistingCRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables 'segédváltozó aminek akkor van értéke ha már létezik ingatlan lehetőség az aktuális ingatlanra

        For Each _ImmovablesPriceListRowsO In View.SelectedObjects
            _ExistingCRMEstate_Leads_Immovables = _ocur.FindObject(Of CRMEstate_Leads_Immovables)(CriteriaOperator.Parse("Immovables=? AND CRMEstate_Leads=?", TryCast(_ImmovablesPriceListRowsO.Offices, Immovables), _CRMEstate_Leads))
            If _ExistingCRMEstate_Leads_Immovables Is Nothing Then
                _CRMEstate_Leads_Immovables = _ocur.CreateObject(Of CRMEstate_Leads_Immovables)()
                _CRMEstate_Leads_Immovables.CRMEstate_Leads = _CRMEstate_Leads

                Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _ocur.FindObject(Of ImmovablesGroupsParameters) _
                                                (CriteriaOperator.Parse("ImmovablesGroups=? AND CustomersFrom.Oid=?" _
                                                    , _ImmovablesPriceListRowsO.Offices.ImmovableGroups, _ImmovablesPriceListRowsO.Offices.CustomersOwner.Oid))
                If _ImmovablesGroupsParameters IsNot Nothing Then
                    _CRMEstate_Leads_Immovables.VAT = _ImmovablesGroupsParameters.VAT
                Else
                    _CRMEstate_Leads_Immovables.VAT = _ocur.FindObject(Of VAT)(CriteriaOperator.Parse("DefaultForBusinnes='True'"))
                End If
                _CRMEstate_Leads_Immovables.Immovables = TryCast(_ImmovablesPriceListRowsO.Offices, Immovables)

                _CRMEstate_Leads_Immovables.AreaListPriceBruttoDEV = _ImmovablesPriceListRowsO.AreaListPriceBruttoDEV
                _CRMEstate_Leads_Immovables.AreaListPriceBruttoDEVRent = _ImmovablesPriceListRowsO.AreaListPriceBruttoDEVRent
                _CRMEstate_Leads_Immovables.AreaListPriceBruttoHUF = _ImmovablesPriceListRowsO.AreaListPriceBruttoHUF
                _CRMEstate_Leads_Immovables.AreaListPriceBruttoHUFRent = _ImmovablesPriceListRowsO.AreaListPriceBruttoHUFRent
                _CRMEstate_Leads_Immovables.AreaListPriceNettoDEV = _ImmovablesPriceListRowsO.AreaListPriceNettoDEV
                _CRMEstate_Leads_Immovables.AreaListPriceNettoDEVRent = _ImmovablesPriceListRowsO.AreaListPriceNettoDEVRent
                _CRMEstate_Leads_Immovables.AreaListPriceNettoHUF = _ImmovablesPriceListRowsO.AreaListPriceNettoHUF
                _CRMEstate_Leads_Immovables.AreaListPriceNettoHUFRent = _ImmovablesPriceListRowsO.AreaListPriceNettoHUFRent

                _CRMEstate_Leads_Immovables.ListPriceBruttoDEV = _ImmovablesPriceListRowsO.ListPriceBruttoDEV
                _CRMEstate_Leads_Immovables.ListPriceBruttoDEVRent = _ImmovablesPriceListRowsO.ListPriceBruttoDEVRent
                _CRMEstate_Leads_Immovables.ListPriceBruttoHUF = _ImmovablesPriceListRowsO.ListPriceBruttoHUF
                _CRMEstate_Leads_Immovables.ListPriceBruttoHUFRent = _ImmovablesPriceListRowsO.ListPriceBruttoHUFRent
                _CRMEstate_Leads_Immovables.ListPriceNettoDEV = _ImmovablesPriceListRowsO.ListPriceNettoDEV
                _CRMEstate_Leads_Immovables.ListPriceNettoDEVRent = _ImmovablesPriceListRowsO.ListPriceNettoDEVRent
                _CRMEstate_Leads_Immovables.ListPriceNettoHUF = _ImmovablesPriceListRowsO.ListPriceNettoHUF
                _CRMEstate_Leads_Immovables.ListPriceNettoHUFRent = _ImmovablesPriceListRowsO.ListPriceNettoHUFRent

                _CRMEstate_Leads_Immovables.Save()
            End If
        Next

        _ocur.CommitChanges()
        View.Refresh()

    End Sub
    Private Sub aSelectImmovables_Garages_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSelectImmovables_Garages.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ListView As ListView = TryCast(View, ListView)
        Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
        Dim _CRMEstate_Leads = TryCast(_PropertyCollectionSource.MasterObject, CRMEstate_Leads)
        Dim _CRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables
        Dim _ImmovablesPriceListRowsG As ImmovablesPriceListRowsG
        Dim _ExistingCRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables 'segédváltozó aminek akkor van értéke ha már létezik ingatlan lehetőség az aktuális ingatlanra

        For Each _ImmovablesPriceListRowsG In View.SelectedObjects
            _ExistingCRMEstate_Leads_Immovables = _ocur.FindObject(Of CRMEstate_Leads_Immovables)(CriteriaOperator.Parse("Immovables=? AND CRMEstate_Leads=?", TryCast(_ImmovablesPriceListRowsG.Garages, Immovables), _CRMEstate_Leads))
            If _ExistingCRMEstate_Leads_Immovables Is Nothing Then
                _CRMEstate_Leads_Immovables = _ocur.CreateObject(Of CRMEstate_Leads_Immovables)()
                _CRMEstate_Leads_Immovables.CRMEstate_Leads = _CRMEstate_Leads

                Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _ocur.FindObject(Of ImmovablesGroupsParameters) _
                                               (CriteriaOperator.Parse("ImmovablesGroups=? AND CustomersFrom.Oid=?" _
                                                   , _ImmovablesPriceListRowsG.Garages.ImmovableGroups, _ImmovablesPriceListRowsG.Garages.CustomersOwner.Oid))
                If _ImmovablesGroupsParameters IsNot Nothing Then
                    _CRMEstate_Leads_Immovables.VAT = _ImmovablesGroupsParameters.VAT
                Else
                    _CRMEstate_Leads_Immovables.VAT = _ocur.FindObject(Of VAT)(CriteriaOperator.Parse("DefaultForBusinnes='True'"))
                End If

                _CRMEstate_Leads_Immovables.Immovables = TryCast(_ImmovablesPriceListRowsG.Garages, Immovables)

                _CRMEstate_Leads_Immovables.AreaPriceDivide = 30

                _CRMEstate_Leads_Immovables.ListPriceBruttoDEV = _ImmovablesPriceListRowsG.ListPriceBruttoDEV
                _CRMEstate_Leads_Immovables.ListPriceBruttoDEVRent = _ImmovablesPriceListRowsG.ListPriceBruttoDEVRent
                _CRMEstate_Leads_Immovables.ListPriceBruttoHUF = _ImmovablesPriceListRowsG.ListPriceBruttoHUF
                _CRMEstate_Leads_Immovables.ListPriceBruttoHUFRent = _ImmovablesPriceListRowsG.ListPriceBruttoHUFRent
                _CRMEstate_Leads_Immovables.ListPriceNettoDEV = _ImmovablesPriceListRowsG.ListPriceNettoDEV
                _CRMEstate_Leads_Immovables.ListPriceNettoDEVRent = _ImmovablesPriceListRowsG.ListPriceNettoDEVRent
                _CRMEstate_Leads_Immovables.ListPriceNettoHUF = _ImmovablesPriceListRowsG.ListPriceNettoHUF
                _CRMEstate_Leads_Immovables.ListPriceNettoHUFRent = _ImmovablesPriceListRowsG.ListPriceNettoHUFRent

                _CRMEstate_Leads_Immovables.Save()
            End If
        Next

        _ocur.CommitChanges()
        View.Refresh()

    End Sub
    Private Sub aSelectImmovables_Container_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSelectImmovables_Container.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ListView As ListView = TryCast(View, ListView)
        Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
        Dim _CRMEstate_Leads = TryCast(_PropertyCollectionSource.MasterObject, CRMEstate_Leads)
        Dim _CRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables
        Dim _ImmovablesPriceListRowsC As ImmovablesPriceListRowsC
        Dim _ExistingCRMEstate_Leads_Immovables As CRMEstate_Leads_Immovables 'segédváltozó aminek akkor van értéke ha már létezik ingatlan lehetőség az aktuális ingatlanra

        For Each _ImmovablesPriceListRowsC In View.SelectedObjects
            _ExistingCRMEstate_Leads_Immovables = _ocur.FindObject(Of CRMEstate_Leads_Immovables)(CriteriaOperator.Parse("Immovables=? AND CRMEstate_Leads=?", TryCast(_ImmovablesPriceListRowsC.Container, Immovables), _CRMEstate_Leads))
            If _ExistingCRMEstate_Leads_Immovables Is Nothing Then
                _CRMEstate_Leads_Immovables = _ocur.CreateObject(Of CRMEstate_Leads_Immovables)()
                _CRMEstate_Leads_Immovables.CRMEstate_Leads = _CRMEstate_Leads

                Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _ocur.FindObject(Of ImmovablesGroupsParameters) _
                                               (CriteriaOperator.Parse("ImmovablesGroups=? AND CustomersFrom.Oid=?" _
                                                   , _ImmovablesPriceListRowsC.Container.ImmovableGroups, _ImmovablesPriceListRowsC.Container.CustomersOwner.Oid))
                If _ImmovablesGroupsParameters IsNot Nothing Then
                    _CRMEstate_Leads_Immovables.VAT = _ImmovablesGroupsParameters.VAT
                Else
                    _CRMEstate_Leads_Immovables.VAT = _ocur.FindObject(Of VAT)(CriteriaOperator.Parse("DefaultForBusinnes='True'"))
                End If

                _CRMEstate_Leads_Immovables.Immovables = TryCast(_ImmovablesPriceListRowsC.Container, Immovables)

                _CRMEstate_Leads_Immovables.AreaPriceDivide = 30

                _CRMEstate_Leads_Immovables.ListPriceBruttoDEV = _ImmovablesPriceListRowsC.ListPriceBruttoDEV
                _CRMEstate_Leads_Immovables.ListPriceBruttoDEVRent = _ImmovablesPriceListRowsC.ListPriceBruttoDEVRent
                _CRMEstate_Leads_Immovables.ListPriceBruttoHUF = _ImmovablesPriceListRowsC.ListPriceBruttoHUF
                _CRMEstate_Leads_Immovables.ListPriceBruttoHUFRent = _ImmovablesPriceListRowsC.ListPriceBruttoHUFRent
                _CRMEstate_Leads_Immovables.ListPriceNettoDEV = _ImmovablesPriceListRowsC.ListPriceNettoDEV
                _CRMEstate_Leads_Immovables.ListPriceNettoDEVRent = _ImmovablesPriceListRowsC.ListPriceNettoDEVRent
                _CRMEstate_Leads_Immovables.ListPriceNettoHUF = _ImmovablesPriceListRowsC.ListPriceNettoHUF
                _CRMEstate_Leads_Immovables.ListPriceNettoHUFRent = _ImmovablesPriceListRowsC.ListPriceNettoHUFRent

                _CRMEstate_Leads_Immovables.Save()
            End If
        Next

        _ocur.CommitChanges()
        View.Refresh()

    End Sub
    Private Sub CRMEstate_Leads_DetailView_DeleteAction_Executing(sender As Object, e As CancelEventArgs)
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _CRMEstate_Leads As CRMEstate_Leads = TryCast(View.SelectedObjects(0), CRMEstate_Leads)
            If _CRMEstate_Leads.CRMEstate_Opportunity.Count > 0 Then Throw New Exception("*Nem lehet törölni a kiválasztott potenciális ügyfelet, mert tartozik hozzá CRM ügy!" & _
                                                        vbNewLine & "Előbb törölnie kell a meglévő CRM ügyet!")
        Catch ex As Exception
            e.Cancel = True
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub CRMEstate_Leads_CRMEstate_Opportunity_ListView_DeleteAction_Executing(sender As Object, e As CancelEventArgs)
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            For Each _CRMEstate_Opportunity As CRMEstate_Opportunity In View.SelectedObjects
                If _CRMEstate_Opportunity.Contracts.Count > 0 Then Throw New Exception("*Nem lehet törölni egy vagy több CRM ügyet, készült(ek) szerződés(ek)!" & _
                                                                                    vbNewLine & "Előbb a szerződéseket kell törölnie!")
            Next
        Catch ex As Exception
            e.Cancel = True
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aCreateNewCRMEstate_Opportunity_Payment_Forecast_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateNewCRMEstate_Opportunity_Payment_Forecast.Execute
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

            Dim _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject
            If _CRMEstate_Opportunity_Immovables Is Nothing Then Throw New Exception("*Nincs kiválasztva ingatlan!")
            Dim _CRMEstate_Opportunity_Payment_Forecast_Collection As New XPCollection(Of CRMEstate_Opportunity_Payment_Forecast)(_onew.Session, _
                                                                                       CriteriaOperator.Parse("CRMEstate_Opportunity_Immovables=?", _onew.GetObjectByKey(Of CRMEstate_Opportunity_Immovables)(_CRMEstate_Opportunity_Immovables.Oid)))

            Dim _SumPricePercent As Double
            For Each _CRMEstateOPF As CRMEstate_Opportunity_Payment_Forecast In _CRMEstate_Opportunity_Payment_Forecast_Collection
                _SumPricePercent += _CRMEstateOPF.SumPricePercent
            Next

            If _SumPricePercent >= 100 Then Throw New Exception("*Nem lehet újabb fizetési ütemezést felvinni mert a teljes összegre készült már ütemezés!")

            Dim _CRMEstate_Opportunity_Payment_Forecast As CRMEstate_Opportunity_Payment_Forecast = _onew.CreateObject(Of CRMEstate_Opportunity_Payment_Forecast)()

            _CRMEstate_Opportunity_Payment_Forecast.CRMEstate_Opportunity_Immovables = _onew.GetObjectByKey(Of CRMEstate_Opportunity_Immovables)(_CRMEstate_Opportunity_Immovables.Oid)
            _CRMEstate_Opportunity_Payment_Forecast.CurrencyName = _onew.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
            Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _onew.FindObject(Of ImmovablesGroupsParameters)(CriteriaOperator.Parse("ImmovablesGroups=?", _
                                                                        _onew.GetObjectByKey(Of ImmovablesGroups)(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups.Oid)))
            If _ImmovablesGroupsParameters IsNot Nothing Then
                _CRMEstate_Opportunity_Payment_Forecast.CustomersFrom = _ImmovablesGroupsParameters.CustomersFrom
            End If

            _CRMEstate_Opportunity_Payment_Forecast.DateExecution = GetSQLTime(_onew.Session)


            _CRMEstate_Opportunity_Payment_Forecast.RowIndex = _CRMEstate_Opportunity_Payment_Forecast_Collection.Count + 1
            _CRMEstate_Opportunity_Payment_Forecast.SumPriceBruttoHUF = _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.ListPriceBruttoHUF
            _CRMEstate_Opportunity_Payment_Forecast.SumPricePercent = 100 - _SumPricePercent

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "CRMEstate_Opportunity_Payment_Forecast_DetailView", True, _CRMEstate_Opportunity_Payment_Forecast)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub CRMEstate_Opportunity_Immovables_CRMEstate_Opportunity_Payment_Forecast_ListView_DeleteAction_Executing(sender As Object, e As CancelEventArgs)
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            For Each _CRMEstate_Opportunity_Payment_Forecast As CRMEstate_Opportunity_Payment_Forecast In View.SelectedObjects

                If _CRMEstate_Opportunity_Payment_Forecast.ContractRows IsNot Nothing Then
                    e.Cancel = True
                    Throw New Exception("*A kiválasztott sorok közt van olyan amiről készült már szerződési sor!" + vbNewLine + "Törlés előtt a szerződést kell stornózni!")
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aCreateContractRent_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateContractRent.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CRMEstate_Opportunity As CRMEstate_Opportunity = TryCast(View.SelectedObjects(0), CRMEstate_Opportunity)

            Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ContractTemplate))

            _CS.BeginUpdateCriteria()
            _CS.Criteria.Add("Criteria", CriteriaOperator.Parse("IsImmovables='True'"))
            _CS.EndUpdateCriteria()

            If _CS.List.Count = 0 Then Throw New Exception("*Nincs egyetlen olyan szerződés sablon sem, amely használható lenne!")

            e.View = Application.CreateListView("ContractTemplate_ListView", _CS, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aCreateContractRent_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateContractRent.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _CRMEstate_Opportunity As CRMEstate_Opportunity = TryCast(View.SelectedObjects(0), CRMEstate_Opportunity)
            Dim _ContractTemplateRent As ContractTemplate = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractTemplate)
            Dim _ContractRows As ContractRows
            Dim _Contracts As Contracts
            Dim _CRMEstate_Opportunity_Immovables_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("CRMEstate_Opportunity_Immovables")

            Dim _NumberOfMonths As Long

            For Each _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables In _CRMEstate_Opportunity_Immovables_ListEditor.ListView.SelectedObjects

                For Each _CRMEstate_Opportunity_Rent As CRMEstate_Opportunity_Rent In _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_Rent

                    Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _ocur.FindObject(Of ImmovablesGroupsParameters)(CriteriaOperator.Parse("ImmovablesGroups=?", _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups))

                    If _ImmovablesGroupsParameters Is Nothing Then Throw New Exception("*Nincs beállítva paraméter a(z) " + _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups.ShortName + _
                                                                                        " ingatlancsoporthoz!" + vbNewLine + "Kérem ellenőrizze!")

                    For Each _CRMEstate_Opportunity_Customers As CRMEstate_Opportunity_Customers In _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_Customers

                        _Contracts = _ocur.FindObject(Of Contracts)(CriteriaOperator.Parse("Customers=? AND CRMEstate_Opportunity_Immovables=?", _CRMEstate_Opportunity_Customers.Customers, _CRMEstate_Opportunity_Immovables))

                        If _Contracts Is Nothing Then
                            _Contracts = _ocur.CreateObject(Of Contracts)()
                            _Contracts.State = State.Ready
                            _Contracts.DocumentNumber = _ContractTemplateRent.NumberGenerator.GetNewNumber(_ContractTemplateRent.NumberGenerator)
                            _Contracts.CustomersFrom = _CRMEstate_Opportunity_Rent.CustomersFrom
                            _Contracts.Customers = _CRMEstate_Opportunity_Customers.Customers
                            _Contracts.DateCreated = GetSQLTime(_ocur.Session)
                            _Contracts.ShortName = _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects.Name & " Bérleti szerződés"
                            _Contracts.CRMEstate_Opportunity_Immovables = _CRMEstate_Opportunity_Immovables
                            _Contracts.UniqueObjects = _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects
                            _Contracts.DateExecution = _CRMEstate_Opportunity_Rent.DateStart
                            _Contracts.DateExpectClose = _CRMEstate_Opportunity_Rent.DateStop
                            _Contracts.ContractTemplate = _ocur.GetObjectByKey(Of ContractTemplate)(_ContractTemplateRent.Oid)
                            _Contracts.HRResponsible = _CRMEstate_Opportunity.HRPerson
                            _Contracts.CurrencyName = _CRMEstate_Opportunity_Rent.CurrencyName
                            _Contracts.CurrencyName_Invoice = _CRMEstate_Opportunity_Rent.CurrencyName
                            _Contracts.JobNumberObjects = _ImmovablesGroupsParameters.ImmovablesGroups.JobNumberObjects
                            _NumberOfMonths = DateDiff(DateInterval.Month, _CRMEstate_Opportunity_Rent.DateStart, _CRMEstate_Opportunity_Rent.DateStop)
                        End If

                        For i = 1 To _NumberOfMonths
                            _ContractRows = _ocur.CreateObject(Of ContractRows)()
                            _ContractRows.Contracts = _Contracts
                            _ContractRows.DateExecution = CDate(_CRMEstate_Opportunity_Rent.DateStart.AddMonths(i).Year & "/" & _CRMEstate_Opportunity_Rent.DateStart.AddMonths(i).Month & "/" & _CRMEstate_Opportunity_Rent.DayOfExecution)
                            _ContractRows.ContractRowsType = eContractRowsType.eFix
                            _ContractRows.Controlling = _ImmovablesGroupsParameters.Controlling_Rent
                            _ContractRows.CostHolder = _ImmovablesGroupsParameters.CostHolder
                            _ContractRows.CostPlace = _ImmovablesGroupsParameters.CostPlace
                            _ContractRows.CurrencyName = _CRMEstate_Opportunity_Rent.CurrencyName
                            _ContractRows.ShortName = "Bértleti díj " & _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects.Name
                            _ContractRows.UniqueObjects = _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects
                            _ContractRows.JobNumberObjects = _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups.JobNumberObjects
                            _ContractRows.NettoPriceDEV = _CRMEstate_Opportunity_Rent.AmountNettoDEV
                            _ContractRows.NettoPriceHUF = _CRMEstate_Opportunity_Rent.AmountNettoHUF
                            _ContractRows.PaymentOption = _ImmovablesGroupsParameters.PaymentOption
                            _ContractRows.QTT = 1
                            _ContractRows.Units = _ocur.FindObject(Of Units)(CriteriaOperator.Parse("ShortName LIKE '%DB%'"))
                            _ContractRows.VAT = _ImmovablesGroupsParameters.VAT
                            _ContractRows.UniqueObjects = _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects
                            _CRMEstate_Opportunity_Rent.Contracts = _Contracts
                        Next
                    Next
                Next
            Next

            _ocur.CommitChanges()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aCreateContractSold_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateContractSold.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CRMEstate_Opportunity As CRMEstate_Opportunity = TryCast(View.SelectedObjects(0), CRMEstate_Opportunity)

            Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ContractTemplate))

            _CS.BeginUpdateCriteria()
            _CS.Criteria.Add("Criteria", CriteriaOperator.Parse("IsImmovables='True'"))
            _CS.EndUpdateCriteria()

            If _CS.List.Count = 0 Then Throw New Exception("*Nincs egyetlen olyan szerződés sablon sem, amely használható lenne!")

            e.View = Application.CreateListView("ContractTemplate_ListView", _CS, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aCreateContractSold_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateContractSold.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _CRMEstate_Opportunity As CRMEstate_Opportunity = TryCast(View.SelectedObjects(0), CRMEstate_Opportunity)
            Dim _Contracts As Contracts
            Dim _ContractRows As ContractRows
            Dim _CRMEstate_Opportunity_Immovables_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("CRMEstate_Opportunity_Immovables")
            Dim _ContractTemplateSold As ContractTemplate = _ocur.GetObjectByKey(Of ContractTemplate)(TryCast(e.PopupWindow.View.SelectedObjects(0), ContractTemplate).Oid)

            'Ellenőrzések 
            For Each _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables In _CRMEstate_Opportunity_Immovables_ListEditor.ListView.SelectedObjects
                'Szándéknyilatkozat
                If _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_AdvanceMoney.Count = 0 Then _
                    Throw New Exception("*Nincs kitötltött szándéknyilatkozat a(z) (" & _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects.Name & ") ingatlanra! Kérjük ellenőrizze!")

                ' -- Tulajdoni hányadok ellenőrzése
                Dim _RigthQuota As Int32 = 0
                For Each _CRMEstate_Opportunity_Customers As CRMEstate_Opportunity_Customers In _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_Customers
                    _RigthQuota += _CRMEstate_Opportunity_Customers.RightQuota
                Next
                If Not _RigthQuota = 100 Then Throw New Exception("*A tulajdoni hányadok nem adják ki a 100%-ot kérjük ellenőrizze!")

                ' -- Fizetési ütemezés ellenőrzése

                Dim _ForeCastSUM As Int32 = 0
                Dim _ForeCastSUM_Calc As Int32 = 0
                For Each _CRMEstate_Opportunity_Payment_Forecast As CRMEstate_Opportunity_Payment_Forecast In _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_Payment_Forecast
                    _ForeCastSUM += _CRMEstate_Opportunity_Payment_Forecast.SumPriceNettoHUF
                Next

                If TryCast(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables, Apartments) IsNot Nothing Then
                    _ForeCastSUM_Calc = Math.Round((TryCast(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables, Apartments).AreaNettoForSale * _
                                                      _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.AreaListPriceNettoHUF), 0, MidpointRounding.AwayFromZero)

                    If _ForeCastSUM < _ForeCastSUM_Calc - 100 Or _ForeCastSUM > _ForeCastSUM_Calc + 100 Then _
                        Throw New Exception("*A fizetési ütemezés nem fedi a teljes vételárat! Kérjük ellenőrizze!")
                End If

                If TryCast(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables, Offices) IsNot Nothing Then
                    _ForeCastSUM_Calc = Math.Round((TryCast(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables, Offices).AreaNettoForSale * _
                                                      _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.AreaListPriceNettoHUF), 0, MidpointRounding.AwayFromZero)

                    If _ForeCastSUM < _ForeCastSUM_Calc - 100 Or _ForeCastSUM > _ForeCastSUM_Calc + 100 Then _
                        Throw New Exception("*A fizetési ütemezés nem fedi a teljes vételárat! Kérjük ellenőrizze!")
                End If

                If TryCast(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables, Garages) IsNot Nothing Then
                    If Not _ForeCastSUM = TryCast(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables, Garages).PriceNettoHUF Then _
                        Throw New Exception("*A fizetési ütemezés nem fedi a teljes vételárat! Kérjük ellenőrizze!")
                End If

                If TryCast(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables, Container) IsNot Nothing Then
                    If Not _ForeCastSUM = TryCast(_CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables, Container).PriceNettoHUF Then _
                        Throw New Exception("*A fizetési ütemezés nem fedi a teljes vételárat! Kérjük ellenőrizze!")
                End If
            Next

            'Szerződések elkészítése
            For Each _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables In _CRMEstate_Opportunity_Immovables_ListEditor.ListView.SelectedObjects
                For Each _CRMEstate_Opportunity_Payment_Forecast As CRMEstate_Opportunity_Payment_Forecast In _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_Payment_Forecast

                    Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _ocur.FindObject(Of ImmovablesGroupsParameters)(CriteriaOperator.Parse("ImmovablesGroups=?", _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups))

                    If _ImmovablesGroupsParameters Is Nothing Then Throw New Exception("*Nincs beállítva paraméter a(z) " + _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups.ShortName + _
                                                                                        " ingatlancsoporthoz!" + vbNewLine + "Kérem ellenőrizze!")
                    For Each _CRMEstate_Opportunity_Customers As CRMEstate_Opportunity_Customers In _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_Customers

                        _Contracts = _ocur.FindObject(Of Contracts)(CriteriaOperator.Parse("CRMEstate_Opportunity_Immovables=? AND Customers=?", _CRMEstate_Opportunity_Immovables, _CRMEstate_Opportunity_Customers.Customers))

                        If _Contracts Is Nothing Then
                            _Contracts = _ocur.CreateObject(Of Contracts)()
                            _Contracts.ContractTemplate = _ContractTemplateSold
                            _Contracts.State = State.Ready
                            _Contracts.DocumentNumber = _ContractTemplateSold.NumberGenerator.GetNewNumber(_ContractTemplateSold.NumberGenerator)
                            _Contracts.CRMEstate_Opportunity_Immovables = _CRMEstate_Opportunity_Immovables
                            _Contracts.CustomersFrom = _CRMEstate_Opportunity_Immovables.CRMEstate_Opportunity_AdvanceMoney(0).CustomersFrom
                            _Contracts.Customers = _CRMEstate_Opportunity_Customers.Customers
                            _Contracts.ShortName = _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects.Name & " adásvételi szerződés"
                            _Contracts.CurrencyName = _CRMEstate_Opportunity_Payment_Forecast.CurrencyName
                            _Contracts.CurrencyName_Invoice = _CRMEstate_Opportunity_Payment_Forecast.CurrencyName
                            _Contracts.CRMEstate_Opportunity_Immovables = _CRMEstate_Opportunity_Immovables

                        End If

                        _ContractRows = _ocur.CreateObject(Of ContractRows)()
                        _ContractRows.Contracts = _Contracts
                        _ContractRows.JobNumberObjects = _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.ImmovableGroups.JobNumberObjects
                        _ContractRows.DateExecution = GetSQLTime(_ocur.Session)
                        _ContractRows.ContractRowsType = eContractRowsType.eAdvance
                        _ContractRows.Controlling = _ImmovablesGroupsParameters.Controlling_Final
                        _ContractRows.CostHolder = _ImmovablesGroupsParameters.CostHolder
                        _ContractRows.CostPlace = _ImmovablesGroupsParameters.CostPlace
                        _ContractRows.NettoPriceHUF = Math.Round((_CRMEstate_Opportunity_Payment_Forecast.SumPriceNettoHUF * (_CRMEstate_Opportunity_Customers.RightQuota / 100)), 0, MidpointRounding.AwayFromZero)
                        _ContractRows.VAT = _ImmovablesGroupsParameters.VAT
                        _ContractRows.UniqueObjects = _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects
                        _ContractRows.PaymentOption = _ImmovablesGroupsParameters.PaymentOption
                        _ContractRows.ShortName = _CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects.Name & " előleg"
                    Next
                Next
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
End Class
