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

'-- Bejövő megrendelés ---------------------------------------------------------------------------------------------------
<DefaultClassOptions()> _
<NavigationItem(False), CreatableItem(False)> _
<Appearance("Hide aOrderIn_ChangeRows when IsEditable=False", AppearanceItemType.Action, "OrderInHeader.IsEditable=False", TargetItems:="aOrderIn_ChangeRows", visibility:=Editors.ViewItemVisibility.Hide)> _
<DefaultProperty("DisplayName")> _
Public Class OrderInRows
    Inherits BaseObject
    Private _OrderInHeader As OrderInHeader ' -- Bejövő megrendelés fejléc
    Private _OfferOutRows As OfferOutRows ' Kiemnő árajánlat sora
    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 _RowIndex As Long ' -- Számla sorindex
    Private _Products As Products ' -- Termék
    Private _QTT As Double '-- Megrendelt mennyiség (amit tőlünk rendelt a vevő)
    Private _QTT_To As Double '-- Továbbrendelt mennyiség
    Private _QTT_Storaged_Will As Double '-- Kitárolásra foglalt mennyiség
    Private _QTT_Storaged As Double '-- Kiadott mennyiség
    Private _QTT_DeliveryNoted As Double '-- Kiszállítózott mennyiség
    Private _QTT_Invoiced As Double '-- Kiszámlázott mennyiség
    Private _QTT_Process As Double '-- Kézzel beírható mennyiség a végrehajtáshoz (pld. mennyi legyen kitárolva ...)

    Private _ListPriceOriginalHUF As Double = 0

    Private _ListPriceHUF As Double = 0 ' Egységár HUF (nettó !)
    Private _ListPriceDEV As Double = 0 ' Egységár DEV (Nettó !)
    Private _DiscountPriceHUF As Double = 0 'Kedvezmény összege HUF
    Private _DiscountPriceDEV As Double = 0 'Kedvezmény összege DEV
    Private _FinalPriceHUF As Double
    Private _FinalPriceDEV As Double
    Private _Description As String
    Private _DescriptionPrivate As String
    Private _QualityOption As QualityOption

    Private _VAT As VAT 'ÁFA kód

    Private _SumPriceNettoHUF As Double 'Netto sorösszess HUF
    Private _SumPriceNettoDEV As Double 'Netto sorösszes DEV
    Private _SumPriceVATHUF As Double 'ÁFA sorösszess HUF
    Private _SumPriceVATDEV As Double 'ÁFA sorösszes DEV
    Private _SumPriceBruttoHUF As Double 'Bruttó sorösszess HUF
    Private _SumPriceBruttoDEV As Double 'Bruttó sorösszes DEV


    '// Termeltetési szerződés összekapcsolása a bejövő megrendeléssel
    Private _APCSheetByProducts As APCSheetByProducts 'Termékfinanszírozás !!!
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        Me.RecalculateRows()
        QTT = Math.Round(QTT, 4, MidpointRounding.AwayFromZero)
        QTT_DeliveryNoted = Math.Round(QTT_DeliveryNoted, 4, MidpointRounding.AwayFromZero)
        QTT_Invoiced = Math.Round(QTT_Invoiced, 4, MidpointRounding.AwayFromZero)
        QTT_Process = Math.Round(QTT_Process, 4, MidpointRounding.AwayFromZero)
        QTT_Storaged = Math.Round(QTT_Storaged, 4, MidpointRounding.AwayFromZero)
        QTT_Storaged_Will = Math.Round(QTT_Storaged_Will, 4, MidpointRounding.AwayFromZero)
    End Sub
    Protected Overrides Sub OnSaved()
        MyBase.OnSaved()
        '//Termeltetési szerződési sor kalkulálása !!!
        If APCSheetByProducts IsNot Nothing Then
            APCSheetByProducts.FundingAmountUsedHUF = 0
            Dim _OrderInRows As OrderInRows
            Dim _OrderInRows_Collection As New XPCollection(Of OrderInRows)(Session, CriteriaOperator.Parse("APCSheetByProducts=?", APCSheetByProducts))
            For Each _OrderInRows In _OrderInRows_Collection
                APCSheetByProducts.FundingAmountUsedHUF += _OrderInRows.SumPriceBruttoHUF
            Next
            Dim _OrderInRowsOther As OrderInRowsOther
            Dim _OrderInRowsOther_Collection As New XPCollection(Of OrderInRowsOther)(Session, CriteriaOperator.Parse("APCSheetByProducts=?", APCSheetByProducts))
            For Each _OrderInRowsOther In _OrderInRowsOther_Collection
                APCSheetByProducts.FundingAmountUsedHUF += _OrderInRowsOther.SumPriceBruttoHUF
            Next
            APCSheetByProducts.FundingAmountAvailableHUF = APCSheetByProducts.FundingAmountHUF - APCSheetByProducts.FundingAmountUsedHUF

            APCSheetByProducts.Save()
            Session.CommitTransaction()
        End If
    End Sub
    Protected Overrides Sub OnDeleted()
        MyBase.OnDeleted()

    End Sub
    Public Sub AddRows(ByVal _p_OrderInHeader As OrderInHeader)
        ' Sor hozzáadása a fejléchez, valamint árkalkuláció a sor termékére
        Me.OrderInHeader = _p_OrderInHeader
        Me.Products = _p_OrderInHeader.row_Products.Products
        Me.QTT = _p_OrderInHeader.row_QTT
        Me.QTT_Process = _p_OrderInHeader.row_QTT
        Me.APCSheetByProducts = _p_OrderInHeader.row_APCSheetByProducts
        Me.VAT = Me.Products.VAT
        If _p_OrderInHeader.OrderInRows.Count = 0 Then
            Me.RowIndex = 0
        Else
            Me.RowIndex = _p_OrderInHeader.OrderInRows.Count - 1
        End If
      

        ' Vizsgáljuk. hogy van e árkalkulációja az ügyfélnek, ha van kalkulálunk ha nincs akkor a normál listaáron marad a termék
        If Me.OrderInHeader.Customers.Pricing IsNot Nothing Then
            If _p_OrderInHeader.OrderInParameters.CurrencyName.ShortName = "HUF" Then ' Ha a Deviza HUF
                Me.ListPriceHUF = Me.OrderInHeader.Customers.Pricing.GetCurrentPriceNettoHUF(_p_OrderInHeader.row_Products.Products, _p_OrderInHeader.row_QTT) ' A kedvezményt tartalmazó listaár HUF-ban
                Me.DiscountPriceHUF = 0 '_p_OrderInHeader.row_Products.Products.ListPriceOutHUF - Me.ListPriceHUF ' A kedvezmény összege HUF-ban
            Else ' Ha a deviza nem HUF
                Me.ListPriceDEV = Me.OrderInHeader.Customers.Pricing.GetCurrentPriceNettoDEV(_p_OrderInHeader.row_Products.Products, _p_OrderInHeader.row_QTT) ' A kedvezményt tartalmazó listaár DEV-ben
                Me.DiscountPriceDEV = 0 '_p_OrderInHeader.row_Products.Products.ListPriceOutDEV - Me.ListPriceDEV ' A kedvezmény összege DEV-ben
            End If
        Else ' Ha nincs árkalkuláció akkor a normál listaáron lesz a termék
            If _p_OrderInHeader.OrderInParameters.CurrencyName.ShortName = "HUF" Then
                Me.ListPriceHUF = Me.Products.ListPriceOutHUF
            Else
                Me.ListPriceDEV = Me.Products.ListPriceOutDEV
            End If
        End If
        If _p_OrderInHeader.row_o_ListPriceHUF > 0 Then
            Me.ListPriceHUF = _p_OrderInHeader.row_o_ListPriceHUF
        End If
        If _p_OrderInHeader.row_o_ListPriceDEV > 0 Then
            Me.ListPriceDEV = _p_OrderInHeader.row_o_ListPriceDEV
        End If

        If _p_OrderInHeader.row_DiscountPercent > 0 Then
            Me.DiscountPriceHUF = Math.Round(Me.ListPriceHUF * ((_p_OrderInHeader.row_DiscountPercent / 100)), 0, MidpointRounding.AwayFromZero)
            Me.DiscountPriceDEV = Math.Round(Me.ListPriceDEV * ((_p_OrderInHeader.row_DiscountPercent / 100)), 0, MidpointRounding.AwayFromZero)
        End If
        Session.CommitTransaction()
    End Sub
    Public Sub RemoveRows(ByVal _p_OrderInHeader As OrderInHeader, ByVal _p_ocur As Xpo.XPObjectSpace)
        For Each _OrderInRows As OrderInRows In _p_OrderInHeader.OrderInRows
            If _OrderInRows.RowIndex > Me.RowIndex Then
                _OrderInRows.RowIndex -= 1
            End If
        Next
    End Sub

    <Association("OrderInHeader", GetType(OrderInHeader))> _
    Property OrderInHeader() As OrderInHeader
        Get
            Return _OrderInHeader
        End Get
        Set(ByVal value As OrderInHeader)
            SetPropertyValue("OrderInHeader", _OrderInHeader, value)
        End Set
    End Property
    Property OfferOutRows As OfferOutRows
        Get
            Return _OfferOutRows
        End Get
        Set(value As OfferOutRows)
            SetPropertyValue("OfferOutRows", _OfferOutRows, 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 RowIndex() As Long
        Get
            Return _RowIndex
        End Get
        Set(ByVal value As Long)
            SetPropertyValue("RowIndex", _RowIndex, 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_To() As Double
        Get
            Return _QTT_To
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT_To", _QTT_To, value)
        End Set
    End Property
    Property QTT_Storaged_Will As Double
        Get
            Return _QTT_Storaged_Will
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT_Storaged_Will", _QTT_Storaged_Will, value)
        End Set
    End Property
    Property QTT_Storaged() As Double
        Get
            Return _QTT_Storaged
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT_Storaged", _QTT_Storaged, value)
        End Set
    End Property
    Property QTT_DeliveryNoted() As Double
        Get
            Return _QTT_DeliveryNoted
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT_DeliveryNoted", _QTT_DeliveryNoted, value)
        End Set
    End Property
    Property QTT_Invoiced() As Double
        Get
            Return _QTT_Invoiced
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT_Invoiced", _QTT_Invoiced, value)
        End Set
    End Property
    <RuleRange("OrderInRows.QTT_Process_RangeRule", "OrderInRows_aGenerateStorageOutFromOrder_Context", 1, 999999999999)> _
    Property QTT_Process As Double
        Get
            Return _QTT_Process
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT_Process", _QTT_Process, 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
    Property DiscountPriceHUF() As Double
        Get
            Return _DiscountPriceHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("DiscountPriceHUF", _DiscountPriceHUF, value)
        End Set
    End Property
    Property DiscountPriceDEV() As Double
        Get
            Return _DiscountPriceDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("DiscountPriceDEV", _DiscountPriceDEV, value)
        End Set
    End Property
    ReadOnly Property FinalPriceDEV() As Double
        Get
            Return ListPriceDEV - DiscountPriceDEV
        End Get
    End Property
    ReadOnly Property FinalPriceHUF() As Double
        Get
            Return ListPriceHUF - DiscountPriceHUF
        End Get
    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 QualityOption As QualityOption
        Get
            Return _QualityOption
        End Get
        Set(value As QualityOption)
            SetPropertyValue("QualityOption", _QualityOption, value)
        End Set
    End Property

    Property VAT As VAT
        Get
            Return _VAT
        End Get
        Set(value As VAT)
            SetPropertyValue("VAT", _VAT, value)
        End Set
    End Property
    Property SumPriceNettoHUF As Double
        Get
            Return _SumPriceNettoHUF
        End Get
        Set(value As Double)
            SetPropertyValue("SumPriceNettoHUF", _SumPriceNettoHUF, value)
        End Set
    End Property
    Property SumPriceNettoDEV As Double
        Get
            Return _SumPriceNettoDEV
        End Get
        Set(value As Double)
            SetPropertyValue("SumPriceNettoDEV", _SumPriceNettoDEV, value)
        End Set
    End Property
    Property SumPriceVATHUF As Double
        Get
            Return _SumPriceVATHUF
        End Get
        Set(value As Double)
            SetPropertyValue("SumPriceVATHUF", _SumPriceVATHUF, value)
        End Set
    End Property
    Property SumPriceVATDEV As Double
        Get
            Return _SumPriceVATDEV
        End Get
        Set(value As Double)
            SetPropertyValue("SumPriceVATDEV", _SumPriceVATDEV, value)
        End Set
    End Property
    Property SumPriceBruttoHUF As Double
        Get
            Return _SumPriceBruttoHUF
        End Get
        Set(value As Double)
            SetPropertyValue("SumPriceBruttoHUF", _SumPriceBruttoHUF, value)
        End Set
    End Property
    Property SumPriceBruttoDEV As Double
        Get
            Return _SumPriceBruttoDEV
        End Get
        Set(value As Double)
            SetPropertyValue("SumPriceBruttoDEV", _SumPriceBruttoDEV, value)
        End Set
    End Property

    Property ListPriceOriginalHUF As Double
        Get
            Return _ListPriceOriginalHUF
        End Get
        Set(value As Double)
            SetPropertyValue("ListPriceOriginalHUF", _ListPriceOriginalHUF, value)
        End Set
    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 OrderInHeader IsNot Nothing Then
                    _DisplayName += ", " & OrderInHeader.DocumentNumber
                End If
            End If
            Return _DisplayName
        End Get
    End Property
    ReadOnly Property DiscountPercent As Double
        Get
            Dim _DiscountPercent As Double = 0
            If ListPriceHUF <> 0 Then
                _DiscountPercent = (DiscountPriceHUF / ListPriceHUF) * 100
            End If
            Return Math.Round(_DiscountPercent, 2, MidpointRounding.AwayFromZero)
        End Get
    End Property
    '// Termeltetési szerződés miatt
    Property APCSheetByProducts As APCSheetByProducts
        Get
            Return _APCSheetByProducts
        End Get
        Set(value As APCSheetByProducts)
            SetPropertyValue("APCSheetByProducts", _APCSheetByProducts, value)
        End Set
    End Property
    '--XPCollections
    <VisibleInListView(False)> _
    ReadOnly Property StorageComputed_Collection As XPCollection(Of StorageComputed)
        Get
            Dim _StorageComputed As XPCollection(Of StorageComputed)
            _StorageComputed = New XPCollection(Of StorageComputed)(Session, CriteriaOperator.Parse("Products=? AND QTTFree>0 and Storage.CustomersFrom=?", Products, Me.OrderInHeader.CustomersFrom))
            Return _StorageComputed
        End Get
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property StorageOutRowsInRows_Collection As XPCollection(Of StorageOutRowsInRows)
        Get
            Return New XPCollection(Of StorageOutRowsInRows)(Session, CriteriaOperator.Parse("StorageOutRows.OrderInRows.Oid=?", Me.Oid))
        End Get
    End Property
    <VisibleInDetailView(False), VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property DeliveryNoteOutRows As XPCollection(Of DeliveryNoteOutRows)
        Get
            Return New XPCollection(Of DeliveryNoteOutRows)(Session, CriteriaOperator.Parse("StorageOutRowsInRows.StorageOutRows.OrderInRows=?", Me))
        End Get
    End Property

    Sub RecalculateRows()
        If Me.OrderInHeader Is Nothing Then Exit Sub
        Dim _VATValue As Double = 0
        If VAT IsNot Nothing Then
            _VATValue = VAT.KeyValue
        End If
        If Me.APCSheetByProducts IsNot Nothing Then
            Dim _InterestValue As Double = 0
            Dim _DifferentDay As Double = 0

            _InterestValue = Me.APCSheetByProducts.APCSheetHeader.APCParameters.InterestValue
            _DifferentDay = DateDiff(DateInterval.Day, Me.OrderInHeader.DateExecution, Me.APCSheetByProducts.FundingDate)
            If _DifferentDay > 0 And _InterestValue > 0 Then
                If ListPriceOriginalHUF = 0 Then
                    ListPriceOriginalHUF = ListPriceHUF
                End If
                ListPriceHUF = ListPriceOriginalHUF + Math.Round(_DifferentDay / 36000 * _InterestValue * ListPriceOriginalHUF, 2, MidpointRounding.AwayFromZero)
            End If
        End If

        Me.SumPriceNettoHUF = Math.Round(FinalPriceHUF * QTT, 0, MidpointRounding.AwayFromZero)
        Me.SumPriceNettoDEV = Math.Round(FinalPriceDEV * QTT, 2, MidpointRounding.AwayFromZero)

        Me.SumPriceVATHUF = Math.Round(Me.SumPriceNettoHUF * (_VATValue), 0, MidpointRounding.AwayFromZero)
        Me.SumPriceVATDEV = Math.Round(Me.SumPriceNettoDEV * (_VATValue), 2, MidpointRounding.AwayFromZero)

        Me.SumPriceBruttoHUF = Me.SumPriceNettoHUF + Me.SumPriceVATHUF
        Me.SumPriceBruttoDEV = Me.SumPriceNettoDEV + Me.SumPriceVATDEV
    End Sub
    <Appearance("Hide DEV properties if HUF", "ViewItem", targetitems:="ListPriceDEV;DiscountPriceDEV;FinalPriceDEV", Context:="ListView", _
        visibility:=DevExpress.ExpressApp.Editors.ViewItemVisibility.Hide)> _
    Public Function RuleHUFMethod() As Boolean
        If Me.OrderInHeader Is Nothing Then
            Return False
        Else
            If Me.OrderInHeader.OrderInParameters Is Nothing Then
                Return False
            Else
                If Me.OrderInHeader.OrderInParameters.CurrencyName Is Nothing Then
                Else
                    If Me.OrderInHeader.OrderInParameters.CurrencyName.ShortName = "HUF" Then
                        Return True
                    Else
                        Return False
                    End If
                End If
            End If
        End If

    End Function
End Class

