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 FarPoint.Win.Spread
Imports DevExpress.ExpressApp.SystemModule
Imports System.IO
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.Win.SystemModule
Imports DevExpress.ExpressApp.Editors


Public Class CornerVC
    Inherits DevExpress.ExpressApp.ViewController
    Dim _WaitFormClass As New WaitFormClass
    Private _FpSpread As FpSpread
    Private _GridListEditor As GridListEditor
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "Corner_InvoiceNameParameters_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", False)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", False)
        End If
        If View.Id = "Corner_CustomerNameParameters_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", False)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", False)
        End If
        If View.Id = "Corner_GLAccounts_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", False)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "GLHeader_GLRows_ListView_Accounts" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", False)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", False)
        End If
        If View.Id = "Corner_DetailView" Then
            AddHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf SaveAction_Executing
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Corner_InvoiceNameParameters_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", True)
        End If
        If View.Id = "Corner_CustomerNameParameters_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", True)
        End If
        If View.Id = "Corner_GLAccounts_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", True)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "GLHeader_GLRows_ListView_Accounts" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", True)
            Frame.GetController(Of PrintingController)().Active.SetItemValue("", True)
        End If
        If View.Id = "Corner_CustomerNameParameters_ListView" Then ' Az inline edit beállítása.
            Dim _ListView As ListView = TryCast(View, ListView)
            _GridListEditor = TryCast(_ListView.Editor, GridListEditor)

            If _GridListEditor IsNot Nothing Then
                Dim _Grid = _GridListEditor.GridView
                RemoveHandler _Grid.ShownEditor, AddressOf GridView_ShownEditor
            End If
        End If
    End Sub
    Private Sub ExcelControlViewItem_ControlCreated(ByVal sender As Object, ByVal e As EventArgs)
        Dim itemControl As Object = (CType(sender, ViewItem)).Control

        _FpSpread = TryCast(itemControl, ExcelUserControl).FpSpreadControl

        Dim _FileName As String = Path.GetTempPath & "Out.xls"
        Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
        Dim _Corner As Corner = TryCast(View.SelectedObjects(0), Corner)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        If _Corner IsNot Nothing Then
            If _ocur.Session.IsNewObject(_Corner) = False Then
                _Corner.ExcelFile.SaveToStream(_StreamFile)
                _StreamFile.Close()
                _FpSpread.OpenExcel(_FileName)
            End If
        End If
    End Sub

    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "Corner_DetailView" Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _ExcelControlViewItem As ViewItem
            If _DetailView IsNot Nothing Then
                _ExcelControlViewItem = _DetailView.GetItems(Of ExcelPropertyEditor)(0)
                If _ExcelControlViewItem IsNot Nothing Then
                    AddHandler _ExcelControlViewItem.ControlCreated, AddressOf ExcelControlViewItem_ControlCreated
                End If
            End If
        End If

        'If View.Id = "Corner_InvoiceNameParameters_ListView" Then ' Az inline edit beállítása.
        '    Dim _ListView As ListView = TryCast(View, ListView)
        '    _GridListEditor = TryCast(_ListView.Editor, GridListEditor)

        '    If _GridListEditor IsNot Nothing Then
        '        _GridListEditor.AllowEdit = True

        '        For i As Integer = 0 To _GridListEditor.Columns.Count - 1
        '            If _GridListEditor.GridView.Columns(i).FieldName <> "Controlling!" Then
        '                _GridListEditor.GridView.Columns(i).OptionsColumn.ReadOnly = True
        '                _GridListEditor.GridView.Columns(i).OptionsColumn.AllowEdit = False
        '                _GridListEditor.GridView.Columns(i).OptionsColumn.TabStop = False
        '            End If
        '        Next i

        '    End If
        'End If

        If View.Id = "Corner_CustomerNameParameters_ListView" Then ' Az inline edit beállítása.
            Dim _ListView As ListView = TryCast(View, ListView)
            _GridListEditor = TryCast(_ListView.Editor, GridListEditor)

            If _GridListEditor IsNot Nothing Then

                Dim _Grid = _GridListEditor.GridView
                AddHandler _Grid.ShownEditor, AddressOf GridView_ShownEditor

                '_GridListEditor.AllowEdit = True

                'For i As Integer = 0 To _GridListEditor.Columns.Count - 1
                '    If _GridListEditor.GridView.Columns(i).FieldName <> "Customers!" Then
                '        _GridListEditor.GridView.Columns(i).OptionsColumn.ReadOnly = True
                '        _GridListEditor.GridView.Columns(i).OptionsColumn.AllowEdit = False
                '        _GridListEditor.GridView.Columns(i).OptionsColumn.TabStop = False
                '    End If
                'Next i

            End If
        End If
    End Sub
    Sub GridView_ShownEditor(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _LookupEdit As LookupEdit = sender.ActiveEditor

        If _LookupEdit.Frame IsNot Nothing Then
            _LookupEdit.Frame.GetController(Of FilterController)().FullTextFilterAction.Value = _GridListEditor.GridView.GetRowCellValue(_GridListEditor.GridView.FocusedRowHandle, "CustomerText")

        End If
    End Sub
    Sub SaveAction_Executing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim _FileName As String = Path.GetTempPath & "In.xls"
        Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
        Dim _Corner As Corner = TryCast(View.SelectedObjects(0), Corner)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        If _Corner IsNot Nothing Then
            _FpSpread.SaveExcel(_StreamFile)
            _StreamFile.Close()
            Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)
            _Corner.ExcelFile = New FileData(_ocur.Session)
            _Corner.ExcelFile.LoadFromStream("Excel worksheet", _StreamFile2)
            _Corner.Save()
            _StreamFile2.Close()
        End If
    End Sub

    Private Sub aCreateGLAccounts_Corner_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateGLAccounts_Corner.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)

        Dim _Corner As Corner = _uow.GetObjectByKey(Of Corner)(TryCast(View.SelectedObjects(0), Corner).Oid)
        Dim _GLAccounts As GLAccounts = Nothing
        Dim _GLAccounts_check As GLAccounts
        Dim _GLRows As GLRows
        Dim _CustomersFrom As CustomersFrom = Nothing
        Dim _InvoiceNameParameters As InvoiceNameParameters
        Dim _CustomerNameParameters As CustomerNameParameters

        Dim _column As Integer = 0
        Dim _row As Integer = 0
        Dim _firstHeaderSRow As Integer
        Dim _FooterIndicator As Boolean = False
        Dim _HeaderIndicator As String = "T."
        Dim _GUID As Guid
        Dim _RowIndex As Integer = 0
        Dim _SysDate As DateTime
        Dim _Rate As Double
        Dim _VAT As VAT = Nothing
        Dim _VATText As String = ""
        Dim _CustomersText As String = ""
        Dim _CustomersFromText As String = ""

        Dim _iCustomer As Integer = 0
        Dim _iPaymentOption As Integer = 0
        Dim _iCurrencyName As Integer = 0
        Dim _iCurrencySum As Integer = 0
        Dim _iDateCreated As Integer = 0
        Dim _iDateExecution As Integer = 0
        Dim _iDatePayment As Integer = 0
        Dim _iDocumentNumber As Integer = 0
        Dim _iVATHeader As Integer = 0
        Dim _iVATRow As Integer = 0
        Dim _iNetHeader As Integer = 0
        Dim _iNetRow As Integer = 0
        Dim _iGrossHeader As Integer = 0
        Dim _iGrossRow As Integer = 0
        Dim _iCustomerNumber As Integer = 0

        Dim _GLAccounts_List As New ArrayList
        _GLAccounts_List.Clear()

        '_uow.GetObjectByKey(Of InvoiceNameParameters)("PHImportType=0 And IsNull(Controlling)")
        _InvoiceNameParameters = _uow.FindObject(Of InvoiceNameParameters)(CriteriaOperator.Parse("PHImportType=0 And IsNull(Controlling)"))
        If _InvoiceNameParameters IsNot Nothing Then ' Megvizsgáljuk, hogy az összes tétel megnevezéséhez tartozik-e controlling adat.
            Throw New UserFriendlyException("A művelet indítása előtt végezze el a Megnevezés paraméterek feldolgozását!")
        End If

        For _column = 0 To _FpSpread.ActiveSheet.GetLastNonEmptyColumn(NonEmptyItemFlag.Data) 'Saját cég nevének  megeresése a táblázatban.
            If _FpSpread.ActiveSheet.Cells(0, _column).Text <> "" Then
                '_CustomersFromText = _FpSpread.ActiveSheet.Cells(0, _column).Text.Trim   'Ha itt kap értéket a változó, a blokkon kívül nem látható az értéke. (???)
                Exit For
            End If
        Next
        _CustomersFromText = _FpSpread.ActiveSheet.Cells(0, _column).Text.Trim

        ' Mégsem a táblázatból olvassuk ki a saját céget, hanem egy propertybe bekérjük.
        '_CustomersFrom = _uow.FindObject(Of CustomersFrom)(CriteriaOperator.Parse("Name=?", _CustomersFromText))
        'If _CustomersFrom Is Nothing Then ' Saját cég keresése.
        '    Throw New UserFriendlyException("A számlát kiállító saját cég nem azonosítható.")
        'End If

        For _row = 0 To _FpSpread.ActiveSheet.GetLastNonEmptyRow(NonEmptyItemFlag.Data) 'Fejléc keresése.
            If _FpSpread.ActiveSheet.Cells(_row, 0).Text = _HeaderIndicator Then
                _firstHeaderSRow = _row
                Exit For
            End If
        Next

        For _column = 0 To _FpSpread.ActiveSheet.GetLastNonEmptyColumn(NonEmptyItemFlag.Data) ' Fejléc feldolgozása.
            Select Case _FpSpread.ActiveSheet.Cells(_firstHeaderSRow, _column).Text
                Case "Számlaszám"
                    _iDocumentNumber = _column
                Case "Megrendelő neve"
                    _iCustomer = _column
                Case "R-Nr"
                    _iCustomerNumber = _column
                Case "Kelte"
                    _iDateCreated = _column
                Case "Teljesítés"
                    _iDateExecution = _column
                Case "Esedékes"
                    _iDatePayment = _column
                Case "Mennyiség"
                    _iCurrencySum = _column
                Case "FM"
                    _iPaymentOption = _column
                Case "Áfakulcs"
                    _iCurrencyName = _column
                Case "ÁFA"
                    If _FpSpread.ActiveSheet.Cells(_firstHeaderSRow, _column).Font.Bold = True Then
                        _iVATHeader = _column
                    Else
                        _iVATRow = _column
                    End If
                Case "Nettó"
                    If _FpSpread.ActiveSheet.Cells(_firstHeaderSRow, _column).Font.Bold = True Then
                        _iNetHeader = _column
                    Else
                        _iNetRow = _column
                    End If
                Case "Bruttó"
                    If _FpSpread.ActiveSheet.Cells(_firstHeaderSRow, _column).Font.Bold = True Then
                        _iGrossHeader = _column
                    Else
                        _iGrossRow = _column
                    End If
            End Select
        Next

        _SysDate = GetSQLTime(_uow)
        _WaitFormClass.InitWork(1, 1, _FpSpread.ActiveSheet.GetLastNonEmptyRow(NonEmptyItemFlag.Data))
        For _row = _firstHeaderSRow + 1 To _FpSpread.ActiveSheet.GetLastNonEmptyRow(NonEmptyItemFlag.Data) 'Számlák készítése indul.

            _VAT = Nothing
            If (_FpSpread.ActiveSheet.Cells(_row, _iDocumentNumber).Text = "SZÁMLÁK ÖSSZESÍTÉS FIZETÉSI MÓDONKÉNT") _
                Or (_FpSpread.ActiveSheet.Cells(_row, _iCustomerNumber).Text = "SZÁMLÁK ÖSSZESÍTÉS FIZETÉSI MÓDONKÉNT") Then 'Innét tudjuk, hogy elfogytak a feldolgozandó sorok.
                _FooterIndicator = True
            End If

            _WaitFormClass.DoWork()

            If (_FpSpread.ActiveSheet.Cells(_row, 0).Text <> _HeaderIndicator) And (_FpSpread.ActiveSheet.Cells(_row, _iDocumentNumber).Text.Trim <> _CustomersFromText) _
                And Not _FooterIndicator Then

                If (_FpSpread.ActiveSheet.Cells(_row, _iDocumentNumber).Text <> "") Then

                    _GLAccounts_check = _uow.FindObject(Of GLAccounts)(CriteriaOperator.Parse("DocumentNumber=? And CustomersFrom=?", _FpSpread.ActiveSheet.Cells(_row, _iDocumentNumber).Text, _Corner.CustomersFrom))
                    If _GLAccounts_check IsNot Nothing Then ' A DocumentNumbernek egyedinek kell lennie.
                        _ocur.Rollback()
                        _WaitFormClass.Finalwork
                        Throw New UserFriendlyException("A művelet nem végezhető el, már létezik számla ezzel a számmal: " + _GLAccounts_check.DocumentNumber)
                    End If

                    '_GLAccounts = _ocur.CreateObjec(Of GLAccounts)()
                    '_GLAccounts = New GLAccounts(_ocur.Session)
                    _GLAccounts = New GLAccounts(_uow)

                    _RowIndex = _RowIndex + 1
                    _GLAccounts.HeaderInfo = Format(_SysDate.Date, "yyyy-MM-dd") + " " + Format(_RowIndex, "0000") + " " + _FpSpread.ActiveSheet.Cells(_row, _iDocumentNumber).Text

                    _GLAccounts.PartnerType = ePartnerType.eReceivables ' Partner típusa.
                    _GLAccounts.CustomersFrom = _Corner.CustomersFrom ' Saját cég.
                    _CustomersText = _FpSpread.ActiveSheet.Cells(_row, _iCustomer).Text ' Partner neve a táblázatban
                    _CustomerNameParameters = _uow.FindObject(Of CustomerNameParameters)(CriteriaOperator.Parse("CustomerNumber=? And PHImportType=0", _FpSpread.ActiveSheet.Cells(_row, _iCustomerNumber).Text))

                    If _FpSpread.ActiveSheet.Cells(_row, _iPaymentOption).Text = "KP" Then ' A fizetési módnak megfelelő ügyfél és fizetési mód kiválasztása.
                        _GLAccounts.Customers = _Corner.Customers_Cash ' Partner.
                        If _CustomerNameParameters IsNot Nothing Then
                            If _CustomerNameParameters.Customers IsNot Nothing Then
                                _GLAccounts.Customers = _CustomerNameParameters.Customers ' Ha találunk partnert a CustomerNameParameters táblában, akkor felülírjuk
                            End If
                        End If
                        _GLAccounts.PaymentOption = _uow.FindObject(Of PaymentOption)(CriteriaOperator.Parse("PayMode=?", ePayMode.InCash)) ' Fizetési mód.
                    Else 'ElseIf _FpSpread.ActiveSheet.Cells(_row, _iPaymentOption).Text = "HK" Then
                        _GLAccounts.Customers = _Corner.Customers_ATM ' Partner
                        If _CustomerNameParameters IsNot Nothing Then
                            If _CustomerNameParameters.Customers IsNot Nothing Then
                                _GLAccounts.Customers = _CustomerNameParameters.Customers ' Ha találunk partnert a CustomerNameParameters táblában, akkor felülírjuk
                            End If
                        End If
                        _GLAccounts.PaymentOption = _uow.FindObject(Of PaymentOption)(CriteriaOperator.Parse("PayMode=?", ePayMode.InTransfer))
                    End If

                    _GLAccounts.CurrencyName = _uow.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", _FpSpread.ActiveSheet.Cells(_row, _iCurrencyName).Text()))
                    If _GLAccounts.CurrencyName Is Nothing Then
                        _ocur.Rollback()
                        _WaitFormClass.Finalwork
                        Throw New UserFriendlyException("Nem sikerült beazonosítani az alábbi devizanemet: " + _FpSpread.ActiveSheet.Cells(_row, _iCurrencyName).Text)
                    End If

                    If _GLAccounts.CurrencyName.ShortName = "HUF" Then ' Árfolyam kiszámolása.
                        _Rate = 1 ' A belső számolásokhoz
                        _GLAccounts.CurrencyRateVAT = 1
                    Else
                        _Rate = Convert.ToDouble(_FpSpread.ActiveSheet.Cells(_row, _iGrossHeader).Text) / Convert.ToDouble(_FpSpread.ActiveSheet.Cells(_row, _iCurrencySum).Text) ' A belső számolásokhoz
                        _GLAccounts.CurrencyRateVAT = Math.Round(_Rate, 4, MidpointRounding.AwayFromZero)
                    End If
                    _GLAccounts.CurrencyRate = _GLAccounts.CurrencyRateVAT ' A két árfolyam azonos.

                    _GLAccounts.DateCreated = Convert.ToDateTime(_FpSpread.ActiveSheet.Cells(_row, _iDateCreated).Text).Date
                    _GLAccounts.DateExecution = Convert.ToDateTime(_FpSpread.ActiveSheet.Cells(_row, _iDateExecution).Text).Date
                    _GLAccounts.DatePayment = Convert.ToDateTime(_FpSpread.ActiveSheet.Cells(_row, _iDatePayment).Text).Date

                    _GLAccounts.DocumentNumber = _FpSpread.ActiveSheet.Cells(_row, _iDocumentNumber).Text
                    _GLAccounts.ConnectInfo = _FpSpread.ActiveSheet.Cells(_row, _iDocumentNumber).Text
                    _GLAccounts.GLDocumentType = eGLDocumentType.eGLAccount ' Dokumentum típusa.

                    _GLAccounts.IsEditable = False ' Nem szerkeszthető.
                    _GLAccounts.IsStorno = False ' Nem sztornózott.

                    _GLAccounts.NoteHeader = "Corner"
                    _GLAccounts.ImportedOther = _Corner.Oid

                    _GLAccounts.Save()
                    _GLAccounts_List.Add(_GLAccounts)
                End If

                If _FpSpread.ActiveSheet.Cells(_row, _iGrossRow).Text <> "" Then  ' Tétel sorok.
                    _GUID = Guid.NewGuid
                    _InvoiceNameParameters = _uow.FindObject(Of InvoiceNameParameters)(CriteriaOperator.Parse("PHImportType=0 And ShortName=?", _FpSpread.ActiveSheet.Cells(_row, _iPaymentOption).Text))

                    If _InvoiceNameParameters Is Nothing Then ' Megvizsgáljuk, hogy az adott tétel megtalálható-e.
                        _ocur.Rollback()
                        _WaitFormClass.Finalwork
                        Throw New UserFriendlyException("A művelet nem végezhető el, végre kell hajtani a Megnevezés paraméterek feldolgozását!")
                    End If

                    If _VAT Is Nothing Then
                        Select Case _FpSpread.ActiveSheet.Cells(_row, _iCurrencyName).Text ' Egyenlőre úgy tűnik, át kell konvertálni az ÁFA kódolkat, ez a PH-ra lett szabva.
                            Case "27"
                                _VATText = "27%"
                            Case "25"
                                _VATText = "25%"
                            Case "18"
                                _VATText = "18%"
                            Case "0"
                                _VATText = "ÁTHK"
                            Case Else
                                _VATText = ""
                        End Select
                        _VAT = _uow.FindObject(Of VAT)(CriteriaOperator.Parse("ShortName=?", _VATText))
                    End If

                    If _VAT Is Nothing Then
                        _VAT = _uow.FindObject(Of VAT)(CriteriaOperator.Parse("ShortName=?", LTrim(RTrim(_FpSpread.ActiveSheet.Cells(_row, _iCurrencyName).Text)) + "%"))
                    End If
                    If _VAT Is Nothing Then
                        _VAT = _uow.FindObject(Of VAT)(CriteriaOperator.Parse("ShortName=?", LTrim(RTrim(_FpSpread.ActiveSheet.Cells(_row, _iCurrencyName).Text)) + " %"))
                    End If 'Nem egységes az áfakulcs sor rögzítése, de megtesszük, amit lehet...

                    If _VAT Is Nothing Then ' Nem találtuk meg az ÁFA-kulcsot
                        _ocur.Rollback()
                        _WaitFormClass.Finalwork
                        Throw New UserFriendlyException("Nem sikerült megtalálni az Áfa-kulcs paramétert: " + _VATText)
                    End If


                    If Convert.ToDouble(_FpSpread.ActiveSheet.Cells(_row, _iNetRow).Text) <> 0 Then ' Nettó rész.
                        _GLRows = New GLRows(_uow)

                        If _GLAccounts.CurrencyName.ShortName = "HUF" Then
                            _GLRows.AmountHUF = Convert.ToDouble(_FpSpread.ActiveSheet.Cells(_row, _iNetRow).Text)
                        Else
                            _GLRows.AmountDEV = Math.Round(Convert.ToDouble(_FpSpread.ActiveSheet.Cells(_row, _iNetRow).Text) / _Rate, 4, MidpointRounding.AwayFromZero)  ' Számolni kell.
                            _GLRows.AmountHUF = Convert.ToDouble(_FpSpread.ActiveSheet.Cells(_row, _iNetRow).Text)
                        End If

                        If _FpSpread.ActiveSheet.Cells(_row, _iPaymentOption).Text = "KP" Then ' Más a főkönyvi szlaszám kp., és más utalás esetén.
                            _GLRows.DebitChartOfAccounts = _Corner.Customers_Cash.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eOut)
                        Else
                            _GLRows.DebitChartOfAccounts = _Corner.Customers_ATM.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eOut)
                        End If
                        _GLRows.CreditChartOfAccounts = _InvoiceNameParameters.Controlling.ChartOfAccounts

                        _GLRows.GLRowsGUID = _GUID
                        _GLRows.ConnectInfo = _GLAccounts.DocumentNumber
                        _GLRows.GLHeader = TryCast(_GLAccounts, GLHeader)

                        _GLRows.Customer = _GLAccounts.Customers
                        _GLRows.PartnerType = ePartnerType.eReceivables
                        _GLRows.NoteRows = _CustomersText
                        _GLRows.GLHeader.NoteHeader = _CustomersText

                        _GLRows.DateVAT = _GLAccounts.DateExecution
                        _GLRows.VAT = _VAT
                        _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eNettoRow

                        _GLRows.CostHolder = _Corner.CostHolder
                        _GLRows.Controlling = _InvoiceNameParameters.Controlling
                        _GLRows.CostPlace = Nothing
                        _GLRows.JobNumberObjects = Nothing

                        _GLRows.Save()
                        _GLAccounts.RecalculateTotal()


                        _GLRows = New GLRows(_uow) ' Áfa rész

                        If _GLAccounts.CurrencyName.ShortName = "HUF" Then
                            _GLRows.AmountHUF = Convert.ToDouble(_FpSpread.ActiveSheet.Cells(_row, _iVATRow).Text)
                        Else
                            _GLRows.AmountDEV = Math.Round(Convert.ToDouble(_FpSpread.ActiveSheet.Cells(_row, _iVATRow).Text) / _Rate, 4, MidpointRounding.AwayFromZero)  ' Számolni kell.
                            _GLRows.AmountHUF = Convert.ToDouble(_FpSpread.ActiveSheet.Cells(_row, _iVATRow).Text)
                        End If

                        If _FpSpread.ActiveSheet.Cells(_row, _iPaymentOption).Text = "KP" Then ' Más a főkönyvi szlaszám kp., és más utalás esetén.
                            _GLRows.DebitChartOfAccounts = _Corner.Customers_Cash.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eOut)
                        Else
                            _GLRows.DebitChartOfAccounts = _Corner.Customers_ATM.CustomersGLParameters.GetChartOfAccounts(_GLAccounts.DateExecution.Year, eVATMode.eOut)
                        End If
                        _GLRows.CreditChartOfAccounts = _VAT.ChartOfAccountsOut

                        _GLRows.GLRowsGUID = _GUID
                        _GLRows.ConnectInfo = _GLAccounts.DocumentNumber
                        _GLRows.GLHeader = TryCast(_GLAccounts, GLHeader)

                        _GLRows.Customer = _GLAccounts.Customers
                        _GLRows.PartnerType = ePartnerType.eReceivables
                        _GLRows.NoteRows = _CustomersText
                        _GLRows.GLHeader.NoteHeader = _CustomersText

                        _GLRows.DateVAT = _GLAccounts.DateExecution
                        _GLRows.VAT = _VAT
                        _GLRows.GLRowsTypeVAT = eGLRowsTypeVAT.eVATRow

                        _GLRows.CostHolder = _Corner.CostHolder
                        _GLRows.Controlling = _InvoiceNameParameters.Controlling
                        _GLRows.CostPlace = Nothing
                        _GLRows.JobNumberObjects = Nothing

                        _GLRows.Save()
                        _GLAccounts.RecalculateTotal()
                    End If
                End If
            End If

        Next
        _uow.CommitChanges()
        _WaitFormClass.Finalwork

        '// Pontozás lefuttatása
        _WaitFormClass.InitWork(1, 1, _GLAccounts_List.Count)
        For Each _GLAccounts In _GLAccounts_List
            _WaitFormClass.DoWork()
            GLFunctions.ReconfigurePCI(_uow, _GLAccounts.CustomersFrom, _GLAccounts.Customers, ePartnerType.eReceivables, _GLAccounts.ConnectInfo)
        Next
        _WaitFormClass.Finalwork


        View.Refresh()

    End Sub

    Private Sub aCheckName_Corner_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCheckName_Corner.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)

        Dim _InvoiceNameParameters As InvoiceNameParameters
        Dim _CustomerNameParameters As CustomerNameParameters

        Dim _firstHeaderSRow As Integer
        Dim _HeaderIndicator As String = "T."
        Dim _ColumnName1 As String = "FM"
        Dim _ColumnName2 As String = "Bruttó"
        Dim _ColumnName3 As String = "Megrendelő neve"
        Dim _ColumnName31 As String = "Útlevél"
        Dim _ColumnName4 As String = "R-Nr"
        Dim _ColumnName5 As String = "Mennyiség"
        Dim _iCol1 As Integer
        Dim _iCol2 As Integer
        Dim _iCol3 As Integer
        Dim _iCol4 As Integer
        Dim _iCol5 As Integer

        Dim _row As Integer
        Dim _column As Integer

        For _row = 0 To _FpSpread.ActiveSheet.GetLastNonEmptyRow(NonEmptyItemFlag.Data) 'Megkeressük az első oldal fejlécét.
            If _FpSpread.ActiveSheet.Cells(_row, 0).Text = _HeaderIndicator Then
                _firstHeaderSRow = _row
                Exit For
            End If
        Next

        For _column = 0 To _FpSpread.ActiveSheet.GetLastNonEmptyColumn(NonEmptyItemFlag.Data) 'Végigmegyünk az oszlopokon.

            Select Case _FpSpread.ActiveSheet.Cells(_firstHeaderSRow, _column).Text ' Fejléc feldolgozása.
                Case _ColumnName1
                    _iCol1 = _column ' Eltároljuk a "fizetési mód/megnevezés" oszlop sorszámát.
                Case _ColumnName2
                    If _FpSpread.ActiveSheet.Cells(_firstHeaderSRow, _column).Font.Bold = False Then
                        _iCol2 = _column ' Eltároljuk a "tételsor bruttó értéke" oszlop sorszámát
                    End If
                Case _ColumnName3 ' Eltároljuk a "Vendég neve/Útlevél" oszlop sorszámát
                    _iCol3 = _column
                Case _ColumnName4 ' Eltároljuk az "R-Nr" oszlop sorszámát
                    _iCol4 = _column
                Case _ColumnName5 ' Eltároljuk a "Mennyiség" oszlop sorszámát
                    _iCol5 = _column

            End Select
        Next

        _WaitFormClass.InitWork(1, 1, _FpSpread.ActiveSheet.GetLastNonEmptyRow(NonEmptyItemFlag.Data))
        For _row = _firstHeaderSRow To _FpSpread.ActiveSheet.GetLastNonEmptyRow(NonEmptyItemFlag.Data) ' Végigmegyünk az egész táblázaton.

            _WaitFormClass.DoWork()

            If _FpSpread.ActiveSheet.Cells(_row, _iCol2).Text <> _ColumnName2 And _FpSpread.ActiveSheet.Cells(_row, _iCol2).Text <> "" Then ' Ha a cella szövege nem üres, de nem is az oszlop fejlécének szövege, akkor találtunk tételsort.
                _InvoiceNameParameters = _uow.FindObject(Of InvoiceNameParameters)(CriteriaOperator.Parse("ShortName=? And PHImportType=0", _FpSpread.ActiveSheet.Cells(_row, _iCol1).Text))

                If _InvoiceNameParameters Is Nothing Then ' Amennyigen a megnevezés még nincs az InvoiceNameParameters táblában, beszúrjuk.
                    _InvoiceNameParameters = New InvoiceNameParameters(_uow)
                    _InvoiceNameParameters.PHImportType = ePHImportType.eCorner
                    _InvoiceNameParameters.ShortName = _FpSpread.ActiveSheet.Cells(_row, _iCol1).Text

                    Try
                        _InvoiceNameParameters.Save()
                        _uow.CommitChanges()
                    Catch
                        _WaitFormClass.Finalwork
                    End Try
                End If
            End If

            If _FpSpread.ActiveSheet.Cells(_row, _iCol3).Text <> _ColumnName3 And _FpSpread.ActiveSheet.Cells(_row, _iCol3).Text <> _ColumnName31 _
                And _FpSpread.ActiveSheet.Cells(_row, _iCol3).Text <> "" And _FpSpread.ActiveSheet.Cells(_row, _iCol5).Text <> "" Then ' Ha a cella szövege nem üres, de nem is az oszlop fejlécének szövege, akkor találtunk tételsort.
                _CustomerNameParameters = _uow.FindObject(Of CustomerNameParameters)(CriteriaOperator.Parse("CustomerNumber=? And PHImportType=0", _FpSpread.ActiveSheet.Cells(_row, _iCol4).Text))

                If _CustomerNameParameters Is Nothing Then ' Amennyigen az ügyfélszám még nincs az CustomerNameParameters táblában, beszúrjuk.
                    _CustomerNameParameters = New CustomerNameParameters(_uow)
                    _CustomerNameParameters.PHImportType = ePHImportType.eCorner
                    _CustomerNameParameters.CustomerText = _FpSpread.ActiveSheet.Cells(_row, _iCol3).Text
                    _CustomerNameParameters.CustomerNumber = _FpSpread.ActiveSheet.Cells(_row, _iCol4).Text

                    Try
                        _CustomerNameParameters.Save()
                        _uow.CommitChanges()
                    Catch
                        _WaitFormClass.Finalwork
                    End Try
                End If
            End If

        Next
        _WaitFormClass.Finalwork
        View.Refresh()
    End Sub
    Private Sub aRemoveGLAccounts_Corner_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRemoveGLAccounts_Corner.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _Corner As Corner = TryCast(View.SelectedObjects(0), Corner)
        Dim _GLAccounts_Collection As New XPCollection(Of GLAccounts)(_uow, CriteriaOperator.Parse("ImportedOther=?", _Corner.Oid))

        _uow.Delete(_GLAccounts_Collection)
        _uow.CommitChanges()

        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
    End Sub
End Class
