Imports System.Data.SqlClient
Imports System.Reflection


Public Class cPostversand


    Property post_id As Integer = -1
    Property post_AvisoId As Object = Nothing '             INT           NULL,
    Property post_SendungsId As Object = Nothing '          INT           NULL,
    Property post_FilialenNr As Object = Nothing '          INT           NULL,
    Property post_AbfertigungsNr As Object = Nothing '      INT           NULL,
    Property post_art As Object = Nothing '                 VARCHAR(200) Not NULL,
    Property post_Empfaenger_Name1 As Object = Nothing '    VARCHAR(40)  Not NULL,
    Property post_Empfaenger_Name2 As Object = Nothing '    VARCHAR(40)  NULL,
    Property post_Empfaenger_Adresse1 As Object = Nothing ' VARCHAR(40)  Not NULL,
    Property post_Empfaenger_Adresse2 As Object = Nothing ' VARCHAR(60)  Not NULL,
    Property post_Empfaenger_Adresse3 As Object = Nothing '      VARCHAR(40)  NULL,
    Property post_ReferenzPaketdienst As Object = Nothing ' VARCHAR(200) NULL,
    Property post_ContainerLKWNr As Object = Nothing '      VARCHAR(200) Not NULL,
    Property post_Datum As Object = Nothing '               DATETIME      Default (getdate()) Not NULL,
    Property post_Sachbearbeiter As Object = Nothing '      VARCHAR(200) Not NULL,
    Property post_SachbearbeiterId As Object = Nothing '    INT           Not NULL,
    Property post_FirmaInAuftragKdnr As Object = Nothing '  INT           NULL,
    Property post_FirmaInAuftrag As Object = Nothing '      VARCHAR(200) NULL,
    Property post_Rolle As Object = Nothing '      VARCHAR(200) NULL,
    'Property post_LKW As Object = Nothing '      VARCHAR(200) NULL,
    Property post_Empfaenger_KdNr As Object = Nothing '      VARCHAR(200) NULL,
    '  Property post_Rolle As Object = Nothing '      VARCHAR(200) NULL,



    Property post_KdAuftrNrAvisierer As Object = Nothing
    Property post_RgEmpfaenger_KdNr As Object = Nothing
    Property post_RgEmpfaenger_Name As Object = Nothing

    Dim SQL As New SQL

    Sub New()
    End Sub

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

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_AvisoId", post_AvisoId))     '        INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_SendungsId", post_SendungsId)) '        INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_FilialenNr", post_FilialenNr))  '        INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_AbfertigungsNr", post_AbfertigungsNr))  '    INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_art", post_art))       '          VARCHAR(200) Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_Empfaenger_Name1", post_Empfaenger_Name1))   ' VARCHAR(40)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_Empfaenger_Name2", post_Empfaenger_Name2)) '   VARCHAR(40)  NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_Empfaenger_Adresse1", post_Empfaenger_Adresse1)) 'VARCHAR(40)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_Empfaenger_Adresse2", post_Empfaenger_Adresse2)) 'VARCHAR(60)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_Empfaenger_Adresse3", post_Empfaenger_Adresse3))    '  VARCHAR(40)  NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_ReferenzPaketdienst", post_ReferenzPaketdienst)) ' VARCHAR(200) NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_ContainerLKWNr", post_ContainerLKWNr))   '#   VARCHAR(200) Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_Datum", post_Datum))         '      DATETIME      Default (getdate()) Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_Sachbearbeiter", post_Sachbearbeiter))  '    VARCHAR(200) Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_SachbearbeiterId", post_SachbearbeiterId))  '  INT           Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_FirmaInAuftragKdnr", post_FirmaInAuftragKdnr)) ' INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_FirmaInAuftrag", post_FirmaInAuftrag))    '  VARCHAR(200) NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_Rolle", post_Rolle))    '  VARCHAR(200) NULL,
        '  list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_LKW", post_LKW))    '  VARCHAR(200) NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_Empfaenger_KdNr", post_Empfaenger_KdNr))    '  VARCHAR(200) NULL,

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_KdAuftrNrAvisierer", post_KdAuftrNrAvisierer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_RgEmpfaenger_KdNr", post_RgEmpfaenger_KdNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("post_RgEmpfaenger_Name", post_RgEmpfaenger_Name))

        Return list
    End Function



    Public Shared Function LOADByFilialeAbfNr(FilialeNr As Integer, AbfertigungsNr As Integer, Optional post_Rolle As String = "") As cPostversand
        Try
            Dim VK As New cPostversand
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblPostversand WHERE post_AbfertigungsNr=@post_AbfertigungsNr AND  post_FilialenNr=@post_FilialenNr " & If(If(post_Rolle, "") <> "", "AND post_Rolle=@post_Rolle ", ""), conn)
                    cmd.Parameters.AddWithValue("@post_FilialenNr", FilialeNr)
                    cmd.Parameters.AddWithValue("@post_AbfertigungsNr", AbfertigungsNr)
                    cmd.Parameters.AddWithValue("@post_Rolle", post_Rolle)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In VK.getParameterList()
                            Dim propInfo As PropertyInfo = VK.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(VK, Nothing)
                            Else
                                propInfo.SetValue(VK, dr.Item(li.Text))
                            End If
                        Next
                        dr.Close()
                        Return VK
                    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 Function SAVE() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

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

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



    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblPostversand WHERE post_id=@post_id  ", conn)
                    cmd.Parameters.AddWithValue("@post_id", post_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 [tblPostversand] SET " & str & " WHERE post_id=@post_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 tblPostversand  (" & 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 [tblPostversand] WITH(updlock,serializable) WHERE post_id=" & Me.post_id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


End Class
