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

'<EditorStateRule("GLCompensation-Partner-hide", "row_ChartOfAccounts", EditorState.Hidden, "row_TransactionType in (0,1)", ViewType.DetailView)> _
'<EditorStateRule("GLCompensation-Partnerconnection-hide", "row_PCIGroup;row_PartnerType", EditorState.Hidden, "row_TransactionType in (2,3) OR IsEditable=False", ViewType.DetailView)> _
'<EditorStateRule("GLCompensation-Hide GLCompensation-LiquidAssets", "LiquidAssets_Main", EditorState.Disabled, "(IsNull(LiquidAssets_Main))=False", ViewType.DetailView)> _
'<EditorStateRule("Hide row_CurrencyRate2  in GLCompensation", "row_CurrencyRate2", EditorState.Hidden, "LiquidAssets_Main.CurrencyName.ShortName =  row_CurrencyName2.ShortName", ViewType.DetailView)> _
'<EditorStateRule("Hide row_CurrencyRate2  in GLCompensation and HUF", "row_CurrencyRate2", EditorState.Hidden, "LiquidAssets_Main.CurrencyName.ShortName ='HUF' or row_CurrencyName2.ShortName='HUF'", ViewType.DetailView)> _
'<EditorStateRule("GLCompensation-Hide DEV info in HUF", "TotalInAmountDEV;TotalOutAmountDEV;BallanceDEV;CloseDEV", EditorState.Hidden, "CurrencyName.ShortName='HUF'", ViewType.DetailView)> _
'<EditorStateRule("GLCompensation-Hide HUF info in DEV", "TotalInAmountHUF;TotalOutAmountHUF;BallanceHUF;CloseHUF", EditorState.Hidden, "CurrencyName.ShortName!='HUF'", ViewType.DetailView)> _
'<EditorStateRule("Disable GLCompensation.row_AmountHUF when DEV != HUF", "row_AmountHUF", EditorState.Disabled, "LiquidAssets_Main.CurrencyName.ShortName != 'HUF'", ViewType.DetailView)> _

<Appearance("GLCompensation-Hide GLCompensation-LiquidAssets", TargetItems:="LiquidAssets_Main", Criteria:="(IsNull(LiquidAssets_Main))=False", Enabled:=False, Context:="DetailView")> _
<Appearance("Disable GLCompensation.row_AmountHUF when DEV != HUF", TargetItems:="row_AmountHUF", Criteria:="LiquidAssets_Main.CurrencyName.ShortName != 'HUF'", Enabled:=False, Context:="DetailView")> _
<DefaultClassOptions()> _
<NavigationItem("FinancialTransactions")> _
<Appearance("GLCompensation-Partner-hide", TargetItems:="row_ChartOfAccounts", Criteria:="row_TransactionType in (0,1)", Visibility:=Editors.ViewItemVisibility.Hide, Context:="DetailView")> _
<Appearance("GLCompensation-Partnerconnection-hide", TargetItems:="row_PCIGroup;row_PartnerType", Criteria:="row_TransactionType in (2,3) OR IsEditable=False", Visibility:=Editors.ViewItemVisibility.Hide, Context:="DetailView")> _
<Appearance("Hide row_CurrencyRate2  in GLCompensation", TargetItems:="row_CurrencyRate2", Criteria:="LiquidAssets_Main.CurrencyName.ShortName =  row_CurrencyName2.ShortName", Visibility:=Editors.ViewItemVisibility.Hide, Context:="DetailView")> _
<Appearance("Hide row_CurrencyRate2  in GLCompensation and HUF", TargetItems:="row_CurrencyRate2", Criteria:="LiquidAssets_Main.CurrencyName.ShortName ='HUF' or row_CurrencyName2.ShortName='HUF'", Visibility:=Editors.ViewItemVisibility.Hide, Context:="DetailView")> _
<Appearance("GLCompensation-Hide DEV info in HUF", TargetItems:="TotalInAmountDEV;TotalOutAmountDEV;BallanceDEV;CloseDEV", Criteria:="CurrencyName.ShortName='HUF'", Visibility:=Editors.ViewItemVisibility.Hide, Context:="DetailView")> _
<Appearance("GLCompensation-Hide HUF info in DEV", TargetItems:="TotalInAmountHUF;TotalOutAmountHUF;BallanceHUF;CloseHUF", Criteria:="CurrencyName.ShortName!='HUF'", Visibility:=Editors.ViewItemVisibility.Hide, Context:="DetailView")> _
<Appearance("Hide in GLCompensation row_Amount2 when CurrencyName2=CurrencyName", AppearanceItemType.ViewItem, "row_CurrencyName2.ShortName=LiquidAssets_Main.CurrencyName.ShortName", TargetItems:="row_CurrencyRate2;row_AmountDEV2", visibility:=DevExpress.ExpressApp.Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide row_ info when IsEditable=False", AppearanceItemType.ViewItem, "IsEditable=False", TargetItems:="row_PartnerType; row_TransactionType; row_ChartOfAccounts;" _
    & "row_PCIGroup;", visibility:=Editors.ViewItemVisibility.Hide)> _
