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
Imports System.Drawing
Imports DevExpress.ExpressApp.Utils

'<EditorStateRule("Hide DateExecution in GLCassa", "DateExecution", EditorState.Disabled, "DocumentNumber !=''", ViewType.DetailView)> _
'<EditorStateRule("Partnerconnection-hide-Cassa", "row_PCIGroup", EditorState.Hidden, "row_CashType=1 OR IsEditable=False", ViewType.DetailView)> _
'<EditorStateRule("Partner-hide-Cassa", "row_ChartOfAccounts;row_VAT;row_Controlling;row_CostHolder;row_JobNumberObjects;row_UniqueObjects;row_CostPlace", EditorState.Hidden, "row_CashType=0 OR IsEditable=False", ViewType.DetailView)> _
'<EditorStateRule("Hide LiquidAssets", "LiquidAssets_Cassa", EditorState.Disabled, "IsNull(LiquidAssets_Cassa)=False OR IsEditable=False", ViewType.DetailView)> _
'<EditorStateRule("Hide row_CurrencyRate2  in GLCassa", "row_CurrencyRate2", EditorState.Hidden, "LiquidAssets_Cassa.CurrencyName.ShortName =  row_CurrencyName2.ShortName", ViewType.DetailView)> _
'<EditorStateRule("Hide row_CurrencyRate2  in GLCassa and HUF", "row_CurrencyRate2", EditorState.Hidden, "LiquidAssets_Cassa.CurrencyName.ShortName ='HUF' or row_CurrencyName2.ShortName='HUF' and IsEditable=True", ViewType.DetailView)> _
'<EditorStateRule("Disable CashMoveType", "CashMoveType", EditorState.Disabled, "CashMoveType!=2", ViewType.DetailView)> _
'<EditorStateRule("Disable AmountHUF when CurrencyName<>HUF", "row_AmountHUF", EditorState.Disabled, "CurrencyName.ShortName<>'HUF'", ViewType.DetailView)> _
'<EditorStateRule("Disable GLCassa.row_AmountHUF when DEV != HUF", "row_AmountHUF", EditorState.Disabled, "LiquidAssets_Cassa.CurrencyName.ShortName != 'HUF'", ViewType.DetailView)> _

<DefaultClassOptions()> _
<NavigationItem("FinancialTransactions")> _
<Appearance("Hide row_ info when IsEditable=False",
                     AppearanceItemType:="ViewItem",
                     criteria:="IsEditable=False",
                     TargetItems:="row_CashType; row_Customer;row_PartnerType;row_PCIGroup;" + _
                                  "row_ConnectInfo; row_ChartOfAccounts; row_AmountHUF;row_AmountDEV;" + _
                                  "row_VAT;row_Controlling;row_CostHolder;row_JobNumberObjects;" + _
                                  "row_UniqueObjects;row_CostPlace;row_CurrencyName2;" + _
                                  "row_CurrencyRate;row_CurrencyRate2;row_AmountDEV2;row_NoteRows",
                     visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide Cassa row_Amount2 when CurrencyName2=CurrencyName",
                     AppearanceItemType:="ViewItem",
                     criteria:="row_CurrencyName2.ShortName=LiquidAssets_Cassa.CurrencyName.ShortName",
                     TargetItems:="row_CurrencyRate2;row_AmountDEV2",
                     visibility:=DevExpress.ExpressApp.Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide Cassa row_Amount2 when PartnerType is not set",
                     AppearanceItemType:="ViewItem",
                     criteria:="row_CashType=1",
                     TargetItems:="row_CurrencyName2;row_CurrencyRate2;row_AmountDEV2",
                     visibility:=DevExpress.ExpressApp.Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide row_CurrencyRate  in GLCassa",
                     AppearanceItemType:="ViewItem",
                     criteria:="LiquidAssets_Cassa.CurrencyName.ShortName = 'HUF' and row_CurrencyName2.ShortName='HUF' and IsEditable=True",
                     TargetItems:="row_CurrencyRate",
                     visibility:=DevExpress.ExpressApp.Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide DateExecution in GLCassa",
                     AppearanceItemType:="ViewItem",
                     TargetItems:="DateExecution",
                     Criteria:="DocumentNumber !=''",
                     Enabled:=False, Context:="DetailView")> _
