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()> _
<NavigationItem(False), CreatableItem(False)> _
<DefaultProperty("Products")> _
Public Class StorageInRows
    Inherits BaseObject

    Private _RowIndex As Long ' -- Sor szám
    Private _StorageInHeader As StorageInHeader ' -- Betárolási biz. fejléc
    Private _Products As Products ' -- Termék
    Private _QTT As Double '-- Betárolt mennyiség
    Private _QTT_Will As Double = 0 '-- Betárolási szándék
    Private _QTT_Out As Double = 0 '-- Kitárolt mennyiség ebből a betárolásból (FIFO !)
    Private _QTT_Out_Will As Double = 0 '--Az össze kitárolási szándék, info az aktuális készletről
    Private _QTT_Overstored As Double = 0 'Raktárközi mozgással áttárolt mennyiség ebből a FIFO lábból
    Private _QTT_Overstor As Double = 0 'Mennyi lett a bevételezett mennyiségből raktárközi átadással kitárolva !
    Private _QTT_Overstored_Will As Double = 0 'Raktárközi mozgással áttárolt mennyiség ebből a FIFO lábból véglegesítés nélkül
    Private _QTT_Overstor_Will As Double = 0 'Mennyi lett a bevételezett mennyiségből raktárközi átadással kitárolva véglegesítés nélkül!
    Private _Active As Boolean = True ' -- Aktív e?
    Private _OnStorno As Boolean ' -- Storno e?
    Private _DeliveryNoteInRows As DeliveryNoteInRows ' Melyik bejövő szállítólevél soráról van (ha van)
    Private _PacketNumber As String 'Kötegszám
    Private _SerialNumber As String 'Sorozatszám
    Private _Weight As Double
    Private _WeightMode As eWeightMode 'Súly értelmezése, egységsúly vagy öszsúly 
    Private _Storage As Storage
    Private _StorageInRowsFrom As StorageInRows 'Raktárközi átadásnál a küldő betárolási sor
    Private _AveragePriceInHUF As Double 'Átlagár forintban

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()

    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If StorageInHeader IsNot Nothing Then
            Storage = StorageInHeader.Storage
        End If
        'Átlagár újraszámolása
        ' Ha nincs DNIR akkor a fejléc dátumig az összes raktár+termék í8ahol van DNIR db/DNIR ár
    End Sub
    Protected Overrides Sub OnSaved()
        MyBase.OnSaved()
        Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(Session, CriteriaOperator.Parse("Storage=? and Products=? AND IsNull(GCRecord)=True", Storage, Products))
        Dim _StorageInRows As StorageInRows
        Dim _StorageComputed As StorageComputed = Session.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? and Products=?", Storage, Products))
        If _StorageComputed IsNot Nothing Then
            _StorageComputed.QTTFree = 0
            _StorageComputed.QTTReserved = 0
            _StorageComputed.QTTReservedIn = 0
            _StorageComputed.QTTReservedOut = 0
        Else
            _StorageComputed = New StorageComputed(Session)
            _StorageComputed.Storage = StorageInHeader.Storage
            _StorageComputed.Products = Products
        End If
        For Each _StorageInRows In _StorageInRows_Collection
            _StorageComputed.QTTFree += (_StorageInRows.QTT - _StorageInRows.QTT_Out - _StorageInRows.QTT_Out_Will)
            _StorageComputed.QTTReservedIn += _StorageInRows.QTT_Will
            _StorageComputed.QTTReservedOut += _StorageInRows.QTT_Out_Will
        Next
        _StorageComputed.Save()
        Session.CommitTransaction()
    End Sub
    Public Sub AddRows(ByVal _p_StorageInHeader As StorageInHeader)
        ' --- Sor hozzáadása Betárolási bizonylathoz manuálisan, vagy Rendelésből automatikusan
        If _p_StorageInHeader IsNot Nothing And _p_StorageInHeader.row_Products IsNot Nothing And _p_StorageInHeader.Active = True Then

            Me.StorageInHeader = _p_StorageInHeader
            Me.Products = _p_StorageInHeader.row_Products
            Me.QTT_Will = _p_StorageInHeader.row_QTT_WILL
            Me.PacketNumber = _p_StorageInHeader.row_PacketNumber
            Me.SerialNumber = _p_StorageInHeader.row_SerialNumber
            Me.Weight = _p_StorageInHeader.row_Weight
            Me.WeightMode = _p_StorageInHeader.row_WeightMode
            If _p_StorageInHeader.StorageInRows.Count = 0 Then
                Me.RowIndex = 1
            Else
                Me.RowIndex = _p_StorageInHeader.StorageInRows.Count
            End If
        End If
    End Sub
    Public Sub RemoveRows()
        For Each _StorageInRows As StorageInRows In Me.StorageInHeader.StorageInRows
            If _StorageInRows.RowIndex > Me.RowIndex Then
                _StorageInRows.RowIndex -= 1
            End If
        Next
    End Sub
    Property RowIndex As Long
        Get
            Return _RowIndex
        End Get
        Set(ByVal value As Long)
            SetPropertyValue("RowIndex", _RowIndex, value)
        End Set
    End Property
    <Association("StorageIn", GetType(StorageInHeader))> _
    Property StorageInHeader() As StorageInHeader
        Get
            Return _StorageInHeader
        End Get
        Set(ByVal value As StorageInHeader)
            SetPropertyValue("StorageInHeader", _StorageInHeader, value)
        End Set
    End Property
    <RuleRequiredField("", DefaultContexts.Save)> _
    Property Products() As Products
        Get
            Return _Products
        End Get
        Set(ByVal value As Products)
            SetPropertyValue("Products", _Products, value)
        End Set
    End Property
    Property QTT() As Double
        Get
            Return _QTT
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT", _QTT, value)
        End Set
    End Property
    Property QTT_Will() As Double
        Get
            Return _QTT_Will
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT_Will", _QTT_Will, value)
        End Set
    End Property
    Property QTT_Out As Double
        Get
            Return _QTT_Out
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT_Out", _QTT_Out, value)
        End Set
    End Property
    Property QTT_Out_Will As Double
        Get
            Return _QTT_Out_Will
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_Out_Will", _QTT_Out_Will, value)
        End Set
    End Property
    Property QTT_Overstored As Double
        Get
            Return _QTT_Overstored
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_Overstored", _QTT_Overstored, value)
        End Set
    End Property
    Property QTT_Overstor As Double
        Get
            Return _QTT_Overstor
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_Overstor", _QTT_Overstor, value)
        End Set
    End Property
    Property QTT_Overstored_Will As Double
        Get
            Return _QTT_Overstored_Will
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_Overstored_Will", _QTT_Overstored_Will, value)
        End Set
    End Property
    Property QTT_Overstor_Will As Double
        Get
            Return _QTT_Overstor_Will
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_Overstor_Will", _QTT_Overstor_Will, value)
        End Set
    End Property
    Property Active() As Boolean
        Get
            Return _Active
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("Active", _Active, value)
        End Set
    End Property
    Property OnStorno As Boolean
        Get
            Return _OnStorno
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("OnStorno", _OnStorno, value)
        End Set
    End Property
    Property DeliveryNoteInRows As DeliveryNoteInRows
        Get
            Return _DeliveryNoteInRows
        End Get
        Set(value As DeliveryNoteInRows)
            SetPropertyValue("DeliveryNoteInRows", _DeliveryNoteInRows, value)
        End Set
    End Property
    Property PacketNumber As String
        Get
            Return _PacketNumber
        End Get
        Set(value As String)
            SetPropertyValue("PacketNumber", _PacketNumber, value)
        End Set
    End Property
    Property SerialNumber As String
        Get
            Return _SerialNumber
        End Get
        Set(value As String)
            SetPropertyValue("SerialNumber", _SerialNumber, value)
        End Set
    End Property
    Property Weight As Double
        Get
            Return _Weight
        End Get
        Set(value As Double)
            SetPropertyValue("Weight", _Weight, value)
        End Set
    End Property
    Property WeightMode As eWeightMode
        Get
            Return _WeightMode
        End Get
        Set(value As eWeightMode)
            SetPropertyValue("WeightMode", _WeightMode, value)
        End Set
    End Property

    <Browsable(False)> _
    Property Storage As Storage
        Get
            Return _Storage
        End Get
        Set(value As Storage)
            SetPropertyValue("Storage", _Storage, value)
        End Set
    End Property
    Property StorageInRowsFrom As StorageInRows
        Get
            Return _StorageInRowsFrom
        End Get
        Set(value As StorageInRows)
            SetPropertyValue("StorageInRowsFrom", _StorageInRowsFrom, 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
    '//XPCollections
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property StorageOutRowsInRows As XPCollection(Of StorageOutRowsInRows)
        Get
            Return New XPCollection(Of StorageOutRowsInRows)(Session, CriteriaOperator.Parse("StorageInRows=?", Me))
        End Get
    End Property
End Class
