Rejection Mails

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

View File

@ -95,6 +95,7 @@
<Compile Include="GraphQL\GraphQLModel.vb" /> <Compile Include="GraphQL\GraphQLModel.vb" />
<Compile Include="GraphQL\GraphQLQuery.vb" /> <Compile Include="GraphQL\GraphQLQuery.vb" />
<Compile Include="GraphQL\GraphQLWriter.vb" /> <Compile Include="GraphQL\GraphQLWriter.vb" />
<Compile Include="ZUGFeRD\ErrorCode.vb" />
<Compile Include="ZUGFeRD\EmailData.vb" /> <Compile Include="ZUGFeRD\EmailData.vb" />
<Compile Include="ZUGFeRD\EmailFunctions.vb" /> <Compile Include="ZUGFeRD\EmailFunctions.vb" />
<Compile Include="ZUGFeRD\EmailStrings.vb" /> <Compile Include="ZUGFeRD\EmailStrings.vb" />

View File

@ -5,6 +5,7 @@ Imports System.Data
Imports System.IO Imports System.IO
Imports System.Data.SqlClient Imports System.Data.SqlClient
Imports System.Collections.Generic Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Namespace ZUGFeRD Namespace ZUGFeRD
Public Class EmailFunctions Public Class EmailFunctions
@ -18,7 +19,112 @@ Namespace ZUGFeRD
_mssql = MSSQL _mssql = MSSQL
End Sub 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 If pEmailData Is Nothing Then
_logger.Warn("EmailData is empty. Email will not be sent!") _logger.Warn("EmailData is empty. Email will not be sent!")
Exit Sub Exit Sub

14
Jobs/ZUGFeRD/ErrorCode.vb Normal file
View File

@ -0,0 +1,14 @@
Public Enum ErrorCode
Unknown = 0
ValidationException = 20001
MD5HashException = 20002
UnsupportedFerdException = 20003
InvalidFerdException = 20004
TooMuchFerdsException = 20005
NoFerdsException = 20006
MissingValueException = 20007
FileSizeLimitReachedException = 20008
OutOfMemoryException = 20009
UnhandledException = 20010
FileMoveException = 200011
End Enum

View File

@ -122,6 +122,11 @@ Namespace ZUGFeRD
Dim oSource = _email.GetOriginalEmailPath(pArgs.OriginalEmailDirectory, pMessageId) Dim oSource = _email.GetOriginalEmailPath(pArgs.OriginalEmailDirectory, pMessageId)
_logger.Debug("Original email path: [{0}]", oSource) _logger.Debug("Original email path: [{0}]", oSource)
If oSource = String.Empty Then
_logger.Warn("Original Email for [{0}] could not be found. Exiting.", pMessageId)
Return New EmailData()
End If
Dim oDateSubDirectoryName As String = Now.ToString("yyyy-MM-dd") Dim oDateSubDirectoryName As String = Now.ToString("yyyy-MM-dd")
Dim oDestination As String Dim oDestination As String
@ -137,11 +142,6 @@ Namespace ZUGFeRD
End Try End Try
End If End If
If oSource = String.Empty Then
_logger.Warn("Original Email for [{0}] could not be found. Exiting.", pMessageId)
Return New EmailData()
End If
' If oEmailData is Nothing, TBEDM_EMAIL_PROFILER_HISTORY for MessageId was not found. ' If oEmailData is Nothing, TBEDM_EMAIL_PROFILER_HISTORY for MessageId was not found.
' This only should happen when testing and db-tables are deleted frequently ' This only should happen when testing and db-tables are deleted frequently
If oEmailData Is Nothing Then If oEmailData Is Nothing Then

View File

