Imports System.Data.SqlClient
Imports System.Reflection


Public Class cUStVLeistender
    Property UStV_Leistender As String
    Property UstV_Leistender_PLZ As Object = Nothing
    Property UstV_Leistender_Strasse As Object = Nothing
    Property UstV_Leistender_Stadt As Object = Nothing
    Property UstV_Leistender_StrasseNr As Object = Nothing
    Property UstV_Leistender_Land As Object = Nothing
    Property UstV_Leistender_UstNr As Object = Nothing
    Property UstV_Leistender_Adresse As Boolean
    Property UStV_LeistenderId As Integer
    Property UstV_aktiv As Boolean = True

    Public hasEntry = False

        Dim SQL As New SQL

    Sub New(UStV_Leistender As String, Optional searchbyUID As Boolean = False)
        If Not searchbyUID Then
            Me.UStV_Leistender = UStV_Leistender
            LOAD()

        Else
            Me.UstV_Leistender_UstNr = UStV_Leistender
            LOADBYUID()

        End If

    End Sub

    Sub New(Id As Integer)
        Me.UStV_LeistenderId = Id
        LOADBYID()
    End Sub

    Sub New(UStV_Leistender As String, Land As String)
        Me.UStV_Leistender = UStV_Leistender
        LOADByLand(Land)
    End Sub


    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("UStV_Leistender", UStV_Leistender))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UstV_Leistender_PLZ", UstV_Leistender_PLZ))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UstV_Leistender_Strasse", UstV_Leistender_Strasse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UstV_Leistender_Stadt", UstV_Leistender_Stadt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UstV_Leistender_StrasseNr", UstV_Leistender_StrasseNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UstV_Leistender_Land", UstV_Leistender_Land))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UstV_Leistender_UstNr", UstV_Leistender_UstNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UstV_Leistender_Adresse", UstV_Leistender_Adresse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UStV_LeistenderId", UStV_LeistenderId,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UstV_aktiv", UstV_aktiv))
        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 tblUStVLeistender  WHERE UStV_Leistender=@UStV_Leistender) " &
                      " BEGIN " & getUpdateCmd() & " END " &
                      " Else " &
                      " BEGIN " & getInsertCmd() & " END " &
                      " commit tran "


        Return SQL.doSQLVarList(sqlstr, "FMZOLL", , list)
    End Function


    Public Function SAVEID() As Integer
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblUStVLeistender WHERE UStV_Leistender=@UStV_Leistender) " &
                      " BEGIN " & getUpdateCmd() & " END " &
                      " Else " &
                      " BEGIN " & getInsertCmd() & " END " &
                      " commit tran "


        UStV_LeistenderId = SQL.doSQLVarListID(UStV_LeistenderId, sqlstr, "FMZOLL", , list)

        Return UStV_LeistenderId
    End Function




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

        Dim sqlstr = getInsertCmd()

        Return SQL.doSQLVarList(sqlstr, "FMZOLL", , list)
    End Function

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblUStVLeistender WHERE UStV_Leistender=@UStV_Leistender  ", conn)
                    cmd.Parameters.AddWithValue("@UStV_Leistender", UStV_Leistender)
                    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
                        hasEntry = True
                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
    End Sub



    Public Sub LOADBYID()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblUStVLeistender WHERE UStV_LeistenderId=@UStV_LeistenderId  ", conn)
                    cmd.Parameters.AddWithValue("@UStV_LeistenderId", UStV_LeistenderId)
                    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
                        hasEntry = True
                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
    End Sub

    Public Sub LOADByUID()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblUStVLeistender WHERE UstV_Leistender_UstNr=@UstV_Leistender_UstNr ", conn)
                    cmd.Parameters.AddWithValue("@UstV_Leistender_UstNr", UstV_Leistender_UstNr)

                    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
                        hasEntry = True
                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
    End Sub

    Public Sub LOADByLand(Land As String)
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblUStVLeistender WHERE UStV_Leistender=@UStV_Leistender and left(UstV_Leistender_UstNr, 2) =  '" & Land & "'", conn)
                    cmd.Parameters.AddWithValue("@UStV_Leistender", UStV_Leistender)

                    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
                        hasEntry = True
                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        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 [tblUStVLeistender] SET " & str & " WHERE UStV_Leistender=@UStV_Leistender ")

        Catch ex As Exception
                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            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 tblUStVLeistender  (" & str & ") VALUES(" & values & ")  ")
        Catch ex As Exception
                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            End Try
            Return ""
        End Function

    End Class

