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.Editors
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.CloneObject

Public Class TrialBalanceVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Structure sReq
        Dim AccountNumber As String
        Dim ParentAccountNumber As String
        Dim DebitAmountHUF As Double
        Dim CreditAmountHUF As Double
        Dim DebitAmountDEV As Double
        Dim CreditAmountDEV As Double
        Dim IsLastLevel As Boolean
    End Structure
    Dim elem As sReq
    Dim sArray(0 To 10000) As sReq
    Dim Index As Integer
    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()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _User As User
        If _ocur IsNot Nothing Then
            _User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
        End If
        MyBase.OnActivated()
        Frame.GetController(Of TrialBalanceVC).aTrialBalanceViewCard.Active.SetItemValue("", False)

        If View.Id = "TrialBalanceHeader_TrialBalanceOpenClose_ListView" Or _
            View.Id = "TrialBalanceHeader_TrialBalance_ListView" Then
            Frame.GetController(Of TrialBalanceVC).aTrialBalanceViewCard.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If

        If View.Id = "TrialBalanceHeader_DetailView" Then
            Dim _TrialBalanceHeader As TrialBalanceHeader = TryCast(View.SelectedObjects(0), TrialBalanceHeader)

            If _TrialBalanceHeader.UserCreated IsNot Nothing Then
                If Not _TrialBalanceHeader.UserCreated.Equals(_User) Then
                    Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
                    Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
                    Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
                    Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
                    Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
                    Frame.GetController(Of TrialBalanceVC).aTrialBalanceQuery.Active.SetItemValue("", False)
                    Frame.GetController(Of TrialBalanceVC).aTrialBalanceOpenCloseQuery.Active.SetItemValue("", False)
                End If
            End If
        End If

        If View.Id = "TrialBalanceHeader_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteria = String.Format("UserCreated.Oid='{0}' or 'SysAdmin'='{1}'", _User.Oid, SecuritySystem.CurrentUserName)
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteriaMode = TargetObjectsCriteriaMode.TrueForAll
        End If
        If View.Id = "TrialBalanceHeader_DetailView_BeforeClose" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Dim _GLOpenCloseHeader = TryCast(TryCast(Frame, NestedFrame).ViewItem.View.SelectedObjects(0), GLOpenCloseHeader)
            If _GLOpenCloseHeader IsNot Nothing Then
                View.CurrentObject = _ocur.GetObject(_GLOpenCloseHeader.TrialBalanceHeaderBeforeClose)
            End If
        End If
        If View.Id = "TrialBalanceHeader_DetailView_AfterClose_SheetProfit" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Dim _GLOpenCloseHeader = TryCast(TryCast(Frame, NestedFrame).ViewItem.View.SelectedObjects(0), GLOpenCloseHeader)
            View.CurrentObject = _ocur.GetObject(_GLOpenCloseHeader.TrialBalanceHeaderAfterClose_SheetProfit)
        End If
        If View.Id = "TrialBalanceHeader_DetailView_AfterClose_Ballance" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Dim _GLOpenCloseHeader = TryCast(TryCast(Frame, NestedFrame).ViewItem.View.SelectedObjects(0), GLOpenCloseHeader)
            View.CurrentObject = _ocur.GetObject(_GLOpenCloseHeader.TrialBalanceHeaderAfterClose_Ballance)
        End If
        If View.Id = "TrialBalanceHeader_DetailView_AfterOpen" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Dim _GLOpenCloseHeader = TryCast(TryCast(Frame, NestedFrame).ViewItem.View.SelectedObjects(0), GLOpenCloseHeader)
            View.CurrentObject = _ocur.GetObject(_GLOpenCloseHeader.TrialBalanceHeaderAfterOpen)
        End If
        If View.Id = "TrialBalanceHeader_DetailView_AfterDividend" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Dim _GLOpenCloseHeader = TryCast(TryCast(Frame, NestedFrame).ViewItem.View.SelectedObjects(0), GLOpenCloseHeader)
            View.CurrentObject = _ocur.GetObject(_GLOpenCloseHeader.TrialBalanceHeaderAfterDividend)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of TrialBalanceVC).aTrialBalanceViewCard.Active.SetItemValue("", False)

        If View.Id = "TrialBalanceHeader_DetailView" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _TrialBalanceHeader As TrialBalanceHeader = TryCast(View.SelectedObjects(0), TrialBalanceHeader)
            Dim _User As User
            If _ocur IsNot Nothing Then
                _User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
            End If

            If _TrialBalanceHeader.UserCreated IsNot Nothing Then
                If Not _TrialBalanceHeader.UserCreated.Equals(_User) Then
                    Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", True)
                    Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", True)
                    Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", True)
                    Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
                    Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
                    Frame.GetController(Of TrialBalanceVC).aTrialBalanceQuery.Active.SetItemValue("", True)
                End If
            End If
        End If
    End Sub

    Private Sub aTrialBalanceQuery_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTrialBalanceQuery.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _TrialBalanceHeader As TrialBalanceHeader = TryCast(View.SelectedObjects(0), TrialBalanceHeader)
        LoadTrialBalanceNew(_ocur, _TrialBalanceHeader)
        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub aTrialBalanceOpenCloseQuery_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTrialBalanceOpenCloseQuery.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _TrialBalanceHeader As TrialBalanceHeader = TryCast(View.SelectedObjects(0), TrialBalanceHeader)
        LoadTrialBalanceOpenClose(_ocur, _TrialBalanceHeader)
        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub


    Private Sub aTrialBalancePrint_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTrialBalancePrint.Execute
        '-- Ez egy beégetés !!
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ReportData As ReportData
        Dim _TrialBalanceHeader As TrialBalanceHeader = TryCast(View.SelectedObjects(0), TrialBalanceHeader)

        If _TrialBalanceHeader IsNot Nothing Then
            _ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Name=?", "Főkönyvi kivonat"))

            If _ReportData IsNot Nothing Then
                Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("TrialBalanceHeader=?", _TrialBalanceHeader), True)
            Else
                Throw New UserFriendlyException("Nincs beállítva a <Főkönyvi kivonat> nyomtatvány !")
            End If
        End If
    End Sub
    Private Sub aViewTrialBalanceDetail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewTrialBalanceDetail.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        Dim _ListView As ListPropertyEditor = TryCast(View, DetailView).FindItem("TrialBalanceOpenClose")
        Dim _TrialBalanceHeader As TrialBalanceHeader = TryCast(View.SelectedObjects(0), TrialBalanceHeader)
        Dim _TrialBalanceOpenClose As TrialBalanceOpenClose = Nothing
        If _ListView.ListView.SelectedObjects.Count > 0 Then _TrialBalanceOpenClose = _ListView.ListView.SelectedObjects(0)

        If _TrialBalanceOpenClose IsNot Nothing Then
            Dim _CS As New CollectionSource(_onew, GetType(GLRows))
            _CS.BeginUpdateCriteria()
            _CS.Criteria.Clear()
            _CS.Criteria("First") = CriteriaOperator.Parse("GLHeader.DateExecution>=? and GetDate(GLHeader.DateExecution) <=? and GLHeader.CustomersFrom.Oid=? and GLHeader.IsEditable=False", _
                                                           _TrialBalanceHeader.DateFrom.Date, _TrialBalanceHeader.DateTo, _TrialBalanceHeader.CustomersFrom.Oid)
            _CS.Criteria("Second") = CriteriaOperator.Parse("DebitChartOfAccounts.AccountNumber like '" & _TrialBalanceOpenClose.AccountNumber & _
                                                            "%' or CreditChartOfAccounts.AccountNumber like '" & _TrialBalanceOpenClose.AccountNumber & "%'")
            _CS.EndUpdateCriteria()
            e.ShowViewParameters.CreatedView = Application.CreateListView("GLRows_ListView_TrialBalance", _CS, False)
        End If
    End Sub
    Private Sub aTrialBalanceViewCard_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTrialBalanceViewCard.Execute

    End Sub
    Private Sub aTrialBalancePrint1_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTrialBalancePrint1.Execute
        '-- Ez egy beégetés !!
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ReportData As ReportData
        Dim _TrialBalanceHeader As TrialBalanceHeader = TryCast(View.SelectedObjects(0), TrialBalanceHeader)

        If _TrialBalanceHeader IsNot Nothing Then
            _ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Name=?", "Főkönyvi kivonat 1"))

            If _ReportData IsNot Nothing Then
                Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("TrialBalanceHeader=?", _TrialBalanceHeader), True)
            Else
                Throw New UserFriendlyException("Nincs beállítva a <Főkönyvi kivonat 1> nyomtatvány !")
            End If
        End If
    End Sub


    '// Subs
    Public Sub LoadTrialBalanceNew(_ocur As Xpo.XPObjectSpace, _TrialBallanceHeader As TrialBalanceHeader)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _ChartOfAccounts As ChartOfAccounts
        Dim _TrialBalanceHeader As TrialBalanceHeader = _uow.GetObjectByKey(Of TrialBalanceHeader)(_TrialBallanceHeader.Oid)
        Dim _AmountHUF As Double = 0

        Dim _GLRows_Collection = Nothing
        Dim _GLRow As GLRows = Nothing

        Dim _TrialBalanceSum As TrialBalanceSum
        Dim _CP As String = ""
        Dim _RowIndex As Long = 0
        Dim _RowIndex_Sum As Long = 0

        If _TrialBalanceHeader Is Nothing Then Exit Sub
        Dim _TrialBallance_Collection As New XPCollection(_uow, GetType(TrialBalance), _
        CriteriaOperator.Parse("TrialBalanceHeader.Oid=?", _TrialBalanceHeader.Oid))
        _uow.Delete(_TrialBallance_Collection)

        Dim _TrialBalanceSum_Collection As New XPCollection(_uow, GetType(TrialBalanceSum), _
             CriteriaOperator.Parse("TrialBalanceHeader.Oid=?", _TrialBalanceHeader.Oid))
        _uow.Delete(_TrialBalanceSum_Collection)

        Dim _TrialBalance As TrialBalance

        '-- Először feltöltjük a főkönyvi számlatükröt -------------------------------------------------------------------------------------------------------------------------------------------------------------
        Dim _ChartOfAccounts_Collection As New XPCollection(_uow, GetType(ChartOfAccounts), CriteriaOperator.Parse("AccountYear=?", _TrialBalanceHeader.DateFrom.Year))
        _ChartOfAccounts_Collection.Sorting.Add(New SortProperty("AccountNumber", DB.SortingDirection.Ascending))
        RaiseEvent InitWork(1, 1, _ChartOfAccounts_Collection.Count)
        For Each _ChartOfAccounts In _ChartOfAccounts_Collection
            _RowIndex += 1
            RaiseEvent DoWork()
            _TrialBalance = New TrialBalance(_uow)
            _TrialBalance.RowIndex = _RowIndex
            _TrialBalance.TrialBalanceHeader = _TrialBalanceHeader
            _TrialBalance.CustomersForm = _TrialBalanceHeader.CustomersFrom
            _TrialBalance.AccountNumber = _ChartOfAccounts.AccountNumber
            _TrialBalance.ChartOfAccounts = _ChartOfAccounts
            _TrialBalance.CreditAmountHUF = 0
            _TrialBalance.CreditRAmountHUF = 0
            _TrialBalance.DebitAmountHUF = 0
            _TrialBalance.DebitRAmountHUF = 0
            _TrialBalance.ShortName = _ChartOfAccounts.Name
        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork
        '-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        Dim DebitQuery As System.Linq.IQueryable(Of Object)
        Dim _GLRows_XPQuery As New XPQuery(Of GLRows)(_uow)

        Dim _TrialBalanceJobNumberObjects_XPQuery As New XPQuery(Of TrialBalanceJobNumberObjects)(_uow)
        Dim cquery = From _TrialBalanceJobNumberObjects In _TrialBalanceJobNumberObjects_XPQuery
                     Where _TrialBalanceJobNumberObjects.TrialBalanceHeader.Oid = _TrialBalanceHeader.Oid
                     Select _TrialBalanceJobNumberObjects.JobNumberObjects.Oid

        If _TrialBalanceHeader.GLReportDateExecutionMode = eDateExecutionMode.eHeader Then

            If _TrialBalanceHeader.TrialBalanceJobNumberObjects.Count > 0 Then
                DebitQuery = From _DebitQuery In _GLRows_XPQuery
                             Where _DebitQuery.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid And _
                                   _DebitQuery.GLHeader.DateExecution.Date >= _TrialBalanceHeader.DateFrom.Date And _
                                   _DebitQuery.GLHeader.DateExecution.Date <= _TrialBalanceHeader.DateTo.Date And _
                                    cquery.Contains(_DebitQuery.JobNumberObjects.Oid) And
                                    _DebitQuery.GLHeader.IsEditable = False
                                    Group _DebitQuery By _DebitQuery.DebitChartOfAccounts.AccountNumber Into g = Group _
                             Select New With {Key .AccountNumber = AccountNumber,
                                              Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF)),
                                              Key .AmountDEV = g.Sum(Function(inv) (inv.AmountDEV))}
            Else
                DebitQuery = From _DebitQuery In _GLRows_XPQuery
                             Where _DebitQuery.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid And _
                                   _DebitQuery.GLHeader.DateExecution.Date >= _TrialBalanceHeader.DateFrom.Date And _
                                   _DebitQuery.GLHeader.DateExecution.Date <= _TrialBalanceHeader.DateTo.Date And _
                                   _DebitQuery.GLHeader.IsEditable = False
                                    Group _DebitQuery By _DebitQuery.DebitChartOfAccounts.AccountNumber Into g = Group _
                             Select New With {Key .AccountNumber = AccountNumber,
                                              Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF)),
                                              Key .AmountDEV = g.Sum(Function(inv) (inv.AmountDEV))}
            End If
        Else
            If _TrialBalanceHeader.TrialBalanceJobNumberObjects.Count > 0 Then
                DebitQuery = From _DebitQuery In _GLRows_XPQuery
                             Where _DebitQuery.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid And _
                                   _DebitQuery.DateExecution.Date >= _TrialBalanceHeader.DateFrom.Date And _
                                   _DebitQuery.DateExecution.Date <= _TrialBalanceHeader.DateTo.Date And _
                                   cquery.Contains(_DebitQuery.JobNumberObjects.Oid) And
                                   _DebitQuery.GLHeader.IsEditable = False
                                    Group _DebitQuery By _DebitQuery.DebitChartOfAccounts.AccountNumber Into g = Group _
                             Select New With {Key .AccountNumber = AccountNumber,
                                              Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF)),
                                              Key .AmountDEV = g.Sum(Function(inv) (inv.AmountDEV))}
            Else
                DebitQuery = From _DebitQuery In _GLRows_XPQuery
                             Where _DebitQuery.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid And _
                                   _DebitQuery.DateExecution.Date >= _TrialBalanceHeader.DateFrom.Date And _
                                   _DebitQuery.DateExecution.Date <= _TrialBalanceHeader.DateTo.Date And _
                                   _DebitQuery.GLHeader.IsEditable = False
                                    Group _DebitQuery By _DebitQuery.DebitChartOfAccounts.AccountNumber Into g = Group _
                             Select New With {Key .AccountNumber = AccountNumber,
                                              Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF)),
                                              Key .AmountDEV = g.Sum(Function(inv) (inv.AmountDEV))}
            End If
            

        End If

        RaiseEvent InitWork(1, 1, DebitQuery.Count)
        For Each item In DebitQuery
            RaiseEvent DoWork()
            For Each _TrialBalance In _TrialBalanceHeader.TrialBalance
                If item.AccountNumber IsNot Nothing Then
                    If _TrialBalance.AccountNumber = Mid(item.AccountNumber, 1, Len(_TrialBalance.AccountNumber)) Then
                        _TrialBalance.DebitAmountHUF += Math.Round(item.AmountHUF, 2, MidpointRounding.AwayFromZero)
                    End If
                End If
            Next
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork
        Dim CreditQuery As System.Linq.IQueryable(Of Object)

        If _TrialBalanceHeader.GLReportDateExecutionMode = eDateExecutionMode.eHeader Then
            If _TrialBalanceHeader.TrialBalanceJobNumberObjects.Count > 0 Then
                CreditQuery = From _CreditQuery In _GLRows_XPQuery
                             Where _CreditQuery.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid And _
                                   _CreditQuery.GLHeader.DateExecution.Date >= _TrialBalanceHeader.DateFrom.Date And _
                                   _CreditQuery.GLHeader.DateExecution.Date <= _TrialBalanceHeader.DateTo.Date And _
                                   cquery.Contains(_CreditQuery.JobNumberObjects.Oid) And
                                   _CreditQuery.GLHeader.IsEditable = False
                                    Group _CreditQuery By _CreditQuery.CreditChartOfAccounts.AccountNumber Into g = Group _
                             Select New With {Key .AccountNumber = AccountNumber,
                                              Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF)),
                                              Key .AmountDEV = g.Sum(Function(inv) (inv.AmountDEV))}
            Else
                CreditQuery = From _CreditQuery In _GLRows_XPQuery
                             Where _CreditQuery.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid And _
                                   _CreditQuery.GLHeader.DateExecution.Date >= _TrialBalanceHeader.DateFrom.Date And _
                                   _CreditQuery.GLHeader.DateExecution.Date <= _TrialBalanceHeader.DateTo.Date And _
                                   _CreditQuery.GLHeader.IsEditable = False
                                    Group _CreditQuery By _CreditQuery.CreditChartOfAccounts.AccountNumber Into g = Group _
                             Select New With {Key .AccountNumber = AccountNumber,
                                              Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF)),
                                              Key .AmountDEV = g.Sum(Function(inv) (inv.AmountDEV))}
            End If
            
        Else
            If _TrialBalanceHeader.TrialBalanceJobNumberObjects.Count > 0 Then
                CreditQuery = From _CreditQuery In _GLRows_XPQuery
                             Where _CreditQuery.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid And _
                                   _CreditQuery.DateExecution.Date >= _TrialBalanceHeader.DateFrom.Date And _
                                   _CreditQuery.DateExecution.Date <= _TrialBalanceHeader.DateTo.Date And _
                                   cquery.Contains(_CreditQuery.JobNumberObjects.Oid) And
                                   _CreditQuery.GLHeader.IsEditable = False
                                    Group _CreditQuery By _CreditQuery.CreditChartOfAccounts.AccountNumber Into g = Group _
                             Select New With {Key .AccountNumber = AccountNumber,
                                              Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF)),
                                              Key .AmountDEV = g.Sum(Function(inv) (inv.AmountDEV))}
            Else
                CreditQuery = From _CreditQuery In _GLRows_XPQuery
                            Where _CreditQuery.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid And _
                                _CreditQuery.DateExecution.Date >= _TrialBalanceHeader.DateFrom.Date And _
                                _CreditQuery.DateExecution.Date <= _TrialBalanceHeader.DateTo.Date And _
                                _CreditQuery.GLHeader.IsEditable = False
                                Group _CreditQuery By _CreditQuery.CreditChartOfAccounts.AccountNumber Into g = Group _
                            Select New With {Key .AccountNumber = AccountNumber,
                                            Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF)),
                                            Key .AmountDEV = g.Sum(Function(inv) (inv.AmountDEV))}
            End If
            

        End If

        RaiseEvent InitWork(1, 1, CreditQuery.Count)
        For Each item In CreditQuery
            RaiseEvent DoWork()
            For Each _TrialBalance In _TrialBalanceHeader.TrialBalance
                If item.AccountNumber IsNot Nothing Then
                    If _TrialBalance.AccountNumber = Mid(item.AccountNumber, 1, Len(_TrialBalance.AccountNumber)) Then
                        _TrialBalance.CreditAmountHUF += Math.Round(item.AmountHUF, 2, MidpointRounding.AwayFromZero)
                    End If
                End If
            Next
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork


        '-- Összesítés -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
        RaiseEvent InitWork(1, 1, _TrialBalanceHeader.TrialBalance.Count)
        For Each _TrialBalance In _TrialBalanceHeader.TrialBalance
            RaiseEvent DoWork()
            If _TrialBalance.DebitAmountHUF > _TrialBalance.CreditAmountHUF Then
                _TrialBalance.DebitRAmountHUF = _TrialBalance.DebitAmountHUF - _TrialBalance.CreditAmountHUF
            ElseIf _TrialBalance.DebitAmountHUF < _TrialBalance.CreditAmountHUF Then
                _TrialBalance.CreditRAmountHUF = _TrialBalance.CreditAmountHUF - _TrialBalance.DebitAmountHUF
            End If

            Select Case _TrialBalance.AccountNumber
                Case "0", "1", "2", "3", "4", "5", "8", "9"
                    _RowIndex_Sum += 1
                    _TrialBalanceSum = New TrialBalanceSum(_uow)
                    _TrialBalanceSum.RowIndex = _RowIndex_Sum
                    Select Case _TrialBalance.AccountNumber
                        Case "0", "1", "2", "3", "4"
                            _TrialBalanceSum.AccountSum = eAccountSum.e1_4
                        Case "5", "8", "9"
                            _TrialBalanceSum.AccountSum = eAccountSum.e5_9
                    End Select
                    _TrialBalanceSum.AccountNumber = _TrialBalance.AccountNumber
                    _TrialBalanceSum.CreditAmountHUF = _TrialBalance.CreditAmountHUF
                    _TrialBalanceSum.CustomersForm = _TrialBalance.CustomersForm
                    _TrialBalanceSum.DebitAmountHUF = _TrialBalance.DebitAmountHUF
                    _TrialBalanceSum.ShortName = _TrialBalance.ShortName
                    _TrialBalanceSum.TrialBalanceHeader = _TrialBalanceHeader
                    _TrialBalanceSum.BallanceHUFClose = _TrialBalanceSum.DebitAmountHUF - _TrialBalanceSum.CreditAmountHUF
            End Select
        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork
        '-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        Dim _TRB_C As New XPCollection(Of TrialBalance)(_uow, CriteriaOperator.Parse("CreditAmountHUF = 0 And DebitAmountHUF = 0 And CreditRAmountHUF = 0 And CreditRAmountHUF = 0"))
        _uow.Delete(_TRB_C)
        _uow.CommitChanges()

        _TrialBalanceHeader.CreateGLRowsStatictics(_uow, _TrialBalanceHeader.DateFrom, _TrialBalanceHeader.DateTo, _TrialBalanceHeader.CustomersFrom, _TrialBalanceHeader.Oid)

    End Sub
    Public Sub LoadTrialBalanceOpenClose(_ocur As Xpo.XPObjectSpace, _TrialBallanceHeader As TrialBalanceHeader)

        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _DebitAmountHUF As Double = 0
        Dim _CreditAmountHUF As Double = 0
        Dim _DebitAmountHUF491 As Double = 0
        Dim _CreditAmountHUF491 As Double = 0
        Dim _DateFromCurrentYear As Date

        Dim _DateFrom As Date


        Dim _ChartOfAccounts As ChartOfAccounts
        Dim _TrialBalanceHeader As TrialBalanceHeader = _uow.GetObjectByKey(Of TrialBalanceHeader)(_TrialBallanceHeader.Oid)

        Dim _TrialBalanceOpenClose_Collection As New XPCollection(_uow, GetType(TrialBalanceOpenClose), _
             CriteriaOperator.Parse("TrialBalanceHeader.Oid=?", _TrialBalanceHeader.Oid))
        _uow.Delete(_TrialBalanceOpenClose_Collection)

        Dim _TrialBalanceSum_Collection As New XPCollection(_uow, GetType(TrialBalanceSum), _
             CriteriaOperator.Parse("TrialBalanceHeader.Oid=?", _TrialBalanceHeader.Oid))
        _uow.Delete(_TrialBalanceSum_Collection)

        Dim _TrialBalanceOpenClose As TrialBalanceOpenClose
        Dim _TrialBalanceSum As TrialBalanceSum

        Dim _RowIndex As Long = 0
        Dim _RowIndex_Sum As Long = 0

        _DateFromCurrentYear = CDate(Year(_TrialBalanceHeader.DateFrom) & "/01/01")
        _DateFrom = _TrialBalanceHeader.DateFrom

        '-- Először feltöltjük a főkönyvi számlatükröt -------------------------------------------------------------------------------------------------------------------------------------------------------------
        Dim _ChartOfAccounts_Collection As New XPCollection(_uow, GetType(ChartOfAccounts), CriteriaOperator.Parse("AccountYear=?", _TrialBalanceHeader.DateFrom.Year))
        _ChartOfAccounts_Collection.Sorting.Add(New SortProperty("AccountNumber", DB.SortingDirection.Ascending))
        RaiseEvent InitWork(1, 1, _ChartOfAccounts_Collection.Count)
        For Each _ChartOfAccounts In _ChartOfAccounts_Collection
            _RowIndex += 1
            RaiseEvent DoWork()
            _TrialBalanceOpenClose = New TrialBalanceOpenClose(_uow)
            _TrialBalanceOpenClose.RowIndex = _RowIndex
            _TrialBalanceOpenClose.TrialBalanceHeader = _TrialBalanceHeader
            _TrialBalanceOpenClose.CustomersForm = _TrialBalanceHeader.CustomersFrom
            _TrialBalanceOpenClose.AccountNumber = _ChartOfAccounts.AccountNumber
            _TrialBalanceOpenClose.CreditAmountHUF = 0
            _TrialBalanceOpenClose.DebitAmountHUF = 0
            _TrialBalanceOpenClose.ShortName = _ChartOfAccounts.Name

            _TrialBalanceOpenClose.BallanceHUF = 0
            _TrialBalanceOpenClose.BallanceHUF491 = 0
            _TrialBalanceOpenClose.BallanceHUF491Open = 0
            _TrialBalanceOpenClose.BallanceHUFClose = 0
            _TrialBalanceOpenClose.BallanceHUFOpen = 0


        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork
        '-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        Dim _GLRows_xpquery = New XPQuery(Of GLRows)(_uow)
        Dim query As System.Linq.IQueryable(Of Object)

        'I. ------------------------------------------------------------------------------------------------------------------------------------
        If _TrialBalanceHeader.GLReportDateExecutionMode = eDateExecutionMode.eHeader Then
            query = From _glrows In _GLRows_xpquery
                        Where _glrows.CreditChartOfAccounts.AccountNumber = "491" And _
                              _glrows.GLHeader.IsEditable = False And _
                              _glrows.GLHeader.DateExecution.Year = _DateFromCurrentYear.Year And _
                              _glrows.GLHeader.DateExecution.Month = 1 And
                              _glrows.GLHeader.DateExecution.Day = 1 And
                              _glrows.GLHeader IsNot Nothing And _
                              _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
            Group _glrows By _glrows.DebitChartOfAccounts.AccountNumber Into g = Group _
            Select New With {Key .AccountNumber = AccountNumber, _
                             Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}
        Else
            query = From _glrows In _GLRows_xpquery
                    Where _glrows.CreditChartOfAccounts.AccountNumber = "491" And _
                          _glrows.GLHeader.IsEditable = False And _
                          _glrows.DateExecution.Year = _DateFromCurrentYear.Year And _
                          _glrows.GLHeader.DateExecution.Month = 1 And
                          _glrows.GLHeader.DateExecution.Day = 1 And
                          _glrows.GLHeader IsNot Nothing And _
                          _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
            Group _glrows By _glrows.DebitChartOfAccounts.AccountNumber Into g = Group _
            Select New With {Key .AccountNumber = AccountNumber, _
                         Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}

        End If

        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()
            For Each _TrialBalanceOpenClose In _TrialBalanceHeader.TrialBalanceOpenClose
                If item.AccountNumber IsNot Nothing Then
                    If _TrialBalanceOpenClose.AccountNumber = Mid(item.AccountNumber, 1, Len(_TrialBalanceOpenClose.AccountNumber)) Then
                        _TrialBalanceOpenClose.BallanceHUF491 += Math.Round(item.AmountHUF, 2, MidpointRounding.AwayFromZero)
                    End If
                End If
            Next
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork

        If _TrialBalanceHeader.GLReportDateExecutionMode = eDateExecutionMode.eHeader Then
            query = From _glrows In _GLRows_xpquery
                        Where _glrows.DebitChartOfAccounts.AccountNumber = "491" And _
                              _glrows.GLHeader.IsEditable = False And _
                              _glrows.GLHeader.DateExecution.Year = _DateFromCurrentYear.Year And _
                              _glrows.GLHeader IsNot Nothing And _
                              _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
            Group _glrows By _glrows.CreditChartOfAccounts.AccountNumber Into g = Group _
            Select New With {Key .AccountNumber = AccountNumber, _
                             Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}
        Else
            query = From _glrows In _GLRows_xpquery
                    Where _glrows.DebitChartOfAccounts.AccountNumber = "491" And _
                          _glrows.GLHeader.IsEditable = False And _
                          _glrows.DateExecution.Year = _DateFromCurrentYear.Year And _
                          _glrows.GLHeader IsNot Nothing And _
                          _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
            Group _glrows By _glrows.CreditChartOfAccounts.AccountNumber Into g = Group _
            Select New With {Key .AccountNumber = AccountNumber, _
                         Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}

        End If

        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()
            For Each _TrialBalanceOpenClose In _TrialBalanceHeader.TrialBalanceOpenClose
                If item.AccountNumber IsNot Nothing Then
                    If _TrialBalanceOpenClose.AccountNumber = Mid(item.AccountNumber, 1, Len(_TrialBalanceOpenClose.AccountNumber)) Then
                        _TrialBalanceOpenClose.BallanceHUF491 -= Math.Round(item.AmountHUF, 2, MidpointRounding.AwayFromZero)
                    End If
                End If
            Next
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork

        'II. ------------------------------------------------------------------------------------------------------------------------------------
        If _TrialBalanceHeader.GLReportDateExecutionMode = eDateExecutionMode.eHeader Then
            query = From _glrows In _GLRows_xpquery
                        Where _glrows.DebitChartOfAccounts.AccountNumber <> "491" And _
                              _glrows.CreditChartOfAccounts.AccountNumber <> "491" And _
                              _glrows.GLHeader.IsEditable = False And _
                              _glrows.GLHeader.DateExecution.Date >= _DateFromCurrentYear.Date And
                              _glrows.GLHeader.DateExecution.Date < _TrialBalanceHeader.DateFrom.Date And
                              _glrows.GLHeader IsNot Nothing And _
                              _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
            Group _glrows By _glrows.DebitChartOfAccounts.AccountNumber Into g = Group _
            Select New With {Key .AccountNumber = AccountNumber, _
                             Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}
        Else
            query = From _glrows In _GLRows_xpquery
                    Where _glrows.DebitChartOfAccounts.AccountNumber <> "491" And _
                          _glrows.CreditChartOfAccounts.AccountNumber <> "491" And _
                          _glrows.GLHeader.IsEditable = False And _
                          _glrows.DateExecution.Date >= _DateFromCurrentYear.Date And
                          _glrows.DateExecution.Date < _TrialBalanceHeader.DateFrom.Date And
                          _glrows.GLHeader IsNot Nothing And _
                          _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
        Group _glrows By _glrows.DebitChartOfAccounts.AccountNumber Into g = Group _
        Select New With {Key .AccountNumber = AccountNumber, _
                         Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}

        End If
        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()
            For Each _TrialBalanceOpenClose In _TrialBalanceHeader.TrialBalanceOpenClose
                If item.AccountNumber IsNot Nothing Then
                    If _TrialBalanceOpenClose.AccountNumber = Mid(item.AccountNumber, 1, Len(_TrialBalanceOpenClose.AccountNumber)) Then
                        _TrialBalanceOpenClose.BallanceHUFOpen += Math.Round(item.AmountHUF, 2, MidpointRounding.AwayFromZero)
                    End If
                End If
            Next
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork

        If _TrialBalanceHeader.GLReportDateExecutionMode = eDateExecutionMode.eHeader Then
            query = From _glrows In _GLRows_xpquery
                        Where _glrows.DebitChartOfAccounts.AccountNumber <> "491" And _
                              _glrows.CreditChartOfAccounts.AccountNumber <> "491" And _
                              _glrows.GLHeader.IsEditable = False And _
                              _glrows.GLHeader.DateExecution.Date >= _DateFromCurrentYear And
                              _glrows.GLHeader.DateExecution.Date < _TrialBalanceHeader.DateFrom.Date And
                              _glrows.GLHeader IsNot Nothing And _
                              _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
            Group _glrows By _glrows.CreditChartOfAccounts.AccountNumber Into g = Group _
            Select New With {Key .AccountNumber = AccountNumber, _
                             Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}
        Else
            query = From _glrows In _GLRows_xpquery
                    Where _glrows.DebitChartOfAccounts.AccountNumber <> "491" And _
                          _glrows.CreditChartOfAccounts.AccountNumber <> "491" And _
                          _glrows.GLHeader.IsEditable = False And _
                          _glrows.DateExecution.Date >= _DateFromCurrentYear And
                          _glrows.DateExecution.Date < _TrialBalanceHeader.DateFrom.Date And
                          _glrows.GLHeader IsNot Nothing And _
                          _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
        Group _glrows By _glrows.CreditChartOfAccounts.AccountNumber Into g = Group _
        Select New With {Key .AccountNumber = AccountNumber, _
                         Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}
        End If
        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()
            For Each _TrialBalanceOpenClose In _TrialBalanceHeader.TrialBalanceOpenClose
                If item.AccountNumber IsNot Nothing Then
                    If _TrialBalanceOpenClose.AccountNumber = Mid(item.AccountNumber, 1, Len(_TrialBalanceOpenClose.AccountNumber)) Then
                        _TrialBalanceOpenClose.BallanceHUFOpen -= Math.Round(item.AmountHUF, 2, MidpointRounding.AwayFromZero)
                    End If
                End If
            Next
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork
        For Each _TrialBalanceOpenClose In _TrialBalanceHeader.TrialBalanceOpenClose
            _TrialBalanceOpenClose.BallanceHUF491Open = _TrialBalanceOpenClose.BallanceHUF491 + _TrialBalanceOpenClose.BallanceHUFOpen
        Next

        'III. ------------------------------------------------------------------------------------------------------------------------------------
        If _TrialBalanceHeader.GLReportDateExecutionMode = eDateExecutionMode.eHeader Then
            query = From _glrows In _GLRows_xpquery
                        Where _glrows.DebitChartOfAccounts.AccountNumber <> "491" And _
                              _glrows.CreditChartOfAccounts.AccountNumber <> "491" And _
                              _glrows.GLHeader.IsEditable = False And _
                              _glrows.GLHeader.DateExecution.Date >= _DateFrom.Date And
                              _glrows.GLHeader.DateExecution.Date <= DateAdd(DateInterval.Minute, 1439, _TrialBalanceHeader.DateTo.Date) And
                              _glrows.GLHeader IsNot Nothing And _
                              _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
            Group _glrows By _glrows.DebitChartOfAccounts.AccountNumber Into g = Group _
            Select New With {Key .AccountNumber = AccountNumber, _
                             Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}
        Else
            query = From _glrows In _GLRows_xpquery
                    Where _glrows.DebitChartOfAccounts.AccountNumber <> "491" And _
                          _glrows.CreditChartOfAccounts.AccountNumber <> "491" And _
                          _glrows.GLHeader.IsEditable = False And _
                          _glrows.DateExecution.Date >= _DateFrom.Date And
                          _glrows.DateExecution.Date <= DateAdd(DateInterval.Minute, 1439, _TrialBalanceHeader.DateTo.Date) And
                          _glrows.GLHeader IsNot Nothing And _
                          _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
        Group _glrows By _glrows.DebitChartOfAccounts.AccountNumber Into g = Group _
        Select New With {Key .AccountNumber = AccountNumber, _
                         Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}
        End If
        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()
            For Each _TrialBalanceOpenClose In _TrialBalanceHeader.TrialBalanceOpenClose
                If item.AccountNumber IsNot Nothing Then
                    If _TrialBalanceOpenClose.AccountNumber = Mid(item.AccountNumber, 1, Len(_TrialBalanceOpenClose.AccountNumber)) Then
                        _TrialBalanceOpenClose.DebitAmountHUF += Math.Round(item.AmountHUF, 2, MidpointRounding.AwayFromZero)
                    End If
                End If
            Next
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork

        If _TrialBalanceHeader.GLReportDateExecutionMode = eDateExecutionMode.eHeader Then
            query = From _glrows In _GLRows_xpquery
                        Where _glrows.DebitChartOfAccounts.AccountNumber <> "491" And _
                              _glrows.CreditChartOfAccounts.AccountNumber <> "491" And _
                              _glrows.GLHeader.IsEditable = False And _
                              _glrows.GLHeader.DateExecution.Date >= _DateFrom.Date And
                              _glrows.GLHeader.DateExecution.Date <= DateAdd(DateInterval.Minute, 1439, _TrialBalanceHeader.DateTo.Date) And
                              _glrows.GLHeader IsNot Nothing And _
                              _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
            Group _glrows By _glrows.CreditChartOfAccounts.AccountNumber Into g = Group _
            Select New With {Key .AccountNumber = AccountNumber, _
                             Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}
        Else
            query = From _glrows In _GLRows_xpquery
                    Where _glrows.DebitChartOfAccounts.AccountNumber <> "491" And _
                          _glrows.CreditChartOfAccounts.AccountNumber <> "491" And _
                          _glrows.GLHeader.IsEditable = False And _
                          _glrows.DateExecution.Date >= _DateFrom.Date And
                          _glrows.DateExecution.Date <= DateAdd(DateInterval.Minute, 1439, _TrialBalanceHeader.DateTo.Date) And
                          _glrows.GLHeader IsNot Nothing And _
                          _glrows.GLHeader.CustomersFrom.Oid = _TrialBalanceHeader.CustomersFrom.Oid _
        Group _glrows By _glrows.CreditChartOfAccounts.AccountNumber Into g = Group _
        Select New With {Key .AccountNumber = AccountNumber, _
                         Key .AmountHUF = g.Sum(Function(inv) (inv.AmountHUF))}
        End If
        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()
            For Each _TrialBalanceOpenClose In _TrialBalanceHeader.TrialBalanceOpenClose
                If item.AccountNumber IsNot Nothing Then
                    If _TrialBalanceOpenClose.AccountNumber = Mid(item.AccountNumber, 1, Len(_TrialBalanceOpenClose.AccountNumber)) Then
                        _TrialBalanceOpenClose.CreditAmountHUF += Math.Round(item.AmountHUF, 2, MidpointRounding.AwayFromZero)
                    End If
                End If
            Next
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork

        For Each _TrialBalanceOpenClose In _TrialBalanceHeader.TrialBalanceOpenClose
            _TrialBalanceOpenClose.BallanceHUF = _TrialBalanceOpenClose.DebitAmountHUF - _TrialBalanceOpenClose.CreditAmountHUF
            _TrialBalanceOpenClose.BallanceHUFClose = _TrialBalanceOpenClose.BallanceHUF491Open + _TrialBalanceOpenClose.BallanceHUF
        Next


        'IV. ------------------------------------------------------------------------------------------------------------------------------------
        RaiseEvent InitWork(1, 1, _TrialBalanceHeader.TrialBalanceOpenClose.Count)
        For Each _TrialBalanceOpenClose In _TrialBalanceHeader.TrialBalanceOpenClose
            RaiseEvent DoWork()
            Select Case _TrialBalanceOpenClose.AccountNumber
                Case "0", "1", "2", "3", "4", "5", "8", "9"
                    _RowIndex_Sum += 1
                    _TrialBalanceSum = New TrialBalanceSum(_uow)
                    _TrialBalanceSum.RowIndex = _RowIndex_Sum
                    Select Case _TrialBalanceOpenClose.AccountNumber
                        Case "0", "1", "2", "3", "4"
                            _TrialBalanceSum.AccountSum = eAccountSum.e1_4
                        Case "5", "8", "9"
                            _TrialBalanceSum.AccountSum = eAccountSum.e5_9
                    End Select
                    _TrialBalanceSum.AccountNumber = _TrialBalanceOpenClose.AccountNumber
                    _TrialBalanceSum.BallanceHUF = _TrialBalanceOpenClose.BallanceHUF
                    _TrialBalanceSum.BallanceHUFClose = _TrialBalanceOpenClose.BallanceHUFClose
                    _TrialBalanceSum.BallanceHUFOpen = _TrialBalanceOpenClose.BallanceHUF491Open
                    _TrialBalanceSum.CreditAmountHUF = _TrialBalanceOpenClose.CreditAmountHUF
                    _TrialBalanceSum.CustomersForm = _TrialBalanceOpenClose.CustomersForm
                    _TrialBalanceSum.DebitAmountHUF = _TrialBalanceOpenClose.DebitAmountHUF
                    _TrialBalanceSum.ShortName = _TrialBalanceOpenClose.ShortName
                    _TrialBalanceSum.TrialBalanceHeader = _TrialBalanceHeader
            End Select
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork


        Dim _TRBO As New XPCollection(Of TrialBalanceOpenClose)(_uow, CriteriaOperator.Parse("BallanceHUF = 0 And BallanceHUF491 = 0 And BallanceHUF491Open = 0 And BallanceHUFClose = 0 And BallanceHUFOpen = 0 And CreditAmountHUF = 0 And DebitAmountHUF=0"))
        _uow.Delete(_TRBO)
        _uow.CommitChanges()

        _TrialBalanceHeader.CreateGLRowsStatictics(_uow, _TrialBalanceHeader.DateFrom, _TrialBalanceHeader.DateTo, _TrialBalanceHeader.CustomersFrom, _TrialBalanceHeader.Oid)


    End Sub

End Class

