
Imports System.Data.SqlClient
Imports System.Reflection

Public Class cEmailBenachrichtigungList
    Property eb_KundenNr As Object = Nothing
    Property eb_ebartId As Object = Nothing
    Public LIST As New List(Of cEmailBenachrichtigung)
    Dim SQL As New SQL

    Sub New()

    End Sub


    Sub New(eb_KundenNr, eb_ebartId)
        Me.eb_KundenNr = eb_KundenNr
        Me.eb_ebartId = eb_ebartId
        LOAD(Me.eb_KundenNr, Me.eb_ebartId)
    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 cEmailBenachrichtigung
        l.eb_KundenNr = eb_KundenNr
        l.eb_ebartId = eb_ebartId
        l.eb_EMail = eb_EMail
        l.eb_cc = cc
        l.eb_bcc = bcc
        LIST.Add(l)
    End Sub
    Public Sub LOAD(eb_KundenNr, eb_ebartId)
        Try
            LIST.Clear()
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblEmailBenachrichtigung WHERE eb_KundenNr=@eb_KundenNr and eb_ebartId=@eb_ebartId  ", conn)
                    cmd.Parameters.AddWithValue("@eb_KundenNr", eb_KundenNr)
                    cmd.Parameters.AddWithValue("@eb_ebartId", eb_ebartId)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cEmailBenachrichtigung
                        l.eb_KundenNr = dr.Item("eb_KundenNr")
                        l.eb_ebartId = dr.Item("eb_ebartId")
                        l.eb_EMail = dr.Item("eb_EMail")
                        l.eb_cc = dr.Item("eb_cc")
                        l.eb_bcc = dr.Item("eb_bcc")
                        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
    Function SAVE()
        If SQL.doSQLVarList("DELETE FROM tblEmailBenachrichtigung WHERE eb_KundenNr=" & Me.eb_KundenNr & " and eb_ebartId=" & Me.eb_ebartId & " ", "FMZOLL") Then
            For Each li In LIST
                If Not li.INSERT() Then Return False
            Next
            Return True
        Else
            Return False
        End If
    End Function