<Appearance("Partnerconnection-hide-Cassa",
                     AppearanceItemType:="ViewItem",
                     TargetItems:="row_PCIGroup",
                     Criteria:="row_CashType=1 OR IsEditable=False",
                     Visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Partner-hide-Cassa",
                     AppearanceItemType:="ViewItem",
                     TargetItems:="row_ChartOfAccounts;row_VAT;row_Controlling;row_CostHolder;row_JobNumberObjects;row_UniqueObjects;row_CostPlace",
                     Criteria:="row_CashType=0 OR IsEditable=False",
                     Visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide LiquidAssets when LiquidAssets is selected",
                     AppearanceItemType:="ViewItem",
                     TargetItems:="LiquidAssets_Cassa",
                     Criteria:="IsNull(LiquidAssets_Cassa)=False OR IsEditable=False",
                     Enabled:=False,
                     Context:="DetailView")> _
<Appearance("Hide row_CurrencyRate2  in GLCassa",
                     AppearanceItemType:="ViewItem",
                     TargetItems:="row_CurrencyRate2",
                     Criteria:="LiquidAssets_Cassa.CurrencyName.ShortName =  row_CurrencyName2.ShortName",
                     Visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide row_CurrencyRate2  in GLCassa and HUF",
                     AppearanceItemType:="ViewItem",
                     TargetItems:="row_CurrencyRate2",
                     Criteria:="LiquidAssets_Cassa.CurrencyName.ShortName ='HUF' or row_CurrencyName2.ShortName='HUF' and IsEditable=True",
                     Visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable CashMoveType",
                     AppearanceItemType:="ViewItem",
                     TargetItems:="CashMoveType",
                     Criteria:="CashMoveType!=2",
                     Enabled:=False,
                     Context:="DetailView")> _
<Appearance("Disable AmountHUF when CurrencyName<>HUF",
                     AppearanceItemType:="ViewItem",
                     TargetItems:="row_AmountHUF",
                     Criteria:="CurrencyName.ShortName<>'HUF' and row_CashType !=2",
                     Enabled:=False, Context:="DetailView")> _
<Appearance("Disable GLCassa.row_AmountHUF when DEV != HUF",
                     AppearanceItemType:="ViewItem",
                     TargetItems:="row_AmountHUF",
                     Criteria:="LiquidAssets_Cassa.CurrencyName.ShortName != 'HUF' and row_CashType !=2",
                     Enabled:=False,
                     Context:="DetailView")> _
<RuleCriteria("RuleCriteria GLCassa-row_ChartOfAccounts", "aToTable_GLCassa_Contexts", "row_ChartOfAccounts.Children.Count=0", "Csak gyermek objektum választható ki!", SkipNullOrEmptyValues:=False)> _
<Appearance("Hide aGetCurrencyRateCassa",
                     AppearanceItemType:="Action",
                     targetitems:="aGetCurrencyRateBank",
                     criteria:="LiquidAssets_Cassa.CurrencyName.ShortName='HUF'",
                     visibility:=DevExpress.ExpressApp.Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide properties if Correction",
                     AppearanceItemType:="ViewItem",
                     TargetItems:="row_AmountDEV;row_CurrencyRate2;row_CurrencyRate;row_CurrencyName2;row_AmountDEV2;row_VAT",
                     Criteria:="row_CashType in (2)",
                     Visibility:=Editors.ViewItemVisibility.Hide)> _
