
Imports System.Data.SqlClient
Imports System.Reflection

Public Class cOrigianlATR
    Property atr_id As Integer

    Property atr_documentNr As Object = Nothing
    Property atr_erfassungsdatum As Date
    Property atr_Sachbearbeiter As String
    Property atr_mitId As Integer
    Property atr_absender As Object = Nothing
    Property atr_empfaenger As Object = Nothing
    Property atr_empfaengerkdnr As Integer = -1
    Property atr_absenderkdnr As Integer = -1
    Property atr_austellungsdatum As Object = Nothing
    Property atr_AvisoId As Integer = -1
    Property atr_SendungsId As Integer = -1
    Property atr_LKW As Object = Nothing
    Property atr_dsId As Object = Nothing
    Property atr_DHL_ReferenzNr As Object = Nothing
    Property atr_location As Object = Nothing
    Property atr_info As Object = Nothing
    Property atr_erhalenDatum As Object = Nothing
    Property atr_erhalten_Art As Object = Nothing
    Property atr_FilialenNr As Integer = -1
    Property atr_AbfertigungsNr As Integer = -1
    Property atr_anhID As Integer = -1


    Public hasEntry = False

    Dim SQL As New SQL

    Sub New()

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

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_documentNr", atr_documentNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_erfassungsdatum", atr_erfassungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_Sachbearbeiter", atr_Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_mitId", atr_mitId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_absender", atr_absender))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_empfaenger", atr_empfaenger))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_austellungsdatum", atr_austellungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_AvisoId", atr_AvisoId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_SendungsId", atr_SendungsId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_LKW", atr_LKW))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_dsId", atr_dsId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_DHL_ReferenzNr", atr_DHL_ReferenzNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_location", atr_location))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_info", atr_info))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_erhalenDatum", atr_erhalenDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_erhalten_Art", atr_erhalten_Art))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_FilialenNr", atr_FilialenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_AbfertigungsNr", atr_AbfertigungsNr))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_absenderkdnr", atr_absenderkdnr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_empfaengerkdnr", atr_empfaengerkdnr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("atr_anhID", atr_anhID))


        Return list
    End Function



    Public Function SAVE() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        Me.atr_mitId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        Me.atr_Sachbearbeiter = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME


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

        Return SQL.doSQLVarList(sqlstr, "FMZOLL", , list)
    End Function
    Public Function DELETE() As Boolean

        Dim sqlstr = " DELETE tblOriginalATR  WHERE atr_id='" & atr_id & "'"

        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblOriginalATR WHERE atr_id=@atr_id  ", conn)
                    cmd.Parameters.AddWithValue("@atr_id", atr_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
                        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 [tblOriginalATR] SET " & str & " WHERE atr_id=@atr_id ")

        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 tblOriginalATR  (" & 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
