Imports IntraSell_DLL
Imports MySql.Data.MySqlClient

Module ModuleAnrufe

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="tel"></param>
    ''' <param name="Notizen"></param>
    ''' <param name="InformationPer"></param>
    ''' <remarks></remarks>
    Function AnrufProtokollierenFuerAnrufendeNummer(tel As String, Notizen As String, InformationPer As String) As Integer
        Try
            Dim gefundenIDNR = KundeSuchen(tel)
            'Kunde finden 
            If gefundenIDNR IsNot Nothing Then
                If gefundenIDNR.Count > 0 Then
                    AnrufProtokollieren(gefundenIDNR(0), Notizen, InformationPer)
                    Return gefundenIDNR(0)
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex, "Fehler in Funktion AnrufProtokollierenFuerAnrufendeNummer", Silent:=True)
        End Try
        Return Nothing
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="IdNr"></param>
    ''' <param name="Notizen"></param>
    ''' <param name="InformationPer"></param>
    ''' <remarks></remarks>
    Sub AnrufProtokollieren(IdNr As Integer, Notizen As String, InformationPer As String)
        Dim cmd = New MySqlCommand With {
            .CommandText = "INSERT into ofAnrufe ( AdrNr, Notizen, InformationPer, BeginZeit, Verrechnung) " &
            " values ( @AdrNr , @Notizen, @InformationPer, @BeginZeit, 0)",
            .Connection = CurrentDB
        }
        cmd.Parameters.AddWithValue("AdrNr", IdNr)
        cmd.Parameters.AddWithValue("Notizen", Notizen)
        cmd.Parameters.AddWithValue("InformationPer", InformationPer)
        cmd.Parameters.AddWithValue("BeginZeit", Now)
        cmd.ExecuteNonQuery()
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="idnr"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function HatHeuteNachrichtBekommen(IdNR As Integer) As Boolean
        Dim cmd = New MySqlCommand With {
            .CommandText = $"select BeginZeit from ofAnrufe 
where AdrNr in (select IDNR from ofAdressen 
					 WHERE 
					  (length(Mobil)>6 and Mobil = (select Mobil from ofAdressen where idnr=@IdNr))
				   OR (length(Tel)>6   and Mobil = (select Tel from ofAdressen where idnr=@IdNr)
				   OR (length(Email)>6 and Email = (select Email from ofAdressen where idnr=@IdNr))
				  )
					 ) 
order by BeginZeit desc",
            .Connection = CurrentDB
        }
        cmd.Parameters.AddWithValue("IdNR", IdNR)

        Dim lastDate = cmd.ExecuteScalar
        If lastDate IsNot Nothing And Not IsDBNull(lastDate) Then
            If CType(lastDate, Date) > Today.AddDays(-1) Then
                Return True
            End If
        End If
        Return False
    End Function

End Module
