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.Data.Filtering
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Xpo
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports System.Linq
Imports System.Linq.Expressions
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Utils
Imports System.Net.Mail

Public Class InvoiceVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        Dim _InvoiceHeader As InvoiceHeader
        MyBase.OnActivated()
        Frame.GetController(Of InvoiceVC)().aInvoiceFromContractsRows.Active.SetItemValue("", False)

        Frame.GetController(Of InvoiceVC)().aFinalizeInvoice.Active.SetItemValue("", False) '-- Végleges számla készítése
        Frame.GetController(Of InvoiceVC)().aFinalizeInvoicewithPopup.Active.SetItemValue("", False) '-- Véglegesítés popuppal
        Frame.GetController(Of InvoiceVC)().aCreateProforma.Active.SetItemValue("", False) '-- Proforma számla készítése
        Frame.GetController(Of InvoiceVC)().aStornoInvoice.Active.SetItemValue("", False) '-- Végleges számla stornírozása
        Frame.GetController(Of InvoiceVC)().aMakeEditableProforma.Active.SetItemValue("", False) ' -- Proforma számla szerkeszthetővé tétele
        Frame.GetController(Of InvoiceVC)().aCreateInvoice_Final_Proforma.Active.SetItemValue("", False) ' -- Végszámla készítése
        Frame.GetController(Of InvoiceVC)().aDeleteSelectedInvoiceRows.Active.SetItemValue("", False) ' Kijelölt sor törlése
        Frame.GetController(Of InvoiceVC)().aViewGLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aDescriptionChanger.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aDescriptionChangerHeader.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aInvoiceRowCloner.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aInvoiceHeaderClone.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aGenerateInvoiceToPdfFiles.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aCloneFullInvoice.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aOpenInvoicePdf.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC).aPCICheckInvoice.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC).aViewPCIDInvoice.Active.SetItemValue("", False)
        '------------- LISTVIEW ----------------------------------------------------------------------------------------------------------------------------
        If View.Id = "InvoiceHeader_ListView_Proforma" Then '-- Proforma lista nézet
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf InvoiceHeader_ListView_Proforma_DeleteAction_Executing
            Frame.GetController(Of InvoiceVC)().aFinalizeInvoice.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aFinalizeInvoicewithPopup.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aMakeEditableProforma.Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aInvoiceHeaderClone.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aCloneFullInvoice.Active.SetItemValue("", True)

            Frame.GetController(Of InvoiceVC)().aCreateInvoiceCreditNote.Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceHeader_InvoiceRows_ListView" Then ' Számla sorok amik egy fejléchez tartoznak
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)

            Dim _ListView As ListView = CType(View, ListView)
            Dim _Cs As PropertyCollectionSource = CType(_ListView.CollectionSource, PropertyCollectionSource)

            If TryCast(_Cs.MasterObject, InvoiceHeader) IsNot Nothing Then
                If TryCast(_Cs.MasterObject, InvoiceHeader).IsEditable = False Then ' Amennyiben szerkeszthető a fejléc csak akkor lehessen sort törölni, és ne lehessen új sort hozzáadni
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
                End If
            Else
                Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            End If
        End If
        If View.Id = "InvoiceHeader_ListView" Then ' Számlák lista nézet
            Frame.GetController(Of InvoiceVC)().aStornoInvoice.Active.SetItemValue("", True) '-- Végleges számla stornírozása
            ' Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aPrintSelectedInvoices.Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aInvoiceHeaderClone.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aGenerateInvoiceToPdfFiles.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aCloneFullInvoice.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aOpenInvoicePdf.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC).aPCICheckInvoice.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC).aViewPCIDInvoice.Active.SetItemValue("", True)
        End If
        If View.Id = "InvoiceHeader_ListView_Editable" Then ' Szerkeszthető számlák
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aInvoiceHeaderClone.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aCloneFullInvoice.Active.SetItemValue("", True)
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf InvoiceHeader_ListView_Editable_DeleteAction_Executing

            Frame.GetController(Of InvoiceVC)().aCreateInvoiceCreditNote.Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceHeader_LookupListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC).aFinalizeInvoice.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC).aFinalizeInvoicewithPopup.Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceRows_ListView_Advanced" Then ' Előleg tipusú számla sorok lista nézet
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC).aPCICheckInvoice.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC).aViewPCIDInvoice.Active.SetItemValue("", True)

            Frame.GetController(Of InvoiceVC)().aCreateInvoiceCreditNote.Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceHeader_ListView_Proforma_Advanced" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf InvoiceHeader_ListView_Proforma_DeleteAction_Executing
            Frame.GetController(Of InvoiceVC)().aPrintSelectedInvoices.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aCloneFullInvoice.Active.SetItemValue("", True)

            Frame.GetController(Of InvoiceVC)().aCreateInvoiceCreditNote.Active.SetItemValue("", False)
            'Frame.GetController(Of InvoiceVC)().a.Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceHeader_InvoiceRows_ListView_Corrigendum" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "GLAccounts_DetailView_InvoiceHeader" Then
            Frame.GetController(Of GLAccountsVC).aMakeEditable_GLAccounts.Active.SetItemValue("", False)
            Frame.GetController(Of GLAccountsVC).aToTable_GLAccounts.Active.SetItemValue("", False)
            Frame.GetController(Of GLAccountsVC).aToTableD_GLAccounts.Active.SetItemValue("", False)
            Frame.GetController(Of GLAccountsVC).aBackToRowEdit_GLAccounts.Active.SetItemValue("", False)
            Frame.GetController(Of GLAccountsVC).aFinal_GLAccounts.Active.SetItemValue("", False)
            Frame.GetController(Of GLAccountsVC).aFinalAndNew_GLAccounts.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)

        End If
        If View.Id = "InvoiceHeader_ListView_Proforma_CustomRate" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf InvoiceHeader_ListView_Proforma_DeleteAction_Executing
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aPrintSelectedInvoices.Active.SetItemValue("", True) '-- Számla nyomtatása
            Frame.GetController(Of InvoiceVC)().aFinalizeInvoice.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aFinalizeInvoicewithPopup.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aCloneFullInvoice.Active.SetItemValue("", True)

            Frame.GetController(Of InvoiceVC)().aCreateInvoiceCreditNote.Active.SetItemValue("", False)

            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf InvoiceHeader_ListView_Proforma_CustomRate_DeleteAction_Executing
        End If
        '------------- DETAILVIEW --------------------------------------------------------------------------------------------------------------------------
        If View.Id = "InvoiceHeader_DetailView" Then ' Számla szerkesztő nézete, többek közt :)

            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).SaveAction.Executing, AddressOf InvoiceHeader_DetailView_SaveActionExecuting
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).SaveAndCloseAction.Executing, AddressOf InvoiceHeader_DetailView_SaveActionExecuting
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).SaveAndNewAction.Executing, AddressOf InvoiceHeader_DetailView_SaveActionExecuting

            If View.SelectedObjects.Count > 0 Then
                _InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)

                Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)

                If _InvoiceHeader.IsEditable Or _InvoiceHeader.IsProforma Then
                    Frame.GetController(Of InvoiceVC)().aFinalizeInvoice.Active.SetItemValue("", True)
                    Frame.GetController(Of InvoiceVC)().aFinalizeInvoicewithPopup.Active.SetItemValue("", True)
                End If
                If _InvoiceHeader.IsEditable = False Then
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController)().Active.SetItemValue("", False)
                Else
                    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
                    Frame.GetController(Of InvoiceVC)().aCreateProforma.Active.SetItemValue("", True)
                End If
                If _InvoiceHeader.IsEditable = True Then
                    Frame.GetController(Of InvoiceVC)().aCreateInvoice_Final_Proforma.Active.SetItemValue("", True)
                    Frame.GetController(Of InvoiceVC)().aDescriptionChanger.Active.SetItemValue("", True)
                    Frame.GetController(Of InvoiceVC)().aDescriptionChangerHeader.Active.SetItemValue("", True)
                    Frame.GetController(Of InvoiceVC)().aInvoiceRowCloner.Active.SetItemValue("", True)
                End If
            Else
                Frame.GetController(Of InvoiceVC)().aCreateInvoice_Final_Proforma.Active.SetItemValue("", True)
                Frame.GetController(Of InvoiceVC)().aDescriptionChanger.Active.SetItemValue("", True)
                Frame.GetController(Of InvoiceVC)().aDescriptionChangerHeader.Active.SetItemValue("", True)
                Frame.GetController(Of InvoiceVC)().aInvoiceRowCloner.Active.SetItemValue("", True)
                Frame.GetController(Of InvoiceVC)().aFinalizeInvoice.Active.SetItemValue("", True)
            End If
        End If


        If View.Id = "InvoiceHeader_DetailView_Corrigendum" Then
            Frame.GetController(Of InvoiceRowVC).aToTable_Invoice.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceRowVC).aBackToRowEdit_Invoice.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceRowVC).aToTableD_Invoice.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC).aFinalizeInvoice.Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aFinalizeInvoicewithPopup.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceAdvancePopup_DetailView" Then ' Előleg bekérő készítő view
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "EMailSendQueueParameters_ListView_PopUp" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf EMailSendQueueParameters_ListView_PopUp_AcceptAction_Executing
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of InvoiceVC)().aInvoiceFromContractsRows.Active.SetItemValue("", False)

        Frame.GetController(Of InvoiceVC)().aFinalizeInvoice.Active.SetItemValue("", False) '-- Végleges számla készítése
        Frame.GetController(Of InvoiceVC)().aFinalizeInvoicewithPopup.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aCreateProforma.Active.SetItemValue("", False) '-- Proforma számla készítése
        Frame.GetController(Of InvoiceVC)().aStornoInvoice.Active.SetItemValue("", False) '-- Végleges számla stornírozása
        Frame.GetController(Of InvoiceVC)().aPrintSelectedInvoices.Active.SetItemValue("", False) '-- Számla nyomtatása
        Frame.GetController(Of InvoiceVC)().aMakeEditableProforma.Active.SetItemValue("", False) ' -- Proforma számla szerkeszthetővé tétele
        Frame.GetController(Of InvoiceVC)().aCreateInvoice_Final_Proforma.Active.SetItemValue("", False) ' -- Végszámla készítése
        Frame.GetController(Of InvoiceVC)().aDeleteSelectedInvoiceRows.Active.SetItemValue("", False) ' Kijelölt sor törlése
        Frame.GetController(Of InvoiceVC)().aViewGLAccounts.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aDescriptionChanger.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aInvoiceRowCloner.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aInvoiceHeaderClone.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aGenerateInvoiceToPdfFiles.Active.SetItemValue("", False)
        Frame.GetController(Of InvoiceVC)().aCloneFullInvoice.Active.SetItemValue("", False)

        If View.Id = "InvoiceHeader_ListView" Then
            Frame.GetController(Of InvoiceVC)().aStornoInvoice.Active.SetItemValue("", False) '-- Végleges számla stornírozása
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aPrintSelectedInvoices.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aInvoiceHeaderClone.Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceAdvancePopup_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "InvoiceHeader_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).SaveAction.Executing, AddressOf InvoiceHeader_DetailView_SaveActionExecuting
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).SaveAndCloseAction.Executing, AddressOf InvoiceHeader_DetailView_SaveActionExecuting
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).SaveAndNewAction.Executing, AddressOf InvoiceHeader_DetailView_SaveActionExecuting
        End If
        If View.Id = "InvoiceRows_ListView_Advanced" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "InvoiceHeader_ListView_Editable" Then ' Szerkeszthető számlák
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aInvoiceHeaderClone.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aCloneFullInvoice.Active.SetItemValue("", False)
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf InvoiceHeader_ListView_Editable_DeleteAction_Executing
        End If
        If View.Id = "InvoiceHeader_InvoiceRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "InvoiceHeader_DetailView_Corrigendum" Then
            Frame.GetController(Of InvoiceRowVC).aToTable_Invoice.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceRowVC).aBackToRowEdit_Invoice.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceRowVC).aToTableD_Invoice.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC).aFinalizeInvoice.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aFinalizeInvoicewithPopup.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "InvoiceHeader_InvoiceRows_ListView_Corrigendum" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
        End If
        If View.Id = "InvoiceHeader_ListView_Proforma" Then '-- Proforma lista nézet
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf InvoiceHeader_ListView_Proforma_DeleteAction_Executing
            Frame.GetController(Of InvoiceVC)().aFinalizeInvoice.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aFinalizeInvoicewithPopup.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aPrintSelectedInvoices.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC)().aMakeEditableProforma.Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aInvoiceHeaderClone.Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceHeader_ListView_Proforma_CustomRate" Then
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of InvoiceVC)().aPrintSelectedInvoices.Active.SetItemValue("", False) '-- Számla nyomtatása
            Frame.GetController(Of InvoiceVC)().aFinalizeInvoice.Active.SetItemValue("", False)
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController).DeleteAction.Executing, AddressOf InvoiceHeader_ListView_Proforma_CustomRate_DeleteAction_Executing
        End If
        If View.Id = "EMailSendQueueParameters_ListView_PopUp" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf EMailSendQueueParameters_ListView_PopUp_AcceptAction_Executing
        End If
    End Sub
    Private Sub aFinalizeInvoice_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalizeInvoice.Execute
        '--Az eljárás egy kimenő számlát véglegesít-------------------------------------------
        Dim _InvoiceHeader As InvoiceHeader
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

        If Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).SaveAction.Enabled Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).SaveAction.DoExecute()
        End If

        For Each _InvoiceHeader In View.SelectedObjects
            FinalizeInvoice(_InvoiceHeader, View, _ocur)

            'Email küldés
            If GLOBAL_SQLServer Like "192.168.6.9*" Then 'CSAK A MOLCONTROL MIATT !
                Try
                    Dim _WebParameters As WebParameters = _onew.FindObject(Of WebParameters)(CriteriaOperator.Parse("1=1"))
                    If _WebParameters Is Nothing Then Throw New Exception("*Nincsnek e-mail paraméterek beállítva!")
                    If _WebParameters.SMTPHost Is Nothing Then Throw New Exception("*Nincs SMTP server beállítva!")
                    If _WebParameters.SMTPUserName = "" Then Throw New Exception("*Nincs felhasználói név beállítva!")
                    If _WebParameters.SMTPPort = 0 Then Throw New Exception("*Nincs SMTP port beállítva!")

                    Dim _MailMessage As New MailMessage
                    Dim _SmtpClient As SmtpClient = New SmtpClient(_WebParameters.SMTPHost)
                    _MailMessage.From = New MailAddress(_WebParameters.Email_SupportAdmin)
                    If _InvoiceHeader.Customers.Contacts.Count > 0 Then
                        For Each _Contacts As Contacts In _InvoiceHeader.Customers.Contacts
                            If Not String.IsNullOrEmpty(_Contacts.Email) Then _MailMessage.To.Add(_Contacts.Email)
                        Next
                    ElseIf Not String.IsNullOrEmpty(_InvoiceHeader.Customers.Email) Then
                        _MailMessage.To.Add(_InvoiceHeader.Customers.Email)
                    Else
                        Throw New Exception("*Nincs email cím beállítva!")
                    End If

                    _MailMessage.Subject = "Új számla készült"
                    _MailMessage.IsBodyHtml = True
                    _MailMessage.Body = String.Format("{0} számmal új számla készült az Ön(ök) részére.", _InvoiceHeader.DocumentNumber)

                    _SmtpClient.Port = _WebParameters.SMTPPort
                    _SmtpClient.Credentials = New System.Net.NetworkCredential(_WebParameters.SMTPUserName, _WebParameters.SMTPPassword)
                    _SmtpClient.EnableSsl = _WebParameters.EnableSsl

                    _SmtpClient.Send(_MailMessage)

                    If _InvoiceHeader.UserCreated IsNot Nothing Then
                        If Not String.IsNullOrEmpty(_InvoiceHeader.UserCreated.Email) Then
                            _MailMessage.To.Clear()
                            _MailMessage.To.Add(_InvoiceHeader.UserCreated.Email)
                            _MailMessage.Body = String.Format("Ön {0} számmal új számlát véglegesített a(z) {1} ügyfél részére.", _
                                                              _InvoiceHeader.DocumentNumber, _InvoiceHeader.Customers.FullName)

                            _SmtpClient.Send(_MailMessage)
                        End If
                    End If
                Catch ex As Exception
                    Dim sf As StackFrame = New StackFrame()
                    Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
                    Dim _ErrorLog As New ErrorLog(_uow)
                    _ErrorLog.Description = ex.Message
                    _ErrorLog.SubName = TryCast(sf.GetMethod(), System.Reflection.MethodBase).Name
                    _uow.CommitTransaction()
                End Try
            End If
        Next
        aPrintSelectedInvoices_Execute(sender, e)
        If TryCast(View, DetailView) IsNot Nothing Then
            View.Close(False)
        Else
            View.Refresh()
        End If

    End Sub
    Private Sub AcceptAction_Executing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim _InvoiceAdvancePopup As InvoiceAdvancePopup = TryCast(View.SelectedObjects(0), InvoiceAdvancePopup)
        If _InvoiceAdvancePopup IsNot Nothing Then
            If _InvoiceAdvancePopup.CurrencyName.ShortName = "HUF" Then
                If _InvoiceAdvancePopup.AmountHUF = 0 Then
                    Throw New Exception("*Nincs megadva összeg !")
                    e.Cancel = True
                End If
            Else
                If _InvoiceAdvancePopup.AmountDEV = 0 Then

                    Throw New Exception("*Nincs megadva deviza összeg !")
                    e.Cancel = True
                End If
            End If
        End If
        Validator.RuleSet.ValidateAll(View.ObjectSpace, View.SelectedObjects, DefaultContexts.Save)
    End Sub
    Private Sub InvoiceHeader_ListView_Proforma_DeleteAction_Executing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim _InvoiceHeader As InvoiceHeader
        Dim _InvoiceRows As InvoiceRows

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _ContractRows_Collection As New XPCollection(Of ContractRows)(_ocur.Session, CriteriaOperator.Parse("IsNull(InvoiceRows)=false"))
        Dim _Worksheet_CostRows_Collection As New XPCollection(Of Worksheet_CostRows)(_ocur.Session, CriteriaOperator.Parse("IsNull(InvoiceRows)=false"))


        Dim _selection As ArrayList
        Dim _selection_WorkSheet As ArrayList
        Dim _ContractRows As ContractRows
        _selection = New ArrayList()
        _selection_WorkSheet = New ArrayList()

        For Each _InvoiceHeader In View.SelectedObjects
            Dim _inoperator As New InOperator("InvoiceRows", _InvoiceHeader.InvoiceRows)
            _ContractRows_Collection.Filter = _inoperator
            For Each _ContractRows In _ContractRows_Collection
                _selection.Add(_ContractRows)
            Next

            Dim _inoperatorCostRows As New InOperator("InvoiceRows", _InvoiceHeader.InvoiceRows)
            _Worksheet_CostRows_Collection.Filter = _inoperatorCostRows
            For Each _Worksheet_CostRows In _Worksheet_CostRows_Collection
                _selection_WorkSheet.Add(_Worksheet_CostRows)
            Next
            For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
                If _InvoiceRows.OrderInRows IsNot Nothing Then
                    _InvoiceRows.OrderInRows.QTT_Invoiced -= _InvoiceRows.QTT
                End If
                If _InvoiceRows.OrderInRowsOther IsNot Nothing Then
                    _InvoiceRows.OrderInRowsOther.QTT_Invoiced -= _InvoiceRows.QTT
                End If
                If _InvoiceRows.DeliveryNoteOutRows IsNot Nothing Then
                    _InvoiceRows.DeliveryNoteOutRows.QTT_Invoiced -= _InvoiceRows.QTT
                End If
            Next
        Next

        For Each _ContractRows In _selection
            _ContractRows.InvoiceRows = Nothing
        Next
        For Each _Worksheet_CostRows As Worksheet_CostRows In _selection_WorkSheet
            _Worksheet_CostRows.InvoiceRows = Nothing
        Next
        _ocur.CommitChanges()

    End Sub
    Private Sub aStornoInvoice_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aStornoInvoice.Execute
        '--Az eljárás egy kimenő számlát sztornóz----------------------------------------------
        Dim _InvoiceHeader As InvoiceHeader
        For Each _InvoiceHeader In View.SelectedObjects
            StornoInvoice(_InvoiceHeader)
        Next
    End Sub
    Private Sub InvoiceVC_Deactivating(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If View.Id Like "InvoiceHeader_ListView" Then
            If View.SelectedObjects.Count > 0 Then
                Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
                'If _InvoiceHeader.IsEditable = True Then
                Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
                'End If
            End If
        End If
    End Sub
    Private Sub aInvoiceFromContractsRows_CustomizePopupWindowParams(ByVal sender As Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aInvoiceFromContractsRows.CustomizePopupWindowParams
        '--- Létrehoz egy Szűrt ListView-t ahol a számla sorokat lehet importálni
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        CType(View.SelectedObjects(0), InvoiceHeader).Save()
        View.ObjectSpace.CommitChanges()
        Dim _InvoiceHeader As InvoiceHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), InvoiceHeader))
        Dim _CS As New CollectionSource(_onew, GetType(ContractRows))

        _CS.BeginUpdateCriteria()
        _CS.Criteria.Clear()
        _CS.Criteria("First") = CriteriaOperator.Parse("Contracts.ContractTemplate.PartnerType=0 AND Contracts.CustomersFrom=? AND Contracts.Customers=? AND DateExecution=? and InvoiceRows=?", _
                                                       _InvoiceHeader.CustomersFrom, _InvoiceHeader.Customers, _
                                                       _InvoiceHeader.DateExecution, Nothing)
        _CS.EndUpdateCriteria()

        e.View = Application.CreateListView("ContractRows_ListView_Invoice", _CS, False)
    End Sub
    Private Sub aInvoiceFromContractsRows_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aInvoiceFromContractsRows.Execute
        '--- Az akció átadja a kiválasztott szerződési sorokat a számla soraihoz-----------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _ContractRows As ContractRows
        Dim _i As Integer = 0 ' segéd változó, hogy csak az első kijelölt sor fizetési és szállítási opcióját rakja be a fejlécbe

        For Each _ContractRows In e.PopupWindow.View.SelectedObjects
            Dim _InvoiceRows As InvoiceRows = _ocur.CreateObject(Of InvoiceRows)()
            _InvoiceRows.InvoiceHeader = _InvoiceHeader
            If _i = 0 Then
                _InvoiceRows.InvoiceHeader.PaymentOption = _ocur.GetObject(_ContractRows.PaymentOption)
                _InvoiceRows.InvoiceHeader.ShipmentOption = _ocur.GetObject(_ContractRows.ShipmentOption)
            End If
            _InvoiceRows.Description = _ocur.GetObject(_ContractRows.Description)
            _InvoiceRows.QTT = _ocur.GetObject(_ContractRows.QTT)
            _InvoiceRows.QualityOption = _ocur.GetObject(_ContractRows.QualityOption)
            _InvoiceRows.VAT = _ocur.GetObject(_ContractRows.VAT)
            _InvoiceRows.Units = _ocur.GetObject(_ContractRows.Units)
            _InvoiceRows.ShortName = _ocur.GetObject(_ContractRows.ShortName)
            _InvoiceRows.ContractRows = _ocur.GetObject(_ContractRows)
            _InvoiceRows.Save()
        Next
        _ocur.CommitChanges()
    End Sub
    Private Sub aPrintSelectedInvoices_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aPrintSelectedInvoices.Execute
        'Meg kell állapítani, hogy a vevő belföldi vagy külföldi.

        Dim _ReportData As ReportData
        Dim _ReportData_DEV As ReportData
        Dim _ReportData_DEV_EU As ReportData
        Dim _InvoiceHeader As InvoiceHeader
        Dim _CurrencyName As CurrencyName
        If e.SelectedObjects.Count > 0 Then
            _InvoiceHeader = TryCast(e.SelectedObjects.Item(0), InvoiceHeader)
            '_InvoiceHeader.RecalculateInvoice()
            _CurrencyName = _InvoiceHeader.CurrencyName
            If _InvoiceHeader IsNot Nothing Then
                _ReportData = _InvoiceHeader.InvoiceType.ReportData
                _ReportData_DEV = _InvoiceHeader.InvoiceType.ReportData_DEV
                _ReportData_DEV_EU = _InvoiceHeader.InvoiceType.ReportData_DEV_EU
                If _ReportData IsNot Nothing Or _InvoiceHeader Is Nothing Then
                    Dim _inoperator As New InOperator("InvoiceHeader", e.SelectedObjects)
                    If _CurrencyName.ShortName = "HUF" Then
                        Dim _ReportServiceController As Reports.ReportServiceController = Frame.GetController(Of Reports.ReportServiceController)()

                        _ReportServiceController.ShowPreview(_ReportData, _inoperator, True)
                    Else
                        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData_DEV, _inoperator, True)
                    End If
                End If
            End If
        End If
    End Sub
    Sub StornoInvoice(ByVal _p_InvoiceHeader As InvoiceHeader)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceRows As InvoiceRows
        Dim _InvoiceRows_New As InvoiceRows
        Dim _InvoiceHeader_New As InvoiceHeader
        Dim _ContractRows_Collection As New XPCollection(Of ContractRows)(_ocur.Session, CriteriaOperator.Parse("IsNull(InvoiceRows)=false"))
        Dim _selection As ArrayList
        Dim _ContractRows As ContractRows


        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        _InvoiceHeader_New = _ocur.CreateObject(Of InvoiceHeader)()
        _InvoiceHeader_New.IsStorno = True : _p_InvoiceHeader.IsStorno = True
        _InvoiceHeader_New.BankInformation = _p_InvoiceHeader.BankInformation
        _InvoiceHeader_New.ConnectInfo = _p_InvoiceHeader.DocumentNumber
        _InvoiceHeader_New.CurrencyName = _p_InvoiceHeader.CurrencyName
        _InvoiceHeader_New.CurrencyRate = _p_InvoiceHeader.CurrencyRate
        _InvoiceHeader_New.Customers = _p_InvoiceHeader.Customers
        _InvoiceHeader_New.CustomersFrom = _p_InvoiceHeader.CustomersFrom
        _InvoiceHeader_New.DateCreated = GetSQLTime(_ocur.Session)
        _InvoiceHeader_New.PaymentOption = _p_InvoiceHeader.PaymentOption
        _InvoiceHeader_New.DateExecution = _p_InvoiceHeader.DateExecution
        _InvoiceHeader_New.DatePayment = _p_InvoiceHeader.DatePayment
        _InvoiceHeader_New.DocumentNumber = _p_InvoiceHeader.InvoiceType.NumberGenerator.GetNewNumber(_p_InvoiceHeader.InvoiceType.NumberGenerator)
        _InvoiceHeader_New.HUNumberToText = _p_InvoiceHeader.HUNumberToText
        _InvoiceHeader_New.InvoiceType = _p_InvoiceHeader.InvoiceType
        _InvoiceHeader_New.IsEditable = False
        _InvoiceHeader_New.IsProforma = False
        _InvoiceHeader_New.QualityOption = _p_InvoiceHeader.QualityOption
        _InvoiceHeader_New.Saved_Customers_Address_City = _p_InvoiceHeader.Saved_Customers_Address_City
        _InvoiceHeader_New.Saved_Customers_Address_Country = _p_InvoiceHeader.Saved_Customers_Address_Country
        _InvoiceHeader_New.Saved_Customers_Address_StateProvince = _p_InvoiceHeader.Saved_Customers_Address_StateProvince
        _InvoiceHeader_New.Saved_Customers_Address_Street = _p_InvoiceHeader.Saved_Customers_Address_Street
        _InvoiceHeader_New.Saved_Customers_Address_ZipPostal = _p_InvoiceHeader.Saved_Customers_Address_ZipPostal
        _InvoiceHeader_New.Saved_Customers_Name = _p_InvoiceHeader.Saved_Customers_Name
        _InvoiceHeader_New.Saved_Customers_VATNumber = _p_InvoiceHeader.Saved_Customers_VATNumber
        _InvoiceHeader_New.Saved_Customers_VATNumberEU = _p_InvoiceHeader.Saved_Customers_VATNumberEU
        _InvoiceHeader_New.Saved_CustomersFrom_Address_City = _p_InvoiceHeader.Saved_CustomersFrom_Address_City
        _InvoiceHeader_New.Saved_CustomersFrom_Address_Country = _p_InvoiceHeader.Saved_CustomersFrom_Address_Country
        _InvoiceHeader_New.Saved_CustomersFrom_Address_StateProvince = _p_InvoiceHeader.Saved_CustomersFrom_Address_StateProvince
        _InvoiceHeader_New.Saved_CustomersFrom_Address_Street = _p_InvoiceHeader.Saved_CustomersFrom_Address_Street
        _InvoiceHeader_New.Saved_CustomersFrom_Address_ZipPostal = _p_InvoiceHeader.Saved_CustomersFrom_Address_ZipPostal
        _InvoiceHeader_New.Saved_CustomersFrom_Bank_AccountNumber = _p_InvoiceHeader.Saved_CustomersFrom_Bank_AccountNumber
        _InvoiceHeader_New.Saved_CustomersFrom_Bank_IBAN = _p_InvoiceHeader.Saved_CustomersFrom_Bank_IBAN
        _InvoiceHeader_New.Saved_CustomersFrom_Bank_ShortName = _p_InvoiceHeader.Saved_CustomersFrom_Bank_ShortName
        _InvoiceHeader_New.Saved_CustomersFrom_Bank_SWIFT = _p_InvoiceHeader.Saved_CustomersFrom_Bank_SWIFT

        _InvoiceHeader_New.Saved_CustomersFrom_Name = _p_InvoiceHeader.Saved_CustomersFrom_Name
        _InvoiceHeader_New.Saved_CustomersFrom_VATNumber = _p_InvoiceHeader.Saved_CustomersFrom_VATNumber
        _InvoiceHeader_New.Saved_CustomersFrom_VATNumberEU = _p_InvoiceHeader.Saved_CustomersFrom_VATNumberEU

        _InvoiceHeader_New.ShipmentOption = _p_InvoiceHeader.ShipmentOption

        _p_InvoiceHeader.Save()
        _InvoiceHeader_New.Save()

        For Each _InvoiceRows In _p_InvoiceHeader.InvoiceRows
            _InvoiceRows_New = New InvoiceRows(TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session)
            _InvoiceRows_New.Controlling = _InvoiceRows.Controlling
            _InvoiceRows_New.CostHolder = _InvoiceRows.CostHolder
            _InvoiceRows_New.CostPlace = _InvoiceRows.CostPlace
            _InvoiceRows_New.CustomsTariffs = _InvoiceRows.CustomsTariffs
            _InvoiceRows_New.Description = _InvoiceRows.Description
            _InvoiceRows_New.DiscountPriceDEV = _InvoiceRows.DiscountPriceDEV
            _InvoiceRows_New.DiscountPriceHUF = _InvoiceRows.DiscountPriceHUF
            _InvoiceRows_New.InvoiceRowtype = _InvoiceRows.InvoiceRowtype
            _InvoiceRows_New.JobNumberObjects = _InvoiceRows.JobNumberObjects
            _InvoiceRows_New.ListPriceDEV = _InvoiceRows.ListPriceDEV
            _InvoiceRows_New.ListPriceHUF = _InvoiceRows.ListPriceHUF
            _InvoiceRows_New.QTT = _InvoiceRows.QTT * (-1)
            _InvoiceRows_New.QualityOption = _InvoiceRows.QualityOption
            _InvoiceRows_New.RowIndex = _InvoiceRows.RowIndex
            _InvoiceRows_New.ShortName = _InvoiceRows.ShortName
            _InvoiceRows_New.UniqueObjects = _InvoiceRows.UniqueObjects
            _InvoiceRows_New.Units = _InvoiceRows.Units
            _InvoiceRows_New.VAT = _InvoiceRows.VAT
            If _InvoiceRows.ContractRows IsNot Nothing Then
                _InvoiceRows_New.ContractRows = _InvoiceRows.ContractRows
            End If
            If _InvoiceRows.OrderInRows IsNot Nothing Then
                _InvoiceRows_New.OrderInRows = _InvoiceRows.OrderInRows
                _InvoiceRows.OrderInRows.QTT_Invoiced -= _InvoiceRows.QTT
            End If
            If _InvoiceRows.OrderInRowsOther IsNot Nothing Then
                _InvoiceRows_New.OrderInRowsOther = _InvoiceRows.OrderInRowsOther
                _InvoiceRows.OrderInRowsOther.QTT_Invoiced -= _InvoiceRows.QTT
            End If
            If _InvoiceRows.DeliveryNoteOutRows IsNot Nothing Then
                _InvoiceRows_New.DeliveryNoteOutRows = _InvoiceRows.DeliveryNoteOutRows
                _InvoiceRows.DeliveryNoteOutRows.QTT_Invoiced -= _InvoiceRows.QTT
            End If
            _InvoiceRows_New.InvoiceHeader = _InvoiceHeader_New
            '------- Ezek hiányoztak 2011.06.20 ----------------------------
            _InvoiceRows_New.ChartOfAccounts = _InvoiceRows.ChartOfAccounts
            _InvoiceRows_New.InvoiceRows = _InvoiceRows.InvoiceRows
            _InvoiceRows_New.InvoiceRowsAdvance = _InvoiceRows.InvoiceRowsAdvance
            _InvoiceRows_New.OrderInRowsOther = _InvoiceRows.OrderInRowsOther
            '--------------------------------------------------------------------------
            _InvoiceRows_New.Save()
        Next
        _ocur.CommitChanges()
        _InvoiceHeader_New.RecalculateInvoice(_InvoiceHeader_New, _ocur)
        _ocur.CommitChanges()
        '-- Szerződési sorokról visszavonni, hogy számlázva vannak ! ------------------------------------------------------------------------------
        _selection = New ArrayList()


        Dim _inoperator As New InOperator("InvoiceRows", _p_InvoiceHeader.InvoiceRows)
        _ContractRows_Collection.Filter = _inoperator
        For Each _ContractRows In _ContractRows_Collection
            _selection.Add(_ContractRows)
        Next

        For Each _ContractRows In _selection
            _ContractRows.InvoiceRows = Nothing
            _ContractRows.Save()
        Next
        _ocur.CommitChanges()
        '------------------------------------------------------------------------------------------------------------------------------------------
        Me.PrintInvoice(_InvoiceHeader_New)

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
        If TryCast(View, DetailView) IsNot Nothing Then
            View.Close(False)
        Else
            View.Refresh()
        End If

    End Sub
    Sub FinalizeInvoice(ByVal _p_InvoiceHeader As InvoiceHeader, _View As DevExpress.ExpressApp.View, _ocur As Xpo.XPObjectSpace)
        On Error GoTo CheckError
        If _View.Id = "InvoiceHeader_DetailView_Corrigendum" Then
            Dim _ActTotalNetto As Double = 0
            For Each _InvoiceRows As InvoiceRows In _p_InvoiceHeader.InvoiceRows
                If _p_InvoiceHeader.CurrencyName.ShortName = "HUF" Then
                    _ActTotalNetto += _InvoiceRows.PriceHUF * _InvoiceRows.QTT
                Else
                    _ActTotalNetto += _InvoiceRows.PriceDEV * _InvoiceRows.QTT
                End If
            Next
            If _ActTotalNetto > 0 Then
                _p_InvoiceHeader.DateExecution = _p_InvoiceHeader.InvoiceHeader.DateExecution
            ElseIf _ActTotalNetto < 0 Then
            Else
            End If
        End If

        If _p_InvoiceHeader.InvoiceRows Is Nothing Then
            _ocur.Rollback()
            Exit Sub
        End If

        If _p_InvoiceHeader.InvoiceRows.Count <= 0 Then
            _ocur.Rollback()
            Exit Sub
        End If

        _p_InvoiceHeader.IsEditable = False
        _p_InvoiceHeader.IsProforma = False
        _p_InvoiceHeader.ConnectInfo = _p_InvoiceHeader.InvoiceType.NumberGenerator.GetNewNumber(_p_InvoiceHeader.InvoiceType.NumberGenerator)
        _p_InvoiceHeader.DocumentNumber = _p_InvoiceHeader.ConnectInfo

        '// Ha a számla helyesbítő, akkor a helyesbített számla száma lesz a pontozási azonosító
        If _p_InvoiceHeader.IsCorrigendum Then
            _p_InvoiceHeader.ConnectInfo = _p_InvoiceHeader.InvoiceHeader.DocumentNumber
        End If
        _p_InvoiceHeader.DateCreated = GetSQLTime(TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session)

        ' -- Ha folyamatos teljesítésű számla akkor a fizetési határidő megegyezik a teljesítés dátumával
        If _p_InvoiceHeader.PaymentOption.PayMode = ePayMode.InContinuous Then
            _p_InvoiceHeader.DatePayment = _p_InvoiceHeader.DateExecution
        Else
            ' -- Egyébként számolja ki a fizetési opcióból, hogy hány nap van a kiállítás dátuma és a fizetési határidő között
            If _p_InvoiceHeader.PaymentOption.PayDay = 0 Then
            Else
                Select Case _p_InvoiceHeader.DatePaymentMode
                    Case eDatePaymentMode.eDateCreated
                        _p_InvoiceHeader.DatePayment = _p_InvoiceHeader.DateCreated.AddDays(_p_InvoiceHeader.PaymentOption.PayDay)
                    Case eDatePaymentMode.eDateExecution
                        _p_InvoiceHeader.DatePayment = _p_InvoiceHeader.DateExecution.AddDays(_p_InvoiceHeader.PaymentOption.PayDay)
                End Select
            End If
        End If

        ' -- Ha előleg típusú számla akkor a teljesítés és a fizetési határidő is megegyezik
        If _p_InvoiceHeader.InvoiceType.InvoiceTypeBase = eInvoiceTypeBase.eAdvancePayment Then
            _p_InvoiceHeader.DatePayment = _p_InvoiceHeader.DateExecution
        End If

        If _p_InvoiceHeader.Customers.Name = "" Then
            _p_InvoiceHeader.Customers.Name = _p_InvoiceHeader.Customers.FullName
        End If

        _p_InvoiceHeader.Saved_Customers_Name = _p_InvoiceHeader.Customers.Name.ToString
        _p_InvoiceHeader.Saved_Customers_Address_Country = _p_InvoiceHeader.Customers.Address1.Country.ToString
        _p_InvoiceHeader.Saved_Customers_Address_ZipPostal = _p_InvoiceHeader.Customers.Address1.ZipPostal.ToString
        _p_InvoiceHeader.Saved_Customers_Address_Street = _p_InvoiceHeader.Customers.Address1.Street.ToString
        _p_InvoiceHeader.Saved_Customers_Address_City = _p_InvoiceHeader.Customers.Address1.City.ToString
        _p_InvoiceHeader.Saved_Customers_Address_StateProvince = _p_InvoiceHeader.Customers.Address1.StateProvince.ToString
        _p_InvoiceHeader.Saved_Customers_VATNumber = _p_InvoiceHeader.Customers.VATNumber.ToString
        _p_InvoiceHeader.Saved_Customers_VATNumberEU = _p_InvoiceHeader.Customers.VATNumberEU.ToString

        _p_InvoiceHeader.Saved_CustomersFrom_Name = _p_InvoiceHeader.CustomersFrom.Name.ToString
        _p_InvoiceHeader.Saved_CustomersFrom_Address_Country = _p_InvoiceHeader.CustomersFrom.Address1.Country.ToString
        _p_InvoiceHeader.Saved_CustomersFrom_Address_ZipPostal = _p_InvoiceHeader.CustomersFrom.Address1.ZipPostal.ToString
        _p_InvoiceHeader.Saved_CustomersFrom_Address_Street = _p_InvoiceHeader.CustomersFrom.Address1.Street.ToString
        _p_InvoiceHeader.Saved_CustomersFrom_Address_City = _p_InvoiceHeader.CustomersFrom.Address1.City.ToString
        _p_InvoiceHeader.Saved_CustomersFrom_Address_StateProvince = _p_InvoiceHeader.CustomersFrom.Address1.StateProvince.ToString
        _p_InvoiceHeader.Saved_CustomersFrom_VATNumber = _p_InvoiceHeader.CustomersFrom.VATNumber.ToString
        _p_InvoiceHeader.Saved_CustomersFrom_VATNumberEU = _p_InvoiceHeader.CustomersFrom.VATNumberEU.ToString

        _p_InvoiceHeader.Saved_CustomersFrom_Bank_ShortName = _p_InvoiceHeader.BankInformation.FullBankAccount.ToString
        _p_InvoiceHeader.Saved_CustomersFrom_Bank_IBAN = _p_InvoiceHeader.BankInformation.IBAN.ToString
        _p_InvoiceHeader.Saved_CustomersFrom_Bank_SWIFT = _p_InvoiceHeader.BankInformation.SWIFT.ToString
        _p_InvoiceHeader.Saved_CustomersFrom_Bank_AccountNumber = _p_InvoiceHeader.BankInformation.AccountNumber.ToString

        _ocur.CommitChanges()
        _p_InvoiceHeader.RecalculateInvoice(_p_InvoiceHeader, _ocur)
        _ocur.CommitChanges()


        Dim _uow_pci As New UnitOfWork(_ocur.Session.DataLayer)
        GLFunctions.ReconfigurePCI(_uow_pci, _p_InvoiceHeader.CustomersFrom, _p_InvoiceHeader.Customers, 0, _p_InvoiceHeader.ConnectInfo)

        Exit Sub
