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.ExpressApp.Utils
Imports System.Xml
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports System.IO
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.XtraSplashScreen

Structure VATReportNAVCodes_Structure
    Public VATCode As String
    Public VATValue As Double
End Structure

Public Class VATReportVC
    Inherits SISBusiness.Module.VATReportVC
    Private _WaitFormClass As WaitFormClass
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

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

        _WaitFormClass = New WaitFormClass
        AddHandler DoWork, AddressOf _WaitFormClass.DoWork
        AddHandler InitWork, AddressOf _WaitFormClass.Initwork
        AddHandler FinalWork, AddressOf _WaitFormClass.FinalWork
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        If View.Id = "VATReportCheckList_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
        End If

        RemoveHandler DoWork, AddressOf _WaitFormClass.DoWork
        RemoveHandler InitWork, AddressOf _WaitFormClass.Initwork
        RemoveHandler FinalWork, AddressOf _WaitFormClass.FinalWork
    End Sub

    Private Sub aVATReport_Create65XML_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aVATReport_Create65XML.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _VATReport As VATReport = TryCast(View.SelectedObjects(0), VATReport)
        Dim _SaveFileDialog As New System.Windows.Forms.SaveFileDialog()
        Dim _FileName65 As String = ""
        Dim _VATNumberError As String = ""
        Dim _NAVProgramPath As String = ""

        '// Ellenőrzések -----------------------
        For Each _VATReportGroupCustomers2M As VATReportGroupCustomers2M In _VATReport.VATReportGroupCustomers2M
            If String.IsNullOrEmpty(_VATReportGroupCustomers2M.Customers.VATNumber) Then
                If String.IsNullOrEmpty(_VATNumberError) Then
                    _VATNumberError = "Hiányzó adószám(ok)!" + vbNewLine
                    _VATNumberError += "2 millió ÁFA érték feletti partnerek lap:"
                    _VATNumberError += _VATReportGroupCustomers2M.Customers.FullName
                Else
                    _VATNumberError += _VATReportGroupCustomers2M.Customers.FullName
                End If
            End If
        Next

        For Each _VATReport07 As VATReport07 In _VATReport.VATReport07
            If String.IsNullOrEmpty(_VATReport07.Customers.VATNumber) Then
                If String.IsNullOrEmpty(_VATNumberError) Then
                    _VATNumberError = "Hiányzó adószám(ok)!" + vbNewLine
                    _VATNumberError += "07 melléklet lap:" + vbNewLine
                    _VATNumberError += _VATReport07.Customers.FullName + vbNewLine
                Else
                    If Not _VATNumberError.Contains("07 melléklet lap:") Then
                        _VATNumberError += "07 melléklet lap:" + vbNewLine
                        _VATNumberError += _VATReport07.Customers.FullName + vbNewLine
                    Else
                        _VATNumberError += _VATReport07.Customers.FullName + vbNewLine
                    End If
                End If
            End If
        Next

        For Each _VATReport08 As VATReport08 In _VATReport.VATReport08
            If String.IsNullOrEmpty(_VATReport08.Customers.VATNumber) Then
                If String.IsNullOrEmpty(_VATNumberError) Then
                    _VATNumberError = "Hiányzó adószám(ok)!" + vbNewLine
                    _VATNumberError += "08 melléklet lap:" + vbNewLine
                    _VATNumberError += _VATReport08.Customers.FullName + vbNewLine
                Else
                    If Not _VATNumberError.Contains("07 melléklet lap:") Then
                        _VATNumberError += "08 melléklet lap:" + vbNewLine
                        _VATNumberError += _VATReport08.Customers.FullName + vbNewLine
                    Else
                        _VATNumberError += _VATReport08.Customers.FullName + vbNewLine
                    End If
                End If
            End If
        Next

        If Not String.IsNullOrEmpty(_VATNumberError) Then Throw New Exception(_VATNumberError)

        If TryCast(Application.Model.Options, IModelOptionsExtender) IsNot Nothing Then
            If File.Exists(TryCast(Application.Model.Options, IModelOptionsExtender).NAVProgramPath) Then
                _NAVProgramPath = TryCast(Application.Model.Options, IModelOptionsExtender).NAVProgramPath
            Else
            End If
        Else
        End If

        If _NAVProgramPath <> "" Then
            _FileName65 = String.Format("{0}{1}.xml", Path.GetTempPath, _VATReport.ShortName)
            Me.Create1465XML(_ocur, _FileName65, _VATReport)
            Dim _ShellCommand As String = String.Format("{0} ""cmd:open.xml {1}""", _NAVProgramPath, _FileName65)
            Shell(_ShellCommand, AppWinStyle.NormalFocus)
        Else
            _SaveFileDialog.Filter = "XML files (*.xml)|*.xml"
            _SaveFileDialog.FileName = _VATReport.ShortName
            _SaveFileDialog.DefaultExt = "xml"
            _SaveFileDialog.FilterIndex = 2
            _SaveFileDialog.RestoreDirectory = True

            If _SaveFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                _FileName65 = _SaveFileDialog.FileName

                Me.Create65XML(_ocur, _FileName65, _VATReport)

                MsgBox(String.Format("Az XML file ""{0}"" névvel a következő helyen került elmentésre:" + vbNewLine + "{1}", System.IO.Path.GetFileName(_SaveFileDialog.FileName), IO.Path.GetFullPath(_SaveFileDialog.FileName)), _
                                                MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Exportálás befejeződött!")
            End If
        End If
    End Sub
    Private Sub aVATReport_Check65XML_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aVATReport_Check65XML.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _VATReport As VATReport = TryCast(View.SelectedObjects(0), VATReport)
        Dim _NAVProgramPath As String = ""

        If TryCast(Application.Model.Options, IModelOptionsExtender) IsNot Nothing Then
            If File.Exists(TryCast(Application.Model.Options, IModelOptionsExtender).NAVProgramPath) Then
                _NAVProgramPath = TryCast(Application.Model.Options, IModelOptionsExtender).NAVProgramPath
            End If
        End If

        If _NAVProgramPath <> "" Then
            Dim _FileName65 As String = Path.GetTempPath & _VATReport.ShortName & ".xml"
            Dim _FileName65Result As String = Path.GetTempPath & _VATReport.ShortName & ".txt"
            If IO.File.Exists(_FileName65) Then IO.File.Delete(_FileName65)
            If IO.File.Exists(_FileName65Result) Then IO.File.Delete(_FileName65Result)

            Me.Create65XML(_ocur, _FileName65, _VATReport)
            Dim _Process As New System.Diagnostics.Process

            With _Process.StartInfo
                .FileName = _NAVProgramPath
                .Arguments = " ""cmd:check.xml.silent " + _FileName65 + ";" + _FileName65Result + """"
                .WindowStyle = ProcessWindowStyle.Hidden
            End With

            _Process.Start()
            _Process.WaitForExit()

            If IO.File.Exists(_FileName65Result) Then
                Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
                Dim _VATReportCheckList_CollectionSource As New CollectionSource(_onew, GetType(VATReportCheckList))
                Dim _StreamReader As StreamReader = My.Computer.FileSystem.OpenTextFileReader(_FileName65Result, System.Text.Encoding.Default)
                Dim _ActLine As String

                Do
                    _ActLine = _StreamReader.ReadLine
                    If _ActLine IsNot Nothing Then
                        Dim _Bytes() As Byte = Encoding.Default.GetBytes(_ActLine)
                        _ActLine = Encoding.Default.GetString(_Bytes)

                        If _ActLine.Contains("Hiba : ") Or _ActLine.Contains("Figyelmeztetések:") Then
                            Dim _VATReportCheckList As New VATReportCheckList(_onew.Session)
                            With _VATReportCheckList
                                .Description = _ActLine
                                If _ActLine.Contains("Hiba : ") Then
                                    .VATReportCheckListType = eVATReportCheckListType.eError
                                Else
                                    .VATReportCheckListType = eVATReportCheckListType.eWarning
                                End If

                            End With

                            _VATReportCheckList_CollectionSource.Add(_VATReportCheckList)
                        End If
                    End If
                Loop Until _ActLine Is Nothing

                _StreamReader.Close()

                Dim _ShowViewParameters As New ShowViewParameters
                Dim _ShowViewSource As New ShowViewSource(Frame, Nothing)

                _ShowViewParameters.CreatedView = Application.CreateListView("VATReportCheckList_ListView", _VATReportCheckList_CollectionSource, True)
                _ShowViewParameters.NewWindowTarget = NewWindowTarget.Separate
                _ShowViewParameters.TargetWindow = TargetWindow.NewModalWindow
                Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)

            End If
        End If
    End Sub

#Region "Subs"
    Private Sub UploadField(_XmlTextWriter As XmlTextWriter, _FieldID As String, _ValueString As String)
        _XmlTextWriter.WriteStartElement("mezo") '
        _XmlTextWriter.WriteAttributeString("eazon", _FieldID)
        _XmlTextWriter.WriteString(_ValueString)
        _XmlTextWriter.WriteEndElement()
    End Sub
    Private Sub Create65XML(_ocur As Xpo.XPObjectSpace, _FileName65 As String, _VATReport As VATReport)
        Dim _XmlTextWriter As New XmlTextWriter(_FileName65, System.Text.Encoding.UTF8)
        _XmlTextWriter.WriteStartDocument()
        _XmlTextWriter.Formatting = Formatting.Indented
        _XmlTextWriter.WriteStartElement("nyomtatvanyok") '
        _XmlTextWriter.WriteAttributeString("xmlns", "http://www.apeh.hu/abev/nyomtatvanyok/2005/01")
        _XmlTextWriter.WriteStartElement("abev")
        _XmlTextWriter.WriteStartElement("hibakszama")
        _XmlTextWriter.WriteString("0")
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("hash")
        _XmlTextWriter.WriteString("058e24f2acc722eae72d2fc8706c0c233bf1a4aa")
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("programverzio")
        _XmlTextWriter.WriteString("v.2.65.0")
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("nyomtatvany")
        _XmlTextWriter.WriteStartElement("nyomtatvanyinformacio")
        _XmlTextWriter.WriteStartElement("nyomtatvanyazonosito")


        _XmlTextWriter.WriteString(Mid(_VATReport.DateFrom.Year.ToString, 3, 2) & "65A") 'Évente változhat !!!!!!
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("nyomtatvanyverzio")
        _XmlTextWriter.WriteString("3.0")
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("adozo")
        _XmlTextWriter.WriteStartElement("nev")
        _XmlTextWriter.WriteString(_VATReport.CustomersFrom.FullName)
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("adoszam")
        _XmlTextWriter.WriteString(_VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("idoszak")
        _XmlTextWriter.WriteStartElement("tol")
        _XmlTextWriter.WriteString(_VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("ig")
        _XmlTextWriter.WriteString(_VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("megjegyzes")
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("mezok")
        UploadField(_XmlTextWriter, "0A0001E001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0A0001E005A", _VATReport.CustomersFrom.FullName)
        UploadField(_XmlTextWriter, "0A0001E006A", _VATReport.CustomersFrom.AdministratorName)
        UploadField(_XmlTextWriter, "0A0001E007A", _VATReport.CustomersFrom.AdministratorPhoneNumber)

        'UploadField(_XmlTextWriter, "0A0001F004A", "O")
        UploadField(_XmlTextWriter, "0A0001F006A", "H")

        ' UploadField(_XmlTextWriter, "0A0001F015A", "1")
        Dim _Page7Number As Long = _VATReport.VATReport07.Count / 36
        If _VATReport.VATReport07.Count Mod 36 > 0 Then
            _Page7Number += 1
        End If
        'If _Page7Number <> 0 Then UploadField(_XmlTextWriter, "0A0001F017A", _Page7Number.ToString) '7. oldal darabszáma

        Dim _Page8Number As Long = _VATReport.VATReport08.Count / 36
        If _VATReport.VATReport08.Count Mod 36 > 0 Then
            _Page8Number += 1
        End If

        'If _Page8Number <> 0 Then UploadField(_XmlTextWriter, "0A0001F018A", _Page8Number.ToString)

        Dim _VATReportGroupCustomers2M_Collection As New ArrayList

        For Each _VATReportGroupCustomers2M As VATReportGroupCustomers2M In _VATReport.VATReportGroupCustomers2M
            If _VATReportGroupCustomers2M_Collection.Count = 0 Then
                _VATReportGroupCustomers2M_Collection.Add(_VATReportGroupCustomers2M.Customers)
            Else
                Dim _Need As Boolean = True
                For Each _Customers As Customers In _VATReportGroupCustomers2M_Collection
                    If _Customers.Oid = _VATReportGroupCustomers2M.Customers.Oid Then
                        _Need = False
                        Exit For
                    End If
                Next
                If _Need Then
                    _VATReportGroupCustomers2M_Collection.Add(_VATReportGroupCustomers2M.Customers)
                End If
            End If
        Next

        If _VATReportGroupCustomers2M_Collection.Count > 0 Then UploadField(_XmlTextWriter, "0A0001F021A", _VATReportGroupCustomers2M_Collection.Count.ToString) ' Számolni!

        UploadField(_XmlTextWriter, "0A0001F001A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0A0001F002A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        If _VATReport.CustomersFrom.DefaultCustomerBankAccounts IsNot Nothing Then
            Dim _AccountNumber As String() = _VATReport.CustomersFrom.DefaultCustomerBankAccounts.AccountNumber.Split(New Char() {"-"c}, StringSplitOptions.RemoveEmptyEntries)
            UploadField(_XmlTextWriter, "0A0001G001A", _AccountNumber(0))
            UploadField(_XmlTextWriter, "0A0001G002A", _AccountNumber(1) + _AccountNumber(2))
            UploadField(_XmlTextWriter, "0A0001G003A", _VATReport.CustomersFrom.DefaultCustomerBankAccounts.ShortName)
        End If

        'UploadField(_XmlTextWriter, "0A0001G017A", "A")

        UploadField(_XmlTextWriter, "0A0001I001A", _VATReport.CustomersFrom.Address1.City.ToString)
        UploadField(_XmlTextWriter, "0A0001I002A", _VATReport.DateVAT.Year.ToString + _VATReport.DateVAT.Month.ToString("00") + _VATReport.DateVAT.Day.ToString("00"))

        '01-01 lap
        _XmlTextWriter.WriteComment("01-01 lap")
        UploadField(_XmlTextWriter, "0B0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0B0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0B0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0B0001B005A", _VATReport.CustomersFrom.FullName)

        Dim _VATReportGroup_5_Collection As New XPCollection(Of VATReportGroup)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND VAT.KeyValuePercent=5 AND PartnerType=0", _VATReport))
        Dim _VATReportGroup_18_Collection As New XPCollection(Of VATReportGroup)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND VAT.KeyValuePercent=18 AND PartnerType=0", _VATReport))
        Dim _VATReportGroup_27_Collection As New XPCollection(Of VATReportGroup)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND VAT.KeyValuePercent=27 AND PartnerType=0", _VATReport))
        Dim _VATReportGroup_FAD_Collection As New XPCollection(Of VATReportGroup)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND VAT.KeyValuePercent=0 AND PartnerType=1 AND VAT.InversState=True", _VATReport))

        Dim _SumAmountVATOutBase As Long = 0
        Dim _SumAmountVATOut As Long = 0
        Dim _SumAmountVATInBase As Long = 0
        Dim _SumAmountVATIn As Long = 0

        If _VATReportGroup_5_Collection.Count > 0 Then
            For Each _VATReportGroup As VATReportGroup In _VATReportGroup_5_Collection
                _SumAmountVATOut += _VATReportGroup.AmountVATOut
                _SumAmountVATOutBase += _VATReportGroup.AmountVATOutBase
            Next
            UploadField(_XmlTextWriter, "0B0001C0005BA", Math.Round(_SumAmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero))
            UploadField(_XmlTextWriter, "0B0001C0005CA", Math.Round(_SumAmountVATOut / 1000, 0, MidpointRounding.AwayFromZero))
        End If

        _SumAmountVATOutBase = 0
        _SumAmountVATOut = 0

        If _VATReportGroup_18_Collection.Count > 0 Then
            For Each _VATReportGroup As VATReportGroup In _VATReportGroup_18_Collection
                _SumAmountVATOut += _VATReportGroup.AmountVATOut
                _SumAmountVATOutBase += _VATReportGroup.AmountVATOutBase
            Next
            UploadField(_XmlTextWriter, "0B0001C0006BA", Math.Round(_SumAmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero))
            UploadField(_XmlTextWriter, "0B0001C0006CA", Math.Round(_SumAmountVATOut / 1000, 0, MidpointRounding.AwayFromZero))
        End If

        _SumAmountVATOutBase = 0
        _SumAmountVATOut = 0

        If _VATReportGroup_27_Collection.Count > 0 Then
            For Each _VATReportGroup As VATReportGroup In _VATReportGroup_27_Collection
                _SumAmountVATOut += _VATReportGroup.AmountVATOut
                _SumAmountVATOutBase += _VATReportGroup.AmountVATOutBase
            Next
            UploadField(_XmlTextWriter, "0B0001C0007BA", Math.Round(_SumAmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero))
            UploadField(_XmlTextWriter, "0B0001C0007CA", Math.Round(_SumAmountVATOut / 1000, 0, MidpointRounding.AwayFromZero))
        End If

        _SumAmountVATOutBase = 0
        _SumAmountVATOut = 0

        If _VATReportGroup_FAD_Collection.Count > 0 Then
            For Each _VATReportGroup As VATReportGroup In _VATReportGroup_FAD_Collection
                _SumAmountVATIn += _VATReportGroup.AmountVATIn
                _SumAmountVATInBase += _VATReportGroup.AmountVATInBase
            Next
            UploadField(_XmlTextWriter, "0B0001C0029BA", Math.Round(_SumAmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))
            UploadField(_XmlTextWriter, "0B0001C0029CA", Math.Round(_SumAmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))
        End If

        _SumAmountVATIn = 0
        _SumAmountVATInBase = 0

        '01-02 lap
        _XmlTextWriter.WriteComment("01-02 lap")
        UploadField(_XmlTextWriter, "0C0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0C0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0C0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0C0001B005A", _VATReport.CustomersFrom.FullName)

        Dim _VATReportGroup_5_Payabels_Collection As New XPCollection(Of VATReportGroup)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND (VAT.KeyValuePercent=5 OR VAT.InversKeyValue=0.05) AND PartnerType=1 AND VAT.ClaimState=True", _VATReport))
        Dim _VATReportGroup_18_Payabels_Collection As New XPCollection(Of VATReportGroup)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND (VAT.KeyValuePercent=18 OR VAT.InversKeyValue=0.18) AND PartnerType=1 AND VAT.ClaimState=True", _VATReport))
        Dim _VATReportGroup_27_Payabels_Collection As New XPCollection(Of VATReportGroup)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND (VAT.KeyValuePercent=27 OR VAT.InversKeyValue=0.27) AND PartnerType=1 AND VAT.ClaimState=True", _VATReport))
        Dim _VATReportGroup_0_Payabels_Collection As New XPCollection(Of VATReportGroup)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND VAT.KeyValuePercent=0 AND PartnerType=1 AND VAT.ClaimState=True and VAT.InversState<>True", _VATReport))
        Dim _VATReportGroup_12_Payabels_Collection As New XPCollection(Of VATReportGroup)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND VAT.KeyValuePercent=12 AND PartnerType=1 AND VAT.ClaimState=True and VAT.InversState<>True and VAT.ShortName = 'Komp 12%'", _VATReport))

        Dim _VATReportGroup_12_Payabels_DB_Collection As New XPCollection(Of VATReportDetail)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND VAT.KeyValuePercent=12 AND PartnerType=1 AND VAT.ClaimState=True and VAT.InversState<>True and VAT.ShortName = 'Komp 12%'", _VATReport))

        If _VATReportGroup_5_Payabels_Collection.Count > 0 Then
            For Each _VATReportGroup As VATReportGroup In _VATReportGroup_5_Payabels_Collection
                _SumAmountVATIn += _VATReportGroup.AmountVATIn
                _SumAmountVATInBase += _VATReportGroup.AmountVATInBase
            Next
            UploadField(_XmlTextWriter, "0C0001C0064BA", Math.Round(_SumAmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))
            UploadField(_XmlTextWriter, "0C0001C0064CA", Math.Round(_SumAmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))
        End If

        _SumAmountVATInBase = 0
        _SumAmountVATIn = 0

        If _VATReportGroup_18_Payabels_Collection.Count > 0 Then
            For Each _VATReportGroup As VATReportGroup In _VATReportGroup_18_Payabels_Collection
                _SumAmountVATIn += _VATReportGroup.AmountVATIn
                _SumAmountVATInBase += _VATReportGroup.AmountVATInBase
            Next
            UploadField(_XmlTextWriter, "0C0001C0065BA", Math.Round(_SumAmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))
            UploadField(_XmlTextWriter, "0C0001C0065CA", Math.Round(_SumAmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))
        End If

        _SumAmountVATInBase = 0
        _SumAmountVATIn = 0

        If _VATReportGroup_27_Payabels_Collection.Count > 0 Then
            For Each _VATReportGroup As VATReportGroup In _VATReportGroup_27_Payabels_Collection
                _SumAmountVATIn += _VATReportGroup.AmountVATIn
                _SumAmountVATInBase += _VATReportGroup.AmountVATInBase
            Next
            UploadField(_XmlTextWriter, "0C0001C0066BA", Math.Round(_SumAmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))
            UploadField(_XmlTextWriter, "0C0001C0066CA", Math.Round(_SumAmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))
        End If

        _SumAmountVATInBase = 0
        _SumAmountVATIn = 0

        If _VATReportGroup_0_Payabels_Collection.Count > 0 Then
            For Each _VATReportGroup As VATReportGroup In _VATReportGroup_0_Payabels_Collection
                _SumAmountVATIn += _VATReportGroup.AmountVATIn
                _SumAmountVATInBase += _VATReportGroup.AmountVATInBase
            Next
            UploadField(_XmlTextWriter, "0C0001C0063BA", Math.Round(_SumAmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))
        End If

        _SumAmountVATInBase = 0
        _SumAmountVATIn = 0

        If _VATReportGroup_12_Payabels_Collection.Count > 0 Then
            For Each _VATReportGroup As VATReportGroup In _VATReportGroup_12_Payabels_Collection
                _SumAmountVATIn += _VATReportGroup.AmountVATIn
                _SumAmountVATInBase += _VATReportGroup.AmountVATInBase
            Next

            UploadField(_XmlTextWriter, "0D0001C0073AA", _VATReportGroup_12_Payabels_DB_Collection.Count)
            UploadField(_XmlTextWriter, "0D0001C0073BA", Math.Round(_SumAmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))
            UploadField(_XmlTextWriter, "0D0001C0073CA", Math.Round(_SumAmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))
        End If

        _SumAmountVATInBase = 0
        _SumAmountVATIn = 0
        '01-03 lap
        _XmlTextWriter.WriteComment("01-03 lap")
        UploadField(_XmlTextWriter, "0D0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0D0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0D0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0D0001B005A", _VATReport.CustomersFrom.FullName)

        '01-04 lap
        _XmlTextWriter.WriteComment("01-04 lap")
        UploadField(_XmlTextWriter, "0E0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0E0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0E0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0E0001B005A", _VATReport.CustomersFrom.FullName)

        '01-05 lap
        _XmlTextWriter.WriteComment("01-05 lap")
        UploadField(_XmlTextWriter, "0F0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0F0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0F0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0F0001B005A", _VATReport.CustomersFrom.FullName)

        '02 lap
        _XmlTextWriter.WriteComment("2 lap")
        UploadField(_XmlTextWriter, "0H0001B001A", "1")
        UploadField(_XmlTextWriter, "0H0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0H0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0H0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0H0001B006A", _VATReport.CustomersFrom.FullName)

        'UploadField(_XmlTextWriter, "0H0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", "")) '_VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        ' UploadField(_XmlTextWriter, "0H0001B002A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        'UploadField(_XmlTextWriter, "0H0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00")) '"1")
        'UploadField(_XmlTextWriter, "0H0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00")) '_VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        'UploadField(_XmlTextWriter, "0H0001B006A", _VATReport.CustomersFrom.FullName)

        '03 lap
        _XmlTextWriter.WriteComment("3 lap")
        UploadField(_XmlTextWriter, "0I0001B001A", "1")
        UploadField(_XmlTextWriter, "0I0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0I0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0I0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0I0001B006A", _VATReport.CustomersFrom.FullName)

        ''04 lap
        '_XmlTextWriter.WriteComment("4 lap")
        'UploadField(_XmlTextWriter, "0J0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        'UploadField(_XmlTextWriter, "0J0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        'UploadField(_XmlTextWriter, "0J0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        'UploadField(_XmlTextWriter, "0J0001B005A", _VATReport.CustomersFrom.FullName)

        ''05 lap
        '_XmlTextWriter.WriteComment("5 lap")
        'UploadField(_XmlTextWriter, "0K0001B001A", "1")
        'UploadField(_XmlTextWriter, "0K0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        'UploadField(_XmlTextWriter, "0K0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        'UploadField(_XmlTextWriter, "0K0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        'UploadField(_XmlTextWriter, "0K0001B006A", _VATReport.CustomersFrom.FullName)

        'UploadField(_XmlTextWriter, "0K0001F001A", _VATReport.CustomersFrom.Address1.City.ToString)
        'UploadField(_XmlTextWriter, "0K0001F002A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + "20")



        Dim _PageIndex As Long = 1
        Dim _PageFullAmountLocal As Long = 0
        Dim _NeedPageHeader As Boolean = True
        Dim _RowIndexPage7Local As Long = 1
        Dim _SumPagePageFullAmount As Long = 0

        '07 lap
        If _VATReport.VATReport07.Count > 0 Then

            _VATReport.VATReport07.Sorting.Add(New SortProperty("Customers.VATNumber", DB.SortingDirection.Ascending))
            _VATReport.VATReport07.Sorting.Add(New SortProperty("GetDate(DateExecution)", DB.SortingDirection.Ascending))
            _VATReport.VATReport07.Sorting.Add(New SortProperty("CustomsTariffs.NAVCode", DB.SortingDirection.Ascending))

            For Each _VATReport07 As VATReport07 In _VATReport.VATReport07
                If _NeedPageHeader Then
                    _XmlTextWriter.WriteComment("7 lap/" + _PageIndex.ToString)
                    UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "B001A", _PageIndex.ToString)
                    UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                    UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                    UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                    UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "B006A", _VATReport.CustomersFrom.FullName)
                    _NeedPageHeader = False
                End If

                UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "AA", _VATReport07.Customers.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "BA", _VATReport07.DateExecution.Year.ToString() + _VATReport07.DateExecution.Month.ToString("00") + _VATReport07.DateExecution.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "CA", _VATReport07.CustomsTariffs.NAVDescription)
                UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "DA", _VATReport07.CustomsTariffs.NAVCode)
                UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "EA", Math.Round(_VATReport07.Weight, 0, MidpointRounding.AwayFromZero).ToString)
                UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "FA", Math.Round(_VATReport07.AmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero))

                _PageFullAmountLocal += Math.Round(_VATReport07.AmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero)
                _SumPagePageFullAmount += Math.Round(_VATReport07.AmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero)

                If _RowIndexPage7Local Mod 36 = 0 Then
                    UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "C0037FA", _PageFullAmountLocal.ToString())
                    _PageIndex += 1
                    _RowIndexPage7Local = 0
                    _PageFullAmountLocal = 0
                    _NeedPageHeader = True
                End If

                _RowIndexPage7Local += 1
            Next
            UploadField(_XmlTextWriter, "0J" + _PageIndex.ToString("0000") + "C0037FA", _PageFullAmountLocal.ToString()) 'Utolsó oldal összesen
            UploadField(_XmlTextWriter, "0B0001C0004BA", _SumPagePageFullAmount.ToString()) '4. sor 1365A
            UploadField(_XmlTextWriter, "0F0001C0100BA", _SumPagePageFullAmount.ToString()) '100. sor 1365A

        End If

        '08 lap
        _PageIndex = 1
        _PageFullAmountLocal = 0
        _NeedPageHeader = True
        Dim _RowIndexPage8Local As Long = 1
        _SumPagePageFullAmount = 0

        If _VATReport.VATReport08.Count > 0 Then

            _VATReport.VATReport08.Sorting.Add(New SortProperty("Customers.VATNumber", DB.SortingDirection.Ascending))
            _VATReport.VATReport08.Sorting.Add(New SortProperty("GetDate(DateExecution)", DB.SortingDirection.Ascending))
            _VATReport.VATReport08.Sorting.Add(New SortProperty("CustomsTariffs.NAVCode", DB.SortingDirection.Ascending))

            For Each _VATReport08 As VATReport08 In _VATReport.VATReport08
                If _NeedPageHeader Then
                    _XmlTextWriter.WriteComment("8 lap/" + _PageIndex.ToString)
                    UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "B001A", "1")
                    UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                    UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                    UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                    UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "B006A", _VATReport.CustomersFrom.FullName)
                    _NeedPageHeader = False
                End If

                UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "AA", _VATReport08.Customers.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "BA", _VATReport08.DateExecution.Year.ToString() + _VATReport08.DateExecution.Month.ToString("00") + _VATReport08.DateExecution.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "CA", _VATReport08.CustomsTariffs.NAVDescription)
                UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "DA", _VATReport08.CustomsTariffs.NAVCode)
                UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "EA", Math.Round(_VATReport08.Weight, 0, MidpointRounding.AwayFromZero).ToString)
                UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "FA", Math.Round(_VATReport08.AmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))

                _PageFullAmountLocal += Math.Round(_VATReport08.AmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero)
                _SumPagePageFullAmount += Math.Round(_VATReport08.AmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero)


                If _RowIndexPage8Local Mod 36 = 0 Then
                    UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "C0037FA", _PageFullAmountLocal.ToString())
                    _PageIndex += 1
                    _RowIndexPage8Local = 0
                    _PageFullAmountLocal = 0
                    _NeedPageHeader = True
                End If

                _RowIndexPage8Local += 1
            Next

            UploadField(_XmlTextWriter, "0K" + _PageIndex.ToString("0000") + "C0037FA", _PageFullAmountLocal.ToString())
            UploadField(_XmlTextWriter, "0F0001C0101BA", _SumPagePageFullAmount.ToString()) '101. sor 1365A
            Dim _FAD_VAT As VAT = _ocur.FindObject(Of VAT)(CriteriaOperator.Parse("ShortName='FAD' AND InversState=True AND Active=True"))

            If _FAD_VAT IsNot Nothing Then
                UploadField(_XmlTextWriter, "0F0001C0101CA", Math.Round(_SumPagePageFullAmount * _FAD_VAT.InversKeyValue, 0, MidpointRounding.AwayFromZero).ToString())
            End If


            _XmlTextWriter.WriteEndElement()
            _XmlTextWriter.WriteEndElement()
        End If
        '65M nyomtatványok generálása
        If _VATReportGroupCustomers2M_Collection.Count > 0 Then
            For Each _Customers As Customers In _VATReportGroupCustomers2M_Collection
                _XmlTextWriter.WriteStartElement("nyomtatvany")
                _XmlTextWriter.WriteStartElement("nyomtatvanyinformacio")
                _XmlTextWriter.WriteStartElement("nyomtatvanyazonosito")
                _XmlTextWriter.WriteString(Mid(_VATReport.DateFrom.Year.ToString, 3, 2) & "65M") 'Évente változhat
                _XmlTextWriter.WriteEndElement() 'nyomtatvanyazonosito
                _XmlTextWriter.WriteStartElement("nyomtatvanyverzio")
                _XmlTextWriter.WriteString("3.0")
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteStartElement("adozo")
                _XmlTextWriter.WriteStartElement("nev")
                _XmlTextWriter.WriteString(_VATReport.CustomersFrom.FullName)
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteStartElement("adoszam")
                _XmlTextWriter.WriteString(_VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteStartElement("albizonylatazonositas")
                _XmlTextWriter.WriteStartElement("megnevezes")
                _XmlTextWriter.WriteString(_Customers.FullName)
                _XmlTextWriter.WriteEndElement() 'megnevezes
                _XmlTextWriter.WriteStartElement("azonosito")
                _XmlTextWriter.WriteString(_Customers.VATNumber.Replace("-", ""))
                _XmlTextWriter.WriteEndElement() 'azonosito
                _XmlTextWriter.WriteEndElement() 'albizonylatazonositas
                _XmlTextWriter.WriteStartElement("idoszak")
                _XmlTextWriter.WriteStartElement("tol")
                _XmlTextWriter.WriteString(_VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteStartElement("ig")
                _XmlTextWriter.WriteString(_VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteStartElement("megjegyzes")
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteEndElement() 'nyomtatvanyinformacio

                _XmlTextWriter.WriteStartElement("mezok")
                UploadField(_XmlTextWriter, "0A0001C001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0A0001C005A", _VATReport.CustomersFrom.FullName)
                If Not String.IsNullOrEmpty(_Customers.VATNumber) Then
                    UploadField(_XmlTextWriter, "0A0001C006A", _Customers.VATNumber.Remove(8, _Customers.VATNumber.Length - 8))
                End If
                UploadField(_XmlTextWriter, "0A0001C008A", _Customers.FullName)
                UploadField(_XmlTextWriter, "0A0001D001A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0A0001D002A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))

                '01 lap
                _XmlTextWriter.WriteComment("01 lap")
                UploadField(_XmlTextWriter, "0B0001B001A", "1")
                UploadField(_XmlTextWriter, "0B0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0B0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0B0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0B0001B006A", _VATReport.CustomersFrom.FullName)
                If Not String.IsNullOrEmpty(_Customers.VATNumber) Then
                    UploadField(_XmlTextWriter, "0B0001B007A", _Customers.VATNumber.Remove(8, _Customers.VATNumber.Length - 8))
                End If
                UploadField(_XmlTextWriter, "0B0001B009A", _Customers.FullName)

                '01-K lap
                _XmlTextWriter.WriteComment("01-K lap")
                UploadField(_XmlTextWriter, "0C0001B001A", "1")
                UploadField(_XmlTextWriter, "0C0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0C0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0C0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0C0001B006A", _VATReport.CustomersFrom.FullName)
                If Not String.IsNullOrEmpty(_Customers.VATNumber) Then
                    UploadField(_XmlTextWriter, "0C0001B007A", _Customers.VATNumber.Remove(8, _Customers.VATNumber.Length - 8))
                End If
                UploadField(_XmlTextWriter, "0C0001B009A", _Customers.FullName)

                '02 lap
                _XmlTextWriter.WriteComment("02 lap")
                UploadField(_XmlTextWriter, "0D0001B001A", "1")
                UploadField(_XmlTextWriter, "0D0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0D0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0D0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0D0001B006A", _VATReport.CustomersFrom.FullName)
                If Not String.IsNullOrEmpty(_Customers.VATNumber) Then
                    UploadField(_XmlTextWriter, "0D0001B007A", _Customers.VATNumber.Remove(8, _Customers.VATNumber.Length - 8))
                End If
                UploadField(_XmlTextWriter, "0D0001B009A", _Customers.FullName)

                '02-K lap
                _XmlTextWriter.WriteComment("02-K lap")
                UploadField(_XmlTextWriter, "0E0001B001A", "1")
                UploadField(_XmlTextWriter, "0E0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0E0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0E0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0E0001B006A", _VATReport.CustomersFrom.FullName)
                If Not String.IsNullOrEmpty(_Customers.VATNumber) Then
                    UploadField(_XmlTextWriter, "0E0001B007A", _Customers.VATNumber.Remove(8, _Customers.VATNumber.Length - 8))
                End If
                UploadField(_XmlTextWriter, "0E0001B009A", _Customers.FullName)

                Dim _VATReportGroupCustomers2M_Customers_Collection As New XPCollection(Of VATReportGroupCustomers2M)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND Customers=?", _VATReport, _Customers))

                Dim _FullAmountVATIn As Long = 0
                Dim _FullAmountVATInBase As Long = 0
                Dim _Full02C As Long = 0
                Dim _Full02D As Long = 0
                Dim _Full0A67DA As Long = 0
                Dim _RowIndex As Long = 1
                For Each _VATReportGroupCustomers2M As VATReportGroupCustomers2M In _VATReportGroupCustomers2M_Customers_Collection
                    If Not String.IsNullOrEmpty(_VATReportGroupCustomers2M.DocumentNumber) Then
                        If _VATReportGroupCustomers2M.PartnerType = ePartnerType.ePayabels Then
                            UploadField(_XmlTextWriter, "0D0001C" + _RowIndex.ToString("0000") + "AA", _VATReportGroupCustomers2M.DocumentNumber)
                            UploadField(_XmlTextWriter, "0D0001C" + _RowIndex.ToString("0000") + "BA", _VATReportGroupCustomers2M.DateExecution.Year.ToString + _VATReportGroupCustomers2M.DateExecution.Month.ToString("00") + _VATReportGroupCustomers2M.DateExecution.Day.ToString("00"))
                            UploadField(_XmlTextWriter, "0D0001C" + _RowIndex.ToString("0000") + "CA", Math.Round(_VATReportGroupCustomers2M.AmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))
                            UploadField(_XmlTextWriter, "0D0001C" + _RowIndex.ToString("0000") + "DA", Math.Round(_VATReportGroupCustomers2M.AmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))

                            _Full02C += Math.Round(_VATReportGroupCustomers2M.AmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero)
                            _Full02D += Math.Round(_VATReportGroupCustomers2M.AmountVATIn / 1000, 0, MidpointRounding.AwayFromZero)
                        End If
                        If _VATReportGroupCustomers2M.PartnerType = ePartnerType.eReceivables Then
                            UploadField(_XmlTextWriter, "0B0001C" + _RowIndex.ToString("0000") + "AA", _VATReportGroupCustomers2M.DocumentNumber)
                            UploadField(_XmlTextWriter, "0B0001C" + _RowIndex.ToString("0000") + "BA", _VATReportGroupCustomers2M.DateExecution.Year.ToString + _VATReportGroupCustomers2M.DateExecution.Month.ToString("00") + _VATReportGroupCustomers2M.DateExecution.Day.ToString("00"))
                            UploadField(_XmlTextWriter, "0B0001C" + _RowIndex.ToString("0000") + "CA", Math.Round(_VATReportGroupCustomers2M.AmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero))
                            UploadField(_XmlTextWriter, "0B0001C" + _RowIndex.ToString("0000") + "DA", Math.Round(_VATReportGroupCustomers2M.AmountVATOut / 1000, 0, MidpointRounding.AwayFromZero))

                            _Full02C += Math.Round(_VATReportGroupCustomers2M.AmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero)
                            _Full02D += Math.Round(_VATReportGroupCustomers2M.AmountVATOut / 1000, 0, MidpointRounding.AwayFromZero)
                        End If
                        _RowIndex += 1
                    Else
                        If _VATReportGroupCustomers2M.PartnerType = ePartnerType.ePayabels Then
                            _Full0A67DA += Math.Round(_VATReportGroupCustomers2M.AmountVATIn / 1000, 0, MidpointRounding.AwayFromZero)
                        End If
                        'If _VATReportGroupCustomers2M.PartnerType = ePartnerType.eReceivables Then
                        '    _Full0A67DA += Math.Round(_VATReportGroupCustomers2M.AmountVATOut / 1000, 0, MidpointRounding.AwayFromZero)
                        'End If
                    End If

                    _FullAmountVATIn += _VATReportGroupCustomers2M.AmountVATIn
                    _FullAmountVATInBase += _VATReportGroupCustomers2M.AmountVATInBase

                Next
                If _Full02C > 0 And _Full02D > 0 Then
                    UploadField(_XmlTextWriter, "0B0001C0037CA", _Full02C.ToString)
                    UploadField(_XmlTextWriter, "0B0001C0037DA", _Full02D.ToString)

                    UploadField(_XmlTextWriter, "0A0001E0004BA", (_RowIndex - 1).ToString)
                    UploadField(_XmlTextWriter, "0A0001E0004CA", _Full02C.ToString)
                    UploadField(_XmlTextWriter, "0A0001E0004DA", _Full02D.ToString)

                    UploadField(_XmlTextWriter, "0A0001E0007BA", (_RowIndex - 1).ToString)
                    UploadField(_XmlTextWriter, "0A0001E0007CA", _Full02C.ToString)
                    '  UploadField(_XmlTextWriter, "0A0001E0007DA", _Full02D.ToString)
                End If
                If _Full0A67DA > 0 Then
                    UploadField(_XmlTextWriter, "0A0001E0006DA", _Full0A67DA.ToString)
                    ' UploadField(_XmlTextWriter, "0A0001E0007DA", _Full0A67DA.ToString)
                End If

                UploadField(_XmlTextWriter, "0A0001E0007DA", (_Full02D + _Full0A67DA).ToString)
                'UploadField(_XmlTextWriter, "0A0001E0004CA", Math.Round(_FullAmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))
                'UploadField(_XmlTextWriter, "0A0001E0007CA", Math.Round(_FullAmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))

                'UploadField(_XmlTextWriter, "0A0001E0004DA", Math.Round(_FullAmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))
                'UploadField(_XmlTextWriter, "0A0001E0007DA", Math.Round(_FullAmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))

                _XmlTextWriter.WriteEndElement() 'mezők
                _XmlTextWriter.WriteEndElement() 'nyomtatvany
            Next
        End If
        _XmlTextWriter.WriteEndElement() 'Nyomtatványok

        _XmlTextWriter.Flush()
        _XmlTextWriter.Close()
    End Sub
    Private Sub Create1465XML(_ocur As Xpo.XPObjectSpace, _FileName65 As String, _VATReport As VATReport)

        On Error Resume Next

        Dim _XmlTextWriter As New XmlTextWriter(_FileName65, System.Text.Encoding.UTF8)
        _XmlTextWriter.WriteStartDocument()
        _XmlTextWriter.Formatting = Formatting.Indented
        _XmlTextWriter.WriteStartElement("nyomtatvanyok") '
        _XmlTextWriter.WriteAttributeString("xmlns", "http://www.apeh.hu/abev/nyomtatvanyok/2005/01")
        _XmlTextWriter.WriteStartElement("abev")
        _XmlTextWriter.WriteStartElement("hibakszama")
        _XmlTextWriter.WriteString("0")
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("hash")
        _XmlTextWriter.WriteString("96b82602300312ce6edd8ef0c784ef4419ba4186")
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("programverzio")
        _XmlTextWriter.WriteString("v.2.50.0")
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("nyomtatvany")
        _XmlTextWriter.WriteStartElement("nyomtatvanyinformacio")
        _XmlTextWriter.WriteStartElement("nyomtatvanyazonosito")
        _XmlTextWriter.WriteString(Mid(_VATReport.DateFrom.Year.ToString, 3, 2) & "65A") 'Évente változhat !!!!!!
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("nyomtatvanyverzio")
        _XmlTextWriter.WriteString("3.0")
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("adozo")
        _XmlTextWriter.WriteStartElement("nev")
        _XmlTextWriter.WriteString(_VATReport.CustomersFrom.FullName)
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("adoszam")
        _XmlTextWriter.WriteString(_VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("idoszak")
        _XmlTextWriter.WriteStartElement("tol")
        _XmlTextWriter.WriteString(_VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("ig")
        _XmlTextWriter.WriteString(_VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("megjegyzes")
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteEndElement()
        _XmlTextWriter.WriteStartElement("mezok")
        UploadField(_XmlTextWriter, "0A0001E001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0A0001E008A", _VATReport.CustomersFrom.FullName)
        ' UploadField(_XmlTextWriter, "0A0001E009A", "X")
        UploadField(_XmlTextWriter, "0A0001E011A", _VATReport.CustomersFrom.Address1.ZipPostal)
        UploadField(_XmlTextWriter, "0A0001E012A", _VATReport.CustomersFrom.Address1.City)
        UploadField(_XmlTextWriter, "0A0001E013A", _VATReport.CustomersFrom.Address1.Street)
        UploadField(_XmlTextWriter, "0A0001E014A", _VATReport.CustomersFrom.PublicLandAddress1)

        If _VATReport.CustomersFrom.Address2 IsNot Nothing Then
            '    UploadField(_XmlTextWriter, "0A0001E020A", "X")
            UploadField(_XmlTextWriter, "0A0001E022A", _VATReport.CustomersFrom.Address2.ZipPostal)
            UploadField(_XmlTextWriter, "0A0001E023A", _VATReport.CustomersFrom.Address2.City)
            UploadField(_XmlTextWriter, "0A0001E024A", _VATReport.CustomersFrom.Address2.Street)
            UploadField(_XmlTextWriter, "0A0001E025A", _VATReport.CustomersFrom.PublicLandAddress2)
        Else
            '   UploadField(_XmlTextWriter, "0A0001E020A", "X")
            UploadField(_XmlTextWriter, "0A0001E022A", _VATReport.CustomersFrom.Address1.ZipPostal)
            UploadField(_XmlTextWriter, "0A0001E023A", _VATReport.CustomersFrom.Address1.City)
            UploadField(_XmlTextWriter, "0A0001E024A", _VATReport.CustomersFrom.Address1.Street)
            UploadField(_XmlTextWriter, "0A0001E025A", _VATReport.CustomersFrom.PublicLandAddress2)
        End If

        UploadField(_XmlTextWriter, "0A0001E031A", _VATReport.CustomersFrom.AdministratorName)
        UploadField(_XmlTextWriter, "0A0001E032A", _VATReport.CustomersFrom.AdministratorPhoneNumber)

        'UploadField(_XmlTextWriter, "0A0001F004A", "O")
        UploadField(_XmlTextWriter, "0A0001F005A", "H")

        ' UploadField(_XmlTextWriter, "0A0001F015A", "1")
        Dim _Page7Number As Long = _VATReport.VATReport07.Count / 36
        If _VATReport.VATReport07.Count Mod 36 > 0 Then
            _Page7Number += 1
        End If
        If _Page7Number <> 0 Then UploadField(_XmlTextWriter, "0A0001F017A", _Page7Number.ToString) '7. oldal darabszáma

        Dim _Page8Number As Long = _VATReport.VATReport08.Count / 36
        If _VATReport.VATReport08.Count Mod 36 > 0 Then
            _Page8Number += 1
        End If

        If _Page8Number <> 0 Then UploadField(_XmlTextWriter, "0A0001F018A", _Page8Number.ToString)

        Dim _VATReportGroupCustomers2M_Collection As New ArrayList

        For Each _VATReportGroupCustomers2M As VATReportGroupCustomers2M In _VATReport.VATReportGroupCustomers2M
            If _VATReportGroupCustomers2M_Collection.Count = 0 Then
                _VATReportGroupCustomers2M_Collection.Add(_VATReportGroupCustomers2M.Customers)
            Else
                Dim _Need As Boolean = True
                For Each _Customers As Customers In _VATReportGroupCustomers2M_Collection
                    If _Customers.Oid = _VATReportGroupCustomers2M.Customers.Oid Then
                        _Need = False
                        Exit For
                    End If
                Next
                If _Need Then
                    _VATReportGroupCustomers2M_Collection.Add(_VATReportGroupCustomers2M.Customers)
                End If
            End If
        Next

        If _VATReportGroupCustomers2M_Collection.Count > 0 Then UploadField(_XmlTextWriter, "0A0001F021A", _VATReportGroupCustomers2M_Collection.Count.ToString) ' Számolni!

        UploadField(_XmlTextWriter, "0A0001F001A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0A0001F002A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        If _VATReport.CustomersFrom.DefaultCustomerBankAccounts IsNot Nothing Then
            Dim _AccountNumber As String() = _VATReport.CustomersFrom.DefaultCustomerBankAccounts.AccountNumber.Split(New Char() {"-"c}, StringSplitOptions.RemoveEmptyEntries)
            UploadField(_XmlTextWriter, "0A0001G001A", _AccountNumber(0))
            UploadField(_XmlTextWriter, "0A0001G002A", _AccountNumber(1) + _AccountNumber(2))
            UploadField(_XmlTextWriter, "0A0001G003A", _VATReport.CustomersFrom.DefaultCustomerBankAccounts.ShortName)
        End If

        'UploadField(_XmlTextWriter, "0A0001G017A", "A")

        UploadField(_XmlTextWriter, "0A0001I001A", _VATReport.CustomersFrom.Address1.City.ToString)
        UploadField(_XmlTextWriter, "0A0001I002A", _VATReport.DateVAT.Year.ToString + _VATReport.DateVAT.Month.ToString("00") + _VATReport.DateVAT.Day.ToString("00"))

        '01-01 lap
        _XmlTextWriter.WriteComment("01-01 lap")
        UploadField(_XmlTextWriter, "0B0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0B0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0B0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0B0001B005A", _VATReport.CustomersFrom.FullName)

        Dim _VATReportNAVCodes_Structure_Collection As New ArrayList
        Dim _VATReportNAVCodes As VATReportNAVCodes_Structure
        Dim _VAT_Collection As New XPCollection(Of VAT)(_ocur.Session)
        Dim _Exists As Boolean = False
        For Each _VAT As VAT In _VAT_Collection
            If Not String.IsNullOrEmpty(_VAT.XML65AFieldIDBaseValueIn) Then
                _Exists = False
                For Each _VATReportNAVCodes In _VATReportNAVCodes_Structure_Collection
                    If _VATReportNAVCodes.VATCode = _VAT.XML65AFieldIDBaseValueIn Then
                        _Exists = True
                        Exit For
                    End If
                Next
                If _Exists = False Then

                    With _VATReportNAVCodes
                        .VATCode = _VAT.XML65AFieldIDBaseValueIn
                        .VATValue = 0
                    End With
                    _VATReportNAVCodes_Structure_Collection.Add(_VATReportNAVCodes)
                End If

            End If

            If Not String.IsNullOrEmpty(_VAT.XML65AFieldIDBaseValueInSum) Then
                _Exists = False
                For Each _VATReportNAVCodes In _VATReportNAVCodes_Structure_Collection
                    If _VATReportNAVCodes.VATCode = _VAT.XML65AFieldIDBaseValueInSum Then
                        _Exists = True
                        Exit For
                    End If
                Next

                If _Exists = False Then

                    With _VATReportNAVCodes
                        .VATCode = _VAT.XML65AFieldIDBaseValueInSum
                        .VATValue = 0
                    End With
                    _VATReportNAVCodes_Structure_Collection.Add(_VATReportNAVCodes)
                End If
            End If


            If Not String.IsNullOrEmpty(_VAT.XML65AFieldIDBaseValueOut) Then
                _Exists = False
                For Each _VATReportNAVCodes In _VATReportNAVCodes_Structure_Collection
                    If _VATReportNAVCodes.VATCode = _VAT.XML65AFieldIDBaseValueOut Then
                        _Exists = True
                        Exit For
                    End If
                Next
                If _Exists = False Then

                    With _VATReportNAVCodes
                        .VATCode = _VAT.XML65AFieldIDBaseValueOut
                        .VATValue = 0
                    End With
                    _VATReportNAVCodes_Structure_Collection.Add(_VATReportNAVCodes)
                End If
            End If

            If Not String.IsNullOrEmpty(_VAT.XML65AFieldIDBaseValueOutSum) Then
                _Exists = False
                For Each _VATReportNAVCodes In _VATReportNAVCodes_Structure_Collection
                    If _VATReportNAVCodes.VATCode = _VAT.XML65AFieldIDBaseValueOutSum Then
                        _Exists = True
                        Exit For
                    End If
                Next
                If _Exists = False Then

                    With _VATReportNAVCodes
                        .VATCode = _VAT.XML65AFieldIDBaseValueOutSum
                        .VATValue = 0
                    End With
                    _VATReportNAVCodes_Structure_Collection.Add(_VATReportNAVCodes)
                End If
            End If

            If Not String.IsNullOrEmpty(_VAT.XML65AFieldIDValueIn) Then
                _Exists = False
                For Each _VATReportNAVCodes In _VATReportNAVCodes_Structure_Collection
                    If _VATReportNAVCodes.VATCode = _VAT.XML65AFieldIDValueIn Then
                        _Exists = True
                        Exit For
                    End If
                Next
                If _Exists = False Then

                    With _VATReportNAVCodes
                        .VATCode = _VAT.XML65AFieldIDValueIn
                        .VATValue = 0
                    End With
                    _VATReportNAVCodes_Structure_Collection.Add(_VATReportNAVCodes)
                End If
            End If

            If Not String.IsNullOrEmpty(_VAT.XML65AFieldIDValueInSum) Then
                _Exists = False
                For Each _VATReportNAVCodes In _VATReportNAVCodes_Structure_Collection
                    If _VATReportNAVCodes.VATCode = _VAT.XML65AFieldIDValueInSum Then
                        _Exists = True
                        Exit For
                    End If
                Next

                If _Exists = False Then

                    With _VATReportNAVCodes
                        .VATCode = _VAT.XML65AFieldIDValueInSum
                        .VATValue = 0
                    End With
                    _VATReportNAVCodes_Structure_Collection.Add(_VATReportNAVCodes)
                End If
            End If

            If Not String.IsNullOrEmpty(_VAT.XML65AFieldIDValueOut) Then
                _Exists = False
                For Each _VATReportNAVCodes In _VATReportNAVCodes_Structure_Collection
                    If _VATReportNAVCodes.VATCode = _VAT.XML65AFieldIDValueOut Then
                        _Exists = True
                        Exit For
                    End If
                Next
                If _Exists = False Then

                    With _VATReportNAVCodes
                        .VATCode = _VAT.XML65AFieldIDValueOut
                        .VATValue = 0
                    End With
                    _VATReportNAVCodes_Structure_Collection.Add(_VATReportNAVCodes)
                End If
            End If

            If Not String.IsNullOrEmpty(_VAT.XML65AFieldIDValueOutSum) Then
                _Exists = False
                For Each _VATReportNAVCodes In _VATReportNAVCodes_Structure_Collection
                    If _VATReportNAVCodes.VATCode = _VAT.XML65AFieldIDValueOutSum Then
                        _Exists = True
                        Exit For
                    End If
                Next
                If _Exists = False Then

                    With _VATReportNAVCodes
                        .VATCode = _VAT.XML65AFieldIDValueOutSum
                        .VATValue = 0
                    End With
                    _VATReportNAVCodes_Structure_Collection.Add(_VATReportNAVCodes)
                End If
            End If
        Next

        For Each _VATReportGroup As VATReportGroup In _VATReport.VATReportGroup
            For _ik = 0 To _VATReportNAVCodes_Structure_Collection.Count - 1
                Dim _VATReportNAVCodes_Structure As VATReportNAVCodes_Structure = _
                    CType(_VATReportNAVCodes_Structure_Collection(_ik), VATReportNAVCodes_Structure)

                If _VATReportNAVCodes_Structure.VATCode = _VATReportGroup.VAT.XML65AFieldIDBaseValueIn Then
                    _VATReportNAVCodes_Structure.VATValue += _VATReportGroup.AmountVATInBase

                    _VATReportNAVCodes_Structure_Collection(_ik) = _VATReportNAVCodes_Structure
                End If
            Next


            For _ik = 0 To _VATReportNAVCodes_Structure_Collection.Count - 1
                Dim _VATReportNAVCodes_Structure As VATReportNAVCodes_Structure = _
                    CType(_VATReportNAVCodes_Structure_Collection(_ik), VATReportNAVCodes_Structure)

                If _VATReportNAVCodes_Structure.VATCode = _VATReportGroup.VAT.XML65AFieldIDBaseValueOut Then
                    _VATReportNAVCodes_Structure.VATValue += _VATReportGroup.AmountVATOutBase

                    _VATReportNAVCodes_Structure_Collection(_ik) = _VATReportNAVCodes_Structure
                End If
            Next

            For _ik = 0 To _VATReportNAVCodes_Structure_Collection.Count - 1
                Dim _VATReportNAVCodes_Structure As VATReportNAVCodes_Structure = _
                    CType(_VATReportNAVCodes_Structure_Collection(_ik), VATReportNAVCodes_Structure)

                If _VATReportNAVCodes_Structure.VATCode = _VATReportGroup.VAT.XML65AFieldIDValueIn Then
                    _VATReportNAVCodes_Structure.VATValue += _VATReportGroup.AmountVATIn

                    _VATReportNAVCodes_Structure_Collection(_ik) = _VATReportNAVCodes_Structure
                End If
            Next

            For _ik = 0 To _VATReportNAVCodes_Structure_Collection.Count - 1
                Dim _VATReportNAVCodes_Structure As VATReportNAVCodes_Structure = _
                    CType(_VATReportNAVCodes_Structure_Collection(_ik), VATReportNAVCodes_Structure)

                If _VATReportNAVCodes_Structure.VATCode = _VATReportGroup.VAT.XML65AFieldIDValueOut Then
                    _VATReportNAVCodes_Structure.VATValue += _VATReportGroup.AmountVATOut

                    _VATReportNAVCodes_Structure_Collection(_ik) = _VATReportNAVCodes_Structure
                End If
            Next

            For _ik = 0 To _VATReportNAVCodes_Structure_Collection.Count - 1
                Dim _VATReportNAVCodes_Structure As VATReportNAVCodes_Structure = _
                    CType(_VATReportNAVCodes_Structure_Collection(_ik), VATReportNAVCodes_Structure)

                If _VATReportNAVCodes_Structure.VATCode = _VATReportGroup.VAT.XML65AFieldIDBaseValueInSum Then
                    _VATReportNAVCodes_Structure.VATValue += _VATReportGroup.AmountVATInBase

                    _VATReportNAVCodes_Structure_Collection(_ik) = _VATReportNAVCodes_Structure
                End If
            Next

            For _ik = 0 To _VATReportNAVCodes_Structure_Collection.Count - 1
                Dim _VATReportNAVCodes_Structure As VATReportNAVCodes_Structure = _
                    CType(_VATReportNAVCodes_Structure_Collection(_ik), VATReportNAVCodes_Structure)

                If _VATReportNAVCodes_Structure.VATCode = _VATReportGroup.VAT.XML65AFieldIDBaseValueOutSum Then
                    _VATReportNAVCodes_Structure.VATValue += _VATReportGroup.AmountVATOutBase

                    _VATReportNAVCodes_Structure_Collection(_ik) = _VATReportNAVCodes_Structure
                End If
            Next

            For _ik = 0 To _VATReportNAVCodes_Structure_Collection.Count - 1
                Dim _VATReportNAVCodes_Structure As VATReportNAVCodes_Structure = _
                    CType(_VATReportNAVCodes_Structure_Collection(_ik), VATReportNAVCodes_Structure)

                If _VATReportNAVCodes_Structure.VATCode = _VATReportGroup.VAT.XML65AFieldIDValueInSum Then
                    _VATReportNAVCodes_Structure.VATValue += _VATReportGroup.AmountVATIn

                    _VATReportNAVCodes_Structure_Collection(_ik) = _VATReportNAVCodes_Structure
                End If
            Next

            For _ik = 0 To _VATReportNAVCodes_Structure_Collection.Count - 1
                Dim _VATReportNAVCodes_Structure As VATReportNAVCodes_Structure = _
                    CType(_VATReportNAVCodes_Structure_Collection(_ik), VATReportNAVCodes_Structure)

                If _VATReportNAVCodes_Structure.VATCode = _VATReportGroup.VAT.XML65AFieldIDValueOutSum Then
                    _VATReportNAVCodes_Structure.VATValue += _VATReportGroup.AmountVATOut

                    _VATReportNAVCodes_Structure_Collection(_ik) = _VATReportNAVCodes_Structure
                End If
            Next
        Next

        For Each _VATReportNAVCodes In _VATReportNAVCodes_Structure_Collection
            UploadField(_XmlTextWriter, _VATReportNAVCodes.VATCode, CType(_VATReportNAVCodes.VATValue / 1000, Long))
        Next


        '01-02 lap
        _XmlTextWriter.WriteComment("01-02 lap")
        UploadField(_XmlTextWriter, "0C0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0C0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0C0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0C0001B005A", _VATReport.CustomersFrom.FullName)

        '01-03 lap
        _XmlTextWriter.WriteComment("01-03 lap")
        UploadField(_XmlTextWriter, "0D0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0D0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0D0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0D0001B005A", _VATReport.CustomersFrom.FullName)

        '01-04 lap
        _XmlTextWriter.WriteComment("01-04 lap")
        UploadField(_XmlTextWriter, "0E0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0E0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0E0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0E0001B005A", _VATReport.CustomersFrom.FullName)

        '01-05 lap
        _XmlTextWriter.WriteComment("01-05 lap")
        UploadField(_XmlTextWriter, "0F0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0F0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0F0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0F0001B005A", _VATReport.CustomersFrom.FullName)

        '02 lap
        _XmlTextWriter.WriteComment("2 lap")
        UploadField(_XmlTextWriter, "0H0001B001A", "1")
        UploadField(_XmlTextWriter, "0H0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0H0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0H0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0H0001B006A", _VATReport.CustomersFrom.FullName)

        'UploadField(_XmlTextWriter, "0H0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", "")) '_VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        ' UploadField(_XmlTextWriter, "0H0001B002A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        'UploadField(_XmlTextWriter, "0H0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00")) '"1")
        'UploadField(_XmlTextWriter, "0H0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00")) '_VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        'UploadField(_XmlTextWriter, "0H0001B006A", _VATReport.CustomersFrom.FullName)

        '03 lap
        _XmlTextWriter.WriteComment("3 lap")
        UploadField(_XmlTextWriter, "0I0001B001A", "1")
        UploadField(_XmlTextWriter, "0I0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0I0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0I0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0I0001B006A", _VATReport.CustomersFrom.FullName)

        '04 lap
        _XmlTextWriter.WriteComment("4 lap")
        UploadField(_XmlTextWriter, "0J0001B001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0J0001B003A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0J0001B004A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0J0001B005A", _VATReport.CustomersFrom.FullName)

        '05 lap
        _XmlTextWriter.WriteComment("5 lap")
        UploadField(_XmlTextWriter, "0K0001B001A", "1")
        UploadField(_XmlTextWriter, "0K0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
        UploadField(_XmlTextWriter, "0K0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0K0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
        UploadField(_XmlTextWriter, "0K0001B006A", _VATReport.CustomersFrom.FullName)

        UploadField(_XmlTextWriter, "0K0001F001A", _VATReport.CustomersFrom.Address1.City.ToString)
        UploadField(_XmlTextWriter, "0K0001F002A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + "20")



        Dim _PageIndex As Long = 1
        Dim _PageFullAmountLocal As Long = 0
        Dim _NeedPageHeader As Boolean = True
        Dim _RowIndexPage7Local As Long = 1
        Dim _SumPagePageFullAmount As Long = 0

        '07 lap
        If _VATReport.VATReport07.Count > 0 Then

            _VATReport.VATReport07.Sorting.Add(New SortProperty("Customers.VATNumber", DB.SortingDirection.Ascending))
            _VATReport.VATReport07.Sorting.Add(New SortProperty("GetDate(DateExecution)", DB.SortingDirection.Ascending))
            _VATReport.VATReport07.Sorting.Add(New SortProperty("CustomsTariffs.NAVCode", DB.SortingDirection.Ascending))

            For Each _VATReport07 As VATReport07 In _VATReport.VATReport07
                If _NeedPageHeader Then
                    _XmlTextWriter.WriteComment("7 lap/" + _PageIndex.ToString)
                    UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "B001A", _PageIndex.ToString)
                    UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                    UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                    UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                    UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "B006A", _VATReport.CustomersFrom.FullName)
                    _NeedPageHeader = False
                End If

                UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "AA", _VATReport07.Customers.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "BA", _VATReport07.DateExecution.Year.ToString() + _VATReport07.DateExecution.Month.ToString("00") + _VATReport07.DateExecution.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "CA", _VATReport07.CustomsTariffs.NAVDescription)
                UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "DA", _VATReport07.CustomsTariffs.NAVCode)
                UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "EA", Math.Round(_VATReport07.Weight, 0, MidpointRounding.AwayFromZero).ToString)
                UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "C" + _RowIndexPage7Local.ToString("0000") + "FA", Math.Round(_VATReport07.AmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero))

                _PageFullAmountLocal += Math.Round(_VATReport07.AmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero)
                _SumPagePageFullAmount += Math.Round(_VATReport07.AmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero)

                If _RowIndexPage7Local Mod 36 = 0 Then
                    UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "C0037FA", _PageFullAmountLocal.ToString())
                    _PageIndex += 1
                    _RowIndexPage7Local = 0
                    _PageFullAmountLocal = 0
                    _NeedPageHeader = True
                End If

                _RowIndexPage7Local += 1
            Next
            UploadField(_XmlTextWriter, "0L" + _PageIndex.ToString("0000") + "C0037FA", _PageFullAmountLocal.ToString()) 'Utolsó oldal összesen
            UploadField(_XmlTextWriter, "0B0001C0004BA", _SumPagePageFullAmount.ToString()) '4. sor 1365A
            UploadField(_XmlTextWriter, "0E0001E0100BA", _SumPagePageFullAmount.ToString()) '100. sor 1365A

        End If

        '08 lap
        _PageIndex = 1
        _PageFullAmountLocal = 0
        _NeedPageHeader = True
        Dim _RowIndexPage8Local As Long = 1
        _SumPagePageFullAmount = 0

        If _VATReport.VATReport08.Count > 0 Then

            _VATReport.VATReport08.Sorting.Add(New SortProperty("Customers.VATNumber", DB.SortingDirection.Ascending))
            _VATReport.VATReport08.Sorting.Add(New SortProperty("GetDate(DateExecution)", DB.SortingDirection.Ascending))
            _VATReport.VATReport08.Sorting.Add(New SortProperty("CustomsTariffs.NAVCode", DB.SortingDirection.Ascending))

            For Each _VATReport08 As VATReport08 In _VATReport.VATReport08
                If _NeedPageHeader Then
                    _XmlTextWriter.WriteComment("8 lap/" + _PageIndex.ToString)
                    UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "B001A", "1")
                    UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                    UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                    UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                    UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "B006A", _VATReport.CustomersFrom.FullName)
                    _NeedPageHeader = False
                End If

                UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "AA", _VATReport08.Customers.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "BA", _VATReport08.DateExecution.Year.ToString() + _VATReport08.DateExecution.Month.ToString("00") + _VATReport08.DateExecution.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "CA", _VATReport08.CustomsTariffs.NAVDescription)
                UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "DA", _VATReport08.CustomsTariffs.NAVCode)
                UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "EA", Math.Round(_VATReport08.Weight, 0, MidpointRounding.AwayFromZero).ToString)
                UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "C" + _RowIndexPage8Local.ToString("0000") + "FA", Math.Round(_VATReport08.AmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))

                _PageFullAmountLocal += Math.Round(_VATReport08.AmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero)
                _SumPagePageFullAmount += Math.Round(_VATReport08.AmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero)


                If _RowIndexPage8Local Mod 36 = 0 Then
                    UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "C0037FA", _PageFullAmountLocal.ToString())
                    _PageIndex += 1
                    _RowIndexPage8Local = 0
                    _PageFullAmountLocal = 0
                    _NeedPageHeader = True
                End If

                _RowIndexPage8Local += 1
            Next

            UploadField(_XmlTextWriter, "0M" + _PageIndex.ToString("0000") + "C0037FA", _PageFullAmountLocal.ToString())
            UploadField(_XmlTextWriter, "0E0001E0101BA", _SumPagePageFullAmount.ToString()) '101. sor 1365A
            Dim _FAD_VAT As VAT = _ocur.FindObject(Of VAT)(CriteriaOperator.Parse("ShortName='FAD' AND InversState=True AND Active=True"))

            If _FAD_VAT IsNot Nothing Then
                UploadField(_XmlTextWriter, "0E0001E0101CA", Math.Round(_SumPagePageFullAmount * _FAD_VAT.InversKeyValue, 0, MidpointRounding.AwayFromZero).ToString())
            End If


            _XmlTextWriter.WriteEndElement()
            _XmlTextWriter.WriteEndElement()
        End If
        '65M nyomtatványok generálása
        If _VATReportGroupCustomers2M_Collection.Count > 0 Then
            For Each _Customers As Customers In _VATReportGroupCustomers2M_Collection
                _XmlTextWriter.WriteStartElement("nyomtatvany")
                _XmlTextWriter.WriteStartElement("nyomtatvanyinformacio")
                _XmlTextWriter.WriteStartElement("nyomtatvanyazonosito")
                _XmlTextWriter.WriteString(Mid(_VATReport.DateFrom.Year.ToString, 3, 2) & "65M") 'Évente változhat
                _XmlTextWriter.WriteEndElement() 'nyomtatvanyazonosito
                _XmlTextWriter.WriteStartElement("nyomtatvanyverzio")
                _XmlTextWriter.WriteString("3.0")
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteStartElement("adozo")
                _XmlTextWriter.WriteStartElement("nev")
                _XmlTextWriter.WriteString(_VATReport.CustomersFrom.FullName)
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteStartElement("adoszam")
                _XmlTextWriter.WriteString(_VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteStartElement("albizonylatazonositas")
                _XmlTextWriter.WriteStartElement("megnevezes")
                _XmlTextWriter.WriteString(_Customers.FullName)
                _XmlTextWriter.WriteEndElement() 'megnevezes
                _XmlTextWriter.WriteStartElement("azonosito")
                _XmlTextWriter.WriteString(_Customers.VATNumber.Replace("-", ""))
                _XmlTextWriter.WriteEndElement() 'azonosito
                _XmlTextWriter.WriteEndElement() 'albizonylatazonositas
                _XmlTextWriter.WriteStartElement("idoszak")
                _XmlTextWriter.WriteStartElement("tol")
                _XmlTextWriter.WriteString(_VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteStartElement("ig")
                _XmlTextWriter.WriteString(_VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteStartElement("megjegyzes")
                _XmlTextWriter.WriteEndElement()
                _XmlTextWriter.WriteEndElement() 'nyomtatvanyinformacio

                _XmlTextWriter.WriteStartElement("mezok")
                UploadField(_XmlTextWriter, "0A0001C001A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0A0001C004A", _VATReport.CustomersFrom.FullName)
                If Not String.IsNullOrEmpty(_Customers.VATNumber) Then
                    UploadField(_XmlTextWriter, "0A0001C005A", _Customers.VATNumber.Remove(8, _Customers.VATNumber.Length - 8))
                End If
                UploadField(_XmlTextWriter, "0A0001C007A", _Customers.FullName)
                UploadField(_XmlTextWriter, "0A0001D001A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0A0001D002A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))

                '01 lap
                _XmlTextWriter.WriteComment("01 lap")
                UploadField(_XmlTextWriter, "0B0001B001A", "1")
                UploadField(_XmlTextWriter, "0B0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0B0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0B0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0B0001B006A", _VATReport.CustomersFrom.FullName)
                If Not String.IsNullOrEmpty(_Customers.VATNumber) Then
                    UploadField(_XmlTextWriter, "0B0001B007A", _Customers.VATNumber.Remove(8, _Customers.VATNumber.Length - 8))
                End If
                UploadField(_XmlTextWriter, "0B0001B009A", _Customers.FullName)

                '01-K lap
                _XmlTextWriter.WriteComment("01-K lap")
                UploadField(_XmlTextWriter, "0C0001B001A", "1")
                UploadField(_XmlTextWriter, "0C0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0C0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0C0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0C0001B006A", _VATReport.CustomersFrom.FullName)
                If Not String.IsNullOrEmpty(_Customers.VATNumber) Then
                    UploadField(_XmlTextWriter, "0C0001B007A", _Customers.VATNumber.Remove(8, _Customers.VATNumber.Length - 8))
                End If
                UploadField(_XmlTextWriter, "0C0001B009A", _Customers.FullName)

                '02 lap
                _XmlTextWriter.WriteComment("02 lap")
                UploadField(_XmlTextWriter, "0D0001B001A", "1")
                UploadField(_XmlTextWriter, "0D0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0D0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0D0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0D0001B006A", _VATReport.CustomersFrom.FullName)
                If Not String.IsNullOrEmpty(_Customers.VATNumber) Then
                    UploadField(_XmlTextWriter, "0D0001B007A", _Customers.VATNumber.Remove(8, _Customers.VATNumber.Length - 8))
                End If
                UploadField(_XmlTextWriter, "0D0001B009A", _Customers.FullName)

                '02-K lap
                _XmlTextWriter.WriteComment("02-K lap")
                UploadField(_XmlTextWriter, "0E0001B001A", "1")
                UploadField(_XmlTextWriter, "0E0001B002A", _VATReport.CustomersFrom.VATNumber.Replace("-", ""))
                UploadField(_XmlTextWriter, "0E0001B004A", _VATReport.DateFrom.Year.ToString + _VATReport.DateFrom.Month.ToString("00") + _VATReport.DateFrom.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0E0001B005A", _VATReport.DateTo.Year.ToString + _VATReport.DateTo.Month.ToString("00") + _VATReport.DateTo.Day.ToString("00"))
                UploadField(_XmlTextWriter, "0E0001B006A", _VATReport.CustomersFrom.FullName)
                If Not String.IsNullOrEmpty(_Customers.VATNumber) Then
                    UploadField(_XmlTextWriter, "0E0001B007A", _Customers.VATNumber.Remove(8, _Customers.VATNumber.Length - 8))
                End If
                UploadField(_XmlTextWriter, "0E0001B009A", _Customers.FullName)

                Dim _VATReportGroupCustomers2M_Customers_Collection As New XPCollection(Of VATReportGroupCustomers2M)(_ocur.Session, CriteriaOperator.Parse("VATReport=? AND Customers=?", _VATReport, _Customers))

                Dim _FullAmountVATIn As Long = 0
                Dim _FullAmountVATInBase As Long = 0
                Dim _Full02C As Long = 0
                Dim _Full02D As Long = 0
                Dim _Full0A67DA As Long = 0
                Dim _RowIndex As Long = 1
                For Each _VATReportGroupCustomers2M As VATReportGroupCustomers2M In _VATReportGroupCustomers2M_Customers_Collection
                    If Not String.IsNullOrEmpty(_VATReportGroupCustomers2M.DocumentNumber) Then
                        UploadField(_XmlTextWriter, "0D0001C" + _RowIndex.ToString("0000") + "AA", _VATReportGroupCustomers2M.DocumentNumber)
                        UploadField(_XmlTextWriter, "0D0001C" + _RowIndex.ToString("0000") + "BA", _VATReportGroupCustomers2M.DateExecution.Year.ToString + _VATReportGroupCustomers2M.DateExecution.Month.ToString("00") + _VATReportGroupCustomers2M.DateExecution.Day.ToString("00"))

                        If _VATReportGroupCustomers2M.PartnerType = ePartnerType.ePayabels Then
                            UploadField(_XmlTextWriter, "0D0001C" + _RowIndex.ToString("0000") + "CA", Math.Round(_VATReportGroupCustomers2M.AmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))
                            UploadField(_XmlTextWriter, "0D0001C" + _RowIndex.ToString("0000") + "DA", Math.Round(_VATReportGroupCustomers2M.AmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))

                            _Full02C += Math.Round(_VATReportGroupCustomers2M.AmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero)
                            _Full02D += Math.Round(_VATReportGroupCustomers2M.AmountVATIn / 1000, 0, MidpointRounding.AwayFromZero)
                        End If
                        If _VATReportGroupCustomers2M.PartnerType = ePartnerType.eReceivables Then
                            UploadField(_XmlTextWriter, "0D0001C" + _RowIndex.ToString("0000") + "CA", Math.Round(_VATReportGroupCustomers2M.AmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero))
                            UploadField(_XmlTextWriter, "0D0001C" + _RowIndex.ToString("0000") + "DA", Math.Round(_VATReportGroupCustomers2M.AmountVATOut / 1000, 0, MidpointRounding.AwayFromZero))

                            _Full02C += Math.Round(_VATReportGroupCustomers2M.AmountVATOutBase / 1000, 0, MidpointRounding.AwayFromZero)
                            _Full02D += Math.Round(_VATReportGroupCustomers2M.AmountVATOut / 1000, 0, MidpointRounding.AwayFromZero)
                        End If
                        _RowIndex += 1
                    Else
                        If _VATReportGroupCustomers2M.PartnerType = ePartnerType.ePayabels Then
                            _Full0A67DA += Math.Round(_VATReportGroupCustomers2M.AmountVATIn / 1000, 0, MidpointRounding.AwayFromZero)
                        End If
                        If _VATReportGroupCustomers2M.PartnerType = ePartnerType.eReceivables Then
                            _Full0A67DA += Math.Round(_VATReportGroupCustomers2M.AmountVATOut / 1000, 0, MidpointRounding.AwayFromZero)
                        End If
                    End If

                    _FullAmountVATIn += _VATReportGroupCustomers2M.AmountVATIn
                    _FullAmountVATInBase += _VATReportGroupCustomers2M.AmountVATInBase

                Next
                If _Full02C > 0 And _Full02D > 0 Then
                    UploadField(_XmlTextWriter, "0D0001C0037CA", _Full02C.ToString)
                    UploadField(_XmlTextWriter, "0D0001C0037DA", _Full02D.ToString)

                    UploadField(_XmlTextWriter, "0A0001E0004BA", (_RowIndex - 1).ToString)
                    UploadField(_XmlTextWriter, "0A0001E0004CA", _Full02C.ToString)
                    UploadField(_XmlTextWriter, "0A0001E0004DA", _Full02D.ToString)

                    UploadField(_XmlTextWriter, "0A0001E0007BA", (_RowIndex - 1).ToString)
                    UploadField(_XmlTextWriter, "0A0001E0007CA", _Full02C.ToString)
                    '  UploadField(_XmlTextWriter, "0A0001E0007DA", _Full02D.ToString)
                End If
                If _Full0A67DA > 0 Then
                    UploadField(_XmlTextWriter, "0A0001E0006DA", _Full0A67DA.ToString)
                    ' UploadField(_XmlTextWriter, "0A0001E0007DA", _Full0A67DA.ToString)
                End If

                UploadField(_XmlTextWriter, "0A0001E0007DA", (_Full02D + _Full0A67DA).ToString)
                'UploadField(_XmlTextWriter, "0A0001E0004CA", Math.Round(_FullAmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))
                'UploadField(_XmlTextWriter, "0A0001E0007CA", Math.Round(_FullAmountVATInBase / 1000, 0, MidpointRounding.AwayFromZero))

                'UploadField(_XmlTextWriter, "0A0001E0004DA", Math.Round(_FullAmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))
                'UploadField(_XmlTextWriter, "0A0001E0007DA", Math.Round(_FullAmountVATIn / 1000, 0, MidpointRounding.AwayFromZero))

                _XmlTextWriter.WriteEndElement() 'mezők
                _XmlTextWriter.WriteEndElement() 'nyomtatvany
            Next
        End If
        _XmlTextWriter.WriteEndElement() 'Nyomtatványok

        _XmlTextWriter.Flush()
        _XmlTextWriter.Close()
    End Sub
#End Region

End Class
