

Imports System.Data.SqlClient
Imports System.Reflection

Public Class cKundenKontakteList
    Property kkd_KundenNr As Object = Nothing
    Property firma As Object = Nothing
    Public LIST As New List(Of cKundenKontakte)
    Dim SQL As New SQL
    'Dim listTodelete As New List(Of cOfferte)

    Sub New(kkd_KundenNr)
        Me.kkd_KundenNr = kkd_KundenNr
        '  Me.OffertenNr = OffertenNr
        LOAD_LIST(Me.kkd_KundenNr)
    End Sub

    Sub New(kkd_KundenNr, firma)
        Me.kkd_KundenNr = kkd_KundenNr
        Me.firma = firma
        LOAD_LIST(Me.kkd_KundenNr, firma)
    End Sub

    Public Sub CLEAR()
        LIST.Clear()
    End Sub
    Public Sub ADD(eb_EMail As String, Optional cc As Boolean = False, Optional bcc As Boolean = False)
        ' Dim l As New cOfferte
        'l.eb_bcc = bcc
        'LIST.Add(l)
    End Sub

    Public Sub LOAD_LIST(kkd_KundenNr, Optional firma = "")
        Try
            LIST.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL(False)
                Using cmd As New SqlCommand("SELECT * FROM [tblKundenKontakt] WHERE kkd_KundenNr=@kkd_KundenNr  " & If(firma <> "", " AND kkd_firma=@firma", ""), conn)
                    cmd.Parameters.AddWithValue("@kkd_KundenNr", kkd_KundenNr)
                    cmd.Parameters.AddWithValue("@firma", firma)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cKundenKontakte
                        For Each i In l.getParameterList()
                            Dim propInfo As PropertyInfo = l.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(l, Nothing)
                            Else
                                propInfo.SetValue(l, dr.Item(i.Text))
                            End If
                        Next
                        LIST.Add(l)

                    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)
        End Try
    End Sub

    'Public Function GET_KundenKontakte_VALUE(param As String) As String
    '    For Each p In LIST
    '        If p.bezeichnung = param Then
    '            Return p.wert
    '        End If
    '    Next
    '    Return ""
    'End Function

    'Public Function GET_KundenKontakte_VALUE_BOOL(param As String) As String
    '    For Each p In LIST
    '        If p.bezeichnung = param Then
    '            Return (p.wert = "1" Or p.wert = "true")
    '        End If
    '    Next
    '    Return False
    'End Function

End Class

