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
Public Enum eReadState
    eSimpleRead = 0 'Mormál leolvasás
    eTransferOutRead = 1 'Átadási leolvasás
    eTransferInRead = 2 'Átvételi leolvasás
End Enum
<DefaultClassOptions()> _
<NavigationItem(False), CreatableItem(False)> _
<Appearance("Red if no price", AppearanceItemType:="ViewItem", criteria:="ReadedValueSumPrice<=0", FontColor:="Red", targetitems:="*")> _
Public Class ImmovablesCostRecordEvents
    Inherits BaseObject
    Private _ImmovablesCostRecord As ImmovablesCostRecord
    Private _ReadedValue As Double
    Private _ReadState As eReadState
    Private _ReadDate As Date  'Mikor történt a leolvasás

    Private _ReadIndex As Long 'Hányadik leolvasás ?
    Private _UserCreated As User
    Private _DateCreated As Date
    Private _ImmovablesCostRecordExcel As ImmovablesCostRecordExcel
    Private _ReadedValuePrev As Double 'Előző esemény értéke EVENT állása
    Private _ReadedValueDiff As Double
    Private _ReadedValuePrice As Double
    Private _ReadedValueSumPrice As Double

    Private _ContractRows As ContractRows
    Private _InvoicePeriod As String
    Private _IsReader As Boolean = False
    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
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If Session.IsNewObject(Me) Then
            If Not SecuritySystem.CurrentUser Is Nothing Then
                Dim currentUser As User = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
                UserCreated = currentUser
            End If
            DateCreated = GetSQLTime(Session)
        End If
    End Sub
    <Association("ImmovablesCostRecord-ImmovablesCostRecordEvents", GetType(ImmovablesCostRecord))> _
    <RuleRequiredField("ImmovablesCostRecordEvents-ImmovablesCostRecord", DefaultContexts.Save)> _
    Property ImmovablesCostRecord As ImmovablesCostRecord
        Get
            Return _ImmovablesCostRecord
        End Get
        Set(value As ImmovablesCostRecord)
            SetPropertyValue("ImmovablesCostRecord", _ImmovablesCostRecord, value)
        End Set
    End Property
    Property ReadedValue As Double
        Get
            Return _ReadedValue
        End Get
        Set(value As Double)
            SetPropertyValue("ReadedValue", _ReadedValue, value)
        End Set
    End Property
    Property ReadState As eReadState
        Get
            Return _ReadState
        End Get
        Set(value As eReadState)

        End Set
    End Property
    <ImmediatePostData()> _
    Property ReadDate As Date
        Get
            Return _ReadDate
        End Get
        Set(value As Date)
            SetPropertyValue("ReadDate", _ReadDate, value)
            OnChanged(ReadDateYear)
            OnChanged(ReadDateMonth)
            OnChanged(ReadDateQuarter)
        End Set
    End Property
    Property ReadDateYear As String
        Get
            Return AlignStr(DatePart(DateInterval.Year, ReadDate).ToString, eDirection.Left, "0", 4)
        End Get
        Set(value As String)
            SetPropertyValue("ReadDateYear", AlignStr(DatePart(DateInterval.Year, ReadDate).ToString, eDirection.Left, "0", 4), value)
        End Set
    End Property
    Property ReadDateQuarter As String
        Get
            Return AlignStr(DatePart(DateInterval.Quarter, ReadDate).ToString, eDirection.Left, "0", 2)
        End Get
        Set(value As String)
            SetPropertyValue("ReadDateQuarter", AlignStr(DatePart(DateInterval.Quarter, ReadDate).ToString, eDirection.Left, "0", 2), value)
        End Set
    End Property
    Property ReadDateMonth As String
        Get
            Return AlignStr(DatePart(DateInterval.Month, ReadDate).ToString, eDirection.Left, "0", 2)
        End Get
        Set(value As String)
            SetPropertyValue("ReadDateMonth", AlignStr(DatePart(DateInterval.Month, ReadDate).ToString, eDirection.Left, "0", 2), value)
        End Set
    End Property

    Property ReadIndex As Long
        Get
            Return _ReadIndex
        End Get
        Set(value As Long)
            SetPropertyValue("ReadIndex", _ReadIndex, value)
        End Set
    End Property
    <NonCloneable()> _
    Property UserCreated As User
        Get
            Return _UserCreated
        End Get
        Set(value As User)
            SetPropertyValue("UserCreated", _UserCreated, value)
        End Set
    End Property
    <NonCloneable()> _
    Property DateCreated As Date
        Get
            Return _DateCreated
        End Get
        Set(value As Date)
            SetPropertyValue("DateCreated", _DateCreated, value)
        End Set
    End Property
    Property ImmovablesCostRecordExcel As ImmovablesCostRecordExcel
        Get
            Return _ImmovablesCostRecordExcel
        End Get
        Set(value As ImmovablesCostRecordExcel)
            SetPropertyValue("ImmovablesCostRecordExcel", _ImmovablesCostRecordExcel, value)
        End Set
    End Property
    Property ReadedValuePrev As Double
        Get
            Return Math.Round(_ReadedValuePrev, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(value As Double)
            SetPropertyValue("_ReadedValuePrev", _ReadedValuePrev, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property ReadedValueDiff As Double
        Get
            Return Math.Round(_ReadedValueDiff, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(value As Double)
            SetPropertyValue("ReadedValueDiff", _ReadedValueDiff, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property ReadedValuePrice As Double
        Get
            Return Math.Round(_ReadedValuePrice, 4, MidpointRounding.AwayFromZero)
        End Get
        Set(value As Double)
            SetPropertyValue("ReadedValuePrice", _ReadedValuePrice, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property ReadedValueSumPrice As Double
        Get
            Return _ReadedValueSumPrice
        End Get
        Set(value As Double)
            SetPropertyValue("ReadedValueSumPrice", _ReadedValueSumPrice, value)
        End Set
    End Property

    <VisibleInDetailView(False), VisibleInLookupListView(False)> _
    Property ContractRows As ContractRows
        Get
            Return _ContractRows
        End Get
        Set(value As ContractRows)
            SetPropertyValue("ContractRows", _ContractRows, value)
        End Set
    End Property
    Property InvoicePeriod As String
        Get
            Return _InvoicePeriod
        End Get
        Set(value As String)
            SetPropertyValue("InvoicePeriod", _InvoicePeriod, value)
        End Set
    End Property
    Property IsReader As Boolean
        Get
            Return _IsReader
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsReader", _IsReader, value)
        End Set
    End Property

    ReadOnly Property Description_ContractRows As String
        Get
            If ImmovablesCostRecord IsNot Nothing Then
                If ImmovablesCostRecord.Description_ContactRows Is Nothing Then
                    Return ""
                Else
                    Return String.Format(ImmovablesCostRecord.Description_ContactRows, Math.Round(ReadedValuePrev, 4, MidpointRounding.AwayFromZero), _
                                                                                       Math.Round(ReadedValue, 4, MidpointRounding.AwayFromZero), _
                                                                                       Math.Round(ReadedValueDiff, 4, MidpointRounding.AwayFromZero))
                End If
            Else
                Return ""
            End If
        End Get
    End Property
End Class
