Imports System.Data.SqlClient
Imports System.Reflection

Public Class cKundenAufschubkonten

    Property kdAkto_id As Integer = -1
    Property kdAkto_KundenNr As Integer
    Property kdAkto_art As Integer
    Property kdAkto_hauptzollamt As Object = Nothing
    Property kdAkto_kontonummer As Object = Nothing
    Property kdAkto_bin As Object = Nothing
    Property kdAkto_eori As Object = Nothing
    Property kdAkto_eoriNL As Object = Nothing

    Dim SQL As New SQL



    Public Shared Function SAVE_LIST(KundenNr As Integer, LIST As List(Of cKundenAufschubkonten)) As Boolean
        SAVE_LIST = DELETE_NOT_EXIST(KundenNr, LIST)
        For Each li In LIST
            If Not li.SAVEByKdNrArt() Then SAVE_LIST = False
        Next
    End Function

    Public Shared Function DELETE_NOT_EXIST(KundenNr As Integer, LIST As List(Of cKundenAufschubkonten)) As Boolean
        Dim SQL As New SQL
        Dim ktoNr = ""
        For Each aa In LIST
            ktoNr &= aa.kdAkto_art & ","
        Next
        If ktoNr.EndsWith(",") Then ktoNr = ktoNr.Substring(0, ktoNr.Length - 1)

        Dim sqlstr = " DELETE FROM [tblKundenAufschubkonten] WHERE kdAkto_KundenNr='" & KundenNr & "' "
        If ktoNr <> "" Then sqlstr &= " AND [kdAkto_art] NOT IN (" & ktoNr & ") "

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


    Sub New()

    End Sub

    Sub New(kdAkto_id)
        Me.kdAkto_id = kdAkto_id
        LOAD()
    End Sub

    Sub New(kdAkto_KundenNr, kdAkto_art)
        Me.kdAkto_KundenNr = kdAkto_KundenNr
        Me.kdAkto_art = kdAkto_art
        LOADByKdNrArt(kdAkto_KundenNr, kdAkto_art)
    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("kdAkto_id", kdAkto_id, , True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdAkto_KundenNr", kdAkto_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdAkto_art", kdAkto_art))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdAkto_hauptzollamt", kdAkto_hauptzollamt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdAkto_kontonummer", kdAkto_kontonummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdAkto_bin", kdAkto_bin))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdAkto_eori", kdAkto_eori))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdAkto_eoriNL", kdAkto_eoriNL))
        Return list
    End Function


    Shared Function LOAD_ListAufschubKTOByKdNr(kdAkto_KundenNr) As List(Of cKundenAufschubkonten)
        Try
            LOAD_ListAufschubKTOByKdNr = New List(Of cKundenAufschubkonten)
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblKundenAufschubkonten WHERE kdAkto_KundenNr=@kdAkto_KundenNr  ", conn)
                    cmd.Parameters.AddWithValue("@kdAkto_KundenNr", kdAkto_KundenNr)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim KTO As New cKundenAufschubkonten
                        For Each li In KTO.getParameterList()
                            Dim propInfo As PropertyInfo = KTO.GetType.GetProperty(li.Scalarvariable)

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

                        Next
                        LOAD_ListAufschubKTOByKdNr.Add(KTO)
                    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)
            LOAD_ListAufschubKTOByKdNr = Nothing
        End Try
    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 tblKundenAufschubkonten WITH(updlock,serializable) WHERE kdAkto_id=@kdAkto_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        kdAkto_id = SQL.doSQLVarListID(kdAkto_id, sqlstr, "FMZOLL", , list)
        Return kdAkto_id > 0

    End Function


    Public Function SAVEByKdNrArt() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblKundenAufschubkonten WITH(updlock,serializable) WHERE kdAkto_KundenNr=@kdAkto_KundenNr and kdAkto_art=@kdAkto_art) " &
                           " BEGIN " & getUpdateCmd2() & " " &
                           " (SELECT kdAkto_id FROM tblKundenAufschubkonten WITH(updlock,serializable) WHERE kdAkto_KundenNr=@kdAkto_KundenNr and kdAkto_art=@kdAkto_art)" &
                           " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        kdAkto_id = SQL.doSQLVarListID(kdAkto_id, sqlstr, "FMZOLL", , list, True)
        Return kdAkto_id > 0

    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblKundenAufschubkonten WHERE kdAkto_id=@kdAkto_id  ", conn)
                    cmd.Parameters.AddWithValue("@kdAkto_id", kdAkto_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 Sub LOADByKdNrArt(kdAkto_KundenNr, kdAkto_art)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblKundenAufschubkonten WHERE kdAkto_KundenNr=@kdAkto_KundenNr AND kdAkto_art=@kdAkto_art  ", conn)
                    cmd.Parameters.AddWithValue("@kdAkto_KundenNr", kdAkto_KundenNr)
                    cmd.Parameters.AddWithValue("@kdAkto_art", kdAkto_art)
                    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 [tblKundenAufschubkonten] SET " & str & " WHERE kdAkto_id=@kdAkto_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 getUpdateCmd2() 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 [tblKundenAufschubkonten] SET " & str & " WHERE kdAkto_KundenNr=@kdAkto_KundenNr AND  kdAkto_art=@kdAkto_art ")

        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 And Not i.isonlyForLoad 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 tblKundenAufschubkonten  (" & 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
