Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Utils

Public Class InvoiceCreditNote_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

#Region "Overrides"
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        If View.Id = "InvoiceCreditNoteHeader_DetailView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceCreditNoteHeader_InvoiceCreditNoteRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceRows_ListView_CreditNote" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf InvoiceRows_ListView_CreditNote_AcceptAction_Executing
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        If View.Id = "InvoiceCreditNoteHeader_DetailView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "InvoiceCreditNoteHeader_InvoiceCreditNoteRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub
#End Region

    Private Sub aInvoiceCreditNoteHeader_AddRows_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aInvoiceCreditNoteHeader_AddRows.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CustomersFrom As CustomersFrom = _onew.GetObjectByKey(Of CustomersFrom)(TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, InvoiceCreditNoteHeader).CustomersFrom.Oid)
        Dim _Customers As Customers = _onew.GetObjectByKey(Of Customers)(TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, InvoiceCreditNoteHeader).Customers.Oid)
        Dim _InvoiceRows_CollectionSource As New CollectionSource(_onew, GetType(InvoiceRows))

        _InvoiceRows_CollectionSource.BeginUpdateCriteria()
        _InvoiceRows_CollectionSource.Criteria.Clear()
        _InvoiceRows_CollectionSource.Criteria.Add("First", CriteriaOperator.Parse("InvoiceHeader.IsStorno=False AND InvoiceHeader.IsEditable=False AND IsNull(InvoiceHeader.GCRecord)=True AND InvoiceHeader.DocumentNumber<>'' AND IsNull(InvoiceHeader)=False AND InvoiceHeader.CustomersFrom=? AND InvoiceHeader.Customers=? AND IsNull(OrderInRows)=False", _
                                                                 _CustomersFrom, _Customers))
        _InvoiceRows_CollectionSource.EndUpdateCriteria()


        e.View = Application.CreateListView("InvoiceRows_ListView_CreditNote", _InvoiceRows_CollectionSource, True)
    End Sub
    Private Sub aInvoiceCreditNoteHeader_AddRows_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aInvoiceCreditNoteHeader_AddRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceCreditNoteHeader As InvoiceCreditNoteHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, InvoiceCreditNoteHeader)

        If _InvoiceCreditNoteHeader IsNot Nothing Then
            For Each _InvoiceRows As InvoiceRows In TryCast(e.PopupWindow.View, ListView).CollectionSource.Collection
                If _InvoiceRows.QTT_Will > 0 Then
                    Dim _InvoiceCreditNoteRows As InvoiceCreditNoteRows = _ocur.FindObject(Of InvoiceCreditNoteRows)(CriteriaOperator.Parse("InvoiceCreditNoteHeader=? AND FromInvoiceRows=?", _
                                                                                                                                            _InvoiceCreditNoteHeader, _ocur.GetObject(_InvoiceRows)))
                    If _InvoiceCreditNoteRows Is Nothing Then
                        _InvoiceCreditNoteRows = New InvoiceCreditNoteRows(_ocur.Session)
                        With _InvoiceCreditNoteRows
                            .InvoiceCreditNoteHeader = _InvoiceCreditNoteHeader
                            .QTT = _InvoiceRows.QTT_Will
                            .PriceHUF = _InvoiceRows.PriceHUF
                            .PriceDEV = _InvoiceRows.PriceDEV
                            .SumPriceHUF = Math.Round(.QTT * .PriceHUF, 0, MidpointRounding.AwayFromZero)
                            .SumPriceDEV = Math.Round(.QTT * .PriceDEV, 2, MidpointRounding.AwayFromZero)
                            .SumPriceVATHUF = Math.Round(.SumPriceHUF * _InvoiceRows.VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                            .SumPriceVATDEV = Math.Round(.SumPriceDEV * _InvoiceRows.VAT.KeyValue, 2, MidpointRounding.AwayFromZero)
                            .SumPriceBruttoHUF = .SumPriceHUF + .SumPriceVATHUF
                            .SumPriceBruttoDEV = .SumPriceDEV + .SumPriceVATDEV
                            .Description = _InvoiceRows.Description
                            .FromInvoiceRows = _ocur.GetObject(_InvoiceRows)
                        End With
                    Else
                        With _InvoiceCreditNoteRows
                            .QTT += _InvoiceRows.QTT_Will
                            .SumPriceHUF = Math.Round(.QTT * .PriceHUF, 0, MidpointRounding.AwayFromZero)
                            .SumPriceDEV = Math.Round(.QTT * .PriceDEV, 2, MidpointRounding.AwayFromZero)
                            .SumPriceVATHUF = Math.Round(.SumPriceHUF * _InvoiceRows.VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                            .SumPriceVATDEV = Math.Round(.SumPriceDEV * _InvoiceRows.VAT.KeyValue, 2, MidpointRounding.AwayFromZero)
                            .SumPriceBruttoHUF = .SumPriceHUF + .SumPriceVATHUF
                            .SumPriceBruttoDEV = .SumPriceDEV + .SumPriceVATDEV
                        End With
                    End If
                End If
            Next

            _ocur.CommitChanges()
        End If

    End Sub
    Private Sub aInvoiceCreditNoteHeader_Finalize_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aInvoiceCreditNoteHeader_Finalize.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
        Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))

        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _InvoiceCreditNoteHeader As InvoiceCreditNoteHeader = TryCast(View.SelectedObjects(0), InvoiceCreditNoteHeader)

            If _InvoiceCreditNoteHeader IsNot Nothing And _InvoiceCreditNoteHeader.InvoiceCreditNoteRows.Count > 0 Then
                If Not LockingModule.LockObject(_uow_lock, GetType(StorageComputed), "-", _User) Then
                    Dim _LockingObject As LockingObject = _onew.FindObject(Of LockingObject)(CriteriaOperator.Parse("TableName=?", GetType(StorageComputed).Name + "-" + "-"))

                    Dim _Prompt As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\StorageComputed", "CanNotStorageInByLocking")
                    Dim _Title As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxError")
                    MsgBox(String.Format(_Prompt, _LockingObject.User.FullName, _LockingObject.ObjectCreated), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, _Title)

                    Exit Sub
                Else
                    'Új számlák darabszámának meghatározása OrderInParameters alapján
                    Dim _OrderInParameters_Array As New ArrayList
                    Dim _InvoiceHeader_Credited_Collection As New ArrayList

                    For Each _InvoiceCreditNoteRows As InvoiceCreditNoteRows In _InvoiceCreditNoteHeader.InvoiceCreditNoteRows
                        If _OrderInParameters_Array.Count = 0 Then
                            _OrderInParameters_Array.Add(_InvoiceCreditNoteRows.FromInvoiceRows.OrderInRows.OrderInHeader.OrderInParameters)
                        Else
                            Dim _Need As Boolean = True

                            For Each _OrderInParameters As OrderInParameters In _OrderInParameters_Array
                                If _OrderInParameters.Oid = _InvoiceCreditNoteRows.FromInvoiceRows.OrderInRows.OrderInHeader.OrderInParameters.Oid Then
                                    _Need = False
                                    Exit For
                                End If
                            Next

                            If _Need Then
                                _OrderInParameters_Array.Add(_InvoiceCreditNoteRows.FromInvoiceRows.OrderInRows.OrderInHeader.OrderInParameters)
                            End If
                        End If
                    Next

                    If _OrderInParameters_Array.Count > 0 Then
                        For Each _OrderInParameters As OrderInParameters In _OrderInParameters_Array

                            Dim _InvoiceHeader_Credited As New InvoiceHeader(_ocur.Session)
                            With _InvoiceHeader_Credited
                                .InvoiceType = _OrderInParameters.InvoiceType
                                .CustomersFrom = _InvoiceCreditNoteHeader.CustomersFrom
                                .Customers = _InvoiceCreditNoteHeader.Customers
                                .DateCreated = GetSQLTime(_ocur.Session)
                                .DateExecution = _InvoiceCreditNoteHeader.DateExecution
                                .DatePayment = _InvoiceCreditNoteHeader.DatePayment
                                .PaymentOption = _InvoiceCreditNoteHeader.PaymentOption
                                .ShipmentOption = _OrderInParameters.ShipmentOption
                                .CurrencyName = _OrderInParameters.CurrencyName                               
                                If .PaymentOption.PayMode <> ePayMode.InCash Then
                                    .BankInformation = _OrderInParameters.BankInformation
                                End If
                                .Description = _InvoiceCreditNoteHeader.Description

                                InvoiceHeaderSavedSettings(_InvoiceHeader_Credited)

                                If .PaymentOption.PayMode = ePayMode.InContinuous Then
                                    .DatePayment = .DateExecution
                                Else
                                    If .PaymentOption.DatePaymentMode = eDatePaymentMode.eDateCreated Then
                                        .DatePayment = .DateCreated.AddDays(.PaymentOption.PayDay)
                                    Else
                                        .DatePayment = .DateExecution.AddDays(.PaymentOption.PayDay)
                                    End If
                                End If
                                If .InvoiceType.NumberGeneratorProforma IsNot Nothing Then
                                    .DocumentNumberProforma = .InvoiceType.NumberGeneratorProforma.GetNewNumber(.InvoiceType.NumberGeneratorProforma)
                                End If

                                'Véglegesítés
                                .IsCreditNote = True
                                .IsEditable = False
                                .IsProforma = False
                                .IsStorno = False

                                '.ConnectInfo = .InvoiceType.NumberGenerator.GetNewNumber(.InvoiceType.NumberGenerator)
                                .DocumentNumber = .InvoiceType.NumberGenerator.GetNewNumber(.InvoiceType.NumberGenerator) '.ConnectInfo
                            End With


                            _InvoiceHeader_Credited_Collection.Add(_InvoiceHeader_Credited)

                            'Számlasorok beszúrása
                            Dim _InvoiceCreditNoteRows_Collection As New XPCollection(Of InvoiceCreditNoteRows)(_ocur.Session, CriteriaOperator.Parse("InvoiceCreditNoteHeader=? AND FromInvoiceRows.OrderInRows.OrderInHeader.OrderInParameters=?", _
                                                                                                                                                      _InvoiceCreditNoteHeader, _OrderInParameters))

                            If _InvoiceCreditNoteRows_Collection.Count > 0 Then
                                For Each _InvoiceCreditNoteRows As InvoiceCreditNoteRows In _InvoiceCreditNoteRows_Collection
                                    Dim _InvoiceRows As New InvoiceRows(_ocur.Session)
                                    With _InvoiceRows
                                        .InvoiceHeader = _InvoiceHeader_Credited
                                        .RowIndex = _InvoiceHeader_Credited.InvoiceRows.Count
                                        .OrderInRows = _InvoiceCreditNoteRows.FromInvoiceRows.OrderInRows
                                        .InvoiceRowtype = eInvoiceRowType.FromOrder
                                        .DiscountPriceDEV = 0
                                        .DiscountPriceHUF = 0
                                        .ListPriceDEV = _InvoiceCreditNoteRows.PriceDEV
                                        .ListPriceHUF = _InvoiceCreditNoteRows.PriceHUF
                                        .QTT = _InvoiceCreditNoteRows.QTT * (-1)
                                        .QualityOption = _InvoiceCreditNoteRows.FromInvoiceRows.QualityOption
                                        .ShortName = _InvoiceCreditNoteRows.FromInvoiceRows.ShortName
                                        .Units = _InvoiceCreditNoteRows.FromInvoiceRows.Units
                                        .VAT = _InvoiceCreditNoteRows.FromInvoiceRows.VAT
                                        .Description = _InvoiceCreditNoteRows.Description
                                        .CustomsTariffs = _InvoiceCreditNoteRows.FromInvoiceRows.CustomsTariffs
                                        .Controlling = _InvoiceCreditNoteRows.FromInvoiceRows.Controlling
                                        .InvoiceRows = _InvoiceCreditNoteRows.FromInvoiceRows
                                    End With
                                    _InvoiceCreditNoteRows.ToInvoiceRows = _InvoiceRows
                                    If String.IsNullOrEmpty(_InvoiceHeader_Credited.ConnectInfo) Then
                                        _InvoiceHeader_Credited.ConnectInfo = _InvoiceCreditNoteRows.FromInvoiceRows.InvoiceHeader.DocumentNumber
                                    End If
                                Next
                            End If
                        Next


                        'Betárolás
                        Dim _StorageInHeader As New StorageInHeader(_ocur.Session)
                        With _StorageInHeader
                            .Storage = _InvoiceCreditNoteHeader.Storage
                            .StorageMoveType = _InvoiceCreditNoteHeader.StorageMoveType
                            .CreateDate = _InvoiceCreditNoteHeader.DateExecution
                        End With

                        If _InvoiceCreditNoteHeader.InvoiceCreditNoteRows.Count > 0 Then
                            For Each _InvoiceCreditNoteRows As InvoiceCreditNoteRows In _InvoiceCreditNoteHeader.InvoiceCreditNoteRows
                                Dim _StorageInRows As New StorageInRows(_ocur.Session)
                                With _StorageInRows
                                    .StorageInHeader = _StorageInHeader
                                    .RowIndex = _StorageInHeader.StorageInRows.Count
                                    .Products = _InvoiceCreditNoteRows.FromInvoiceRows.OrderInRows.Products
                                    .QTT_Will = _InvoiceCreditNoteRows.QTT
                                    .AveragePriceInHUF = (_InvoiceCreditNoteRows.ToInvoiceRows.SumAveragePriceHUF / _InvoiceCreditNoteRows.ToInvoiceRows.QTT)

                                    If _InvoiceCreditNoteRows.FromInvoiceRows.DeliveryNoteOutRows IsNot Nothing Then
                                        .PacketNumber = _InvoiceCreditNoteRows.FromInvoiceRows.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.PacketNumber
                                        .SerialNumber = _InvoiceCreditNoteRows.FromInvoiceRows.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.SerialNumber
                                        .Weight = Math.Round(_InvoiceCreditNoteRows.FromInvoiceRows.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.Weight / _InvoiceCreditNoteRows.FromInvoiceRows.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.QTT * _InvoiceCreditNoteRows.QTT, 4, MidpointRounding.AwayFromZero)
                                        .WeightMode = _InvoiceCreditNoteRows.FromInvoiceRows.DeliveryNoteOutRows.StorageOutRowsInRows.StorageInRows.WeightMode
                                    End If

                                    Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _
                                                                                                                            .Products, _StorageInHeader.Storage))

                                    If _StorageComputed Is Nothing Then
                                        _StorageComputed = _ocur.CreateObject(Of StorageComputed)()
                                        _StorageComputed.Storage = .Storage
                                        _StorageComputed.Products = .Products
                                        _StorageComputed.QTTReservedIn = .QTT_Will
                                    Else
                                        _StorageComputed.QTTReservedIn += .QTT_Will
                                    End If

                                End With
                            Next
                        End If

                        'Betárolás véglegesítése
                        For Each _StorageInRows As StorageInRows In _StorageInHeader.StorageInRows

                            Dim _StorageComputed As StorageComputed = _onew.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _
                                                                                    _onew.GetObject(_StorageInRows.Products), _onew.GetObject(_StorageInHeader.Storage)))

                            If _StorageInRows.QTT_Will > 0 Then
                                _StorageInRows.QTT = _StorageInRows.QTT_Will
                                _StorageComputed.QTTFree += _StorageInRows.QTT_Will
                                _StorageComputed.QTTReservedIn -= _StorageInRows.QTT_Will

                                Dim _StorageMoveEvents As New StorageMoveEvents(_onew.Session)
                                With _StorageMoveEvents
                                    .StorageInRows = _onew.GetObject(_StorageInRows)
                                    .QTT = _StorageInRows.QTT_Will
                                    .QTT_Free_After = _StorageComputed.QTTFree
                                    .QTT_Free_Before = _StorageMoveEvents.QTT_Free_After - _StorageMoveEvents.QTT
                                    .MoveEventsType = eMoveEventsType.StorageInFinal
                                    .ExecutionDate = _StorageInRows.StorageInHeader.CreateDate
                                End With

                                _StorageInRows.QTT_Will = 0
                            End If
                        Next

                        _StorageInHeader.IsEditable = False
                        _StorageInHeader.IsFinalized = True

                        _ocur.CommitChanges()

                        For Each _InvoiceHeader As InvoiceHeader In _InvoiceHeader_Credited_Collection
                            _InvoiceHeader.RecalculateInvoice(_InvoiceHeader, _ocur)
                        Next
                        _ocur.CommitChanges()

                        Dim _ReportData_Storage As ReportData = _onew.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _StorageInHeader.Storage.ReportIn.Oid), True)
                        If _ReportData_Storage Is Nothing Or _StorageInHeader Is Nothing Then
                        Else
                            Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData_Storage, CriteriaOperator.Parse("StorageInHeader.DocumentNumber =?", _StorageInHeader.DocumentNumber), True)
                        End If

                        For Each _InvoiceHeader As InvoiceHeader In _InvoiceHeader_Credited_Collection
                            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.Oid=?", _InvoiceHeader.Oid), True)
                                    Else
                                        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData_DEV, CriteriaOperator.Parse("InvoiceHeader.Oid=?", _InvoiceHeader.Oid), True)
                                    End If
                                End If
                            End If
                        Next
                    End If
                End If
            End If
            View.Close()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            LockingModule.UnLockObject(_uow_lock, GetType(StorageComputed), "-", _User)
        End Try
    End Sub
    Private Sub InvoiceRows_ListView_CreditNote_AcceptAction_Executing(sender As Object, e As CancelEventArgs)
        Try
            Dim _onew As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            For Each _InvoiceRows As InvoiceRows In TryCast(View, ListView).CollectionSource.Collection
                If _InvoiceRows.QTT_Will > 0 Then
                    If (_InvoiceRows.QTT_CreditNote + _InvoiceRows.QTT_Will) > _InvoiceRows.QTT Then
                        e.Cancel = True
                        Throw New Exception(String.Format("A {0} rendelésen lévő {1} sorban az eladott mennyiség kisebb, mint a visszavenni kívánt mennyiség!", _
                                                          _InvoiceRows.InvoiceHeader.DocumentNumber, _InvoiceRows.OrderInRows.Products.ShortName))
                    End If
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub InvoiceHeaderSavedSettings(ByVal _InvoiceHeader As InvoiceHeader)
        On Error GoTo CheckError
        With _InvoiceHeader
            .Saved_Customers_Name = _InvoiceHeader.Customers.Name.ToString
            .Saved_Customers_Address_Country = _InvoiceHeader.Customers.Address1.Country.ToString
            .Saved_Customers_Address_ZipPostal = _InvoiceHeader.Customers.Address1.ZipPostal.ToString
            .Saved_Customers_Address_Street = _InvoiceHeader.Customers.Address1.Street.ToString
            .Saved_Customers_Address_City = _InvoiceHeader.Customers.Address1.City.ToString
            .Saved_Customers_Address_StateProvince = _InvoiceHeader.Customers.Address1.StateProvince.ToString
            .Saved_Customers_VATNumber = _InvoiceHeader.Customers.VATNumber.ToString
            .Saved_Customers_VATNumberEU = _InvoiceHeader.Customers.VATNumberEU.ToString

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

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

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