Imports System.Data.SqlClient
Imports System.Reflection

Public Class cUIDAbfrage


    Property uid_id As Integer
    Property uid_KundenNr As Integer
    Property uid_Datum As Object = Nothing
    Property uid_MaId As Integer
    Property uid_Sachbearbeiter As Object = Nothing
    Property uid_requesterCountryCode As Object = Nothing
    Property uid_requesterVatNumber As Object = Nothing
    Property uid_countryCode As Object = Nothing
    Property uid_vatNumber As Object = Nothing
    Property uid_requestDate As Object = Nothing
    Property uid_valid As Boolean
    Property uid_traderName As Object = Nothing
    Property uid_traderCompanyType As Object = Nothing
    Property uid_traderAddress As Object = Nothing
    Property uid_traderStreet As Object = Nothing
    Property uid_traderPostcode As Object = Nothing
    Property uid_traderCity As Object = Nothing
    Property uid_traderNameMatch As Object = Nothing
    Property uid_traderCompanyTypeMatch As Object = Nothing
    Property uid_traderStreetMatch As Object = Nothing
    Property uid_traderPostcodeMatch As Object = Nothing
    Property uid_traderCityMatch As Object = Nothing
    Property uid_requestIdentifier As Object = Nothing




    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(uid_id)
        Me.uid_id = uid_id
        LOAD()
    End Sub

    Public Function getParameterList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_id", uid_id, , True, True))


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_KundenNr", uid_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_Datum", uid_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_MaId", uid_MaId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_Sachbearbeiter", uid_Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_requesterCountryCode", uid_requesterCountryCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_requesterVatNumber", uid_requesterVatNumber))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_countryCode", uid_countryCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_vatNumber", uid_vatNumber))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_requestDate", uid_requestDate))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_valid", uid_valid))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderName", uid_traderName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderCompanyType", uid_traderCompanyType))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderAddress", uid_traderAddress))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderStreet", uid_traderStreet))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderPostcode", uid_traderPostcode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderCity", uid_traderCity))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderNameMatch", uid_traderNameMatch))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderCompanyTypeMatch", uid_traderCompanyTypeMatch))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderStreetMatch", uid_traderStreetMatch))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderPostcodeMatch", uid_traderPostcodeMatch))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_traderCityMatch", uid_traderCityMatch))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("uid_requestIdentifier", uid_requestIdentifier))


        Return list
    End Function



    Public Function SAVE() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblUIDAbfrage WITH(updlock,serializable) WHERE uid_id=@uid_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        uid_id = SQL.doSQLVarListID(uid_id, sqlstr, "FMZOLL", , list)
        Return uid_id > 0
    End Function

    Public Shared Function LOADByKdNrDate(uid_KundenNr As Integer, uid_Datum As Date) As cUIDAbfrage
        Try
            Dim UID As New cUIDAbfrage
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblUIDAbfrage WHERE uid_KundenNr=@uid_KundenNr AND  cast(uid_Datum as date)=@uid_Datum  ", conn)
                    cmd.Parameters.AddWithValue("@uid_KundenNr", uid_KundenNr)
                    cmd.Parameters.AddWithValue("@uid_Datum", uid_Datum.ToShortDateString)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In UID.getParameterList()
                            Dim propInfo As PropertyInfo = UID.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(UID, Nothing)
                            Else
                                propInfo.SetValue(UID, dr.Item(li.Text))
                            End If

                        Next
                        dr.Close()
                        Return UID
                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return Nothing
    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblUIDAbfrage WHERE uid_id=@uid_id  ", conn)
                    cmd.Parameters.AddWithValue("@uid_id", uid_id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(li.Text))
                            End If

                        Next

                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
    End Sub


    Public Function getUpdateCmd() As String
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

            Dim str As String = ""
            For Each i In list
                If Not i.isPrimaryParam Then
                    str &= "[" & i.Text & "] = @" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
                End If
            Next
            str = str.Substring(0, str.Length - 1) 'wg. ','
            Return (" UPDATE [tblUIDAbfrage] SET " & str & " WHERE uid_id=@uid_id ")

        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return ""
    End Function


    Public Function getInsertCmd() As String
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
            Dim str As String = ""
            Dim values As String = ""
            For Each i In list
                If Not i.isPrimaryParam Then
                    str &= "[" & i.Text & "],"
                    values &= "@" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
                End If
            Next
            str = str.Substring(0, str.Length - 1) 'wg. ','
            values = values.Substring(0, values.Length - 1) 'wg. ','
            Return (" INSERT INTO tblUIDAbfrage  (" & str & ") VALUES(" & values & ")  ")
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return ""
    End Function



    Public Shared Function ABFRAGE(UstIdKz As String, UstIdNr As String, ByRef valid As Boolean) As cUIDAbfrage

        Try
            Dim vat As New UIDPruefung.checkVatService

            Dim countryCode As String = UstIdKz
            Dim vatNumber As String = UstIdNr
            Dim traderName As String
            Dim traderCompanyType As String
            Dim traderStreet As String
            Dim traderPostcode As String
            Dim traderCity As String
            Dim requesterCountryCode As String = "AT"
            Dim requesterVatNumber As String = "U62663022"
            'Dim valid As Boolean
            Dim traderAddress As String
            Dim traderNameMatch As Integer
            Dim traderNameMatchSpecified As Boolean
            Dim traderCompanyTypeMatch As Integer
            Dim traderCompanyTypeMatchSpecified As Boolean
            Dim traderStreetMatch As Integer
            Dim traderStreetMatchSpecified As Boolean
            Dim traderPostcodeMatch As Integer
            Dim traderPostcodeMatchSpecified As Boolean
            Dim traderCityMatch As Integer
            Dim traderCityMatchSpecified As Boolean
            Dim requestIdentifier As String



            '   Dim ass As New EU_UID_Pruefung.checkVatPortTypeClient()

            '  ass.checkVatPortTypeClient()

            ' Dim s = EU3t.checkVat("AT", "U53187000", valid, name, adress)
            'Dim s = vat.checkVat(ADRESSE.UstIdKz, ADRESSE.UstIdNr, valid, name, adress)
            Dim s As Date = vat.checkVatApprox(countryCode, vatNumber, traderName, traderCompanyType, traderStreet, traderPostcode, traderCity, requesterCountryCode, requesterVatNumber, valid, traderAddress, traderNameMatch, traderNameMatchSpecified, traderCompanyTypeMatch, traderCompanyTypeMatchSpecified, traderStreetMatch, traderStreetMatchSpecified, traderPostcodeMatch, traderPostcodeMatchSpecified, traderCityMatch, traderCityMatchSpecified, requestIdentifier)


            Dim UID As New cUIDAbfrage

            ' UID()
            '     MsgBox(requestIdentifier)
            '  MsgBox(traderName)
            '  MsgBox(traderStreet)
            ''    MsgBox(traderAddress)
            '        '  MsgBox(adress)
            '  MsgBox(name)


            Return UID
        Catch ex As Exception


            Return Nothing
        End Try
    End Function

End Class
