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.Persistent.Base.General
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Linq
Imports System.Linq.Expressions
Imports DevExpress.ExpressApp.ConditionalAppearance

'<EditorStateRuleAttribute("Hide DEV properties when HUF is selected in GeneralLedger", "CurrencyRate; row_AmountDEV", EditorState.Hidden, "CurrencyName.ShortName = 'HUF' and GLDocumentType in (0,1,3,4,5)", ViewType.DetailView)> _
'<EditorStateRule("Hide CustomersFrom", "CustomersFrom", EditorState.Disabled, "(IsNull(CustomersFrom))=False", ViewType.DetailView)> _

<DefaultClassOptions()> _
<NavigationItem(False), CreatableItem(False)> _
<Appearance("Hide DEV properties when HUF is selected in GeneralLedger", _
                        TargetItems:="CurrencyRate; row_AmountDEV", _
                        Criteria:="CurrencyName.ShortName = 'HUF' and GLDocumentType in (0,1,3,4,5)", _
                        Visibility:=Editors.ViewItemVisibility.Hide, _
                        Context:="DetailView")> _
<Appearance("Hide CustomersFrom",
                        TargetItems:="CustomersFrom",
                        Criteria:="(IsNull(CustomersFrom))=False",
                        Enabled:=False,
                        Context:="DetailView")> _
<Appearance("Hide row_ info when IsEditable=False",
                        AppearanceItemType:="ViewItem",
                        criteria:="IsEditable=False",
                        TargetItems:="row_GLHeader; row_DebitChartOfAccounts; row_CreditChartOfAccounts; row_AmountHUF; row_AmountDEV; row_LiquidAssets;" + _
                                     " row_Customer; row_DateVAT; row_VAT; row_NoteRows; row_UniqueObjects; row_Controlling; row_JobNumberObjects; " + _
                                     " row_CostPlace; row_CostHolder; row_ConnectInfo;",
                        visibility:=Editors.ViewItemVisibility.Hide)> _
<RuleCriteria("RuleCriteria GLHeader-row_VAT-GLCassa", "aToTable_GLCassa_Contexts", "row_VAT.InversState=False", "Fordított ÁFA nem választható ki !")> _
<RuleCriteria("RuleCriteria GLHeader-row_Controlling", "aToTable_GLAccounts_Contexts", _
    "(row_Controlling.IsParent=False) or (Rule5DMatrix.RequireControlling=False and isnull(row_Controlling)=True)", _
    "Gyüjtő kontrollszám nem választható ki!", SkipNullOrEmptyValues:=False)> _
<RuleCriteria("RuleCriteria GLHeader-row_DebitChartOfAccounts", "aToTable_GLMixed_Contexts", "row_DebitChartOfAccounts.Children.Count=0", "Csak gyermek objektum választható ki!", SkipNullOrEmptyValues:=False)> _
<RuleCriteria("RuleCriteria GLHeader-row_CreditChartOfAccounts", "aToTable_GLMixed_Contexts", "row_CreditChartOfAccounts.Children.Count=0", "Csak gyermek objektum választható ki!", SkipNullOrEmptyValues:=False)> _
<RuleCriteria("RuleCriteria GLHeader-ChartOfAccountsYearState-Save", "*", _
    "ChartOfAccountsYearState=False", _
    "Zárolt főkönyvi év, nem lehet bizonylatot rögzíteni, módosítani! ", SkipNullOrEmptyValues:=True)> _
