
Imports System.Data.SqlClient
Imports System.Reflection

Public Class cGelangensbestaetigung

    Property gb_nr As Integer
    Property gb_firma As Object = Nothing
    Property gb_KdNr As Object = Nothing
    Property gb_datetime As Date
    Property gb_sachbearbeiter As Object = Nothing
    Property gb_sachbearbeiterid As Integer



    Dim SQL As New SQL

    Sub New(gb_nr As Integer)
        Me.gb_nr = gb_nr
        Me.gb_sachbearbeiter = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        Me.gb_sachbearbeiterid = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        Me.gb_datetime = Now
        LOAD()
    End Sub


    Sub New(gb_firma As String, gb_KdNr As Object)
        Me.gb_nr = SQL.getValueTxtBySqlVarList("SELECT isnull(MAX([gb_nr])+1,( YEAR( GETDATE() ) % 100 ) * 1000000) FROM [tblGelangensbestaetigung] WHERE left(cast([gb_nr] as VARCHAR),2) = ( YEAR( GETDATE() ) % 100 )", "FMZOLL")
        Me.gb_firma = gb_firma
        Me.gb_KdNr = gb_KdNr
        Me.gb_sachbearbeiter = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        Me.gb_sachbearbeiterid = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        Me.gb_datetime = Now
    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("gb_nr", gb_nr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gb_firma", gb_firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gb_KdNr", gb_KdNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gb_datetime", gb_datetime))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gb_sachbearbeiter", gb_sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gb_sachbearbeiterid", gb_sachbearbeiterid))
        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 tblGelangensbestaetigung WITH(updlock,serializable) WHERE gb_nr=@gb_nr) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        Return SQL.doSQLVarList(sqlstr, "FMZOLL", , list)
    End Function

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

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