@ -228,7 +228,7 @@ Public Class ImportZUGFeRDFiles
Dim oBody = String.Format(EmailStrings.EMAIL_VALIDATION_ERROR, oErrorList) Dim oBody = String.Format(EmailStrings.EMAIL_VALIDATION_ERROR, oErrorList)
Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId)
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "ValidationException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "ValidationException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.ValidationException)
AddRejectedState(oMessageId, "ValidationException", "Die Rechnungsvalidierung ist fehlgeschlagen!", "", oSQLTransaction) AddRejectedState(oMessageId, "ValidationException", "Die Rechnungsvalidierung ist fehlgeschlagen!", "", oSQLTransaction)
Catch ex As MD5HashException Catch ex As MD5HashException
@ -241,7 +241,7 @@ Public Class ImportZUGFeRDFiles
Dim oBody = String.Format(EmailStrings.EMAIL_MD5_ERROR, ex.FileName) Dim oBody = String.Format(EmailStrings.EMAIL_MD5_ERROR, ex.FileName)
Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId)
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "MD5HashException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "MD5HashException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.MD5HashException)
AddRejectedState(oMessageId, "MD5HashException", "Die gesendete Rechnung wurde bereits verarbeitet!", "", oSQLTransaction) AddRejectedState(oMessageId, "MD5HashException", "Die gesendete Rechnung wurde bereits verarbeitet!", "", oSQLTransaction)
Catch ex As UnsupportedFerdException Catch ex As UnsupportedFerdException
@ -254,7 +254,7 @@ Public Class ImportZUGFeRDFiles
Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId)
Dim oBody As String = String.Format(EmailStrings.EMAIL_UNSUPPORTED_DOCUMENT, oEmailData.Subject, ex.XmlFile) Dim oBody As String = String.Format(EmailStrings.EMAIL_UNSUPPORTED_DOCUMENT, oEmailData.Subject, ex.XmlFile)
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "UnsupportedFerdException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "UnsupportedFerdException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.UnsupportedFerdException)
AddRejectedState(oMessageId, "UnsupportedFerdException", "Nicht unterstütztes Datenformat", "", oSQLTransaction) AddRejectedState(oMessageId, "UnsupportedFerdException", "Nicht unterstütztes Datenformat", "", oSQLTransaction)
Catch ex As InvalidFerdException Catch ex As InvalidFerdException
@ -267,7 +267,7 @@ Public Class ImportZUGFeRDFiles
Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId)
Dim oBody = String.Format(EmailStrings.EMAIL_INVALID_DOCUMENT, oEmailData.Subject) Dim oBody = String.Format(EmailStrings.EMAIL_INVALID_DOCUMENT, oEmailData.Subject)
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "InvalidFerdException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "InvalidFerdException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.InvalidFerdException)
AddRejectedState(oMessageId, "InvalidFerdException", "Inkorrekte Formate", "", oSQLTransaction) AddRejectedState(oMessageId, "InvalidFerdException", "Inkorrekte Formate", "", oSQLTransaction)
Catch ex As TooMuchFerdsException Catch ex As TooMuchFerdsException
@ -278,7 +278,7 @@ Public Class ImportZUGFeRDFiles
Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId)
Dim oBody = String.Format(EmailStrings.EMAIL_TOO_MUCH_FERDS, oEmailData.Subject) Dim oBody = String.Format(EmailStrings.EMAIL_TOO_MUCH_FERDS, oEmailData.Subject)
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "TooMuchFerdsException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "TooMuchFerdsException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.TooMuchFerdsException)
AddRejectedState(oMessageId, "TooMuchFerdsException", "Email enthielt mehr als ein ZUGFeRD-Dokument", "", oSQLTransaction) AddRejectedState(oMessageId, "TooMuchFerdsException", "Email enthielt mehr als ein ZUGFeRD-Dokument", "", oSQLTransaction)
Catch ex As NoFerdsException Catch ex As NoFerdsException
@ -289,7 +289,7 @@ Public Class ImportZUGFeRDFiles
Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId)
Dim oBody = String.Format(EmailStrings.EMAIL_NO_FERDS, oEmailData.Subject) Dim oBody = String.Format(EmailStrings.EMAIL_NO_FERDS, oEmailData.Subject)
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "NoFerdsException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "NoFerdsException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.NoFerdsException)
AddRejectedState(oMessageId, "NoFerdsException", " Email enthielt keine ZUGFeRD-Dokumente", "", oSQLTransaction) AddRejectedState(oMessageId, "NoFerdsException", " Email enthielt keine ZUGFeRD-Dokumente", "", oSQLTransaction)
Catch ex As MissingValueException Catch ex As MissingValueException
@ -304,7 +304,7 @@ Public Class ImportZUGFeRDFiles
Dim oBody = _email.CreateBodyForMissingProperties(ex.File.Name, ex.MissingProperties) Dim oBody = _email.CreateBodyForMissingProperties(ex.File.Name, ex.MissingProperties)
Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId)
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "MissingValueException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "MissingValueException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.MissingValueException)
AddRejectedState(oMessageId, "MissingValueException", "Es fehlten ZugferdSpezifikationen", oMessage, oSQLTransaction) AddRejectedState(oMessageId, "MissingValueException", "Es fehlten ZugferdSpezifikationen", oMessage, oSQLTransaction)
Catch ex As FileSizeLimitReachedException Catch ex As FileSizeLimitReachedException
@ -322,7 +322,7 @@ Public Class ImportZUGFeRDFiles
Dim oBody = String.Format(EmailStrings.EMAIL_FILE_SIZE_REACHED, oArgs.MaxAttachmentSizeInMegaBytes, oFileWithoutMessageId) Dim oBody = String.Format(EmailStrings.EMAIL_FILE_SIZE_REACHED, oArgs.MaxAttachmentSizeInMegaBytes, oFileWithoutMessageId)
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "FileSizeLimitReachedException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "FileSizeLimitReachedException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.FileSizeLimitReachedException)
AddRejectedState(oMessageId, "FileSizeLimitReachedException", "Erlaubte Dateigröße überschritten", "", oSQLTransaction) AddRejectedState(oMessageId, "FileSizeLimitReachedException", "Erlaubte Dateigröße überschritten", "", oSQLTransaction)
@ -341,7 +341,7 @@ Public Class ImportZUGFeRDFiles
.From = oArgs.ExceptionEmailAddress, .From = oArgs.ExceptionEmailAddress,
.Subject = $"OutOfMemoryException im ZUGFeRD-Parser @ {oMessageId}" .Subject = $"OutOfMemoryException im ZUGFeRD-Parser @ {oMessageId}"
} }
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "OutOfMemoryException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "OutOfMemoryException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.OutOfMemoryException)
' Rollback Transaction ' Rollback Transaction
oSQLTransaction.Rollback() oSQLTransaction.Rollback()
@ -360,7 +360,7 @@ Public Class ImportZUGFeRDFiles
.From = oArgs.ExceptionEmailAddress, .From = oArgs.ExceptionEmailAddress,
.Subject = $"UnhandledException im ZUGFeRD-Parser @ {oMessageId}" .Subject = $"UnhandledException im ZUGFeRD-Parser @ {oMessageId}"
} }
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "UnhandledException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "UnhandledException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.UnhandledException)
' Rollback Transaction ' Rollback Transaction
oSQLTransaction.Rollback() oSQLTransaction.Rollback()
@ -386,7 +386,7 @@ Public Class ImportZUGFeRDFiles
.From = oArgs.ExceptionEmailAddress, .From = oArgs.ExceptionEmailAddress,
.Subject = $"FileMoveException im ZUGFeRD-Parser @ {oMessageId}" .Subject = $"FileMoveException im ZUGFeRD-Parser @ {oMessageId}"
} }
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "FileMoveException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "FileMoveException", _EmailOutAccountId, oArgs.NamePortal, oArgs.RejectionTemplateId, ErrorCode.FileMoveException)
_logger.Warn("Could not move files!") _logger.Warn("Could not move files!")
_logger.Error(ex) _logger.Error(ex)

View File

@ -16,6 +16,7 @@ Public Class WorkerArgs
' Email Parameter ' Email Parameter
Public EmailOutProfileId As Integer = 0 Public EmailOutProfileId As Integer = 0
Public RejectionTemplateId As Integer = 0
' Misc Flag Parameters ' Misc Flag Parameters
Public ExceptionEmailAddress As String = Nothing Public ExceptionEmailAddress As String = Nothing