Imports System.Xml
Imports DevExpress.Xpo
Imports System.Configuration
Imports SISBusiness.RabbitMQ.Module
Imports DevExpress.Data.Filtering
Imports System.Collections.Specialized
Imports DevExpress.Persistent.BaseImpl
Imports SISBusiness.Module

Public Module GetCustomers
    Public Sub CustomerImport(_XML As XmlDocument, _RootXMLTag As String, _errorLogFile As String, _Session As DevExpress.Xpo.Session, Optional IsService As Boolean = False)
        Try
            Dim _Oid_DH As String = _XML.SelectSingleNode(_RootXMLTag + "/id").InnerText
            Dim _uow As UnitOfWork
            If _Session.IsConnected Then
                _uow = New UnitOfWork(_Session.DataLayer)
                _uow.Dictionary.GetDataStoreSchema(GetType(Customers).Assembly)
                InitAuditTrail(_uow)
                If Not IsService Then
                    WriteErrorLog(String.Format("Fogadás... {0} OID_DH:{1} Ügyfélszám:{2}", GLOBAL_Index.ToString, _Oid_DH, _XML.SelectSingleNode(_RootXMLTag + "/ugyfelszam").InnerText), _errorLogFile)
                End If
                GLOBAL_Index += 1


                Dim _Customers As Customers = _uow.FindObject(Of Customers)(CriteriaOperator.Parse("CustomerNumber=?", _Oid_DH))
                If _Customers Is Nothing Then
                    _Customers = _uow.FindObject(Of Customers)(CriteriaOperator.Parse("Oid_DH=?", _Oid_DH))
                End If

                If _Customers IsNot Nothing Then
                    _Customers.Oid_DH = _Oid_DH
                    If _Customers.Name <> _XML.SelectSingleNode(_RootXMLTag + "/nev").InnerText Then
                        _Customers.Name = _XML.SelectSingleNode(_RootXMLTag + "/nev").InnerText
                        _Customers.FullName = _Customers.Name
                    End If
                    _Customers.Url_DH = _XML.SelectSingleNode(_RootXMLTag + "/dh_url").InnerText
                    Dim _konyvelesi_parameter As String = _XML.SelectSingleNode(_RootXMLTag + "/konyvelesi_parameter").InnerText
                    Dim _NameValueCollection As NameValueCollection = ConfigurationManager.GetSection("CustomersGLParameters")

                    Dim _CustomersGLParameters As CustomersGLParameters = _uow.FindObject(Of CustomersGLParameters)(CriteriaOperator.Parse("Oid=?", _NameValueCollection(_konyvelesi_parameter)))
                    _Customers.CustomersGLParameters = _CustomersGLParameters
                    Dim _Address1City As String = _XML.SelectSingleNode(_RootXMLTag + "/varos").InnerText
                    Dim _Address1Street As String = _XML.SelectSingleNode(_RootXMLTag + "/utca").InnerText + " " + _XML.SelectSingleNode(_RootXMLTag + "/hazszam").InnerText
                    Dim _Address1ZipPostal As String = _XML.SelectSingleNode(_RootXMLTag + "/irszam").InnerText
                    Dim _Address1 As Address = _uow.FindObject(Of Address)(CriteriaOperator.Parse("City=? AND Street=? AND ZipPostal=?", _Address1City, _Address1Street, _Address1ZipPostal))

                    If _Address1 Is Nothing Then
                        _Address1 = New Address(_uow)
                        With _Address1
                            .City = _Address1City
                            .Street = _Address1Street
                            .ZipPostal = _Address1ZipPostal
                        End With

                        _Customers.Address1 = _Address1
                    End If

                    Dim _Address2City As String = _XML.SelectSingleNode(_RootXMLTag + "/levelezesivaros").InnerText
                    Dim _Address2Street As String = _XML.SelectSingleNode(_RootXMLTag + "/levelezesiutca").InnerText + " " + _XML.SelectSingleNode(_RootXMLTag + "/levelezesihazszam").InnerText
                    Dim _Address2ZipPostal As String = _XML.SelectSingleNode(_RootXMLTag + "/levelezesiirszam").InnerText
                    Dim _Address2 As Address = _uow.FindObject(Of Address)(CriteriaOperator.Parse("City=? AND Street=? AND ZipPostal=?", _Address2City, _Address2Street, _Address2ZipPostal))

                    If _Address2 Is Nothing Then
                        _Address2 = New Address(_uow)
                        With _Address2
                            .City = _Address2City
                            .Street = _Address2Street
                            .ZipPostal = _Address2ZipPostal
                        End With

                        _Customers.Address1 = _Address1
                    End If

                    Dim _XmlNodeList As XmlNodeList = _XML.SelectNodes(_RootXMLTag + "/bankszamlaszamok")

                    For Each _XmlNode As XmlNode In _XmlNodeList
                        If _XmlNode.HasChildNodes Then
                            For Each _XmlNode_Bank As XmlNode In _XmlNode.ChildNodes
                                Dim _AccountNumber As String = _XmlNode_Bank.SelectSingleNode("bankszamlaszam").InnerText
                                Dim _ShortName As String = _Customers.FullName + " " + _AccountNumber

                                Dim _CustomerBankAccounts As CustomerBankAccounts = _uow.FindObject(Of CustomerBankAccounts)(CriteriaOperator.Parse("AccountNumber=? AND ShortName=?", _AccountNumber, _ShortName))
                                If _CustomerBankAccounts Is Nothing Then
                                    _CustomerBankAccounts = New CustomerBankAccounts(_uow)
                                    With _CustomerBankAccounts
                                        .AccountNumber = _XmlNode_Bank.SelectSingleNode("bankszamlaszam").InnerText
                                        .Customers = _Customers
                                        .ShortName = _Customers.FullName + " " + .AccountNumber
                                        Select Case _XmlNode_Bank.SelectSingleNode("automatikusfeldolgozas").InnerText
                                            Case "1"
                                                .AutomaticProcessing = True
                                            Case "0"
                                                .AutomaticProcessing = False
                                            Case Else
                                                .AutomaticProcessing = False
                                        End Select
                                    End With
                                    _Customers.CustomerBankAccounts.Add(_CustomerBankAccounts)
                                End If
                            Next
                        End If
                    Next
                    _Customers.QueueUpdated = GetSQLTime(_uow)
                    If IsService Then
                        '  Diagnostics.EventLog.WriteEntry(ConfigurationManager.AppSettings("ServiceName"), "Fogadás: [Customer:" + _Customers.Oid_DH + "]")
                    End If
                Else
                    _Customers = New Customers(_uow)
                    With _Customers
                        .Oid_DH = _Oid_DH
                        .CustomerNumber = _XML.SelectSingleNode(_RootXMLTag + "/ugyfelszam").InnerText
                        .Name = _XML.SelectSingleNode(_RootXMLTag + "/nev").InnerText
                        .FullName = .Name
                        .Url_DH = _XML.SelectSingleNode(_RootXMLTag + "/dh_url").InnerText
                        Dim _konyvelesi_parameter As String = _XML.SelectSingleNode(_RootXMLTag + "/konyvelesi_parameter").InnerText
                        Dim _NameValueCollection As NameValueCollection = ConfigurationManager.GetSection("CustomersGLParameters")

                        Dim _CustomersGLParameters As CustomersGLParameters = _uow.FindObject(Of CustomersGLParameters)(CriteriaOperator.Parse("Oid=?", _NameValueCollection(_konyvelesi_parameter)))
                        .CustomersGLParameters = _CustomersGLParameters

                        Dim _Address1 As New Address(_uow)
                        With _Address1
                            .City = _XML.SelectSingleNode(_RootXMLTag + "/varos").InnerText
                            .Street = _XML.SelectSingleNode(_RootXMLTag + "/utca").InnerText + " " + _XML.SelectSingleNode(_RootXMLTag + "/hazszam").InnerText
                            .ZipPostal = _XML.SelectSingleNode(_RootXMLTag + "/irszam").InnerText
                        End With
                        .Address1 = _Address1

                        Dim _Address2 As New Address(_uow)
                        With _Address2
                            .City = _XML.SelectSingleNode(_RootXMLTag + "/levelezesivaros").InnerText
                            .Street = _XML.SelectSingleNode(_RootXMLTag + "/levelezesiutca").InnerText + " " + _XML.SelectSingleNode(_RootXMLTag + "/levelezesihazszam").InnerText
                            .ZipPostal = _XML.SelectSingleNode(_RootXMLTag + "/levelezesiirszam").InnerText
                        End With
                        .Address2 = _Address2
                        Dim _XmlNodeList As XmlNodeList = _XML.SelectNodes(_RootXMLTag + "/bankszamlaszamok")

                        For Each _XmlNode As XmlNode In _XmlNodeList
                            If _XmlNode.HasChildNodes Then
                                For Each _XmlNode_Bank As XmlNode In _XmlNode.ChildNodes
                                    Dim _CustomerBankAccounts As New CustomerBankAccounts(_uow)
                                    With _CustomerBankAccounts
                                        .AccountNumber = _XmlNode_Bank.SelectSingleNode("bankszamlaszam").InnerText
                                        .Customers = _Customers
                                        .ShortName = "Bank-" & _Customers.CustomerNumber
                                    End With
                                    .CustomerBankAccounts.Add(_CustomerBankAccounts)
                                Next
                            End If
                        Next
                    End With
                    _Customers.QueueUpdated = GetSQLTime(_uow)
                    'If IsService Then
                    '    Diagnostics.EventLog.WriteEntry(ConfigurationManager.AppSettings("ServiceName"), "Fogadás: [Customer:" + _Customers.Oid_DH + "]")
                    'End If
                End If
                _uow.CommitChanges()
                SaveAuditTrail(_uow)
            End If
        Catch ex As Exception
            If IsService Then
                'Diagnostics.EventLog.WriteEntry(ConfigurationManager.AppSettings("ServiceName"), "Fogadás HIBA: " + ex.Message)
            Else
                WriteErrorLog(Now + " : " + ex.Message, _errorLogFile)
            End If
        End Try
    End Sub
End Module
