Imports System.Data.SqlClient
Imports System.Reflection

Public Class cFremdSpeditionenZuordnung


    Property fremdz_Id As Integer
    Property fremdz_firma As Object = Nothing
    Property fremdz_KundenNr As Object = Nothing
    Property fremdz_plz As Object = Nothing
    Property fremdz_ort As Object = Nothing
    Property fremdz_verzolltBei As Object = Nothing
    Property fremdz_bemerkung As Object = Nothing
    Property fremdz_SpeditionKundenNr As Object = Nothing
    Property fremdz_Datum As Object = Nothing
    Property fremdz_Sachbearbeiter As Object = Nothing


    Public hasEntry As Boolean = False

    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(fremdz_Id)
        Me.fremdz_Id = fremdz_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("fremdz_Id", fremdz_Id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fremdz_firma", fremdz_firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fremdz_KundenNr", fremdz_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fremdz_plz", fremdz_plz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fremdz_ort", fremdz_ort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fremdz_verzolltBei", fremdz_verzolltBei))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fremdz_bemerkung", fremdz_bemerkung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fremdz_SpeditionKundenNr", fremdz_SpeditionKundenNr))

        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 tblFremdSpeditionenZuordnung WITH(updlock,serializable) WHERE fremdz_Id=@fremdz_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblFremdSpeditionenZuordnung WHERE fremdz_Id=@fremdz_Id  ", conn)
                    cmd.Parameters.AddWithValue("@fremdz_Id", fremdz_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
                            hasEntry = True
                        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 [tblFremdSpeditionenZuordnung] SET " & str & " WHERE fremdz_Id=@fremdz_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 tblFremdSpeditionenZuordnung  (" & 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 Function DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblFremdSpeditionenZuordnung] WITH(updlock,serializable) WHERE fremdz_Id=" & Me.fremdz_Id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Public Shared Function DELETE(id As Integer) As Boolean
        Dim SQL As New SQL
        Dim sqlstr = " DELETE FROM [tblFremdSpeditionenZuordnung]  WHERE fremdz_Id=" & id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function
End Class