End Class
Public Class cEmailBenachrichtigung
    Property eb_KundenNr As Object = Nothing
    Property eb_ebartId As Object = Nothing
    Property eb_EMail As Object = Nothing
    Property eb_cc As Boolean = False
    Property eb_bcc As Boolean = False

    Dim SQL As New SQL

    Sub New()
    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("eb_KundenNr", eb_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_ebartId", eb_ebartId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_EMail", eb_EMail))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_cc", eb_cc))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("eb_bcc", eb_bcc))
        Return list
    End Function

    Public Function INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        Return SQL.doSQLVarList(getInsertCmd, "FMZOLL", , list)
    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 tblEmailBenachrichtigung  (" & 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 Sub eMails_einlesen(Kdnr As String, art As Integer, ByRef Emails As String, ByRef Emails_CC As String, ByRef Emails_BCC As String, ByRef Emails_list_sent As VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList)
        'ART:
        '1=Ankunft
        '2=Freigabe
        '3=Ankunft Export
        '4=Freigabe Export
        Dim Firmen As New cFirmenDAL
        Dim Firma As New cFirma

        'In diesen Strings werden die Mailadressen übergeben:
        Emails = ""
        Emails_CC = ""
        Emails_BCC = ""

        If Kdnr Is Nothing OrElse Not IsNumeric(Kdnr) Then Exit Sub
        '  Dim Avisierer As New cKunde(hAuftraggeber_kdnr)
        '  Dim Fraechter As New cKunde(hFrächter_kdnr)
        Dim Kdnr_Mails As New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList(Kdnr, art)
        ' Dim Fraechter_Mails As New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList(hFrächter_kdnr, art)
        '  If hAuftraggeber_kdnr <> hFrächter_kdnr Then Fraechter_Mails = New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList(hFrächter_kdnr, art)

        Dim Emails_list As New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList
        Dim Emails_CC_list As New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList
        Dim Emails_BCC_list As New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList

        'Dim Emails As String
        For Each i In Kdnr_Mails.LIST


            If i.eb_bcc Then
                If Not existingNEW(Kdnr, i.eb_bcc, "BCC", Emails_list_sent.LIST) Then Emails_BCC_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            ElseIf i.eb_cc Then
                If Not existingNEW(Kdnr, i.eb_cc, "CC", Emails_list_sent.LIST) Then Emails_CC_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            Else
                If Not existingNEW(Kdnr, i.eb_EMail, "", Emails_list_sent.LIST) Then Emails_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            End If

            'DAS WAR VORHER:::::::::::
            'If i.eb_bcc Then
            '    If Not existing(Kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existingWOKdNr(i.eb_EMail, Emails_list_sent.LIST) Then Emails_BCC_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            'ElseIf i.eb_cc Then
            '    If Not existing(Kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existingWOKdNr(i.eb_EMail, Emails_list_sent.LIST) Then Emails_CC_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            'Else
            '    If Not existing(Kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existingWOKdNr(i.eb_EMail, Emails_list_sent.LIST) Then Emails_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            'End If
            ':::::::::::::::::::::::

            '''Es muss mit KdNr geprüft werden, sonst wird zB OPUS/SOMEDAY nciht gesendet (gleiche Mail, andere KdNr)
            '''If i.eb_bcc Then
            '''    If Not existing(Kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existingWOKdNr(i.eb_EMail, Emails_list_sent.LIST) Then Emails_BCC_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            '''ElseIf i.eb_cc Then
            '''    If Not existing(Kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existingWOKdNr(i.eb_EMail, Emails_list_sent.LIST) Then Emails_CC_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            '''Else
            '''    If Not existing(Kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existingWOKdNr(i.eb_EMail, Emails_list_sent.LIST) Then Emails_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            '''End If
        Next

        For Each i In Emails_list.LIST
            Emails &= i.eb_EMail & ";"
        Next
        For Each i In Emails_CC_list.LIST
            Emails_CC &= i.eb_EMail & ";"
        Next
        For Each i In Emails_BCC_list.LIST
            Emails_BCC &= i.eb_EMail & ";"
        Next

        For Each i In Emails_list.LIST : Emails_list_sent.LIST.Add(i) : Next
        For Each i In Emails_CC_list.LIST : Emails_list_sent.LIST.Add(i) : Next
        For Each i In Emails_BCC_list.LIST : Emails_list_sent.LIST.Add(i) : Next

    End Sub

    Public Shared Function getAbsenderEmail() As String
        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            Case "IMEX" : Return "noreply@imex-group.at"
            Case "ATILLA" : Return "noreply@verag.ag"
            Case "UNISPED" : Return "No-Reply@unisped.at"
            Case "FRONTOFFICE" : Return "noreply@front-office.eu"
            Case "AMBAR" : Return "import@ambarlog.com"
            Case Else : Return "noreply@verag.ag"
        End Select
    End Function

    Public Shared Function getBCCEmail() As String
        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            Case "IMEX" : Return "noreply@imex-group.at"
            Case "ATILLA" : Return "noreply@verag.ag"
            Case "UNISPED" : Return "No-Reply@unisped.at"
            Case "FRONTOFFICE" : Return "noreply@front-office.eu"
            Case "AMBAR" : Return "" '"noreply@verag.ag"
            Case Else : Return "noreply@verag.ag"
        End Select
    End Function

    Shared Sub eMails_einlesenOLD(hFrächter_kdnr As String, hAuftraggeber_kdnr As String, art As Integer, ByRef Emails As String, ByRef Emails_CC As String, ByRef Emails_BCC As String, ByRef Emails_list_sent As VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList)
        'ART:
        '1=Ankunft
        '2=Freigabe
        '3=Ankunft Export
        '4=Freigabe Export
        Dim Firmen As New cFirmenDAL
        Dim Firma As New cFirma

        Emails = ""
        Emails_CC = ""
        Emails_BCC = ""

        '  Dim Avisierer As New cKunde(hAuftraggeber_kdnr)
        '  Dim Fraechter As New cKunde(hFrächter_kdnr)
        Dim Avisierer_Mails As New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList(hAuftraggeber_kdnr, art)
        Dim Fraechter_Mails As New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList(hFrächter_kdnr, art)
        If hAuftraggeber_kdnr <> hFrächter_kdnr Then Fraechter_Mails = New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList(hFrächter_kdnr, art)

        Dim Emails_list As New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList
        Dim Emails_CC_list As New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList
        Dim Emails_BCC_list As New VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigungList

        'Dim Emails As String
        For Each i In Avisierer_Mails.LIST
            If i.eb_bcc Then
                If Not existing(hFrächter_kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existing(hFrächter_kdnr, i.eb_EMail, Emails_list_sent.LIST) Then Emails_BCC_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            ElseIf i.eb_cc Then
                If Not existing(hFrächter_kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existing(hFrächter_kdnr, i.eb_EMail, Emails_list_sent.LIST) Then Emails_CC_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            Else
                If Not existing(hFrächter_kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existing(hFrächter_kdnr, i.eb_EMail, Emails_list_sent.LIST) Then Emails_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            End If
        Next
        For Each i In Fraechter_Mails.LIST
            If i.eb_bcc Then
                If Not existing(hFrächter_kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existing(hFrächter_kdnr, i.eb_EMail, Emails_list_sent.LIST) Then Emails_BCC_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            ElseIf i.eb_cc Then
                If Not existing(hFrächter_kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existing(hFrächter_kdnr, i.eb_EMail, Emails_list_sent.LIST) Then Emails_CC_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            Else
                If Not existing(hFrächter_kdnr, i.eb_EMail, Emails_BCC_list.LIST, Emails_CC_list.LIST, Emails_list.LIST) And Not existing(hFrächter_kdnr, i.eb_EMail, Emails_list_sent.LIST) Then Emails_list.LIST.Add(i) ' &= i.eb_EMail & ";"
            End If
        Next

        For Each i In Emails_list.LIST
            Emails &= i.eb_EMail & ";"
        Next
        For Each i In Emails_CC_list.LIST
            Emails_CC &= i.eb_EMail & ";"
        Next
        For Each i In Emails_BCC_list.LIST
            Emails_BCC &= i.eb_EMail & ";"
        Next

        For Each i In Emails_list.LIST : Emails_list_sent.LIST.Add(i) : Next

    End Sub
    Shared Function existingNEW(KundenNr As Integer, Email As String, art As String, Emails_list As List(Of VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigung)) As Boolean
        If Emails_list IsNot Nothing Then
            For Each i In Emails_list
                Select Case art
                    Case "BCC" : If i.eb_bcc = Email And i.eb_KundenNr = KundenNr Then Return True
                    Case "CC" : If i.eb_cc = Email And i.eb_KundenNr = KundenNr Then Return True
                    Case Else : If i.eb_EMail = Email And i.eb_KundenNr = KundenNr Then Return True
                End Select

            Next
        End If

        Return False
    End Function
    Shared Function existing(KundenNr As Integer, Email As String, Emails_list As List(Of VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigung), Optional ListEmails_CC_list As List(Of VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigung) = Nothing, Optional ListEmails_BCC_list As List(Of VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigung) = Nothing) As Boolean
        If Emails_list IsNot Nothing Then
            For Each i In Emails_list
                If i.eb_EMail = Email And i.eb_KundenNr = KundenNr Then Return True
            Next
        End If
        If ListEmails_CC_list IsNot Nothing Then
            For Each i In ListEmails_CC_list
                If i.eb_EMail = Email And i.eb_KundenNr = KundenNr Then Return True
            Next
        End If
        If ListEmails_BCC_list IsNot Nothing Then
            For Each i In ListEmails_BCC_list
                If i.eb_EMail = Email And i.eb_KundenNr = KundenNr Then Return True
            Next
        End If
        Return False
    End Function
    Shared Function existingWOKdNr(Email As String, Emails_list As List(Of VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigung), Optional ListEmails_CC_list As List(Of VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigung) = Nothing, Optional ListEmails_BCC_list As List(Of VERAG_PROG_ALLGEMEIN.cEmailBenachrichtigung) = Nothing) As Boolean
        If Emails_list IsNot Nothing Then
            For Each i In Emails_list
                If i.eb_EMail = Email Then Return True
            Next
        End If
        If ListEmails_CC_list IsNot Nothing Then
            For Each i In ListEmails_CC_list
                If i.eb_EMail = Email Then Return True
            Next
        End If
        If ListEmails_BCC_list IsNot Nothing Then
            For Each i In ListEmails_BCC_list
                If i.eb_EMail = Email Then Return True
            Next
        End If
        Return False
    End Function

End Class
