Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports System.Linq
Imports System.Linq.Expressions
Imports DevExpress.ExpressApp.SystemModule

Public Class GLOpenCloseVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork
    Public Sub New()
        MyBase.New()
        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
    End Sub

    Private Sub aGenerateOpenCloseRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGenerateOpenCloseRows.Execute
        Try

            GLOBAL_HAS_Audit = False


            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
            Dim _GLOpenCloseHeader As GLOpenCloseHeader = _uow.GetObjectByKey(Of GLOpenCloseHeader)(_onew.GetObject(TryCast(View.SelectedObjects(0), GLOpenCloseHeader)).Oid)
            'Dim _GLOpenCloseRows As GLOpenCloseRows

            'Dim _GLOpenCloseRows_Collection As New XPCollection(Of GLOpenCloseRows)(_uow, CriteriaOperator.Parse("GLOpenCloseHeader.Oid=?", _GLOpenCloseHeader.Oid))
            '_uow.Delete(_GLOpenCloseRows_Collection)

            Dim _ChartOfAccounts As ChartOfAccounts
            Dim _ChartOfAccounts_Collection As New XPCollection(Of ChartOfAccounts)(_uow, CriteriaOperator.Parse("AccountYear=? and IsParent=False", _GLOpenCloseHeader.AccountYearClose))

            Dim _DebitAmountHUF As Double = 0
            Dim _CreditAmountHUF As Double = 0
            Dim _GLRows_Collection As New XPQuery(Of GLRows)(_uow)

            Dim _ChartOfAccounts_491 As ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='491' and AccountYear=?", _GLOpenCloseHeader.AccountYearOpen))
            Dim _ChartOfAccounts_599 As ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='599' and AccountYear=?", _GLOpenCloseHeader.AccountYearClose))
            Dim _ChartOfAccounts_493 As ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='493' and AccountYear=?", _GLOpenCloseHeader.AccountYearClose))
            Dim _ChartOfAccounts_492 As ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='492' and AccountYear=?", _GLOpenCloseHeader.AccountYearClose))

            'RaiseEvent InitWork(1, 1, _ChartOfAccounts_Collection.Count)
            'For Each _ChartOfAccounts In _ChartOfAccounts_Collection
            '    RaiseEvent DoWork()
            '    _DebitAmountHUF = 0
            '    _CreditAmountHUF = 0

            '    Dim query_d = From _GLRows In _GLRows_Collection
            '    Where _GLRows.DebitChartOfAccounts.AccountNumber = _ChartOfAccounts.AccountNumber And _
            '          _GLRows.GLHeader.IsEditable = False And _
            '          _GLRows.GLHeader.DateExecution.Year >= _GLOpenCloseHeader.AccountYearClose And _
            '          _GLRows.GLHeader.CustomersFrom Is _GLOpenCloseHeader.CustomersFrom And _
            '          _GLRows.GLHeader.DateExecution.Year < _GLOpenCloseHeader.AccountYearOpen _
            '    Group _GLRows By _GLRows.DebitChartOfAccounts.AccountNumber Into gg = Group
            '    Select New With {Key .DebitAmountHUF = gg.Sum(Function(inv) inv.AmountHUF)}
            '    For Each item In query_d
            '        _DebitAmountHUF = Math.Round(item.DebitAmountHUF, 2, MidpointRounding.AwayFromZero)
            '    Next

            '    Dim query_c = From _GLRows In _GLRows_Collection
            '    Where _GLRows.CreditChartOfAccounts.AccountNumber = _ChartOfAccounts.AccountNumber And _
            '          _GLRows.GLHeader.IsEditable = False And _
            '          _GLRows.GLHeader.DateExecution.Year >= _GLOpenCloseHeader.AccountYearClose And _
            '          _GLRows.GLHeader.CustomersFrom Is _GLOpenCloseHeader.CustomersFrom And _
            '          _GLRows.GLHeader.DateExecution.Year < _GLOpenCloseHeader.AccountYearOpen _
            '    Group _GLRows By _GLRows.CreditChartOfAccounts.AccountNumber Into gg = Group
            '    Select New With {Key .CreditAmountHUF = gg.Sum(Function(inv) inv.AmountHUF)}
            '    For Each item In query_c
            '        _CreditAmountHUF = Math.Round(item.CreditAmountHUF, 2, MidpointRounding.AwayFromZero)
            '    Next

            '    If _DebitAmountHUF - _CreditAmountHUF <> 0 Then
            '        _GLOpenCloseRows = _uow.FindObject(Of GLOpenCloseRows)(CriteriaOperator.Parse("ChartOfAccounts.AccountNumber=? and GLOpenCloseHeader.Oid=?", _ChartOfAccounts.AccountNumber, _GLOpenCloseHeader.Oid))
            '        If _GLOpenCloseRows Is Nothing Then
            '            _GLOpenCloseRows = New GLOpenCloseRows(_uow)
            '            _GLOpenCloseRows.GLOpenCloseHeader = _GLOpenCloseHeader
            '        End If

            '        If _DebitAmountHUF >= _CreditAmountHUF Then
            '            _GLOpenCloseRows.DebitAmountHUF = _DebitAmountHUF - _CreditAmountHUF
            '        Else
            '            _GLOpenCloseRows.CreditAmountHUF = _CreditAmountHUF - _DebitAmountHUF
            '        End If

            '        _GLOpenCloseRows.ChartOfAccounts = _ChartOfAccounts


            '        Select Case Mid(_ChartOfAccounts.AccountNumber, 1, 1)
            '            Case "5"
            '                _GLOpenCloseRows.OnlyClose = True
            '                _GLOpenCloseRows.ChartOfAccountsClose = _ChartOfAccounts_599
            '            Case "8", "9"
            '                _GLOpenCloseRows.OnlyClose = True
            '                _GLOpenCloseRows.ChartOfAccountsClose = _ChartOfAccounts_493
            '            Case Else
            '                _GLOpenCloseRows.OnlyClose = False
            '                _GLOpenCloseRows.ChartOfAccountsClose = _ChartOfAccounts_492
            '                _GLOpenCloseRows.ChartOfAccountsOpen = _ChartOfAccounts_491
            '        End Select
            '    End If
            '    _uow.CommitChanges()
            'Next
            '_uow.CommitChanges()
            'RaiseEvent FinalWork

            Frame.GetController(Of Nuvolar.Module.TrialBalanceVC).LoadTrialBalanceNew(_onew, _GLOpenCloseHeader.TrialBalanceHeaderBeforeClose)
            Frame.GetController(Of Nuvolar.Module.TrialBalanceVC).LoadTrialBalanceOpenClose(_onew, _GLOpenCloseHeader.TrialBalanceHeaderBeforeClose)
            _uow.CommitChanges()
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "HIBA !")
        Finally
            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork
        End Try

    End Sub
    Private Sub aCloseAccountYearBallance_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCloseAccountYearBallance.Execute
        '// Mérleg számlák lezárása csak akkor, ha már van eredményszámla lezárás
        '// K1-4 T492 12.31
        '// T1-4 K491 01.01


        '// Nem lehet olyan tétel, ami nincs feladva erre az évre !
        '// InvoiceHeader
        '// Registryheader
        Try
            GLOBAL_HAS_Audit = False
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _GLOpenCloseHeader As GLOpenCloseHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), GLOpenCloseHeader))
            'Dim _GLOpenCloseRows As GLOpenCloseRows
            Dim _TrialBalance As TrialBalance
            Dim _LiquidAssetsYear As LiquidAssetsYear
            Dim _GLBank As GLBank
            Dim _GLCassa As GLCassa

            Dim _GLRows_Close As GLRows
            Dim _ChartOfAccounts_492 As ChartOfAccounts = _onew.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='492' and AccountYear=?", _GLOpenCloseHeader.AccountYearClose))

            Dim IsOk As Boolean = True
            Dim _ChartOfAccounts_Ok As ChartOfAccounts = Nothing
            '// Ellenõrzések ---------------------------------------------------------------------------
            'RaiseEvent InitWork(1, 1, _GLOpenCloseHeader.GLOpenCloseRows.Count)
            'For Each _GLOpenCloseRows In _GLOpenCloseHeader.GLOpenCloseRows
            '    RaiseEvent DoWork()
            '    Select Case Mid(_GLOpenCloseRows.ChartOfAccounts.AccountNumber, 1, 1)
            '        Case "1", "2", "3", "4"
            '            _ChartOfAccounts_Ok = _onew.FindObject(Of ChartOfAccounts) _
            '                                (CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", _GLOpenCloseRows.ChartOfAccounts.AccountNumber, _GLOpenCloseHeader.AccountYearOpen))
            '            If _ChartOfAccounts_Ok Is Nothing Then
            '                RaiseEvent FinalWork
            '                Throw New Exception(String.Format("Nincs megnyitva ez a főkönyvi számla: {0}", _GLOpenCloseRows.ChartOfAccounts.Name))
            '            End If
            '    End Select
            'Next
            'RaiseEvent FinalWork
            '// Zárási bizonylat -----------------------------------------------------------------------
            If _GLOpenCloseHeader.GLMixed_AfterClose_Ballance IsNot Nothing Then
                _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterClose_Ballance.GLRows)
            Else
                _GLOpenCloseHeader.GLMixed_AfterClose_Ballance = _onew.CreateObject(Of GLMixed)()
            End If


            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.CurrencyName = _onew.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.CurrencyRate = 1
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.CustomersFrom = _GLOpenCloseHeader.CustomersFrom
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.DateCreated = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.DateExecution = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.DatePayment = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.DocumentNumber = _GLOpenCloseHeader.ShortName & " - mérlegszámla záró"
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.GLDocumentType = eGLDocumentType.eGLMixed
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.GLOpenCloseHeader = _GLOpenCloseHeader
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.IsEditable = False
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.IsStorno = False
            _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.NoteHeader = ""

            RaiseEvent InitWork(1, 1, _GLOpenCloseHeader.TrialBalanceHeaderAfterDividend.TrialBalance.Count)
            For Each _TrialBalance In _GLOpenCloseHeader.TrialBalanceHeaderAfterDividend.TrialBalance
                RaiseEvent DoWork()
                Select Case Mid(_TrialBalance.ChartOfAccounts.AccountNumber, 1, 1)
                    Case "1", "2", "3", "4"
                        If _TrialBalance.ChartOfAccounts.IsParent = False Then
                            _GLRows_Close = _onew.CreateObject(Of GLRows)()
                            _GLRows_Close.GLHeader = _GLOpenCloseHeader.GLMixed_AfterClose_Ballance
                            _GLRows_Close.GLRowsGUID = Guid.NewGuid
                            If (_TrialBalance.DebitAmountHUF - _TrialBalance.CreditAmountHUF) > 0 Then
                                _GLRows_Close.DebitChartOfAccounts = _ChartOfAccounts_492
                                _GLRows_Close.CreditChartOfAccounts = _TrialBalance.ChartOfAccounts
                                _GLRows_Close.AmountHUF = Math.Abs(_TrialBalance.DebitAmountHUF - _TrialBalance.CreditAmountHUF)
                                _LiquidAssetsYear = _onew.FindObject(Of LiquidAssetsYear) _
                                                                        (CriteriaOperator.Parse("AccountYear=? and ChartOfAccounts=? and LiquidAssets.CustomerFrom.Oid=?", _
                                                                        _GLRows_Close.GLHeader.DateExecution.Year, _GLRows_Close.CreditChartOfAccounts, _GLOpenCloseHeader.CustomersFrom.Oid))
                                '// Ha pénzeszköz, akkor HUF + DEV érték lezárása is !
                                If _LiquidAssetsYear IsNot Nothing Then
                                    _GLRows_Close.LiquidAssets = _LiquidAssetsYear.LiquidAssets

                                    Select Case _LiquidAssetsYear.LiquidAssets.LiquidAssetsType
                                        Case eLiquidAssetsType.NormalBank
                                            _GLBank = _onew.FindObject(Of GLBank)(CriteriaOperator.Parse("LiquidAssets_Main=?", _LiquidAssetsYear.LiquidAssets))
                                            If _GLBank IsNot Nothing Then
                                                _GLRows_Close.AmountDEV = _GLBank.GetBallanceDEV(_LiquidAssetsYear.LiquidAssets, _GLRows_Close.GLHeader.DateExecution)
                                            End If
                                        Case eLiquidAssetsType.DepositCheckout, eLiquidAssetsType.NormalCheckout, eLiquidAssetsType.CurrencyCheckout
                                            _GLCassa = _onew.FindObject(Of GLCassa)(CriteriaOperator.Parse("LiquidAssets_Cassa=?", _LiquidAssetsYear.LiquidAssets))
                                            If _GLCassa IsNot Nothing Then
                                                _GLRows_Close.AmountDEV = _GLCassa.GetBallanceDEV(_LiquidAssetsYear.LiquidAssets, _GLRows_Close.GLHeader.DateExecution)
                                            End If
                                    End Select

                                End If
                            Else
                                _GLRows_Close.CreditChartOfAccounts = _ChartOfAccounts_492
                                _GLRows_Close.DebitChartOfAccounts = _TrialBalance.ChartOfAccounts
                                _GLRows_Close.AmountHUF = Math.Abs(_TrialBalance.DebitAmountHUF - _TrialBalance.CreditAmountHUF)
                                _LiquidAssetsYear = _onew.FindObject(Of LiquidAssetsYear) _
                                                                        (CriteriaOperator.Parse("AccountYear=? and ChartOfAccounts=? and LiquidAssets.CustomerFrom.Oid=?", _
                                                                        _GLRows_Close.GLHeader.DateExecution.Year, _GLRows_Close.DebitChartOfAccounts, _GLOpenCloseHeader.CustomersFrom.Oid))
                                '// Ha pénzeszköz, akkor HUF + DEV érték lezárása is !
                                If _LiquidAssetsYear IsNot Nothing Then
                                    _GLRows_Close.LiquidAssets = _LiquidAssetsYear.LiquidAssets

                                    Select Case _LiquidAssetsYear.LiquidAssets.LiquidAssetsType
                                        Case eLiquidAssetsType.NormalBank
                                            _GLBank = _onew.FindObject(Of GLBank)(CriteriaOperator.Parse("LiquidAssets_Main=?", _LiquidAssetsYear.LiquidAssets))
                                            If _GLBank IsNot Nothing Then
                                                _GLRows_Close.AmountDEV = _GLBank.GetBallanceDEV(_LiquidAssetsYear.LiquidAssets, _GLRows_Close.GLHeader.DateExecution)
                                            End If
                                        Case eLiquidAssetsType.DepositCheckout, eLiquidAssetsType.NormalCheckout, eLiquidAssetsType.CurrencyCheckout
                                            _GLCassa = _onew.FindObject(Of GLCassa)(CriteriaOperator.Parse("LiquidAssets_Cassa=?", _LiquidAssetsYear.LiquidAssets))
                                            If _GLCassa IsNot Nothing Then
                                                _GLRows_Close.AmountDEV = _GLCassa.GetBallanceDEV(_LiquidAssetsYear.LiquidAssets, _GLRows_Close.GLHeader.DateExecution)
                                            End If
                                    End Select

                                End If
                            End If
                            _GLRows_Close.DateExecution = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
                            _GLRows_Close.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
                            _GLRows_Close.NoteRows = "-> 492"
                            _GLRows_Close.PartnerType = ePartnerType.eNotSet
                            _GLRows_Close.TransactionType = eTransactionType.eNotSet


                        End If
                End Select
            Next
            RaiseEvent FinalWork
            _GLOpenCloseHeader.IsClosedSheetProfit = True
            _GLOpenCloseHeader.IsClosedBallance = True
            _onew.CommitChanges()

            '// Záró utáni fk. kivonat lekérdezése !!!
            Frame.GetController(Of Nuvolar.Module.TrialBalanceVC).LoadTrialBalanceNew(_onew, _GLOpenCloseHeader.TrialBalanceHeaderAfterClose_Ballance)
            Frame.GetController(Of Nuvolar.Module.TrialBalanceVC).LoadTrialBalanceOpenClose(_onew, _GLOpenCloseHeader.TrialBalanceHeaderAfterClose_Ballance)
            _onew.CommitChanges()
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "HIBA !")
        Finally

            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork
        End Try

    End Sub
    Private Sub aCloseAccountYearSheetProfit_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCloseAccountYearSheetProfit.Execute
        '// Eredményszámlák zárása
        '// 5 -> K599 -> T419
        Try
            GLOBAL_HAS_Audit = False
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _GLOpenCloseHeader As GLOpenCloseHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), GLOpenCloseHeader))

            Dim _GLRows As GLRows

            Dim _AmountHUF_599 As Double = 0
            '// Ellenõrzések ---------------------------------------------------------------------------
            'Dim _InvoiceHeader_Collection As New XPCollection(Of InvoiceHeader)(_ocur.Session, CriteriaOperator.Parse("GetYear(DateExecution)=? and isnull(GLAccounts)=True and InvoiceType.InvoiceTypeBase in (0,1,2,3)", _GLOpenCloseHeader.AccountYearClose))
            'If _InvoiceHeader_Collection.Count > 0 Then
            '    Throw New Exception("*Van még erre az évre olyan kimenõ számla, ami nincs lekönyvelve !")
            'End If
            'Dim _RegistryHeader_Collection As New XPCollection(Of RegistryHeader)(_ocur.Session, CriteriaOperator.Parse("GetYear(F_DateExecution)=? and isnull(F_GLAccounts)=True and RegistryType.RegistryMainType in (0,2,3,4)", _GLOpenCloseHeader.AccountYearClose))
            'If _RegistryHeader_Collection.Count > 0 Then
            '    Throw New Exception("*Van még erre az évre olyan iktatott számla, ami nincs lekönyvelve !")
            'End If

            '// Ellenõrzés, hogy van-e 599 fk. számla és az levél-e?
            Dim _ChartOfAccounts_599 As ChartOfAccounts = _onew.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountYear=? and AccountNumber='599'", _GLOpenCloseHeader.AccountYearClose))
            If _ChartOfAccounts_599 IsNot Nothing Then
                If _ChartOfAccounts_599.Children.Count > 0 Then
                    Throw New Exception("*Nem könyvelhetõ az 599 költség záró számla, mert vannak gyermekei !")
                End If
            Else
                Throw New Exception("*Nincs 599 költség záró számla !")
            End If

            '// Ellenõrzés, hogy van-e 493 fk. számla és az levél-e?
            Dim _ChartOfAccounts_493 As ChartOfAccounts = _onew.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountYear=? and AccountNumber='493'", _GLOpenCloseHeader.AccountYearClose))
            If _ChartOfAccounts_493 IsNot Nothing Then
                If _ChartOfAccounts_493.Children.Count > 0 Then
                    Throw New Exception("*Nem könyvelhetõ az 493 számla, mert vannak gyermekei !")
                End If
            Else
                Throw New Exception("*Nincs 493 eredmény záró számla !")
            End If

            '// 599 - 493 Zárási bizonylat -----------------------------------------------------------------------
            If _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit IsNot Nothing Then
                _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.GLRows)
            Else
                _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit = _onew.CreateObject(Of GLMixed)()
            End If

            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.CurrencyName = _onew.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.CurrencyRate = 1
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.CustomersFrom = _GLOpenCloseHeader.CustomersFrom
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.DateCreated = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.DateExecution = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.DatePayment = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.DocumentNumber = _GLOpenCloseHeader.ShortName & " - eredményszámla záró"
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.GLDocumentType = eGLDocumentType.eGLMixed
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.GLOpenCloseHeader = _GLOpenCloseHeader
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.IsEditable = False
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.IsStorno = False
            _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.NoteHeader = ""

            RaiseEvent InitWork(1, 1, _GLOpenCloseHeader.TrialBalanceHeaderBeforeClose.TrialBalance.Count)
            For Each _TrialBalance As TrialBalance In _GLOpenCloseHeader.TrialBalanceHeaderBeforeClose.TrialBalance
                RaiseEvent DoWork()
                If _TrialBalance.ChartOfAccounts.AccountNumber Like "5*" And _TrialBalance.ChartOfAccounts.IsParent = False Then
                    _GLRows = _onew.CreateObject(Of GLRows)()
                    _GLRows.GLHeader = _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit
                    _GLRows.GLRowsGUID = Guid.NewGuid
                    If (_TrialBalance.DebitAmountHUF - _TrialBalance.CreditAmountHUF) > 0 Then
                        _GLRows.DebitChartOfAccounts = _ChartOfAccounts_599
                        _GLRows.CreditChartOfAccounts = _TrialBalance.ChartOfAccounts
                        _GLRows.AmountHUF = Math.Abs(_TrialBalance.DebitAmountHUF - _TrialBalance.CreditAmountHUF)
                        _AmountHUF_599 += _GLRows.AmountHUF
                    Else
                        _GLRows.CreditChartOfAccounts = _ChartOfAccounts_599
                        _GLRows.DebitChartOfAccounts = _TrialBalance.ChartOfAccounts
                        _GLRows.AmountHUF = Math.Abs(_TrialBalance.DebitAmountHUF - _TrialBalance.CreditAmountHUF)
                        _AmountHUF_599 -= _GLRows.AmountHUF
                    End If
                    _GLRows.DateExecution = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
                    _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
                    _GLRows.NoteRows = "-> 599"
                    _GLRows.PartnerType = ePartnerType.eNotSet
                    _GLRows.TransactionType = eTransactionType.eNotSet
                End If
            Next
            RaiseEvent FinalWork
            '// K599 +K8 + T9 -> 493-re 
            _GLRows = _onew.CreateObject(Of GLRows)()
            _GLRows.GLHeader = _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit
            _GLRows.GLRowsGUID = Guid.NewGuid
            If _AmountHUF_599 > 0 Then
                _GLRows.DebitChartOfAccounts = _ChartOfAccounts_493
                _GLRows.CreditChartOfAccounts = _ChartOfAccounts_599
                _GLRows.AmountHUF = _AmountHUF_599
            Else
                _GLRows.CreditChartOfAccounts = _ChartOfAccounts_493
                _GLRows.DebitChartOfAccounts = _ChartOfAccounts_599
                _GLRows.AmountHUF = _AmountHUF_599
            End If
            _GLRows.DateExecution = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
            _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
            _GLRows.NoteRows = "599 -> 493"
            _GLRows.PartnerType = ePartnerType.eNotSet
            _GLRows.TransactionType = eTransactionType.eNotSet

            RaiseEvent InitWork(1, 1, _GLOpenCloseHeader.TrialBalanceHeaderBeforeClose.TrialBalance.Count)
            For Each _TrialBalance As TrialBalance In _GLOpenCloseHeader.TrialBalanceHeaderBeforeClose.TrialBalance
                RaiseEvent DoWork()
                If (_TrialBalance.ChartOfAccounts.AccountNumber Like "8*" Or _TrialBalance.ChartOfAccounts.AccountNumber Like "9*") And _TrialBalance.ChartOfAccounts.IsParent = False Then
                    _GLRows = _onew.CreateObject(Of GLRows)()
                    _GLRows.GLHeader = _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit
                    _GLRows.GLRowsGUID = Guid.NewGuid
                    If (_TrialBalance.DebitAmountHUF - _TrialBalance.CreditAmountHUF) > 0 Then
                        _GLRows.DebitChartOfAccounts = _ChartOfAccounts_493
                        _GLRows.CreditChartOfAccounts = _TrialBalance.ChartOfAccounts
                        _GLRows.AmountHUF = Math.Abs(_TrialBalance.DebitAmountHUF - _TrialBalance.CreditAmountHUF)
                    Else
                        _GLRows.CreditChartOfAccounts = _ChartOfAccounts_493
                        _GLRows.DebitChartOfAccounts = _TrialBalance.ChartOfAccounts
                        _GLRows.AmountHUF = Math.Abs(_TrialBalance.DebitAmountHUF - _TrialBalance.CreditAmountHUF)
                    End If
                    _GLRows.DateExecution = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
                    _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
                    _GLRows.NoteRows = ""
                    _GLRows.PartnerType = ePartnerType.eNotSet
                    _GLRows.TransactionType = eTransactionType.eNotSet
                End If
            Next

            '// Beállítani, hogy megtörtént az eredményszámlák zárása

            _GLOpenCloseHeader.IsClosedSheetProfit = True
            _onew.CommitChanges()
            RaiseEvent FinalWork

            '// Záró utáni fk. kivonat lekérdezése !!!
            Frame.GetController(Of Nuvolar.Module.TrialBalanceVC).LoadTrialBalanceNew(_onew, _GLOpenCloseHeader.TrialBalanceHeaderAfterClose_SheetProfit)
            Frame.GetController(Of Nuvolar.Module.TrialBalanceVC).LoadTrialBalanceOpenClose(_onew, _GLOpenCloseHeader.TrialBalanceHeaderAfterClose_SheetProfit)
            _onew.CommitChanges()

            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "HIBA !")
        Finally
            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork
        End Try

    End Sub
    Private Sub aReopenAccountYear_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aReopenAccountYear.Execute
        '// Lezárt év visszanyitása
        Try
            GLOBAL_HAS_Audit = False

            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _GLOpenCloseHeader As GLOpenCloseHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), GLOpenCloseHeader))

            If _GLOpenCloseHeader IsNot Nothing Then


                RaiseEvent InitWork(1, 1, 5)
                RaiseEvent DoWork()
                Dim _GLRows_Collection As New XPCollection(Of GLRows)(_onew.Session, CriteriaOperator.Parse("GLHeader.GLOpenCloseHeader=?", _GLOpenCloseHeader))
                _onew.Delete(_GLRows_Collection)

                RaiseEvent DoWork()
                Dim _GLMixed_Collection As New XPCollection(Of GLMixed)(_onew.Session, CriteriaOperator.Parse("GLOpenCloseHeader=?", _GLOpenCloseHeader))
                _onew.Delete(_GLMixed_Collection)

                _GLOpenCloseHeader.IsClosedBallance = False
                _GLOpenCloseHeader.IsClosedSheetProfit = False
                RaiseEvent DoWork()

                '// Itt elég sok mindent kell törölni !
                If _GLOpenCloseHeader.TrialBalanceHeaderAfterClose_Ballance IsNot Nothing Then
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterClose_Ballance.TrialBalance)
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterClose_Ballance.TrialBalanceOpenClose)
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterClose_Ballance.TrialBalanceSum)
                End If
                If _GLOpenCloseHeader.TrialBalanceHeaderAfterClose_SheetProfit IsNot Nothing Then
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterClose_SheetProfit.TrialBalance)
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterClose_SheetProfit.TrialBalanceOpenClose)
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterClose_SheetProfit.TrialBalanceSum)
                End If
                If _GLOpenCloseHeader.TrialBalanceHeaderAfterDividend IsNot Nothing Then
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterDividend.TrialBalance)
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterDividend.TrialBalanceOpenClose)
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterDividend.TrialBalanceSum)
                End If

                If _GLOpenCloseHeader.TrialBalanceHeaderAfterOpen IsNot Nothing Then
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterOpen.TrialBalance)
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterOpen.TrialBalanceOpenClose)
                    _onew.Delete(_GLOpenCloseHeader.TrialBalanceHeaderAfterOpen.TrialBalanceSum)
                End If
                RaiseEvent DoWork()
                If _GLOpenCloseHeader.GLMixed_AfterClose_Ballance IsNot Nothing Then
                    _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterClose_Ballance.GLRows)
                    _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterClose_Ballance)
                End If

                If _GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit IsNot Nothing Then
                    _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit.GLRows)
                    _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterClose_SheetProfit)
                End If

                If _GLOpenCloseHeader.GLMixed_AfterDividend IsNot Nothing Then
                    _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterDividend.GLRows)
                    _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterDividend)
                End If

                If _GLOpenCloseHeader.GLMixed_AfterOpen IsNot Nothing Then
                    _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterOpen.GLRows)
                    _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterOpen)
                End If
                RaiseEvent DoWork()
                _GLOpenCloseHeader.IsClosedBallance = False
                _GLOpenCloseHeader.IsClosedSheetProfit = False
                _GLOpenCloseHeader.IsDividend = False
                _GLOpenCloseHeader.IsOpenNew = False
                _onew.CommitChanges()

                RaiseEvent FinalWork
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "HIBA !")
        Finally
            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork
        End Try
    End Sub
    Private Sub aOpenNewAccountYear_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenNewAccountYear.Execute
        Try
            GLOBAL_HAS_Audit = False
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace


            Dim _GLOpenCloseHeader As GLOpenCloseHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), GLOpenCloseHeader))
            Dim _GLRows_Open As GLRows

            Dim _LiquidAssetsYear_Collection As New XPCollection(Of LiquidAssetsYear)(_onew.Session, CriteriaOperator.Parse("AccountYear=? and LiquidAssets.CustomerFrom.Oid=? and LiquidAssets.CurrencyName.ShortName<>'HUF'", _GLOpenCloseHeader.AccountYearOpen, _GLOpenCloseHeader.CustomersFrom.Oid))
            Dim _LiquidAssetsYear As LiquidAssetsYear

            Dim _ChartOfAccounts_491 As ChartOfAccounts = _onew.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber='491' and AccountYear=?", _GLOpenCloseHeader.AccountYearOpen))

            If _GLOpenCloseHeader.GLMixed_AfterOpen Is Nothing Then
                _GLOpenCloseHeader.GLMixed_AfterOpen = _onew.CreateObject(Of GLMixed)()
            Else
                _onew.Delete(_GLOpenCloseHeader.GLMixed_AfterOpen.GLRows)
            End If
            _GLOpenCloseHeader.IsOpenNew = True
            _GLOpenCloseHeader.GLMixed_AfterOpen = _onew.CreateObject(Of GLMixed)()
            _GLOpenCloseHeader.GLMixed_AfterOpen.CurrencyName = _onew.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
            _GLOpenCloseHeader.GLMixed_AfterOpen.CurrencyRate = 1
            _GLOpenCloseHeader.GLMixed_AfterOpen.CustomersFrom = _GLOpenCloseHeader.CustomersFrom
            _GLOpenCloseHeader.GLMixed_AfterOpen.DateCreated = New Date(_GLOpenCloseHeader.AccountYearOpen, 1, 1)
            _GLOpenCloseHeader.GLMixed_AfterOpen.DateExecution = New Date(_GLOpenCloseHeader.AccountYearOpen, 1, 1)
            _GLOpenCloseHeader.GLMixed_AfterOpen.DatePayment = New Date(_GLOpenCloseHeader.AccountYearOpen, 1, 1)
            _GLOpenCloseHeader.GLMixed_AfterOpen.DocumentNumber = _GLOpenCloseHeader.ShortName & " - mérlegszámla nyitó"
            _GLOpenCloseHeader.GLMixed_AfterOpen.GLDocumentType = eGLDocumentType.eGLMixed
            _GLOpenCloseHeader.GLMixed_AfterOpen.GLOpenCloseHeader = _GLOpenCloseHeader
            _GLOpenCloseHeader.GLMixed_AfterOpen.IsEditable = False
            _GLOpenCloseHeader.GLMixed_AfterOpen.IsStorno = False
            _GLOpenCloseHeader.GLMixed_AfterOpen.NoteHeader = ""

            RaiseEvent InitWork(1, 1, _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.GLRows.Count)
            For Each _GLRows_Close In _GLOpenCloseHeader.GLMixed_AfterClose_Ballance.GLRows
                RaiseEvent DoWork()
                _GLRows_Open = _onew.CreateObject(Of GLRows)()
                _GLRows_Open.GLHeader = _GLOpenCloseHeader.GLMixed_AfterOpen
                _GLRows_Open.GLRowsGUID = Guid.NewGuid

                _GLRows_Open.AmountHUF = _GLRows_Close.AmountHUF
                _GLRows_Open.AmountDEV = _GLRows_Close.AmountDEV

                If _GLRows_Close.CreditChartOfAccounts.AccountNumber = 492 Then
                    _GLRows_Open.DebitChartOfAccounts = _ChartOfAccounts_491
                    _GLRows_Open.CreditChartOfAccounts = _onew.FindObject(Of ChartOfAccounts) _
                        (CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", _GLRows_Close.DebitChartOfAccounts.AccountNumber, _GLOpenCloseHeader.AccountYearOpen))
                Else
                    _GLRows_Open.CreditChartOfAccounts = _ChartOfAccounts_491
                    _GLRows_Open.DebitChartOfAccounts = _onew.FindObject(Of ChartOfAccounts) _
                        (CriteriaOperator.Parse("AccountNumber=? and AccountYear=?", _GLRows_Close.CreditChartOfAccounts.AccountNumber, _GLOpenCloseHeader.AccountYearOpen))
                    For Each _LiquidAssetsYear In _LiquidAssetsYear_Collection
                        If _GLRows_Open.DebitChartOfAccounts.AccountNumber = _LiquidAssetsYear.ChartOfAccounts.AccountNumber Then

                            Dim _LiquidAssetsRate As LiquidAssetsRate = _onew.FindObject(Of LiquidAssetsRate)(CriteriaOperator.Parse("DateExecution=? and LiquidAssets.Oid=?", New Date(_GLOpenCloseHeader.AccountYearOpen, 1, 1), _LiquidAssetsYear.LiquidAssets.Oid))
                            If _LiquidAssetsRate Is Nothing Then
                                _LiquidAssetsRate = _onew.CreateObject(Of LiquidAssetsRate)()
                            End If
                            _LiquidAssetsRate.LiquidAssets = _LiquidAssetsYear.LiquidAssets
                            _LiquidAssetsRate.DateExecution = New Date(_GLOpenCloseHeader.AccountYearOpen, 1, 1)
                            _LiquidAssetsRate.AmountHUF = _GLRows_Close.AmountHUF
                            _LiquidAssetsRate.AmountDEV = _GLRows_Close.AmountDEV

                        End If
                    Next
                End If


                _GLRows_Open.DateExecution = New Date(_GLOpenCloseHeader.AccountYearClose, 12, 31)
                _GLRows_Open.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow
                _GLRows_Open.NoteRows = "491 ->"
                _GLRows_Open.PartnerType = ePartnerType.eNotSet
                _GLRows_Open.TransactionType = eTransactionType.eNotSet

            Next
            RaiseEvent FinalWork

            _onew.CommitChanges()

            Frame.GetController(Of Nuvolar.Module.TrialBalanceVC).LoadTrialBalanceNew(_onew, _GLOpenCloseHeader.TrialBalanceHeaderAfterOpen)
            Frame.GetController(Of Nuvolar.Module.TrialBalanceVC).LoadTrialBalanceOpenClose(_onew, _GLOpenCloseHeader.TrialBalanceHeaderAfterOpen)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "HIBA !")
        Finally
            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork
        End Try

    End Sub
    Private Sub aGenerateTrialBalanceDividend_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGenerateTrialBalanceDividend.Execute
        Try
            GLOBAL_HAS_Audit = False
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _GLOpenCloseHeader As GLOpenCloseHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), GLOpenCloseHeader))

            _GLOpenCloseHeader.IsDividend = True
            Frame.GetController(Of Nuvolar.Module.TrialBalanceVC).LoadTrialBalanceNew(_onew, _GLOpenCloseHeader.TrialBalanceHeaderAfterDividend)
            Frame.GetController(Of Nuvolar.Module.TrialBalanceVC).LoadTrialBalanceOpenClose(_onew, _GLOpenCloseHeader.TrialBalanceHeaderAfterDividend)
            _onew.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "HIBA !")
        Finally
            GLOBAL_HAS_Audit = False
        End Try

    End Sub

    Private Sub aRepairBadGLRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRepairBadGLRows.Execute
        Try
            GLOBAL_HAS_Audit = False
            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
            Dim _GLOpenCloseHeader As GLOpenCloseHeader = TryCast(View.SelectedObjects(0), GLOpenCloseHeader)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
            Dim _GLRows_Collection As New XPCollection(Of GLRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _uow, CriteriaOperator.Parse("GLHeader.IsEditable=False and GetYear(GLHeader.DateExecution)=?", _GLOpenCloseHeader.AccountYearClose))
            Dim _GLRows As GLRows
            Dim _ChartOfAccounts As ChartOfAccounts

            RaiseEvent InitWork(1, 1, _GLRows_Collection.Count)
            For Each _GLRows In _GLRows_Collection
                RaiseEvent DoWork()

                '// Törölt FK. számla rendberakása
                If _GLRows.DebitChartOfAccounts.IsDeleted Then
                    _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountYear=? and AccountNumber=?", _GLRows.GLHeader.DateExecution.Year, _GLRows.DebitChartOfAccounts.AccountNumber))
                    If _ChartOfAccounts IsNot Nothing Then
                        _GLRows.DebitChartOfAccounts = _ChartOfAccounts
                    End If
                End If
                If _GLRows.CreditChartOfAccounts.IsDeleted Then
                    _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountYear=? and AccountNumber=?", _GLRows.GLHeader.DateExecution.Year, _GLRows.CreditChartOfAccounts.AccountNumber))
                    If _ChartOfAccounts IsNot Nothing Then
                        _GLRows.CreditChartOfAccounts = _ChartOfAccounts
                    End If
                End If

                '// Nem azonos év rendberakása

            Next
            _uow.CommitChanges()
            RaiseEvent FinalWork
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "HIBA !")
        Finally
            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork
            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
        End Try

    End Sub
End Class
