diff --git a/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb b/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb index 177261cc..ebd81d6d 100644 --- a/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb +++ b/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb @@ -254,8 +254,8 @@ Public Class ImportZUGFeRDFiles oRejected = False End Try If oRejected = False Then - HISTORY_ID = oMD5DT.Rows(0).Item("GUID") - Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{HISTORY_ID}]") + Dim oHistoryId = oMD5DT.Rows(0).Item("GUID") + Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{oHistoryId}]") Else _logger.Info("ZuGFeRDFile already has been worked, but formerly obviously was rejected!") End If @@ -336,18 +336,20 @@ Public Class ImportZUGFeRDFiles 'If no errors occurred... 'Log the History If oMD5CheckSum <> String.Empty Then - Dim oInsertCommand = $"INSERT INTO TBEDM_ZUGFERD_HISTORY_IN (MESSAGE_ID, MD5HASH) VALUES ('{oMessageId}', '{oMD5CheckSum}')" - _firebird.ExecuteNonQueryWithConnection(oInsertCommand, oFBConnection, Firebird.TransactionMode.ExternalTransaction, oFBTransaction) + Create_HistoryEntry(oMessageId, oMD5CheckSum, "SUCCESS", oFBTransaction) - ' History ID is only need in case of an error - oFBTransaction.Commit() - - Try - Dim oSQL = $"SELECT MAX(GUID) FROM TBEDM_ZUGFERD_HISTORY_IN WHERE MESSAGE_ID = '{oMessageId}'" - HISTORY_ID = _firebird.GetScalarValue(oSQL) - Catch ex As Exception - HISTORY_ID = 0 - End Try + 'Dim oInsertCommand = $"INSERT INTO TBEDM_ZUGFERD_HISTORY_IN (MESSAGE_ID, MD5HASH) VALUES ('{oMessageId}', '{oMD5CheckSum}')" + '_firebird.ExecuteNonQueryWithConnection(oInsertCommand, oFBConnection, Firebird.TransactionMode.ExternalTransaction, oFBTransaction) + '' History ID is only need in case of an error + 'oFBTransaction.Commit() + 'Try + ' Dim oSQL = $"SELECT MAX(GUID) FROM TBEDM_ZUGFERD_HISTORY_IN WHERE MESSAGE_ID = '{oMessageId}'" + ' HISTORY_ID = _firebird.GetScalarValue(oSQL) + 'Catch ex As Exception + ' HISTORY_ID = 0 + 'End Try + Else + Create_HistoryEntry(oMessageId, String.Empty, "SUCCESS (with empty MD5Hash)", oFBTransaction) End If oIsSuccess = True @@ -355,10 +357,11 @@ Public Class ImportZUGFeRDFiles Catch ex As MD5HashException _logger.Error(ex) - 'oFBTransaction.Rollback() - Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - Already processed (MD5Hash)' WHERE GUID = '{HISTORY_ID}'" - _firebird.ExecuteNonQuery(oSQL) + 'Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = '{oMessage}' WHERE MESSAGE_ID = '{oMessageId}'" + '_firebird.ExecuteNonQuery(oSQL, oFBTransaction) + Dim oMessage = "REJECTED - Already processed (MD5Hash)" + Update_HistoryEntry(oMessageId, oMD5CheckSum, oMessage, oFBTransaction) Dim oBody = EmailStrings.EMAIL_MD5_ERROR Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) @@ -367,10 +370,13 @@ Public Class ImportZUGFeRDFiles Catch ex As InvalidFerdException _logger.Error(ex) - 'oFBTransaction.Rollback() - Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - ZUGFeRD yes but incorrect format' WHERE GUID = '{HISTORY_ID}'" - _firebird.ExecuteNonQuery(oSQL) + 'Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - ZUGFeRD yes but incorrect format' WHERE GUID = '{HISTORY_ID}'" + '_firebird.ExecuteNonQuery(oSQL, oFBTransaction) + + ' When InvalidFerdException is thrown, we don't have a MD5Hash yet. + ' That 's why we set it to String.Empty here. + Create_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but incorrect format", oFBTransaction) Dim oBody = EmailStrings.EMAIL_INVALID_DOCUMENT Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) @@ -379,10 +385,10 @@ Public Class ImportZUGFeRDFiles Catch ex As TooMuchFerdsException _logger.Error(ex) - 'oFBTransaction.Rollback() - Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - More than one ZUGFeRD-document in email' WHERE GUID = '{HISTORY_ID}'" - _firebird.ExecuteNonQuery(oSQL) + 'Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - More than one ZUGFeRD-document in email' WHERE GUID = '{HISTORY_ID}'" + '_firebird.ExecuteNonQuery(oSQL, oFBTransaction) + Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - More than one ZUGFeRD-document in email", oFBTransaction) Dim oBody = EmailStrings.EMAIL_TOO_MUCH_FERDS Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) @@ -391,10 +397,10 @@ Public Class ImportZUGFeRDFiles Catch ex As NoFerdsException _logger.Error(ex) - 'oFBTransaction.Rollback() - Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - no ZUGFeRD-Document in email' WHERE GUID = '{HISTORY_ID}'" - _firebird.ExecuteNonQuery(oSQL) + 'Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - no ZUGFeRD-Document in email' WHERE GUID = '{HISTORY_ID}'" + '_firebird.ExecuteNonQuery(oSQL, oFBTransaction) + Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - no ZUGFeRD-Document in email", oFBTransaction) Dim oBody = EmailStrings.EMAIL_NO_FERDS Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) @@ -403,14 +409,14 @@ Public Class ImportZUGFeRDFiles Catch ex As MissingValueException _logger.Error(ex) - 'oFBTransaction.Rollback() Dim oMessage As String = "" For Each prop In oMissingProperties oMessage &= $"- {prop}" Next - Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - Missing Required Properties: [{oMessage}]' WHERE GUID = '{HISTORY_ID}'" - _firebird.ExecuteNonQuery(oSQL) + 'Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - Missing Required Properties: [{oMessage}]' WHERE GUID = '{HISTORY_ID}'" + '_firebird.ExecuteNonQuery(oSQL, oFBTransaction) + Create_HistoryEntry(oMessageId, oMD5CheckSum, $"REJECTED - Missing Required Properties: [{oMessage}]", oFBTransaction) Dim oBody = CreateBodyForMissingProperties(ex.File.Name, oMissingProperties) Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) @@ -652,4 +658,32 @@ Public Class ImportZUGFeRDFiles Return "" End Try End Function + + Private Function Create_HistoryEntry(MessageId As String, MD5Checksum As String, Message As String, Transaction As FbTransaction) As Boolean + Try + Dim oSQL = $"INSERT INTO TBEDM_ZUGFERD_HISTORY_IN (COMMENT, MD5HASH, MESSAGE_ID) VALUES ('{Message}', '{MD5Checksum}', '{MessageId}')" + _firebird.ExecuteNonQueryWithConnection(oSQL, Transaction.Connection, Firebird.TransactionMode.ExternalTransaction, Transaction) + + Return True + Catch ex As Exception + _logger.Warn("History Entry count not be created for message id [{0}] and md5 [{1}]", MessageId, MD5Checksum) + _logger.Error(ex) + + Return False + End Try + End Function + + Private Function Update_HistoryEntry(MessageId As String, MD5Checksum As String, Message As String, Transaction As FbTransaction) As Boolean + Try + Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = '{Message}' WHERE MD5HASH = '{MD5Checksum}' AND MESSAGE_ID = '{MessageId}'" + _firebird.ExecuteNonQueryWithConnection(oSQL, Transaction.Connection, Firebird.TransactionMode.ExternalTransaction, Transaction) + + Return True + Catch ex As Exception + _logger.Warn("History Entry count not be updated for message id [{0}] and md5 [{1}]", MessageId, MD5Checksum) + _logger.Error(ex) + + Return False + End Try + End Function End Class