CheckError:
        If Err.Number = 91 Then Resume Next
        If Err.Number = 13 Then Resume Next

    End Sub
    Private Sub aCreateProforma_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateProforma.Execute
        Dim _InvoiceHeader As InvoiceHeader
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _NG As NumberGenerator

        If Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).SaveAction.Enabled Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController).SaveAction.DoExecute()
        End If

        On Error GoTo CheckError

        For Each _InvoiceHeader In e.SelectedObjects
            If _InvoiceHeader.PaymentOption.PayMode = ePayMode.InContinuous Then
                _InvoiceHeader.DatePayment = _InvoiceHeader.DateExecution
            Else
                If _InvoiceHeader.PaymentOption.PayDay = 0 Then
                Else
                    Select Case _InvoiceHeader.DatePaymentMode
                        Case eDatePaymentMode.eDateExecution
                            _InvoiceHeader.DatePayment = _InvoiceHeader.DateExecution.AddDays(_InvoiceHeader.PaymentOption.PayDay)
                        Case eDatePaymentMode.eDateCreated
                            _InvoiceHeader.DatePayment = _InvoiceHeader.DateCreated.AddDays(_InvoiceHeader.PaymentOption.PayDay)
                    End Select
                End If
            End If
            If _InvoiceHeader.InvoiceType.NumberGeneratorProforma IsNot Nothing Then
                _NG = _InvoiceHeader.InvoiceType.NumberGeneratorProforma
                _InvoiceHeader.DocumentNumberProforma = _NG.GetNewNumber(_NG)
            End If
            _InvoiceHeader.IsProforma = True
            _InvoiceHeader.IsEditable = False

            _InvoiceHeader.Saved_Customers_Name = _InvoiceHeader.Customers.Name.ToString
            _InvoiceHeader.Saved_Customers_Address_Country = _InvoiceHeader.Customers.Address1.Country.ToString
            _InvoiceHeader.Saved_Customers_Address_ZipPostal = _InvoiceHeader.Customers.Address1.ZipPostal.ToString
            _InvoiceHeader.Saved_Customers_Address_Street = _InvoiceHeader.Customers.Address1.Street.ToString
            _InvoiceHeader.Saved_Customers_Address_City = _InvoiceHeader.Customers.Address1.City.ToString
            _InvoiceHeader.Saved_Customers_Address_StateProvince = _InvoiceHeader.Customers.Address1.StateProvince.ToString
            _InvoiceHeader.Saved_Customers_VATNumber = _InvoiceHeader.Customers.VATNumber.ToString
            _InvoiceHeader.Saved_Customers_VATNumberEU = _InvoiceHeader.Customers.VATNumberEU.ToString

            _InvoiceHeader.Saved_CustomersFrom_Name = _InvoiceHeader.CustomersFrom.Name.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_ZipPostal = _InvoiceHeader.CustomersFrom.Address1.ZipPostal.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_Street = _InvoiceHeader.CustomersFrom.Address1.Street.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_City = _InvoiceHeader.CustomersFrom.Address1.City.ToString
            _InvoiceHeader.Saved_CustomersFrom_Address_StateProvince = _InvoiceHeader.CustomersFrom.Address1.StateProvince.ToString
            _InvoiceHeader.Saved_CustomersFrom_VATNumber = _InvoiceHeader.CustomersFrom.VATNumber.ToString
            _InvoiceHeader.Saved_CustomersFrom_VATNumberEU = _InvoiceHeader.CustomersFrom.VATNumberEU.ToString

            _InvoiceHeader.Saved_CustomersFrom_Bank_ShortName = _InvoiceHeader.BankInformation.FullBankAccount.ToString
            _InvoiceHeader.Saved_CustomersFrom_Bank_IBAN = _InvoiceHeader.BankInformation.IBAN.ToString
            _InvoiceHeader.Saved_CustomersFrom_Bank_SWIFT = _InvoiceHeader.BankInformation.SWIFT.ToString
            _InvoiceHeader.Saved_CustomersFrom_Bank_AccountNumber = _InvoiceHeader.BankInformation.AccountNumber.ToString

            _ocur.CommitChanges()
            _InvoiceHeader.RecalculateInvoice(_InvoiceHeader, _ocur)
            _ocur.CommitChanges()

        Next
        _ocur.CommitChanges()

        If e.SelectedObjects.Count = 1 Then
            _InvoiceHeader = TryCast(e.SelectedObjects(0), InvoiceHeader)
            Me.PrintInvoice(_InvoiceHeader)
            If TryCast(View, DetailView) IsNot Nothing Then
                View.Close()
            End If
        End If
        Exit Sub
