Imports System.Data.SqlClient
Imports System.Reflection

Public Class cSDLAbholung

    Property sdlAb_Id As Integer

    Property sdlAb_Erfasst As Object = Now
    Property sdlAb_ErfassungSachbearbeiter As Object = Nothing
    Property sdlAb_ErfassungSachbearbeiterId As Object = Nothing
    Property sdlAb_ErfassungVermerk As Object = Nothing

    Property sdlAb_AbholungLKWKz As Object = Nothing
    Property sdlAb_AbholungFahrername As Object = Nothing
    Property sdlAb_AbholungKdNr As Object = Nothing
    Property sdlAb_AbholungFirma As Object = Nothing
    Property sdlAb_AbholungFahrerSMS As Object = Nothing
    Property sdlAb_Abholung As Object = Nothing
    Property sdlAb_AbholungSachbearbeiter As Object = Nothing
    Property sdlAb_AbholungSachbearbeiterId As Object = Nothing
    Property sdlAb_Abgeholt As Boolean = False
    Property sdlAb_Storniert As Boolean = False
    Property sdlAb_AbholungVermerk As Object = Nothing

    Property sdlAb_FahrerAbholAuftrag As Object = Nothing
    Property sdlAb_AbholungFremdDL As Boolean = False


    Property POS As New List(Of cSDLAbholungPositionen)
    Dim SQL As New SQL


    Sub New()
    End Sub

    Sub New(sdlAb_Id)
        Me.sdlAb_Id = sdlAb_Id
        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("sdlAb_Id", sdlAb_Id, , True))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_Erfasst", sdlAb_Erfasst))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_ErfassungSachbearbeiter", sdlAb_ErfassungSachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_ErfassungSachbearbeiterId", sdlAb_ErfassungSachbearbeiterId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_ErfassungVermerk", sdlAb_ErfassungVermerk)) 


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_AbholungLKWKz", sdlAb_AbholungLKWKz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_AbholungFahrername", sdlAb_AbholungFahrername))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_AbholungKdNr", sdlAb_AbholungKdNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_AbholungFirma", sdlAb_AbholungFirma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_AbholungFahrerSMS", sdlAb_AbholungFahrerSMS))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_Abholung", sdlAb_Abholung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_AbholungSachbearbeiter", sdlAb_AbholungSachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_AbholungSachbearbeiterId", sdlAb_AbholungSachbearbeiterId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_Abgeholt", sdlAb_Abgeholt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_Storniert", sdlAb_Storniert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_AbholungVermerk", sdlAb_AbholungVermerk))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_FahrerAbholAuftrag", sdlAb_FahrerAbholAuftrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAb_AbholungFremdDL", sdlAb_AbholungFremdDL))

        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 tblSDLAbholung WITH(updlock,serializable) WHERE sdlAb_Id=@sdlAb_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "
        Dim id = SQL.doSQLVarListID(sdlAb_Id, sqlstr, "SDL", , list)

        If id > 0 Then
           Me.sdlAb_Id = id
            Return SAVE_POS()
        Else
            Return False
        End If
    End Function


    Public Function DELETE() As Boolean
        If DELETE_POS() Then
            Return SQL.doSQL(" DELETE FROM tblSDLAbholung WHERE sdlAb_Id=" & sdlAb_Id & " ", "SDL")
        End If
        Return False
    End Function

    Public Function DELETE_POS() As Boolean
        Return SQL.doSQL(" DELETE FROM tblSDLAbholungPositionen WHERE sdlAbp_sdlAb_Id=" & sdlAb_Id & " ", "SDL")
    End Function

    Public Function SAVE_POS() As Boolean
        SAVE_POS = True
        If Not DELETE_POS() Then Return False
        For Each p In POS
            p.sdlAbp_sdlAb_Id = Me.sdlAb_Id
            If Not p.SAVE() Then SAVE_POS = False
        Next
    End Function


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

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

                        Next
                        LOAD_POS() 
                    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 Sub LOAD_POS()
        Try 
            POS.Clear()
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblSDLAbholungPositionen WHERE sdlAbp_sdlAb_Id=@sdlAb_Id  ORDER BY sdlAbp_KfzKennzeichen,sdlAbp_SDLNr", conn)
                    cmd.Parameters.AddWithValue("@sdlAb_Id", sdlAb_Id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim P As New cSDLAbholungPositionen
                        For Each l In P.getParameterList()
                            Dim propInfo As PropertyInfo = P.GetType.GetProperty(l.Scalarvariable)
                            If dr.Item(l.Text) Is DBNull.Value Then
                                propInfo.SetValue(P, Nothing)
                            Else
                                propInfo.SetValue(P, dr.Item(l.Text))
                            End If
                        Next
                        POS.Add(P)
                    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
    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 [tblSDLAbholung] SET " & str & " WHERE sdlAb_Id=@sdlAb_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 tblSDLAbholung  (" & 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 SET_ABHOLUNG(sdlAb_Abgeholt As Boolean, Optional sdlAb_AbholungVermerk As Object = Nothing, Optional sdlAb_FahrerAbholAuftrag As Object = Nothing) As Boolean
        Me.sdlAb_Abgeholt = sdlAb_Abgeholt
        If Me.sdlAb_Abgeholt Then
            Me.sdlAb_Abholung = Now
            Me.sdlAb_AbholungSachbearbeiterId = VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_id
            Me.sdlAb_AbholungSachbearbeiter = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        Else
            Me.sdlAb_Abholung = Nothing
            Me.sdlAb_AbholungSachbearbeiterId = Nothing
            Me.sdlAb_AbholungSachbearbeiter = Nothing
        End If
        Me.sdlAb_AbholungVermerk = sdlAb_AbholungVermerk

        If sdlAb_FahrerAbholAuftrag IsNot Nothing Then Me.sdlAb_FahrerAbholAuftrag = sdlAb_FahrerAbholAuftrag
        Return SAVE()
    End Function


    Public Function SET_FAHRERABHOLUNG(sdlAb_FahrerAbholAuftrag As Boolean, Optional sdlAb_AbholungVermerk As Object = Nothing) As Boolean
        Me.sdlAb_FahrerAbholAuftrag = sdlAb_FahrerAbholAuftrag
        Me.sdlAb_AbholungVermerk = sdlAb_AbholungVermerk

        Return SAVE()
    End Function

    Public Function SET_BEMERKUNG(sdlAb_AbholungVermerk As Object) As Boolean
        
        Me.sdlAb_AbholungVermerk = sdlAb_AbholungVermerk

        Return SAVE()
    End Function
    Public Function SET_STORNO() As Boolean
        Me.sdlAb_Storniert = Not Me.sdlAb_Storniert
        Return SAVE()
    End Function


End Class



Public Class cSDLAbholungPositionen

    Property sdlAbp_Id As Integer
    Property sdlAbp_sdlAb_Id As Object = Nothing
    Property sdlAbp_KundenNr As Object = Nothing
    Property sdlAbp_KfzKennzeichen As Object = Nothing
    Property sdlAbp_SDLNr As Object = Nothing
    Property sdlAbp_History As Object = Nothing
    Property sdlAbp_KarteBoxBezeichnung As Object = Nothing
    Property sdlAbp_KartenNr As Object = Nothing
    Property sdlAbp_PIN As Object = Nothing
     

    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(sdlAbp_Id)
        Me.sdlAbp_Id = sdlAbp_Id
        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("sdlAbp_Id", sdlAbp_Id, , True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAbp_sdlAb_Id", sdlAbp_sdlAb_Id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAbp_KundenNr", sdlAbp_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAbp_KfzKennzeichen", sdlAbp_KfzKennzeichen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAbp_SDLNr", sdlAbp_SDLNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAbp_History", sdlAbp_History))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAbp_KarteBoxBezeichnung", sdlAbp_KarteBoxBezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAbp_KartenNr", sdlAbp_KartenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sdlAbp_PIN", sdlAbp_PIN))

        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 tblSDLAbholungPositionen WITH(updlock,serializable) WHERE sdlAbp_Id=@sdlAbp_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "
        Dim id = SQL.doSQLVarListID(sdlAbp_Id, sqlstr, "SDL", , list)
        Me.sdlAbp_Id = id
        Return (id > 0)
    End Function


    Public Function DELETE() As Boolean
        Return SQL.doSQL(" DELETE FROM tblSDLAbholungPositionen WHERE sdlAbp_Id=" & sdlAbp_Id & " ", "SDL")
    End Function


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

                            If dr.Item(l.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(l.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 [tblSDLAbholungPositionen] SET " & str & " WHERE sdlAbp_Id=@sdlAbp_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 tblSDLAbholungPositionen  (" & 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



End Class 