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 System.Drawing
Imports DevExpress.ExpressApp.Utils

<DefaultClassOptions()> _
<NavigationItem(False), CreatableItem(False)> _
<DeferredDeletion(False)> _
Public Class PCIGroup
    Inherits BaseObject

    Private _CustomersFrom As CustomersFrom
    Private _Customers As Customers
    Private _PartnerType As ePartnerType
    Private _ConnectInfo As String
    Private _DocumentNumber As String = "" 'Számla eredeti bizonylatszáma
    Private _RegistryNumber As String = ""
    Private _CurrencyName As CurrencyName
    Private _BallanceHUF As Double
    Private _BallanceDEV As Double

    Private _BallanceHUF_P As Double
    Private _BallanceDEV_P As Double
    Private _BallanceHUF_R As Double
    Private _BallanceDEV_R As Double
    Private _BallanceHUF_B As Double
    Private _BallanceDEV_B As Double

    Private _DateCreated_Account As Date
    Private _DatePayment_Account As Date
    Private _DateExecution_Account As Date
    Private _CurrencyName_Account As CurrencyName
    Private _Paymode_Account As ePayMode
    Private _AmountHUF_Account As Double
    Private _AmountDEV_Account As Double

    Private _BallanceState As Long
    Private _BallanceRateDiff As Long
    Private _RateDiffState As Long = 0 'Árfolyam státusz

    Private _ChartOfAccounts_Customers As ChartOfAccounts 'Partnerhez tartozó Fk. számla száma
    Private _DateLastConnected As Date 'Utolsó p.ü. teljesítés dátuma
    Private _Description As String 'Egyéb megjegyzésrivate 

    Private _InvoiceHeader As InvoiceHeader
    Private _RegistryHeader As RegistryHeader
    Private _GLAccounts As GLAccounts


    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()
        ' Place here your initialization code.
        BallanceDEV = 0
        BallanceDEV_B = 0
        BallanceDEV_P = 0
        BallanceDEV_R = 0
        BallanceHUF = 0
        BallanceHUF_B = 0
        BallanceHUF_P = 0
        BallanceHUF_R = 0

        BallanceState = 0
        BallanceRateDiff = -1
    End Sub

    Property CustomersFrom As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    Property Customers As Customers
        Get
            Return _Customers
        End Get
        Set(value As Customers)
            SetPropertyValue("Customers", _Customers, value)
        End Set
    End Property
    Property PartnerType As ePartnerType
        Get
            Return _PartnerType
        End Get
        Set(value As ePartnerType)
            SetPropertyValue("_PartnerType", _PartnerType, value)
        End Set
    End Property
    Property ConnectInfo As String
        Get
            Return _ConnectInfo
        End Get
        Set(value As String)
            SetPropertyValue("ConnectInfo", _ConnectInfo, value)
        End Set
    End Property
    Property DocumentNumber As String
        Get
            Return _DocumentNumber
        End Get
        Set(value As String)
            SetPropertyValue("DocumentNumber", _DocumentNumber, value)
        End Set
    End Property
    Property RegistryNumber As String
        Get
            Return _RegistryNumber
        End Get
        Set(value As String)
            SetPropertyValue("RegistryNumber", _RegistryNumber, value)
        End Set
    End Property
    Property CurrencyName As CurrencyName
        Get
            Return _CurrencyName
        End Get
        Set(value As CurrencyName)
            SetPropertyValue("CurrencyName", _CurrencyName, value)
        End Set
    End Property
    Property BallanceHUF As Double
        Get
            Return _BallanceHUF
        End Get
        Set(value As Double)
            SetPropertyValue("BallanceHUF", _BallanceHUF, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                Select Case PartnerType
                    Case ePartnerType.ePayabels
                        BallanceHUF_P = value * -1
                    Case ePartnerType.eReceivables
                        BallanceHUF_R = value
                End Select
                BallanceHUF_B = BallanceHUF_R - BallanceHUF_P
                BallanceState = 0
                RateDiffState = 0
                BallanceRateDiff = 0
                If CurrencyName_Account IsNot Nothing Then
                    If _CurrencyName_Account.ShortName = "HUF" Then
                        BallanceRateDiff = -1
                        If Math.Round(value, 0, MidpointRounding.AwayFromZero) = 0 Then BallanceState = 1
                    Else
                        If Math.Round(BallanceDEV, 2, MidpointRounding.AwayFromZero) = 0 Then BallanceState = 1
                        If Math.Round(value, 0, MidpointRounding.AwayFromZero) <> 0 And Math.Round(BallanceDEV, 2, MidpointRounding.AwayFromZero) = 0 Then
                            BallanceRateDiff = 1
                            If PartnerType = ePartnerType.eReceivables Then
                                If Math.Round(value, 0, MidpointRounding.AwayFromZero) > 0 Then
                                    RateDiffState = -1
                                Else
                                    RateDiffState = 1
                                End If
                            ElseIf PartnerType = ePartnerType.ePayabels Then
                                If Math.Round(value, 0, MidpointRounding.AwayFromZero) > 0 Then
                                    RateDiffState = 1
                                Else
                                    RateDiffState = -1
                                End If
                            End If
                        End If
                    End If

                Else
                    If CurrencyName IsNot Nothing Then
                        If CurrencyName.ShortName = "HUF" Then
                            If Math.Round(value, 0, MidpointRounding.AwayFromZero) = 0 Then BallanceState = 1
                        Else
                            If Math.Round(BallanceDEV, 2, MidpointRounding.AwayFromZero) = 0 Then BallanceState = 1
                        End If
                    Else
                        If Math.Round(value, 0, MidpointRounding.AwayFromZero) = 0 Then BallanceState = 1
                    End If
                End If
            End If
        End Set
    End Property
    Property BallanceDEV As Double
        Get
            Return _BallanceDEV
        End Get
        Set(value As Double)
            SetPropertyValue("BallanceDEV", _BallanceDEV, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                Select Case PartnerType
                    Case ePartnerType.ePayabels
                        BallanceDEV_P = value * -1
                    Case ePartnerType.eReceivables
                        BallanceDEV_R = value
                End Select
                BallanceDEV_B = BallanceDEV_R - BallanceDEV_P
            End If
        End Set
    End Property

    Property BallanceHUF_P As Double
        Get
            Return _BallanceHUF_P
        End Get
        Set(value As Double)
            SetPropertyValue("BallanceHUF_P", _BallanceHUF_P, value)
        End Set
    End Property
    Property BallanceDEV_P As Double
        Get
            Return _BallanceDEV_P
        End Get
        Set(value As Double)
            SetPropertyValue("BallanceDEV_P", _BallanceDEV_P, value)
        End Set
    End Property
    Property BallanceHUF_R As Double
        Get
            Return _BallanceHUF_R
        End Get
        Set(value As Double)
            SetPropertyValue("BallanceHUF_R", _BallanceHUF_R, value)
        End Set
    End Property
    Property BallanceDEV_R As Double
        Get
            Return _BallanceDEV_R
        End Get
        Set(value As Double)
            SetPropertyValue("BallanceDEV_R", _BallanceDEV_R, value)
        End Set
    End Property
    Property BallanceHUF_B As Double
        Get
            Return _BallanceHUF_B
        End Get
        Set(value As Double)
            SetPropertyValue("BallanceHUF_B", _BallanceHUF_B, value)
        End Set
    End Property
    Property BallanceDEV_B As Double
        Get
            Return _BallanceDEV_B
        End Get
        Set(value As Double)
            SetPropertyValue("BallanceDEV_B", _BallanceDEV_B, value)
        End Set
    End Property

    Property DateCreated_Account As Date
        Get
            Return _DateCreated_Account
        End Get
        Set(value As Date)
            SetPropertyValue("DateCreated_Account", _DateCreated_Account, value)
        End Set
    End Property
    Property DatePayment_Account As Date
        Get
            Return _DatePayment_Account
        End Get
        Set(value As Date)
            SetPropertyValue("DatePayment_Account", _DatePayment_Account, value)
        End Set
    End Property
    Property DateExecution_Account As Date
        Get
            Return _DateExecution_Account
        End Get
        Set(value As Date)
            SetPropertyValue("DateExecution_Account", _DateExecution_Account, value)
        End Set
    End Property
    Property CurrencyName_Account As CurrencyName
        Get
            Return _CurrencyName_Account
        End Get
        Set(value As CurrencyName)
            SetPropertyValue("CurrencyName_Account", _CurrencyName_Account, value)
        End Set
    End Property
    Property Paymode_Account As ePayMode
        Get
            Return _Paymode_Account
        End Get
        Set(value As ePayMode)
            SetPropertyValue("Paymode_Account", _Paymode_Account, value)
        End Set
    End Property
    Property AmountHUF_Account As Double
        Get
            Return _AmountHUF_Account
        End Get
        Set(value As Double)
            SetPropertyValue("AmountHUF_Account", _AmountHUF_Account, value)
        End Set
    End Property
    Property AmountDEV_Account As Double
        Get
            Return _AmountDEV_Account
        End Get
        Set(value As Double)
            SetPropertyValue("AmountDEV_Account", _AmountDEV_Account, value)
        End Set
    End Property

    Property BallanceState As Long
        Get
            Return _BallanceState
        End Get
        Set(value As Long)
            SetPropertyValue("BallanceState", _BallanceState, value)
        End Set
    End Property
    Property BallanceRateDiff As Long
        Get
            Return _BallanceRateDiff
        End Get
        Set(value As Long)
            SetPropertyValue("BallanceRateDiff", _BallanceRateDiff, value)
        End Set
    End Property
    Property RateDiffState As Long
        Get
            Return _RateDiffState
        End Get
        Set(value As Long)
            SetPropertyValue("RateDiffState", _RateDiffState, value)
        End Set
    End Property

    Property DateLastConnected As Date
        Get
            Return _DateLastConnected
        End Get
        Set(value As Date)
            SetPropertyValue("DateLastConnected", _DateLastConnected, value)
        End Set
    End Property
    <VisibleInDetailView(False)> _
    ReadOnly Property BallanceRateDiffImage As Image
        Get
            If PartnerType = ePartnerType.eReceivables Then
                If RateDiffState = -1 Then 'Árfolyam veszteség
                    Return ImageLoader.Instance.GetImageInfo("nav_down_red").Image
                Else 'Árfolyam nyereség
                    Return ImageLoader.Instance.GetImageInfo("nav_up_green").Image
                End If
            ElseIf PartnerType = ePartnerType.ePayabels Then
                If RateDiffState = 1 Then 'Árfolyam nyereség
                    Return ImageLoader.Instance.GetImageInfo("nav_up_green").Image
                Else 'Árfolyam veszteség
                    Return ImageLoader.Instance.GetImageInfo("nav_down_red").Image
                End If
            Else
                Return ImageLoader.Instance.GetImageInfo("bullet_ball_glass_blue").Image
            End If
        End Get
    End Property
    <Size(-1)> _
    Property Description As String
        Get
            Return _Description
        End Get
        Set(value As String)
            SetPropertyValue("Description", _Description, value)
        End Set
    End Property

    Property InvoiceHeader As InvoiceHeader
        Get
            Return _InvoiceHeader
        End Get
        Set(value As InvoiceHeader)
            SetPropertyValue("InvoiceHeader", _InvoiceHeader, value)
        End Set
    End Property
    Property RegistryHeader As RegistryHeader
        Get
            Return _RegistryHeader
        End Get
        Set(value As RegistryHeader)
            SetPropertyValue("RegistryHeader", _RegistryHeader, value)
        End Set
    End Property
    Property GLAccounts As GLAccounts
        Get
            Return _GLAccounts
        End Get
        Set(value As GLAccounts)
            SetPropertyValue("GLAccounts", _GLAccounts, value)
        End Set
    End Property

    ReadOnly Property DelayedDayLastConnected As Long
        Get
            Dim _Now As DateTime = Now
            If Me.DateLastConnected <> #12:00:00 AM# Then
                If InvoiceHeader IsNot Nothing Then
                    Return DateDiff(DateInterval.Day, Me.InvoiceHeader.DatePayment.Date, Me.DateLastConnected.Date)
                Else
                    Return 0
                End If
            Else
                If InvoiceHeader IsNot Nothing Then
                    Return DateDiff(DateInterval.Day, Me.InvoiceHeader.DatePayment.Date, Now.Date)
                Else
                    Return 0
                End If
                Return 0
            End If
        End Get
    End Property
    'Property ChartOfAccounts_Customers As ChartOfAccounts
    '    Get
    '        Return _ChartOfAccounts_Customers
    '    End Get
    '    Set(value As ChartOfAccounts)
    '        SetPropertyValue("ChartOfAccounts_Customers", _ChartOfAccounts_Customers, value)
    '    End Set
    'End Property
End Class
