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 System.Linq
Imports System.Linq.Expressions

<DefaultClassOptions()> _
<NavigationItem(False), CreatableItem(False)> _
Public Class OrderInAcknowledgementHeader
    Inherits BaseObject
    Private _OrderInHeader As OrderInHeader
    Private _DateCreated As Date
    Private _DocumentNumber As String
    Private _ShipmentOption As ShipmentOption
    Private _PaymentOption As PaymentOption
    Private _NoteHeader As String
    Private _UserCreated As User
    Private _ObjectCreated As Date
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        Me.ObjectCreated = GetSQLTime(Session)
        Me.UserCreated = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
    End Sub
    <Association("OrderInHeader-OrderInAcknowledgementHeader", GetType(OrderInHeader))> _
    Property OrderInHeader As OrderInHeader
        Get
            Return _OrderInHeader
        End Get
        Set(value As OrderInHeader)
            SetPropertyValue("OrderInHeader", _OrderInHeader, value)
        End Set
    End Property
    Property DateCreated As Date
        Get
            Return _DateCreated
        End Get
        Set(value As Date)
            SetPropertyValue("DateCreated", _DateCreated, value)
        End Set
    End Property
    Property DocumentNumber As String
        Get
            Return _DocumentNumber
        End Get
        Set(value As String)
            SetPropertyValue("DocumentNumber", _DocumentNumber, value)
        End Set
    End Property
    Property ShipmentOption As ShipmentOption
        Get
            Return _ShipmentOption
        End Get
        Set(value As ShipmentOption)
            SetPropertyValue("ShipmentOption", _ShipmentOption, value)
        End Set
    End Property
    Property PaymentOption As PaymentOption
        Get
            Return _PaymentOption
        End Get
        Set(value As PaymentOption)
            SetPropertyValue("PaymentOption", _PaymentOption, value)
        End Set
    End Property
    <Size(-1)> _
    Property NoteHeader As String
        Get
            Return _NoteHeader
        End Get
        Set(value As String)
            SetPropertyValue("NoteHeader", _NoteHeader, value)
        End Set
    End Property
    <NonCloneable> _
    Property UserCreated As User
        Get
            Return _UserCreated
        End Get
        Set(value As User)
            SetPropertyValue("UserCreated", _UserCreated, value)
        End Set
    End Property
    <NonCloneable> _
    Property ObjectCreated As Date
        Get
            Return _ObjectCreated
        End Get
        Set(value As Date)
            SetPropertyValue("ObjectCreated", _ObjectCreated, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    <Association("OrderInAcknowledgementHeader-OrderInAcknowledgementRows", GetType(OrderInAcknowledgementRows))> _
    ReadOnly Property OrderInAcknowledgementRows As XPCollection(Of OrderInAcknowledgementRows)
        Get
            Return GetCollection(Of OrderInAcknowledgementRows)("OrderInAcknowledgementRows")
        End Get
    End Property
    <VisibleInListView(False)> _
    <Association("OrderInAcknowledgementHeader-OrderInAcknowledgementVAT", GetType(OrderInAcknowledgementVAT))> _
    ReadOnly Property OrderInAcknowledgementVAT As XPCollection(Of OrderInAcknowledgementVAT)
        Get
            Return GetCollection(Of OrderInAcknowledgementVAT)("OrderInAcknowledgementVAT")
        End Get
    End Property
    Sub RecalculateOrderInAcknowledgement(ByVal _OrderInAcknowledgementHeader As OrderInAcknowledgementHeader, ByVal _ocur As Xpo.XPObjectSpace)
        '-- A rutin újragenerálja a pénzügyi sorokat
        Dim _OrderInAcknowledgementVAT As OrderInAcknowledgementVAT
        Dim i As Long = 1
        Dim j As Long = 1
        Dim _Exists As Boolean = False

        Session.Delete(_OrderInAcknowledgementHeader.OrderInAcknowledgementVAT)
        Dim OIAR = New XPQuery(Of OrderInAcknowledgementRows)(_ocur.Session)
        Dim query = From invoice In OIAR
                    Where invoice.OrderInAcknowledgementHeader.Oid = _OrderInAcknowledgementHeader.Oid _
            Group invoice By invoice.VAT Into g = Group _
            Select New With {Key .VAT = VAT, _
                             Key .AmountNettoHUF = g.Sum(Function(inv) (inv.FinalPriceHUF) * inv.QTT), _
                             Key .AmountNettoDEV = g.Sum(Function(inv) (inv.FinalPriceDEV) * inv.QTT)}
        For Each item In query
            _OrderInAcknowledgementVAT = _ocur.CreateObject(Of OrderInAcknowledgementVAT)()
            _OrderInAcknowledgementVAT.OrderInAcknowledgementHeader = _OrderInAcknowledgementHeader
            _OrderInAcknowledgementVAT.VAT = item.VAT
            _OrderInAcknowledgementVAT.AmountNettoHUF = Math.Round(item.AmountNettoHUF, 0, MidpointRounding.AwayFromZero)
            _OrderInAcknowledgementVAT.AmountVATHUF = 0
            If item.VAT IsNot Nothing Then
                _OrderInAcknowledgementVAT.AmountVATHUF = Math.Round(item.VAT.KeyValue * _OrderInAcknowledgementVAT.AmountNettoHUF, 0, MidpointRounding.AwayFromZero)
            End If
            _OrderInAcknowledgementVAT.AmountBruttoHUF = _OrderInAcknowledgementVAT.AmountNettoHUF + _OrderInAcknowledgementVAT.AmountVATHUF

            _OrderInAcknowledgementVAT.AmountNettoDEV = Math.Round(item.AmountNettoDEV, 2, MidpointRounding.AwayFromZero)
            _OrderInAcknowledgementVAT.AmountVATDEV = 0
            If item.VAT IsNot Nothing Then
                _OrderInAcknowledgementVAT.AmountVATDEV = Math.Round(item.VAT.KeyValue * _OrderInAcknowledgementVAT.AmountNettoDEV, 2, MidpointRounding.AwayFromZero)
            End If
            _OrderInAcknowledgementVAT.AmountBruttoDEV = _OrderInAcknowledgementVAT.AmountNettoDEV + _OrderInAcknowledgementVAT.AmountVATDEV

            _OrderInAcknowledgementVAT.Save()
            _OrderInAcknowledgementHeader.OrderInAcknowledgementVAT.Add(_OrderInAcknowledgementVAT)
        Next
        _OrderInAcknowledgementHeader.Save()
    End Sub

End Class