Public Class cKundenKontakte
    Property kkd_id As Integer

    Property kkd_KundenNr As Integer
    Property kkd_kkaId As Object = Nothing
    Property kkd_kkaBez As Object = Nothing
    Property kkd_Tel As Object = Nothing
    Property kkd_Email As Object = Nothing
    Property kkd_EmailCC As Object = Nothing
    Property kkd_EmailBCC As Object = Nothing
    Property kkd_Fax As Object = Nothing
    Property kkd_Homepage As Object = Nothing
    Property kkd_Mobil As Object = Nothing
    Property kkd_AnsprechpartnerAnrede As Object = Nothing
    Property kkd_AnsprechpartnerVorname As Object = Nothing
    Property kkd_AnsprechpartnerNachname As Object = Nothing
    Property kkd_Ort As Object = Nothing
    Property kkd_PLZ As Object = Nothing
    Property kkd_Adresse As Object = Nothing
    Property kkd_GebDatum As Object = Nothing
    Property kkd_Land As Object = Nothing
    Property kkd_Passgueltigbis As Object = Nothing
    Property kkd_Firmenname As Object = Nothing
    Property kkd_Geburtsort As Object = Nothing
    Property kkd_Geburtsland As Object = Nothing
    Property kkd_letzteAenderungUser As String
    Property kkd_letzteAenderung As DateTime

    Public hasEntry = False




    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL


    Sub New()

    End Sub

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


    Sub New(kkda_id, kkd_KundenNr)
        Me.kkd_kkaId = kkda_id
        Me.kkd_KundenNr = kkd_KundenNr
        LOAD(kkda_id, kkd_KundenNr)
    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("kkd_id", kkd_id,, True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_KundenNr", kkd_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_kkaId", kkd_kkaId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_kkaBez", kkd_kkaBez))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Tel", kkd_Tel))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Email", kkd_Email))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_EmailCC", kkd_EmailCC))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_EmailBCC", kkd_EmailBCC))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Fax", kkd_Fax))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Homepage", kkd_Homepage))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Mobil", kkd_Mobil))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_AnsprechpartnerAnrede", kkd_AnsprechpartnerAnrede))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_AnsprechpartnerVorname", kkd_AnsprechpartnerVorname))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_AnsprechpartnerNachname", kkd_AnsprechpartnerNachname))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Adresse", kkd_Adresse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Ort", kkd_Ort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_PLZ", kkd_PLZ))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_GebDatum", kkd_GebDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Land", kkd_Land))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Passgueltigbis", kkd_Passgueltigbis))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Firmenname", kkd_Firmenname))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Geburtsort", kkd_Geburtsort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_Geburtsland", kkd_Geburtsland))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_letzteAenderungUser", kkd_letzteAenderungUser))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kkd_letzteAenderung", kkd_letzteAenderung))


        Return list
    End Function





    Public Sub LOAD(kkd_id)
        Try

            hasEntry = False

            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM [tblKundenKontakt] WHERE kkd_id=@kkd_id ", conn)
                    cmd.Parameters.AddWithValue("@kkd_id", kkd_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
            '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

    Public Sub LOAD(kkda_id, kkd_KundenNr)
        Try

            hasEntry = False

            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM [tblKundenKontakt] WHERE kkd_kkaId=@kkd_kkaId and kkd_KundenNr=@kkd_KundenNr ", conn)
                    cmd.Parameters.AddWithValue("@kkd_kkaId", kkda_id)
                    cmd.Parameters.AddWithValue("@kkd_KundenNr", kkd_KundenNr)

                    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
            '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



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

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblKundenKontakt   WHERE kkd_id=@kkd_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        kkd_id = SQL.doSQLVarListID(kkd_id, sqlstr, "FMZOLL",  , list)
        Return (kkd_id > 0)
    End Function

    Public Function INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        kkd_id = SQL.doSQLVarListID(kkd_id, getInsertCmd, "FMZOLL",  , list)
        Return (kkd_id > 0)
    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 [tblKundenKontakt] SET " & str & " WHERE kkd_id=@kkd_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 DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblKundenKontakt] WITH(updlock,serializable) WHERE kkd_id=" & Me.kkd_id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Public Shared Function DELETE_ALL_KONTAKTE(kkd_KundenNr, Optional kkd_kkaId = -1) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
        Dim sqlstr = " DELETE FROM [tblKundenKontakt] WITH(updlock,serializable) WHERE kkd_KundenNr=" & kkd_KundenNr & If(kkd_kkaId > 0, " AND kkd_kkaId='" & kkd_kkaId & "'", "")
        Return SQL.doSQL(sqlstr, "FMZOLL")
    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 tblKundenKontakt  (" & 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

    Shared Function getMailAdress(KdNr As Integer, bez As String) As String
        If KdNr < 0 Then Return ""
        Dim KD_LIST = New cKundenKontakteList(KdNr)

        If KD_LIST IsNot Nothing Then
            For Each li In KD_LIST.LIST
                If li.kkd_kkaBez = bez Then Return li.kkd_Email
            Next
        End If
        Return ""
    End Function
    Shared Function getMailAdressCC(KdNr As Integer, bez As String) As String
        If KdNr < 0 Then Return ""
        Dim KD_LIST = New cKundenKontakteList(KdNr)
        If KD_LIST IsNot Nothing Then
            For Each li In KD_LIST.LIST
                If li.kkd_kkaBez = bez Then Return li.kkd_EmailCC
            Next
        End If
        Return ""
    End Function
    Shared Function getMailAdressBCC(KdNr As Integer, bez As String) As String
        If KdNr < 0 Then Return ""
        Dim KD_LIST = New cKundenKontakteList(KdNr)
        If KD_LIST IsNot Nothing Then
            For Each li In KD_LIST.LIST
                If li.kkd_kkaBez = bez Then Return li.kkd_EmailBCC
            Next
        End If
        Return ""
    End Function
    Shared Function getKdNrFromMailAdress(KMail As String, Optional serchInKd As Boolean = True) As Integer
        getKdNrFromMailAdress = -1
        If KMail = "" Then Return -1
        If Not KMail.Contains("@") Then Return -1

        Dim whereFiliale = ""
        If VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA <> "VERAG" And VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA <> "ATILLA" Then
            whereFiliale = "  AND [FilialenNr]='" & VERAG_PROG_ALLGEMEIN.cAllgemein.STAMMFILIALE & "' "
        Else
            'SQLstr &= "  AND isnull([FilialenNr],0)<>'5501' "
            whereFiliale = "  AND isnull([FilialenNr],0) NOT IN ('5501','5601','5701','5801') "
        End If

        Dim SQL As New SQL
        getKdNrFromMailAdress = SQL.getValueTxtBySql("SELECT TOP 1 [kkd_KundenNr] FROM [tblKundenKontakt] INNER JOIN Kunden ON KundenNr=kkd_KundenNr where kkd_Email LIKE ('%" & KMail & "%') AND kkd_kkaBez='OUTLOOK_AVISO_Eingang' " & whereFiliale, "FMZOLL",,, -1)

        If serchInKd AndAlso getKdNrFromMailAdress < 0 Then
            getKdNrFromMailAdress = SQL.getValueTxtBySql("SELECT TOP 1 AdressenNr FROM Adressen INNER JOIN Kunden ON KundenNr=AdressenNr WHERE [E-Mail] LIKE ('%" & KMail & "%') " & whereFiliale, "FMZOLL",,, -1)

            If getKdNrFromMailAdress < 0 Then
                Dim sp = KMail.Split("@")
                getKdNrFromMailAdress = SQL.getValueTxtBySql("SELECT TOP 1 AdressenNr FROM Adressen INNER JOIN Kunden ON KundenNr=AdressenNr WHERE [E-Mail] LIKE ('%" & sp(1) & "%') " & whereFiliale, "FMZOLL",,, -1)
            End If
        End If
    End Function
End Class
