Imports System.Data.SqlClient
Imports System.Reflection

Public Class cFremdwaehrungskurse


    Property fw_id As String
    Property fw_iso2 As String
    Property fw_iso3 As String
    Property fw_land As String
    Property fw_startdatum As Date
    Property fw_enddatum As Date
    Property fw_kurswert As Double


    Dim SQL As New SQL

    Sub New()

    End Sub


    Public 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("fw_id", fw_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fw_iso2", fw_iso2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fw_iso3", fw_iso3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fw_land", fw_land))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fw_startdatum", fw_startdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fw_enddatum", fw_enddatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("fw_kurswert", fw_kurswert))

        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 tblZOLL_Wechselkurse WITH(updlock,serializable) WHERE fw_iso2=@fw_iso2 AND fw_startdatum=@fw_startdatum AND fw_enddatum=@fw_enddatum AND fw_kurswert=@fw_kurswert) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "


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



    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 [tblZOLL_Wechselkurse] SET " & str & " WHERE fw_iso2=@fw_iso2 AND fw_startdatum=@fw_startdatum AND fw_enddatum=@fw_enddatum AND fw_kurswert=@fw_kurswert")

        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 tblZOLL_Wechselkurse  (" & 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() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
    '    Dim sqlstr = " DELETE FROM [tblEZB_Waehrungskurse] WITH(updlock,serializable) WHERE  ezb_waehrungscode='" & ezb_waehrungscode & "' AND ezb_datum='" & ezb_datum & "' "
    '    Return SQL.doSQL(sqlstr, "FMZOLL")
    'End Function


    'Shared Function UPDATE_Waehrungstabelle(datum As Date) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
    '    Dim sqlstr = " UPDATE [Währungstabelle] SET [Währungstabelle].Eurokurs=tblEZB_Waehrungskurse.ezb_kurs
    '                    FROM [Währungstabelle] inner join tblEZB_Waehrungskurse on [Währungstabelle].Währungsschlüssel=tblEZB_Waehrungskurse.ezb_waehrungsschluessel where  tblEZB_Waehrungskurse.ezb_datum='" & datum.ToShortDateString & "' "
    '    Return (New SQL).doSQL(sqlstr, "FMZOLL")
    'End Function

    'Shared Function GET_KURS(ezb_waehrungscode As String, datum As Date) As Double
    '    Dim sqlstr = " SELECT TOP 1 [ezb_kurs]  FROM [tblEZB_Waehrungskurse]  where ezb_waehrungscode='" & ezb_waehrungscode & "' and ezb_datum<='" & datum.ToShortTimeString & "' order by ezb_datum desc"
    '    Return (New SQL).getValueTxtBySql(sqlstr, "FMZOLL",,, 0)
    'End Function

    'Shared Function EXCHANGE(betrag As Double, waehrungscode As String, datum As Date) As Double
    '    Dim sqlstr = " SELECT TOP 1 [ezb_kurs]  FROM [tblEZB_Waehrungskurse]  where ezb_waehrungscode='" & waehrungscode & "' and ezb_datum<='" & datum.ToShortTimeString & "' order by ezb_datum desc"
    '    Return (New SQL).getValueTxtBySql(sqlstr, "FMZOLL",,, 0) * betrag
    'End Function


End Class