<RuleCriteria("RuleCriteria GLCompensation-row_ChartOfAccounts", "aToTable_GLCompensation_Contexts", "row_ChartOfAccounts.Children.Count=0", "Csak gyermek objektum választható ki!", SkipNullOrEmptyValues:=False)> _
Public Class GLCompensation
    Inherits GLHeader
    Private _PartnerType As ePartnerType
    Private _TransactionType As eTransactionType
    Private _ChartOfAccounts As ChartOfAccounts
    Private _TotalInAmountHUF As Double = 0
    Private _TotalOutAmountHUF As Double = 0
    Private _TotalInAmountDEV As Double = 0
    Private _TotalOutAmountDEV As Double = 0
    Private _LiquidAssets As LiquidAssets
    Private _BallanceHUF As Double
    Private _BallanceDEV As Double
    Private _PCIGroup As PCIGroup
    Private _GLHeader_RateDiff As GLHeader 'Kapcsolódó árf. kül. elszámolási vegyes bizonylat

    '-- A másodlagos 
    Private _row_AmountDEV2 As Double = 0
    Private _row_CurrencyRate2 As Double = 1
    Private _row_CurrencyName2 As CurrencyName

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        If Session.IsNewObject(Me) Then Me.GLDocumentType = eGLDocumentType.eGLCompensation
    End Sub
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_PartnerType() As ePartnerType
        Get
            Return _PartnerType
        End Get
        Set(ByVal value As ePartnerType)
            SetPropertyValue("row_PartnerType", _PartnerType, value)
        End Set
    End Property
    <NonPersistent()> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    Property row_TransactionType() As eTransactionType
        Get
            Return _TransactionType
        End Get
        Set(ByVal value As eTransactionType)
            SetPropertyValue("row_TransactionType", _TransactionType, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                Select Case value
                    Case eTransactionType.eNormalCredit, eTransactionType.eNormalDebit, eTransactionType.eNotSet
                        row_CurrencyName2 = LiquidAssets_Main.CurrencyName
                End Select
                Select Case value
                    Case eTransactionType.eNormalCredit, eTransactionType.eNormalDebit, eTransactionType.eNotSet
                        Me.row_PartnerType = ePartnerType.eNotSet
                End Select
            End If
        End Set
    End Property
    <NonPersistent()> _
    <DataSourceCriteria("AccountYear=GetYear('@This.DateExecution')")> _
    <RuleRequiredField("GLCompensation-row_ChartOfAccounts", "aToTable_GLCompensation_Contexts", TargetCriteria:="row_TransactionType in (2,3,4)")> _
    <VisibleInListView(False)> _
    Property row_ChartOfAccounts() As ChartOfAccounts
        Get
            Return _ChartOfAccounts
        End Get
        Set(ByVal value As ChartOfAccounts)
            SetPropertyValue("row_ChartOfAccounts", _ChartOfAccounts, value)
        End Set
    End Property
    <NonPersistent()> _
    <ImmediatePostData(True)> _
    <DataSourceCriteria("CustomersFrom='@This.CustomersFrom' and PartnerType='@This.row_PartnerType' and BallanceState=0 and isnull(CurrencyName_Account)=False ")> _
    <VisibleInListView(False)> _
    Property row_PCIGroup() As PCIGroup
        Get
            Return _PCIGroup
        End Get
        Set(ByVal value As PCIGroup)
            SetPropertyValue("row_PCIGroup", _PCIGroup, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If value IsNot Nothing Then
                    row_ConnectInfo = value.ConnectInfo
                    row_Customer = value.Customers
                    row_PartnerType = value.PartnerType
                    If value.CurrencyName_Account.ShortName = "HUF" Then
                        If Me.CurrencyName.ShortName = "HUF" Then
                            Me.row_AmountHUF = Math.Abs(value.BallanceHUF)
                            row_CurrencyName2 = Session.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
                        Else
                            row_CurrencyName2 = Session.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))

                        End If
                    Else
                        If Me.CurrencyName.ShortName = value.CurrencyName_Account.ShortName Then
                            Me.row_AmountDEV = Math.Abs(value.BallanceDEV)
                            row_CurrencyName2 = Session.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", value.CurrencyName_Account.ShortName))
                        Else
                            row_CurrencyName2 = Session.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", value.CurrencyName_Account.ShortName))
                        End If
                    End If
                End If
            End If
        End Set
    End Property
    <ImmediatePostData(True)> _
    <DataSourceCriteria("LiquidAssetsType=3 and CustomerFrom='@This.CustomersFrom'")> _
    <RuleRequiredField("GLCompensation-LiquidAssets_Main", "aFinal_GLBank_Context")> _
    <RuleRequiredField("GLCompensation-LiquidAssets_Main_2", "aFinalAndNew_GLBank_Context")> _
    <VisibleInListView(False)> _
    Property LiquidAssets_Main() As LiquidAssets
        Get
            Return _LiquidAssets
        End Get
        Set(ByVal value As LiquidAssets)
            SetPropertyValue("LiquidAssets_Main", _LiquidAssets, value)
            If Session.IsObjectsSaving = False And Session.IsObjectsLoading = False Then
                row_LiquidAssets = value
                If value IsNot Nothing Then
                    CurrencyName = value.CurrencyName
                    Me.row_CurrencyName2 = CurrencyName
                    BallanceHUF = GetBallanceHUF(value, Now)
                    BallanceDEV = GetBallanceDEV(value, Now)
                    If Me.CustomersFrom IsNot Nothing Then
                        If Me.DocumentNumber Is Nothing Then
                            If value.NumberGeneratorIn IsNot Nothing Then
                                Me.DocumentNumber = value.NumberGeneratorIn.GetNewNumber(value.NumberGeneratorIn)
                                Me.Save()
                                Session.CommitTransaction()
                            End If
                        End If
                    End If
                End If
            End If
        End Set
    End Property
    <NonPersistent()> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    Property row_AmountDEV2 As Double
        Get
            Return _row_AmountDEV2
        End Get
        Set(value As Double)
            SetPropertyValue("row_AmountDEV2", _row_AmountDEV2, value)
        End Set
    End Property
    <NonPersistent()> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    <RuleRange("GLCompensation.CurrencyName.ToTable", "aToTable_GLCompensation_Contexts", 0.01, 9999999, _
        TargetCriteria:="LiquidAssets_Main.CurrencyName.ShortName != row_CurrencyName2.ShortName and row_CurrencyName2.ShortName !='HUF' and LiquidAssets_Main.CurrencyName.ShortName !='HUF'")> _
    Property row_CurrencyRate2 As Double
        Get
            Return _row_CurrencyRate2
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("row_CurrencyRate2", _row_CurrencyRate2, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If row_CurrencyName2 Is Nothing Then row_CurrencyName2 = LiquidAssets_Main.CurrencyName
                If LiquidAssets_Main.CurrencyName.ShortName = "HUF" Then
                    If LiquidAssets_Main.CurrencyName.ShortName = row_CurrencyName2.ShortName Then
                    Else
                        row_AmountDEV2 = row_AmountHUF * Math.Round(row_CurrencyRate2, 2, MidpointRounding.AwayFromZero)
                    End If
                Else
                    If LiquidAssets_Main.CurrencyName.ShortName = row_CurrencyName2.ShortName Then
                    Else
                        row_AmountDEV2 = row_AmountDEV * Math.Round(row_CurrencyRate2, 2, MidpointRounding.AwayFromZero)
                    End If
                End If
            End If
        End Set
    End Property
    <NonPersistent()> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    Property row_CurrencyName2 As SISBusiness.Module.CurrencyName
        Get
            Return _row_CurrencyName2
        End Get
        Set(value As SISBusiness.Module.CurrencyName)
            SetPropertyValue("row_CurrencyName2", _row_CurrencyName2, value)
        End Set
    End Property
    Property TotalInAmountHUF As Double
        Get
            Return _TotalInAmountHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("TotalInAmountHUF", _TotalInAmountHUF, value)
        End Set
    End Property
    Property TotalOutAmountHUF As Double
        Get
            Return _TotalOutAmountHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("TotalOutAmountHUF", _TotalOutAmountHUF, value)
        End Set
    End Property
    Property TotalInAmountDEV As Double
        Get
            Return _TotalInAmountDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("TotalInAmountDEV", _TotalInAmountDEV, value)
        End Set
    End Property
    Property TotalOutAmountDEV As Double
        Get
            Return _TotalOutAmountDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("TotalOutAmountDEV", _TotalOutAmountDEV, value)
        End Set
    End Property
    Property BallanceHUF As Double
        Get
            Return _BallanceHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("BallanceHUF", _BallanceHUF, value)
        End Set
    End Property
    Property BallanceDEV As Double
        Get
            Return _BallanceDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("BallanceDEV", _BallanceDEV, value)
        End Set
    End Property
    Property GLHeader_RateDiff As GLHeader
        Get
            Return _GLHeader_RateDiff
        End Get
        Set(value As GLHeader)
            SetPropertyValue("GLHeader_RateDiff", _GLHeader_RateDiff, value)
        End Set
    End Property
    Sub RecalculateTotal()
        Dim _GLRows As GLRows
        TotalInAmountHUF = 0
        TotalOutAmountHUF = 0
        TotalInAmountDEV = 0
        TotalOutAmountDEV = 0
        For Each _GLRows In Me.GLRows
            TotalInAmountHUF += _GLRows.InAmountHUF
            TotalOutAmountHUF += _GLRows.OutAmountHUF
            TotalInAmountDEV += _GLRows.InAmountDEV
            TotalOutAmountDEV += _GLRows.OutAmountDEV
        Next
        BallanceHUF = TotalInAmountHUF - TotalOutAmountHUF
        BallanceDEV = TotalInAmountDEV - _TotalOutAmountDEV
    End Sub
    Function GetBallanceHUF(ByVal _LiquidAssets As LiquidAssets, ByVal _ToDate As Date) As Double
        Dim _GLCompensation_Collection As New XPCollection(Of GLCompensation)(Session, _
            CriteriaOperator.Parse("LiquidAssets_Main=? and DateExecution<=? and IsEditable=False", _LiquidAssets, _ToDate))
        Dim _GLCompensation As GLCompensation
        Dim _GLRows As GLRows
        Dim _InAmountHUF As Double = 0
        Dim _OutAmountHUF As Double = 0

        For Each _GLCompensation In _GLCompensation_Collection
            For Each _GLRows In _GLCompensation.GLRows
                _InAmountHUF += _GLRows.InAmountHUF
                _OutAmountHUF += _GLRows.OutAmountHUF
            Next
        Next
        Return _InAmountHUF - _OutAmountHUF
    End Function
    Function GetBallanceDEV(ByVal _LiquidAssets As LiquidAssets, ByVal _ToDate As Date) As Double
        Dim _GLCompensation_Collection As New XPCollection(Of GLCompensation)(Session, _
            CriteriaOperator.Parse("LiquidAssets_Main=? and DateExecution<=? and IsEditable=False", _LiquidAssets, _ToDate))
        Dim _GLBank As GLCompensation
        Dim _GLRows As GLRows
        Dim _InAmountDEV As Double = 0
        Dim _OutAmountDEV As Double = 0

        For Each _GLBank In _GLCompensation_Collection
            For Each _GLRows In _GLBank.GLRows
                _InAmountDEV += _GLRows.InAmountDEV
                _OutAmountDEV += _GLRows.OutAmountDEV
            Next
        Next
        Return _InAmountDEV - _OutAmountDEV
    End Function
    <RuleRange("Zero for GLCompensation HUF", "aFinal_GLCompensation_Context", 0, 0, targetcriteria:="LiquidAssets_Main.CurrencyName.ShortName = 'HUF'")> _
    ReadOnly Property CloseHUF As Double
        Get
            Return Math.Round(TotalInAmountHUF, 2, MidpointRounding.AwayFromZero) - Math.Round(TotalOutAmountHUF, 2, MidpointRounding.AwayFromZero)
        End Get
    End Property
    <RuleRange("Zero for GLCompensation DEV", "aFinal_GLCompensation_Context", 0, 0, targetcriteria:="LiquidAssets_Main.CurrencyName.ShortName != 'HUF'")> _
    ReadOnly Property CloseDEV As Double
        Get
            Return Math.Round(TotalInAmountDEV, 2, MidpointRounding.AwayFromZero) - Math.Round(TotalOutAmountDEV, 2, MidpointRounding.AwayFromZero)
        End Get
    End Property

    Overrides Property row_Controlling As Controlling
        Get
            Return MyBase.row_Controlling
        End Get
        Set(value As Controlling)
            MyBase.row_Controlling = value
            If Session.IsObjectsSaving = False And Session.IsObjectsLoading = False Then
                If value.ChartOfAccounts IsNot Nothing Then
                    row_ChartOfAccounts = value.GetChartOfAccounts(Me.DateExecution.Year, False)
                End If
            End If
        End Set
    End Property
End Class