Public Class GLCassa
    Inherits GLHeader
    Private _CashMoveType As eCashMoveType = eCashMoveType.eNotSet
    Private _CashType As eCashType
    Private _ChartOfAccounts As ChartOfAccounts
    Private _PCIGroup As PCIGroup
    Private _PartnerType As ePartnerType
    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 _GLHeader_RateDiff As GLHeader 'Kapcsolódó árf. kül. elszámolási vegyes bizonylat
    Private _GLClosingTime As GLClosingTime
    '-- A másodlagos 
    Private _row_CurrencyRate As Double
    Private _row_AmountDEV2 As Double = 0
    Private _row_CurrencyRate2 As Double = 0
    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.eGLCassa
            Me.DateExecution = GetSQLTime(Session)
            Me.DocumentNumber = ""
            Me.IsEditable = False
            Me.IsStorno = False
            Me.DocumentNumber = ""
            Me.row_CashType = eCashType.eNormal
        End If
    End Sub
    <RuleValueComparison("RuleValue-CashMoveType", "aFinal_GLCassa_Context", ValueComparisonType.NotEquals, eCashMoveType.eNotSet)> _
    <RuleValueComparison("RuleValue-CashMoveType2", "aFinalAndNew_GLCassa_Context", ValueComparisonType.NotEquals, eCashMoveType.eNotSet)> _
    <RuleValueComparison("RuleValue-CashMoveType3", "aToTable_GLCassa_Contexts", ValueComparisonType.NotEquals, eCashMoveType.eNotSet)> _
    Property CashMoveType() As eCashMoveType
        Get
            Return _CashMoveType
        End Get
        Set(ByVal value As eCashMoveType)
            SetPropertyValue("CashMoveType", _CashMoveType, value)
        End Set
    End Property
    <Size(-1)> _
    <NonPersistent()> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    Property row_CashType() As eCashType
        Get
            Return _CashType
        End Get
        Set(ByVal value As eCashType)
            SetPropertyValue("row_CashType", _CashType, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If value = eCashType.eNormal Or value = eCashType.eCorrectionHUF Then
                    If LiquidAssets_Cassa IsNot Nothing Then row_CurrencyName2 = LiquidAssets_Cassa.CurrencyName
                    row_PartnerType = ePartnerType.eNotSet
                End If
            End If
        End Set
    End Property
    <NonPersistent()> _
    <RuleRequiredField("GLCassa-row_ChartOfAccounts", "aToTable_GLCassa_Contexts", TargetCriteria:="row_CashType=1")> _
    <DataSourceCriteria("AccountYear=GetYear('@This.DateExecution')")> _
    <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()> _
    <DataSourceCriteria("CustomersFrom='@This.CustomersFrom' and PartnerType='@This.row_PartnerType'  and BallanceState=0 and isnull(CurrencyName_Account)=False ")> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    Property row_PCIGroup() As PCIGroup
        Get
            Return _PCIGroup
        End Get
        Set(ByVal value As PCIGroup)
            SetPropertyValue("row_PCIGroup", _PCIGroup, value)
            If Session.IsObjectsSaving = False And Session.IsObjectsLoading = False Then
                If value IsNot Nothing Then
                    row_ConnectInfo = value.ConnectInfo
                    row_Customer = value.Customers
                    row_PartnerType = value.PartnerType
                    If value.CurrencyName_Account IsNot Nothing Then
                        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 If
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    <RuleValueComparison("GLCassa-row_PartnerType", "aToTable_GLCassa_Contexts", ValueComparisonType.NotEquals, ePartnerType.eNotSet, TargetCriteria:="row_CashType in (0)")> _
    Property row_PartnerType() As ePartnerType
        Get
            Return _PartnerType
        End Get
        Set(ByVal value As ePartnerType)
            SetPropertyValue("row_PartnerType", _PartnerType, value)
        End Set
    End Property
    <DataSourceCriteria("LiquidAssetsType in (0,2,4) AND CustomerFrom='@This.CustomersFrom'")> _
    <ImmediatePostData()> _
    <RuleRequiredField("GLCassa-LiquidAssets_Cassa", "aFinal_GLCassa_Context")> _
    <RuleRequiredField("GLCassa-LiquidAssets_Cassa2", "aFinalAndNew_GLCassa_Context")> _
    <VisibleInListView(False)> _
    Property LiquidAssets_Cassa As LiquidAssets
        Get
            Return _LiquidAssets
        End Get
        Set(ByVal value As LiquidAssets)
            SetPropertyValue("LiquidAssets_Cassa", _LiquidAssets, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                BallanceHUF = Me.GetBallanceHUF(value, Now)
                BallanceDEV = Me.GetBallanceDEV(value, Now)
                CurrencyName = value.CurrencyName
                row_CurrencyName2 = value.CurrencyName
            End If
        End Set
    End Property
    <NonPersistent()> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    <RuleRange("GLCassa-row_CurrencyRate", "aToTable_GLCassa_Contexts", 1.1, 999999, TargetCriteria:="LiquidAssets_Cassa.CurrencyName.ShortName != 'HUF' and row_CashType !=2")> _
    Property row_CurrencyRate As Double
        Get
            Return _row_CurrencyRate
        End Get
        Set(value As Double)
            SetPropertyValue("row_CurrencyRate", _row_CurrencyRate, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If row_CurrencyName2 Is Nothing Then row_CurrencyName2 = LiquidAssets_Cassa.CurrencyName
                If LiquidAssets_Cassa.CurrencyName.ShortName = "HUF" And row_CurrencyName2.ShortName <> "HUF" Then
                    If row_CurrencyRate <> 0 Then row_AmountDEV2 = Math.Round(row_AmountHUF / row_CurrencyRate, 2, MidpointRounding.AwayFromZero)
                End If
                If LiquidAssets_Cassa.CurrencyName.ShortName <> "HUF" And row_CurrencyName2.ShortName = "HUF" Then
                    row_AmountHUF = row_AmountDEV * Math.Round(row_CurrencyRate, 2, MidpointRounding.AwayFromZero)
                    row_AmountDEV2 = row_AmountHUF
                End If
                If LiquidAssets_Cassa.CurrencyName.ShortName <> "HUF" And row_CurrencyName2.ShortName <> "HUF" Then
                    row_AmountHUF = row_AmountDEV * Math.Round(row_CurrencyRate, 2, MidpointRounding.AwayFromZero)

                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)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If row_CurrencyName2 Is Nothing Then row_CurrencyName2 = LiquidAssets_Cassa.CurrencyName
                If LiquidAssets_Cassa.CurrencyName.ShortName <> "HUF" And _
                    row_CurrencyName2.ShortName <> "HUF" And _
                    LiquidAssets_Cassa.CurrencyName.ShortName <> row_CurrencyName2.ShortName Then
                    If row_AmountDEV <> 0 Then
                        row_CurrencyRate2 = Math.Round(row_AmountDEV2 / row_AmountDEV, 4, MidpointRounding.AwayFromZero)
                    End If
                End If
            End If
        End Set
    End Property
    <NonPersistent()> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    <RuleRange("GLCassa.CurrencyName.ToTable", "aToTable_GLCassa_Contexts", 0.01, 999999, _
        TargetCriteria:="LiquidAssets_Cassa.CurrencyName.ShortName != row_CurrencyName2.ShortName and LiquidAssets_Cassa.CurrencyName.ShortName !='HUF' and row_CurrencyName2.ShortName !='HUF' and row_CashType !=2")> _
    Property row_CurrencyRate2 As Double
        Get
            Return _row_CurrencyRate2
        End Get
        Set(value As Double)
            SetPropertyValue("row_CurrencyRate2", _row_CurrencyRate2, value)
        End Set
    End Property
    <NonPersistent()> _
    <ImmediatePostData(True)> _
    <VisibleInListView(False)> _
    Property row_CurrencyName2 As CurrencyName
        Get
            Return _row_CurrencyName2
        End Get
        Set(value As 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
    <NonPersistent()>
    Property BallanceHUF As Double
        Get
            Return _BallanceHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("BallanceHUF", _BallanceHUF, value)
        End Set
    End Property
    <NonPersistent()> _
    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
    <Browsable(False)> _
    Property GLClosingTime As GLClosingTime
        Get
            Return _GLClosingTime
        End Get
        Set(value As GLClosingTime)
            SetPropertyValue("GLClosingTime", _GLClosingTime, value)
        End Set
    End Property
    Sub RecalculateTotal(ByVal _GLCassa As GLCassa, ByVal _ocur As Xpo.XPObjectSpace, ByVal _WidthCommit As Boolean)
        Dim _GLRows As GLRows
        _GLCassa.TotalInAmountHUF = 0
        _GLCassa.TotalOutAmountHUF = 0
        _GLCassa.TotalInAmountDEV = 0
        _GLCassa.TotalOutAmountDEV = 0
        For Each _GLRows In _GLCassa.GLRows
            _GLCassa.TotalInAmountHUF += _GLRows.InAmountHUF
            _GLCassa.TotalOutAmountHUF += _GLRows.OutAmountHUF
            _GLCassa.TotalInAmountDEV += _GLRows.InAmountDEV
            _GLCassa.TotalOutAmountDEV += _GLRows.OutAmountDEV
        Next
        If _GLCassa.TotalInAmountDEV > 0 Or _GLCassa.TotalInAmountHUF > 0 Then
            If _GLCassa.CurrencyName.ShortName = "HUF" Then
                _GLCassa.HUNumberToText = GetHUNumberToText(_GLCassa.TotalInAmountHUF)
            Else
                _GLCassa.HUNumberToText = GetHUNumberToText(_GLCassa.TotalInAmountDEV)
            End If
        Else
            If _GLCassa.CurrencyName.ShortName = "HUF" Then
                _GLCassa.HUNumberToText = GetHUNumberToText(_GLCassa.TotalOutAmountHUF)
            Else
                _GLCassa.HUNumberToText = GetHUNumberToText(_GLCassa.TotalOutAmountDEV)
            End If
        End If
        If _WidthCommit = True Then _ocur.CommitChanges()
    End Sub
    Sub CreateRoundingRow(ByVal _GLCassa As GLCassa, ByVal _ocur As Xpo.XPObjectSpace)
        Dim _GLRows As GLRows
        Dim _GLRows_New As GLRows
        Dim _GLHeader As GLHeader = TryCast(_GLCassa, GLHeader)
        Dim _InAmountHUF As Double = 0
        Dim _OutAmountHUF As Double = 0
        Dim _DebitChartOfAccounts As ChartOfAccounts
        Dim _CreditChartOfAccounts As ChartOfAccounts

        If _GLCassa IsNot Nothing Then
            For Each _GLRows In _GLCassa.GLRows
                If _GLRows.LiquidAssets.IsRounding = True Then
                    '-- Törölni kell a kerekítési sort ! -------------------------------------------------------------------------------------------------------

                    '-------------------------------------------------------------------------------------------------------------------------------------------
                    Dim _GUID As Guid = Guid.NewGuid
                    If _GLCassa.TotalInAmountHUF > 0 Then 'Befizetés
                        _InAmountHUF = _GLCassa.TotalInAmountHUF Mod 5
                        Select Case _InAmountHUF
                            Case 1, 2 'Lefele kerekítés
                                _CreditChartOfAccounts = _GLRows.LiquidAssets.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                                _DebitChartOfAccounts = _GLRows.LiquidAssets.GetChartOfAccounts(_GLCassa.DateExecution.Year, 1)
                                _GLRows_New = _ocur.CreateObject(Of GLRows)()
                                _GLRows_New.InAmountHUF = _InAmountHUF * -1
                                _GLRows_New.CreditAmountHUF = _InAmountHUF
                                _GLRows_New.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _InAmountHUF, _
                                       _GLRows.LiquidAssets, Nothing, _GLCassa.DateExecution, Nothing, "Kerekítési különbözet", Nothing, Nothing, _
                                        Nothing, Nothing, Nothing, "", eGLRowsTypeVAT.eRoundingRow, _GUID, eTransactionType.eNormalDebit, eCashType.eNormal, _
                                         ePartnerType.eNotSet)
                                _ocur.CommitChanges()
                            Case 3, 4 'fölfele kerekítés
                                _CreditChartOfAccounts = _GLRows.LiquidAssets.GetChartOfAccounts(_GLCassa.DateExecution.Year, 2)
                                _DebitChartOfAccounts = _GLRows.LiquidAssets.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                                _GLRows_New = _ocur.CreateObject(Of GLRows)()
                                _GLRows_New.InAmountHUF = 5 - _InAmountHUF
                                _GLRows_New.DebitAmountHUF = 5 - _InAmountHUF
                                _GLRows_New.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, 5 - _InAmountHUF, _
                                       _GLRows.LiquidAssets, Nothing, _GLCassa.DateExecution, Nothing, "Kerekítési különbözet", Nothing, Nothing, _
                                        Nothing, Nothing, Nothing, "", eGLRowsTypeVAT.eRoundingRow, _GUID, eTransactionType.eNormalDebit, eCashType.eNormal, _
                                         ePartnerType.eNotSet)
                                _ocur.CommitChanges()
                        End Select
                    Else 'Kifizetés
                        _OutAmountHUF = _GLCassa.TotalOutAmountHUF Mod 5
                        Select Case _OutAmountHUF
                            Case 1, 2 'Lefele kerekítés
                                _CreditChartOfAccounts = _GLRows.LiquidAssets.GetChartOfAccounts(_GLCassa.DateExecution.Year, 2)
                                _DebitChartOfAccounts = _GLRows.LiquidAssets.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                                _GLRows_New = _ocur.CreateObject(Of GLRows)()
                                _GLRows_New.OutAmountHUF = _OutAmountHUF * -1
                                _GLRows_New.DebitAmountHUF = _OutAmountHUF
                                _GLRows_New.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, _OutAmountHUF, _
                                       _GLRows.LiquidAssets, Nothing, _GLCassa.DateExecution, Nothing, "Kerekítési különbözet", Nothing, Nothing, _
                                        Nothing, Nothing, Nothing, "", eGLRowsTypeVAT.eRoundingRow, _GUID, eTransactionType.eNormalCredit, eCashType.eNormal, _
                                         ePartnerType.eNotSet)
                                _ocur.CommitChanges()
                            Case 3, 4 'fölfele kerekítés
                                _CreditChartOfAccounts = _GLRows.LiquidAssets.GetChartOfAccounts(_GLCassa.DateExecution.Year, 0)
                                _DebitChartOfAccounts = _GLRows.LiquidAssets.GetChartOfAccounts(_GLCassa.DateExecution.Year, 1)
                                _GLRows_New = _ocur.CreateObject(Of GLRows)()
                                _GLRows_New.OutAmountHUF = (5 - _OutAmountHUF)
                                _GLRows_New.CreditAmountHUF = 5 - _OutAmountHUF
                                _GLRows_New.AddGLRows(_GLHeader, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, (5 - _OutAmountHUF), _
                                       _GLRows.LiquidAssets, Nothing, _GLCassa.DateExecution, Nothing, "Kerekítési különbözet", Nothing, Nothing, _
                                        Nothing, Nothing, Nothing, "", eGLRowsTypeVAT.eRoundingRow, _GUID, eTransactionType.eNormalCredit, eCashType.eNormal, _
                                         ePartnerType.eNotSet)
                                _ocur.CommitChanges()
                        End Select
                    End If
                End If
                Exit For
            Next
        End If
    End Sub

    Function GetBallanceHUF(ByVal _LiquidAssets As LiquidAssets, ByVal _ToDate As Date) As Double
        Dim _GLCassa_Collection As New XPCollection(Of GLCassa)(PersistentCriteriaEvaluationBehavior.InTransaction, Session, _
                  CriteriaOperator.Parse("LiquidAssets_Cassa=? and DateExecution<=? and DocumentNumber<>''", _LiquidAssets, DateAdd(DateInterval.Minute, 1439, _ToDate.Date)))
        Dim _GLCassa As GLCassa
        Dim _GLRows As GLRows
        Dim _InAmountHUF As Double = 0
        Dim _OutAmountHUF As Double = 0

        For Each _GLCassa In _GLCassa_Collection
            For Each _GLRows In _GLCassa.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 _GLCassa_Collection As New XPCollection(Of GLCassa)(PersistentCriteriaEvaluationBehavior.InTransaction, Session, _
                    CriteriaOperator.Parse("LiquidAssets_Cassa=? and DateExecution<=? and DocumentNumber<>''", _LiquidAssets, DateAdd(DateInterval.Minute, 1439, _ToDate.Date)))
        Dim _GLCassa As GLCassa
        Dim _GLRows As GLRows
        Dim _InAmountDEV As Double = 0
        Dim _OutAmountDEV As Double = 0

        For Each _GLCassa In _GLCassa_Collection
            For Each _GLRows In _GLCassa.GLRows
                _InAmountDEV += _GLRows.InAmountDEV
                _OutAmountDEV += _GLRows.OutAmountDEV
            Next
        Next
        Return _InAmountDEV - _OutAmountDEV
    End Function

    Function CheckIsNegative(ByVal _ocur As Xpo.XPObjectSpace, ByVal _p_LiquidAssets As LiquidAssets, ByVal _p_GLCassa As GLCassa) As Date
        Dim _TotalHUF As Double = 0
        Dim _TotalDEV As Double = 0
        Dim _GLCassa As GLCassa
        Dim _GLRows As GLRows
        Dim _IsNegative As Boolean = False
        Dim _IsNegativeDate As Date = Nothing
        Dim _GLCassa_Collection As New XPCollection(Of GLCassa)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                  CriteriaOperator.Parse("(LiquidAssets_Cassa=? and IsStorno=False and DocumentNumber<>'') or Oid=?", _p_LiquidAssets, _p_GLCassa.Oid))

        _GLCassa_Collection.Sorting.Add(New SortProperty("DateExecution", DB.SortingDirection.Ascending))

        _IsNegativeDate = #1/1/1980#

        For Each _GLCassa In _GLCassa_Collection
            For Each _GLRows In _GLCassa.GLRows
                _TotalHUF += _GLRows.InAmountHUF - _GLRows.OutAmountHUF
                _TotalDEV += _GLRows.InAmountDEV - _GLRows.OutAmountDEV
            Next
            If LiquidAssets_Cassa.CurrencyName.ShortName = "HUF" Then
                If _TotalHUF < 0 Then
                    _IsNegative = True
                    _IsNegativeDate = _GLCassa.DateExecution
                    Exit For
                End If
            Else
                If _TotalDEV < 0 Then
                    _IsNegative = True
                    _IsNegativeDate = _GLCassa.DateExecution
                    Exit For
                End If
            End If
        Next

        Return _IsNegativeDate
    End Function
    Function CheckIsNegativeifStorno(ByVal _ocur As Xpo.XPObjectSpace, ByVal _GLCassaifStorno As GLCassa) As Date
        Dim _TotalHUF As Double = 0
        Dim _TotalDEV As Double = 0
        Dim _GLCassa As GLCassa
        Dim _GLRows As GLRows
        Dim _IsNegative As Boolean = False
        Dim _IsNegativeDate As Date = Nothing
        Dim _GLCassa_Collection As New XPCollection(Of GLCassa)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                  CriteriaOperator.Parse("LiquidAssets_Cassa=? and IsStorno=False and Oid<>?", _GLCassaifStorno.LiquidAssets_Cassa, _GLCassaifStorno.Oid))

        _GLCassa_Collection.Sorting.Add(New SortProperty("DateExecution", DB.SortingDirection.Ascending))
        _IsNegativeDate = #1/1/1980#

        For Each _GLCassa In _GLCassa_Collection
            For Each _GLRows In _GLCassa.GLRows
                _TotalHUF += _GLRows.InAmountHUF - _GLRows.OutAmountHUF
                _TotalDEV += _GLRows.InAmountDEV - _GLRows.OutAmountDEV
            Next
            If LiquidAssets_Cassa.CurrencyName.ShortName = "HUF" Then
                If _TotalHUF < 0 Then
                    _IsNegative = True
                    _IsNegativeDate = _GLCassa.DateExecution
                    Exit For
                End If
            Else
                If _TotalDEV < 0 Then
                    _IsNegative = True
                    _IsNegativeDate = _GLCassa.DateExecution
                    Exit For
                End If
            End If
        Next

        Return _IsNegativeDate
    End Function
    Public Sub PrintCassaDocument(ByVal _GLCassa As GLCassa, ByVal _ocur As Xpo.XPObjectSpace, ByVal _Frame As Frame)
        Dim _ReportData As ReportData
        Dim _GLHeader As GLHeader
        If _GLCassa IsNot Nothing Then
            _GLHeader = TryCast(_GLCassa, GLHeader)
            _ReportData = _GLCassa.LiquidAssets_Cassa.ReportData
            If _ReportData IsNot Nothing Or _GLCassa Is Nothing Then
                _Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("GLHeader=?", _GLHeader), True)
            End If
        End If
    End Sub

    Function CheckIfPartnerCashFlowsInMonthGreaterThan1500000(ByVal _ocur As Xpo.XPObjectSpace, ByVal _p_GLCassa As GLCassa, _
                                                              ByVal _p_Customers As Customers, ByVal _p_DateExecution As Date, _
                                                              _p_CurrentInAmountHUF As Double, _p_CurrentOutAmountHUF As Double) As Boolean
        Dim _Valid As Boolean = True
        Dim _SumInAmountHUF As Double = _p_CurrentInAmountHUF
        Dim _SumOutAmountHUF As Double = _p_CurrentOutAmountHUF

        Dim _GLRows_Collection As New XPCollection(Of GLRows)(_ocur.Session, CriteriaOperator.Parse("GetYear(GLHeader.DateExecution)=? and GetMonth(GLHeader.DateExecution)=? and GLHeader.IsEditable=False", _
                                                                                    _p_GLCassa.DateExecution.Year, _p_GLCassa.DateExecution.Month))
        Dim _GLRows As GLRows
        For Each _GLRows In _GLRows_Collection
            If TryCast(_GLRows.GLHeader, GLCassa) Is _p_GLCassa Then
                _SumInAmountHUF += _GLRows.InAmountHUF
                _SumOutAmountHUF = _GLRows.OutAmountHUF
            End If

        Next

        If _SumInAmountHUF >= 1500000 Or _SumOutAmountHUF > 1500000 Then
            _Valid = False
        End If
        Return _Valid

    End Function
    <VisibleInDetailView(False)> _
    ReadOnly Property Image1 As Image
        Get
            If Me.GLClosingTime IsNot Nothing Then
                Return ImageLoader.Instance.GetImageInfo("lock_warning").Image
            Else
                If IsStorno Then
                    Return ImageLoader.Instance.GetImageInfo("document_delete").Image
                Else
                    Return ImageLoader.Instance.GetImageInfo("edit").Image
                End If

            End If

        End Get
    End Property
    '-- Overrides property
    <RuleValueComparison("GLCassa-row_AmountDEV", "aToTable_GLCassa_Contexts", ValueComparisonType.GreaterThan, 0, TargetCriteria:="CurrencyName.ShortName != 'HUF' and row_CashType !=2 ")> _
    Overrides Property row_AmountDEV As Double
        Get
            Return MyBase.row_AmountDEV
        End Get
        Set(value As Double)
            MyBase.row_AmountDEV = value
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If row_CurrencyName2 Is Nothing Then row_CurrencyName2 = LiquidAssets_Cassa.CurrencyName
                If Me.row_CurrencyRate > 0 Then
                    Me.row_AmountHUF = value * Me.row_CurrencyRate
                End If
                If LiquidAssets_Cassa.CurrencyName.ShortName <> "HUF" Then
                    If LiquidAssets_Cassa.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
    Overrides Property row_AmountHUF As Double
        Get
            Return MyBase.row_AmountHUF
        End Get
        Set(value As Double)
            MyBase.row_AmountHUF = value
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If row_CurrencyName2 Is Nothing Then row_CurrencyName2 = LiquidAssets_Cassa.CurrencyName
                If LiquidAssets_Cassa.CurrencyName.ShortName = "HUF" Then
                    If LiquidAssets_Cassa.CurrencyName.ShortName = row_CurrencyName2.ShortName Then
                    Else
                        row_AmountDEV2 = row_AmountHUF * Math.Round(row_CurrencyRate2, 2, MidpointRounding.AwayFromZero)
                    End If
                End If
            End If
        End Set
    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 IsNot Nothing Then
                    row_ChartOfAccounts = value.GetChartOfAccounts(Me.DateExecution.Year, False)
                End If
            End If
        End Set
    End Property
    <RuleRequiredField("GLHeader.row_Customer.GLCassa", "aToTable_GLCassa_Contexts", targetcriteria:="row_PartnerType<>-1")> _
    Public Overrides Property row_Customer As Customers
        Get
            Return MyBase.row_Customer
        End Get
        Set(value As Customers)
            MyBase.row_Customer = value
        End Set
    End Property
End Class
