Imports System
Imports System.ComponentModel

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation

Imports DevExpress.ExpressApp.ConditionalAppearance

'-- Paraméterek, enumok
<DefaultClassOptions()> _
<NavigationItem(False), CreatableItem(False)> _
<Appearance("CRMEstate_Opportunity_Rent Hide HUF If currencyname != HUF", AppearanceItemType:="LayoutItem", Criteria:="CurrencyName.ShortName != 'HUF'", Context:="Any", TargetItems:="AmountNettoHUF", Visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("CRMEstate_Opportunity_Rent Hide DEV If currencyname = HUF", AppearanceItemType:="LayoutItem", Criteria:="CurrencyName.ShortName = 'HUF'", Context:="Any", TargetItems:="AmountNettoDEV", Visibility:=Editors.ViewItemVisibility.Hide)> _
Public Class CRMEstate_Opportunity_Rent
    Inherits BaseObject
    Private _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables
    Private _CustomersFrom As CustomersFrom
    Private _Contracts As Contracts 'Bérleti szerzõdés
    Private _DateCreated As Date 'Szerzõdés kelte
    Private _DateStart As Date 'Szerzõdés -tól
    Private _DateStop As Date 'Szerzõdés -ig
    Private _DayOfExecution As eDayOfMonth 'Forduló nap
    Private _AmountNettoHUF As Double 'Havi bérleti dij
    Private _AmountNettoDEV As Double 'Havi bérleti dij devizában
    Private _CurrencyName As CurrencyName 'Deviza
    Private _Description As String 'Egyéb megjegyzések
    'Bérleti szerződés paraméterek
    Private _Controlling As Controlling
    Private _PaymentOption As PaymentOption '--Fizetési opció
    Private _CostHolder As CostHolder ' -- Költségviselõ
    Private _CostPlace As CostPlace '-- Költséghely
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
        ' This constructor is used when an object is loaded from a persistent storage.
        ' Do not place any code here or place it only when the IsLoading property is false:
        ' if (!IsLoading){
        '   It is now OK to place your initialization code here.
        ' }
        ' or as an alternative, move your initialization code into the AfterConstruction method.
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        Me.DateCreated = GetSQLTime(Session)
        Me.CurrencyName = Session.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        CheckImmovableForRent(Me)
    End Sub
    Protected Overrides Sub OnDeleting()
        MyBase.OnDeleting()

    End Sub
    <Association("CRMEstate_Opportunity_Rent-CRMEstate_Opportunity_Immovables", GetType(CRMEstate_Opportunity_Immovables))> _
    Property CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables
        Get
            Return _CRMEstate_Opportunity_Immovables
        End Get
        Set(value As CRMEstate_Opportunity_Immovables)
            SetPropertyValue("CRMEstate_Opportunity_Immovables", _CRMEstate_Opportunity_Immovables, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving And value IsNot Nothing Then
                If TryCast(value.CRMEstate_Leads_Immovables.Immovables.CustomersOwner, CustomersFrom) IsNot Nothing Then
                    Me.CustomersFrom = value.CRMEstate_Leads_Immovables.Immovables.CustomersOwner
                End If
                Me.AmountNettoHUF = value.CRMEstate_Leads_Immovables.Immovables.PriceNettoHUFRent
                Me.AmountNettoDEV = value.CRMEstate_Leads_Immovables.Immovables.PriceNettoDEVRent
            End If
        End Set
    End Property
    Property Contracts As Contracts
        Get
            Return _Contracts
        End Get
        Set(value As Contracts)
            SetPropertyValue("Contracts", _Contracts, value)
        End Set
    End Property
    <RuleRequiredField("CRMEstate_Opportunity_Rent.CustomersFrom", DefaultContexts.Save)> _
    Property CustomersFrom As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    <RuleRequiredField("CRMEstate_Opportunity_Rent.DateCreated", DefaultContexts.Save)> _
    Property DateCreated As Date
        Get
            Return _DateCreated
        End Get
        Set(value As Date)
            SetPropertyValue("DateCreated", _DateCreated, value)
        End Set
    End Property
    <RuleRequiredField("CRMEstate_Opportunity_Rent.DateStart", DefaultContexts.Save)> _
    Property DateStart As Date
        Get
            Return _DateStart
        End Get
        Set(value As Date)
            SetPropertyValue("DateStart", _DateStart, value)
        End Set
    End Property
    <RuleRequiredField("CRMEstate_Opportunity_Rent.DateStop", DefaultContexts.Save)> _
    Property DateStop As Date
        Get
            Return _DateStop
        End Get
        Set(value As Date)
            SetPropertyValue("DateStop", _DateStop, value)
        End Set
    End Property
    Property DayOfExecution As eDayOfMonth
        Get
            Return _DayOfExecution
        End Get
        Set(value As eDayOfMonth)
            SetPropertyValue("DayOfExecution", _DayOfExecution, value)
        End Set
    End Property
    <RuleRange("CRMEstate_Opportunity_Rent.AmountNettoHUF", DefaultContexts.Save, 0.1, Int64.MaxValue, TargetCriteria:="CurrencyName.ShortName = 'HUF'")> _
    Property AmountNettoHUF As Double
        Get
            Return _AmountNettoHUF
        End Get
        Set(value As Double)
            SetPropertyValue("AmountNettoHUF", _AmountNettoHUF, value)
        End Set
    End Property
    <RuleRange("CRMEstate_Opportunity_Rent.AmountNettoDEV", DefaultContexts.Save, 0.1, Int64.MaxValue, TargetCriteria:="CurrencyName.ShortName != 'HUF'")> _
    Property AmountNettoDEV As Double
        Get
            Return _AmountNettoDEV
        End Get
        Set(value As Double)
            SetPropertyValue("AmountNettoDEV", _AmountNettoDEV, value)
        End Set
    End Property
    <ImmediatePostData()> _
    <RuleRequiredField("CRMEstate_Opportunity_Rent.CurrencyRate", DefaultContexts.Save)> _
    Property CurrencyName As CurrencyName
        Get
            Return _CurrencyName
        End Get
        Set(value As CurrencyName)
            SetPropertyValue("CurrencyName", _CurrencyName, value)
        End Set
    End Property
    <Size(4000)> _
    Property Description As String
        Get
            Return _Description
        End Get
        Set(value As String)
            SetPropertyValue("Description", _Description, value)
        End Set
    End Property
    <RuleRequiredField("CRMEstate_Opportunity_Rent-Controlling", DefaultContexts.Save)> _
    <DataSourceCriteria("IsParent=False")> _
    Property Controlling() As Controlling
        Get
            Return _Controlling
        End Get
        Set(ByVal value As Controlling)
            SetPropertyValue("Controlling", _Controlling, value)
        End Set
    End Property
    <RuleRequiredField("CRMEstate_Opportunity_Rent-PaymentOptions", DefaultContexts.Save, TargetCriteria:="Contracts.ContractTemplate.PartnerType = 0")> _
    Property PaymentOption() As PaymentOption
        Get
            Return _PaymentOption
        End Get
        Set(ByVal value As PaymentOption)
            SetPropertyValue("PaymentOption", _PaymentOption, value)
        End Set
    End Property
    <RuleRequiredField("CRMEstate_Opportunity_Rent-CostHolder", DefaultContexts.Save)> _
    Property CostHolder As CostHolder
        Get
            Return _CostHolder
        End Get
        Set(ByVal value As CostHolder)
            SetPropertyValue("CostHolder", _CostHolder, value)
        End Set
    End Property
    Property CostPlace As CostPlace
        Get
            Return _CostPlace
        End Get
        Set(ByVal value As CostPlace)
            SetPropertyValue("CostPlace", _CostPlace, value)
        End Set
    End Property
    Public Function CheckImmovableForRent(p_CRMEstate_Opportunity_Rent As CRMEstate_Opportunity_Rent) As Boolean

        'Ellenőrzi a bérlési foglalásokat

        Dim _IsReservationOK As Boolean = False
        Dim _ExistingReservation As CRMEstate_Opportunity_Rent = Session.FindObject(Of CRMEstate_Opportunity_Rent)(CriteriaOperator.Parse("CRMEstate_Opportunity_Immovables =? AND ((DateStart>=? AND DateStart<=?) OR (DateStop<=? AND DateStop>=?))", _
                            p_CRMEstate_Opportunity_Rent.CRMEstate_Opportunity_Immovables, p_CRMEstate_Opportunity_Rent.DateStart, p_CRMEstate_Opportunity_Rent.DateStop, p_CRMEstate_Opportunity_Rent.DateStop, p_CRMEstate_Opportunity_Rent.DateStart))

        If _ExistingReservation IsNot Nothing And _ExistingReservation IsNot p_CRMEstate_Opportunity_Rent Then
            Throw New Exception("*A kiválasztott ingatlanra (" & p_CRMEstate_Opportunity_Rent.CRMEstate_Opportunity_Immovables.CRMEstate_Leads_Immovables.Immovables.UniqueObjects.Name & ") vonatkozó foglalás ütközik egy foglalással a következő időintervallumra: " & _ExistingReservation.DateStart.ToString & " - " & _ExistingReservation.DateStop.ToString & "!")
        Else
            _IsReservationOK = True
        End If

        Return _IsReservationOK

    End Function
End Class



