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.Xpo.Metadata
Imports System.Drawing
<DefaultClassOptions()> _
<NavigationItem("RealEstateStocks")> _
Public Class Offices
    Inherits Immovables
    Private _Floor As Floors
    Private _Stairway As Stairways
    Private _RoomString As String
    Private _WindowDirection As eWindowDirection = eWindowDirection.NotSet
    Private _AreaPriceNettoHUF As Double
    Private _AreaPriceNettoDEV As Double
    Private _PriceBruttoHUF As Double
    Private _PriceBruttoDEV As Double
    Private _PriceNettoHUF As Double 'Nettó vételár (HUF)
    Private _PriceNettoDEV As Double
    Private _AreaNetto As Double = 0
    Private _AreaNettoUnder190 As Double = 0
    Private _AreaNettoForSale As Double = 0
    Private _AreaNettoForCostRecord As Double = 0 'Költség számító négyzetméter
    Private _Direction As eIDirection = eIDirection.NotSet
    Private _IsTerrace As Boolean
    Private _AreaTerrace As Double = 0
    Private _Note As String
    Private _UniqueTag As String
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Protected Overrides Sub OnSaving()
        _AreaNetto = 0
        _AreaNettoUnder190 = 0
        _PriceBruttoHUF = 0
        Dim _LL As LocalityLists
        For Each _LL In LocalityLists
            _AreaNetto += _LL.AreaNetto
            _AreaNettoUnder190 += _LL.AreaUnder190
            _PriceBruttoHUF += PriceBruttoHUF + (_LL.AreaNetto * _AreaPriceNettoHUF)
        Next
        If Session.IsNewObject(Me) Then
            If UniqueTag <> "*" Then Me.MakeUniqueObjects_Offices()
        End If
        MyBase.OnSaving()
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        Me.ImmovablesTypes = eImmovablesTypes.eOffice
    End Sub
    Protected Sub RecalculatePrice()
        PriceNettoHUF = Math.Round(AreaNettoForSale * AreaPriceNettoHUF, 0, MidpointRounding.AwayFromZero)
        PriceNettoDEV = Math.Round(AreaNettoForSale * AreaPriceNettoDEV, 0, MidpointRounding.AwayFromZero)
    End Sub
    Protected Sub RecalculatePriceBrutto()
        PriceBruttoHUF = Math.Round(PriceNettoHUF * 1.25, 0, MidpointRounding.AwayFromZero)
        PriceBruttoDEV = Math.Round(PriceNettoDEV * 1.25, 0, MidpointRounding.AwayFromZero)
    End Sub
    Protected Sub RecalculateAreaNettoForSale()
        AreaNettoForSale = Math.Round((AreaNetto + (AreaNettoUnder190 / 2) + (AreaTerrace / 2)), 2, MidpointRounding.AwayFromZero)
    End Sub
    <ImmediatePostData(True), DataSourceCriteria("ImmovablesGroups = '@This.ImmovableGroups'")>
    Property Floor() As Floors
        Get
            Return _Floor
        End Get
        Set(ByVal value As Floors)
            SetPropertyValue("Floor", _Floor, value)
        End Set
    End Property
    <ImmediatePostData(True), DataSourceCriteria("ImmovablesGroups = '@This.ImmovableGroups'")>
    Property Stairway() As Stairways
        Get
            Return _Stairway
        End Get
        Set(ByVal value As Stairways)
            SetPropertyValue("Stairway", _Stairway, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    <Association("Offices-LocalityLists", GetType(LocalityLists))> _
    Public ReadOnly Property LocalityLists() As XPCollection(Of LocalityLists)
        Get
            Return GetCollection(Of LocalityLists)("LocalityLists")
        End Get
    End Property
    Property WindowDirection() As eWindowDirection
        Get
            Return _WindowDirection
        End Get
        Set(ByVal value As eWindowDirection)
            SetPropertyValue("WindowDirection", _WindowDirection, value)
        End Set
    End Property
    Property AreaPriceNettoHUF() As Double
        Get
            Return _AreaPriceNettoHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AreaPriceNettoHUF", _AreaPriceNettoHUF, value)
        End Set
    End Property
    Property AreaPriceNettoDEV() As Double
        Get
            Return _AreaPriceNettoDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AreaPriceNettoDEV", _AreaPriceNettoDEV, value)
        End Set
    End Property
    Property Direction() As eIDirection
        Get
            Return _Direction
        End Get
        Set(ByVal value As eIDirection)
            SetPropertyValue("Direction", _Direction, value)
        End Set
    End Property
    Property PriceBruttoHUF() As Double
        Get
            Return _PriceBruttoHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("PriceBruttoHUF", _PriceBruttoHUF, value)
        End Set
    End Property
    Property PriceBruttoDEV() As Double
        Get
            Return _PriceBruttoDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("PriceBruttoDEV", _PriceBruttoDEV, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property PriceNettoDEV As Double
        Get
            Return _PriceNettoDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("PriceNettoDEV", _PriceNettoDEV, value)
            If Session.IsObjectsSaving = False And Session.IsObjectsLoading = False Then
                RecalculatePriceBrutto()
            End If
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property PriceNettoHUF As Double
        Get
            Return _PriceNettoHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("PriceNettoHUF", _PriceNettoHUF, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                Me.RecalculatePriceBrutto()
            End If
        End Set
    End Property
    Property AreaNetto() As Double
        Get
            Return _AreaNetto
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AreaNetto", _AreaNetto, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                RecalculateAreaNettoForSale()
            End If
        End Set
    End Property
    Property AreaNettoUnder190() As Double
        Get
            Return _AreaNettoUnder190
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AreaNettoUnder190", _AreaNettoUnder190, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                RecalculateAreaNettoForSale()
            End If
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property AreaNettoForSale As Double
        Get
            Return _AreaNettoForSale
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AreaNettoForSale", _AreaNettoForSale, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                MyBase.RecalculatePriceRent()
                RecalculatePrice()
            End If
        End Set
    End Property
    Property AreaNettoForCostRecord As Double
        Get
            Return _AreaNettoForCostRecord
        End Get
        Set(value As Double)
            SetPropertyValue("AreaNettoForCostRecord", _AreaNettoForCostRecord, value)
        End Set
    End Property
    Property IsTerrace() As Boolean
        Get
            Return _IsTerrace
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsTerrace", _IsTerrace, value)
        End Set
    End Property
    Property AreaTerrace As Double
        Get
            Return _AreaTerrace
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AreaTerrace", _AreaTerrace, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                RecalculateAreaNettoForSale()
            End If
        End Set
    End Property
    <Size(-1)> _
    Property Note() As String
        Get
            Return _Note
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Note", _Note, value)
        End Set
    End Property
    <RuleRequiredField("Offices-UniqueTag", DefaultContexts.Save)> _
    Property UniqueTag() As String
        Get
            Return _UniqueTag
        End Get
        Set(ByVal value As String)
            SetPropertyValue("UniqueTag", _UniqueTag, value)
        End Set
    End Property
    Public Sub MakeUniqueObjects_Offices()
        Dim _Name_ImmovablesGroups As String : Dim _UniqueObjects_ImmovablesGroups As UniqueObjects
        Dim _Name_ImmovablesGroups_X As String : Dim _UniqueObjects_ImmovablesGroups_X As UniqueObjects
        Dim _Name_Stairway As String : Dim _UniqueObjects_Stairway As UniqueObjects
        Dim _Name_Stairway_X As String : Dim _UniqueObjects_Stairway_X As UniqueObjects
        Dim _Name_Floor As String : Dim _UniqueObjects_Floor As UniqueObjects
        Dim _Name_Floor_X As String : Dim _UniqueObjects_Floor_X As UniqueObjects
        Dim _Name_Offices As String : Dim _UniqueObjects_Offices As UniqueObjects


        _Name_ImmovablesGroups = Me.ImmovableGroups.UniqueTag
        _Name_ImmovablesGroups_X = Me.ImmovableGroups.UniqueTag & "-X"

        _UniqueObjects_ImmovablesGroups = Session.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _Name_ImmovablesGroups))
        If _UniqueObjects_ImmovablesGroups Is Nothing Then
            _UniqueObjects_ImmovablesGroups = New UniqueObjects(Session)
            _UniqueObjects_ImmovablesGroups.Name = _Name_ImmovablesGroups
            _UniqueObjects_ImmovablesGroups.Save()
        End If
        _UniqueObjects_ImmovablesGroups_X = Session.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _Name_ImmovablesGroups_X))
        If _UniqueObjects_ImmovablesGroups_X Is Nothing Then
            _UniqueObjects_ImmovablesGroups_X = New UniqueObjects(Session)
            _UniqueObjects_ImmovablesGroups_X.Name = _Name_ImmovablesGroups_X
            _UniqueObjects_ImmovablesGroups_X.Parent = _UniqueObjects_ImmovablesGroups
            _UniqueObjects_ImmovablesGroups_X.Save()
        End If

        _Name_Stairway = _Name_ImmovablesGroups & "-" & _Stairway.UniqueTag
        _Name_Stairway_X = _Name_ImmovablesGroups & "-" & Stairway.UniqueTag & "-X"

        _UniqueObjects_Stairway = Session.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _Name_Stairway))
        If _UniqueObjects_Stairway Is Nothing Then
            _UniqueObjects_Stairway = New UniqueObjects(Session)
            _UniqueObjects_Stairway.Name = _Name_Stairway
            _UniqueObjects_Stairway.Parent = _UniqueObjects_ImmovablesGroups
            _UniqueObjects_Stairway.Save()
        End If
        _UniqueObjects_Stairway_X = Session.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _Name_Stairway_X))
        If _UniqueObjects_Stairway_X Is Nothing Then
            _UniqueObjects_Stairway_X = New UniqueObjects(Session)
            _UniqueObjects_Stairway_X.Name = _Name_Stairway_X
            _UniqueObjects_Stairway_X.Parent = _UniqueObjects_Stairway
            _UniqueObjects_Stairway_X.Save()
        End If

        _Name_Floor = _Name_ImmovablesGroups & "-" & _Stairway.UniqueTag & "-" & Me.Floor.UniqueTag
        _Name_Floor_X = _Name_ImmovablesGroups & "-" & _Stairway.UniqueTag & "-" & Me.Floor.UniqueTag & "-X"

        _UniqueObjects_Floor = Session.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _Name_Floor))
        If _UniqueObjects_Floor Is Nothing Then
            _UniqueObjects_Floor = New UniqueObjects(Session)
            _UniqueObjects_Floor.Name = _Name_Floor
            _UniqueObjects_Floor.Parent = _UniqueObjects_Stairway
            _UniqueObjects_Floor.Save()
        End If
        _UniqueObjects_Floor_X = Session.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _Name_Floor_X))
        If _UniqueObjects_Floor_X Is Nothing Then
            _UniqueObjects_Floor_X = New UniqueObjects(Session)
            _UniqueObjects_Floor_X.Name = _Name_Floor_X
            _UniqueObjects_Floor_X.Parent = _UniqueObjects_Floor
            _UniqueObjects_Floor_X.Save()
        End If

        _Name_Offices = Me.ImmovableGroups.UniqueTag & "-" & Me.Stairway.UniqueTag & "-" & Me.Floor.UniqueTag & _UniqueTag

        _UniqueObjects_Offices = Session.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _Name_Offices))
        If _UniqueObjects_Offices Is Nothing Then
            _UniqueObjects_Offices = New UniqueObjects(Session)
            _UniqueObjects_Offices.Name = _Name_Offices
            _UniqueObjects_Offices.Parent = _UniqueObjects_Floor
            _UniqueObjects_Offices.Save()
            Me.UniqueObjects = _UniqueObjects_Offices
        End If
    End Sub

    '-----NonPersistes-----
    <NonPersistent(), VisibleInListView(False), VisibleInLookupListView(False)> _
    Property OfferListPriceNettoHUF As Double = 0
    <NonPersistent(), VisibleInListView(False), VisibleInLookupListView(False)> _
    Property OfferListPriceNettoHUFRent As Double = 0
    <NonPersistent(), VisibleInListView(False), VisibleInLookupListView(False)> _
    Property OfferListPriceBruttoHUF As Double = 0
    <NonPersistent(), VisibleInListView(False), VisibleInLookupListView(False)> _
    Property OfferListPriceBruttoHUFRent As Double = 0
End Class ' Irodák, üzletek
