Imports System.Data.SqlClient
Imports System.Reflection

Public Class cSicherheiten

    Property gs_gsId As Integer
    Property gs_ATBNr As String
    Property gs_posNr As Integer
    Property gs_datum As DateTime
    Property gs_erstellungsdatum As DateTime
    Property gs_warenwert As Object
    Property gs_sicherheitsbetrag As Object
    Property gs_saldo As Object
    Property gs_standort As String
    Property gs_art As String
    Property gs_gsNr As Integer
    Property gs_ust As Integer
    Property gs_freitext As String
    Property gs_filialenNr As Integer
    Property gs_abfertigungsnr As String
    Property gs_LKWKZ As String
    Property gs_atr As String = ""
    Property gs_warenort As String
    Property gs_zollsatz As Object
    Property gs_avisoId As Integer

    Property gs_isRundungsdiff As Boolean

    Public hasEntry = False

    Dim SQL As New SQL

    Sub New(gs_gsID, gs_posNr)
        Me.gs_gsId = gs_gsID
        Me.gs_posNr = gs_posNr
        LOAD()
    End Sub

    Sub New(gs_gsNr)
        Me.gs_gsNr = gs_gsNr
        LOADGSNR()
    End Sub

    Sub New()

    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("gs_gsId", gs_gsId,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_ATBNr", gs_ATBNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_posNr", gs_posNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_datum", gs_datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_erstellungsdatum", gs_erstellungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_warenwert", gs_warenwert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_sicherheitsbetrag", gs_sicherheitsbetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_saldo", gs_saldo))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_standort", gs_standort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_art", gs_art))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_gsNr", gs_gsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_ust", gs_ust))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_freitext", gs_freitext))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_filialenNr", gs_filialenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_abfertigungsnr", gs_abfertigungsnr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_LKWKZ", gs_LKWKZ))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_atr", gs_atr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_warenort", gs_warenort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_zollsatz", gs_zollsatz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_avisoId", gs_avisoId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gs_isRundungsdiff", gs_isRundungsdiff))

        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 [tblGesamtsicherheit]  WHERE gs_gsId=@gs_gsId AND gs_posNr=@gs_posNr) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    Public Function SAVEOBJECT() As Integer
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM [tblGesamtsicherheit]  WHERE gs_gsId=@gs_gsId AND gs_posNr=@gs_posNr) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        gs_gsId = SQL.doSQLVarListID(gs_gsId, sqlstr, "AVISO", , list)
        Return gs_gsId
    End Function



    Public Function UPDATEWithNewPosNr() As Integer

        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN " & getUpdateCmd() & " END "

        gs_gsId = SQL.doSQLVarListID(gs_gsId, sqlstr, "AVISO", , List)
        Return gs_gsId
    End Function




    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("SELECT * FROM [tblGesamtsicherheit] WHERE gs_gsId=@gs_gsId AND gs_posNr=@gs_posNr  ", conn)
                    cmd.Parameters.AddWithValue("@gs_gsID", gs_gsId)
                    cmd.Parameters.AddWithValue("@gs_posNr", gs_posNr)
                    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
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
    End Sub


    Public Sub LOADGSNR()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("SELECT * FROM [tblGesamtsicherheit] WHERE gs_gsNr=@gs_gsNr  ", conn)
                    cmd.Parameters.AddWithValue("@gs_gsNr", gs_gsNr)

                    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
            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 [tblGesamtsicherheit] SET " & str & " WHERE gs_gsId=@gs_gsId") ' AND gs_posNr=@gs_posNr ")

        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 [tblGesamtsicherheit]  (" & 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()
        SQL.doSQL("delete from [tblGesamtsicherheit] where gs_gsId = '" & Me.gs_gsId & "' AND gs_posNr='" & Me.gs_posNr & "'", "AVISO")
    End Function

End Class
