Imports System.Data.SqlClient

Public Class cRKSV_Kasse

    Property rksv_id As Integer = -1
    Property rksv_bez As String = ""
    Property rksv_CompanyGUID As String = ""

    Property rksv_firma As String = ""
    Property rksv_startdatum As Object = Nothing
    Property rksv_aktiv As Boolean = True
    Property rksvDE_aktiv As Boolean = True
    Property rksv_BelegIdZaehler As Integer = 0
    Property rksv_Umsatzzaehler As Double = 0.0
    Property rksv_lastJWS As String = ""
    Property rksv_lastQR As String = ""
    Property rksv_lastBeleg As String = ""
    Property rksv_ITG_id As Object = Nothing
    Property rksv_AesKey As Object = Nothing
    Property rksv_KassaGuid As Object = Nothing
    Property rksv_ipAdresse As Object = Nothing
    Property rksv_DE_TSE_Serial As Object = Nothing
    Property rksv_DE_user As Object = Nothing
    Property rksv_DE_pwd As Object = Nothing
    Property rksv_DE_apiToken As Object = Nothing
    Property rksv_DE_license As Object = Nothing
    Property rksv_StornoIncreaseBelegCnt As Object = Nothing
    Property rksv_FT_CashboxID As Object = Nothing
    Property rksv_FT_AccessToken As Object = Nothing
    Property rksv_FT_RestServiceURL As Object = Nothing
    Property rksv_FT_Country As Object = Nothing


    Dim SQL As New SQL

    Public Sub New()

    End Sub
    Public Sub New(rksv_id)
        LOAD(rksv_id)
    End Sub

    Public Function INCREASE_BELEG_UMSATZ(umsatz) As Integer
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand(" UPDATE [tblRKSV_Kassen] SET rksv_BelegIdZaehler=isnull(rksv_BelegIdZaehler,0)+1, rksv_Umsatzzaehler=isnull(rksv_Umsatzzaehler,0)+@umsatz, rksv_lastBeleg=getDate()  WHERE rksv_id=@rksv_id; SELECT TOP 1 rksv_BelegIdZaehler,rksv_Umsatzzaehler,rksv_lastJWS,rksv_lastBeleg FROM tblRKSV_Kassen WHERE rksv_id=@rksv_id", conn)

                    cmd.Parameters.AddWithValue("@rksv_id", rksv_id)
                    cmd.Parameters.AddWithValue("@umsatz", umsatz)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        Me.rksv_BelegIdZaehler = cSqlDb.checkNullReturnValue(dr.Item("rksv_BelegIdZaehler"), Nothing)
                        Me.rksv_Umsatzzaehler = cSqlDb.checkNullReturnValue(dr.Item("rksv_Umsatzzaehler"), Nothing)
                        Me.rksv_lastBeleg = cSqlDb.checkNullReturnValue(dr.Item("rksv_lastBeleg"), Nothing)
                    End If
                    dr.Close()
                End Using
            End Using
            Return Me.rksv_BelegIdZaehler
        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 Nothing
        If False Then

            If rksv_BelegIdZaehler > 0 Then
                rksv_BelegIdZaehler += 1
            Else
                rksv_BelegIdZaehler = 1
            End If

            If rksv_Umsatzzaehler > 0 Then
                rksv_Umsatzzaehler += umsatz
            Else
                rksv_Umsatzzaehler = umsatz
            End If
            ' rksv_lastJWS = JWS
            SAVE()
        End If
        Return -1
    End Function
    Public Function DECREASE_BELEG_UMSATZ(rksv_BelegIdZaehler_old, umsatz) As Integer
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand(" UPDATE [tblRKSV_Kassen] SET rksv_BelegIdZaehler=@rksv_BelegIdZaehler, rksv_Umsatzzaehler=isnull(rksv_Umsatzzaehler,0)+@umsatz, rksv_lastBeleg=getDate()  WHERE rksv_id=@rksv_id; SELECT TOP 1 rksv_BelegIdZaehler,rksv_Umsatzzaehler,rksv_lastJWS,rksv_lastBeleg FROM tblRKSV_Kassen WHERE rksv_id=@rksv_id", conn)

                    cmd.Parameters.AddWithValue("@rksv_id", rksv_id)
                    cmd.Parameters.AddWithValue("@rksv_BelegIdZaehler", rksv_BelegIdZaehler_old)
                    cmd.Parameters.AddWithValue("@umsatz", umsatz)
                    cmd.CommandTimeout = 180
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        Me.rksv_BelegIdZaehler = cSqlDb.checkNullReturnValue(dr.Item("rksv_BelegIdZaehler"), Nothing)
                        Me.rksv_Umsatzzaehler = cSqlDb.checkNullReturnValue(dr.Item("rksv_Umsatzzaehler"), Nothing)
                        Me.rksv_lastBeleg = cSqlDb.checkNullReturnValue(dr.Item("rksv_lastBeleg"), Nothing)
                    End If
                    dr.Close()
                End Using
            End Using
            Return Me.rksv_BelegIdZaehler
        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 Nothing
        If False Then

            If rksv_BelegIdZaehler > 0 Then
                rksv_BelegIdZaehler += 1
            Else
                rksv_BelegIdZaehler = 1
            End If

            If rksv_Umsatzzaehler > 0 Then
                rksv_Umsatzzaehler += umsatz
            Else
                rksv_Umsatzzaehler = umsatz
            End If
            ' rksv_lastJWS = JWS
            SAVE()
        End If
        Return -1
    End Function
    Public Function INCREASE_ONLY_BELEG_UMSATZ(umsatz) As Integer
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand(" UPDATE [tblRKSV_Kassen] SET rksv_Umsatzzaehler=isnull(rksv_Umsatzzaehler,0)+@umsatz, rksv_lastBeleg=getDate()  WHERE rksv_id=@rksv_id; SELECT TOP 1 rksv_BelegIdZaehler,rksv_Umsatzzaehler,rksv_lastJWS,rksv_lastBeleg FROM tblRKSV_Kassen WHERE rksv_id=@rksv_id", conn)

                    cmd.Parameters.AddWithValue("@rksv_id", rksv_id)
                    cmd.Parameters.AddWithValue("@umsatz", umsatz)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        Me.rksv_BelegIdZaehler = cSqlDb.checkNullReturnValue(dr.Item("rksv_BelegIdZaehler"), Nothing)
                        Me.rksv_Umsatzzaehler = cSqlDb.checkNullReturnValue(dr.Item("rksv_Umsatzzaehler"), Nothing)
                        Me.rksv_lastBeleg = cSqlDb.checkNullReturnValue(dr.Item("rksv_lastBeleg"), Nothing)
                    End If
                    dr.Close()
                End Using
            End Using
            Return Me.rksv_BelegIdZaehler
        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 Nothing
        If False Then

            If rksv_BelegIdZaehler > 0 Then
                rksv_BelegIdZaehler += 1
            Else
                rksv_BelegIdZaehler = 1
            End If

            If rksv_Umsatzzaehler > 0 Then
                rksv_Umsatzzaehler += umsatz
            Else
                rksv_Umsatzzaehler = umsatz
            End If
            ' rksv_lastJWS = JWS
            SAVE()
        End If
        Return -1
    End Function

    Public Function SET_LAST_QR_JWS(JWS, qr) As Boolean
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand(" UPDATE [tblRKSV_Kassen] SET rksv_lastJWS=@jws,rksv_lastQR=@qr WHERE rksv_id=@rksv_id; SELECT TOP 1 rksv_lastJWS,rksv_lastQR FROM tblRKSV_Kassen WHERE rksv_id=@rksv_id", conn)

                    cmd.Parameters.AddWithValue("@rksv_id", rksv_id)
                    cmd.Parameters.AddWithValue("@JWS", JWS)
                    cmd.Parameters.AddWithValue("@qr", qr)

                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        Me.rksv_lastJWS = cSqlDb.checkNullReturnValue(dr.Item("rksv_lastJWS"), Nothing)
                        Me.rksv_lastQR = cSqlDb.checkNullReturnValue(dr.Item("rksv_lastQR"), Nothing)
                    End If
                    dr.Close()
                End Using
            End Using
            Return True
        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 False
    End Function


    Public Function SAVE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.MyListItem2) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM [tblRKSV_Kassen] WITH(updlock,serializable) WHERE rksv_id=@rksv_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "
        'MsgBox(sqlstr)
        Return SQL.doSQL(sqlstr, "FMZOLL", , list)
    End Function

    Public Sub LOAD(rksv_id As Integer)
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblRKSV_Kassen WHERE rksv_id=@rksv_id ", conn)
                    cmd.Parameters.AddWithValue("@rksv_id", rksv_id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then

                        Me.rksv_id = cSqlDb.checkNullReturnValue(dr.Item("rksv_id"), Nothing)
                        Me.rksv_bez = cSqlDb.checkNullReturnValue(dr.Item("rksv_bez"), Nothing)
                        Me.rksv_CompanyGUID = cSqlDb.checkNullReturnValue(dr.Item("rksv_CompanyGUID"), Nothing)

                        Me.rksv_firma = cSqlDb.checkNullReturnValue(dr.Item("rksv_firma"), Nothing)
                        Me.rksv_startdatum = cSqlDb.checkNullReturnValue(dr.Item("rksv_startdatum"), Nothing)
                        Me.rksv_aktiv = cSqlDb.checkNullReturnValue(dr.Item("rksv_aktiv"), Nothing)
                        Me.rksvDE_aktiv = cSqlDb.checkNullReturnValue(dr.Item("rksvDE_aktiv"), Nothing)
                        Me.rksv_BelegIdZaehler = cSqlDb.checkNullReturnValue(dr.Item("rksv_BelegIdZaehler"), Nothing)
                        Me.rksv_Umsatzzaehler = cSqlDb.checkNullReturnValue(dr.Item("rksv_Umsatzzaehler"), Nothing)
                        Me.rksv_lastJWS = cSqlDb.checkNullReturnValue(dr.Item("rksv_lastJWS"), Nothing)
                        Me.rksv_lastQR = cSqlDb.checkNullReturnValue(dr.Item("rksv_lastQR"), Nothing)
                        Me.rksv_lastBeleg = cSqlDb.checkNullReturnValue(dr.Item("rksv_lastBeleg"), Nothing)
                        Me.rksv_ITG_id = cSqlDb.checkNullReturnValue(dr.Item("rksv_ITG_id"), Nothing)
                        Me.rksv_AesKey = cSqlDb.checkNullReturnValue(dr.Item("rksv_AesKey"), Nothing)
                        Me.rksv_KassaGuid = cSqlDb.checkNullReturnValue(dr.Item("rksv_KassaGuid"), Nothing)
                        Me.rksv_ipAdresse = cSqlDb.checkNullReturnValue(dr.Item("rksv_ipAdresse"), Nothing)
                        Me.rksv_DE_TSE_Serial = cSqlDb.checkNullReturnValue(dr.Item("rksv_DE_TSE_Serial"), Nothing)
                        Me.rksv_DE_user = cSqlDb.checkNullReturnValue(dr.Item("rksv_DE_user"), Nothing)
                        Me.rksv_DE_pwd = cSqlDb.checkNullReturnValue(dr.Item("rksv_DE_pwd"), Nothing)
                        Me.rksv_DE_apiToken = cSqlDb.checkNullReturnValue(dr.Item("rksv_DE_apiToken"), Nothing)
                        Me.rksv_DE_license = cSqlDb.checkNullReturnValue(dr.Item("rksv_DE_license"), Nothing)
                        Me.rksv_StornoIncreaseBelegCnt = cSqlDb.checkNullBool(dr.Item("rksv_StornoIncreaseBelegCnt"))
                        Me.rksv_FT_CashboxID = cSqlDb.checkNullReturnValue(dr.Item("rksv_FT_CashboxID"), Nothing)
                        Me.rksv_FT_AccessToken = cSqlDb.checkNullReturnValue(dr.Item("rksv_FT_AccessToken"), Nothing)
                        Me.rksv_FT_RestServiceURL = cSqlDb.checkNullReturnValue(dr.Item("rksv_FT_RestServiceURL"), Nothing)
                        Me.rksv_FT_Country = cSqlDb.checkNullReturnValue(dr.Item("rksv_FT_Country"), Nothing)

                    End If
                    dr.Close()
                End Using
            End Using
            'Return Nothing
        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 Nothing
    End Sub


    Function getParameterList() As List(Of VERAG_PROG_ALLGEMEIN.MyListItem2)
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.MyListItem2)
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_id", rksv_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_bez", rksv_bez))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_CompanyGUID", rksv_CompanyGUID))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_firma", rksv_firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_startdatum", rksv_startdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_aktiv", rksv_aktiv))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksvDE_aktiv", rksvDE_aktiv))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_BelegIdZaehler", rksv_BelegIdZaehler))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_Umsatzzaehler", rksv_Umsatzzaehler))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_lastJWS", rksv_lastJWS))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_lastQR", rksv_lastQR))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_lastBeleg", rksv_lastBeleg))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_ITG_id", rksv_ITG_id))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_AesKey", rksv_AesKey))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_KassaGuid", rksv_KassaGuid))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_ipAdresse", rksv_ipAdresse))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_DE_TSE_Serial", rksv_DE_TSE_Serial))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_DE_user", rksv_DE_user))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_DE_pwd", rksv_DE_pwd))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_DE_apiToken", rksv_DE_apiToken))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_DE_license", rksv_DE_license))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_StornoIncreaseBelegCnt", rksv_StornoIncreaseBelegCnt))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_FT_CashboxID", rksv_FT_CashboxID))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_FT_AccessToken", rksv_FT_AccessToken))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_FT_RestServiceURL", rksv_FT_RestServiceURL))
        list.Add(New VERAG_PROG_ALLGEMEIN.MyListItem2("rksv_FT_Country", rksv_FT_Country))


        Return list
    End Function


    Public Function getUpdateCmd() As String
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.MyListItem2) = getParameterList()

            Dim str As String = ""
            For Each i In list
                str &= "[" & i.Text & "] = @" & i.Text.Replace("-", "").Replace(" ", "") & ","
            Next
            str = str.Substring(0, str.Length - 1) 'wg. ','
            Return (" UPDATE [tblRKSV_Kassen] SET " & str & " WHERE rksv_id=@rksv_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.MyListItem2) = getParameterList()
            Dim str As String = ""
            Dim values As String = ""
            For Each i In list
                str &= "[" & i.Text & "],"
                values &= "@" & i.Text.Replace("-", "").Replace(" ", "") & ","
            Next
            str = str.Substring(0, str.Length - 1) 'wg. ','
            values = values.Substring(0, values.Length - 1) 'wg. ','
            Return (" INSERT INTO [tblRKSV_Kassen]  (" & 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