CheckError:
        If Err.Number = 91 Then Resume Next
        If Err.Number = 13 Then Resume Next
    End Sub
    Private Sub aCheckConnectInfo_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
        Dim _GLRows As GLRows = View.ObjectSpace.CreateObject(Of GLRows)()
        Dim _InvoiceHeader As InvoiceHeader = Nothing


    End Sub
    Sub PrintInvoice(ByVal _InvoiceHeader As InvoiceHeader)
        '-- Kinyomtat egy számlát
        Dim _ReportData As ReportData
        Dim _ReportData_DEV As ReportData
        Dim _ReportData_DEV_EU As ReportData
        Dim _CurrencyName As CurrencyName
        _CurrencyName = _InvoiceHeader.CurrencyName
        If _InvoiceHeader IsNot Nothing Then
            _ReportData = _InvoiceHeader.InvoiceType.ReportData
            _ReportData_DEV = _InvoiceHeader.InvoiceType.ReportData_DEV
            _ReportData_DEV_EU = _InvoiceHeader.InvoiceType.ReportData_DEV_EU
            If _ReportData IsNot Nothing Or _InvoiceHeader Is Nothing Then
                If _CurrencyName.ShortName = "HUF" Then
                    Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("InvoiceHeader=?", _InvoiceHeader), True)
                Else
                    Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData_DEV, CriteriaOperator.Parse("InvoiceHeader=?", _InvoiceHeader), True)
                End If

            End If
        End If
    End Sub
    Private Sub aCreateInvoice_Advance_Proforma_CustomizePopupWindowParams(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateInvoice_Advance_Proforma.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _InvoiceAdvancePopup As InvoiceAdvancePopup = _onew.CreateObject(Of InvoiceAdvancePopup)()

        e.View = Application.CreateDetailView(_onew, "InvoiceAdvancePopup_DetailView", False, _InvoiceAdvancePopup)
    End Sub
    Private Sub aCreateInvoice_Advance_Proforma_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateInvoice_Advance_Proforma.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _InvoiceAdvancePopup As InvoiceAdvancePopup = TryCast(e.PopupWindow.View.SelectedObjects(0), InvoiceAdvancePopup)
        Dim _InvoiceHeader_New As InvoiceHeader = _ocur.CreateObject(Of InvoiceHeader)()
        Dim _InvoiceRows As InvoiceRows = _ocur.CreateObject(Of InvoiceRows)()
        Dim _NG As NumberGenerator

        On Error GoTo CheckError

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)

        _InvoiceHeader_New.IsProforma = True
        _InvoiceHeader_New.IsEditable = False
        _InvoiceHeader_New.CurrencyName = _ocur.GetObject(_InvoiceAdvancePopup.CurrencyName)

        _InvoiceHeader_New.CurrencyRate = _ocur.GetObject(_InvoiceAdvancePopup.CurrencyRate)
        _InvoiceHeader_New.Customers = _ocur.GetObject(_InvoiceAdvancePopup.Customers)
        _InvoiceHeader_New.CustomersFrom = _ocur.GetObject(_InvoiceAdvancePopup.CustomersFrom)
        _InvoiceHeader_New.DateExecution = _ocur.GetObject(_InvoiceAdvancePopup.DateExecution)
        _InvoiceHeader_New.DatePayment = _ocur.GetObject(_InvoiceAdvancePopup.DatePayment)
        _InvoiceHeader_New.PaymentOption = _ocur.GetObject(_InvoiceAdvancePopup.PaymentOption)
        _InvoiceHeader_New.Description = _InvoiceAdvancePopup.Description

        If _InvoiceHeader_New.PaymentOption.PayMode = ePayMode.InTransfer Then
            _InvoiceHeader_New.BankInformation = _ocur.GetObject(_InvoiceAdvancePopup.BankInformation)
        End If
        _InvoiceHeader_New.InvoiceType = _ocur.GetObject(_InvoiceAdvancePopup.InvoiceType)
        If _InvoiceHeader_New.InvoiceType.NumberGeneratorProforma IsNot Nothing Then
            _NG = _InvoiceHeader_New.InvoiceType.NumberGeneratorProforma
            _InvoiceHeader_New.DocumentNumberProforma = _NG.GetNewNumber(_NG)
        End If
        If _InvoiceHeader_New.CurrencyName.ShortName = "HUF" Then
            _InvoiceHeader_New.TotalBruttoHUF = _ocur.GetObject(_InvoiceAdvancePopup.AmountHUF)
        Else
            _InvoiceHeader_New.TotalBruttoDEV = _InvoiceAdvancePopup.AmountDEV
            _InvoiceHeader_New.TotalBruttoHUF = _InvoiceAdvancePopup.AmountHUF
        End If

        _InvoiceHeader_New.Saved_Customers_Name = _InvoiceHeader_New.Customers.Name.ToString
        _InvoiceHeader_New.Saved_Customers_Address_Country = _InvoiceHeader_New.Customers.Address1.Country.ToString
        _InvoiceHeader_New.Saved_Customers_Address_ZipPostal = _InvoiceHeader_New.Customers.Address1.ZipPostal.ToString
        _InvoiceHeader_New.Saved_Customers_Address_Street = _InvoiceHeader_New.Customers.Address1.Street.ToString
        _InvoiceHeader_New.Saved_Customers_Address_City = _InvoiceHeader_New.Customers.Address1.City.ToString
        _InvoiceHeader_New.Saved_Customers_Address_StateProvince = _InvoiceHeader_New.Customers.Address1.StateProvince.ToString
        _InvoiceHeader_New.Saved_Customers_VATNumber = _InvoiceHeader_New.Customers.VATNumber.ToString
        _InvoiceHeader_New.Saved_Customers_VATNumberEU = _InvoiceHeader_New.Customers.VATNumberEU.ToString

        _InvoiceHeader_New.Saved_CustomersFrom_Name = _InvoiceHeader_New.CustomersFrom.Name.ToString
        _InvoiceHeader_New.Saved_CustomersFrom_Address_ZipPostal = _InvoiceHeader_New.CustomersFrom.Address1.ZipPostal.ToString
        _InvoiceHeader_New.Saved_CustomersFrom_Address_Street = _InvoiceHeader_New.CustomersFrom.Address1.Street.ToString
        _InvoiceHeader_New.Saved_CustomersFrom_Address_City = _InvoiceHeader_New.CustomersFrom.Address1.City.ToString
        _InvoiceHeader_New.Saved_CustomersFrom_Address_StateProvince = _InvoiceHeader_New.CustomersFrom.Address1.StateProvince.ToString
        _InvoiceHeader_New.Saved_CustomersFrom_VATNumber = _InvoiceHeader_New.CustomersFrom.VATNumber.ToString
        _InvoiceHeader_New.Saved_CustomersFrom_VATNumberEU = _InvoiceHeader_New.CustomersFrom.VATNumberEU.ToString

        _InvoiceHeader_New.Saved_CustomersFrom_Bank_ShortName = _InvoiceHeader_New.BankInformation.FullBankAccount.ToString
        _InvoiceHeader_New.Saved_CustomersFrom_Bank_IBAN = _InvoiceHeader_New.BankInformation.IBAN.ToString
        _InvoiceHeader_New.Saved_CustomersFrom_Bank_SWIFT = _InvoiceHeader_New.BankInformation.SWIFT.ToString
        _InvoiceHeader_New.Saved_CustomersFrom_Bank_AccountNumber = _InvoiceHeader_New.BankInformation.AccountNumber.ToString


        _InvoiceRows.InvoiceHeader = _ocur.GetObject(_InvoiceHeader_New)
        _InvoiceRows.QTT = 1

        _InvoiceRows.VAT = _ocur.GetObject(_InvoiceAdvancePopup.VAT)
        _InvoiceRows.ShortName = "Előleg"

        If _InvoiceHeader_New.CurrencyName.ShortName = "HUF" Then
            If _InvoiceRows.VAT IsNot Nothing Then
                _InvoiceRows.ListPriceHUF = Math.Round(_InvoiceAdvancePopup.AmountHUF / (100 + _InvoiceRows.VAT.KeyValuePercent) * 100, 0, MidpointRounding.AwayFromZero)
            Else
                _InvoiceRows.ListPriceHUF = _InvoiceAdvancePopup.AmountHUF
            End If
        Else
            If _InvoiceRows.VAT IsNot Nothing Then
                _InvoiceRows.ListPriceDEV = Math.Round(_InvoiceAdvancePopup.AmountDEV / (100 + _InvoiceRows.VAT.KeyValuePercent) * 100, 0, MidpointRounding.AwayFromZero)
                _InvoiceRows.ListPriceHUF = _InvoiceRows.ListPriceDEV * _InvoiceAdvancePopup.CurrencyRate
            Else
                _InvoiceRows.ListPriceDEV = _InvoiceAdvancePopup.AmountDEV
                _InvoiceRows.ListPriceHUF = _InvoiceAdvancePopup.AmountHUF
            End If
        End If
        _InvoiceRows.Units = _ocur.FindObject(Of Units)(CriteriaOperator.Parse("ShortName = 'DB'"))

        If _InvoiceRows.Units Is Nothing Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Units As Units = _onew.CreateObject(Of Units)()
            _Units.ShortName = "DB"
            _InvoiceRows.Units = _Units
        End If
        _InvoiceRows.Description = _InvoiceAdvancePopup.Description
        _InvoiceRows.Controlling = _ocur.GetObject(_InvoiceAdvancePopup.Controlling)
        _InvoiceRows.CostHolder = _ocur.GetObject(_InvoiceAdvancePopup.CostHolder)
        _InvoiceRows.CostPlace = _ocur.GetObject(_InvoiceAdvancePopup.CostPlace)
        _InvoiceRows.UniqueObjects = _ocur.GetObject(_InvoiceAdvancePopup.UniqueObjects)
        _InvoiceRows.JobNumberObjects = _ocur.GetObject(_InvoiceAdvancePopup.JobNumberObjects)

        _ocur.CommitChanges()
        _InvoiceHeader_New.RecalculateInvoice(_InvoiceHeader_New, _ocur)
        _ocur.CommitChanges()
        Me.PrintInvoice(_InvoiceHeader_New)
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
        View.Close()
        Exit Sub
