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

Public Class VATReportVC
    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()

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _User As User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))


        Frame.GetController(Of VATReportVC).aVATClosePreviewPDF.Active.SetItemValue("", False)

        If View.Id Like "VATReport_VATReportDetail_ListView" Or _
            View.Id = "VATReport_VATReportGroup_ListView" Or _
            View.Id = "VATReport_VATReportNoPayDetail_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If

        If View.Id = "VATReport_DetailView" Then
            Dim _VATReport As VATReport = TryCast(View.SelectedObjects(0), VATReport)

            If _VATReport.Closed = True Then
                Frame.GetController(Of VATReportVC).aGenerateVATReport.Active.SetItemValue("", False)
            Else
                Frame.GetController(Of VATReportVC).aGenerateVATReport.Active.SetItemValue("", True)
            End If

            If _VATReport.UserCreated IsNot Nothing Then
                If Not _VATReport.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 VATReportVC).aGenerateVATReport.Active.SetItemValue("", False)
                End If
            End If
        End If

        If View.Id = "VATReport_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteria = "UserCreated.Oid='" & _User.Oid.ToString & "'"
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteriaMode = TargetObjectsCriteriaMode.TrueForAll
        End If

        If View.Id = "VATReport_ListView_Closed" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Frame.GetController(Of VATReportVC).aVATClosePreviewPDF.Active.SetItemValue("", True)
        End If
        If View.Id = "VATReport_VATReportGroupCustomers2M_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Frame.GetController(Of VATReportVC).aVATClosePreviewPDF.Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "VATReport_DetailView" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _VATReport As VATReport = TryCast(View.SelectedObjects(0), VATReport)
            Dim _User As User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))

            If _VATReport.UserCreated IsNot Nothing Then
                If Not _VATReport.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 VATReportVC).aGenerateVATReport.Active.SetItemValue("", True)
                End If
            End If
        End If
        If View.Id = "VATReport_VATReportGroupCustomers2M_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aGenerateVATReport_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGenerateVATReport.Execute
        '-- Az ÁFA kimutatást elkészíti

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)

        Dim _VATReportDetail As VATReportDetail
        Dim _VATReportNoPayDetail As VATReportNoPayDetail
        Dim _VATReportGroup As VATReportGroup
        Dim _VATReport As VATReport = _uow.GetObjectByKey(Of VATReport)(TryCast(View.SelectedObjects(0), VATReport).Oid)
        Dim _InvoiceVATRows As InvoiceVATRows
        Dim _RegistryRowsFinancial As RegistryRowsFinancial
        Dim _GLRows As GLRows
        Dim i As Long = 0
        Dim _VRD As VATReportDetail
        Dim _Array As New ArrayList
        Dim _PCIDetail_Collection As XPCollection(Of PCIDetail)
        Dim _PCIDetail As PCIDetail = Nothing
        Dim _BallanceHUF As Double = 0
        Dim _GLAccounts As GLAccounts = Nothing
        Dim _SQL As String = ""

        If _VATReport Is Nothing Then Exit Sub

        RaiseEvent InitWork(1, 1, 1)

        Dim _VatReportDetail_Collection As New XPCollection(Of VATReportDetail)(_uow, CriteriaOperator.Parse("VATReport=?", _VATReport))
        _uow.Delete(_VatReportDetail_Collection)
        Dim _VatReportNoPayDetail_Collection As New XPCollection(Of VATReportNoPayDetail)(_uow, CriteriaOperator.Parse("VATReport=?", _VATReport))
        _uow.Delete(_VatReportNoPayDetail_Collection)
        Dim _VatReportGroup_Collection As New XPCollection(Of VATReportGroup)(_uow, CriteriaOperator.Parse("VATReport=?", _VATReport))
        _uow.Delete(_VatReportGroup_Collection)
        Dim _VATReportGroupCustomers2M_Collection As New XPCollection(Of VATReportGroupCustomers2M)(_uow, CriteriaOperator.Parse("VATReport=?", _VATReport))
        _uow.Delete(_VATReportGroupCustomers2M_Collection)


        Select Case GLOBAL_SQLType
            Case eSQLType.MySQL
                _SQL = " UPDATE InvoiceVATRows t1 JOIN Invoiceheader t2 ON"
                _SQL += " t1.InvoiceHeader = t2.Oid"
                _SQL += " SET t1.DateVAT = t2.DateExecution"
                _SQL += " WHERE t1.DateVAT IS NULL AND t2.GCRecord IS NULL"
            Case eSQLType.MSSQL
                _SQL = " UPDATE InvoiceVATRows t1 JOIN Invoiceheader t2 ON t1.InvoiceHeader = t2.Oid AND t1.DateVAT IS NULL AND t2.GCRecord IS NULL"
                _SQL += " SET t1.DateVAT=t2.DateExecution"
            Case eSQLType.PostgreSQL
                _SQL = "UPDATE ""InvoiceVATRows"" Set ""DateVAT""=t2.""DateExecution"""
                _SQL += " from ""InvoiceVATRows"" t1 JOIN ""InvoiceHeader"" t2 On"
                _SQL += " t1.""InvoiceHeader""=t2.""Oid"""
                _SQL += " WHERE t1.""DateVAT"" Is NULL And t2.""GCRecord"" Is NULL"
        End Select

        _uow.ExecuteNonQuery(_SQL)
        _uow.CommitChanges()

        'ÁFA P.Ü. szemlélet ellenőrzése -----------------------------------------
        Me.CheckVATAvowalType(_ocur, _VATReport.DateFrom, _VATReport.DateTo)
        '-----------------------------------------------------------------------

        '-- Először a vevői számlák InvoiceVATRows-ból
        Dim _InvoiceVATRows_Collection As New XPCollection(Of InvoiceVATRows)(_uow, _
                CriteriaOperator.Parse("GetDate(DateVAT)>=? And GetDate(DateVAT)<=?" & _
                                       " And InvoiceHeader.CustomersFrom=? And InvoiceHeader.IsEditable=False" & _
                                       " And InvoiceHeader.IsProforma=False And InvoiceHeader.InvoiceType.InvoiceTypeBase In (0,1,2,3)", _
                                       _VATReport.DateFrom.Date, _VATReport.DateTo.Date, _VATReport.CustomersFrom))
        RaiseEvent InitWork(1, 1, _InvoiceVATRows_Collection.Count)
        For Each _InvoiceVATRows In _InvoiceVATRows_Collection
            RaiseEvent DoWork()
            If _InvoiceVATRows.InvoiceHeader.VATAvowalType = 1 Then
                _PCIDetail_Collection = New XPCollection(Of PCIDetail)(_uow,
                                            CriteriaOperator.Parse("GetDate(DateExecution)<=? And " +
                                                                   " PartnerType=? And " +
                                                                   " ConnectInfo=? And " +
                                                                   " Customers=? And " +
                                                                   " CustomersFrom=?", _VATReport.DateTo.Date, ePartnerType.eReceivables,
                                                                   _InvoiceVATRows.InvoiceHeader.ConnectInfo, _InvoiceVATRows.InvoiceHeader.Customers,
                                                                   _InvoiceVATRows.InvoiceHeader.CustomersFrom))
                _BallanceHUF = 0
                For Each _PCIDetail In _PCIDetail_Collection
                    _BallanceHUF = _PCIDetail.PCIGroup.AmountHUF_Account
                    Exit For
                Next
                For Each _PCIDetail In _PCIDetail_Collection
                    If _PCIDetail.MainType = "02-Bank" Or _PCIDetail.MainType = "03-Cassa" Or _PCIDetail.MainType = "04-Compensation" Or _PCIDetail.MainType = "05-Ratediff" Then
                        _VATReportDetail = New VATReportDetail(_uow)
                        _VATReportDetail.VATReportDetailRowType = eVATReportDetailRowType.efromInvoice
                        _VATReportDetail.VATReport = _VATReport
                        _VATReportDetail.AmountVATOut = Math.Round(_InvoiceVATRows.AmountVATHUF * (Math.Abs(_PCIDetail.BallanceHUF) / Math.Abs(_BallanceHUF)), 0, MidpointRounding.AwayFromZero)
                        _VATReportDetail.AmountVATOutBase = Math.Round(_InvoiceVATRows.AmountNettoHUF * (Math.Abs(_PCIDetail.BallanceHUF) / Math.Abs(_BallanceHUF)), 0, MidpointRounding.AwayFromZero)
                        _VATReportDetail.AmountVATIn = 0
                        _VATReportDetail.AmountVATInBase = 0
                        _VATReportDetail.VAT = _InvoiceVATRows.VAT
                        _VATReportDetail.DocumentNumber = _InvoiceVATRows.InvoiceHeader.DocumentNumber
                        _VATReportDetail.Customers = _InvoiceVATRows.InvoiceHeader.Customers
                        _VATReportDetail.DateExecution = _InvoiceVATRows.InvoiceHeader.DateExecution
                        _VATReportDetail.PartnerType = ePartnerType.eReceivables
                        _VATReportDetail.DateVAT = _PCIDetail.DateExecution
                        _VATReportDetail.PayMode = _InvoiceVATRows.InvoiceHeader.PaymentOption.PayMode
                        _VATReportDetail.Note = _InvoiceVATRows.InvoiceHeader.Description
                    End If
                Next

            Else
                _VATReportDetail = New VATReportDetail(_uow)
                _VATReportDetail.VATReportDetailRowType = eVATReportDetailRowType.efromInvoice
                _VATReportDetail.VATReport = _VATReport
                _VATReportDetail.AmountVATOut = _InvoiceVATRows.AmountVATHUF
                _VATReportDetail.AmountVATOutBase = _InvoiceVATRows.AmountNettoHUF
                _VATReportDetail.AmountVATIn = 0
                _VATReportDetail.AmountVATInBase = 0
                _VATReportDetail.VAT = _InvoiceVATRows.VAT
                _VATReportDetail.DocumentNumber = _InvoiceVATRows.InvoiceHeader.DocumentNumber
                _VATReportDetail.Customers = _InvoiceVATRows.InvoiceHeader.Customers
                _VATReportDetail.DateExecution = _InvoiceVATRows.InvoiceHeader.DateExecution
                _VATReportDetail.PartnerType = ePartnerType.eReceivables
                _VATReportDetail.DateVAT = _InvoiceVATRows.InvoiceHeader.DateExecution
                _VATReportDetail.PayMode = _InvoiceVATRows.InvoiceHeader.PaymentOption.PayMode
                _VATReportDetail.Note = _InvoiceVATRows.InvoiceHeader.Description
            End If
        Next
        'RaiseEvent FinalWork
        '-- Másodszor az iktatott szállítói számlák
        Dim _RegistryRowsFinancial_Collection As New XPCollection(Of RegistryRowsFinancial)(_uow, _
                CriteriaOperator.Parse("DateVAT>=? And DateVAT<=?" & _
                                       " And RegistryHeader.CustomersFrom=? And RegistryHeader.IsEditable=False And RegistryHeader.IsStorno=False", _
                                       _VATReport.DateFrom, DateAdd(DateInterval.Minute, 1439, _VATReport.DateTo.Date), _VATReport.CustomersFrom))
        RaiseEvent InitWork(1, 1, _RegistryRowsFinancial_Collection.Count)
        For Each _RegistryRowsFinancial In _RegistryRowsFinancial_Collection
            RaiseEvent DoWork()

            If _RegistryRowsFinancial.RegistryHeader.F_VATAvowalType = eVATAvowalType.eFinancial Then
                _PCIDetail_Collection = New XPCollection(Of PCIDetail)(_uow,
                                            CriteriaOperator.Parse("GetDate(DateExecution)<=? And " +
                                                                   " PartnerType=? And " +
                                                                   " ConnectInfo=? And " +
                                                                   " Customers=? And " +
                                                                   " CustomersFrom=?", _VATReport.DateTo.Date, ePartnerType.ePayabels,
                                                                   _RegistryRowsFinancial.RegistryHeader.F_ConnectInfo, _RegistryRowsFinancial.RegistryHeader.Customers,
                                                                   _RegistryRowsFinancial.RegistryHeader.CustomersFrom))
                _BallanceHUF = 0
                For Each _PCIDetail In _PCIDetail_Collection
                    _BallanceHUF = _PCIDetail.PCIGroup.AmountHUF_Account
                    Exit For
                Next

                For Each _PCIDetail In _PCIDetail_Collection
                    If _PCIDetail.MainType = "02-Bank" Or _PCIDetail.MainType = "03-Cassa" Or _PCIDetail.MainType = "04-Compensation" Or _PCIDetail.MainType = "05-Ratediff" Then
                        _VATReportDetail = New VATReportDetail(_uow)
                        _VATReportDetail.VATReportDetailRowType = eVATReportDetailRowType.efromRegistry
                        _VATReportDetail.VATReport = _VATReport
                        _VATReportDetail.AmountVATOut = 0
                        _VATReportDetail.AmountVATOutBase = 0


                        _VATReportDetail.AmountVATIn = Math.Round(_RegistryRowsFinancial.BruttoAmount / (100 + _RegistryRowsFinancial.VAT.KeyValuePercent) * _RegistryRowsFinancial.VAT.KeyValuePercent, 0, MidpointRounding.AwayFromZero)
                        _VATReportDetail.AmountVATInBase = _RegistryRowsFinancial.BruttoAmount - _VATReportDetail.AmountVATIn
                        _VATReportDetail.AmountVATIn = Math.Round(_VATReportDetail.AmountVATIn * (Math.Abs(_PCIDetail.BallanceHUF) / Math.Abs(_BallanceHUF)), 0, MidpointRounding.AwayFromZero)
                        _VATReportDetail.AmountVATInBase = Math.Round(_VATReportDetail.AmountVATInBase * (Math.Abs(_PCIDetail.BallanceHUF) / Math.Abs(_BallanceHUF)), 0, MidpointRounding.AwayFromZero)


                        _VATReportDetail.VAT = _RegistryRowsFinancial.VAT
                        _VATReportDetail.DocumentNumber = _RegistryRowsFinancial.RegistryHeader.DocumentNumber
                        _VATReportDetail.Customers = _RegistryRowsFinancial.RegistryHeader.Customers
                        _VATReportDetail.DateExecution = _RegistryRowsFinancial.RegistryHeader.F_DateExecution
                        _VATReportDetail.PartnerType = ePartnerType.ePayabels
                        _VATReportDetail.DateVAT = _PCIDetail.DateExecution
                        _VATReportDetail.PayMode = _RegistryRowsFinancial.RegistryHeader.F_PayMode
                        _VATReportDetail.RegistryHeader = _RegistryRowsFinancial.RegistryHeader
                        _VATReportDetail.Note = _RegistryRowsFinancial.RegistryHeader.ShortName
                    End If
                Next

            Else
                _VATReportDetail = New VATReportDetail(_uow)
                _VATReportDetail.VATReportDetailRowType = eVATReportDetailRowType.efromRegistry
                _VATReportDetail.VATReport = _VATReport

                _VATReportDetail.AmountVATIn = Math.Round(_RegistryRowsFinancial.BruttoAmount / (100 + _RegistryRowsFinancial.VAT.KeyValuePercent) * _RegistryRowsFinancial.VAT.KeyValuePercent, 0, MidpointRounding.AwayFromZero)
                _VATReportDetail.AmountVATInBase = _RegistryRowsFinancial.BruttoAmount - _VATReportDetail.AmountVATIn

                If _RegistryRowsFinancial.VAT IsNot Nothing Then
                    If _RegistryRowsFinancial.VAT.InversState = True Then
                        _VATReportDetail.AmountVATOut = Math.Round((_RegistryRowsFinancial.BruttoAmount * _RegistryRowsFinancial.VAT.InversKeyValue), 0, MidpointRounding.AwayFromZero)
                        _VATReportDetail.AmountVATOutBase = _RegistryRowsFinancial.BruttoAmount

                        _VATReportDetail.AmountVATIn = Math.Round((_RegistryRowsFinancial.BruttoAmount * _RegistryRowsFinancial.VAT.InversKeyValue), 0, MidpointRounding.AwayFromZero)
                        _VATReportDetail.AmountVATInBase = _RegistryRowsFinancial.BruttoAmount

                    Else
                        _VATReportDetail.AmountVATOut = 0
                        _VATReportDetail.AmountVATOutBase = 0
                    End If
                Else
                    _VATReportDetail.AmountVATOut = 0
                    _VATReportDetail.AmountVATOutBase = 0
                End If

                _VATReportDetail.VAT = _RegistryRowsFinancial.VAT
                _VATReportDetail.DocumentNumber = _RegistryRowsFinancial.RegistryHeader.DocumentNumber
                _VATReportDetail.Customers = _RegistryRowsFinancial.RegistryHeader.Customers
                _VATReportDetail.DateExecution = _RegistryRowsFinancial.RegistryHeader.F_DateExecution
                _VATReportDetail.PartnerType = ePartnerType.ePayabels
                _VATReportDetail.DateVAT = _RegistryRowsFinancial.DateVAT
                _VATReportDetail.PayMode = _RegistryRowsFinancial.RegistryHeader.F_PayMode
                _VATReportDetail.RegistryHeader = _RegistryRowsFinancial.RegistryHeader
                _VATReportDetail.Note = _RegistryRowsFinancial.RegistryHeader.ShortName
            End If
        Next

        'RaiseEvent FinalWork

        '-- Harmadszor a közvetlen vevői számlák !
        Dim _GLRows_Collection_0 As New XPCollection(Of GLRows)(_uow, _
         CriteriaOperator.Parse(" GLHeader.IsEditable=False And GLHeader.CustomersFrom=? And " & _
                                " GLRowsTypeVAT=1 And GLHeader.GLDocumentType=1 And " & _
                                " (isnull(GLHeader.ImportedInvoice)=True And PartnerType=0) And " & _
                                " DateVAT>=? And DateVAT<=?", _
                                _VATReport.CustomersFrom, _VATReport.DateFrom, DateAdd(DateInterval.Minute, 1439, _VATReport.DateTo.Date)))
        RaiseEvent InitWork(1, 1, _GLRows_Collection_0.Count)
        For Each _GLRows In _GLRows_Collection_0
            RaiseEvent DoWork()

            

            _VATReportDetail = New VATReportDetail(_uow)
            _VATReportDetail.VATReportDetailRowType = eVATReportDetailRowType.efromAccountDirect
            _VATReportDetail.VATReport = _VATReport

            _VATReportDetail.AmountVATOut = _GLRows.AmountHUF
            If _GLRows.VAT.KeyValue <> 0 Then
                _VATReportDetail.AmountVATOutBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.KeyValue), 0)
            Else
                _VATReportDetail.AmountVATOutBase = 0
                Dim _GLRows_Netto_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLRowsGUID=? And GLRowsTypeVAT=0", _GLRows.GLRowsGUID))
                Dim _GLRows_Netto As GLRows
                For Each _GLRows_Netto In _GLRows_Netto_Collection
                    _VATReportDetail.AmountVATOutBase += _GLRows_Netto.AmountHUF
                Next
            End If

            _VATReportDetail.DocumentNumber = _GLRows.GLHeader.DocumentNumber
            _VATReportDetail.Customers = _GLRows.Customer
            _VATReportDetail.DateExecution = _GLRows.GLHeader.DateExecution
            _VATReportDetail.DateVAT = _GLRows.DateVAT
            _VATReportDetail.PayMode = TryCast(_GLRows.GLHeader, GLAccounts).PaymentOption.PayMode
            _VATReportDetail.Note = _GLRows.NoteRows
            _VATReportDetail.PartnerType = ePartnerType.eReceivables
            _VATReportDetail.VAT = _GLRows.VAT

        Next
        'RaiseEvent FinalWork

        '-- Negyedszer a közvetlen szállítói számlák iktatás nélkül !
        Dim _GLRows_Collection_1 As New XPCollection(Of GLRows)(_uow, _
         CriteriaOperator.Parse(" GLHeader.IsEditable=False And GLHeader.CustomersFrom=? And " & _
                                " GLRowsTypeVAT=1 And GLHeader.GLDocumentType=1 And " & _
                                " (isnull(GLHeader.ImportedRegistryHeader)=True And PartnerType=1 And GLHeader.IsRegistryGLAccounts=False) And " & _
                                " DateVAT>=? And DateVAT<=?", _
                                _VATReport.CustomersFrom, _VATReport.DateFrom, DateAdd(DateInterval.Minute, 1439, _VATReport.DateTo.Date)))
        RaiseEvent InitWork(1, 1, _GLRows_Collection_1.Count)
        For Each _GLRows In _GLRows_Collection_1
            RaiseEvent DoWork()

            _GLAccounts = TryCast(_GLRows.GLHeader, GLAccounts)

            If _GLAccounts.VATAvowalType = eVATAvowalType.eFinancial Then
                _PCIDetail_Collection = New XPCollection(Of PCIDetail)(_uow,
                                            CriteriaOperator.Parse("GetDate(DateExecution)<=? And " +
                                                                   " PartnerType=? And " +
                                                                   " ConnectInfo=? And " +
                                                                   " Customers=? And " +
                                                                   " CustomersFrom=?", _VATReport.DateTo.Date, ePartnerType.ePayabels,
                                                                   _GLAccounts.ConnectInfo, _GLAccounts.Customers,
                                                                   _GLAccounts.CustomersFrom))
                _BallanceHUF = 0
                For Each _PCIDetail In _PCIDetail_Collection
                    _BallanceHUF = _PCIDetail.PCIGroup.AmountHUF_Account
                    Exit For
                Next
                For Each _PCIDetail In _PCIDetail_Collection
                    If _PCIDetail.MainType = "02-Bank" Or _PCIDetail.MainType = "03-Cassa" Or _PCIDetail.MainType = "04-Compensation" Or _PCIDetail.MainType = "05-Ratediff" Then

                        _VATReportDetail = New VATReportDetail(_uow)
                        _VATReportDetail.VATReportDetailRowType = eVATReportDetailRowType.efromAccountDirect
                        _VATReportDetail.VATReport = _VATReport

                        _VATReportDetail.AmountVATIn = _GLRows.AmountHUF
                        If _GLRows.VAT.KeyValue <> 0 Then
                            _VATReportDetail.AmountVATInBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.KeyValue), 0, MidpointRounding.AwayFromZero)
                        Else
                            _VATReportDetail.AmountVATInBase = 0
                            Dim _GLRows_Netto_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLRowsGUID=? And GLRowsTypeVAT=0", _GLRows.GLRowsGUID))
                            Dim _GLRows_Netto As GLRows
                            For Each _GLRows_Netto In _GLRows_Netto_Collection
                                _VATReportDetail.AmountVATInBase += _GLRows_Netto.AmountHUF
                            Next
                        End If

                        _VATReportDetail.DocumentNumber = _GLRows.GLHeader.DocumentNumber
                        _VATReportDetail.Customers = _GLRows.Customer
                        _VATReportDetail.DateExecution = _GLRows.GLHeader.DateExecution
                        _VATReportDetail.DateVAT = _GLRows.DateVAT
                        _VATReportDetail.PayMode = TryCast(_GLRows.GLHeader, GLAccounts).PaymentOption.PayMode
                        _VATReportDetail.Note = _GLRows.NoteRows
                        _VATReportDetail.PartnerType = _GLRows.PartnerType
                        _VATReportDetail.VAT = _GLRows.VAT
                        If _GLRows.VAT.InversState = True Then
                            _VATReportDetail.AmountVATOut = _GLRows.AmountHUF
                            If _GLRows.VAT.InversKeyValue <> 0 Then _VATReportDetail.AmountVATOutBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.InversKeyValue), 0, MidpointRounding.AwayFromZero)
                            If _GLRows.VAT.InversKeyValue <> 0 Then _VATReportDetail.AmountVATInBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.InversKeyValue), 0, MidpointRounding.AwayFromZero)
                        End If

                        '---------------------------
                        _VATReportDetail.AmountVATOut = Math.Round(_VATReportDetail.AmountVATOut * (Math.Abs(_PCIDetail.BallanceHUF) / Math.Abs(_BallanceHUF)), 0, MidpointRounding.AwayFromZero)
                        _VATReportDetail.AmountVATOutBase = Math.Round(_VATReportDetail.AmountVATOutBase * (Math.Abs(_PCIDetail.BallanceHUF) / Math.Abs(_BallanceHUF)), 0, MidpointRounding.AwayFromZero)
                        _VATReportDetail.AmountVATIn = Math.Round(_VATReportDetail.AmountVATIn * (Math.Abs(_PCIDetail.BallanceHUF) / Math.Abs(_BallanceHUF)), 0, MidpointRounding.AwayFromZero)
                        _VATReportDetail.AmountVATInBase = Math.Round(_VATReportDetail.AmountVATInBase * (Math.Abs(_PCIDetail.BallanceHUF) / Math.Abs(_BallanceHUF)), 0, MidpointRounding.AwayFromZero)
                        '--------------------------
                    End If
                Next
            Else
                _VATReportDetail = New VATReportDetail(_uow)
                _VATReportDetail.VATReportDetailRowType = eVATReportDetailRowType.efromAccountDirect
                _VATReportDetail.VATReport = _VATReport

                _VATReportDetail.AmountVATIn = _GLRows.AmountHUF
                If _GLRows.VAT.KeyValue <> 0 Then
                    _VATReportDetail.AmountVATInBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.KeyValue), 0, MidpointRounding.AwayFromZero)
                Else
                    _VATReportDetail.AmountVATInBase = 0
                    Dim _GLRows_Netto_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLRowsGUID=? And GLRowsTypeVAT=0", _GLRows.GLRowsGUID))
                    Dim _GLRows_Netto As GLRows
                    For Each _GLRows_Netto In _GLRows_Netto_Collection
                        _VATReportDetail.AmountVATInBase += _GLRows_Netto.AmountHUF
                    Next
                End If

                _VATReportDetail.DocumentNumber = _GLRows.GLHeader.DocumentNumber
                _VATReportDetail.Customers = _GLRows.Customer
                _VATReportDetail.DateExecution = _GLRows.GLHeader.DateExecution
                _VATReportDetail.DateVAT = _GLRows.DateVAT
                _VATReportDetail.PayMode = TryCast(_GLRows.GLHeader, GLAccounts).PaymentOption.PayMode
                _VATReportDetail.Note = _GLRows.NoteRows
                _VATReportDetail.PartnerType = _GLRows.PartnerType
                _VATReportDetail.VAT = _GLRows.VAT
                If _GLRows.VAT.InversState = True Then
                    _VATReportDetail.AmountVATOut = _GLRows.AmountHUF
                    If _GLRows.VAT.InversKeyValue <> 0 Then _VATReportDetail.AmountVATOutBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.InversKeyValue), 0, MidpointRounding.AwayFromZero)
                    If _GLRows.VAT.InversKeyValue <> 0 Then _VATReportDetail.AmountVATInBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.InversKeyValue), 0, MidpointRounding.AwayFromZero)
                End If
            End If
        Next
        'RaiseEvent FinalWork

        '-- Ötödször a közvetlen szállítói számlák iktatással !
        Dim _GLRows_Collection_2 As New XPCollection(Of GLRows)(_uow, _
         CriteriaOperator.Parse(" GLHeader.IsEditable=False And GLHeader.CustomersFrom=? And " & _
                                " GLRowsTypeVAT=1 And GLHeader.GLDocumentType=1 And " & _
                                " (isnull(GLHeader.ImportedRegistryHeader)=False And PartnerType=1 And GLHeader.IsRegistryGLAccounts=True) And " & _
                                " DateVAT>=? And DateVAT<=?", _
                                _VATReport.CustomersFrom, _VATReport.DateFrom, DateAdd(DateInterval.Minute, 1439, _VATReport.DateTo.Date)))
        RaiseEvent InitWork(1, 1, _GLRows_Collection_2.Count)
        For Each _GLRows In _GLRows_Collection_2
            RaiseEvent DoWork()
            _VATReportDetail = New VATReportDetail(_uow)
            _VATReportDetail.VATReportDetailRowType = eVATReportDetailRowType.efromAccount
            _VATReportDetail.VATReport = _VATReport

            _VATReportDetail.AmountVATIn = _GLRows.AmountHUF
            If _GLRows.VAT.KeyValue <> 0 Then
                _VATReportDetail.AmountVATInBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.KeyValue), 0, MidpointRounding.AwayFromZero)
            Else
                _VATReportDetail.AmountVATInBase = 0
                Dim _GLRows_Netto_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLRowsGUID=? And GLRowsTypeVAT=0", _GLRows.GLRowsGUID))
                Dim _GLRows_Netto As GLRows
                For Each _GLRows_Netto In _GLRows_Netto_Collection
                    _VATReportDetail.AmountVATInBase += _GLRows_Netto.AmountHUF
                Next
            End If

            _VATReportDetail.DocumentNumber = _GLRows.GLHeader.DocumentNumber
            _VATReportDetail.Customers = _GLRows.Customer
            _VATReportDetail.DateExecution = _GLRows.GLHeader.DateExecution
            _VATReportDetail.DateVAT = _GLRows.DateVAT
            _VATReportDetail.PayMode = TryCast(_GLRows.GLHeader, GLAccounts).PaymentOption.PayMode
            _VATReportDetail.Note = _GLRows.NoteRows
            _VATReportDetail.PartnerType = _GLRows.PartnerType
            _VATReportDetail.VAT = _GLRows.VAT
            If _GLRows.VAT.InversState = True Then
                _VATReportDetail.AmountVATOut = _GLRows.AmountHUF
                If _GLRows.VAT.InversKeyValue <> 0 Then _VATReportDetail.AmountVATOutBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.InversKeyValue), 0, MidpointRounding.AwayFromZero)
                If _GLRows.VAT.InversKeyValue <> 0 Then _VATReportDetail.AmountVATInBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.InversKeyValue), 0, MidpointRounding.AwayFromZero)
            End If
        Next
        'RaiseEvent FinalWork

        '-- Hatodszor a normál ÁFÁs pénztári tételek !
        Dim _GLRowsCassa_Collection As New XPCollection(Of GLRows)(_uow, _
         CriteriaOperator.Parse("GLHeader.IsEditable=False And GLHeader.CustomersFrom=? And " & _
                                " GLRowsTypeVAT=1 And GLHeader.GLDocumentType=3 And " & _
                                " DateVAT>=? And DateVAT<=? And isnull(VAT)=False", _
                                _VATReport.CustomersFrom, _VATReport.DateFrom, DateAdd(DateInterval.Minute, 1439, _VATReport.DateTo.Date)))
        RaiseEvent InitWork(1, 1, _GLRowsCassa_Collection.Count)
        For Each _GLRows In _GLRowsCassa_Collection
            RaiseEvent DoWork()
            _VATReportDetail = New VATReportDetail(_uow)
            _VATReportDetail.VATReportDetailRowType = eVATReportDetailRowType.efromCassa
            _VATReportDetail.VATReport = _VATReport

            If _GLRows.OutAmountHUF > 0 Then
                _VATReportDetail.AmountVATIn = _GLRows.AmountHUF
                _VATReportDetail.AmountVATIn = _GLRows.AmountHUF

                If _GLRows.VAT.KeyValue <> 0 Then
                    _VATReportDetail.AmountVATInBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.KeyValue), 0, MidpointRounding.AwayFromZero)
                Else
                    _VATReportDetail.AmountVATInBase = 0
                    Dim _GLRows_Netto_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLRowsGUID=? And GLRowsTypeVAT=0", _GLRows.GLRowsGUID))
                    Dim _GLRows_Netto As GLRows
                    For Each _GLRows_Netto In _GLRows_Netto_Collection
                        _VATReportDetail.AmountVATInBase += _GLRows_Netto.AmountHUF
                    Next
                End If

                _VATReportDetail.DocumentNumber = _GLRows.GLHeader.DocumentNumber & ", " & _GLRows.ConnectInfo
                _VATReportDetail.Customers = _GLRows.Customer
                _VATReportDetail.DateExecution = _GLRows.GLHeader.DateExecution
                _VATReportDetail.DateVAT = _GLRows.DateVAT
                _VATReportDetail.PayMode = ePayMode.InCash
                If String.IsNullOrEmpty(_GLRows.NoteRows) = False Then
                    _VATReportDetail.Note = _GLRows.NoteRows
                Else
                    _VATReportDetail.Note = _GLRows.GLHeader.NoteHeader
                End If
                _VATReportDetail.PartnerType = ePartnerType.ePayabels
                _VATReportDetail.VAT = _GLRows.VAT
                If _GLRows.VAT.InversState = True Then
                    _VATReportDetail.AmountVATOut = _GLRows.AmountHUF
                    If _GLRows.VAT.InversKeyValue <> 0 Then _VATReportDetail.AmountVATOutBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.InversKeyValue), 0, MidpointRounding.AwayFromZero)
                    If _GLRows.VAT.InversKeyValue <> 0 Then _VATReportDetail.AmountVATInBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.InversKeyValue), 0, MidpointRounding.AwayFromZero)
                End If
            Else
                _VATReportDetail.AmountVATOut = _GLRows.AmountHUF

                If _GLRows.VAT.KeyValue <> 0 Then
                    _VATReportDetail.AmountVATOutBase = Math.Round(_GLRows.AmountHUF / (_GLRows.VAT.KeyValue), 0)
                Else
                    _VATReportDetail.AmountVATOutBase = 0
                    Dim _GLRows_Netto_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("GLRowsGUID=? And GLRowsTypeVAT=0", _GLRows.GLRowsGUID))
                    Dim _GLRows_Netto As GLRows
                    For Each _GLRows_Netto In _GLRows_Netto_Collection
                        _VATReportDetail.AmountVATOutBase += _GLRows_Netto.AmountHUF
                    Next
                End If

                _VATReportDetail.DocumentNumber = _GLRows.GLHeader.DocumentNumber & ", " & _GLRows.ConnectInfo
                _VATReportDetail.Customers = _GLRows.Customer
                _VATReportDetail.DateExecution = _GLRows.GLHeader.DateExecution
                _VATReportDetail.DateVAT = _GLRows.DateVAT
                _VATReportDetail.PayMode = ePayMode.InCash
                If String.IsNullOrEmpty(_GLRows.NoteRows) = False Then
                    _VATReportDetail.Note = _GLRows.NoteRows
                Else
                    _VATReportDetail.Note = _GLRows.GLHeader.NoteHeader
                End If
                _VATReportDetail.PartnerType = ePartnerType.eReceivables
                _VATReportDetail.VAT = _GLRows.VAT
            End If

        Next
        'RaiseEvent FinalWork
        '-------------------------------------------------------------------------
        'hetedszer a vegyes ÁFÁ-s tételek
        Dim _Need As Boolean = False
        Dim _GLRowsMixed_Collection As New XPCollection(Of GLRows)(_uow, _
         CriteriaOperator.Parse("GLHeader.IsEditable=False And GLHeader.CustomersFrom=? And " & _
                                " GLHeader.GLDocumentType=0 And " & _
                                " GetDate(GLHeader.DateExecution)>=? And GetDate(GLHeader.DateExecution)<=? And isnull(VAT)=False", _
                                _VATReport.CustomersFrom, _VATReport.DateFrom.Date, _VATReport.DateTo.Date))
        RaiseEvent InitWork(1, 1, _GLRowsMixed_Collection.Count)
        For Each _GLRows In _GLRowsMixed_Collection
            RaiseEvent DoWork()
            _Need = False
            If _GLRows.DebitChartOfAccounts.AccountNumber = _GLRows.VAT.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eIn).AccountNumber Then
                _Need = True
            End If
            If _GLRows.CreditChartOfAccounts.AccountNumber = _GLRows.VAT.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut).AccountNumber Then
                _Need = True
            End If

            If _Need Then
                _VATReportDetail = New VATReportDetail(_uow)
                _VATReportDetail.VATReportDetailRowType = eVATReportDetailRowType.efromMixed
                _VATReportDetail.VATReport = _VATReport
                If _GLRows.DebitChartOfAccounts.AccountNumber = _GLRows.VAT.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eIn).AccountNumber Then
                    'Szállítói levonható áfa
                    _VATReportDetail.AmountVATIn = _GLRows.AmountHUF
                    _VATReportDetail.AmountVATInBase = Math.Round(_GLRows.AmountHUF / _GLRows.VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                End If
                If _GLRows.CreditChartOfAccounts.AccountNumber = _GLRows.VAT.GetChartOfAccounts(_GLRows.GLHeader.DateExecution.Year, eVATMode.eOut).AccountNumber Then
                    'Vevői fizetendő áfa
                    _VATReportDetail.AmountVATOut = _GLRows.AmountHUF
                    _VATReportDetail.AmountVATOutBase = Math.Round(_GLRows.AmountHUF / _GLRows.VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                End If
                _VATReportDetail.DocumentNumber = _GLRows.GLHeader.DocumentNumber & ", " & _GLRows.ConnectInfo
                _VATReportDetail.DateExecution = _GLRows.GLHeader.DateExecution
                _VATReportDetail.DateVAT = _GLRows.GLHeader.DateExecution
                _VATReportDetail.PayMode = ePayMode.NotSet
                If String.IsNullOrEmpty(_GLRows.NoteRows) = False Then
                    _VATReportDetail.Note = _GLRows.NoteRows
                Else
                    _VATReportDetail.Note = _GLRows.GLHeader.NoteHeader
                End If
                _VATReportDetail.PartnerType = ePartnerType.eNotSet
                _VATReportDetail.VAT = _GLRows.VAT
            End If
        Next
        _uow.CommitChanges()

        'RaiseEvent FinalWork
        _VATReport.TotalAmountIn = 0
        _VATReport.TotalAmountOut = 0
        i = 0
        Dim query = From _VATD In _VATReport.VATReportDetail
                    Where _VATD.VATReport Is _VATReport
            Group _VATD By _VATD.VAT, _
                           _VATD.PartnerType Into g = Group _
            Select New With {Key .VAT = VAT, _
                             Key .PartnerType = PartnerType, _
                             Key .AmountVATIn = g.Sum(Function(inv) inv.AmountVATIn), _
                             Key .AmountVATInBase = g.Sum(Function(inv) inv.AmountVATInBase), _
                             Key .AmountVATOut = g.Sum(Function(inv) inv.AmountVATOut), _
                             Key .AmountVATOutBase = g.Sum(Function(inv) inv.AmountVATOutBase)}

        For Each item In query
            _VATReportGroup = New VATReportGroup(_uow)
            _VATReportGroup.VATReport = _VATReport
            _VATReportGroup.VAT = item.VAT
            _VATReportGroup.PartnerType = item.PartnerType
            _VATReportGroup.AmountVATIn = item.AmountVATIn
            _VATReportGroup.AmountVATInBase = item.AmountVATInBase
            _VATReportGroup.AmountVATOut = item.AmountVATOut
            _VATReportGroup.AmountVATOutBase = item.AmountVATOutBase
            _VATReportGroup.Save()
            _VATReport.TotalAmountOut += item.AmountVATOut
            _VATReport.TotalAmountIn += item.AmountVATIn
        Next
        _uow.CommitChanges()

        '----------- Most a nem fizetett számlák listája (csak szállítói !!!)
        Dim PCI As New XPQuery(Of PCIDetail)(_uow)
        Dim query3 = From _PCI In PCI
                    Where (_PCI.DateExecution <= DateAdd(DateInterval.Minute, 1439, _VATReport.DateTo.Date) And
                          _PCI.Customers IsNot Nothing And
                          _PCI.ConnectInfo IsNot Nothing And
                          _PCI.PartnerType = ePartnerType.ePayabels And
                          _PCI.MainType = "01-Account") Or
                         (_PCI.DateExecution < _VATReport.DateVAT And
                           _PCI.Customers IsNot Nothing And
                          _PCI.ConnectInfo IsNot Nothing And
                          _PCI.PartnerType = ePartnerType.ePayabels And
                          _PCI.MainType <> "01-Account")
            Group _PCI By _PCI.CustomersFrom, _
                          _PCI.Customers, _
                          _PCI.PartnerType, _
                          _PCI.CurrencyName, _
                          _PCI.ConnectInfo Into g = Group
            Order By PartnerType, CurrencyName, CustomersFrom, Customers, ConnectInfo
            Select New With {Key .CustomersFrom = CustomersFrom, _
                             Key .Customers = Customers, _
                             Key .PartnerType = PartnerType, _
                             Key .ConnectInfo = ConnectInfo, _
                             Key .CurrencyName = CurrencyName, _
                             Key .BallanceHUF = g.Sum(Function(inv) inv.BallanceHUF), _
                             Key .BallanceDEV = g.Sum(Function(inv) inv.BallanceDEV)}


        RaiseEvent InitWork(1, 1, query3.Count)
        _VatReportDetail_Collection = New XPCollection(Of VATReportDetail)(_uow, CriteriaOperator.Parse("VATReport=? And PartnerType=1", _VATReport))
        _VatReportDetail_Collection.Sorting.Add(New SortProperty("Customers.FullName", DB.SortingDirection.Ascending))
        _VatReportDetail_Collection.Sorting.Add(New SortProperty("DocumentNumber", DB.SortingDirection.Ascending))

        For Each _VATReportDetail In _VatReportDetail_Collection
            _Array.Add(_VATReportDetail)
        Next
        For Each item In query3
            RaiseEvent DoWork()

            For Each _VATReportDetail In _Array
                If item.Customers IsNot Nothing AndAlso _VATReportDetail.Customers IsNot Nothing Then
                    If item.Customers.FullName = _VATReportDetail.Customers.FullName And _
                       item.ConnectInfo = _VATReportDetail.DocumentNumber Then
                        If item.BallanceHUF <> 0 Then
                            _VRD = _uow.GetObjectByKey(Of VATReportDetail)(_VATReportDetail.Oid)
                            _VRD.AmountVATInNoPay = 0
                            _VRD.AmountVATInBaseNoPay = 0

                            _VATReportNoPayDetail = New VATReportNoPayDetail(_uow)
                            _VATReportNoPayDetail.VATReport = _VATReport
                            _VATReportNoPayDetail.VAT = _VRD.VAT
                            _VATReportNoPayDetail.AmountVATIn = _VRD.AmountVATIn
                            _VATReportNoPayDetail.AmountVATInBase = _VRD.AmountVATInBase
                            _VATReportNoPayDetail.Customers = _VRD.Customers
                            _VATReportNoPayDetail.DateExecution = _VRD.DateExecution
                            _VATReportNoPayDetail.DateVAT = _VRD.DateVAT
                            _VATReportNoPayDetail.DocumentNumber = _VRD.DocumentNumber
                            _VATReportNoPayDetail.Note = _VRD.Note
                            _VATReportNoPayDetail.PartnerType = ePartnerType.ePayabels
                            _VATReportNoPayDetail.PayMode = _VRD.PayMode
                            _VATReportNoPayDetail.RegistryHeader = _VRD.RegistryHeader

                        Else
                            _VRD = _uow.GetObjectByKey(Of VATReportDetail)(_VATReportDetail.Oid)
                            _VRD.AmountVATInNoPay = _VATReportDetail.AmountVATIn
                            _VRD.AmountVATInBaseNoPay = _VATReportDetail.AmountVATInBase

                        End If
                    End If
                End If
            Next
        Next
        _uow.CommitChanges()
        'RaiseEvent FinalWork


        '// FAD-os vevői szlák
        Select Case GLOBAL_SQLType
            Case eSQLType.MySQL, eSQLType.MSSQL
                _uow.ExecuteNonQuery(String.Format("delete from VATReport07 where VATReport='{0}'", _VATReport.Oid))
                _uow.ExecuteNonQuery(String.Format("delete from VATReport08 where VATReport='{0}'", _VATReport.Oid))
                _uow.CommitChanges()
            Case eSQLType.PostgreSQL
                _uow.ExecuteNonQuery(String.Format("delete from ""VATReport07"" where ""VATReport""='{0}'", _VATReport.Oid))
                _uow.ExecuteNonQuery(String.Format("delete from ""VATReport08"" where ""VATReport""='{0}'", _VATReport.Oid))
                _uow.CommitChanges()

        End Select

        '//07 ! -----------------------------------------------------------------------------------------------------------------------------------

        '// Nettó súlyok összeszámlálása a FAD alapján !
        Dim _InvoiceRows_07_Collection As New XPCollection(Of InvoiceRows)(_uow, _
                                      CriteriaOperator.Parse("VAT.InversState=True and GetDate(InvoiceHeader.DateExecution)>=? and " + _
                                                             "GetDate(InvoiceHeader.DateExecution)<=? and InvoiceHeader.IsEditable=False and " + _
                                                             "InvoiceHeader.DocumentNumber !='' and InvoiceHeader.CustomersFrom.Oid=? and " + _
                                                             "IsNull(OrderInRows)=False", _VATReport.DateFrom.Date, _VATReport.DateTo.Date, _
                                                             _VATReport.CustomersFrom.Oid))
        Dim _InvoiceRows_07 As InvoiceRows
        Dim _Weight_NAV As Double = 0
        For Each _InvoiceRows_07 In _InvoiceRows_07_Collection
            _Weight_NAV = 0

            If _InvoiceRows_07.DeliveryNoteOutRows IsNot Nothing Then
                If _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows IsNot Nothing Then
                    If _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows IsNot Nothing Then
                        Select Case _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.Units.ShortName
                            Case "to", "tonna"
                                _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.WeightMode = eWeightMode.eTotalWeightperQuantity
                                _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.Weight = _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.QTT * 1000
                                _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.Save()
                            Case "kg"
                                _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.WeightMode = eWeightMode.eTotalWeightperQuantity
                                _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.Weight = _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.QTT
                                _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.Save()
                        End Select
                        '_Weight_NAV += (_InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.Weight / _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.QTT) * _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.QTT
                        _Weight_NAV += (_InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.Weight / _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows.QTT) * _InvoiceRows_07.QTT
                    Else
                        If _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.Weight > 0 Then
                            '_Weight_NAV += (_InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.Weight / _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.QTT) * _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.QTT
                            _Weight_NAV += (_InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.Weight / _InvoiceRows_07.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.QTT) * _InvoiceRows_07.QTT
                        End If
                    End If
                End If
                _InvoiceRows_07.Weight_NAV = _Weight_NAV
            ElseIf _InvoiceRows_07.OrderInRows IsNot Nothing Then
                Dim _StorageOutRowsInRows_07 As StorageOutRowsInRows

                For Each _StorageOutRowsInRows_07 In _InvoiceRows_07.StorageOutRowsInRows
                    If _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows IsNot Nothing Then
                        Select Case _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.Units.ShortName
                            Case "to", "tonna"
                                _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.WeightMode = eWeightMode.eTotalWeightperQuantity
                                _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.Weight = _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.QTT * 1000
                                _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.Save()
                            Case "kg"
                                _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.WeightMode = eWeightMode.eTotalWeightperQuantity
                                _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.Weight = _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.QTT
                                _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.Save()
                        End Select
                        _Weight_NAV += (_StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.Weight / _StorageOutRowsInRows_07.StorageInRows.DeliveryNoteInRows.QTT) * _InvoiceRows_07.QTT
                    Else
                        If _StorageOutRowsInRows_07.StorageInRows.Weight > 0 Then
                            _Weight_NAV += (_StorageOutRowsInRows_07.StorageInRows.Weight / _StorageOutRowsInRows_07.StorageInRows.QTT) * _InvoiceRows_07.QTT
                        End If
                    End If
                Next
                _InvoiceRows_07.Weight_NAV = _Weight_NAV
            End If
        Next

        _uow.CommitChanges()

        _uow.ExecuteNonQuery("UPDATE InvoiceRows t1 JOIN InvoiceRows t2 ON t1.InvoiceRows=t2.Oid AND t2.QTT<>0 SET t1.Weight_NAV=(t2.Weight_NAV/t2.QTT)*t1.QTT")

        Dim _VATReport07 As VATReport07
        Dim _InvoiceRows As New XPQuery(Of InvoiceRows)(_uow)
        Dim query4 = From _IRW In _InvoiceRows
        Where _IRW.OrderInRows IsNot Nothing And
              _IRW.OrderInRows.Products.CustomsTariff IsNot Nothing And
              String.IsNullOrEmpty(_IRW.OrderInRows.Products.CustomsTariff.NAVCode) = False And
              _IRW.InvoiceHeader.DateExecution.Date >= _VATReport.DateFrom.Date And
              _IRW.InvoiceHeader.DateExecution.Date <= _VATReport.DateTo.Date And
              _IRW.InvoiceHeader.CustomersFrom.Oid = _VATReport.CustomersFrom.Oid And
              _IRW.VAT.InversState = True And
              _IRW.InvoiceHeader.IsEditable = False And
              String.IsNullOrEmpty(_IRW.InvoiceHeader.DocumentNumber) = False And
              _IRW.InvoiceHeader.InvoiceType.InvoiceTypeBase = eInvoiceTypeBase.eNormalInvoice
              Group _IRW By _IRW.InvoiceHeader.Customers, _IRW.OrderInRows.Products.CustomsTariff,
                            _IRW.InvoiceHeader.DateExecution.Year,
                            _IRW.InvoiceHeader.DateExecution.Month,
                            _IRW.InvoiceHeader.DateExecution.Day Into g = Group
        Select New With {Key .Customers = Customers,
                         Key .CustomTariff = CustomsTariff,
                         Key .DateExecutionYear = [Year],
                         Key .DateExecutionMonth = [Month],
                         Key .DateExecutionDay = [Day],
                         Key .Weight = g.Sum(Function(inv) inv.Weight_NAV),
                         Key .Amount = g.Sum(Function(inv) inv.SumPriceHUF)}

        RaiseEvent InitWork(1, 1, query4.Count)
        For Each _item In query4
            RaiseEvent DoWork()
            _VATReport07 = New VATReport07(_uow)
            _VATReport07.VATReport = _VATReport
            _VATReport07.DateExecution = New Date(_item.DateExecutionYear, _item.DateExecutionMonth, _item.DateExecutionDay)
            _VATReport07.Customers = _item.Customers
            _VATReport07.CustomsTariffs = _item.CustomTariff
            _VATReport07.Weight = _item.Weight
            _VATReport07.AmountVATOutBase = _item.Amount
        Next
        _uow.CommitChanges()
        'RaiseEvent FinalWork



        '//08-as nyomtatvány a leigazolásokból !

        '//RegistryRowsFinancial-ból az ÁFA !!!
        Dim _DeliveryNoteInRRFC_Collection_VAT As New XPCollection(Of DeliveryNoteInRRFC) _
            (_uow, CriteriaOperator.Parse("GetDate(RegistryRowsFinancialControlling.RegistryHeader.F_DateVAT)>=? and " + _
                                          "GetDate(RegistryRowsFinancialControlling.RegistryHeader.F_DateVAT)<=?", _
                                          _VATReport.DateFrom.Date, _VATReport.DateTo.Date))
        Dim _DeliveryNoteInRRFC_VAT As DeliveryNoteInRRFC

        For Each _DeliveryNoteInRRFC_VAT In _DeliveryNoteInRRFC_Collection_VAT
            If _DeliveryNoteInRRFC_VAT.RegistryRowsFinancialControlling.RegistryHeader.RegistryRowsFinancial.Count = 1 Then
                _DeliveryNoteInRRFC_VAT.DeliveryNoteInRows.VAT = _DeliveryNoteInRRFC_VAT.RegistryRowsFinancialControlling.RegistryHeader.RegistryRowsFinancial(0).VAT
            ElseIf _DeliveryNoteInRRFC_VAT.RegistryRowsFinancialControlling.RegistryHeader.RegistryRowsFinancial.Count = 2 Then
                If _DeliveryNoteInRRFC_VAT.RegistryRowsFinancialControlling.RegistryHeader.RegistryRowsFinancial(0).VAT Is _DeliveryNoteInRRFC_VAT.RegistryRowsFinancialControlling.RegistryHeader.RegistryRowsFinancial(1).VAT Then
                    _DeliveryNoteInRRFC_VAT.DeliveryNoteInRows.VAT = _DeliveryNoteInRRFC_VAT.RegistryRowsFinancialControlling.RegistryHeader.RegistryRowsFinancial(0).VAT
                End If
            End If
        Next
        _uow.CommitChanges()


        Dim _VATReport08 As VATReport08
        Dim _DeliveryNoteInRRFC As New XPQuery(Of DeliveryNoteInRRFC)(_uow)
        Dim query5 = From _DNIRRFC In _DeliveryNoteInRRFC
                     Where _DNIRRFC.RegistryRowsFinancialControlling.RegistryHeader IsNot Nothing And
              _DNIRRFC.DeliveryNoteInRows.Products.CustomsTariff IsNot Nothing And
              String.IsNullOrEmpty(_DNIRRFC.DeliveryNoteInRows.Products.CustomsTariff.NAVCode) = False And
              _DNIRRFC.RegistryRowsFinancialControlling.RegistryHeader.F_DateVAT.Date >= _VATReport.DateFrom.Date And
              _DNIRRFC.RegistryRowsFinancialControlling.RegistryHeader.F_DateVAT.Date <= _VATReport.DateTo.Date And
              _DNIRRFC.RegistryRowsFinancialControlling.RegistryHeader.CustomersFrom.Oid = _VATReport.CustomersFrom.Oid And
              _DNIRRFC.DeliveryNoteInRows.VAT.InversState = True And
              _DNIRRFC.DeliveryNoteInRows.DeliveryNoteInHeader.IsEditable = False And
              String.IsNullOrEmpty(_DNIRRFC.DeliveryNoteInRows.DeliveryNoteInHeader.DocumentNumber) = False
                     Group _DNIRRFC By _DNIRRFC.RegistryRowsFinancialControlling.RegistryHeader.Customers, _DNIRRFC.DeliveryNoteInRows.Products.CustomsTariff,
                            _DNIRRFC.RegistryRowsFinancialControlling.RegistryHeader.F_DateVAT.Date Into g = Group
                     Select New With {Key .Customers = Customers,
                         Key .CustomTariff = CustomsTariff,
                         Key .DateExecution = [Date],
                         Key .Weight = g.Sum(Function(inv) inv.DeliveryNoteInRows.Weight),
                         Key .Amount = g.Sum(Function(inv) inv.RegistryRowsFinancialControlling.Amount)}

        RaiseEvent InitWork(1, 1, query5.Count)
        For Each _item In query5
            RaiseEvent DoWork()
            _VATReport08 = New VATReport08(_uow)
            _VATReport08.VATReport = _VATReport
            _VATReport08.DateExecution = _item.DateExecution
            _VATReport08.Customers = _item.Customers
            _VATReport08.CustomsTariffs = _item.CustomTariff
            _VATReport08.Weight = _item.Weight
            _VATReport08.AmountVATInBase = Math.Round(_item.Amount, 0, MidpointRounding.AwayFromZero)
        Next
        _uow.CommitChanges()
        'RaiseEvent FinalWork


        '// Most a 2M forgalmuak !!!

        '// Először azok, akiknek a számláján lévő ÁFA >= 2.000.000, de 2015 január 1. után 1.000.000
        Dim _CheckSumValue As Long
        If _VATReport.DateFrom.Date.Year > 2014 Then
            _CheckSumValue = 1000000
        Else
            _CheckSumValue = 2000000
        End If
        Dim _VATReportGroupCustomers2M As VATReportGroupCustomers2M
        Dim _VATReportDetail_XPQ As New XPQuery(Of VATReportDetail)(_uow)
        Dim query7 = From _VRDA In _VATReportDetail_XPQ
                   Where _VRDA.VATReport.Oid = _VATReport.Oid And
                   _VRDA.VAT.InversState = False
                   Group _VRDA By _VRDA.PartnerType, _VRDA.Customers, _VRDA.DocumentNumber, _VRDA.DateVAT.Date Into g = Group
                   Select New With {Key .PartnerType = PartnerType,
                                    Key .Customers = Customers,
                                    Key .DocumentNumber = DocumentNumber,
                                    Key .DateVAT = [Date],
                                    Key .AmountVATIn = g.Sum(Function(inv) inv.AmountVATIn),
                                    Key .AmountVATInBase = g.Sum(Function(inv) inv.AmountVATInBase),
                                    Key .AmountVATOut = g.Sum(Function(inv) inv.AmountVATOut),
                                    Key .AmountVATOutBase = g.Sum(Function(inv) inv.AmountVATOutBase)}

        For Each _item In query7
            If _item.AmountVATIn >= _CheckSumValue Then
                _VATReportGroupCustomers2M = New VATReportGroupCustomers2M(_uow)
                _VATReportGroupCustomers2M.VATReport = _VATReport
                _VATReportGroupCustomers2M.VAT2MType = eVAT2MType.eDetail
                _VATReportGroupCustomers2M.Customers = _item.Customers
                _VATReportGroupCustomers2M.AmountVATIn = _item.AmountVATIn
                _VATReportGroupCustomers2M.AmountVATInBase = _item.AmountVATInBase
                _VATReportGroupCustomers2M.DocumentNumber = _item.DocumentNumber
                _VATReportGroupCustomers2M.PartnerType = _item.PartnerType
                _VATReportGroupCustomers2M.DateExecution = _item.DateVAT
            End If
            If _item.AmountVATOut >= _CheckSumValue Then
                _VATReportGroupCustomers2M = New VATReportGroupCustomers2M(_uow)
                _VATReportGroupCustomers2M.VATReport = _VATReport
                _VATReportGroupCustomers2M.VAT2MType = eVAT2MType.eDetail
                _VATReportGroupCustomers2M.Customers = _item.Customers
                _VATReportGroupCustomers2M.AmountVATOut = _item.AmountVATOut
                _VATReportGroupCustomers2M.AmountVATOutBase = _item.AmountVATOutBase
                _VATReportGroupCustomers2M.DocumentNumber = _item.DocumentNumber
                _VATReportGroupCustomers2M.PartnerType = _item.PartnerType
                _VATReportGroupCustomers2M.DateExecution = _item.DateVAT
            End If
        Next
        _uow.CommitChanges()

        Dim _AmountVATIn_Detail As Double = 0
        Dim _AmountVATOut_Detail As Double = 0
        Dim _AmountVATInBase_Detail As Double = 0
        Dim _AmountVATOutBase_Detail As Double = 0
        Dim query8 = From _VRDAX In _VATReportDetail_XPQ
                   Where _VRDAX.VATReport.Oid = _VATReport.Oid And
                   _VRDAX.VAT.InversState = False
                   Group _VRDAX By _VRDAX.PartnerType, _VRDAX.Customers Into g = Group
                   Select New With {Key .PartnerType = PartnerType,
                                    Key .Customers = Customers,
                                    Key .AmountVATIn = g.Sum(Function(inv) inv.AmountVATIn),
                                    Key .AmountVATInBase = g.Sum(Function(inv) inv.AmountVATInBase),
                                    Key .AmountVATOut = g.Sum(Function(inv) inv.AmountVATOut),
                                    Key .AmountVATOutBase = g.Sum(Function(inv) inv.AmountVATOutBase)}
        For Each _item In query8

            If _item.AmountVATIn >= _CheckSumValue Or _item.AmountVATOut >= _CheckSumValue Then
                Dim _X As New XPCollection(Of VATReportGroupCustomers2M) _
                    (_uow, CriteriaOperator.Parse("VATReport.Oid=? and VAT2MType=0 and Customers.Oid=? and PartnerType=?", _
                                                  _VATReport.Oid, _item.Customers.Oid, _item.PartnerType))
                _AmountVATIn_Detail = 0
                _AmountVATOut_Detail = 0
                _AmountVATInBase_Detail = 0
                _AmountVATOutBase_Detail = 0
                For Each _Z In _X
                    _AmountVATIn_Detail += _Z.AmountVATIn
                    _AmountVATOut_Detail += _Z.AmountVATOut
                    _AmountVATInBase_Detail += _Z.AmountVATIn
                    _AmountVATOutBase_Detail += _Z.AmountVATOut
                Next

                If _item.AmountVATIn - _AmountVATIn_Detail > 0 Then
                    _VATReportGroupCustomers2M = New VATReportGroupCustomers2M(_uow)
                    _VATReportGroupCustomers2M.AmountVATIn = _item.AmountVATIn - _AmountVATIn_Detail
                    _VATReportGroupCustomers2M.AmountVATInBase = _item.AmountVATInBase - _AmountVATInBase_Detail
                    _VATReportGroupCustomers2M.VATReport = _VATReport
                    _VATReportGroupCustomers2M.Customers = _item.Customers
                    _VATReportGroupCustomers2M.PartnerType = _item.PartnerType
                    _VATReportGroupCustomers2M.VAT2MType = eVAT2MType.eCummulative
                End If
                If _item.AmountVATOut - _AmountVATOut_Detail > 0 Then
                    _VATReportGroupCustomers2M = New VATReportGroupCustomers2M(_uow)
                    _VATReportGroupCustomers2M.AmountVATOut = _item.AmountVATOut - _AmountVATOut_Detail
                    _VATReportGroupCustomers2M.AmountVATOutBase = _item.AmountVATOutBase - _AmountVATOutBase_Detail
                    _VATReportGroupCustomers2M.VATReport = _VATReport
                    _VATReportGroupCustomers2M.Customers = _item.Customers
                    _VATReportGroupCustomers2M.PartnerType = _item.PartnerType
                    _VATReportGroupCustomers2M.VAT2MType = eVAT2MType.eCummulative
                End If

            End If
        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork

        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub GenerateVATAvowalInfo(_uow As UnitOfWork, _VATReport As VATReport)
        Dim _PCIDetail_XPQuery As New XPQuery(Of PCIDetail)(_uow)


        'Dim query = From _StorageOutRowsInRows In _StorageOutRowsInRows_XPQuery
        '            Where _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.StorageMoveType.MoveType = eMoveType.InventoryTransfer And
        '                  _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.IsEditable = False And
        '                  String.IsNullOrEmpty(_StorageOutRowsInRows.StorageOutRows.StorageOutHeader.DocumentNumber) = False And
        '                  _StorageOutRowsInRows.StorageOutRows.StorageOutHeader IsNot Nothing
        '        Group _StorageOutRowsInRows By _StorageOutRowsInRows.StorageInRows.Oid Into g = Group
        '        Select New With {.StorageInRows = Oid,
        '                         .QTT_Sum = g.Sum(Function(inv) inv.QTT)}

    End Sub
    Private Sub aViewAttachments_VAT_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewAttachments_VAT.Execute
        Dim _VATReportDetail As VATReportDetail = TryCast(View.SelectedObjects(0), VATReportDetail)
        Dim _CustomersFrom As CustomersFrom = _VATReportDetail.VATReport.CustomersFrom
        Dim _Customers As Customers = _VATReportDetail.Customers
        Dim _DocumentNumber As String = _VATReportDetail.DocumentNumber

        ViewAttachments.ViewAttachments(View.ObjectSpace, Frame, _CustomersFrom, _Customers, _DocumentNumber, "")
    End Sub
    Private Sub aViewRegistry_VATNoPay_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewRegistry_VATNoPay.Execute
        Dim _VATReportNoPayDetail As VATReportNoPayDetail = TryCast(View.SelectedObjects(0), VATReportNoPayDetail)
        Dim _CustomersFrom As CustomersFrom = _VATReportNoPayDetail.VATReport.CustomersFrom
        Dim _Customers As Customers = _VATReportNoPayDetail.Customers
        Dim _DocumentNumber As String = _VATReportNoPayDetail.DocumentNumber
        ViewRegistry.ViewRegistry(View.ObjectSpace, Application, e, _CustomersFrom, _Customers, _DocumentNumber)
    End Sub
    Private Sub aViewRegistry_VAT_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewRegistry_VAT.Execute
        Dim _VATReportDetail As VATReportDetail = TryCast(View.SelectedObjects(0), VATReportDetail)
        Dim _CustomersFrom As CustomersFrom = _VATReportDetail.VATReport.CustomersFrom
        Dim _Customers As Customers = _VATReportDetail.Customers
        Dim _DocumentNumber As String = _VATReportDetail.DocumentNumber
        ViewRegistry.ViewRegistry(View.ObjectSpace, Application, e, _CustomersFrom, _Customers, _DocumentNumber)
    End Sub
    Private Sub aVATReportPrint_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aVATReportPrint.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ReportData As ReportData
        Dim _VATReport As VATReport = TryCast(View.SelectedObjects(0), VATReport)

        If _VATReport IsNot Nothing Then
            _ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Name=?", "ÁFA Riport"))

            If _ReportData IsNot Nothing Then
                Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("VATReport=?", _VATReport), True)
            Else
                Throw New UserFriendlyException("Nincs beállítva a ÁFA Riport nyomtatvány !")
            End If
        End If
    End Sub
    Private Sub aViewAttachments_VATNoPay_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewAttachments_VATNoPay.Execute
        Dim _VATReportNoPayDetail As VATReportNoPayDetail = TryCast(View.SelectedObjects(0), VATReportNoPayDetail)
        Dim _CustomersFrom As CustomersFrom = _VATReportNoPayDetail.VATReport.CustomersFrom
        Dim _Customers As Customers = _VATReportNoPayDetail.Customers
        Dim _DocumentNumber As String = _VATReportNoPayDetail.DocumentNumber
        ViewAttachments.ViewAttachments(View.ObjectSpace, Frame, _CustomersFrom, _Customers, _DocumentNumber, "")
    End Sub

    Private Sub aVATClose_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aVATClose.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _VATReport As VATReport = TryCast(View.SelectedObjects(0), VATReport)

        _VATReport.Closed = True
        _ocur.CommitChanges()

    End Sub

    Private Sub aVATCloseBack_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aVATCloseBack.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _VATReport As VATReport = TryCast(View.SelectedObjects(0), VATReport)

        _VATReport.Closed = False
        _ocur.CommitChanges()
    End Sub

    Private Sub aVATClosePreviewPDF_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aVATClosePreviewPDF.Execute

    End Sub

    Private Sub CheckVATAvowalType(_ocur As Xpo.XPObjectSpace, _FromDate As Date, _ToDate As Date)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)

        Dim _PCIDetail_Collection As New XPCollection(Of PCIDetail)(_uow, CriteriaOperator.Parse("GetDate(DateExecution)>=? and GetDate(DateExecution)<=?", _FromDate, _ToDate))
        Dim _PCIDetail As PCIDetail

        RaiseEvent InitWork(1, 1, _PCIDetail_Collection.Count)
        For Each _PCIDetail In _PCIDetail_Collection
            Dim _GLAccounts As GLAccounts
            Dim _GLRows As GLRows
            RaiseEvent DoWork()
            Dim _GLAccounts_Collection As New XPCollection(Of GLAccounts) _
                (_uow, CriteriaOperator.Parse("VATAvowalType=1 and CustomersFrom=? and Customers=? and PartnerType=? and  DocumentNumber=? and IsEditable=False",
                                              _PCIDetail.CustomersFrom, _PCIDetail.Customers, _PCIDetail.PartnerType, _PCIDetail.ConnectInfo))

            For Each _GLAccounts In _GLAccounts_Collection
                For Each _GLRows In _GLAccounts.GLRows
                    _GLRows.DateVAT = _PCIDetail.DateExecution
                    _uow.CommitChanges()
                Next
            Next
        Next


    End Sub

    Private Sub aGenerateVATCompare_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGenerateVATCompare.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _SQL As String = ""

        Dim _VATCompare As VATCompare = _uow.GetObjectByKey(Of VATCompare)(TryCast(View.SelectedObjects(0), VATCompare).Oid)
        Dim _VATReportDetail As VATReportDetail
        Dim _VATCompareDetail As VATCompareDetail
        RaiseEvent InitWork(1, 1, 1)
        _uow.Delete(_VATCompare.VATCompareDetail)
        _uow.CommitChanges()

        RaiseEvent InitWork(1, 1, _VATCompare.VATReportSource.VATReportDetail.Count)
        For Each _VATReportDetail In _VATCompare.VATReportSource.VATReportDetail

            RaiseEvent DoWork()
            _VATReportDetail = _uow.GetObjectByKey(Of VATReportDetail)(_VATReportDetail.Oid)

            _VATCompareDetail = New VATCompareDetail(_uow)
            _VATCompareDetail.VATCompare = _VATCompare
            _VATCompareDetail.VAT = _VATReportDetail.VAT
            _VATCompareDetail.PayMode = _VATReportDetail.PayMode
            _VATCompareDetail.PartnerType = _VATReportDetail.PartnerType
            _VATCompareDetail.Note = _VATReportDetail.Note
            _VATCompareDetail.DocumentNumber = _VATReportDetail.DocumentNumber
            _VATCompareDetail.DateVAT = _VATReportDetail.DateVAT
            _VATCompareDetail.DateExecution = _VATReportDetail.DateExecution
            _VATCompareDetail.Customers = _VATReportDetail.Customers
            _VATCompareDetail.AmountVATOutBase_Source = _VATReportDetail.AmountVATOutBase
            _VATCompareDetail.AmountVATOut_Source = _VATReportDetail.AmountVATOut
            _VATCompareDetail.AmountVATInBase_Source = _VATReportDetail.AmountVATInBase
            _VATCompareDetail.AmountVATIn_Source = _VATReportDetail.AmountVATIn

        Next
        _uow.CommitChanges()

        RaiseEvent InitWork(1, 1, _VATCompare.VATReportDestination.VATReportDetail.Count)
        For Each _VATReportDetail In _VATCompare.VATReportDestination.VATReportDetail

            RaiseEvent DoWork()
            _VATReportDetail = _uow.GetObjectByKey(Of VATReportDetail)(_VATReportDetail.Oid)

            _VATCompareDetail = _uow.FindObject(Of VATCompareDetail)(CriteriaOperator.Parse("Customers=? and DocumentNumber=? and PartnerType=? and VAT=? and DateVAT=?",
                                                                                            _VATReportDetail.Customers, _VATReportDetail.DocumentNumber,
                                                                                            _VATReportDetail.PartnerType, _VATReportDetail.VAT, _VATReportDetail.DateVAT))
            If _VATCompareDetail IsNot Nothing Then
            Else
                _VATCompareDetail = New VATCompareDetail(_uow)
                _VATCompareDetail.VATCompare = _VATCompare
                _VATCompareDetail.VAT = _VATReportDetail.VAT
                _VATCompareDetail.PayMode = _VATReportDetail.PayMode
                _VATCompareDetail.PartnerType = _VATReportDetail.PartnerType
                _VATCompareDetail.Note = _VATReportDetail.Note
                _VATCompareDetail.DocumentNumber = _VATReportDetail.DocumentNumber
                _VATCompareDetail.DateVAT = _VATReportDetail.DateVAT
                _VATCompareDetail.DateExecution = _VATReportDetail.DateExecution
                _VATCompareDetail.Customers = _VATReportDetail.Customers
            End If

            
            _VATCompareDetail.AmountVATOutBase_Destination = _VATReportDetail.AmountVATOutBase
            _VATCompareDetail.AmountVATOut_Destination = _VATReportDetail.AmountVATOut
            _VATCompareDetail.AmountVATInBase_Destination = _VATReportDetail.AmountVATInBase
            _VATCompareDetail.AmountVATIn_Destination = _VATReportDetail.AmountVATIn

        Next

        _uow.CommitChanges()
        RaiseEvent FinalWork

        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub

    Private Sub aViewAttachments_VATCompareDetail_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aViewAttachments_VATCompareDetail.Execute
        Dim _VATCompareDetail As VATCompareDetail = TryCast(View.SelectedObjects(0), VATCompareDetail)
        Dim _CustomersFrom As CustomersFrom = _VATCompareDetail.VATCompare.CustomersFrom
        Dim _Customers As Customers = _VATCompareDetail.Customers
        Dim _DocumentNumber As String = _VATCompareDetail.DocumentNumber

        ViewAttachments.ViewAttachments(View.ObjectSpace, Frame, _CustomersFrom, _Customers, _DocumentNumber, "")
    End Sub
End Class
