diff --git a/Jobs/Jobs.vbproj b/Jobs/Jobs.vbproj index 3b82f5ae..6b3deada 100644 --- a/Jobs/Jobs.vbproj +++ b/Jobs/Jobs.vbproj @@ -95,6 +95,7 @@ + diff --git a/Jobs/ZUGFeRD/EmailFunctions.vb b/Jobs/ZUGFeRD/EmailFunctions.vb index 3d7562c8..2569c01c 100644 --- a/Jobs/ZUGFeRD/EmailFunctions.vb +++ b/Jobs/ZUGFeRD/EmailFunctions.vb @@ -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) + ''' + ''' 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 + ''' + ''' E-Mail Message ID + ''' Body Text + ''' Email Data object + ''' Exception Title + ''' Sending Profile from config + ''' Name of the Portal from config + ''' ID for E-Mail-Template from config + ''' Error Code + 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 + + + ''' + ''' Function calls SP PRDD_SEND_REJECTION_MAIL + ''' for sending rejection mail. + ''' + ''' E-Mail Message ID + ''' GUID for TBDD_EMAIL_TEMPLATE from config + ''' ErrorID (TBDD_GUI_LANGUAGE_PHRASE) + ''' Sending profile from config + 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 diff --git a/Jobs/ZUGFeRD/ErrorCode.vb b/Jobs/ZUGFeRD/ErrorCode.vb new file mode 100644 index 00000000..0d8f41e9 --- /dev/null +++ b/Jobs/ZUGFeRD/ErrorCode.vb @@ -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 diff --git a/Jobs/ZUGFeRD/FileFunctions.vb b/Jobs/ZUGFeRD/FileFunctions.vb index e0060913..98ac7bd9 100644 --- a/Jobs/ZUGFeRD/FileFunctions.vb +++ b/Jobs/ZUGFeRD/FileFunctions.vb @@ -122,6 +122,11 @@ Namespace ZUGFeRD Dim oSource = _email.GetOriginalEmailPath(pArgs.OriginalEmailDirectory, pMessageId) _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 oDestination As String @@ -137,11 +142,6 @@ Namespace ZUGFeRD End Try 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. ' This only should happen when testing and db-tables are deleted frequently If oEmailData Is Nothing Then diff --git a/Jobs/ZUGFeRD/ImportZUGFeRDFiles.vb b/Jobs/ZUGFeRD/ImportZUGFeRDFiles.vb index e843ae64..3d4c7e27 100644 --- a/Jobs/ZUGFeRD/ImportZUGFeRDFiles.vb +++ b/Jobs/ZUGFeRD/ImportZUGFeRDFiles.vb @@ -228,7 +228,7 @@ Public Class ImportZUGFeRDFiles Dim oBody = String.Format(EmailStrings.EMAIL_VALIDATION_ERROR, oErrorList) 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) Catch ex As MD5HashException @@ -241,7 +241,7 @@ Public Class ImportZUGFeRDFiles Dim oBody = String.Format(EmailStrings.EMAIL_MD5_ERROR, ex.FileName) 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) Catch ex As UnsupportedFerdException @@ -254,7 +254,7 @@ Public Class ImportZUGFeRDFiles Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) 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) Catch ex As InvalidFerdException @@ -267,7 +267,7 @@ Public Class ImportZUGFeRDFiles Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) 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) Catch ex As TooMuchFerdsException @@ -278,7 +278,7 @@ Public Class ImportZUGFeRDFiles Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) 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) Catch ex As NoFerdsException @@ -289,7 +289,7 @@ Public Class ImportZUGFeRDFiles Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId) 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) Catch ex As MissingValueException @@ -304,7 +304,7 @@ Public Class ImportZUGFeRDFiles Dim oBody = _email.CreateBodyForMissingProperties(ex.File.Name, ex.MissingProperties) 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) Catch ex As FileSizeLimitReachedException @@ -322,7 +322,7 @@ Public Class ImportZUGFeRDFiles 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) @@ -341,7 +341,7 @@ Public Class ImportZUGFeRDFiles .From = oArgs.ExceptionEmailAddress, .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 oSQLTransaction.Rollback() @@ -360,7 +360,7 @@ Public Class ImportZUGFeRDFiles .From = oArgs.ExceptionEmailAddress, .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 oSQLTransaction.Rollback() @@ -386,7 +386,7 @@ Public Class ImportZUGFeRDFiles .From = oArgs.ExceptionEmailAddress, .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.Error(ex) diff --git a/Jobs/ZUGFeRD/WorkerArgs.vb b/Jobs/ZUGFeRD/WorkerArgs.vb index 154b150e..f10474e9 100644 --- a/Jobs/ZUGFeRD/WorkerArgs.vb +++ b/Jobs/ZUGFeRD/WorkerArgs.vb @@ -16,6 +16,7 @@ Public Class WorkerArgs ' Email Parameter Public EmailOutProfileId As Integer = 0 + Public RejectionTemplateId As Integer = 0 ' Misc Flag Parameters Public ExceptionEmailAddress As String = Nothing