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.SystemModule
Imports DevExpress.Persistent.Base.Security
Imports DevExpress.ExpressApp.ConditionalAppearance
Imports DevExpress.ExpressApp.Filtering

<DefaultClassOptions()> _
<NavigationItem("Contracts")> _
<Appearance("Hide ReportHeader in COC when IsEditable=False", AppearanceItemType:="ViewItem", TargetItems:="ReportHeader;ReportFooter", Criteria:="IsEditable=False", Visibility:=DevExpress.ExpressApp.Editors.ViewItemVisibility.Hide)> _
Public Class CertificateofContractHeader
    Inherits BaseObject
    Private _Contracts As Contracts
    Private _DateExecution As Date
    Private _DocumentNumber As String
    Private _IsStorno As Boolean = False
    Private _IsEditable As Boolean = True
    Private _DateCreated As Date
    Private _ReportHeader As String
    Private _ReportFooter As String
    Private _ObjectCreated As Date
    Private _UserCreated As User
    Private _AmountHUF As Double
    Private _AmountDEV As Double
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If DocumentNumber Is Nothing Then
            If _Contracts IsNot Nothing Then
                If _Contracts.ContractTemplate.COC_NumberGenerator IsNot Nothing Then
                    _DocumentNumber = _Contracts.ContractTemplate.COC_NumberGenerator.GetNewNumber(_Contracts.ContractTemplate.COC_NumberGenerator)
                End If
            End If
        End If
        If Session.IsNewObject(Me) Then
            ObjectCreated = GetSQLTime(Session)
            UserCreated = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
        End If
    End Sub
    <RuleRequiredField("CertificateofContractHeader-Contracts", DefaultContexts.Save)> _
    <DataSourceCriteria("ContractTemplate.PartnerType=1 AND ContractTemplate.IsTechnical=False AND (State = 1 OR State = 2)")> _
    Property Contracts() As Contracts
        Get
            Return _Contracts

        End Get
        Set(ByVal value As Contracts)
            SetPropertyValue("Contracts", _Contracts, value)
        End Set
    End Property
    <RuleRequiredField("CertificateofContractHeader-DateExecution", DefaultContexts.Save)> _
    Property DateExecution() As Date
        Get
            Return _DateExecution
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateExecution", _DateExecution, value)
        End Set
    End Property
    <NonCloneable()> _
    Property DocumentNumber As String
        Get
            Return _DocumentNumber
        End Get
        Set(ByVal value As String)
            SetPropertyValue("DocumentNumber", _DocumentNumber, value)
        End Set
    End Property
    Property IsStorno As Boolean
        Get
            Return _IsStorno
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsStorno", _IsStorno, value)
        End Set
    End Property
    Property IsEditable As Boolean
        Get
            Return _IsEditable
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsEditable", _IsEditable, value)
        End Set
    End Property
    <Size(-1)> _
    Property ReportHeader As String
        Get
            Return _ReportHeader
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ReportHeader", _ReportHeader, value)
        End Set
    End Property
    <Size(-1)> _
    Property ReportFooter As String
        Get
            Return _ReportFooter
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ReportFooter", _ReportFooter, 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
    <NonCloneable> _
    Property UserCreated As User
        Get
            Return _UserCreated
        End Get
        Set(value As User)
            SetPropertyValue("UserCreated", _UserCreated, value)
        End Set
    End Property
    Property AmountHUF As Double
        Get
            Return _AmountHUF
        End Get
        Set(value As Double)
            SetPropertyValue("AmountHUF", _AmountHUF, value)
        End Set
    End Property
    Property AmountDEV As Double
        Get
            Return _AmountDEV
        End Get
        Set(value As Double)
            SetPropertyValue("AmountDEV", _AmountDEV, value)
        End Set
    End Property

    ReadOnly Property DisplayName As String
        Get
            If Contracts IsNot Nothing Then
                Return DocumentNumber & ", " & Contracts.DisplayName
            Else
                Return "<>"
            End If
        End Get
    End Property
    <Association("CertificateofContractHeader-CertificateofContractRows", GetType(CertificateofContractRows)), VisibleInListView(False)> _
    ReadOnly Property CertificateofContractRows As XPCollection(Of CertificateofContractRows)
        Get
            Return GetCollection(Of CertificateofContractRows)("CertificateofContractRows")
        End Get
    End Property
    Sub Recalculate(_uow As UnitOfWork, _CertificateofContractHeader As CertificateofContractHeader, Optional _WidthCommit As Boolean = False)
        Dim _CertificateofContractRows As CertificateofContractRows
        Dim _AmountHUF As Double = 0
        Dim _AmountDEV As Double = 0
        On Error GoTo CheckError

        _CertificateofContractHeader.AmountHUF = 0
        _CertificateofContractHeader.AmountDEV = 0
        For Each _CertificateofContractRows In _CertificateofContractHeader.CertificateofContractRows
            _AmountHUF += _CertificateofContractRows.ContractRows.QTT * _CertificateofContractRows.ContractRows.NettoPriceHUF
            _AmountDEV += _CertificateofContractRows.ContractRows.QTT * _CertificateofContractRows.ContractRows.NettoPriceDEV
        Next
        _CertificateofContractHeader.AmountHUF = _AmountHUF
        _CertificateofContractHeader.AmountDEV = _AmountDEV

        If _WidthCommit Then _uow.CommitChanges()
        Exit Sub

CheckError:

        If Err.Number = 13 Then Resume Next
        If Err.Number = 91 Then Resume Next
    End Sub

End Class
