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)> _
<Appearance("Disable InsertServicesToOfferOut and aImportRecipe", AppearanceItemType.Action, "OfferOutHeader.IsEditable = False", TargetItems:="InsertServicesToOfferOut;aImportRecipe", Enabled:=False)> _
Public Class OfferOutRows
    Inherits BaseObject
    Private _OfferOutHeader As OfferOutHeader 'Árajánlat fejléce
    Private _RowType As eRowType 'Sor miről szól ?
    Private _RowGroup1 As String 'Szöveges csoportosítás (nem kötelező)
    Private _RowGroup2 As String 'Szöveges másodlagos csoportosítás (nem kötelező)
    Private _Product As Products 'Termék
    Private _Installation As Installation 'Gyártmány
    Private _RowIndex As Long 'Sor index (mozgathatónak kell lennie !)
    Private _CustomsTariffs As CustomsTariffs 'Vámtarifaszám, vagy szj szám vagy még ami lesz
    Private _ShortName As String 'Megnevezés -> Ez megy majd rá később a számlára is !
    Private _Description As String 'Leírás -> ez is rá fog menni a számlára !
    Private _DescriptionPrivate As String 'Na ez csak látszódjon, de nem menjen föl semmilyen doksira !!!
    Private _ListPriceHUF As Double = 0 'Listaár HUF
    Private _ListPriceDEV As Double = 0 ' Listaár DEV
    Private _DiscountPriceHUF As Double = 0 'Kedvezmény összege HUF
    Private _DiscountPriceDEV As Double = 0 'Kedvezmény összege DEV
    Private _DiscountPercent As Double = 0 'Kedvezmény  %-ban (56 az 56% !!!)
    Private _QTT As Double = 0 'Mennyiség. A Mennyiségi egység vagy a Products-ból, vag Installationnál DB
    Private _Units As Units 'Mennyiségi egység
    Private _QualityOption As QualityOption 'Minőségi opció
    Private _VAT As VAT 'ÁFA kód
    Private _SumPriceHUF As Double
    Private _SumPriceDEV As Double
    Private _FinalPriceDEV As Double
    Private _FinalPriceHUF As Double
    Private _OfferInRows As OfferInRows 'Bejövő ajánlat sora !!! (Alvállalkozó!!!)
    Private _Recipe As Recipe 'Milyen receptből van ez a sor

    Private _CanChangeProperty As Boolean = True

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    <Association("OfferOutHeader", GetType(OfferOutHeader))> _
    Property OfferOutHeader() As OfferOutHeader
        Get
            Return _OfferOutHeader
        End Get
        Set(ByVal value As OfferOutHeader)
            SetPropertyValue("OfferOutHeader", _OfferOutHeader, value)
        End Set
    End Property
    Property RowType() As eRowType
        Get
            Return _RowType
        End Get
        Set(ByVal value As eRowType)
            SetPropertyValue("RowType", _RowType, value)
        End Set
    End Property
    <RuleRequiredField("", "aTotable_OfferOut")> _
    Property Product() As Products
        Get
            Return _Product
        End Get
        Set(ByVal value As Products)
            SetPropertyValue("Product", _Product, value)

            If value Is Nothing Then
            Else
                _ShortName = value.ShortName
                _ListPriceHUF = value.ListPriceOutHUF
                OnChanged("ShortName")
                OnChanged("ListPriceHUF")
            End If

        End Set
    End Property
    Property Installation As Installation
        Get
            Return _Installation
        End Get
        Set(ByVal value As Installation)
            SetPropertyValue("Installation", _Installation, value)
        End Set
    End Property
    Property RowIndex() As Long
        Get
            Return _RowIndex
        End Get
        Set(ByVal value As Long)
            SetPropertyValue("RowIndex", _RowIndex, value)
        End Set
    End Property
    Property RowGroup1 As String
        Get
            Return _RowGroup1
        End Get
        Set(value As String)
            SetPropertyValue("RowGroup1", _RowGroup1, value)
        End Set
    End Property
    Property RowGroup2 As String
        Get
            Return _RowGroup2
        End Get
        Set(value As String)
            SetPropertyValue("RowGroup2", _RowGroup2, value)
        End Set
    End Property
    Property CustomsTariffs() As CustomsTariffs
        Get
            Return _CustomsTariffs
        End Get
        Set(ByVal value As CustomsTariffs)
            SetPropertyValue("CustomsTariffs", _CustomsTariffs, value)
        End Set
    End Property
    <Size(255)> _
    Property ShortName() As String
        Get
            Return _ShortName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    <Size(-1)> _
    Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Description", _Description, value)
        End Set
    End Property
    <Size(-1)> _
    Property DescriptionPrivate As String
        Get
            Return _DescriptionPrivate
        End Get
        Set(ByVal value As String)
            SetPropertyValue("DescriptionPrivate", _DescriptionPrivate, value)
        End Set
    End Property
    Property ListPriceHUF() As Double
        Get
            Return _ListPriceHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("ListPriceHUF", _ListPriceHUF, value)

        End Set
    End Property
    Property ListPriceDEV() As Double
        Get
            Return _ListPriceDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("ListPriceDEV", _ListPriceDEV, value)

        End Set
    End Property
    <ImmediatePostData()> _
    Property DiscountPercent() As Double
        Get
            Return _DiscountPercent
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("DiscountPercent", _DiscountPercent, value)

        End Set
    End Property
    <RuleRequiredField("", DefaultContexts.Save)> _
    <RuleRange("", DefaultContexts.Save, 0.001, 999999999)> _
    Property QTT() As Double
        Get
            Return _QTT
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT", _QTT, value)
            RecalculateRow()
        End Set
    End Property
    Property Units As Units
        Get
            Return _Units
        End Get
        Set(value As Units)
            SetPropertyValue("Units", _Units, 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 VAT() As VAT
        Get
            Return _VAT
        End Get
        Set(ByVal value As VAT)
            SetPropertyValue("VAT", _VAT, value)
        End Set
    End Property
    Property FinalPriceDEV() As Double
        Get
            Return _FinalPriceDEV
        End Get
        Set(value As Double)
            SetPropertyValue("FinalPriceDEV", _FinalPriceDEV, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If ListPriceDEV = 0 Then ListPriceDEV = value
                If CanChangeProperty Then
                    RecalculateRow2()
                    RecalculateRow()
                End If
            End If
        End Set
    End Property
    Property FinalPriceHUF() As Double
        Get
            Return _FinalPriceHUF
        End Get
        Set(value As Double)
            SetPropertyValue("FinalPriceHUF", _FinalPriceHUF, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If ListPriceHUF = 0 Then ListPriceHUF = value
                If CanChangeProperty Then
                    RecalculateRow2()
                    RecalculateRow()
                End If
            End If
        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
    <Browsable(False)> _
    Property DiscountPriceHUF() As Double
        Get
            Return _DiscountPriceHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("DiscountPriceHUF", _DiscountPriceHUF, value)
        End Set
    End Property
    <Browsable(False)> _
    Property DiscountPriceDEV() As Double
        Get
            Return _DiscountPriceDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("DiscountPriceDEV", _DiscountPriceDEV, value)
        End Set
    End Property
    Property OfferInRows As OfferInRows
        Get
            Return _OfferInRows
        End Get
        Set(value As OfferInRows)
            SetPropertyValue("OfferInRows", _OfferInRows, value)
        End Set
    End Property
    Property Recipe As Recipe
        Get
            Return _Recipe
        End Get
        Set(value As Recipe)
            SetPropertyValue("Recipe", _Recipe, value)
        End Set
    End Property
    <Browsable(False)> _
    Property CanChangeProperty As Boolean
        Get
            Return _CanChangeProperty
        End Get
        Set(value As Boolean)
            SetPropertyValue("CanChangeProperty", _CanChangeProperty, value)
        End Set
    End Property
    Public Sub RecalculateRow()
        DiscountPriceDEV = ListPriceDEV * (DiscountPercent / 100)
        DiscountPriceHUF = ListPriceHUF * (DiscountPercent / 100)
        SumPriceDEV = (ListPriceDEV - DiscountPriceDEV) * QTT
        SumPriceHUF = (ListPriceHUF - DiscountPriceHUF) * QTT
    End Sub
    Public Sub RecalculateRow2()
        If Me.OfferOutHeader.CurrencyName.ShortName = "HUF" Then
            If ListPriceHUF <> 0 Then
                DiscountPercent = 100 - (FinalPriceHUF / ListPriceHUF * 100)
            End If
        Else
            If ListPriceDEV <> 0 Then
                DiscountPercent = 100 - (FinalPriceDEV / ListPriceDEV * 100)
            End If
        End If
    End Sub
    Public Sub AddRows(ByVal _p_OfferOutHeader As OfferOutHeader)
        ' Sor hozzáadása
        CanChangeProperty = False
        Me.OfferOutHeader = _p_OfferOutHeader

        If _p_OfferOutHeader.row_RowType = eRowType.IsInstallation Then ' Sor hozzáadása ha gyártmány típusú a sor
            Me.Installation = _p_OfferOutHeader.row_Installation
            Me.RowType = _p_OfferOutHeader.row_RowType
            Me.QTT = _p_OfferOutHeader.row_QTT
            Me.Units = Session.FindObject(Of Units)(CriteriaOperator.Parse("ShortName='DB'"))
            Me.ListPriceHUF = _p_OfferOutHeader.row_ListPriceHUF
            Me.ListPriceDEV = _p_OfferOutHeader.row_ListPriceDEV
            Me.DiscountPercent = _p_OfferOutHeader.row_DiscountPercent
            Me.RowGroup1 = _p_OfferOutHeader.row_RowGroup1
            Me.RowGroup2 = _p_OfferOutHeader.row_RowGroup2
            Me.VAT = _p_OfferOutHeader.row_VAT

            RecalculateRow()
            FinalPriceDEV = ListPriceDEV - DiscountPriceDEV
            FinalPriceHUF = ListPriceHUF - DiscountPriceHUF
        End If

        If _p_OfferOutHeader.row_RowType = eRowType.IsOther Then ' Sor hozzáadása ha egyéb típusú a sor
            Me.ShortName = _p_OfferOutHeader.row_ShortName
            Me.RowType = _p_OfferOutHeader.row_RowType
            Me.QTT = _p_OfferOutHeader.row_QTT
            Me.Units = _p_OfferOutHeader.row_Units
            Me.ListPriceHUF = _p_OfferOutHeader.row_ListPriceHUF
            Me.ListPriceDEV = _p_OfferOutHeader.row_ListPriceDEV
            Me.DiscountPercent = _p_OfferOutHeader.row_DiscountPercent
            Me.Description = _p_OfferOutHeader.row_Description
            Me.VAT = _p_OfferOutHeader.row_VAT
            Me.RowGroup1 = _p_OfferOutHeader.row_RowGroup1
            Me.RowGroup2 = _p_OfferOutHeader.row_RowGroup2

            RecalculateRow()
            FinalPriceDEV = ListPriceDEV - DiscountPriceDEV
            FinalPriceHUF = ListPriceHUF - DiscountPriceHUF
        End If

        If _p_OfferOutHeader.row_RowType = eRowType.IsProduct Then ' Sor hozzáadása ha termék típusú a sor
            Me.Product = _p_OfferOutHeader.row_Products
            Me.RowType = _p_OfferOutHeader.row_RowType
            Me.QTT = _p_OfferOutHeader.row_QTT
            Me.Units = _p_OfferOutHeader.row_Products.Units
            Me.ListPriceHUF = _p_OfferOutHeader.row_ListPriceHUF
            Me.ListPriceDEV = _p_OfferOutHeader.row_ListPriceDEV
            Me.DiscountPercent = _p_OfferOutHeader.row_DiscountPercent
            Me.VAT = _p_OfferOutHeader.row_Products.VAT
            Me.RowGroup1 = _p_OfferOutHeader.row_RowGroup1
            Me.RowGroup2 = _p_OfferOutHeader.row_RowGroup2
            RecalculateRow()
            FinalPriceDEV = ListPriceDEV - DiscountPriceDEV
            FinalPriceHUF = ListPriceHUF - DiscountPriceHUF
        End If

        ' Indexelés
        If _p_OfferOutHeader.OfferOutRows.Count = 0 Then
            Me.RowIndex = 0
        Else
            Me.RowIndex = _p_OfferOutHeader.OfferOutRows.Count - 1
        End If
        CanChangeProperty = True
    End Sub
End Class
