Imports System
Imports System.ComponentModel

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp.ConditionalAppearance
Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports System.Linq
Imports System.Linq.Expressions
<DefaultClassOptions()> _
<NavigationItem(False), CreatableItem(False)> _
<DefaultProperty("DisplayName")> _
Public Class OrderOutRows
    Inherits BaseObject
    Private _RowIndex As Long 'Sor index
    Private _OrderOutHeader As OrderOutHeader 'Rendelés fejléc
    Private _ShipmentOption As ShipmentOption 'Szállítási opció
    Private _QualityOption As QualityOption 'Minőségi opció
    Private _UrgentOption As UrgentOption ' Sürgősségi opció
    Private _Products As Products 'Termék
    Private _QTT As Double 'Rendelt mennyiség
    Private _QTT_DeliveryNoteIn As Double = 0 'Eddig betárolt mennyiség
    Private _ListPriceHUF As Double 'Termék listaára HUF
    Private _ListPriceDEV As Double 'Termék listaára DEV
    Private _SumPriceHUF As Double = 0
    Private _SumPriceDEV As Double = 0
    Private _DiscountPercent As Double 'Termék ár kedvezmény %-ban
    Private _NoteRows As String 'Sor megjegyzés ami felkerül a rendelésre
    Private _NoteRowsPrivate As String 'Belső megjegyzés, ami nem kerül fel a nyomtatványra
    Private _OrderOutRows As OrderOutRows 'Eredeti rendelési sor (csak helyesbítésnél érdekes)
    Private _IsCorrigendum As Boolean = False 'Helyesbített a sor ?
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        RecalculateSum()
        If OrderOutRows IsNot Nothing Then
            OrderOutRows.IsCorrigendum = True
        End If
    End Sub
    Private Sub RecalculateSum()
        SumPriceHUF = QTT * ListPriceHUF
        SumPriceDEV = QTT * ListPriceDEV
        If Me.DiscountPercent <> 0 Then
            Me.SumPriceHUF = Me.SumPriceHUF - (Me.SumPriceHUF * Me.DiscountPercent / 100)
            Me.SumPriceDEV = Me.SumPriceDEV - (Me.SumPriceDEV * Me.DiscountPercent / 100)
        End If
    End Sub
    Public Sub AddRows(ByVal _p_OrderOutHeader As OrderOutHeader, Optional _p_OrderOutRows As OrderOutRows = Nothing)
        ' Sor hozzáadása a fejléchez, valamint árkalkuláció a sor termékére
        Me.OrderOutHeader = _p_OrderOutHeader
        Me.OrderOutRows = _p_OrderOutRows
        Me.Products = _p_OrderOutHeader.row_Products
        Me.ListPriceDEV = _p_OrderOutHeader.row_ListPriceDEV
        Me.ListPriceHUF = _p_OrderOutHeader.row_ListPriceHUF
        Me.DiscountPercent = _p_OrderOutHeader.row_DiscountPercent
        Me.QTT = _p_OrderOutHeader.row_QTT
        If _p_OrderOutHeader.OrderOutRows.Count = 0 Then
            Me.RowIndex = 1
        Else
            Me.RowIndex = _p_OrderOutHeader.OrderOutRows.Count
        End If
    End Sub
    Public Sub RemoveRows(ByVal _p_OrderOutHeader As OrderOutHeader)
        If Me.OrderOutHeader.IsEditable = True Then
            For Each _OrderOutRows As OrderOutRows In _p_OrderOutHeader.OrderOutRows
                If _OrderOutRows.RowIndex > Me.RowIndex Then
                    _OrderOutRows.RowIndex -= 1
                End If
            Next
            Me.Delete()
            Me.Save()
            Session.CommitTransaction()
        End If
    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("OrderOutHeader-OrderOutRows", GetType(OrderOutHeader))> _
    Property OrderOutHeader As OrderOutHeader
        Get
            Return _OrderOutHeader
        End Get
        Set(ByVal value As OrderOutHeader)
            SetPropertyValue("OrderOutHeader", _OrderOutHeader, value)
        End Set
    End Property
    Property ShipmentOption() As ShipmentOption
        Get
            Return _ShipmentOption
        End Get
        Set(ByVal value As ShipmentOption)
            SetPropertyValue("ShipmentOption", _ShipmentOption, value)
        End Set
    End Property
    Property QualityOption() As QualityOption
        Get
            Return _QualityOption
        End Get
        Set(ByVal value As QualityOption)
            SetPropertyValue("QualityOption", _QualityOption, value)
        End Set
    End Property
    Property UrgentOption() As UrgentOption
        Get
            Return _UrgentOption
        End Get
        Set(ByVal value As UrgentOption)
            SetPropertyValue("UrgentOption", _UrgentOption, value)
        End Set
    End Property
    <RuleRequiredField("OrderOutRows.Products", DefaultContexts.Save)> _
    Property Products() As Products
        Get
            Return _Products
        End Get
        Set(ByVal value As Products)
            SetPropertyValue("Products", _Products, value)
        End Set
    End Property
    <RuleRange("OrderOutRows.QTT", DefaultContexts.Save, 0.1, Double.MaxValue, TargetCriteria:="IsNull(OrderOutRows)=True")> _
    Property QTT() As Double
        Get
            Return _QTT
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT", _QTT, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                Me.SumPriceHUF = value * Me.ListPriceHUF
                Me.SumPriceDEV = value * Me.ListPriceDEV
                If Me.DiscountPercent <> 0 Then
                    Me.SumPriceHUF = Me.SumPriceHUF - (Me.SumPriceHUF * Me.DiscountPercent / 100)
                    Me.SumPriceDEV = Me.SumPriceDEV - (Me.SumPriceDEV * Me.DiscountPercent / 100)
                End If
            End If
        End Set
    End Property
    Property QTT_DeliveryNoteIn As Double
        Get
            Return _QTT_DeliveryNoteIn
        End Get
        Set(value As Double)
            SetPropertyValue("QTT_DeliveryNoteIn", _QTT_DeliveryNoteIn, value)
        End Set
    End Property
    <RuleRange("OrderOutRows.ListPriceHUF", DefaultContexts.Save, 0.02, Double.MaxValue, TargetCriteria:="OrderOutHeader.CurrencyName.ShortName='HUF'")> _
    Property ListPriceHUF() As Double
        Get
            Return _ListPriceHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("ListPriceHUF", _ListPriceHUF, value)

        End Set
    End Property
    <RuleRange("OrderOutRows.ListPriceDEV", DefaultContexts.Save, 0.001, Double.MaxValue, TargetCriteria:="OrderOutHeader.CurrencyName.ShortName!='HUF'")> _
    Property ListPriceDEV() As Double
        Get
            Return _ListPriceDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("ListPriceDEV", _ListPriceDEV, value)
        End Set
    End Property
    Property SumPriceHUF As Double
        Get
            Return _SumPriceHUF
        End Get
        Set(value As Double)
            SetPropertyValue("SumPriceHUF", _SumPriceHUF, value)
        End Set
    End Property
    Property SumPriceDEV As Double
        Get
            Return _SumPriceDEV
        End Get
        Set(value As Double)
            SetPropertyValue("SumPriceDEV", _SumPriceDEV, value)
        End Set
    End Property
    ' <RuleRange("OrderOutRows.DiscountPercent", DefaultContexts.Save, 0, 100)> _
    Property DiscountPercent() As Double
        Get
            Return _DiscountPercent
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("DiscountPercent", _DiscountPercent, value)
        End Set
    End Property
    Property NoteRows() As String
        Get
            Return _NoteRows
        End Get
        Set(ByVal value As String)
            SetPropertyValue("NoteRows", _NoteRows, value)
        End Set
    End Property
    Property NoteRowsPrivate() As String
        Get
            Return _NoteRowsPrivate
        End Get
        Set(ByVal value As String)
            SetPropertyValue("NoteRowsPrivate", _NoteRowsPrivate, value)
        End Set
    End Property
    Property OrderOutRows As OrderOutRows
        Get
            Return _OrderOutRows
        End Get
        Set(value As OrderOutRows)
            SetPropertyValue("OrderOutRows", _OrderOutRows, value)
        End Set
    End Property
    Property IsCorrigendum As Boolean
        Get
            Return _IsCorrigendum
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsCorrigendum", _IsCorrigendum, value)
        End Set
    End Property

    ReadOnly Property QTT_Original As Double
        Get
            If OrderOutRows IsNot Nothing Then
                Return OrderOutRows.QTT
            Else
                Return 0
            End If
        End Get
    End Property
    <VisibleInDetailView(False)> _
    <VisibleInListView(False)> _
    <VisibleInLookupListView(False)> _
    ReadOnly Property DisplayName As String
        Get
            Dim _DisplayName As String = ""
            If Me.Products IsNot Nothing Then
                _DisplayName = Me.Products.ProductNumber.ToString & ", " & Me.Products.ShortName.ToString
                If OrderOutHeader IsNot Nothing Then
                    _DisplayName += ", " & OrderOutHeader.DocumentNumber
                End If
            End If
            Return _DisplayName
        End Get
    End Property
End Class
