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

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

    Private _InvoiceFactoringHeader As InvoiceFactoringHeader
    Private _InvoiceHeader As InvoiceHeader
    Private _InterestValueHUF As Double ' Faktor kamat összege
    Private _CapitalValueHUF As Double ' "TŐKE" összege
    Private _RiskValueHUF As Double ' Kockázat összege

    Private _FeeValueHUF As Double ' Faktor díj összege
    Private _DiscountInterestPercent As Double 'Diszkont kamat !!!
    Private _InterestDay As Long
    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.
        InterestValueHUF = 0
        CapitalValueHUF = 0
        RiskValueHUF = 0
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()

        If IsDeleted = False Then
            RecalculateValues()
        End If

    End Sub

    <Association("InvoiceFactoringHeader-InvoiceFactoringRows", GetType(InvoiceFactoringHeader))> _
    Property InvoiceFactoringHeader As InvoiceFactoringHeader
        Get
            Return _InvoiceFactoringHeader
        End Get
        Set(value As InvoiceFactoringHeader)
            SetPropertyValue("InvoiceFactoringHeader", _InvoiceFactoringHeader, 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 InterestValueHUF As Double
        Get
            Return _InterestValueHUF
        End Get
        Set(value As Double)
            SetPropertyValue("InterestValueHUF", _InterestValueHUF, value)
        End Set
    End Property
    Property CapitalValueHUF As Double
        Get
            Return _CapitalValueHUF
        End Get
        Set(value As Double)
            SetPropertyValue("CapitalValueHUF", _CapitalValueHUF, value)
        End Set
    End Property
    Property RiskValueHUF As Double
        Get
            Return _RiskValueHUF
        End Get
        Set(value As Double)
            SetPropertyValue("RiskValueHUF", _RiskValueHUF, value)
        End Set
    End Property
    Property FeeValueHUF As Double
        Get
            Return _FeeValueHUF
        End Get
        Set(value As Double)
            SetPropertyValue("FeeValueHUF", _FeeValueHUF, value)
        End Set
    End Property

    Private Sub RecalculateValues()
        Dim _InterestPercent As Double = 0

        If InvoiceFactoringHeader.InterestPercent > 0 Then
            _InterestPercent = InvoiceFactoringHeader.InterestPercent
        Else
            _InterestPercent = InvoiceHeader.InvoiceFactoringParameter.InterestPercent
        End If

        If InvoiceHeader IsNot Nothing Then
            If InvoiceHeader.InvoiceFactoringParameter IsNot Nothing Then
                _InterestDay = DateDiff(DateInterval.Day, InvoiceFactoringHeader.DateExecution, _InvoiceHeader.DatePayment) + _InvoiceHeader.InvoiceFactoringParameter.GracePeriod
                _DiscountInterestPercent = (360 * _InterestPercent / 100) /
                                           (360 + (_InterestPercent / 100) * _InterestDay)

                FeeValueHUF = Math.Round(((_InterestDay \ 30) + 1) * (InvoiceHeader.InvoiceFactoringParameter.InterestPercentMonth / 100) * InvoiceHeader.TotalBruttoHUF, 0, MidpointRounding.AwayFromZero)
                RiskValueHUF = Math.Round(InvoiceHeader.TotalBruttoHUF * InvoiceHeader.InvoiceFactoringParameter.RiskPercent / 100, 0, MidpointRounding.AwayFromZero)
                InterestValueHUF = Math.Round(((InvoiceHeader.TotalBruttoHUF - RiskValueHUF) * _InterestDay * _DiscountInterestPercent) / 360, 0, MidpointRounding.AwayFromZero)
                CapitalValueHUF = Math.Round(InvoiceHeader.TotalBruttoHUF - InterestValueHUF - RiskValueHUF - FeeValueHUF, 0, MidpointRounding.AwayFromZero)


            End If
        End If
    End Sub
End Class
