Imports System.Data.SqlClient
Imports System.Reflection

Public Class cGesamtsicherheitsReferenz

    Property gsr_id As Integer
    Property gsr_zollsatz As Integer

    'Property zollsatzMulti As Object = Nothing
    Property gsr_referenzwert As Object = Nothing
    Property gsr_ustEnabled As Boolean = False
    Property gsr_ust As Object = Nothing
    Property gsr_Saldo As Object = Nothing
    Property gsr_ATR As Boolean = False
    Property gsr_standort As Object = Nothing
    Property gsr_firma As Object = Nothing

    Public hasEntry = False

    Dim SQL As New SQL

    Sub New()

    End Sub


    Sub New(gsr_id As Integer)
        gsr_id = gsr_id
        LOADID()
    End Sub

    Sub New(_gsr_standort As String)
        gsr_standort = _gsr_standort
        LOAD()
    End Sub

    Sub New(_gsr_standort As String, _gsr_firma As String)
        gsr_standort = _gsr_standort
        gsr_firma = _gsr_firma
        LOAD(_gsr_firma)
    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("gsr_standort", gsr_standort,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsr_zollsatz", gsr_zollsatz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsr_referenzwert", gsr_referenzwert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsr_ustEnabled", gsr_ustEnabled))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsr_ust", gsr_ust))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsr_ATR", gsr_ATR))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsr_id", gsr_id))


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

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

    Public Sub LOAD(Optional Firma As String = "")


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


                Using cmd As New SqlCommand("SELECT * FROM tblGesamtsicherheitReferenz WHERE gsr_Standort=@gsr_standort  " & IIf(Firma <> "", "AND gsr_firma=@gsr_firma", ""), conn)
                    cmd.Parameters.AddWithValue("@gsr_standort", gsr_standort)
                    If Firma <> "" Then cmd.Parameters.AddWithValue("@gsr_firma", gsr_firma)
                    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 LOADID(Optional Firma As String = "")


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

                Using cmd As New SqlCommand("SELECT * FROM tblGesamtsicherheitReferenz WHERE gsr_id=@gsr_id  ", conn)
                    cmd.Parameters.AddWithValue("@gsr_id", gsr_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 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 [tblGesamtsicherheitReferenz] SET " & str & " WHERE gsr_standort=@gsr_standort ")

        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 [tblGesamtsicherheitReferenz]  (" & 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 [tblGesamtsicherheitReferenz] Set gsr_aktSaldo = '" & gsr_saldo & "' WHERE gsr_Standort = '" & gsr_standort & "'", "AVISO")
    End Function

    Public Function CalcSaldo() 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 = '" & gsr_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_gsnr IN (select gs_gsnr from [tblGesamtsicherheit] where gs_standort = '" & gsr_standort & "' ))"

        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

        gsr_saldo = gsr_referenzwert + mrn
        Return gsr_saldo
    End Function

End Class
