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(False), CreatableItem(False)> _
Public Class OrderInRowsOther 'Egyéb sorok
    Inherits BaseObject
    Private _OrderInHeader As OrderInHeader
    Private _OfferOutRows As OfferOutRows
    Private _RowIndex As Long = 0 'Számla sorindex
    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 _ShortName As String 'Megnevezés
    Private _Description As String 'Leírás
    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 _QTT As Double = 0 'Mennyiség
    Private _QTT_Invoiced As Double = 0 '-- Kiszámlázott mennyiség
    Private _QTT_Process As Double = 0 '-- Kézzel beírható mennyiség a végrehajtáshoz (pld. mennyi legyen kitárolva ...)
    Private _Units As Units ' Mennyiségi egység
    Private _VAT As VAT 'ÁFA
    Private _QualityOption As QualityOption 'Minőségi kondíció
    Private _UniqueObjects As UniqueObjects ' -- Egyedi azonosító
    Private _CostHolder As CostHolder ' -- Költség viselő
    Private _CostPlace As CostPlace ' -- Költség viselő
    Private _JobNumberObjects As JobNumberObjects ' -- Project
    Private _CustomsTariffs As CustomsTariffs 'SZJ szám
    '//Összesítések miatt

    Private _ListPriceOriginalHUF As Double = 0

    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 !!!

    '// Munka !!
    Private _WorkState As WorkState 'Munkafolyamatokat tartalmazó szabad bázis
    Private _WorkStateGroup As String 'Sorrendiséget, csoportosítást meghatározó mező
    Private _WorkStateExpiration As Date 'Határidő
    Private _WorkStateStatus As DevExpress.Persistent.Base.General.TaskStatus

    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
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        Me.RecalculateRows()
    End Sub
    Protected Overrides Sub OnSaved()
        MyBase.OnSaved()
        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


    <Association("OrderInHeader-OrderInRowsOther", GetType(OrderInHeader))> _
    Property OrderInHeader As OrderInHeader
        Get
            Return _OrderInHeader
        End Get
        Set(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 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
    <RuleRequiredField("OrderInRowsOther-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
    Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Description", _Description, 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
    <RuleRequiredField("OrderInRowsOther-QTT", DefaultContexts.Save)> _
    Property QTT() As Double
        Get
            Return _QTT
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("QTT", _QTT, value)
        End Set
    End Property
    <RuleRange("OrderInRowsOther.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 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
    <RuleRequiredField("OrderInRowsOther-Units", DefaultContexts.Save)> _
    Property Units() As Units
        Get
            Return _Units
        End Get
        Set(ByVal value As Units)
            SetPropertyValue("Units", _Units, value)
        End Set
    End Property
    <RuleRequiredField("OrderInRowsOther-VAT", DefaultContexts.Save)> _
    Property VAT() As VAT
        Get
            Return _VAT
        End Get
        Set(ByVal value As VAT)
            SetPropertyValue("VAT", _VAT, 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 UniqueObjects As UniqueObjects
        Get
            Return _UniqueObjects
        End Get
        Set(ByVal value As UniqueObjects)
            SetPropertyValue("UniqueObjects", _UniqueObjects, value)
        End Set
    End Property
    Property CostHolder As CostHolder
        Get
            Return _CostHolder
        End Get
        Set(ByVal value As CostHolder)
            SetPropertyValue("CostHolder", _CostHolder, value)
        End Set
    End Property
    Property CostPlace As CostPlace
        Get
            Return _CostPlace
        End Get
        Set(ByVal value As CostPlace)
            SetPropertyValue("CostPlace", _CostPlace, value)
        End Set
    End Property
    Property JobNumberObjects As JobNumberObjects
        Get
            Return _JobNumberObjects
        End Get
        Set(ByVal value As JobNumberObjects)
            SetPropertyValue("JobNumberObjects", _JobNumberObjects, value)
        End Set
    End Property
    Property CustomsTariffs As CustomsTariffs
        Get
            Return _CustomsTariffs
        End Get
        Set(value As CustomsTariffs)
            SetPropertyValue("CustomsTariffs", _CustomsTariffs, 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
    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

    '// Munkafolyamat miatt
    Property WorkState As WorkState
        Get
            Return _WorkState
        End Get
        Set(value As WorkState)
            SetPropertyValue("WorkState", _WorkState, value)
        End Set
    End Property
    Property WorkStateGroup As String
        Get
            Return _WorkStateGroup
        End Get
        Set(value As String)
            SetPropertyValue("WorkStateGroup", _WorkStateGroup, value)
        End Set
    End Property
    Property WorkStateExpiration As Date
        Get
            Return _WorkStateExpiration
        End Get
        Set(value As Date)
            SetPropertyValue("WorkStateExpiration", _WorkStateExpiration, value)
        End Set
    End Property
    Property WorkStateStatus As DevExpress.Persistent.Base.General.TaskStatus
        Get
            Return _WorkStateStatus
        End Get
        Set(value As DevExpress.Persistent.Base.General.TaskStatus)
            SetPropertyValue("WorkStateStatus", _WorkStateStatus, value)
        End Set
    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


    Public Sub AddRows(ByVal _p_OrderInHeader As OrderInHeader)
        ' Sor hozzáadása a fejléchez

        Me.OrderInHeader = _p_OrderInHeader
        Me.CustomsTariffs = _p_OrderInHeader.row_o_CustomsTariffs
        Me.ShortName = _p_OrderInHeader.row_o_ShortName
        Me.QTT = _p_OrderInHeader.row_o_QTT
        Me.Description = _p_OrderInHeader.row_o_Description
        Me.ListPriceDEV = _p_OrderInHeader.row_o_ListPriceDEV
        Me.ListPriceHUF = _p_OrderInHeader.row_o_ListPriceHUF
        Me.Units = _p_OrderInHeader.row_o_Units
        Me.VAT = _p_OrderInHeader.row_o_VAT
        Me.DiscountPriceDEV = Math.Round(((_p_OrderInHeader.row_o_DiscountPercent / 100) * Me.ListPriceDEV), 2, MidpointRounding.AwayFromZero)
        Me.DiscountPriceHUF = Math.Round(((_p_OrderInHeader.row_o_DiscountPercent / 100) * Me.ListPriceHUF), 2, MidpointRounding.AwayFromZero)
        Me.APCSheetByProducts = _p_OrderInHeader.row_o_APCSheetByProducts

        If _p_OrderInHeader.OrderInRowsOther.Count = 0 Then
            Me.RowIndex = 0
        Else
            Me.RowIndex = _p_OrderInHeader.OrderInRowsOther.Count - 1
        End If

        Me.Save()
    End Sub
    Public Sub RemoveRows(ByVal _p_OrderInHeader As OrderInHeader)
        If _p_OrderInHeader.IsEditable = True Then

            For Each _OrderInRowsOther As OrderInRowsOther In _p_OrderInHeader.OrderInRowsOther
                If _OrderInRowsOther.RowIndex > Me.RowIndex Then
                    _OrderInRowsOther.RowIndex -= 1
                End If
            Next

            ' Me.Delete()
        End If
    End Sub

    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
End Class