CheckError:
        If Err.Number = 91 Then Resume Next
        If Err.Number = 13 Then Resume Next
    End Sub
    Private Sub aMakeEditableProforma_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aMakeEditableProforma.Execute
        ' -- Az akció egy kiválasztott proforma számlát állít vissza szerkeszthetőre, ha az nem szerződésből származik -----------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _IsFromContracts As Boolean = False
        Dim _InvoiceRows As InvoiceRows
        Dim _selection As ArrayList = New ArrayList()
        If _InvoiceHeader Is Nothing Then Exit Sub

        _selection.Clear()

        For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
            If _InvoiceRows.ContractRows IsNot Nothing Then
                _IsFromContracts = True
            End If
        Next

        If _IsFromContracts = False Then
            _InvoiceHeader.IsEditable = True
            _InvoiceHeader.IsProforma = False

            For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
                If _InvoiceRows.InvoiceRowsAdvance IsNot Nothing Then
                    _InvoiceRows.InvoiceRowsAdvance.InvoiceRowsAdvance = Nothing
                    _selection.Add(_InvoiceRows)
                    _InvoiceRows.Save()
                End If
            Next
            _InvoiceHeader.Save()
            _ocur.CommitChanges()

            _ocur.Delete(_selection)
            _ocur.CommitChanges()
        End If
    End Sub
    Private Sub aCreateInvoice_Final_Proforma_CustomizePopupWindowParams(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateInvoice_Final_Proforma.CustomizePopupWindowParams
        ' Létrehoz egy szűrt ListView-t ahol a számlához tartozó előlegsorokat lehet kijelölni
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _CS As New CollectionSource(_onew, GetType(InvoiceRows))

        _CS.BeginUpdateCriteria()
        _CS.Criteria.Clear()
        _CS.Criteria("First") = CriteriaOperator.Parse("InvoiceHeader.Customers=? And InvoiceHeader.CustomersFrom=? And InvoiceHeader.InvoiceType.InvoiceTypeBase in (0,1) And IsNull(InvoiceRowsAdvance)=True And InvoiceHeader.IsEditable=False And InvoiceHeader.IsStorno=False And InvoiceHeader.IsProforma=False And InvoiceHeader.CurrencyName.ShortName=?" _
                                                       , _onew.GetObject(_InvoiceHeader.Customers), _onew.GetObject(_InvoiceHeader.CustomersFrom), _onew.GetObject(_InvoiceHeader.CurrencyName.ShortName))
        _CS.EndUpdateCriteria()

        e.View = Application.CreateListView("InvoiceRows_ListView_Advanced", _CS, False)
    End Sub
    Private Sub aCreateInvoice_Final_Proforma_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateInvoice_Final_Proforma.Execute
        ' Az akció egy szerkeszthető kimenő számlából, egy proforma számlát készít, amihez ha van, akkor hozzáadja a kiválasztott előleg sorokat.
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _InvoiceRows As InvoiceRows
        Dim _InvoiceRows_New As InvoiceRows
        Dim _NG As NumberGenerator
        Dim _ReportData As ReportData

        If _InvoiceHeader.InvoiceType.NumberGeneratorProforma IsNot Nothing Then
            _NG = _InvoiceHeader.InvoiceType.NumberGeneratorProforma
            _InvoiceHeader.DocumentNumberProforma = _NG.GetNewNumber(_NG)
        End If

        _InvoiceHeader.IsProforma = True
        _InvoiceHeader.IsEditable = False

        On Error GoTo CheckError
        _InvoiceHeader.Saved_Customers_Name = _InvoiceHeader.Customers.Name.ToString
        _InvoiceHeader.Saved_Customers_Address_Country = _InvoiceHeader.Customers.Address1.Country.ToString
        _InvoiceHeader.Saved_Customers_Address_ZipPostal = _InvoiceHeader.Customers.Address1.ZipPostal.ToString
        _InvoiceHeader.Saved_Customers_Address_Street = _InvoiceHeader.Customers.Address1.Street.ToString
        _InvoiceHeader.Saved_Customers_Address_City = _InvoiceHeader.Customers.Address1.City.ToString
        _InvoiceHeader.Saved_Customers_Address_StateProvince = _InvoiceHeader.Customers.Address1.StateProvince.ToString
        _InvoiceHeader.Saved_Customers_VATNumber = _InvoiceHeader.Customers.VATNumber.ToString
        _InvoiceHeader.Saved_Customers_VATNumberEU = _InvoiceHeader.Customers.VATNumberEU.ToString

        _InvoiceHeader.Saved_CustomersFrom_Name = _InvoiceHeader.CustomersFrom.Name.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_ZipPostal = _InvoiceHeader.CustomersFrom.Address1.ZipPostal.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_Street = _InvoiceHeader.CustomersFrom.Address1.Street.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_City = _InvoiceHeader.CustomersFrom.Address1.City.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_StateProvince = _InvoiceHeader.CustomersFrom.Address1.StateProvince.ToString
        _InvoiceHeader.Saved_CustomersFrom_VATNumber = _InvoiceHeader.CustomersFrom.VATNumber.ToString
        _InvoiceHeader.Saved_CustomersFrom_VATNumberEU = _InvoiceHeader.CustomersFrom.VATNumberEU.ToString

        _InvoiceHeader.Saved_CustomersFrom_Bank_ShortName = _InvoiceHeader.BankInformation.FullBankAccount.ToString
        _InvoiceHeader.Saved_CustomersFrom_Bank_IBAN = _InvoiceHeader.BankInformation.IBAN.ToString
        _InvoiceHeader.Saved_CustomersFrom_Bank_SWIFT = _InvoiceHeader.BankInformation.SWIFT.ToString
        _InvoiceHeader.Saved_CustomersFrom_Bank_AccountNumber = _InvoiceHeader.BankInformation.AccountNumber.ToString


        For Each _InvoiceRows In e.PopupWindow.View.SelectedObjects ' A popup-on kiválasztott előleg sorok hozzáadása
            _InvoiceRows_New = _ocur.CreateObject(Of InvoiceRows)()

            _InvoiceRows_New.InvoiceHeader = _InvoiceHeader
            _InvoiceRows_New.QTT = _InvoiceRows.QTT * -1
            _InvoiceRows_New.InvoiceRowsAdvance = _ocur.GetObject(_InvoiceRows)
            _InvoiceRows_New.ContractRows = _ocur.GetObject(_InvoiceRows.ContractRows)
            _InvoiceRows_New.Controlling = _ocur.GetObject(_InvoiceRows.Controlling)
            _InvoiceRows_New.CostHolder = _ocur.GetObject(_InvoiceRows.CostHolder)
            _InvoiceRows_New.CostPlace = _ocur.GetObject(_InvoiceRows.CostPlace)
            _InvoiceRows_New.CustomsTariffs = _ocur.GetObject(_InvoiceRows.CustomsTariffs)
            _InvoiceRows_New.Description = _ocur.GetObject(_InvoiceRows.Description)
            _InvoiceRows_New.DiscountPriceDEV = _ocur.GetObject(_InvoiceRows.DiscountPriceDEV)
            _InvoiceRows_New.DiscountPriceHUF = _ocur.GetObject(_InvoiceRows.DiscountPriceHUF)
            _InvoiceRows_New.InvoiceRowtype = _ocur.GetObject(_InvoiceRows.InvoiceRowtype)
            _InvoiceRows_New.JobNumberObjects = _ocur.GetObject(_InvoiceRows.JobNumberObjects)
            _InvoiceRows_New.ListPriceDEV = _ocur.GetObject(_InvoiceRows.ListPriceDEV)
            _InvoiceRows_New.ListPriceHUF = _ocur.GetObject(_InvoiceRows.ListPriceHUF)
            _InvoiceRows_New.QualityOption = _ocur.GetObject(_InvoiceRows.QualityOption)
            _InvoiceRows_New.ShortName = _ocur.GetObject(_InvoiceRows.ShortName)
            _InvoiceRows_New.UniqueObjects = _ocur.GetObject(_InvoiceRows.UniqueObjects)
            _InvoiceRows_New.Units = _ocur.GetObject(_InvoiceRows.Units)
            _InvoiceRows_New.VAT = _ocur.GetObject(_InvoiceRows.VAT)

        Next
        _ocur.CommitChanges()
        _InvoiceHeader.RecalculateInvoice(_InvoiceHeader, _ocur) ' A sorok összegeinek újraszámolása
        _ocur.CommitChanges()

        Me.PrintInvoice(_InvoiceHeader)

        Exit Sub
CheckError:
        If Err.Number = 91 Then Resume Next
        If Err.Number = 13 Then Resume Next
    End Sub
    Private Sub aUp_InvoiceRow_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
        ' A kiválaszott sort egy sorral feljebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.CurrentObject, InvoiceHeader)
        Dim _InvoiceRows_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("InvoiceRows") ' Bekeresi az összes bejövő megrendelési sort
        Dim _InvoiceRowsSelected As InvoiceRows
        Dim _InvoiceRowsMinus As InvoiceRows = Nothing
        Dim _Invoicerows As InvoiceRows

        Try
            _InvoiceRowsSelected = TryCast(_InvoiceRows_ListEditor.ListView.SelectedObjects(0), InvoiceRows) ' Kiveszi a kijelölt sorok közül az elsőt
        Catch
            ' TESZT MIATT!!!! ----------------------------------------------------------------
            Throw New Exception("*Nincs sor kiválasztva, vagy nem a sor kijelölése az aktív!")
        End Try

        If _InvoiceRowsSelected.RowIndex > 0 Then
            For Each _Invoicerows In _InvoiceHeader.InvoiceRows
                If _Invoicerows.RowIndex = _InvoiceRowsSelected.RowIndex - 1 Then
                    _InvoiceRowsMinus = _Invoicerows
                    Exit For
                End If
            Next

            If _InvoiceRowsMinus IsNot Nothing Then
                _InvoiceRowsSelected.RowIndex -= 1
                _InvoiceRowsMinus.RowIndex += 1
            End If
            _InvoiceRowsMinus.Save()
            _InvoiceRowsSelected.Save()
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aDown_InvoiceRow_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
        ' A kiválaszott sort egy sorral lejebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.CurrentObject, InvoiceHeader)
        Dim _InvoiceRows_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("InvoiceRows") ' Bekeresi az összes bejövő megrendelési sort
        Dim _InvoiceRowsSelected As InvoiceRows
        Dim _InvoiceRowPlus As InvoiceRows = Nothing
        Dim _InvoiceRows As InvoiceRows

        Try
            _InvoiceRowsSelected = TryCast(_InvoiceRows_ListEditor.ListView.SelectedObjects(0), InvoiceRows) ' Kiveszi a kijelölt sorok közül az elsőt
        Catch
            ' TESZT MIATT!!!! ----------------------------------------------------------------
            Throw New Exception("*Nincs sor kiválasztva, vagy nem a sor kijelölése az aktív!")
        End Try

        If _InvoiceRowsSelected.RowIndex < _InvoiceHeader.InvoiceRows.Count - 1 Then
            For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
                If _InvoiceRows.RowIndex = _InvoiceRowsSelected.RowIndex + 1 Then
                    _InvoiceRowPlus = _InvoiceRows
                    Exit For
                End If
            Next

            If _InvoiceRowPlus IsNot Nothing Then
                _InvoiceRowsSelected.RowIndex += 1
                _InvoiceRowPlus.RowIndex -= 1
            End If
            _InvoiceRowPlus.Save()
            _InvoiceRowsSelected.Save()
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aDeleteSelectedInvoiceRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aDeleteSelectedInvoiceRows.Execute
        ' Az akció a számla kiválasztott sorait törli --------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = e.SelectedObjects(0).InvoiceHeader
        Dim _InvoiceRows_Collection As ArrayList = New ArrayList

        For Each _InvoiceRows As InvoiceRows In e.SelectedObjects ' Végigmegy a fejléchez tartozó összes soron amit nem törlünk, és sorrendbe állítja a sorszámokat
            Dim _InvRows As InvoiceRows = _InvoiceRows
            For Each _InvoiceRows2 As InvoiceRows In _InvoiceHeader.InvoiceRows
                If _InvoiceRows2.RowIndex > _InvRows.RowIndex Then
                    _InvoiceRows2.RowIndex -= 1
                End If
            Next
        Next

        For Each _InvoiceRows As InvoiceRows In e.SelectedObjects
            _InvoiceRows_Collection.Add(_InvoiceRows)
        Next

        _ocur.Delete(_InvoiceRows_Collection)
        View.Refresh()
    End Sub
    Private Sub aFinalizeInvoiceAdvance_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aFinalizeInvoiceAdvance.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _InvoiceRows As InvoiceRows
        Dim _InvoiceAdvancePopup As InvoiceAdvancePopup

        _InvoiceAdvancePopup = _onew.CreateObject(Of InvoiceAdvancePopup)()
        _InvoiceAdvancePopup.IsEditable = False
        _InvoiceAdvancePopup.CustomersFrom = _onew.GetObject(_InvoiceHeader.CustomersFrom)
        _InvoiceAdvancePopup.Customers = _onew.GetObject(_InvoiceHeader.Customers)
        _InvoiceAdvancePopup.InvoiceType = _onew.GetObject(_InvoiceHeader.InvoiceType)
        _InvoiceAdvancePopup.CurrencyName = _onew.GetObject(_InvoiceHeader.CurrencyName)
        _InvoiceAdvancePopup.CurrencyRate = _InvoiceHeader.CurrencyRate
        _InvoiceAdvancePopup.AmountHUF = _InvoiceHeader.TotalBruttoHUF
        _InvoiceAdvancePopup.AmountDEV = _InvoiceHeader.TotalBruttoDEV
        _InvoiceAdvancePopup.PaymentOption = _onew.GetObject(_InvoiceHeader.PaymentOption)
        _InvoiceAdvancePopup.Description = _InvoiceHeader.Description
        _InvoiceAdvancePopup.BankInformation = _onew.GetObject(_InvoiceHeader.BankInformation)
        For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
            _InvoiceAdvancePopup.Controlling = _onew.GetObject(_InvoiceRows.Controlling)
            _InvoiceAdvancePopup.CostHolder = _onew.GetObject(_InvoiceRows.CostHolder)
            _InvoiceAdvancePopup.CostPlace = _onew.GetObject(_InvoiceRows.CostPlace)
            _InvoiceAdvancePopup.JobNumberObjects = _onew.GetObject(_InvoiceRows.JobNumberObjects)
            _InvoiceAdvancePopup.UniqueObjects = _onew.GetObject(_InvoiceRows.UniqueObjects)
            _InvoiceAdvancePopup.VAT = _onew.GetObject(_InvoiceRows.VAT)
        Next
        e.View = Application.CreateDetailView(_onew, "InvoiceAdvancePopup_DetailView", False, _InvoiceAdvancePopup)
    End Sub
    Private Sub aFinalizeInvoiceAdvance_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aFinalizeInvoiceAdvance.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _InvoiceRows As InvoiceRows
        Dim _InvoiceAdvancePopup As InvoiceAdvancePopup = TryCast(_ocur.GetObject(e.PopupWindow.View.SelectedObjects(0)), InvoiceAdvancePopup)

        If _InvoiceAdvancePopup IsNot Nothing Then
            For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
                _InvoiceRows.Controlling = _ocur.GetObject(_InvoiceAdvancePopup.Controlling)
                If _InvoiceHeader.CurrencyName.ShortName = "HUF" Then
                    If _InvoiceRows.VAT IsNot Nothing Then
                        _InvoiceRows.ListPriceHUF = Math.Round(_InvoiceAdvancePopup.AmountHUF / (100 + _InvoiceRows.VAT.KeyValuePercent) * 100, 0, MidpointRounding.AwayFromZero)
                    Else
                        _InvoiceRows.ListPriceHUF = _ocur.GetObject(_InvoiceAdvancePopup.AmountHUF)
                    End If
                Else
                    If _InvoiceRows.VAT IsNot Nothing Then
                        _InvoiceRows.ListPriceDEV = Math.Round(_InvoiceAdvancePopup.AmountDEV / (100 + _InvoiceRows.VAT.KeyValuePercent) * 100, 0, MidpointRounding.AwayFromZero)
                    Else
                        _InvoiceRows.ListPriceDEV = _ocur.GetObject(_InvoiceAdvancePopup.AmountDEV)
                    End If
                End If
            Next
            _InvoiceHeader.Description = _InvoiceAdvancePopup.Description
            _InvoiceHeader.DateExecution = _InvoiceAdvancePopup.DateExecution
            _InvoiceHeader.PaymentOption = _ocur.GetObject(_InvoiceAdvancePopup.PaymentOption)
            _InvoiceHeader.DatePayment = _InvoiceHeader.DateExecution

            Me.FinalizeInvoice(_InvoiceHeader, View, _ocur)
            Me.PrintInvoice(_InvoiceHeader)
            If TryCast(View, DetailView) IsNot Nothing Then
                View.Close(False)
            Else
                View.Refresh()
            End If
        End If
    End Sub
    Private Sub aViewGLAccounts_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewGLAccounts.Execute
        If View.SelectedObjects.Count > 0 Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
            If _InvoiceHeader IsNot Nothing Then
                Dim _GLAccounts As GLAccounts = _InvoiceHeader.GLAccounts
                If _GLAccounts IsNot Nothing Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "GLAccounts_DetailView_InvoiceHeader", False, _onew.GetObject(_GLAccounts))
                Else
                    Throw New Exception("*Nincs lekönyvelve a számla!")
                End If
            End If
        End If
    End Sub
    Private Sub aCorrInvoiceHeader_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCorrInvoiceHeader.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _InvoiceHeader_New As InvoiceHeader = _onew.CreateObject(Of InvoiceHeader)()

        _InvoiceHeader_New.BankInformation = _onew.GetObject(_InvoiceHeader.BankInformation)
        _InvoiceHeader_New.Customers = _onew.GetObject(_InvoiceHeader.Customers)
        _InvoiceHeader_New.CustomersFrom = _onew.GetObject(_InvoiceHeader.CustomersFrom)
        _InvoiceHeader_New.InvoiceType = _onew.GetObject(_InvoiceHeader.InvoiceType)
        _InvoiceHeader_New.IsCorrigendum = True
        _InvoiceHeader_New.IsEditable = True
        _InvoiceHeader_New.IsProforma = False
        _InvoiceHeader_New.IsStorno = False
        _InvoiceHeader_New.InvoiceHeader = _onew.GetObject(_InvoiceHeader)
        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "InvoiceHeader_DetailView_Corrigendum", True, _InvoiceHeader_New)
    End Sub
    Private Sub aDescriptionChanger_Cancel(sender As Object, e As System.EventArgs) Handles aDescriptionChanger.Cancel
    End Sub
    Private Sub aDescriptionChanger_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aDescriptionChanger.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader_DescriptionChanger_PopUp As InvoiceHeader_DescriptionChanger_PopUp
        Dim _CS_Description As New CollectionSource(_onew, GetType(InvoiceHeader_DescriptionChanger_PopUp))

        Dim _InvoiceHeader_xpquery = New XPQuery(Of InvoiceHeader)(_ocur.Session)
        Dim query = From _InvoiceHeader In _InvoiceHeader_xpquery _
                    Where _InvoiceHeader.Description IsNot Nothing _
        Group _InvoiceHeader By _InvoiceHeader.Description Into g = Group _
        Select New With {Key .Description = Description}
        For Each item In query
            _InvoiceHeader_DescriptionChanger_PopUp = _onew.CreateObject(Of InvoiceHeader_DescriptionChanger_PopUp)()
            _InvoiceHeader_DescriptionChanger_PopUp.Description = item.Description
            _CS_Description.Add(_InvoiceHeader_DescriptionChanger_PopUp)
        Next
        e.View = Application.CreateListView("InvoiceHeader_DescriptionChanger_PopUp_ListView", _CS_Description, False)
    End Sub
    Private Sub aDescriptionChanger_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aDescriptionChanger.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        For Each _Description In e.PopupWindow.View.SelectedObjects
            _InvoiceHeader.Description += vbNewLine & TryCast(_Description, InvoiceHeader_DescriptionChanger_PopUp).Description
        Next
    End Sub
    Private Sub aInvoiceRowCloner_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aInvoiceRowCloner.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As New CollectionSource(_onew, GetType(InvoiceRows))
        _CS.BeginUpdateCriteria()
        _CS.Criteria.Clear()
        _CS.Criteria("RowType") = CriteriaOperator.Parse("InvoiceRowCorrigendumType=0") 'Csak a normál sorok felkínálása
        _CS.EndUpdateCriteria()

        e.View = Application.CreateListView("InvoiceRows_ListView", _CS, False)
    End Sub
    Private Sub aInvoiceRowCloner_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aInvoiceRowCloner.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _InvoiceRowsPopUp As InvoiceRows = TryCast(e.PopupWindow.View.SelectedObjects(0), InvoiceRows)
        If e.PopupWindow.View.SelectedObjects.Count = 1 Then
            If _InvoiceRowsPopUp.Controlling IsNot Nothing Then
                _InvoiceHeader.row_Controlling = _ocur.GetObjectByKey(Of Controlling)(_InvoiceRowsPopUp.Controlling.Oid)
            End If
            If _InvoiceRowsPopUp.CostHolder IsNot Nothing Then
                _InvoiceHeader.row_CostHolder = _ocur.GetObjectByKey(Of CostHolder)(_InvoiceRowsPopUp.CostHolder.Oid)
            End If
            If _InvoiceRowsPopUp.CostPlace IsNot Nothing Then
                _InvoiceHeader.row_CostPlace = _ocur.GetObjectByKey(Of CostPlace)(_InvoiceRowsPopUp.CostPlace.Oid)
            End If
            _InvoiceHeader.row_Description = _InvoiceRowsPopUp.Description
            _InvoiceHeader.row_DiscountPercent = _InvoiceRowsPopUp.DiscountPercent
            If _InvoiceRowsPopUp.JobNumberObjects IsNot Nothing Then
                _InvoiceHeader.row_JobNumberObjects = _ocur.GetObjectByKey(Of JobNumberObjects)(_InvoiceRowsPopUp.JobNumberObjects.Oid)
            End If
            _InvoiceHeader.row_ListPriceDEV = _InvoiceRowsPopUp.ListPriceDEV
            _InvoiceHeader.row_ListPriceHUF = _InvoiceRowsPopUp.ListPriceHUF
            _InvoiceHeader.row_QTT = _InvoiceRowsPopUp.QTT
            If _InvoiceRowsPopUp.QualityOption IsNot Nothing Then
                _InvoiceHeader.row_QualityOption = _ocur.GetObjectByKey(Of QualityOption)(_InvoiceRowsPopUp.QualityOption.Oid)
            End If
            _InvoiceHeader.row_ShortName = _InvoiceRowsPopUp.ShortName
            If _InvoiceRowsPopUp.UniqueObjects IsNot Nothing Then
                _InvoiceHeader.row_UniqueObjects = _ocur.GetObjectByKey(Of UniqueObjects)(_InvoiceRowsPopUp.UniqueObjects.Oid)
            End If
            If _InvoiceRowsPopUp.Units IsNot Nothing Then
                _InvoiceHeader.row_Units = _ocur.GetObjectByKey(Of Units)(_InvoiceRowsPopUp.Units.Oid)
            End If
            If _InvoiceRowsPopUp.VAT IsNot Nothing Then
                _InvoiceHeader.row_VAT = _ocur.GetObjectByKey(Of VAT)(_InvoiceRowsPopUp.VAT.Oid)
            End If
        Else
            _ocur.Rollback()
            '----------------------------------TESZT MIATT------------------------------------->>
            Throw New Exception("*Nincs kiválasztva egy sor sem, vagy több sor került együtesesen kiválasztásra, így a műveletet nem lehet végrahajtani!")
            '----------------------------------TESZT MIATT-------------------------------------<<
        End If
    End Sub
    Private Sub aInvoiceHeaderClone_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aInvoiceHeaderClone.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _InvoiceHeaderNEW As InvoiceHeader
        If _InvoiceHeader IsNot Nothing Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            _InvoiceHeaderNEW = _onew.CreateObject(Of InvoiceHeader)()

            _InvoiceHeaderNEW.CustomersFrom = _onew.GetObject(_InvoiceHeader.CustomersFrom)
            _InvoiceHeaderNEW.DateExecution = _InvoiceHeader.DateExecution
            _InvoiceHeaderNEW.DatePayment = _InvoiceHeader.DatePayment
            _InvoiceHeaderNEW.Description = _InvoiceHeader.Description
            _InvoiceHeaderNEW.PaymentOption = _onew.GetObject(_InvoiceHeader.PaymentOption)
            _InvoiceHeaderNEW.ShipmentOption = _onew.GetObject(_InvoiceHeader.ShipmentOption)
            _InvoiceHeaderNEW.QualityOption = _onew.GetObject(_InvoiceHeader.QualityOption)
            _InvoiceHeaderNEW.CurrencyName = _onew.GetObject(_InvoiceHeader.CurrencyName)
            _InvoiceHeaderNEW.CurrencyRate = _InvoiceHeader.CurrencyRate
            _InvoiceHeaderNEW.IsEditable = True
            _InvoiceHeaderNEW.IsProforma = False
            _InvoiceHeaderNEW.IsStorno = False
            _InvoiceHeaderNEW.IsCorrigendum = False

            For Each _InvoiceRows As InvoiceRows In _InvoiceHeader.InvoiceRows
                Dim _InvoiceRowsNEW As InvoiceRows = _onew.CreateObject(Of InvoiceRows)()

                _InvoiceRowsNEW.InvoiceHeader = _InvoiceHeaderNEW
                _InvoiceRowsNEW.InvoiceRowtype = _InvoiceRows.InvoiceRowtype
                _InvoiceRowsNEW.RowIndex = _InvoiceRows.RowIndex
                _InvoiceRowsNEW.CustomsTariffs = _onew.GetObject(_InvoiceRows.CustomsTariffs)
                _InvoiceRowsNEW.ShortName = _InvoiceRows.ShortName
                _InvoiceRowsNEW.Description = _InvoiceRows.Description
                _InvoiceRowsNEW.ListPriceHUF = _InvoiceRows.ListPriceHUF
                _InvoiceRowsNEW.ListPriceDEV = _InvoiceRows.ListPriceDEV
                _InvoiceRowsNEW.DiscountPriceHUF = _InvoiceRows.DiscountPriceHUF
                _InvoiceRowsNEW.DiscountPriceDEV = _InvoiceRows.DiscountPriceDEV
                _InvoiceRowsNEW.QTT = _InvoiceRows.QTT
                _InvoiceRowsNEW.Units = _onew.GetObject(_InvoiceRows.Units)
                _InvoiceRowsNEW.VAT = _onew.GetObject(_InvoiceRows.VAT)
                _InvoiceRowsNEW.Controlling = _onew.GetObject(_InvoiceRows.Controlling)
                _InvoiceRowsNEW.UniqueObjects = _onew.GetObject(_InvoiceRows.UniqueObjects)
                _InvoiceRowsNEW.CostHolder = _onew.GetObject(_InvoiceRows.CostHolder)
                _InvoiceRowsNEW.CostPlace = _onew.GetObject(_InvoiceRows.CostPlace)
                _InvoiceRowsNEW.JobNumberObjects = _onew.GetObject(_InvoiceRows.JobNumberObjects)
            Next
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "InvoiceHeader_DetailView", True, _onew.GetObject(_InvoiceHeaderNEW))
        Else
            _ocur.Rollback()
        End If
    End Sub
    Private Sub InvoiceHeader_ListView_Proforma_CustomRate_DeleteAction_Executing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        For Each _InvoiceHeader As InvoiceHeader In View.SelectedObjects
            For Each _InvoiceRows As InvoiceRows In _InvoiceHeader.InvoiceRows
                If _InvoiceRows IsNot Nothing Then
                    If _InvoiceRows.ContractRows IsNot Nothing Then
                        _InvoiceRows.ContractRows.InvoiceRows = Nothing
                        _InvoiceRows.ContractRows = Nothing
                    End If
                End If
            Next
        Next
        _ocur.CommitChanges()
    End Sub
    Private Sub aGenerateInvoiceToPdfFiles_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGenerateInvoiceToPdfFiles.Execute
        If View.SelectedObjects.Count > 0 Then
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            RaiseEvent InitWork(0, 1, View.SelectedObjects.Count)
            Dim _InvoiceHeader As InvoiceHeader
            For Each _InvoiceHeader In View.SelectedObjects
                RaiseEvent DoWork()
                If _InvoiceHeader.CurrencyName IsNot Nothing Then
                    If _InvoiceHeader.PrintCopy = 0 And _InvoiceHeader.PDF_Original Is Nothing Then
                        _InvoiceHeader.PrintCopy = 1
                        _InvoiceHeader.GeneratePDFs(_ocur, _InvoiceHeader, True)
                        _InvoiceHeader.PrintCopy = 2
                        _InvoiceHeader.GeneratePDFs(_ocur, _InvoiceHeader, True)
                    ElseIf TryCast(SecuritySystem.CurrentUser, User).UserName = "SysAdmin" Then
                        _InvoiceHeader.PrintCopy = 1
                        _InvoiceHeader.GeneratePDFs(_ocur, _InvoiceHeader, True)
                        _InvoiceHeader.PrintCopy = 2
                        _InvoiceHeader.GeneratePDFs(_ocur, _InvoiceHeader, True)
                    End If
                End If
            Next
            _ocur.CommitChanges()
            RaiseEvent FinalWork
        Else
            Throw New Exception("*Nincs egy kijelölt sor sem!")
        End If
    End Sub
    Private Sub aCloneFullInvoice_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCloneFullInvoice.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Try
            If View.SelectedObjects.Count = 0 Then Throw New Exception("*Nincs kiválasztva egy számla sem!")

            Dim _InvoiceHeader_OnlyOne As InvoiceHeader = Nothing 'Ha csak 1 van, akkor benyitjuk a detailview-ját

            For Each _InvoiceHeader_Selected As InvoiceHeader In View.SelectedObjects
                Dim _InvoiceHeader_OLD As InvoiceHeader = _uow.GetObjectByKey(Of InvoiceHeader)(TryCast(_InvoiceHeader_Selected, InvoiceHeader).Oid)

                If _InvoiceHeader_OLD IsNot Nothing Then
                    Dim _InvoiceHeader As New InvoiceHeader(_uow)

                    _InvoiceHeader.CustomersFrom = _InvoiceHeader_OLD.CustomersFrom
                    _InvoiceHeader.InvoiceType = _InvoiceHeader_OLD.InvoiceType
                    _InvoiceHeader.Customers = _InvoiceHeader_OLD.Customers
                    _InvoiceHeader.InvoicePeriod = _InvoiceHeader_OLD.InvoicePeriod
                    _InvoiceHeader.Description = _InvoiceHeader_OLD.Description
                    _InvoiceHeader.PaymentOption = _InvoiceHeader_OLD.PaymentOption
                    _InvoiceHeader.ShipmentOption = _InvoiceHeader_OLD.ShipmentOption
                    _InvoiceHeader.QualityOption = _InvoiceHeader_OLD.QualityOption
                    _InvoiceHeader.CurrencyName = _InvoiceHeader_OLD.CurrencyName
                    _InvoiceHeader.BankInformation = _InvoiceHeader_OLD.BankInformation

                    If _InvoiceHeader_OLD.InvoiceRows.Count > 0 Then
                        For Each _InvoiceRows_OLD As InvoiceRows In _InvoiceHeader_OLD.InvoiceRows
                            Dim _InvoiceRows As New InvoiceRows(_uow)

                            _InvoiceRows.InvoiceHeader = _InvoiceHeader
                            _InvoiceRows.InvoiceRowtype = _InvoiceRows_OLD.InvoiceRowtype
                            _InvoiceRows.InvoicePeriod = _InvoiceHeader_OLD.InvoicePeriod
                            _InvoiceRows.RowIndex = _InvoiceRows_OLD.RowIndex
                            _InvoiceRows.CustomsTariffs = _InvoiceRows.CustomsTariffs
                            _InvoiceRows.ShortName = _InvoiceRows_OLD.ShortName
                            _InvoiceRows.Description = _InvoiceRows_OLD.Description
                            _InvoiceRows.ListPriceHUF = _InvoiceRows_OLD.ListPriceHUF
                            _InvoiceRows.ListPriceDEV = _InvoiceRows_OLD.ListPriceDEV
                            _InvoiceRows.DiscountPriceHUF = _InvoiceRows_OLD.DiscountPriceHUF
                            _InvoiceRows.DiscountPriceDEV = _InvoiceRows_OLD.DiscountPriceDEV
                            _InvoiceRows.QTT = _InvoiceRows_OLD.QTT
                            _InvoiceRows.Units = _InvoiceRows_OLD.Units
                            _InvoiceRows.VAT = _InvoiceRows_OLD.VAT
                            _InvoiceRows.QualityOption = _InvoiceRows_OLD.QualityOption

                            _InvoiceRows.Controlling = _InvoiceRows_OLD.Controlling
                            _InvoiceRows.UniqueObjects = _InvoiceRows_OLD.UniqueObjects
                            _InvoiceRows.CostHolder = _InvoiceRows_OLD.CostHolder
                            _InvoiceRows.CostPlace = _InvoiceRows_OLD.CostPlace
                            _InvoiceRows.JobNumberObjects = _InvoiceRows_OLD.JobNumberObjects
                        Next
                    End If

                    If View.SelectedObjects.Count = 1 Then
                        _InvoiceHeader_OnlyOne = _InvoiceHeader
                    End If
                End If
            Next
            _uow.CommitChanges()
            If View.SelectedObjects.Count = 1 Then
                Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
                e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "InvoiceHeader_DetailView", True, _onew.GetObject(_InvoiceHeader_OnlyOne))
            End If
            If Frame.GetController(Of RefreshController).RefreshAction.Active Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If
        Catch ext As Exception
            MsgBox(ext.Message)
            _uow.RollbackTransaction()
        End Try
    End Sub
    Private Sub aOpenInvoicePdf_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SingleChoiceActionExecuteEventArgs) Handles aOpenInvoicePdf.Execute
        If View.SelectedObjects.Count > 0 Then
            Try
                Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
                Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)

                If _InvoiceHeader.PDF_Original Is Nothing Or _InvoiceHeader.PDF_Copy Is Nothing Then Throw New Exception("*Nincs generált PDF számla!")

                Dim _FileData As FileData = _ocur.CreateObject(Of FileData)()
                Select Case e.SelectedChoiceActionItem.Id
                    Case "Original"
                        _FileData = _InvoiceHeader.PDF_Original
                    Case "Copy"
                        _FileData = _InvoiceHeader.PDF_Copy
                    Case Else
                End Select
                Dim _FileName As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetTempPath, _FileData.FileName)
                Dim _StreamFile As System.IO.Stream = New System.IO.FileStream(_FileName, System.IO.FileMode.Create)

                _FileData.SaveToStream(_StreamFile)
                _StreamFile.Close()
                Process.Start(_FileName)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            Throw New Exception("*Nincs egy kijelölt sor sem!")
        End If
    End Sub
    Private Sub InvoiceHeader_DetailView_SaveActionExecuting(sender As Object, e As CancelEventArgs)
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
            If _InvoiceHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _InvoiceHeader.InvoiceRows.Count > 0 And _InvoiceHeader.CurrencyName.ShortName <> "HUF" Then
                For Each _InvoiceRows As InvoiceRows In _InvoiceHeader.InvoiceRows
                    _InvoiceRows.ListPriceHUF = _InvoiceRows.ListPriceDEV * _InvoiceHeader.CurrencyRate
                Next
            End If
            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
    Private Sub aDescriptionChangerHeader_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aDescriptionChangerHeader.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader_DescriptionChanger_PopUp As InvoiceHeader_DescriptionChanger_PopUp
        Dim _CS_Description As New CollectionSource(_onew, GetType(InvoiceHeader_DescriptionChanger_PopUp))

        Dim _InvoiceHeader_xpquery = New XPQuery(Of InvoiceHeader)(_ocur.Session)
        Dim query = From _InvoiceHeader In _InvoiceHeader_xpquery _
                    Where _InvoiceHeader.DescriptionHeader IsNot Nothing _
        Group _InvoiceHeader By _InvoiceHeader.DescriptionHeader Into g = Group _
        Select New With {Key .DescriptionHeader = DescriptionHeader}
        For Each item In query
            _InvoiceHeader_DescriptionChanger_PopUp = _onew.CreateObject(Of InvoiceHeader_DescriptionChanger_PopUp)()
            _InvoiceHeader_DescriptionChanger_PopUp.Description = item.DescriptionHeader
            _CS_Description.Add(_InvoiceHeader_DescriptionChanger_PopUp)
        Next
        e.View = Application.CreateListView("InvoiceHeader_DescriptionChanger_PopUp_ListView", _CS_Description, False)
    End Sub
    Private Sub aDescriptionChangerHeader_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aDescriptionChangerHeader.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        For Each _Description In e.PopupWindow.View.SelectedObjects
            _InvoiceHeader.DescriptionHeader += vbNewLine & TryCast(_Description, InvoiceHeader_DescriptionChanger_PopUp).Description
        Next
    End Sub

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

        Dim _InvoiceHeader As InvoiceHeader

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _InvoiceHeader In View.SelectedObjects
            RaiseEvent DoWork()
            If _InvoiceHeader IsNot Nothing Then
                GLFunctions.ReconfigurePCI(_uow, _InvoiceHeader.CustomersFrom, _InvoiceHeader.Customers, 0, _InvoiceHeader.ConnectInfo)
            End If
        Next
        RaiseEvent FinalWork

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

    Private Sub aViewPCIDInvoice_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewPCIDInvoice.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)

        ViewPDCI.ViewPDCIDetail(_onew, Application, e, _InvoiceHeader.CustomersFrom, _
                          _InvoiceHeader.Customers, ePartnerType.eReceivables, _
                          _InvoiceHeader.ConnectInfo)
    End Sub

    Private Sub aViewInvoiceHold_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewInvoiceHold.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _InvoiceHoldPopup As InvoiceHoldPopup = Nothing

        If _InvoiceHeader IsNot Nothing Then
            _InvoiceHoldPopup = _onew.CreateObject(Of InvoiceHoldPopup)()
            _InvoiceHoldPopup.InvoiceHeader = _onew.GetObject(_InvoiceHeader)

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "InvoiceHoldPopup_DetailView", False, _InvoiceHoldPopup)
        End If
    End Sub

    Private Sub aRecalculateHUF_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRecalculateHUF.Execute
        '// Az eljárás beállítja a jó HUF összegeket a DEV alapján
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _InvoiceHeader_ocur As InvoiceHeader
        Dim _InvoiceHeader As InvoiceHeader
        Dim _InvoiceVATRows As InvoiceVATRows
        Try

            RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
            For Each _InvoiceHeader_ocur In View.SelectedObjects
                RaiseEvent DoWork()
                If _InvoiceHeader_ocur.CurrencyName.ShortName <> "HUF" Then
                    _InvoiceHeader = _uow.GetObjectByKey(Of InvoiceHeader)(_InvoiceHeader_ocur.Oid)
                    If _InvoiceHeader IsNot Nothing Then
                        Dim _InvoiceRows As InvoiceRows
                        For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
                            _InvoiceRows.DiscountPriceHUF = _InvoiceRows.DiscountPriceDEV * _InvoiceHeader.CurrencyRate
                            _InvoiceRows.ListPriceHUF = _InvoiceRows.ListPriceDEV * _InvoiceHeader.CurrencyRate
                        Next
                        _uow.CommitChanges()

                        For Each _InvoiceVATRows In _InvoiceHeader.InvoiceVATRows
                            _InvoiceVATRows.AmountBruttoHUF = Math.Round(_InvoiceVATRows.AmountBruttoDEV * _InvoiceHeader.CurrencyRate, 0, MidpointRounding.AwayFromZero)
                            _InvoiceVATRows.AmountNettoHUF = Math.Round(_InvoiceVATRows.AmountNettoDEV * _InvoiceHeader.CurrencyRate, 0, MidpointRounding.AwayFromZero)
                            _InvoiceVATRows.AmountVATHUF = Math.Round(_InvoiceVATRows.AmountVATDEV * _InvoiceHeader.CurrencyRate, 0, MidpointRounding.AwayFromZero)

                        Next
                        _InvoiceHeader.TotalBruttoHUF = Math.Round(_InvoiceHeader.TotalBruttoDEV * _InvoiceHeader.CurrencyRate, 0, MidpointRounding.AwayFromZero)

                    End If
                End If
                _InvoiceHeader_ocur.RecalculateInvoice(_InvoiceHeader_ocur, _ocur)
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            RaiseEvent FinalWork
        End Try
    End Sub
    Private Sub InvoiceHeader_ListView_Editable_DeleteAction_Executing(sender As Object, e As CancelEventArgs)
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            For Each _InvoiceHeader As InvoiceHeader In View.SelectedObjects
                If _InvoiceHeader.InvoiceRows.Count > 0 Then
                    For Each _InvoiceRows As InvoiceRows In _InvoiceHeader.InvoiceRows
                        If _InvoiceRows.OrderInRows IsNot Nothing Then
                            _InvoiceRows.OrderInRows.QTT_Invoiced -= _InvoiceRows.QTT
                        End If
                        If _InvoiceRows.DeliveryNoteOutRows IsNot Nothing Then
                            _InvoiceRows.DeliveryNoteOutRows.QTT_Invoiced -= _InvoiceRows.QTT
                        End If
                        If _InvoiceRows.InvoiceRows IsNot Nothing Then
                            Dim _OrigInvoiceRows As InvoiceRows = _ocur.FindObject(Of InvoiceRows)(CriteriaOperator.Parse("InvoiceRowCorrigendumType=1 AND InvoiceRows.Oid=?", _InvoiceRows.InvoiceRows.Oid))
                            If _OrigInvoiceRows IsNot Nothing Then
                                _OrigInvoiceRows.InvoiceRowCorrigendumType = eInvoiceRowCorrigendumType.eNormal
                            End If
                        End If
                    Next
                End If
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub

    Private Sub aFinalizeInvoicewithPopup_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aFinalizeInvoicewithPopup.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)

        Dim _InvoiceHeaderWithPopup As InvoiceHeaderWithPopup = _onew.CreateObject(Of InvoiceHeaderWithPopup)()

        _InvoiceHeaderWithPopup.Description = _InvoiceHeader.Description
        _InvoiceHeaderWithPopup.DescriptionHeader = _InvoiceHeader.DescriptionHeader

        e.View = Application.CreateDetailView(_onew, "InvoiceHeaderWithPopup_DetailView", True, _InvoiceHeaderWithPopup)
    End Sub

    Private Sub aFinalizeInvoicewithPopup_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aFinalizeInvoicewithPopup.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _InvoiceHeaderWithPopup As InvoiceHeaderWithPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), InvoiceHeaderWithPopup)

        If _InvoiceHeader IsNot Nothing Then
            _InvoiceHeader.Description = _InvoiceHeaderWithPopup.Description
            _InvoiceHeader.DescriptionHeader = _InvoiceHeaderWithPopup.DescriptionHeader
            _InvoiceHeader.NotNeedFinancial = _InvoiceHeaderWithPopup.NotNeedFinancial
            Me.FinalizeInvoice(_InvoiceHeader, View, _ocur)
        End If

    End Sub
    Private Sub aCreateInvoiceCreditNote_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aCreateInvoiceCreditNote.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
        Dim _InvoiceCreditNoteHeader As New InvoiceCreditNoteHeader(_onew.Session)
        With _InvoiceCreditNoteHeader
            .CustomersFrom = _onew.GetObject(_InvoiceHeader.CustomersFrom)
            .Customers = _onew.GetObject(_InvoiceHeader.Customers)
            .PaymentOption = _onew.GetObject(_InvoiceHeader.PaymentOption)
            .Description = _onew.GetObject(_InvoiceHeader.DescriptionHeader)
        End With

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "InvoiceCreditNoteHeader_DetailView", True, _InvoiceCreditNoteHeader)
    End Sub
    Private Sub aInvoiceHeader_GenereateEmailSendQueue_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aInvoiceHeader_GenereateEmailSendQueue.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _EMailSendQueueParameters_CollectionSource As New CollectionSource(_onew, GetType(EMailSendQueueParameters))

        e.View = Application.CreateListView("EMailSendQueueParameters_ListView_PopUp", _EMailSendQueueParameters_CollectionSource, True)
    End Sub
    Private Sub aInvoiceHeader_GenereateEmailSendQueue_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aInvoiceHeader_GenereateEmailSendQueue.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _NOEmail As New ArrayList
        Try
            Dim _SelectObject As New ArrayList
            _SelectObject.AddRange(View.SelectedObjects)

            Dim _EMailSendQueueHeader As New EMailSendQueueHeader(_ocur.Session)
            With _EMailSendQueueHeader
                .EmailSendQueueParameters = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), EMailSendQueueParameters))
                .ShortName = "Elektronikus számla"
            End With
            _ocur.CommitChanges()

            RaiseEvent InitWork(1, 1, _SelectObject.Count)
            For Each _InvoiceHeader As InvoiceHeader In _SelectObject
                Dim _EMailTo As String = ""

                Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("Customers=? AND IsInvoiceResponsible=True", _InvoiceHeader.Customers), True)
                If _Contacts IsNot Nothing Then
                    _EMailTo = _Contacts.Email
                Else
                    _EMailTo = _InvoiceHeader.Customers.Email
                End If
                If Not String.IsNullOrEmpty(_EMailTo) Then
                    If _InvoiceHeader.PDF_Original Is Nothing Then
                        If _InvoiceHeader.CurrencyName IsNot Nothing Then
                            If _InvoiceHeader.PrintCopy = 0 And _InvoiceHeader.PDF_Original Is Nothing Then
                                _InvoiceHeader.PrintCopy = 1
                                _InvoiceHeader.GeneratePDFs(_ocur, _InvoiceHeader, True)
                                _InvoiceHeader.PrintCopy = 2
                                _InvoiceHeader.GeneratePDFs(_ocur, _InvoiceHeader, True)
                            ElseIf TryCast(SecuritySystem.CurrentUser, User).UserName = "SysAdmin" Then
                                _InvoiceHeader.PrintCopy = 1
                                _InvoiceHeader.GeneratePDFs(_ocur, _InvoiceHeader, True)
                                _InvoiceHeader.PrintCopy = 2
                                _InvoiceHeader.GeneratePDFs(_ocur, _InvoiceHeader, True)
                            End If
                        End If
                    End If
                    Dim _EMailSendQueueRows As EMailSendQueueRows = _ocur.FindObject(Of EMailSendQueueRows)(CriteriaOperator.Parse("EMailSendQueueHeader=? AND EMailTo=?", _EMailSendQueueHeader, _EMailTo), True)

                    If _EMailSendQueueRows Is Nothing Then
                        _EMailSendQueueRows = New EMailSendQueueRows(_ocur.Session)
                        With _EMailSendQueueRows
                            .EMailSendQueueHeader = _EMailSendQueueHeader
                            If _Contacts IsNot Nothing Then
                                .HTMLBody = String.Format(_EMailSendQueueHeader.EmailSendQueueParameters.HTMLBody, _Contacts.Customers.FullName)
                            Else
                                .HTMLBody = String.Format(_EMailSendQueueHeader.EmailSendQueueParameters.HTMLBody, _InvoiceHeader.Customers.FullName)
                            End If

                            .EMailTo = _EMailTo
                            .EMailFrom = _EMailSendQueueHeader.EmailSendQueueParameters.EMailFrom
                            .Subject = _EMailSendQueueHeader.EmailSendQueueParameters.Subject

                            Dim _EMailSendQueueRows_FileAttachment As New EMailSendQueueRows_FileAttachment(_ocur.Session)
                            With _EMailSendQueueRows_FileAttachment
                                Dim _FileName As String = IO.Path.GetTempPath + _InvoiceHeader.PDF_Original.FileName
                                Dim _StreamFile As System.IO.Stream = New System.IO.FileStream(_FileName, System.IO.FileMode.Create)

                                _InvoiceHeader.PDF_Original.SaveToStream(_StreamFile)
                                _StreamFile.Close()
                                .EMailSendQueueRows = _EMailSendQueueRows
                                .FileAttachments = New FileData(_ocur.Session)

                                Dim _StreamFile2 As New System.IO.FileStream(_FileName, System.IO.FileMode.Open)

                                .FileAttachments.LoadFromStream(_InvoiceHeader.PDF_Original.FileName, _StreamFile2)
                                _StreamFile2.Close()
                                .FileAttachments.FileName = System.IO.Path.GetFileName(_FileName)

                                System.IO.File.Delete(_FileName)
                            End With

                            .EMailSendQueueRows_FileAttachment.Add(_EMailSendQueueRows_FileAttachment)
                        End With
                    Else
                        With _EMailSendQueueRows
                            Dim _EMailSendQueueRows_FileAttachment As New EMailSendQueueRows_FileAttachment(_ocur.Session)
                            With _EMailSendQueueRows_FileAttachment
                                Dim _FileName As String = IO.Path.GetTempPath + _InvoiceHeader.PDF_Original.FileName
                                Dim _StreamFile As System.IO.Stream = New System.IO.FileStream(_FileName, System.IO.FileMode.Create)

                                _InvoiceHeader.PDF_Original.SaveToStream(_StreamFile)
                                _StreamFile.Close()
                                .EMailSendQueueRows = _EMailSendQueueRows
                                .FileAttachments = New FileData(_ocur.Session)

                                Dim _StreamFile2 As New System.IO.FileStream(_FileName, System.IO.FileMode.Open)

                                .FileAttachments.LoadFromStream(_InvoiceHeader.PDF_Original.FileName, _StreamFile2)
                                _StreamFile2.Close()
                                .FileAttachments.FileName = System.IO.Path.GetFileName(_FileName)

                                System.IO.File.Delete(_FileName)
                            End With

                            .EMailSendQueueRows_FileAttachment.Add(_EMailSendQueueRows_FileAttachment)
                        End With
                    End If
                Else
                    _NOEmail.Add(_InvoiceHeader.DocumentNumber)
                End If
                RaiseEvent DoWork()
            Next
            If _EMailSendQueueHeader.EMailSendQueueRows.Count > 0 Then
                _ocur.CommitChanges()
            Else
                _ocur.Delete(_EMailSendQueueHeader)
                _ocur.CommitChanges()
            End If

            View.Refresh()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            RaiseEvent FinalWork
            If _NOEmail.Count > 0 Then
                Dim _ErrorMessage As String = "A következő számlák küldése nem lehetséges:" + vbNewLine

                For Each _Email As String In _NOEmail
                    _ErrorMessage += _Email + vbNewLine
                Next

                _ErrorMessage += "Nincs beállítva sem az ügyfélhez sem a kapcsothoz e-mail cím!"
                MsgBox(_ErrorMessage, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
            End If
        End Try
    End Sub
    Private Sub EMailSendQueueParameters_ListView_PopUp_AcceptAction_Executing(sender As Object, e As CancelEventArgs)
        If View.SelectedObjects.Count = 0 Then Throw New Exception("*Nincs kiválasztva egyetlen paraméter sem!")
        If View.SelectedObjects.Count > 1 Then Throw New Exception("*Egy e-mail csomaghoz csak egy paraméter választható ki!")
    End Sub

    Private Sub aBookEntryInvoiceHeader_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aBookEntryInvoiceHeader.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader
        Dim _InvoiceRows As InvoiceRows
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _InvoiceHeader In View.SelectedObjects
            RaiseEvent DoWork()
            For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
                _InvoiceRows.ReadyforBookEntryRows = True
            Next
            _InvoiceHeader.ReadyforBookEntry = True
            _InvoiceHeader.BookEntry(0, _InvoiceHeader, _uow, False)
        Next
        _uow.CommitChanges()
        _ocur.CommitChanges()
        RaiseEvent FinalWork
    End Sub

   
    Private Sub aGeneratePDFandSendDefaultMailClient_Invoice_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGeneratePDFandSendDefaultMailClient_Invoice.Execute

    End Sub
End Class

