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

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Public Class SystemTool_VC
    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
    Private Sub aSystemTool_ConnectionValidator_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aSystemTool_ConnectionValidator.Execute
        'Beep()

    End Sub
    Private Sub aSystemTool_CopyChartOfAccounts_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aSystemTool_CopyChartOfAccounts.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CopyChartOfAccounts_PopUp As New CopyChartOfAccounts_PopUp(_onew.Session)

        e.View = Application.CreateDetailView(_onew, "CopyChartOfAccounts_PopUp_DetailView", False, _CopyChartOfAccounts_PopUp)
    End Sub
    Private Sub aSystemTool_CopyChartOfAccounts_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aSystemTool_CopyChartOfAccounts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _SystemTool As SystemTool = TryCast(View.SelectedObjects(0), SystemTool)
        Dim _CopyChartOfAccounts_PopUp As CopyChartOfAccounts_PopUp = _ocur.GetObject(e.PopupWindow.View.SelectedObjects(0))
        Dim _Session_From As New Session
        Dim _Session_To As New Session

        _Session_From.ConnectionString = GetConnectionString(0, _SystemTool)
        _Session_To.ConnectionString = GetConnectionString(1, _SystemTool)
        Try
            _Session_From.Connect()
            If _Session_From.IsConnected Then
                _Session_To.Connect()
                If _Session_To.IsConnected Then
                    Dim _uow_From As New UnitOfWork(_Session_From.DataLayer)
                    Dim _uow_To As New UnitOfWork(_Session_To.DataLayer)
                    Dim _ChartOfAccounts_Collection_From As New XPCollection(Of ChartOfAccounts)(_uow_From, CriteriaOperator.Parse("AccountYear=?", _CopyChartOfAccounts_PopUp.ChartOfAccountsYear_From))
                    Dim _ChartOfAccountsYear_From As ChartOfAccountsYear = _uow_From.FindObject(Of ChartOfAccountsYear)(CriteriaOperator.Parse("AccountYear=?", _CopyChartOfAccounts_PopUp.ChartOfAccountsYear_From))

                    Dim _ChartOfAccountsYear_To As ChartOfAccountsYear = _uow_To.FindObject(Of ChartOfAccountsYear)(CriteriaOperator.Parse("AccountYear=?", _CopyChartOfAccounts_PopUp.ChartOfAccountsYear_To))
                    If _ChartOfAccountsYear_To Is Nothing Then
                        _ChartOfAccountsYear_To = New ChartOfAccountsYear(_uow_To)
                        With _ChartOfAccountsYear_To
                            .AccountYear = _CopyChartOfAccounts_PopUp.ChartOfAccountsYear_To
                        End With
                        _uow_To.CommitChanges()
                    End If

                    Dim _ChartOfAccounts_Collection_To As New XPCollection(Of ChartOfAccounts)(_uow_To, CriteriaOperator.Parse("AccountYear=?", _ChartOfAccountsYear_To.AccountYear))
                    'ChartOfAccounts.AccountNumber-re kell vizsgálni, ha van nem szabad bántani

                    RaiseEvent InitWork(1, 1, _ChartOfAccounts_Collection_From.Count)

                    If _ChartOfAccounts_Collection_To.Count = 0 Then
                        'Új év nem kell vizsgálgatni a létezőket
                        For Each _ChartOfAccounts As ChartOfAccounts In _ChartOfAccounts_Collection_From
                            Dim _ChartOfAccounts_New_To As New ChartOfAccounts(_uow_To)
                            With _ChartOfAccounts_New_To
                                .Name = _ChartOfAccounts.Name
                                .Parent = Nothing
                                .AccountNumber = _ChartOfAccounts.AccountNumber
                                .AccountYear = _ChartOfAccountsYear_To.AccountYear
                            End With
                            RaiseEvent DoWork()
                        Next
                    Else
                        'Szívás, meglévő évhez kell beszúrni dolgokat, vizsgálgatni kell az AccountNumber-eket
                        For Each _ChartOfAccounts As ChartOfAccounts In _ChartOfAccounts_Collection_From
                            If Not IsExistChartOfAccounts(_ChartOfAccounts.AccountNumber, _ChartOfAccounts_Collection_To) Then
                                Dim _ChartOfAccounts_New_To As New ChartOfAccounts(_uow_To)
                                With _ChartOfAccounts_New_To
                                    .Name = _ChartOfAccounts.Name
                                    .Parent = Nothing
                                    .AccountNumber = _ChartOfAccounts.AccountNumber
                                    .AccountYear = _ChartOfAccountsYear_To.AccountYear
                                End With
                            End If
                            RaiseEvent DoWork()
                        Next
                    End If

                    _uow_To.CommitChanges()

                    RaiseEvent FinalWork

                    _ChartOfAccounts_Collection_To = New XPCollection(Of ChartOfAccounts)(_uow_To, CriteriaOperator.Parse("AccountYear=?", _ChartOfAccountsYear_To.AccountYear))

                    RaiseEvent InitWork(1, 1, _ChartOfAccounts_Collection_To.Count)
                    For Each _ChartOfAccounts As ChartOfAccounts In _ChartOfAccounts_Collection_To
                        RecursiveSetParent(_uow_To, _ChartOfAccountsYear_To.AccountYear, _ChartOfAccounts.AccountNumber, _ChartOfAccounts)
                        RaiseEvent DoWork()
                    Next
                    _uow_To.CommitChanges()
                    RaiseEvent FinalWork


                    'Select Case _SystemTool.SQLType_To
                    '    Case eSQLType.MSSQL
                    '        _uow_To.ExecuteNonQuery("ALTER TABLE ChartOfAccountsYear NOCHECK CONSTRAINT ALL")
                    '        _uow_To.ExecuteNonQuery("UPDATE ChartOfAccountsYear SET Oid='" + _ChartOfAccountsYear_From.Oid.ToString + "' WHERE Oid='" + _ChartOfAccountsYear_New.Oid.ToString + "'")
                    '        _uow_To.ExecuteNonQuery("ALTER TABLE ChartOfAccountsYear CHECK CONSTRAINT ALL")
                    '    Case eSQLType.MySQL
                    '        '--==Oid regenerálás==--
                    '        _uow_To.ExecuteNonQuery("UPDATE ChartOfAccountsYear SET Oid='" + _ChartOfAccountsYear_From.Oid.ToString + "' WHERE Oid='" + _ChartOfAccountsYear_New.Oid.ToString + "'")
                    '        '--===================--
                    '    Case eSQLType.PostgreSQL
                    '        _uow_To.ExecuteNonQuery("UPDATE ""ChartOfAccountsYear"" SET ""Oid""='" + _ChartOfAccountsYear_From.Oid.ToString + "' WHERE ""Oid""='" + _ChartOfAccountsYear_New.Oid.ToString + "'")
                    'End Select


                    'RaiseEvent InitWork(1, 1, _ChartOfAccounts_Collection.Count)
                    'For Each _ChartOfAccounts As ChartOfAccounts In _ChartOfAccounts_Collection
                    '    Dim _ChartOfAccounts_New As New ChartOfAccounts(_uow_To)
                    '    With _ChartOfAccounts_New
                    '        .Name = _ChartOfAccounts.Name
                    '        .Parent = Nothing
                    '        .AccountNumber = _ChartOfAccounts.AccountNumber
                    '        .AccountYear = _CopyChartOfAccounts_PopUp.TargetAccountYear

                    '    End With
                    '    _uow_To.CommitChanges()

                    '      Select _SystemTool.SQLType_To
                    '        Case eSQLType.MSSQL
                    '            _uow_To.ExecuteNonQuery("ALTER TABLE HCategory NOCHECK CONSTRAINT ALL")
                    '            _uow_To.ExecuteNonQuery("ALTER TABLE ChartOfAccounts NOCHECK CONSTRAINT ALL")
                    '            _uow_To.ExecuteNonQuery("UPDATE HCategory SET Oid='" + _ChartOfAccounts.Oid.ToString + "' WHERE Oid='" + _ChartOfAccounts_New.Oid.ToString + "'")
                    '            _uow_To.ExecuteNonQuery("UPDATE ChartOfAccounts SET Oid='" + _ChartOfAccounts.Oid.ToString + "' WHERE Oid='" + _ChartOfAccounts_New.Oid.ToString + "'")
                    '            _uow_To.ExecuteNonQuery("ALTER TABLE HCategory CHECK CONSTRAINT ALL")
                    '            _uow_To.ExecuteNonQuery("ALTER TABLE ChartOfAccounts CHECK CONSTRAINT ALL")
                    '        Case eSQLType.MySQL
                    '            '--==Oid regenerálás==--
                    '            _uow_To.ExecuteNonQuery("SET foreign_key_checks = 0")
                    '            _uow_To.ExecuteNonQuery("UPDATE HCategory SET Oid='" + _ChartOfAccounts.Oid.ToString + "' WHERE Oid='" + _ChartOfAccounts_New.Oid.ToString + "'")
                    '            _uow_To.ExecuteNonQuery("UPDATE ChartOfAccounts SET Oid='" + _ChartOfAccounts.Oid.ToString + "' WHERE Oid='" + _ChartOfAccounts_New.Oid.ToString + "'")
                    '            _uow_To.ExecuteNonQuery("SET foreign_key_checks = 1")
                    '            '--===================--
                    '        Case eSQLType.PostgreSQL
                    '            _uow_To.ExecuteNonQuery("ALTER TABLE ""HCategory"" DISABLE TRIGGER ALL")
                    '            _uow_To.ExecuteNonQuery("ALTER TABLE ""ChartOfAccounts"" DISABLE TRIGGER ALL")
                    '            _uow_To.ExecuteNonQuery("UPDATE ""HCategory"" SET ""Oid""='" + _ChartOfAccounts.Oid.ToString + "' WHERE ""Oid""='" + _ChartOfAccounts_New.Oid.ToString + "'")
                    '            _uow_To.ExecuteNonQuery("UPDATE ""ChartOfAccounts"" SET ""Oid""='" + _ChartOfAccounts.Oid.ToString + "' WHERE ""Oid""='" + _ChartOfAccounts_New.Oid.ToString + "'")
                    '            _uow_To.ExecuteNonQuery("ALTER TABLE ""HCategory"" ENABLE TRIGGER ALL")
                    '            _uow_To.ExecuteNonQuery("ALTER TABLE ""ChartOfAccounts"" ENABLE TRIGGER ALL")
                    '    End Select
                    '    RaiseEvent DoWork()
                    'Next
                    'RaiseEvent FinalWork


                    '_ChartOfAccounts_Collection = New XPCollection(Of ChartOfAccounts)(_uow_To, CriteriaOperator.Parse("AccountYear=?", _CopyChartOfAccounts_PopUp.TargetAccountYear))
                    'RaiseEvent InitWork(1, 1, _ChartOfAccounts_Collection.Count)

                    'For Each _ChartOfAccounts In _ChartOfAccounts_Collection
                    '    RecursiveSetParent(_uow_To, _CopyChartOfAccounts_PopUp.TargetAccountYear, _ChartOfAccounts.AccountNumber, _ChartOfAccounts)
                    '    RaiseEvent DoWork()
                    'Next
                    '_uow_To.CommitChanges()
                    'RaiseEvent FinalWork
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If _Session_From.IsConnected Then _Session_From.Disconnect()
            If _Session_To.IsConnected Then _Session_To.Disconnect()
            RaiseEvent FinalWork
        End Try
    End Sub
    Private Function GetConnectionString(_Type As Integer, _SystemTool As SystemTool) As String
        Dim _ConnectionString As String = ""
        If _Type = 0 Then
            Select Case _SystemTool.SQLType_From
                Case eSQLType.MSSQL
                    _ConnectionString = DevExpress.Xpo.DB.MSSqlConnectionProvider.GetConnectionString(_SystemTool.SQLServer_From, _SystemTool.SQLUser_From, _SystemTool.SQLPassword_From, _SystemTool.SQLDatabase_From)
                Case eSQLType.MySQL
                    _ConnectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_SystemTool.SQLServer_From, _SystemTool.SQLUser_From, _SystemTool.SQLPassword_From, _SystemTool.SQLDatabase_From)
                Case eSQLType.PostgreSQL
                    _ConnectionString = DevExpress.Xpo.DB.PostgreSqlConnectionProvider.GetConnectionString(_SystemTool.SQLServer_From, _SystemTool.SQLUser_From, _SystemTool.SQLPassword_From, _SystemTool.SQLDatabase_From)
            End Select
        ElseIf _Type = 1 Then
            Select Case _SystemTool.SQLType_To
                Case eSQLType.MSSQL
                    _ConnectionString = DevExpress.Xpo.DB.MSSqlConnectionProvider.GetConnectionString(_SystemTool.SQLServer_To, _SystemTool.SQLUser_To, _SystemTool.SQLPassword_To, _SystemTool.SQLDatabase_To)
                Case eSQLType.MySQL
                    _ConnectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_SystemTool.SQLServer_To, _SystemTool.SQLUser_To, _SystemTool.SQLPassword_To, _SystemTool.SQLDatabase_To)
                Case eSQLType.PostgreSQL
                    _ConnectionString = DevExpress.Xpo.DB.PostgreSqlConnectionProvider.GetConnectionString(_SystemTool.SQLServer_To, _SystemTool.SQLUser_To, _SystemTool.SQLPassword_To, _SystemTool.SQLDatabase_To)
            End Select
        End If

        Return _ConnectionString
    End Function
    Private Sub RecursiveSetParent(_uow As UnitOfWork, _Year As Long, _AccountNumber As String, _ChartOfAccount_Set As ChartOfAccounts)
        Dim _AccountNumber_Parent As String = LTrim(RTrim(Mid(_AccountNumber, 1, Len(_AccountNumber) - 1)))
        Dim _ChartOfAccounts_Parent As ChartOfAccounts

        If _AccountNumber_Parent = "" Then
        Else
            _ChartOfAccounts_Parent = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountYear=? and AccountNumber=?", _Year, _AccountNumber_Parent))
            If _ChartOfAccounts_Parent Is Nothing Then
                RecursiveSetParent(_uow, _Year, _AccountNumber_Parent, _ChartOfAccount_Set)
            Else
                _ChartOfAccount_Set.Parent = _ChartOfAccounts_Parent
                _uow.CommitChanges()

            End If
        End If
    End Sub
    Private Sub aSystemTool_CopyVAT_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aSystemTool_CopyVAT.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _SystemTool As SystemTool = TryCast(View.SelectedObjects(0), SystemTool)
        Dim _VAT_Collection As XPCollection(Of VAT)
        Dim _Session_From As New Session
        Dim _Session_To As New Session
        _Session_From.ConnectionString = GetConnectionString(0, _SystemTool)
        _Session_To.ConnectionString = GetConnectionString(1, _SystemTool)
        Try
            _Session_From.Connect()
            If _Session_From.IsConnected Then
                _Session_To.Connect()
                If _Session_To.IsConnected Then

                    Dim _uow_From As New UnitOfWork(_Session_From.DataLayer)
                    Dim _uow_To As New UnitOfWork(_Session_To.DataLayer)
                    _VAT_Collection = New XPCollection(Of VAT)(_uow_From, CriteriaOperator.Parse("1=1"))

                    RaiseEvent InitWork(1, 1, _VAT_Collection.Count)
                    For Each _VAT As VAT In _VAT_Collection
                        Dim _VAT_New As New VAT(_uow_To)
                        With _VAT_New
                            .ShortName = _VAT.ShortName
                            .Description = _VAT.Description
                            .KeyValue = _VAT.KeyValue
                            .ChartOfAccountsOut = _uow_To.GetObjectByKey(Of ChartOfAccounts)(_VAT.ChartOfAccountsOut.Oid)
                            .ChartOfAccountsIn = _uow_To.GetObjectByKey(Of ChartOfAccounts)(_VAT.ChartOfAccountsIn.Oid)
                            .ClaimState = _VAT.ClaimState
                            .Active = _VAT.Active
                            .InversState = _VAT.InversState
                            .InversKeyValue = _VAT.InversKeyValue
                            .DefaultForBusinnes = _VAT.DefaultForBusinnes
                            .DefaultForExternalEUNoVAT = _VAT.DefaultForExternalEUNoVAT
                            .DefaultForExternalOther = _VAT.DefaultForExternalOther
                            .DefaultForInversVAT = _VAT.DefaultForInversVAT
                        End With
                        _uow_To.CommitChanges()
                        Select Case _SystemTool.SQLType_To
                            Case eSQLType.MSSQL
                                '--==Oid regenerálás==--
                                _uow_To.ExecuteNonQuery("UPDATE VAT SET Oid='" + _VAT.Oid.ToString + "' WHERE Oid='" + _VAT_New.Oid.ToString + "'")
                                '--===================--
                            Case eSQLType.MySQL
                                '--==Oid regenerálás==--
                                _uow_To.ExecuteNonQuery("UPDATE VAT SET Oid='" + _VAT.Oid.ToString + "' WHERE Oid='" + _VAT_New.Oid.ToString + "'")
                                '--===================--
                            Case eSQLType.PostgreSQL
                                '--==Oid regenerálás==--
                                _uow_To.ExecuteNonQuery("UPDATE ""VAT"" SET ""Oid""='" + _VAT.Oid.ToString + "' WHERE ""Oid""='" + _VAT_New.Oid.ToString + "'")
                                '--===================--
                        End Select

                        RaiseEvent DoWork()
                    Next
                    RaiseEvent FinalWork
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If _Session_From.IsConnected Then _Session_From.Disconnect()
            If _Session_To.IsConnected Then _Session_To.Disconnect()
            RaiseEvent FinalWork
        End Try
    End Sub
    Private Sub aSystemTool_CopyCostHolder_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aSystemTool_CopyCostHolder.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _SystemTool As SystemTool = TryCast(View.SelectedObjects(0), SystemTool)
        Dim _CostHolder_Collection As XPCollection(Of CostHolder)
        Dim _Session_From As New Session
        Dim _Session_To As New Session
        _Session_From.ConnectionString = GetConnectionString(0, _SystemTool)
        _Session_To.ConnectionString = GetConnectionString(1, _SystemTool)
        Try
            _Session_From.Connect()
            If _Session_From.IsConnected Then
                _Session_To.Connect()
                If _Session_To.IsConnected Then

                    Dim _uow_From As New UnitOfWork(_Session_From.DataLayer)
                    Dim _uow_To As New UnitOfWork(_Session_To.DataLayer)
                    _CostHolder_Collection = New XPCollection(Of CostHolder)(_uow_From, CriteriaOperator.Parse("1=1"))

                    RaiseEvent InitWork(1, 1, _CostHolder_Collection.Count)
                    For Each _CostHolder As CostHolder In _CostHolder_Collection
                        Dim _CostHolder_New As New CostHolder(_uow_To)
                        With _CostHolder_New
                            .Name = _CostHolder.Name
                            If _CostHolder.Parent IsNot Nothing Then
                                Dim _Parent As CostHolder = _uow_From.FindObject(Of CostHolder)(CriteriaOperator.Parse("Oid=?", _CostHolder.Parent.Oid))
                                .Parent = _uow_To.GetObjectByKey(Of CostHolder)(_Parent.Oid)
                            End If                   
                        End With
                        _uow_To.CommitChanges()

                         Select _SystemTool.SQLType_To
                            Case eSQLType.MSSQL
                                _uow_To.ExecuteNonQuery("ALTER TABLE HCategory NOCHECK CONSTRAINT ALL")
                                _uow_To.ExecuteNonQuery("ALTER TABLE CostHolder NOCHECK CONSTRAINT ALL")
                                _uow_To.ExecuteNonQuery("UPDATE HCategory SET Oid='" + _CostHolder.Oid.ToString + "' WHERE Oid='" + _CostHolder_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("UPDATE CostHolder SET Oid='" + _CostHolder.Oid.ToString + "' WHERE Oid='" + _CostHolder_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("ALTER TABLE HCategory CHECK CONSTRAINT ALL")
                                _uow_To.ExecuteNonQuery("ALTER TABLE CostHolder CHECK CONSTRAINT ALL")
                            Case eSQLType.MySQL
                                '--==Oid regenerálás==--
                                _uow_To.ExecuteNonQuery("SET foreign_key_checks = 0")
                                _uow_To.ExecuteNonQuery("UPDATE HCategory SET Oid='" + _CostHolder.Oid.ToString + "' WHERE Oid='" + _CostHolder_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("UPDATE CostHolder SET Oid='" + _CostHolder.Oid.ToString + "' WHERE Oid='" + _CostHolder_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("SET foreign_key_checks = 1")
                                '--===================--
                            Case eSQLType.PostgreSQL
                                _uow_To.ExecuteNonQuery("ALTER TABLE ""HCategory"" DISABLE TRIGGER ALL")
                                _uow_To.ExecuteNonQuery("ALTER TABLE ""CostHolder"" DISABLE TRIGGER ALL")
                                _uow_To.ExecuteNonQuery("UPDATE ""CostHolder"" SET ""Oid""='" + _CostHolder.Oid.ToString + "' WHERE ""Oid""='" + _CostHolder_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("UPDATE ""HCategory"" SET ""Oid""='" + _CostHolder.Oid.ToString + "' WHERE ""Oid""='" + _CostHolder_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("ALTER TABLE ""HCategory"" ENABLE TRIGGER ALL")
                                _uow_To.ExecuteNonQuery("ALTER TABLE ""CostHolder"" ENABLE TRIGGER ALL")
                        End Select

                        RaiseEvent DoWork()
                    Next
                    RaiseEvent FinalWork
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If _Session_From.IsConnected Then _Session_From.Disconnect()
            If _Session_To.IsConnected Then _Session_To.Disconnect()
            RaiseEvent FinalWork
        End Try
    End Sub
    Private Sub aSystemTool_CopyControlling_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aSystemTool_CopyControlling.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _SystemTool As SystemTool = TryCast(View.SelectedObjects(0), SystemTool)
        Dim _Controlling_Collection As XPCollection(Of Controlling)
        Dim _Session_From As New Session
        Dim _Session_To As New Session
        _Session_From.ConnectionString = GetConnectionString(0, _SystemTool)
        _Session_To.ConnectionString = GetConnectionString(1, _SystemTool)
        Try
            _Session_From.Connect()
            If _Session_From.IsConnected Then
                _Session_To.Connect()
                If _Session_To.IsConnected Then

                    Dim _uow_From As New UnitOfWork(_Session_From.DataLayer)
                    Dim _uow_To As New UnitOfWork(_Session_To.DataLayer)
                    _Controlling_Collection = New XPCollection(Of Controlling)(_uow_From, CriteriaOperator.Parse("1=1"))

                    RaiseEvent InitWork(1, 1, _Controlling_Collection.Count)
                    For Each _Controlling As Controlling In _Controlling_Collection
                        Dim _Controlling_New As New Controlling(_uow_To)
                        With _Controlling_New
                            .Name = _Controlling.Name
                            If _Controlling.Parent IsNot Nothing Then
                                Dim _Parent As Controlling = _uow_From.FindObject(Of Controlling)(CriteriaOperator.Parse("Oid=?", _Controlling.Parent.Oid))
                                .Parent = _uow_To.GetObjectByKey(Of Controlling)(_Parent.Oid)
                            End If
                            If _Controlling.ChartOfAccounts IsNot Nothing Then
                                .ChartOfAccounts = _uow_To.GetObjectByKey(Of ChartOfAccounts)(_Controlling.ChartOfAccounts.Oid)
                            End If
                            If _Controlling.ChartOfAccounts2 IsNot Nothing Then
                                .ChartOfAccounts2 = _uow_To.GetObjectByKey(Of ChartOfAccounts)(_Controlling.ChartOfAccounts2.Oid)
                            End If
                            .IsCOCRequired = _Controlling.IsCOCRequired
                            .COCAboveAmount = _Controlling.COCAboveAmount
                            .IsParent = _Controlling.IsParent
                            .Controllingdirection = _Controlling.Controllingdirection
                            .IsReInvoiceReason = _Controlling.IsReInvoiceReason
                            .ReInvoiceDescription = _Controlling.ReInvoiceDescription
                            .TechnicalContractLimit = _Controlling.TechnicalContractLimit
                            If _Controlling.Controlling1 IsNot Nothing Then
                                .Controlling1 = _uow_To.GetObjectByKey(Of Controlling)(_Controlling.Controlling1.Oid)
                            Else
                                .Controlling1 = Nothing
                            End If
                            If _Controlling.Controlling2 IsNot Nothing Then
                                .Controlling2 = _uow_To.GetObjectByKey(Of Controlling)(_Controlling.Controlling2.Oid)
                            Else
                                .Controlling2 = Nothing
                            End If
                            If _Controlling.Controlling3 IsNot Nothing Then
                                .Controlling3 = _uow_To.GetObjectByKey(Of Controlling)(_Controlling.Controlling3.Oid)
                            Else
                                .Controlling3 = Nothing
                            End If
                            If _Controlling.Controlling4 IsNot Nothing Then
                                .Controlling4 = _uow_To.GetObjectByKey(Of Controlling)(_Controlling.Controlling4.Oid)
                            Else
                                .Controlling4 = Nothing
                            End If
                            If _Controlling.Controlling5 IsNot Nothing Then
                                .Controlling5 = _uow_To.GetObjectByKey(Of Controlling)(_Controlling.Controlling5.Oid)
                            Else
                                .Controlling5 = Nothing
                            End If
                            .IsAdvancePayment = _Controlling.IsAdvancePayment
                            .IsEstateStatistics = _Controlling.IsEstateStatistics
                        End With
                        _uow_To.CommitChanges()


                         Select _SystemTool.SQLType_To
                            Case eSQLType.MSSQL
                                _uow_To.ExecuteNonQuery("ALTER TABLE HCategory NOCHECK CONSTRAINT ALL")
                                _uow_To.ExecuteNonQuery("ALTER TABLE Controlling NOCHECK CONSTRAINT ALL")
                                _uow_To.ExecuteNonQuery("UPDATE HCategory SET Oid='" + _Controlling.Oid.ToString + "' WHERE Oid='" + _Controlling_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("UPDATE Controlling SET Oid='" + _Controlling.Oid.ToString + "' WHERE Oid='" + _Controlling_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("ALTER TABLE HCategory CHECK CONSTRAINT ALL")
                                _uow_To.ExecuteNonQuery("ALTER TABLE Controlling CHECK CONSTRAINT ALL")
                            Case eSQLType.MySQL
                                '--==Oid regenerálás==--
                                _uow_To.ExecuteNonQuery("SET foreign_key_checks = 0")
                                _uow_To.ExecuteNonQuery("UPDATE HCategory SET Oid='" + _Controlling.Oid.ToString + "' WHERE Oid='" + _Controlling_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("UPDATE Controlling SET Oid='" + _Controlling.Oid.ToString + "' WHERE Oid='" + _Controlling_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("SET foreign_key_checks = 1")
                                '--===================--
                            Case eSQLType.PostgreSQL
                                _uow_To.ExecuteNonQuery("ALTER TABLE ""HCategory"" DISABLE TRIGGER ALL")
                                _uow_To.ExecuteNonQuery("ALTER TABLE ""Controlling"" DISABLE TRIGGER ALL")
                                _uow_To.ExecuteNonQuery("UPDATE ""HCategory"" SET ""Oid""='" + _Controlling.Oid.ToString + "' WHERE ""Oid""='" + _Controlling_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("UPDATE ""Controlling"" SET ""Oid""='" + _Controlling.Oid.ToString + "' WHERE ""Oid""='" + _Controlling_New.Oid.ToString + "'")
                                _uow_To.ExecuteNonQuery("ALTER TABLE ""HCategory"" ENABLE TRIGGER ALL")
                                _uow_To.ExecuteNonQuery("ALTER TABLE ""Controlling"" ENABLE TRIGGER ALL")

                        End Select

                        RaiseEvent DoWork()
                    Next
                    RaiseEvent FinalWork
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If _Session_From.IsConnected Then _Session_From.Disconnect()
            If _Session_To.IsConnected Then _Session_To.Disconnect()
            RaiseEvent FinalWork
        End Try
    End Sub
    Private Sub aSystemTool_ExportOids_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aSystemTool_ExportOids.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ExportOids_PopUp As New ExportOids_PopUp(_onew.Session)

        e.View = Application.CreateDetailView(_onew, "ExportOids_PopUp_DetailView", False, _ExportOids_PopUp)
    End Sub
    Private Sub aSystemTool_ExportOids_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aSystemTool_ExportOids.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ExportOids_PopUp As ExportOids_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ExportOids_PopUp)
        If _ExportOids_PopUp IsNot Nothing Then
            Dim _FileName As String = IO.Path.GetTempPath + "Export.txt"
            If IO.File.Exists(_FileName) Then IO.File.Delete(_FileName)
            Dim _StreamWriter As New System.IO.StreamWriter(_FileName, True)
            Select Case _ExportOids_PopUp.ExportOidClass
                Case eExportOidClass.Controlling
                    Dim _Controlling_Collection As New XPCollection(Of Controlling)(_ocur.Session, CriteriaOperator.Parse("1=1"))
                    _StreamWriter.WriteLine("<Services_Controlling>")
                    _StreamWriter.WriteLine("<!--szolgaltatascsoport id - Controlling Oid-->")
                    Dim _Index As Integer = 1
                    For Each _Controlling As Controlling In _Controlling_Collection
                        _StreamWriter.WriteLine("<!--" + _Controlling.Name + "-->")
                        _StreamWriter.WriteLine("<add key=" + _Index.ToString + " value=" + _Controlling.Oid.ToString + "></add>")
                        _Index += 1
                    Next
                    _StreamWriter.WriteLine("</Services_Controlling>")
                Case eExportOidClass.CostHolder
                    Dim _CostHolder_Collection As New XPCollection(Of CostHolder)(_ocur.Session, CriteriaOperator.Parse("1=1"))
                    _StreamWriter.WriteLine("<Services_CostHolder>")
                    _StreamWriter.WriteLine("<!--szolgaltatascsoport id - CostHolder Oid-->")
                    Dim _Index As Integer = 1
                    For Each _CostHolder As CostHolder In _CostHolder_Collection
                        _StreamWriter.WriteLine("<!--" + _CostHolder.Name + "-->")
                        _StreamWriter.WriteLine("<add key=" + _Index.ToString + " value=" + _CostHolder.Oid.ToString + "></add>")
                        _Index += 1
                    Next
                    _StreamWriter.WriteLine("</Services_CostHolder>")
                Case eExportOidClass.VAT
                    Dim _VAT_Collection As New XPCollection(Of VAT)(_ocur.Session, CriteriaOperator.Parse("1=1"))
                    _StreamWriter.WriteLine("<Services_VAT>")
                    _StreamWriter.WriteLine("<!--afanev - VAT Oid-->")
                    Dim _Index As Integer = 1
                    For Each _VAT As VAT In _VAT_Collection
                        _StreamWriter.WriteLine("<!--" + _VAT.ShortName + "-->")
                        _StreamWriter.WriteLine("<add key=" + _VAT.KeyValuePercent.ToString + " value=" + _VAT.Oid.ToString + "></add>")
                    Next
                    _StreamWriter.WriteLine("</Services_VAT>")
            End Select
            _StreamWriter.Close()

            If _ExportOids_PopUp.OpenFileAfterExport Then
                Process.Start(_FileName)
            End If
        End If

    End Sub
    Private Sub aSystemTool_CopyCustomersGLParameters_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aSystemTool_CopyCustomersGLParameters.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _SystemTool As SystemTool = TryCast(View.SelectedObjects(0), SystemTool)
        Dim _CustomersGLParameters_Collection As XPCollection(Of CustomersGLParameters)
        Dim _Session_From As New Session
        Dim _Session_To As New Session
        _Session_From.ConnectionString = GetConnectionString(0, _SystemTool)
        _Session_To.ConnectionString = GetConnectionString(1, _SystemTool)
        Try
            _Session_From.Connect()
            If _Session_From.IsConnected Then
                _Session_To.Connect()
                If _Session_To.IsConnected Then

                    Dim _uow_From As New UnitOfWork(_Session_From.DataLayer)
                    Dim _uow_To As New UnitOfWork(_Session_To.DataLayer)
                    _CustomersGLParameters_Collection = New XPCollection(Of CustomersGLParameters)(_uow_From, CriteriaOperator.Parse("1=1"))

                    RaiseEvent InitWork(1, 1, _CustomersGLParameters_Collection.Count)
                    For Each _CustomersGLParameters As CustomersGLParameters In _CustomersGLParameters_Collection
                        Dim _CustomersGLParameters_New As New CustomersGLParameters(_uow_To)
                        With _CustomersGLParameters_New
                            .ShortName = _CustomersGLParameters.ShortName
                            .DefaultforBusiness = _CustomersGLParameters.DefaultforBusiness
                        End With
                        _uow_To.CommitChanges()

                        Select Case _SystemTool.SQLType_To
                            Case eSQLType.MSSQL
                                _uow_To.ExecuteNonQuery("UPDATE CustomersGLParameters SET Oid='" + _CustomersGLParameters.Oid.ToString + "' WHERE Oid='" + _CustomersGLParameters_New.Oid.ToString + "'")
                            Case eSQLType.MySQL
                                '--==Oid regenerálás==--
                                _uow_To.ExecuteNonQuery("UPDATE CustomersGLParameters SET Oid='" + _CustomersGLParameters.Oid.ToString + "' WHERE Oid='" + _CustomersGLParameters_New.Oid.ToString + "'")
                                '--===================--
                            Case eSQLType.PostgreSQL
                                _uow_To.ExecuteNonQuery("UPDATE ""CustomersGLParameters"" SET ""Oid""='" + _CustomersGLParameters.Oid.ToString + "' WHERE ""Oid""='" + _CustomersGLParameters_New.Oid.ToString + "'")
                        End Select

                        RaiseEvent DoWork()
                    Next
                    RaiseEvent FinalWork
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If _Session_From.IsConnected Then _Session_From.Disconnect()
            If _Session_To.IsConnected Then _Session_To.Disconnect()
            RaiseEvent FinalWork
        End Try
    End Sub
    Private Sub aSystemTool_CopyUnits_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aSystemTool_CopyUnits.Execute
        'Beep()

    End Sub
    Private Sub aSystemTool_CopyWorkflowSystem_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aSystemTool_CopyWorkflowSystem.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _SystemTool As SystemTool = TryCast(View.SelectedObjects(0), SystemTool)
        Dim _Session_From As New Session
        _Session_From.ConnectionString = GetConnectionString(0, _SystemTool)
        _Session_From.Connect()
        If _Session_From.IsConnected Then
            Dim _CopyWorkflowSystem_CollectionSource As New CollectionSource(_onew, GetType(CopyWorkflowSystem_PopUp))
            Dim _WorkflowSystem_Collection As New XPCollection(Of WorkflowSystem)(_Session_From)

            If _WorkflowSystem_Collection.Count > 0 Then
                For Each _WorkflowSystem As WorkflowSystem In _WorkflowSystem_Collection
                    Dim _CopyWorkflowSystem_PopUp As New CopyWorkflowSystem_PopUp(_onew.Session)
                    With _CopyWorkflowSystem_PopUp
                        .NeedToCopy = True
                        .WorkflowSystem = _onew.GetObject(_WorkflowSystem)
                    End With
                    _CopyWorkflowSystem_CollectionSource.Add(_CopyWorkflowSystem_PopUp)
                Next
            End If
            e.View = Application.CreateListView("CopyWorkflowSystem_PopUp_ListView", _CopyWorkflowSystem_CollectionSource, True)
            _Session_From.Disconnect()
        End If

    End Sub
    Private Sub aSystemTool_CopyWorkflowSystem_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aSystemTool_CopyWorkflowSystem.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _SystemTool As SystemTool = TryCast(View.SelectedObjects(0), SystemTool)
            Dim _Session_From As New Session
            Dim _Session_To As New Session
            _Session_From.ConnectionString = GetConnectionString(0, _SystemTool)
            _Session_To.ConnectionString = GetConnectionString(1, _SystemTool)

            _Session_From.Connect()
            If _Session_From.IsConnected Then
                _Session_To.Connect()
                If _Session_To.IsConnected Then
                    RaiseEvent InitWork(1, 1, TryCast(e.PopupWindow.View, ListView).CollectionSource.GetCount())
                    Dim _uow_To As New UnitOfWork(_Session_To.DataLayer)

                    For Each _CopyWorkflowSystem_PopUp As CopyWorkflowSystem_PopUp In TryCast(e.PopupWindow.View, ListView).CollectionSource.Collection
                        If _CopyWorkflowSystem_PopUp.NeedToCopy Then
                            Dim _WorkflowSystem As New WorkflowSystem(_uow_To)
                            With _WorkflowSystem
                                .CustomersFrom = _uow_To.FindObject(Of CustomersFrom)(CriteriaOperator.Parse("IsDefault=True"))
                                .PartnerType = _CopyWorkflowSystem_PopUp.WorkflowSystem.PartnerType
                                .ShortName = _CopyWorkflowSystem_PopUp.WorkflowSystem.ShortName
                                .Description = _CopyWorkflowSystem_PopUp.WorkflowSystem.Description
                            End With

                            For Each _WorkflowSystemSteps As WorkflowSystemSteps In _CopyWorkflowSystem_PopUp.WorkflowSystem.WorkflowSystemSteps
                                Dim _WorkflowSystemSteps_NEW As New WorkflowSystemSteps(_uow_To)
                                With _WorkflowSystemSteps_NEW
                                    .WorkflowSystem = _WorkflowSystem
                                    .ShortName = _WorkflowSystemSteps.ShortName
                                    .StepIndex = _WorkflowSystemSteps.StepIndex
                                    .TaskDescription = _WorkflowSystemSteps.TaskDescription
                                    .ExpirationDayofFailure = _WorkflowSystemSteps.ExpirationDayofFailure
                                    .ActionOnSuccess = _WorkflowSystemSteps.ActionOnSuccess
                                    .ActionOnFailure = _WorkflowSystemSteps.ActionOnFailure
                                    .IsLastStep = _WorkflowSystemSteps.IsLastStep
                                    .IsReadyForImport = _WorkflowSystemSteps.IsReadyForImport
                                    .StepFinancialType = _WorkflowSystemSteps.StepFinancialType
                                End With
                            Next
                            _uow_To.CommitChanges()
                            RaiseEvent DoWork()
                        End If
                    Next
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        Finally
            RaiseEvent FinalWork
        End Try
    End Sub
    Private Function IsExistChartOfAccounts(_AccountNumberIn As String, _ChartOfAccounts_CollectionIn As XPCollection(Of ChartOfAccounts)) As Boolean
        Dim _Result As Boolean = False
        For Each _ChartOfAccounts As ChartOfAccounts In _ChartOfAccounts_CollectionIn
            If _ChartOfAccounts.AccountNumber = _AccountNumberIn Then
                _Result = True
                Exit For
            End If
        Next

        Return _Result
    End Function

End Class