<DefaultProperty("DocumentNumber")> _
Public Class GLHeader
    Inherits BaseObject
    Private _GLDocumentType As eGLDocumentType 'Bizonylat tipusa
    Private _CustomersFrom As CustomersFrom 'Saját c g
    Private _CurrencyName As CurrencyName ' devizanem
    Private _CurrencyRate As Double = 1 'Deviza árfolyam
    Private _DocumentNumber As String 'Bizonylatszám, számlaszám
    Private _RegistryNumber As String 'Iktatószám
    Private _DateCreated As Date 'Kiállítás dátuma
    Private _DateExecution As Date 'Teljesítés dátuma
    Private _DatePayment As Date 'Fizetési határidő
    Private _NoteHeader As String 'Fejléc megjegyzés
    Private _IsEditable As Boolean = True 'Szerkeszthető
    Private _IsStorno As Boolean = False 'Stornó bizonylat
    Private _ImportedInvoice As InvoiceHeader 'melyik vevõi számlából készült
    Private _ImportedRegistryHeader As RegistryHeader 'Melyik szállítói iktatásból jött
    Private _ImportedOther As Guid 'Egyéb importált azonosító
    Private _HUNumberToText As String ' Azaz string !!!
    Private _CashHandler As String ' Pénztárnál befizető vagy átvevő

    Private _GLOpenCloseHeader As GLOpenCloseHeader 'Melyik év végi záráshoz tartozik !

    ' Beviteli mezők

    Private _row_GLHeader As GLHeader
    Private _row_DebitChartOfAccounts As ChartOfAccounts 'Tartozik számlaszám
    Private _row_CreditChartOfAccounts As ChartOfAccounts ' Követel számlaszám
    Private _row_AmountHUF As Double ' összeg forintban
    Private _row_AmountDEV As Double ' összeg devizában
    Private _row_LiquidAssets As LiquidAssets ' pénzeszköz
    Private _row_Customer As Customers ' partner
    Private _row_DateVAT As Date 'sor dátum
    Private _row_VAT As VAT
    Private _row_NoteRows As String

    Private _row_UniqueObjects As UniqueObjects 'Egyedi azonosító
    Private _row_Controlling As Controlling 'Kontrolling szám
    Private _row_JobNumberObjects As JobNumberObjects 'Munkaszám
    Private _row_CostPlace As CostPlace 'Költséghely
    Private _row_CostHolder As CostHolder 'Költségviselõ
    Private _row_ConnectInfo As String 'Folyószámla analitika kötési szám

    Private _IsRegistryGLAccounts As Boolean 'Rögzítés után van automatikus iktatás
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        CurrencyName = Session.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", "HUF"))
        If Session.IsNewObject(Me) Then
            Me.GLDocumentType = eGLDocumentType.eGLMixed
            IsEditable = True
            IsStorno = False
        End If

    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()

        If Session.IsObjectToDelete(Me) = False Then
            If Me.GLRows.Count > 0 Then
                ReorderRows()
            End If
        End If
    End Sub
    Private Sub ReorderRows()
        Dim _GLRows As GLRows
        Dim _GLRows_New As GLRows = Nothing
        Dim _RowIndex As Long = 0
        Dim _GLRows_Array As New ArrayList
        '//------------------------------------------------------------------------------------------
        Me.GLRows.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))
        For Each _GLRows In Me.GLRows
            If Session.IsNewObject(_GLRows) Then
                _GLRows_New = _GLRows
            Else
                _GLRows_Array.Add(_GLRows)
            End If
        Next

        If _GLRows_New IsNot Nothing Then
            _GLRows_Array.Add(_GLRows_New)
        End If

        For Each _GLRows In _GLRows_Array
            If _GLRows.RowIndex <> _RowIndex Then
                _GLRows.RowIndex = _RowIndex
                _GLRows.Save()
            End If
            _RowIndex += 1
        Next
    End Sub
    Private Sub RecalculateAmountHUF()
        If Me.row_AmountDEV <> 0 Then
            If GLDocumentType = 0 Or GLDocumentType = 1 Or GLDocumentType = 3 Or GLDocumentType = 4 Or GLDocumentType = 5 Then
                Me.row_AmountHUF = Math.Round(Me.row_AmountDEV * CurrencyRate, 2, MidpointRounding.AwayFromZero)
            End If
        Else
            Me.row_AmountHUF = 0
        End If
    End Sub
    <VisibleInListView(False)> _
    <Association("GLHeader-GLFileData", GetType(GLFileData)), Aggregated(), ExpandObjectMembers(ExpandObjectMembers.Never)> _
    ReadOnly Property GLFileData As XPCollection(Of GLFileData)
        Get
            Return GetCollection(Of GLFileData)("GLFileData")
        End Get
    End Property
    '<RuleValueComparison("", DefaultContexts.Save, ValueComparisonType.GreaterThan, "0")> _
    ReadOnly Property GLRowsCount() As Long
        Get
            Return GLRows.Count
        End Get
    End Property
    Property GLDocumentType() As eGLDocumentType
        Get
            Return _GLDocumentType
        End Get
        Set(ByVal value As eGLDocumentType)
            SetPropertyValue("GLDocumentType", _GLDocumentType, value)
        End Set
    End Property
    <RuleRequiredField("GLAccounts-CustomersFrom", "aFinal_GLAccount_Context")> _
    <RuleRequiredField("GLCassa-CustomersFrom", "aFinal_GLCassa_Context")> _
    <RuleRequiredField("GLCassa-CustomersFrom1", "aFinalAndNew_GLCassa_Context")> _
    <RuleRequiredField("GLMixed-CustomersFrom", "aFinal_GLMixed_Context")> _
    <ImmediatePostData()> _
    Property CustomersFrom() As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(ByVal value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    <RuleRequiredField("GLHeader.DocumentNumber.GLAccounts", "aFinal_GLAccount_Context")> _
    <RuleRequiredField("GLHeader.DocumentNumber.GLBank.Final", "aFinal_GLBank_Context")> _
    <RuleRequiredField("GLHeader.DocumentNumber.GLBank.FinalAndNew", "aFinalAndNew_GLBank_Context")> _
    <ImmediatePostData(True)> _
    <NonCloneable()> _
    Overridable Property DocumentNumber() As String
        Get
            Return _DocumentNumber
        End Get
        Set(ByVal value As String)
            SetPropertyValue("DocumentNumber", _DocumentNumber, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If Me.GLDocumentType = eGLDocumentType.eGLAccount Then
                    row_ConnectInfo = DocumentNumber
                End If
            End If
        End Set
    End Property
    <NonCloneable()> _
    Property RegistryNumber() As String
        Get
            Return _RegistryNumber
        End Get
        Set(ByVal value As String)
            SetPropertyValue("RegistryNumber", _RegistryNumber, value)
        End Set
    End Property
    <RuleRequiredField("GLHeader.DateCreated", "aFinal_GLAccount_Context")> _
    Property DateCreated() As Date
        Get
            Return _DateCreated
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateCreated", _DateCreated, value)
        End Set
    End Property
    <RuleRequiredField("GLHeader.DateExecution", "aFinal_GLAccount_Context")> _
    <RuleRequiredField("GLBank.DateExecution.Final", "aFinalAndNew_GLBank_Context")> _
    <RuleRequiredField("GLBank.DateExecution.FinalAndNew", "aFinal_GLBank_Context")> _
    <RuleRequiredField("GLMixed-DateExecution", "aFinal_GLMixed_Context")> _
    <RuleRequiredField("GLHeader.DateExecution.Defaultcontexts.Save", DefaultContexts.Save)> _
    Property DateExecution() As Date
        Get
            Return _DateExecution
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateExecution", _DateExecution, value)
            If row_DateVAT = Nothing Then
                row_DateVAT = value
            End If
            If TryCast(Me, GLBank) IsNot Nothing Then
                If Me.row_LiquidAssets IsNot Nothing Then
                    TryCast(Me, GLBank).BallanceHUF = TryCast(Me, GLBank).GetBallanceHUF(Me.row_LiquidAssets, value)
                    TryCast(Me, GLBank).BallanceDEV = TryCast(Me, GLBank).GetBallanceDEV(Me.row_LiquidAssets, value)
                End If
            End If
        End Set
    End Property
    ReadOnly Property DateExecutionYear As String
        Get
            Return DateExecution.Date.Year.ToString
        End Get
    End Property
    ReadOnly Property DateExecutionQuarter As String
        Get
            Return AlignStr(DatePart(DateInterval.Quarter, DateExecution).ToString, eDirection.Left, " ", 2) & ". NÉ."
        End Get
    End Property
    ReadOnly Property DateExecutionMonth As String
        Get
            Return String.Format("{0}. {1}", AlignStr(DateExecution.Date.Month.ToString, eDirection.Left, " ", 2), Format(DateExecution, "MMMM"))
        End Get
    End Property
    ReadOnly Property DateExecutionWeekOfYear As String
        Get
            Return AlignStr(DatePart(DateInterval.WeekOfYear, DateExecution), eDirection.Left, " ", 2) & ". hét"
        End Get
    End Property
    ReadOnly Property DateExecutionString As Date
        Get
            Return DateExecution.Date.ToString("d")
        End Get
    End Property
    <RuleRequiredField("GLHeader.DatePayment", "aFinal_GLAccount_Context")> _
    Property DatePayment() As Date
        Get
            Return _DatePayment
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DatePayment", _DatePayment, value)
        End Set
    End Property
    <Size(-1)> _
    Property NoteHeader() As String
        Get
            Return _NoteHeader
        End Get
        Set(ByVal value As String)
            SetPropertyValue("NoteHeader", _NoteHeader, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If String.IsNullOrEmpty(value) And String.IsNullOrEmpty(row_NoteRows) = True Then
                    row_NoteRows = value
                End If
            End If
        End Set
    End Property
    <RuleRequiredField("GLHeader.CurrencyName", "aFinal_GLAccount_Context")> _
    Property CurrencyName() As CurrencyName
        Get
            Return _CurrencyName
        End Get
        Set(ByVal value As CurrencyName)
            SetPropertyValue("CurrencyName", _CurrencyName, value)
        End Set
    End Property
    <RuleRange("GLHeader.CurrencyName.ToTable", "aToTable_GLAccounts_Contexts", 2, 999999999999999999, TargetCriteria:="CurrencyName.ShortName != 'HUF'")> _
    <ImmediatePostData(True)> _
    Property CurrencyRate() As Double
        Get
            'If CurrencyName.ShortName = "HUF" Then
            'Return 1
            'Else
            Return _CurrencyRate
            'End If
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("CurrencyRate", _CurrencyRate, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                Me.RecalculateAmountHUF()
            End If
        End Set
    End Property
    <NonCloneable()> _
    <ImmediatePostData()> _
    Property IsEditable() As Boolean
        Get
            Return _IsEditable
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsEditable", _IsEditable, value)
        End Set
    End Property
    <NonCloneable()> _
    Property IsStorno() As Boolean
        Get
            Return _IsStorno
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsStorno", _IsStorno, value)
        End Set
    End Property
    Property HUNumberToText As String
        Get
            Return _HUNumberToText
        End Get
        Set(ByVal value As String)
            SetPropertyValue("HUNumberToText", _HUNumberToText, value)
        End Set
    End Property
    <Size(-1)> _
    <RuleRequiredField("CashType-GLHeader", "aFinal_GLCassa_Context", Targetcriteria:="GLDocumentType=3")> _
    <RuleRequiredField("CashType-GLHeader-FN", "aFinalAndNew_GLCassa_Context", Targetcriteria:="GLDocumentType=3")> _
    Property CashHandler As String
        Get
            Return _CashHandler
        End Get
        Set(ByVal value As String)
            SetPropertyValue("CashHandler", _CashHandler, value)
        End Set
    End Property
    Property ImportedInvoice As InvoiceHeader
        Get
            Return _ImportedInvoice
        End Get
        Set(ByVal value As InvoiceHeader)
            SetPropertyValue("ImportedInvoice", _ImportedInvoice, value)
        End Set
    End Property
    Property ImportedRegistryHeader As RegistryHeader
        Get
            Return _ImportedRegistryHeader
        End Get
        Set(ByVal value As RegistryHeader)
            SetPropertyValue("ImportedRegistryHeader", _ImportedRegistryHeader, value)
        End Set
    End Property
    <Browsable(False)> _
    Property ImportedOther As Guid
        Get
            Return _ImportedOther
        End Get
        Set(ByVal value As Guid)
            SetPropertyValue("ImportedOther", _ImportedOther, value)
        End Set
    End Property
    <Browsable(False)> _
    Property GLOpenCloseHeader As GLOpenCloseHeader
        Get
            Return _GLOpenCloseHeader
        End Get
        Set(value As GLOpenCloseHeader)
            SetPropertyValue("GLOpenCloseHeader", _GLOpenCloseHeader, value)
        End Set
    End Property

    'Nonpersistent tulajdonságok a sorok rögzítéséhez
    <NonPersistent()> _
    <DataSourceCriteria("AccountYear=GetYear('@This.DateExecution')")> _
    <RuleRequiredField("GLMixed-row_DebitChartOfAccounts", "aToTable_GLMixed_Contexts")> _
    <VisibleInListView(False)> _
    Property row_DebitChartOfAccounts() As ChartOfAccounts
        Get
            Return _row_DebitChartOfAccounts
        End Get
        Set(ByVal value As ChartOfAccounts)
            SetPropertyValue("row_DebitChartOfAccounts", _row_DebitChartOfAccounts, value)
        End Set
    End Property
    <NonPersistent()> _
    <DataSourceCriteria("AccountYear=GetYear('@This.DateExecution')")> _
    <RuleRequiredField("GLMixed-row_CreditChartOfAccounts", "aToTable_GLMixed_Contexts")> _
    <VisibleInListView(False)> _
    Property row_CreditChartOfAccounts() As ChartOfAccounts
        Get
            Return _row_CreditChartOfAccounts
        End Get
        Set(ByVal value As ChartOfAccounts)
            SetPropertyValue("row_CreditChartOfAccounts", _row_CreditChartOfAccounts, value)
        End Set
    End Property
    <NonPersistent()> _
    <RuleValueComparison("GLMixed-row_AmountHUF", "aToTable_GLMixed_Contexts", ValueComparisonType.NotEquals, 0)> _
    <RuleValueComparison("GLCassa-row_AmountHUF", "aToTable_GLCassa_Contexts", ValueComparisonType.GreaterThan, 0)> _
    <RuleValueComparison("GLBank-row_AmountHUF", "aToTable_GLBank_Contexts", ValueComparisonType.GreaterThan, 0)> _
    <RuleValueComparison("GLCompensation-row_AmountHUF", "aToTable_GLCompensation_Contexts", ValueComparisonType.GreaterThan, 0)> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    Overridable Property row_AmountHUF() As Double
        Get
            Return _row_AmountHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("row_AmountHUF", _row_AmountHUF, value)
        End Set
    End Property
    <NonPersistent()> _
    <RuleValueComparison("GLMixed-row_AmountDEV", "aToTable_GLMixed_Contexts", ValueComparisonType.NotEquals, 0, TargetCriteria:="CurrencyName.ShortName != 'HUF'")> _
    <RuleValueComparison("GLCompensation-row_AmountDEV", "aToTable_GLCompensation_Contexts", ValueComparisonType.GreaterThan, 0, TargetCriteria:="CurrencyName.ShortName != 'HUF'")> _
    <ImmediatePostData(True)> _
        <VisibleInListView(False)> _
    Overridable Property row_AmountDEV() As Double
        Get
            Return _row_AmountDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("row_AmountDEV", _row_AmountDEV, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                Me.RecalculateAmountHUF()
            End If
        End Set
    End Property
    '<DataSourceCriteria("Iif('@this.GLDocumentType' = 2, True, False) == (LiquidAssetsType = 1)")> _
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_LiquidAssets() As LiquidAssets
        Get
            Return _row_LiquidAssets
        End Get
        Set(ByVal value As LiquidAssets)
            SetPropertyValue("row_LiquidAssets", _row_LiquidAssets, value)
        End Set
    End Property
    <NonPersistent()> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    Overridable Property row_Customer() As Customers
        Get
            Return _row_Customer
        End Get
        Set(ByVal value As Customers)
            SetPropertyValue("row_Customer", _row_Customer, value)
            If Session.IsObjectsSaving = False And Session.IsObjectsLoading = False Then
                If value IsNot Nothing Then
                    If CashHandler = "" Then CashHandler = value.Name
                End If
            End If
        End Set
    End Property
    <NonPersistent()> _
    <RuleRequiredField("GLHeader.row_DateVAT", "aToTable_GLAccounts_Contexts")> _
     <VisibleInListView(False)> _
    Property row_DateVAT() As Date
        Get
            Return _row_DateVAT
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("row_DateVAT", _row_DateVAT, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    <RuleRequiredField("GLHeader.row_VAT.ToTable", "aToTable_GLAccounts_Contexts")> _
    Property row_VAT() As VAT
        Get
            Return _row_VAT
        End Get
        Set(ByVal value As VAT)
            SetPropertyValue("row_VAT", _row_VAT, value)
        End Set
    End Property
    <NonPersistent()> _
    <Size(-1)> _
     <VisibleInListView(False)> _
    Property row_NoteRows() As String
        Get
            Return _row_NoteRows
        End Get
        Set(ByVal value As String)
            SetPropertyValue("row_NoteRows", _row_NoteRows, value)
        End Set
    End Property
    'Rule5DMatrix ...
    <NonPersistent()> _
    <VisibleInListView(False)> _
    <RuleRequiredField("GLHeader-row_Controlling-aToTable_GLAccounts_Contexts", "aToTable_GLAccounts_Contexts", TargetCriteria:="Rule5DMatrix.RequireControlling=True")> _
    Overridable Property row_Controlling() As Controlling
        Get
            Return _row_Controlling
        End Get
        Set(ByVal value As Controlling)
            SetPropertyValue("row_Controlling", _row_Controlling, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    <RuleRequiredField("GLHeader-row_CostPlace-aToTable_GLAccounts_Contexts", "aToTable_GLAccounts_Contexts", TargetCriteria:="Rule5DMatrix.RequireCostPlace=True")> _
    Property row_CostPlace() As CostPlace
        Get
            Return _row_CostPlace
        End Get
        Set(ByVal value As CostPlace)
            SetPropertyValue("row_CostPlace", _row_CostPlace, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    <RuleRequiredField("GLHeader-row_CostHolder-aToTable_GLAccounts_Contexts", "aToTable_GLAccounts_Contexts", TargetCriteria:="Rule5DMatrix.RequireCostHolder=True")> _
    Property row_CostHolder() As CostHolder
        Get
            Return _row_CostHolder
        End Get
        Set(ByVal value As CostHolder)
            SetPropertyValue("_row_CostHolder", _row_CostHolder, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    <RuleRequiredField("GLHeader-row_UniqueObjects-aToTable_GLAccounts_Contexts", "aToTable_GLAccounts_Contexts", TargetCriteria:="Rule5DMatrix.RequireUniqueObjects=True")> _
    Property row_UniqueObjects() As UniqueObjects
        Get
            Return _row_UniqueObjects
        End Get
        Set(ByVal value As UniqueObjects)
            SetPropertyValue("row_UniqueObjects", _row_UniqueObjects, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    <RuleRequiredField("GLHeader-row_JobNumberObjects-aToTable_GLAccounts_Contexts", "aToTable_GLAccounts_Contexts", TargetCriteria:="Rule5DMatrix.RequireJobNumberObjects=True")> _
    Property row_JobNumberObjects() As JobNumberObjects
        Get
            Return _row_JobNumberObjects
        End Get
        Set(ByVal value As JobNumberObjects)
            SetPropertyValue("row_JobNumberObjects", _row_JobNumberObjects, value)
        End Set
    End Property
    <Browsable(False)> _
    ReadOnly Property Rule5DMatrix As Rule5DMatrix
        Get
            Dim _Rule5DforGL As Rule5DforGL = Session.FindObject(Of Rule5DforGL)(CriteriaOperator.Parse("GLDocumentType=?", GLDocumentType))
            If _Rule5DforGL IsNot Nothing Then
                Return _Rule5DforGL.Rule5DMatrix
            Else
                Return Nothing
            End If
        End Get
    End Property

    <Browsable(False)>
    Property IsRegistryGLAccounts As Boolean
        Get
            Return _IsRegistryGLAccounts
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsRegistryGLAccounts", _IsRegistryGLAccounts, value)
        End Set
    End Property
    <NonPersistent()> _
    <RuleRequiredField("GLHeader.row_ConnectInfo", "aFinal_GLAccount_Context")> _
    <RuleRequiredField("GLHeader.row_ConnectInfo.GLCassa", "aToTable_GLCassa_Contexts", targetcriteria:="row_PartnerType<>-1")> _
    <ImmediatePostData(True)> _
        <VisibleInListView(False)> _
    Overridable Property row_ConnectInfo() As String
        Get
            Return _row_ConnectInfo
        End Get
        Set(ByVal value As String)
            SetPropertyValue("row_ConnectInfo", _row_ConnectInfo, value)
        End Set
    End Property

    <VisibleInListView(False)> _
    <Association("GLHeader", GetType(GLRows))> _
    Public ReadOnly Property GLRows() As XPCollection(Of GLRows)
        Get
            Return GetCollection(Of GLRows)("GLRows")
        End Get
    End Property


    Sub Reconfigure_GLDetail()
        '-- Az eljárás újragenerálja a GLDetail sorokat a késõbbi DTR elemzéshez

    End Sub

    <Browsable(False)> _
    ReadOnly Property ChartOfAccountsYearState As Boolean
        Get
            Dim _ChartOfAccountsYear As ChartOfAccountsYear = Session.FindObject(Of ChartOfAccountsYear)(CriteriaOperator.Parse("AccountYear=?", Me.DateExecution.Year))
            If _ChartOfAccountsYear Is Nothing Then
                Return False
            Else
                Return _ChartOfAccountsYear.IsClosed
            End If
        End Get
    End Property
    '-- Árfolyam veszteség, nyereség elszámolása
    Sub Reconfigure_RateDiff(ByVal _p_ocur As Xpo.XPObjectSpace, ByVal _p_GLHeader As GLHeader)
        Dim _GLHeader_Ratediff As GLMixed = Nothing
        Dim _GLCassa As GLCassa
        Dim _GLBank As GLBank
        Dim _GLCompensation As GLCompensation
        Dim _GLRows As GLRows
        Dim _GLRows_RateDiff As GLRows
        Dim _PCIDetail As PCIDetail
        Dim _CurrencyRate_Account As Double
        Dim _AmountHUF_RatedIff As Double
        Dim _DebitChartOfAccounts As ChartOfAccounts
        Dim _CreditChartOfAccounts As ChartOfAccounts
        Dim _Controlling As Controlling
        Dim _GLMixedNG As GLMixedNG
        Dim _GLHeader_RateDiffParameter = _p_ocur.FindObject(Of GLHeader_RateDiffParameter)(CriteriaOperator.Parse("1=1"))

        If _GLHeader_RateDiffParameter Is Nothing Then Exit Sub


        Dim _ChartOfAccounts_RateDiff_Win As ChartOfAccounts = _GLHeader_RateDiffParameter.GetChartOfAccounts(_p_GLHeader.DateExecution.Year, eWinLoss.eRateDiffWin)
        Dim _ChartOfAccounts_RateDiff_Loss As ChartOfAccounts = _GLHeader_RateDiffParameter.GetChartOfAccounts(_p_GLHeader.DateExecution.Year, eWinLoss.eRateDiffLoss)
        Dim _Controlling_RateDiff_Win As Controlling = _GLHeader_RateDiffParameter.Controlling_RateDiff_Win
        Dim _Controlling_RateDiff_Loss As Controlling = _GLHeader_RateDiffParameter.Controlling_RateDiff_Loss

        Dim _InAmountHUF As Double = 0
        Dim _OutAmountHUF As Double = 0
        Select Case _p_GLHeader.GLDocumentType
            Case eGLDocumentType.eGLBank
                _GLHeader_Ratediff = TryCast(_p_GLHeader, GLBank).GLHeader_RateDiff
                TryCast(_p_GLHeader, GLBank).GLHeader_RateDiff = Nothing
                TryCast(_p_GLHeader, GLBank).Save()
            Case eGLDocumentType.eGLCassa
                _GLHeader_Ratediff = TryCast(_p_GLHeader, GLCassa).GLHeader_RateDiff
                TryCast(_p_GLHeader, GLCassa).GLHeader_RateDiff = Nothing
                TryCast(_p_GLHeader, GLCassa).Save()
            Case eGLDocumentType.eGLCompensation
                _GLHeader_Ratediff = TryCast(_p_GLHeader, GLCompensation).GLHeader_RateDiff
                TryCast(_p_GLHeader, GLCompensation).GLHeader_RateDiff = Nothing
                TryCast(_p_GLHeader, GLCompensation).Save()
        End Select

        If _GLHeader_Ratediff IsNot Nothing Then
            _p_ocur.Delete(_GLHeader_Ratediff.GLRows)
            _p_ocur.Delete(_GLHeader_Ratediff)
        End If


        For Each _GLRows In _p_GLHeader.GLRows
            If _GLRows.CashType = eCashType.ePartner And _GLRows.Customer IsNot Nothing Then
                _PCIDetail = _p_ocur.FindObject(Of PCIDetail) _
                    (CriteriaOperator.Parse("CustomersFrom=? and Customers=? and PartnerType=? and ConnectInfo=? and MainType='01-Account' and CurrencyName.ShortName<>'HUF'", _
                    _p_GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo), True)
                If _PCIDetail IsNot Nothing Then
                    If _PCIDetail.AmountDEV_Account <> 0 Then
                        _CurrencyRate_Account = Math.Round(_PCIDetail.AmountHUF_Account / _PCIDetail.AmountDEV_Account, 4, MidpointRounding.AwayFromZero)
                        If _GLRows.AmountDEV <> 0 Then
                            _AmountHUF_RatedIff = 0
                            _AmountHUF_RatedIff = (_GLRows.AmountDEV * _CurrencyRate_Account) - _GLRows.AmountHUF
                            Select Case _GLRows.PartnerType
                                Case ePartnerType.ePayabels
                                    If _GLRows.OutAmountDEV > 0 Then 'Terhelés
                                        If _AmountHUF_RatedIff < 0 Then 'Veszteség szállítóval szemben
                                            _DebitChartOfAccounts = _ChartOfAccounts_RateDiff_Loss
                                            _CreditChartOfAccounts = _GLRows.DebitChartOfAccounts
                                            _InAmountHUF = Math.Abs(_AmountHUF_RatedIff)
                                            _OutAmountHUF = 0
                                        Else 'Nyereség szállítóval szemben
                                            _DebitChartOfAccounts = _GLRows.DebitChartOfAccounts
                                            _CreditChartOfAccounts = _ChartOfAccounts_RateDiff_Win
                                            _InAmountHUF = 0
                                            _OutAmountHUF = Math.Abs(_AmountHUF_RatedIff)
                                        End If

                                    ElseIf _GLRows.InAmountDEV > 0 Then 'Jóváírás
                                        If _AmountHUF_RatedIff < 0 Then 'Veszteség szállítóval szemben
                                            _DebitChartOfAccounts = _ChartOfAccounts_RateDiff_Loss
                                            _CreditChartOfAccounts = _GLRows.CreditChartOfAccounts
                                            _InAmountHUF = Math.Abs(_AmountHUF_RatedIff)
                                            _OutAmountHUF = 0
                                        Else 'Nyereség szállítóval szemben
                                            _DebitChartOfAccounts = _GLRows.CreditChartOfAccounts
                                            _CreditChartOfAccounts = _ChartOfAccounts_RateDiff_Win
                                            _InAmountHUF = 0
                                            _OutAmountHUF = Math.Abs(_AmountHUF_RatedIff)
                                        End If
                                    End If
                                Case ePartnerType.eReceivables
                                    If _GLRows.OutAmountDEV > 0 Then 'Terhelés
                                        If _AmountHUF_RatedIff > 0 Then 'Veszteség vevővel szemben
                                            _DebitChartOfAccounts = _ChartOfAccounts_RateDiff_Loss
                                            _CreditChartOfAccounts = _GLRows.DebitChartOfAccounts
                                            _InAmountHUF = Math.Abs(_AmountHUF_RatedIff)
                                            _OutAmountHUF = 0
                                        Else 'Nyereség vevővel szemben
                                            _DebitChartOfAccounts = _GLRows.DebitChartOfAccounts
                                            _CreditChartOfAccounts = _ChartOfAccounts_RateDiff_Win
                                            _InAmountHUF = 0
                                            _OutAmountHUF = Math.Abs(_AmountHUF_RatedIff)
                                        End If

                                    ElseIf _GLRows.InAmountDEV > 0 Then 'Jóváírás
                                        If _AmountHUF_RatedIff > 0 Then 'Veszteség vevővel szemben
                                            _DebitChartOfAccounts = _ChartOfAccounts_RateDiff_Loss
                                            _CreditChartOfAccounts = _GLRows.CreditChartOfAccounts
                                            _InAmountHUF = Math.Abs(_AmountHUF_RatedIff)
                                            _OutAmountHUF = 0
                                        Else 'Nyereség vevő szemben
                                            _DebitChartOfAccounts = _GLRows.CreditChartOfAccounts
                                            _CreditChartOfAccounts = _ChartOfAccounts_RateDiff_Win
                                            _InAmountHUF = 0
                                            _OutAmountHUF = Math.Abs(_AmountHUF_RatedIff)
                                        End If
                                    End If
                            End Select

                            If _AmountHUF_RatedIff <> 0 Then

                                If _GLHeader_Ratediff Is Nothing Then
                                    _GLHeader_Ratediff = _p_ocur.CreateObject(Of GLMixed)()
                                End If
                                _GLHeader_Ratediff.CustomersFrom = _p_GLHeader.CustomersFrom
                                _GLMixedNG = _p_ocur.FindObject(Of GLMixedNG)(CriteriaOperator.Parse("CustomersFrom=?", _GLHeader_Ratediff.CustomersFrom), True)
                                If _GLMixedNG IsNot Nothing Then
                                    If _GLHeader_Ratediff.DocumentNumber = "" Then
                                        _GLHeader_Ratediff.DocumentNumber = _GLMixedNG.NumberGenerator.GetNewNumber(_GLMixedNG.NumberGenerator)
                                    End If
                                End If
                                _GLHeader_Ratediff.DateExecution = _p_GLHeader.DateExecution
                                _GLHeader_Ratediff.DateCreated = _p_GLHeader.DateCreated
                                _GLHeader_Ratediff.CurrencyRate = 1

                                _GLHeader_Ratediff.GLDocumentType = eGLDocumentType.eGLRateDiff
                                _GLHeader_Ratediff.IsEditable = False
                                _GLHeader_Ratediff.IsStorno = False
                                _GLHeader_Ratediff.CurrencyName = _p_ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
                                _GLHeader_Ratediff.Save()

                                _GLRows_RateDiff = _p_ocur.CreateObject(Of GLRows)()
                                _GLRows_RateDiff.InAmountHUF = _InAmountHUF
                                _GLRows_RateDiff.OutAmountHUF = _OutAmountHUF
                                _GLRows_RateDiff.GLHeader = _GLHeader_Ratediff
                                _GLRows_RateDiff.AddGLRows(_GLHeader_Ratediff, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, Math.Abs(_AmountHUF_RatedIff), Nothing, _
                                                            _GLRows.Customer, Nothing, Nothing, "", Nothing, _Controlling, Nothing, Nothing, Nothing, _GLRows.ConnectInfo, _
                                                             eGLRowsTypeVAT.eNettoRow, Guid.NewGuid, _GLRows.TransactionType, eCashType.ePartner, _GLRows.PartnerType)
                                _GLRows_RateDiff.Save()
                            End If
                        End If
                    End If
                End If
            End If
        Next
        If _GLHeader_Ratediff IsNot Nothing Then
            Select Case _p_GLHeader.GLDocumentType
                Case eGLDocumentType.eGLBank
                    _GLBank = TryCast(_p_GLHeader, GLBank)
                    _GLBank.GLHeader_RateDiff = _GLHeader_Ratediff
                    _GLBank.Save()
                Case eGLDocumentType.eGLCassa
                    _GLCassa = TryCast(_p_GLHeader, GLCassa)
                    _GLCassa.GLHeader_RateDiff = _GLHeader_Ratediff
                    _GLCassa.Save()
                Case eGLDocumentType.eGLCompensation
                    _GLCompensation = TryCast(_p_GLHeader, GLCompensation)
                    _GLCompensation.GLHeader_RateDiff = _GLHeader_Ratediff
                    _GLCompensation.Save()
            End Select
        Else
            Select Case _p_GLHeader.GLDocumentType
                Case eGLDocumentType.eGLBank
                    _GLBank = TryCast(_p_GLHeader, GLBank)
                    _GLBank.GLHeader_RateDiff = Nothing
                    _GLBank.Save()
                Case eGLDocumentType.eGLCassa
                    _GLCassa = TryCast(_p_GLHeader, GLCassa)
                    _GLCassa.GLHeader_RateDiff = Nothing
                    _GLCassa.Save()
                Case eGLDocumentType.eGLCompensation
                    _GLCompensation = TryCast(_p_GLHeader, GLCompensation)
                    _GLCompensation.GLHeader_RateDiff = Nothing
                    _GLCompensation.Save()
            End Select
        End If
        _p_ocur.CommitChanges()
    End Sub



End Class

