

Imports System.Data.SqlClient
Imports System.Reflection

Public Class cAufschubKonten
    Property brgakto_id As Integer
    Property brgakto_bez As String
    Property brgakto_betrag As Decimal
    Property brgakto_ktonr As String
    Property brgakto_warn1 As Decimal
    Property brgakto_warn2 As Decimal
    Property brgakto_gs_standort As Object = Nothing
    Property brgakto_gs_zollsatz As Integer
    Property brgakto_gs_ustEnabled As Boolean = False
    Property brgakto_gs_ust As Object = Nothing
    Property brgakto_gs_aktSaldo As Decimal
    Property brgakto_gs_ATR As Boolean = False

    Public hasEntry = False

    Dim SQL As New SQL

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

    Sub New(brgakto_gs_standort As String)
        Me.brgakto_gs_standort = brgakto_gs_standort
        LOADStandort()
        brgakto_betrag = getReferenzbetrag(Today)
    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("brgakto_id", brgakto_id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_bez", brgakto_bez))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_betrag", brgakto_betrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_ktonr", brgakto_ktonr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_warn1", brgakto_warn1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_warn2", brgakto_warn2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_gs_standort", brgakto_gs_standort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_gs_zollsatz", brgakto_gs_zollsatz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_gs_ustEnabled", brgakto_gs_ustEnabled))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_gs_ust", brgakto_gs_ust))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_gs_ATR", brgakto_gs_ATR))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("brgakto_gs_aktSaldo", brgakto_gs_aktSaldo))


        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 tblBrgAufschubKonten  WHERE brgakto_id=@brgakto_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

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


    Public Sub LOADStandort()


        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()


                Using cmd As New SqlCommand("SELECT * FROM tblBrgAufschubKonten WHERE brgakto_gs_standort=@brgakto_gs_standort  ", conn)
                    cmd.Parameters.AddWithValue("@brgakto_gs_standort", brgakto_gs_standort)
                    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 [tblBrgAufschubKonten] SET " & str & " WHERE brgakto_id=@brgakto_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 tblBrgAufschubKonten  (" & 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 updateSaldo()
        Return SQL.doSQL("Update [tblBrgAufschubKonten] Set brgakto_gs_aktSaldo = CAST(REPLACE('" & brgakto_gs_aktSaldo & "' ,',','.')AS decimal(18,2)) WHERE brgakto_gs_standort = '" & brgakto_gs_standort & "'", "ADMIN")
    End Function

    Public Function CalcSaldo(Optional dtDate As DataTable = Nothing) As String

        Dim SaldoString As String =
            "select

                (select SUM( 
                    case 
    	                when gs_art = 'Open' then (gs_sicherheitsbetrag * -1)
    	                when gs_art = 'Close' then gs_sicherheitsbetrag
                    end) 
					
					as SicherheitsSaldoGS from [tblGesamtsicherheit] Where gs_standort = '" & brgakto_gs_standort & "')+

                    (select SUM( 
                    case  
    	                when gsp_art = 'Open' then (gsp_sicherheitsbetrag * -1)
    	                when gsp_art = 'Close' then gsp_sicherheitsbetrag
                    end) 
					as SicherheitsSaldoPositionen from [tblGesamtsicherheitsPositionen] Where gsp_gsid IN (select gs_gsId from [tblGesamtsicherheit] where gs_standort = '" & brgakto_gs_standort & "' And ISNULL(gs_ReferenzAenderung,0) = 0 ))"

        Dim dt As New DataTable
            Dim mrn As Double = 0
            dt = SQL.loadDgvBySql(SaldoString, "AVISO", 100, True)
            If dt.Rows(0).Item(0).ToString.Length > 0 Then
                mrn = dt.Rows(0).Item(0)
            End If

        brgakto_gs_aktSaldo = getReferenzbetrag(Today) + mrn
        updateSaldo()


        Return brgakto_gs_aktSaldo
    End Function

    Public Function getReferenzbetrag(nowDate As Date) As Double

        Try

            Return SQL.getValueTxtBySql("select TOP (1) brgarefb_refbetrag from [tblBrgAufschubReferenzberaege] where [brgarefb_gsid]='" & brgakto_id & "' and brgarefb_gs_aktiv = 1 And [brgarefb_gueltig_ab] <= '" & nowDate & "' order by [brgarefb_gueltig_ab] desc", "ADMIN",,,,)

        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 Function

End Class
