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


<DefaultClassOptions()> _
<DefaultProperty("Products")> _
<NavigationItem("StorageStocks")> _
Public Class StorageComputed '-- Készletinfo
    Inherits BaseObject
    Private _Storage As Storage ' -- Raktár
    Private _Products As Products ' -- Termék
    Private _QTTFree As Double = 0 ' -- Szabad készlet
    Private _QTTReserved As Double = 0 ' -- Foglalás kitárolásra
    Private _QTTReservedIn As Double = 0 ' -- Betárolás alatt
    Private _QTTReservedOut As Double = 0 ' -- Kitárolás alatt
    Private _QTT_Minimum As Double 'Minimum mennyiség
    Private _QTT_Maximum As Double 'Feltöltés erre a mennyiségre
    Private _QTT_Remainder_In As Double 'Nem teljesített kimenő megrendelés a szállító felé
    Private _QTT_Remainder_To As Double 'Nem teljesített továbbrendelt tétel
    Private _QTT_FIFO As Double 'Összes betárolt mennyiség minus a kitárolt mennyiség StorageInRows.QTT-StorageInRows-QTT_Out ez mindig >= 0 (nulla)!
    Private _AveragePriceInHUF As Double 'Átlagár forintban
    Private _MimumumPriceOutHUF As Double 'Átlagárból számított minmimális eladási ár
    Private _MinimumPriceIsChecked As Boolean = False 'Az átlagárból számított adat rendben van-e
    Private _PricingMinimum As PricingMinimum 'Mimimum ár számító rutin

    Private _SumAveragePriceInHUFFree As Double 'Összesen átlagár szerinti készlet érték HUF
    Private _SumFIFOPriceInHUFFree As Double 'FIFO szerinti készletérték HUF

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Protected Overrides Sub OnSaved()
        MyBase.OnSaved()
        '// Mentés után
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        '// Mentéskor
        Me.RecalculateAveragePrice()
        Me.RecalculateMinimumPrice()
    End Sub
    Property Storage As Storage
        Get
            Return _Storage
        End Get
        Set(ByVal value As Storage)
            SetPropertyValue("Storage", _Storage, value)
        End Set
    End Property
    Property Products As Products
        Get
            Return _Products
        End Get
        Set(ByVal value As Products)
            SetPropertyValue("Products", _Products, value)
        End Set
    End Property
    Property QTTFree As Double
        Get
            Return _QTTFree
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTTFree", _QTTFree, Math.Round(value, 4, MidpointRounding.AwayFromZero))
        End Set
    End Property
    Property QTTReserved As Double
        Get
            Return _QTTReserved
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTTReserved", _QTTReserved, value)
        End Set
    End Property
    Property QTTReservedIn As Double
        Get
            Return _QTTReservedIn
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTTReservedIn", _QTTReservedIn, value)
        End Set
    End Property
    Property QTTReservedOut As Double
        Get
            Return _QTTReservedOut
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTTReservedOut", _QTTReservedOut, value)
        End Set
    End Property
    Property QTT_Minimum As Double
        Get
            Return _QTT_Minimum
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_Minimum", _QTT_Minimum, value)
        End Set
    End Property
    Property QTT_Maximum As Double
        Get
            Return _QTT_Maximum
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_Maximum", _QTT_Maximum, value)
        End Set
    End Property

    Property QTT_Remainder_In As Double
        Get
            Return _QTT_Remainder_In
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_Remainder_In", _QTT_Remainder_In, value)
        End Set
    End Property
    Property QTT_Remainder_To As Double
        Get
            Return _QTT_Remainder_To
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_Remainder_To", _QTT_Remainder_To, value)
        End Set
    End Property
    Property QTT_FIFO As Double
        Get
            Return _QTT_FIFO
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_FIFO", _QTT_FIFO, value)
        End Set
    End Property

    Property AveragePriceInHUF As Double
        Get
            Return _AveragePriceInHUF
        End Get
        Set(value As Double)
            SetPropertyValue("AveragePriceInHUF", _AveragePriceInHUF, value)
        End Set
    End Property
    Property MimumumPriceOutHUF As Double
        Get
            Return _MimumumPriceOutHUF
        End Get
        Set(value As Double)
            SetPropertyValue("MimumumPriceOutHUF", _MimumumPriceOutHUF, value)
        End Set
    End Property
    Property PricingMinimum As PricingMinimum
        Get
            Return _PricingMinimum
        End Get
        Set(value As PricingMinimum)
            SetPropertyValue("PricingMinimum", _PricingMinimum, value)
        End Set
    End Property
    Property MinimumPriceIsChecked As Boolean
        Get
            Return _MinimumPriceIsChecked
        End Get
        Set(value As Boolean)
            SetPropertyValue("MinimumPriceIsChecked", _MinimumPriceIsChecked, value)
        End Set
    End Property

    Property SumAveragePriceInHUFFree As Double
        Get
            Return _SumAveragePriceInHUFFree
        End Get
        Set(value As Double)
            SetPropertyValue("SumAveragePriceInHUFFree", _SumAveragePriceInHUFFree, value)
        End Set
    End Property
    Property SumFIFOPriceInHUFFree As Double
        Get
            Return _SumFIFOPriceInHUFFree
        End Get
        Set(value As Double)
            SetPropertyValue("SumFIFOPriceInHUFFree", _SumFIFOPriceInHUFFree, value)
        End Set
    End Property
    '//XPCollections
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property StorageInRows As XPCollection(Of StorageInRows)
        Get
            Return New XPCollection(Of StorageInRows)(Session, CriteriaOperator.Parse("StorageInHeader.Storage=? and Products=?", Me.Storage, Me.Products))
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property StorageMoveEvents As XPCollection(Of StorageMoveEvents)
        Get
            Return New XPCollection(Of StorageMoveEvents)(Session, CriteriaOperator.Parse("IsNull(StorageInRows.GCRecord)=True and IsNull(StorageInRows.StorageInHeader.GCRecord)=True and StorageInRows.Storage=? and StorageInRows.Products=?", Me.Storage, Me.Products))
        End Get
    End Property

    Public Sub RecalculateAveragePrice()
        If Me.Storage Is Nothing Then Exit Sub
        If Me.Products Is Nothing Then Exit Sub

        Dim _StorageDateClearingPrice_Collection As New XPCollection(Of StorageDateClearingPrice)(Session, CriteriaOperator.Parse("Products=?", Me.Products))
        Dim _StorageDateClearingPrice As StorageDateClearingPrice

        AveragePriceInHUF = 0
        _StorageDateClearingPrice_Collection.Sorting.Add(New SortProperty("DateExecution", DB.SortingDirection.Descending))
        For Each _StorageDateClearingPrice In _StorageDateClearingPrice_Collection
            _AveragePriceInHUF = _StorageDateClearingPrice.AveragePriceHUF_After
            Exit For
        Next
        Me.SumAveragePriceInHUFFree = Me.QTTFree * Me.AveragePriceInHUF
    End Sub
    Public Sub RecalculateMinimumPrice()
        If Me.PricingMinimum IsNot Nothing Then
            MimumumPriceOutHUF = Math.Round(AveragePriceInHUF * (1 + (Me.PricingMinimum.PricePercent / 100)), 0, MidpointRounding.AwayFromZero)
        Else
            MimumumPriceOutHUF = 0
            Me.MinimumPriceIsChecked = False
        End If
    End Sub
    Sub RecalculateSumFIFOPriceInHUFFree()


    End Sub
    
End Class