

Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cFilialen
    Property FilialenNr As Integer

    Property Stammfiliale As Object = Nothing
    Property Grenzstelle As Object = Nothing
    Property Speditionsbuch As Object =Nothing
    Property Name_1 As Object = Nothing
    Property Name_2 As Object = Nothing
    Property Postfach As Object = Nothing
    Property PLZPF As Object = Nothing
    Property Straße As Object = Nothing
    Property LandKz As Object = Nothing
    Property PLZ As Object = Nothing
    Property Ort As Object = Nothing
    Property Telefon As Object = Nothing
    Property Telefax As Object = Nothing
    Property E_Mail As Object = Nothing
    Property UstIdKz As Object = Nothing
    Property UstIdNr As Object = Nothing
    Property KorrespondentenNr As Object = Nothing
    Property Absender As Object = Nothing
    Property Fil_Status As Object = Nothing
    Property PP440 As Object = Nothing
    Property Firma As Object = Nothing
    Property Cluster As Object = Nothing
    Property PP441 As Object = Nothing

    Public hasEntry = False


    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(FilialenNr)
        Me.FilialenNr = FilialenNr
        LOAD()
    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("FilialenNr", FilialenNr,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Stammfiliale", Stammfiliale))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Grenzstelle", Grenzstelle))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Speditionsbuch", Speditionsbuch))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Name 1", Name_1, "Name_1"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Name 2", Name_2, "Name_2"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Postfach", Postfach))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PLZPF", PLZPF))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Straße", Straße))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LandKz", LandKz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PLZ", PLZ))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Ort", Ort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Telefon", Telefon))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Telefax", Telefax))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("E-Mail", E_Mail, "E_Mail"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UstIdKz", UstIdKz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("UstIdNr", UstIdNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KorrespondentenNr", KorrespondentenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Absender", Absender))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Fil_Status", Fil_Status))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PP440", PP440))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Firma", Firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Cluster", Cluster))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PP441", PP441))

        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 Filialen  WHERE FilialenNr=@FilialenNr) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        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 Filialen WHERE FilialenNr=@FilialenNr  ", conn)
                    cmd.Parameters.AddWithValue("@FilialenNr", FilialenNr)
                    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
            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 Filialen SET " & str & " WHERE FilialenNr=@FilialenNr ")

        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 Filialen  (" & 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 LOAD_LIST() As List(Of cFilialen)
        Dim FILIALEN As New List(Of cFilialen)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM Filialen ", conn)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim FILIALE As New cFilialen
                        For Each li In FILIALE.getParameterList()
                            Dim propInfo As PropertyInfo = FILIALE.GetType.GetProperty(li.Scalarvariable)

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

                        Next
                        FILIALEN.Add(FILIALE)
                    End While
                    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 FILIALEN
    End Function
End Class
