Rejection Mails

Calling DB Procedure to created Rejection Mails
This commit is contained in:
2024-03-20 13:04:42 +01:00
parent 8d672b4b49
commit c83d24a086
6 changed files with 139 additions and 17 deletions

View File

@@ -5,6 +5,7 @@ Imports System.Data
Imports System.IO
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Namespace ZUGFeRD
Public Class EmailFunctions
@@ -18,7 +19,112 @@ Namespace ZUGFeRD
_mssql = MSSQL
End Sub
Public Sub AddToEmailQueueMSSQL(MessageId As String, BodyText As String, pEmailData As EmailData, SourceProcedure As String, pEmailAccountId As Integer, NamePortal As String)
''' <summary>
''' Method to decide wether we use the old or the new
''' Rejection E-mail method.
'''
''' TODO we have no information about the language of the receiver at the moment
''' </summary>
''' <param name="pMessageId">E-Mail Message ID</param>
''' <param name="pBodyText">Body Text</param>
''' <param name="pEmailData">Email Data object</param>
''' <param name="pSourceProcedure">Exception Title</param>
''' <param name="pEmailAccountId">Sending Profile from config</param>
''' <param name="pNamePortal">Name of the Portal from config</param>
''' <param name="pTemplateId">ID for E-Mail-Template from config</param>
''' <param name="pErrorCode">Error Code</param>
Public Sub AddToEmailQueueMSSQL(pMessageId As String, pBodyText As String, pEmailData As EmailData, pSourceProcedure As String,
pEmailAccountId As Integer, pNamePortal As String, pTemplateId As Integer, pErrorCode As ErrorCode)
Dim useLegacyMethod = True
Dim oErrorCode As String = String.Empty
' ErrorCode valid?
If pErrorCode <> ErrorCode.Unknown Then
Dim intCode As Integer = DirectCast(pErrorCode, Integer)
oErrorCode = $"ZUGFERD_Rejection_{intCode}"
Dim oSQL = $"SELECT COUNT(*) FROM TBDD_GUI_LANGUAGE_PHRASE WHERE TITLE = '{oErrorCode}'"
If _mssql.GetScalarValue(oSQL) > 0 Then
useLegacyMethod = False
Else
_logger.Warn($"Rejection reason [{oErrorCode}] not found in TBDD_GUI_LANGUAGE_PHRASE!")
End If
End If
' Gibt es das Template in TBDD_EMAIL_TEMPLATE?
If useLegacyMethod = False AndAlso pTemplateId > 0 Then
Try
Dim oSQL = $"SELECT COUNT(*) FROM TBDD_EMAIL_TEMPLATE WHERE GUID = {pTemplateId}"
If _mssql.GetScalarValue(oSQL) <= 0 Then
_logger.Warn($"EMAIL_TEMPLATE [{pTemplateId}] not found in TBDD_EMAIL_TEMPLATE!")
useLegacyMethod = True
End If
Catch ex As Exception
_logger.Error(ex)
useLegacyMethod = True
End Try
Else
_logger.Debug($"RejectionTemplateId not configured!")
useLegacyMethod = True
End If
' Check if Stored Procedure PRDD_SEND_REJECTION_MAIL exists
If useLegacyMethod = False Then
Try
Dim oSQL = $"SELECT COUNT(*) FROM sys.objects WHERE type = 'P' AND OBJECT_ID = OBJECT_ID('dbo.PRDD_SEND_REJECTION_MAIL')"
If _mssql.GetScalarValue(oSQL) <= 0 Then
_logger.Warn($"Procedure ['PRDD_SEND_REJECTION_MAIL'] not found in Database!")
useLegacyMethod = True
End If
Catch ex As Exception
_logger.Error(ex)
useLegacyMethod = True
End Try
End If
If useLegacyMethod = True Then
_logger.Warn("New rejection mail logic is not configured correctly, use legacy logic instead!")
AddToEmailQueueMSSQL(pMessageId, pBodyText, pEmailData, pSourceProcedure, pEmailAccountId, pNamePortal)
Else
_logger.Debug("New rejection mail logic is configured!")
AddToEmailQueueMSSQL(pMessageId, pTemplateId, oErrorCode, pEmailAccountId)
End If
End Sub
''' <summary>
''' Function calls SP PRDD_SEND_REJECTION_MAIL
''' for sending rejection mail.
''' </summary>
''' <param name="pMessageId">E-Mail Message ID</param>
''' <param name="pTemplateId">GUID for TBDD_EMAIL_TEMPLATE from config</param>
''' <param name="pErrorCode">ErrorID (TBDD_GUI_LANGUAGE_PHRASE)</param>
''' <param name="pEmailAccountId">Sending profile from config</param>
Private Sub AddToEmailQueueMSSQL(pMessageId As String, pTemplateId As Integer, pErrorCode As String, pEmailAccountId As Integer)
Try
Dim oExecute = $"EXECUTE dbo.PRDD_SEND_REJECTION_MAIL
'{pMessageId}'
, 0
, {pEmailAccountId}
, 'ZUGFeRD Service'
, {pTemplateId}
, '{pErrorCode}'
, 77
"
If _mssql.ExecuteNonQuery(oExecute) = False Then
_logger.Warn("Could not execute PRDD_SEND_REJECTION_MAIL. See error log!")
End If
Catch ex As Exception
_logger.Error(ex)
End Try
End Sub
Private Sub AddToEmailQueueMSSQL(MessageId As String, BodyText As String, pEmailData As EmailData, SourceProcedure As String, pEmailAccountId As Integer, NamePortal As String)
If pEmailData Is Nothing Then
_logger.Warn("EmailData is empty. Email will not be sent!")
Exit Sub