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("BusinessStocks")> _
<DefaultProperty("ShortName")> _
Public Class Pricing ' -- Árképzés
    Inherits BaseObject
    Private _ShortName As String 'Árképzés megnevezése
    Private _PartnerType As ePartnerType 'Vevő vagy szállító
    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.
    End Sub
    <RuleRequiredField("Pricing-ShortName", DefaultContexts.Save)> _
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    <RuleRequiredField("Pricing-PartnerType", DefaultContexts.Save)> _
    Property PartnerType As ePartnerType
        Get
            Return _PartnerType
        End Get
        Set(value As ePartnerType)
            SetPropertyValue("PartnerType", _PartnerType, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    <Association("Pricing-PricingFix", GetType(PricingFix))> _
    ReadOnly Property PricingFix As XPCollection(Of PricingFix)
        Get
            Return GetCollection(Of PricingFix)("PricingFix")
        End Get
    End Property
    <VisibleInListView(False)> _
    <Association("Pricing-PricingFixStaged", GetType(PricingFixStaged))> _
    ReadOnly Property PricingFixStaged As XPCollection(Of PricingFixStaged)
        Get
            Return GetCollection(Of PricingFixStaged)("PricingFixStaged")
        End Get
    End Property
    <VisibleInListView(False)> _
    <Association("Pricing-PricingProductsGroups", GetType(PricingProductsGroups))> _
    ReadOnly Property PricingProductsGroups As XPCollection(Of PricingProductsGroups)
        Get
            Return GetCollection(Of PricingProductsGroups)("PricingProductsGroups")
        End Get
    End Property
    <VisibleInListView(False)> _
    <Association("Pricing-PricingCRM", GetType(PricingCRM))> _
    ReadOnly Property PricingCRM As XPCollection(Of PricingCRM)
        Get
            Return GetCollection(Of PricingCRM)("PricingCRM")
        End Get
    End Property
    Function GetCurrentPriceNettoHUF(ByVal _p_Products As Products, ByVal _p_QTT As Double, Optional _p_CRM_Opportunities As CRM_Opportunities = Nothing) As Double
        'A függvény visszatér erre az árképzésre vonatkozólag, hogy a paraméterben megadott terméknek mennyi az ára HUF-ban
        Dim _i As Integer = 0 ' segédváltozó, hogy ha nincs a megadott intervallumokban a mennyiség akkor a normál listaárral térünk vissza
        Dim _PricingCRM As PricingCRM

        If _p_CRM_Opportunities IsNot Nothing Then
            For Each _PricingCRM In Me.PricingCRM
                If _p_CRM_Opportunities Is _PricingCRM.CRM_Opportunities And _PricingCRM.ProductsGroups Is Nothing Then
                    If PartnerType = ePartnerType.ePayabels Then
                        Return _p_Products.ListPriceInHUF - ((_PricingCRM.DiscountPercent / 100) * _p_Products.ListPriceInHUF)
                    Else
                        Return _p_Products.ListPriceOutHUF - ((_PricingCRM.DiscountPercent / 100) * _p_Products.ListPriceOutHUF)
                    End If
                    Exit For
                ElseIf _p_CRM_Opportunities Is _PricingCRM.CRM_Opportunities And _p_Products.ProductGroups Is _PricingCRM.ProductsGroups Then
                    If PartnerType = ePartnerType.ePayabels Then
                        Return _p_Products.ListPriceInHUF - ((_PricingCRM.DiscountPercent / 100) * _p_Products.ListPriceInHUF)
                    Else
                        Return _p_Products.ListPriceOutHUF - ((_PricingCRM.DiscountPercent / 100) * _p_Products.ListPriceOutHUF)
                    End If

                    Exit For
                End If
            Next
        End If
        '// Akciós ÁR alapján !

        For Each _PricingFix As PricingFix In PricingFix '1. Fix árat megnézni
            If _PricingFix.Products Is _p_Products Then
                If _PricingFix.CurrencyName.ShortName = "HUF" Then
                    Return _PricingFix.PriceNetto
                End If
            End If
        Next
        For Each _PricingFixStaged As PricingFixStaged In PricingFixStaged '2. Fix lépcsõs árat megnézni
            If _PricingFixStaged.Products Is _p_Products Then
                If _p_QTT >= _PricingFixStaged.VolumeFrom And _p_QTT <= _PricingFixStaged.VolumeTo Then
                    If _PricingFixStaged.CurrencyName.ShortName = "HUF" Then
                        Return _PricingFixStaged.PriceNetto
                    End If
                Else
                    _i = _i + 1
                End If
                If _i >= PricingFixStaged.Count Then ' Amennyiben a megadott intervallumokon kívül van a megadott mennyiség, akkor normál listaár
                    Return _p_Products.ListPriceOutHUF
                End If
            End If
        Next
        For Each _PricingProductsGroups As PricingProductsGroups In PricingProductsGroups '3. Termékcsoport árat megnézni

            If _PricingProductsGroups.ProductsGroups Is Nothing Then
                If PartnerType = ePartnerType.ePayabels Then
                    Return _p_Products.ListPriceInHUF - ((_PricingProductsGroups.DiscountPercent / 100) * _p_Products.ListPriceInHUF)
                Else
                    Return _p_Products.ListPriceOutHUF - ((_PricingProductsGroups.DiscountPercent / 100) * _p_Products.ListPriceOutHUF)
                End If
                Exit For
            Else
                If _PricingProductsGroups.ProductsGroups Is _p_Products.ProductGroups Then ' Ha a levélelemnek meg van adva a kedvezmény
                    If PartnerType = ePartnerType.ePayabels Then
                        Return _p_Products.ListPriceInHUF - ((_PricingProductsGroups.DiscountPercent / 100) * _p_Products.ListPriceInHUF)
                    Else
                        Return _p_Products.ListPriceOutHUF - ((_PricingProductsGroups.DiscountPercent / 100) * _p_Products.ListPriceOutHUF)
                    End If
                Else
                    Return RecurziveProductsGroupsHUF(_p_Products, _PricingProductsGroups, _p_Products.ProductGroups) ' A levélelemnek nincs megadva a kedvezmény akkor rekurzív bejárás
                End If
            End If
        Next
    End Function
    Function RecurziveProductsGroupsHUF(ByVal _p_Products As Products, ByVal _p_PricingProductsGroups As PricingProductsGroups, ByVal _p_ProductsGroups As ProductsGroups)
        'A treelist bejárása rekurzívan, mindaddig amíg van szülő, és keressük van e termékcsoport kedvezmény, ha van visszatérünk vele, ha nincs akkor az eredeti listaárral térünk vissza
        If _p_ProductsGroups.Parent IsNot Nothing Then
            If _p_ProductsGroups.Parent Is _p_PricingProductsGroups.ProductsGroups Then
                Return _p_Products.ListPriceOutHUF - ((_p_PricingProductsGroups.DiscountPercent / 100) * _p_Products.ListPriceOutHUF) ' Visszatérés a levélelemhez legközelebbi termékcsoport kedvezményes árral.
            End If
            RecurziveProductsGroupsHUF(_p_Products, _p_PricingProductsGroups, _p_ProductsGroups.Parent) ' Rekurzív hívás, hogy minden elemen keressen
        End If
        Return _p_Products.ListPriceOutHUF ' Visszatérés az eredeti listaárral
    End Function
    Function GetCurrentPriceNettoDEV(ByVal _p_Products As Products, ByVal _p_QTT As Double, Optional _p_CRM_Opportunities As CRM_Opportunities = Nothing) As Double
        'A függvény visszatér erre az árképzésre vonatkozólag, hogy a paraméterben megadott terméknek mennyi az ára adott DEV-ben
        Dim _i As Integer = 0 ' segédváltozó, hogy ha nincs a megadott intervallumokban a mennyiség akkor a normál listaárral térünk vissza
        Dim _PricingCRM As PricingCRM

        If _p_CRM_Opportunities IsNot Nothing Then
            For Each _PricingCRM In Me.PricingCRM
                If _p_CRM_Opportunities Is _PricingCRM.CRM_Opportunities And _PricingCRM.ProductsGroups Is Nothing Then
                    If PartnerType = ePartnerType.ePayabels Then
                        Return _p_Products.ListPriceInDEV - ((_PricingCRM.DiscountPercent / 100) * _p_Products.ListPriceInDEV)
                    Else
                        Return _p_Products.ListPriceOutDEV - ((_PricingCRM.DiscountPercent / 100) * _p_Products.ListPriceOutDEV)
                    End If
                    Exit For
                ElseIf _p_CRM_Opportunities Is _PricingCRM.CRM_Opportunities And _p_Products.ProductGroups Is _PricingCRM.ProductsGroups Then
                    If PartnerType = ePartnerType.ePayabels Then
                        Return _p_Products.ListPriceInDEV - ((_PricingCRM.DiscountPercent / 100) * _p_Products.ListPriceInDEV)
                    Else
                        Return _p_Products.ListPriceOutDEV - ((_PricingCRM.DiscountPercent / 100) * _p_Products.ListPriceOutDEV)
                    End If

                    Exit For
                End If
            Next
        End If

        For Each _PricingFix As PricingFix In PricingFix '1. Fix árat megnézni
            If _PricingFix.Products Is _p_Products Then
                If _PricingFix.CurrencyName.ShortName <> "HUF" Then
                    Return _PricingFix.PriceNetto
                End If
            End If
        Next
        For Each _PricingFixStaged As PricingFixStaged In PricingFixStaged '2. Fix lépcsõs árat megnézni
            If _PricingFixStaged.Products Is _p_Products Then
                If _p_QTT >= _PricingFixStaged.VolumeFrom And _p_QTT <= _PricingFixStaged.VolumeTo Then
                    If _PricingFixStaged.CurrencyName.ShortName <> "HUF" Then
                        Return _PricingFixStaged.PriceNetto
                    End If
                Else
                    _i = _i + 1
                End If
                If _i >= PricingFixStaged.Count Then ' Amennyiben a megadott intervallumokon kívül van a megadott mennyiség, akkor normál listaár
                    Return _p_Products.ListPriceOutDEV
                End If
            End If
        Next
        For Each _PricingProductsGroups As PricingProductsGroups In PricingProductsGroups '3. Termékcsoport árat megnézni

            If _PricingProductsGroups.ProductsGroups Is Nothing Then
                If PartnerType = ePartnerType.ePayabels Then
                    Return _p_Products.ListPriceInDEV - ((_PricingProductsGroups.DiscountPercent / 100) * _p_Products.ListPriceInDEV)
                Else
                    Return _p_Products.ListPriceOutDEV - ((_PricingProductsGroups.DiscountPercent / 100) * _p_Products.ListPriceOutDEV)
                End If
                Exit For
            Else
                If _PricingProductsGroups.ProductsGroups Is _p_Products.ProductGroups Then ' Ha a levélelemnek meg van adva a kedvezmény
                    If PartnerType = ePartnerType.ePayabels Then
                        Return _p_Products.ListPriceInDEV - ((_PricingProductsGroups.DiscountPercent / 100) * _p_Products.ListPriceInDEV)
                    Else
                        Return _p_Products.ListPriceOutDEV - ((_PricingProductsGroups.DiscountPercent / 100) * _p_Products.ListPriceOutDEV)
                    End If
                Else
                    Return RecurziveProductsGroupsDEV(_p_Products, _PricingProductsGroups, _p_Products.ProductGroups) ' A levélelemnek nincs megadva a kedvezmény akkor rekurzív bejárás
                End If
            End If
        Next
    End Function
    Function RecurziveProductsGroupsDEV(ByVal _p_Products As Products, ByVal _p_PricingProductsGroups As PricingProductsGroups, ByVal _p_ProductsGroups As ProductsGroups)
        'A treelist bejárása rekurzívan, mindaddig amíg van szülő, és keressük van e termékcsoport kedvezmény, ha van visszatérünk vele, ha nincs akkor az eredeti listaárral térünk vissza
        If _p_ProductsGroups.Parent IsNot Nothing Then
            If _p_ProductsGroups.Parent Is _p_PricingProductsGroups.ProductsGroups Then
                Return _p_Products.ListPriceOutDEV - ((_p_PricingProductsGroups.DiscountPercent / 100) * _p_Products.ListPriceOutDEV) ' Visszatérés a levélelemhez legközelebbi termékcsoport kedvezményes árral.
            End If
            RecurziveProductsGroupsDEV(_p_Products, _p_PricingProductsGroups, _p_ProductsGroups.Parent) ' Rekurzív hívás, hogy minden elemen keressen 
        End If
        Return _p_Products.ListPriceOutDEV ' Visszatérés az eredeti listaárral
    End Function
End Class
