Compare commits
2 Commits
3c149a32e9
...
a64823ae5e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a64823ae5e | ||
|
|
c187bdbe5e |
@@ -3,7 +3,7 @@ Imports System.Text.RegularExpressions
|
|||||||
Imports DigitalData.Modules.Logging
|
Imports DigitalData.Modules.Logging
|
||||||
|
|
||||||
Public Class FileGroups
|
Public Class FileGroups
|
||||||
Private _logger As Logger
|
Private ReadOnly _logger As Logger
|
||||||
|
|
||||||
Public Sub New(LogConfig As LogConfig)
|
Public Sub New(LogConfig As LogConfig)
|
||||||
_logger = LogConfig.GetLogger()
|
_logger = LogConfig.GetLogger()
|
||||||
@@ -25,7 +25,7 @@ Public Class FileGroups
|
|||||||
Dim oMessageId = GetMessageIdFromFileName(oFile.Name)
|
Dim oMessageId = GetMessageIdFromFileName(oFile.Name)
|
||||||
|
|
||||||
If oMessageId Is Nothing Then
|
If oMessageId Is Nothing Then
|
||||||
_logger.Warn("File {0} did not have the required filename-format!", oMessageId)
|
_logger.Warn("File {0} did not have the required filename-format!", oFile.Name)
|
||||||
Continue For
|
Continue For
|
||||||
End If
|
End If
|
||||||
|
|
||||||
|
|||||||
@@ -7,11 +7,13 @@ Public Class Exceptions
|
|||||||
Inherits ApplicationException
|
Inherits ApplicationException
|
||||||
|
|
||||||
Public ReadOnly File As FileInfo
|
Public ReadOnly File As FileInfo
|
||||||
|
Public ReadOnly MissingProperties As List(Of String)
|
||||||
|
|
||||||
Public Sub New(File As FileInfo)
|
Public Sub New(pFile As FileInfo, pMissingProperties As List(Of String))
|
||||||
MyBase.New($"Missing values in [{File.Name}]")
|
MyBase.New($"Missing values in [{pFile.Name}]")
|
||||||
|
|
||||||
Me.File = File
|
Me.File = pFile
|
||||||
|
Me.MissingProperties = pMissingProperties
|
||||||
End Sub
|
End Sub
|
||||||
End Class
|
End Class
|
||||||
|
|
||||||
|
|||||||
@@ -97,6 +97,7 @@
|
|||||||
<Compile Include="ZUGFeRD\EmailFunctions.vb" />
|
<Compile Include="ZUGFeRD\EmailFunctions.vb" />
|
||||||
<Compile Include="ZUGFeRD\EmailStrings.vb" />
|
<Compile Include="ZUGFeRD\EmailStrings.vb" />
|
||||||
<Compile Include="ZUGFeRD\FileFunctions.vb" />
|
<Compile Include="ZUGFeRD\FileFunctions.vb" />
|
||||||
|
<Compile Include="ZUGFeRD\HashFunctions.vb" />
|
||||||
<Compile Include="ZUGFeRD\HistoryFunctions.vb" />
|
<Compile Include="ZUGFeRD\HistoryFunctions.vb" />
|
||||||
<Compile Include="ZUGFeRD\ImportZUGFeRDFiles.vb" />
|
<Compile Include="ZUGFeRD\ImportZUGFeRDFiles.vb" />
|
||||||
<Compile Include="ZUGFeRD\WorkerArgs.vb" />
|
<Compile Include="ZUGFeRD\WorkerArgs.vb" />
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ Imports System.Runtime.InteropServices
|
|||||||
<Assembly: AssemblyCompany("Digital Data")>
|
<Assembly: AssemblyCompany("Digital Data")>
|
||||||
<Assembly: AssemblyProduct("Modules.Jobs")>
|
<Assembly: AssemblyProduct("Modules.Jobs")>
|
||||||
<Assembly: AssemblyCopyright("Copyright © 2023")>
|
<Assembly: AssemblyCopyright("Copyright © 2023")>
|
||||||
<Assembly: AssemblyTrademark("2.1.0.0")>
|
<Assembly: AssemblyTrademark("2.2.0.0")>
|
||||||
|
|
||||||
<Assembly: ComVisible(False)>
|
<Assembly: ComVisible(False)>
|
||||||
|
|
||||||
@@ -30,5 +30,5 @@ Imports System.Runtime.InteropServices
|
|||||||
' Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
|
' Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
|
||||||
' übernehmen, indem Sie "*" eingeben:
|
' übernehmen, indem Sie "*" eingeben:
|
||||||
|
|
||||||
<Assembly: AssemblyVersion("2.1.0.0")>
|
<Assembly: AssemblyVersion("2.2.0.0")>
|
||||||
<Assembly: AssemblyFileVersion("2.1.0.0")>
|
<Assembly: AssemblyFileVersion("2.2.0.0")>
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ Imports DigitalData.Modules.Base
|
|||||||
Imports DigitalData.Modules.Database
|
Imports DigitalData.Modules.Database
|
||||||
Imports DigitalData.Modules.Interfaces
|
Imports DigitalData.Modules.Interfaces
|
||||||
Imports DigitalData.Modules.Logging
|
Imports DigitalData.Modules.Logging
|
||||||
Imports Microsoft.VisualBasic.FileIO
|
|
||||||
|
|
||||||
Namespace ZUGFeRD
|
Namespace ZUGFeRD
|
||||||
Public Class FileFunctions
|
Public Class FileFunctions
|
||||||
@@ -13,29 +12,31 @@ Namespace ZUGFeRD
|
|||||||
Private ReadOnly _logger As Logger
|
Private ReadOnly _logger As Logger
|
||||||
Private ReadOnly _mssql As MSSQLServer
|
Private ReadOnly _mssql As MSSQLServer
|
||||||
Private ReadOnly _filesystem As FilesystemEx
|
Private ReadOnly _filesystem As FilesystemEx
|
||||||
|
Private ReadOnly _email As ZUGFeRD.EmailFunctions
|
||||||
|
|
||||||
Public Sub New(LogConfig As LogConfig, MSSQL As MSSQLServer)
|
Public Sub New(pLogConfig As LogConfig, pMSSQL As MSSQLServer)
|
||||||
_logConfig = LogConfig
|
_logConfig = pLogConfig
|
||||||
_logger = _logConfig.GetLogger()
|
_logger = _logConfig.GetLogger()
|
||||||
_mssql = MSSQL
|
_mssql = pMSSQL
|
||||||
_filesystem = New FilesystemEx(LogConfig)
|
_email = New EmailFunctions(pLogConfig, pMSSQL)
|
||||||
|
_filesystem = New FilesystemEx(pLogConfig)
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Public Sub MoveFiles(
|
Public Sub MoveFiles(
|
||||||
Args As WorkerArgs,
|
pArgs As WorkerArgs,
|
||||||
MessageId As String,
|
pMessageId As String,
|
||||||
Files As List(Of FileInfo),
|
pFiles As List(Of FileInfo),
|
||||||
AttachmentFiles As List(Of FileInfo),
|
pAttachmentFiles As List(Of FileInfo),
|
||||||
EmbeddedAttachments As List(Of PDFEmbeds.EmbeddedFile),
|
pEmbeddedAttachments As List(Of PDFEmbeds.EmbeddedFile),
|
||||||
MoveDirectory As String,
|
pMoveDirectory As String,
|
||||||
IsSuccess As Boolean)
|
pIsSuccess As Boolean)
|
||||||
|
|
||||||
Dim oFinalMoveDirectory As String = MoveDirectory
|
Dim oFinalMoveDirectory As String = pMoveDirectory
|
||||||
Dim oDateSubDirectoryName As String = Now.ToString("yyyy\\MM\\dd")
|
Dim oDateSubDirectoryName As String = Now.ToString("yyyy\\MM\\dd")
|
||||||
Dim oAttachmentDirectory As String = Path.Combine(oFinalMoveDirectory, Args.AttachmentsSubDirectory, oDateSubDirectoryName)
|
Dim oAttachmentDirectory As String = Path.Combine(oFinalMoveDirectory, pArgs.AttachmentsSubDirectory, oDateSubDirectoryName)
|
||||||
|
|
||||||
' Files will be moved to a subfolder for the current day if they are rejected
|
' Files will be moved to a subfolder for the current day if they are rejected
|
||||||
If Not IsSuccess Then
|
If Not pIsSuccess Then
|
||||||
oFinalMoveDirectory = Path.Combine(oFinalMoveDirectory, oDateSubDirectoryName)
|
oFinalMoveDirectory = Path.Combine(oFinalMoveDirectory, oDateSubDirectoryName)
|
||||||
End If
|
End If
|
||||||
|
|
||||||
@@ -48,7 +49,7 @@ Namespace ZUGFeRD
|
|||||||
End Try
|
End Try
|
||||||
End If
|
End If
|
||||||
|
|
||||||
If Not Directory.Exists(oAttachmentDirectory) And AttachmentFiles.Count > 0 Then
|
If Not Directory.Exists(oAttachmentDirectory) And pAttachmentFiles.Count > 0 Then
|
||||||
Try
|
Try
|
||||||
Directory.CreateDirectory(oAttachmentDirectory)
|
Directory.CreateDirectory(oAttachmentDirectory)
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
@@ -57,7 +58,7 @@ Namespace ZUGFeRD
|
|||||||
End If
|
End If
|
||||||
|
|
||||||
' Filter out Attachments from `Files`
|
' Filter out Attachments from `Files`
|
||||||
Dim oInvoiceFiles As List(Of FileInfo) = Files.Except(AttachmentFiles).ToList()
|
Dim oInvoiceFiles As List(Of FileInfo) = pFiles.Except(pAttachmentFiles).ToList()
|
||||||
|
|
||||||
' Move PDF/A Files
|
' Move PDF/A Files
|
||||||
For Each oFile In oInvoiceFiles
|
For Each oFile In oInvoiceFiles
|
||||||
@@ -74,7 +75,7 @@ Namespace ZUGFeRD
|
|||||||
Next
|
Next
|
||||||
|
|
||||||
' Move non-PDF/A Email Attachments/Files
|
' Move non-PDF/A Email Attachments/Files
|
||||||
For Each oFile In AttachmentFiles
|
For Each oFile In pAttachmentFiles
|
||||||
Try
|
Try
|
||||||
Dim oFilePath = _filesystem.GetVersionedFilename(Path.Combine(oAttachmentDirectory, oFile.Name))
|
Dim oFilePath = _filesystem.GetVersionedFilename(Path.Combine(oAttachmentDirectory, oFile.Name))
|
||||||
|
|
||||||
@@ -87,9 +88,9 @@ Namespace ZUGFeRD
|
|||||||
Next
|
Next
|
||||||
|
|
||||||
' Write Embedded Files to disk
|
' Write Embedded Files to disk
|
||||||
For Each oResult In EmbeddedAttachments
|
For Each oResult In pEmbeddedAttachments
|
||||||
Try
|
Try
|
||||||
Dim oFileName As String = $"{MessageId}~{oResult.FileName}"
|
Dim oFileName As String = $"{pMessageId}~{oResult.FileName}"
|
||||||
Dim oFilePath As String = Path.Combine(oAttachmentDirectory, oFileName)
|
Dim oFilePath As String = Path.Combine(oAttachmentDirectory, oFileName)
|
||||||
|
|
||||||
If Not File.Exists(oAttachmentDirectory) Then
|
If Not File.Exists(oAttachmentDirectory) Then
|
||||||
@@ -108,6 +109,72 @@ Namespace ZUGFeRD
|
|||||||
|
|
||||||
_logger.Info("Finished moving files")
|
_logger.Info("Finished moving files")
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
|
Public Function MoveAndRenameEmailToRejected(pArgs As WorkerArgs, pMessageId As String) As EmailData
|
||||||
|
_logger.Info("Moving Mail with MessageId [{0}] to Rejected folder", pMessageId)
|
||||||
|
_logger.Debug("Fetching Email Data")
|
||||||
|
|
||||||
|
Dim oEmailData = _email.GetEmailDataForMessageId(pMessageId)
|
||||||
|
_logger.Debug("Email Data fetched!")
|
||||||
|
|
||||||
|
Dim oSource = _email.GetOriginalEmailPath(pArgs.OriginalEmailDirectory, pMessageId)
|
||||||
|
_logger.Debug("Original email path: [{0}]", oSource)
|
||||||
|
|
||||||
|
Dim oDateSubDirectoryName As String = Now.ToString("yyyy-MM-dd")
|
||||||
|
Dim oDestination As String
|
||||||
|
|
||||||
|
Dim oRejectedDirectory As String = Path.Combine(pArgs.RejectedEmailDirectory, oDateSubDirectoryName)
|
||||||
|
|
||||||
|
' Create the destination directory if it does not exist
|
||||||
|
_logger.Debug("Creating destination directory [{0}]", oRejectedDirectory)
|
||||||
|
If Not Directory.Exists(oRejectedDirectory) Then
|
||||||
|
Try
|
||||||
|
Directory.CreateDirectory(oRejectedDirectory)
|
||||||
|
Catch ex As Exception
|
||||||
|
_logger.Error(ex)
|
||||||
|
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
|
||||||
|
oDestination = _email.GetEmailPathWithSubjectAsName(oRejectedDirectory, pMessageId)
|
||||||
|
Else
|
||||||
|
oDestination = _email.GetEmailPathWithSubjectAsName(oRejectedDirectory, StringEx.ConvertTextToSlug(oEmailData.Subject))
|
||||||
|
End If
|
||||||
|
|
||||||
|
_logger.Debug("Destination for eml file is {0}", oDestination)
|
||||||
|
|
||||||
|
Dim oFinalFileName = _filesystem.GetVersionedFilename(oDestination)
|
||||||
|
|
||||||
|
_logger.Debug("Versioned filename for eml file is {0}", oFinalFileName)
|
||||||
|
|
||||||
|
If oEmailData Is Nothing Then
|
||||||
|
_logger.Warn("Could not get Email Data from Database. File {0} will not be moved!", oSource)
|
||||||
|
Return New EmailData()
|
||||||
|
End If
|
||||||
|
|
||||||
|
'---------------------------
|
||||||
|
|
||||||
|
Try
|
||||||
|
_logger.Info("Moving email from {0} to {1}", oSource, oFinalFileName)
|
||||||
|
File.Move(oSource, oFinalFileName)
|
||||||
|
oEmailData.Attachment = oFinalFileName
|
||||||
|
Catch ex As Exception
|
||||||
|
_logger.Warn("File {0} could not be moved! Original Filename will be used!", oSource)
|
||||||
|
_logger.Error(ex)
|
||||||
|
oEmailData.Attachment = oSource
|
||||||
|
End Try
|
||||||
|
|
||||||
|
_logger.Info("Email [{0}] moved to rejected folder!", pMessageId)
|
||||||
|
|
||||||
|
Return oEmailData
|
||||||
|
End Function
|
||||||
End Class
|
End Class
|
||||||
|
|
||||||
End Namespace
|
End Namespace
|
||||||
|
|||||||
123
Jobs/ZUGFeRD/HashFunctions.vb
Normal file
123
Jobs/ZUGFeRD/HashFunctions.vb
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
Imports DigitalData.Modules.Base
|
||||||
|
Imports DigitalData.Modules.Database
|
||||||
|
Imports DigitalData.Modules.Jobs.Exceptions
|
||||||
|
Imports DigitalData.Modules.Logging
|
||||||
|
Imports System.Data
|
||||||
|
Imports System.IO
|
||||||
|
Imports System.Security.Cryptography
|
||||||
|
|
||||||
|
Public Class HashFunctions
|
||||||
|
Inherits BaseClass
|
||||||
|
|
||||||
|
Private ReadOnly Database As MSSQLServer
|
||||||
|
|
||||||
|
Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer)
|
||||||
|
MyBase.New(pLogConfig)
|
||||||
|
Database = pDatabase
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Generates the MD5 Checksum of a file and checks it against the histroy table TBEDM_ZUGFERD_HISTORY_IN
|
||||||
|
''' </summary>
|
||||||
|
''' <param name="pFilePath">The path of the file to be checked</param>
|
||||||
|
''' <param name="pIgnoreRejectionStatus">Should the check take into account the rejection status of the file?</param>
|
||||||
|
''' <returns>The MD5 Checksum of the file, or an empty string, if the Checksum could not be created</returns>
|
||||||
|
''' <exception cref="MD5HashException">Throws, when the file should be rejected, ie. if it already exists in the table</exception>
|
||||||
|
Public Function GenerateAndCheck_MD5Sum(pFile As FileInfo, pMessageId As String, pIgnoreRejectionStatus As Boolean) As String
|
||||||
|
Dim oMD5CheckSum = CreateMD5(pFile.FullName)
|
||||||
|
|
||||||
|
' Exit if MD5 could not be created
|
||||||
|
If oMD5CheckSum = String.Empty Then
|
||||||
|
Logger.Warn("MD5 Checksum is nothing for file [{0}]!", pFile.FullName)
|
||||||
|
Return oMD5CheckSum
|
||||||
|
End If
|
||||||
|
|
||||||
|
' Check if Checksum exists in History Table
|
||||||
|
'Dim oCheckCommand = $"SELECT * FROM TBEDM_ZUGFERD_HISTORY_IN WHERE GUID = (SELECT MAX(GUID) FROM TBEDM_ZUGFERD_HISTORY_IN WHERE UPPER(MD5HASH) = UPPER('{oMD5CheckSum}'))"
|
||||||
|
'Dim oTable As DataTable = _firebird.GetDatatable(oCheckCommand, Firebird.TransactionMode.NoTransaction)
|
||||||
|
|
||||||
|
' Check if Checksum exists in History Table
|
||||||
|
' TODO: WHAT THE FUCK IS THIS
|
||||||
|
Dim oCheckCommand = $"SELECT * FROM TBEMLP_HISTORY WHERE GUID = (SELECT MAX(GUID) FROM TBEMLP_HISTORY WHERE UPPER(MD5HASH) = UPPER('{oMD5CheckSum}'))"
|
||||||
|
Dim oTable As DataTable = Database.GetDatatable(oCheckCommand, MSSQLServer.TransactionMode.NoTransaction)
|
||||||
|
|
||||||
|
' If History entries could not be fetched, just return the MD5 Checksum
|
||||||
|
If IsNothing(oTable) Then
|
||||||
|
Logger.Warn("Be careful: oExistsDT is nothing for file [{0}]!", pFile.FullName)
|
||||||
|
Return oMD5CheckSum
|
||||||
|
End If
|
||||||
|
|
||||||
|
' If Checksum does not exist in History entries, just return the MD5 Checksum
|
||||||
|
If oTable.Rows.Count = 0 Then
|
||||||
|
Logger.Debug("File [{0}] was not found in History!", pFile.FullName)
|
||||||
|
Return oMD5CheckSum
|
||||||
|
End If
|
||||||
|
|
||||||
|
' ====================================================
|
||||||
|
' Checksum exists in History entries, reject!
|
||||||
|
' ====================================================
|
||||||
|
|
||||||
|
Dim oRejected As Boolean
|
||||||
|
Dim oHistoryId As Integer
|
||||||
|
|
||||||
|
' Try to read Rejected Status and History Id
|
||||||
|
Try
|
||||||
|
Dim oRow As DataRow = oTable.Rows.Item(0)
|
||||||
|
oRejected = oRow.ItemEx("CUST_REJECTED", False)
|
||||||
|
oHistoryId = oRow.Item("GUID")
|
||||||
|
|
||||||
|
Catch ex As Exception
|
||||||
|
Logger.Warn("Error while converting REJECTED: " & ex.Message)
|
||||||
|
oRejected = False
|
||||||
|
|
||||||
|
End Try
|
||||||
|
|
||||||
|
' Try to get the original filename from Attachment table
|
||||||
|
' If this fails, falls back to the new filename (<msgid>~Attm<i>.ext)
|
||||||
|
Dim oSQL = $"SELECT EMAIL_ATTMT FROM TBEMLP_HISTORY_ATTACHMENT WHERE EMAIL_ATTMT_INDEX = '{pFile.Name}'"
|
||||||
|
Dim oEmailAttachment = Database.GetScalarValue(oSQL, MSSQLServer.TransactionMode.NoTransaction)
|
||||||
|
Dim oOriginalName = ObjectEx.NotNull(oEmailAttachment, pFile.Name)
|
||||||
|
|
||||||
|
Logger.Info("File with MessageId [{0}] and Filename [{1}] has already been processed.", pMessageId, oOriginalName)
|
||||||
|
|
||||||
|
' If the file was already rejected, it is allowed to be processed again,
|
||||||
|
' even if the Checksum exists in the history entries (default case)
|
||||||
|
' Which means, if it was not rejected before, it will be rejected in any case!
|
||||||
|
'
|
||||||
|
' This logic can be overwritten by the IgnoreRejectionStatus parameter.
|
||||||
|
' If it is set to true, the file will be rejected if the file exists in the history entries,
|
||||||
|
' regardless of the rejected parameter.
|
||||||
|
|
||||||
|
If oRejected = True And pIgnoreRejectionStatus = True Then
|
||||||
|
Logger.Info("ZuGFeRDFile already has been processed, but Rejection Status will be ignored!")
|
||||||
|
ElseIf oRejected = False Then
|
||||||
|
Logger.Info("ZuGFeRDFile already has been processed. Will be rejected.")
|
||||||
|
Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{oHistoryId}] - Case 1", oOriginalName)
|
||||||
|
Else
|
||||||
|
Logger.Info("ZuGFeRDFile already has been processed. Will be rejected.")
|
||||||
|
Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{oHistoryId}] - Case 2", oOriginalName)
|
||||||
|
End If
|
||||||
|
|
||||||
|
Return oMD5CheckSum
|
||||||
|
End Function
|
||||||
|
|
||||||
|
Private Function CreateMD5(pFilename As String) As String
|
||||||
|
Try
|
||||||
|
Dim oMD5 As New MD5CryptoServiceProvider
|
||||||
|
Dim oHash As Byte()
|
||||||
|
Dim oHashString As String
|
||||||
|
Dim oResult As String = ""
|
||||||
|
|
||||||
|
Using oFileStream As New FileStream(pFilename, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
|
||||||
|
oHash = oMD5.ComputeHash(oFileStream)
|
||||||
|
oHashString = BitConverter.ToString(oHash)
|
||||||
|
End Using
|
||||||
|
|
||||||
|
oResult = oHashString.Replace("-", "")
|
||||||
|
Return oResult
|
||||||
|
Catch ex As Exception
|
||||||
|
Logger.Error(ex)
|
||||||
|
Return ""
|
||||||
|
End Try
|
||||||
|
End Function
|
||||||
|
End Class
|
||||||
@@ -37,6 +37,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
Private ReadOnly _email As ZUGFeRD.EmailFunctions
|
Private ReadOnly _email As ZUGFeRD.EmailFunctions
|
||||||
Private ReadOnly _file As ZUGFeRD.FileFunctions
|
Private ReadOnly _file As ZUGFeRD.FileFunctions
|
||||||
Private ReadOnly _history As ZUGFeRD.HistoryFunctions
|
Private ReadOnly _history As ZUGFeRD.HistoryFunctions
|
||||||
|
Private ReadOnly _hash As HashFunctions
|
||||||
Private ReadOnly _embeds As PDFEmbeds
|
Private ReadOnly _embeds As PDFEmbeds
|
||||||
|
|
||||||
Private ReadOnly _gdpictureLicenseKey As String
|
Private ReadOnly _gdpictureLicenseKey As String
|
||||||
@@ -45,9 +46,13 @@ Public Class ImportZUGFeRDFiles
|
|||||||
Private _EmailOutAccountId As Integer
|
Private _EmailOutAccountId As Integer
|
||||||
|
|
||||||
Private Class ProcessFileResult
|
Private Class ProcessFileResult
|
||||||
Public ZugferdFileCount As Integer
|
Public ZugferdFileFound As Boolean = False
|
||||||
Public MD5Hash As String = Nothing
|
|
||||||
|
|
||||||
|
Public ZugferdFileCount As Integer = 0
|
||||||
|
Public MD5Checksum As String = Nothing
|
||||||
|
|
||||||
|
Public EmailAttachmentFiles As New List(Of FileInfo)
|
||||||
|
Public EmbeddedAttachmentFiles As New List(Of PDFEmbeds.EmbeddedFile)
|
||||||
End Class
|
End Class
|
||||||
|
|
||||||
Private Class DatabaseConnections
|
Private Class DatabaseConnections
|
||||||
@@ -64,6 +69,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
_file = New ZUGFeRD.FileFunctions(LogConfig, _mssql)
|
_file = New ZUGFeRD.FileFunctions(LogConfig, _mssql)
|
||||||
_history = New ZUGFeRD.HistoryFunctions(LogConfig, _mssql)
|
_history = New ZUGFeRD.HistoryFunctions(LogConfig, _mssql)
|
||||||
_embeds = New PDFEmbeds(LogConfig)
|
_embeds = New PDFEmbeds(LogConfig)
|
||||||
|
_hash = New HashFunctions(_logConfig, _mssql)
|
||||||
|
|
||||||
_logger.Debug("Registering GDPicture License")
|
_logger.Debug("Registering GDPicture License")
|
||||||
If _mssql IsNot Nothing Then
|
If _mssql IsNot Nothing Then
|
||||||
@@ -152,147 +158,36 @@ Public Class ImportZUGFeRDFiles
|
|||||||
Dim oEmbeddedAttachmentFiles As New List(Of PDFEmbeds.EmbeddedFile)
|
Dim oEmbeddedAttachmentFiles As New List(Of PDFEmbeds.EmbeddedFile)
|
||||||
|
|
||||||
Dim oMessageId As String = oFileGroup.Key
|
Dim oMessageId As String = oFileGroup.Key
|
||||||
Dim oMissingProperties As New List(Of String)
|
|
||||||
Dim oMD5CheckSum As String = String.Empty
|
Dim oMD5CheckSum As String = String.Empty
|
||||||
|
|
||||||
_logger.Info("Start processing file group {0}", oMessageId)
|
_logger.Info("Start processing file group {0}", oMessageId)
|
||||||
|
|
||||||
' TODO: Use this refactored function
|
Dim oEmailDataBase = _email.GetEmailDataForMessageId(oMessageId)
|
||||||
' ProcessFileGroup(oMessageId, oFileGroupFiles, oConnections, oArgs)
|
|
||||||
|
|
||||||
Try
|
Try
|
||||||
For Each oFile In oFileGroupFiles
|
For Each oFile In oFileGroupFiles
|
||||||
' 09.12.2021: oDocument is now an Object, because have different classes corresponding to the
|
Dim oResult As ProcessFileResult = ProcessFile(oMessageId, oEmailDataBase, oZUGFeRDCount, oFile, oConnections, oArgs)
|
||||||
' different versions of ZUGFeRD and the type is unknown at compile-time.
|
|
||||||
' 17.11.2022: oDocument is now a Tuple of (String, Object), to be able to return the filename
|
|
||||||
' of the extracted xml file.
|
|
||||||
' 21.12.2022: oDocument is now an object of type ZugferdResult to be able to save
|
|
||||||
' the new meta data, ie. the type of schema (zugferd version)
|
|
||||||
Dim oDocument As ZUGFeRDInterface.ZugferdResult
|
|
||||||
|
|
||||||
' Start a global group counter for each file
|
If oResult.ZugferdFileFound = False Then
|
||||||
Dim oGlobalGroupCounter = 0
|
_logger.Debug("Zugferd File found")
|
||||||
' Clear missing properties for the new file
|
oMD5CheckSum = oResult.MD5Checksum
|
||||||
oMissingProperties = New List(Of String)
|
oZUGFeRDCount = oResult.ZugferdFileCount
|
||||||
|
oEmailAttachmentFiles.AddRange(oResult.EmailAttachmentFiles)
|
||||||
' Only pdf files are allowed from here on
|
oEmbeddedAttachmentFiles.AddRange(oResult.EmbeddedAttachmentFiles)
|
||||||
If Not oFile.Name.ToUpper.EndsWith(".PDF") Then
|
|
||||||
_logger.Debug("Skipping non-pdf file {0}", oFile.Name)
|
|
||||||
oEmailAttachmentFiles.Add(oFile)
|
|
||||||
|
|
||||||
' Checking filesize for attachment files
|
|
||||||
If _filesystem.TestFileSizeIsLessThanMaxFileSize(oFile.FullName, oArgs.MaxAttachmentSizeInMegaBytes) = False Then
|
|
||||||
_logger.Warn("Filesize for File [{0}] exceeded limit of {1} MB", oFile.Name, oArgs.MaxAttachmentSizeInMegaBytes)
|
|
||||||
Throw New FileSizeLimitReachedException(oFile.Name, oArgs.MaxAttachmentSizeInMegaBytes)
|
|
||||||
End If
|
|
||||||
|
|
||||||
Continue For
|
|
||||||
End If
|
End If
|
||||||
|
|
||||||
_logger.Info("Start processing file {0}", oFile.Name)
|
|
||||||
|
|
||||||
' Checking filesize for pdf files
|
|
||||||
If _filesystem.TestFileSizeIsLessThanMaxFileSize(oFile.FullName, oArgs.MaxAttachmentSizeInMegaBytes) = False Then
|
|
||||||
_logger.Warn("Filesize for File [{0}] exceeded limit of {1} MB", oFile.Name, oArgs.MaxAttachmentSizeInMegaBytes)
|
|
||||||
Throw New FileSizeLimitReachedException(oFile.Name, oArgs.MaxAttachmentSizeInMegaBytes)
|
|
||||||
End If
|
|
||||||
|
|
||||||
Try
|
|
||||||
oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oFile.FullName)
|
|
||||||
|
|
||||||
Catch ex As ValidationException
|
|
||||||
Throw ex
|
|
||||||
|
|
||||||
Catch ex As ZUGFeRDExecption
|
|
||||||
Select Case ex.ErrorType
|
|
||||||
Case ZUGFeRDInterface.ErrorType.NoZugferd
|
|
||||||
_logger.Info("File [{0}] is not a valid ZUGFeRD document. Skipping.", oFile.Name)
|
|
||||||
oEmailAttachmentFiles.Add(oFile)
|
|
||||||
Continue For
|
|
||||||
|
|
||||||
Case ZUGFeRDInterface.ErrorType.UnsupportedFormat
|
|
||||||
_logger.Info("File [{0}/{1}] is an unsupported ZUFeRD document format!", oFile.Name, ex.XmlFile)
|
|
||||||
Throw New UnsupportedFerdException(ex.XmlFile)
|
|
||||||
|
|
||||||
Case ZUGFeRDInterface.ErrorType.NoValidZugferd
|
|
||||||
_logger.Info("File [{0}] is an Incorrectly formatted ZUGFeRD document!", oFile.Name)
|
|
||||||
Throw New InvalidFerdException()
|
|
||||||
|
|
||||||
Case Else
|
|
||||||
_logger.Warn("Unexpected Error occurred while extracting ZUGFeRD Information from file {0}", oFile.Name)
|
|
||||||
Throw ex
|
|
||||||
End Select
|
|
||||||
End Try
|
|
||||||
|
|
||||||
' Check if there are more than one ZUGFeRD files
|
|
||||||
If oZUGFeRDCount = 1 Then
|
|
||||||
Throw New TooMuchFerdsException()
|
|
||||||
End If
|
|
||||||
|
|
||||||
' Since extraction went well, increase the amount of ZUGFeRD files
|
|
||||||
oZUGFeRDCount += 1
|
|
||||||
|
|
||||||
' Extract all attachments with the extensions specified in `AllowedExtensions`.
|
|
||||||
' If you need to extract and use embedded xml files, you need to filter out the zugferd-invoice.xml yourself.
|
|
||||||
' Right now the zugferd-invoice.xml is filtered out because `AllowedExtensions` does not contain `xml`.
|
|
||||||
Dim oAttachments = oAttachmentExtractor.Extract(oFile.FullName, AllowedExtensions)
|
|
||||||
If oAttachments Is Nothing Then
|
|
||||||
_logger.Warn("Attachments for file [{0}] could not be extracted", oFile.FullName)
|
|
||||||
Else
|
|
||||||
oEmbeddedAttachmentFiles.AddRange(oAttachments)
|
|
||||||
End If
|
|
||||||
|
|
||||||
' Check the Checksum and rejection status
|
|
||||||
oMD5CheckSum = GenerateAndCheck_MD5Sum(oFile, oMessageId, oArgs.IgnoreRejectionStatus)
|
|
||||||
|
|
||||||
' Check the document against the configured property map and return:
|
|
||||||
' - a List of valid properties
|
|
||||||
' - a List of missing properties
|
|
||||||
|
|
||||||
Dim oPropertyMap = _zugferd.FilterPropertyMap(oArgs.PropertyMap, oDocument.Specification)
|
|
||||||
Dim oCheckResult = _zugferd.PropertyValues.CheckPropertyValues(oDocument.SchemaObject, oPropertyMap, oMessageId)
|
|
||||||
|
|
||||||
_logger.Info("Properties checked: [{0}] missing properties / [{1}] valid properties found.", oCheckResult.MissingProperties.Count, oCheckResult.ValidProperties.Count)
|
|
||||||
|
|
||||||
If oCheckResult.MissingProperties.Count > 0 Then
|
|
||||||
_logger.Warn("[{0}] missing properties found. Exiting.", oCheckResult.MissingProperties.Count)
|
|
||||||
oMissingProperties = oCheckResult.MissingProperties
|
|
||||||
Throw New MissingValueException(oFile)
|
|
||||||
Else
|
|
||||||
_logger.Debug("No missing properties found. Continuing.")
|
|
||||||
|
|
||||||
End If
|
|
||||||
|
|
||||||
DeleteExistingPropertyValues(oMessageId, oConnections)
|
|
||||||
|
|
||||||
Dim oFirstProperty = oCheckResult.ValidProperties.FirstOrDefault()
|
|
||||||
If oFirstProperty IsNot Nothing Then
|
|
||||||
InsertPropertyValue(oMessageId, oConnections, New PropertyValues.ValidProperty() With {
|
|
||||||
.MessageId = oMessageId,
|
|
||||||
.Description = "ZUGFeRDSpezifikation",
|
|
||||||
.GroupCounter = 0,
|
|
||||||
.IsRequired = False,
|
|
||||||
.Value = oDocument.Specification,
|
|
||||||
.TableName = oFirstProperty.TableName,
|
|
||||||
.TableColumn = "ZUGFERD_SPECIFICATION"
|
|
||||||
})
|
|
||||||
End If
|
|
||||||
|
|
||||||
For Each oProperty In oCheckResult.ValidProperties
|
|
||||||
InsertPropertyValue(oMessageId, oConnections, oProperty)
|
|
||||||
Next
|
|
||||||
Next
|
Next
|
||||||
|
|
||||||
'Check if there are no ZUGFeRD files
|
'Check if there are no ZUGFeRD files
|
||||||
If oZUGFeRDCount = 0 Then
|
If oZUGFeRDCount = 0 Then
|
||||||
' If NonZugferdDirectory is not set, a NoFerdsException will be thrown and a rejection will be generated
|
' If NonZugferdDirectory is not set, a NoFerdsException will be thrown and a rejection will be generated
|
||||||
' This is the default/initial behaviour.
|
' This is the default/initial behaviour.
|
||||||
If oArgs.NonZugferdDirectory Is Nothing OrElse oArgs.NonZugferdDirectory = String.Empty Then
|
If String.IsNullOrEmpty(oArgs.NonZugferdDirectory) Then
|
||||||
Throw New NoFerdsException()
|
Throw New NoFerdsException()
|
||||||
End If
|
End If
|
||||||
|
|
||||||
' Also, if the directory is set but does not exist, still a rejection will be generated.
|
' Also, if the directory is set but does not exist, still a rejection will be generated.
|
||||||
If Not IO.Directory.Exists(oArgs.NonZugferdDirectory) Then
|
If Not Directory.Exists(oArgs.NonZugferdDirectory) Then
|
||||||
Throw New NoFerdsException()
|
Throw New NoFerdsException()
|
||||||
End If
|
End If
|
||||||
|
|
||||||
@@ -302,12 +197,11 @@ Public Class ImportZUGFeRDFiles
|
|||||||
|
|
||||||
End If
|
End If
|
||||||
|
|
||||||
'If no errors occurred...
|
'If no errors occurred, update the history table for this MessageId
|
||||||
'Log the History
|
If String.IsNullOrEmpty(oMD5CheckSum) Then
|
||||||
If oMD5CheckSum <> String.Empty Then
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "SUCCESS")
|
|
||||||
Else
|
|
||||||
_history.Update_HistoryEntry(oMessageId, String.Empty, "SUCCESS (with empty MD5Hash)")
|
_history.Update_HistoryEntry(oMessageId, String.Empty, "SUCCESS (with empty MD5Hash)")
|
||||||
|
Else
|
||||||
|
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "SUCCESS")
|
||||||
End If
|
End If
|
||||||
|
|
||||||
oIsSuccess = True
|
oIsSuccess = True
|
||||||
@@ -326,7 +220,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
Next
|
Next
|
||||||
|
|
||||||
Dim oBody = String.Format(EmailStrings.EMAIL_VALIDATION_ERROR, oErrorList)
|
Dim oBody = String.Format(EmailStrings.EMAIL_VALIDATION_ERROR, oErrorList)
|
||||||
Dim oEmailData = 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)
|
||||||
AddRejectedState(oMessageId, "ValidationException", "Die Rechnungsvalidierung ist fehlgeschlagen!", "", oSQLTransaction)
|
AddRejectedState(oMessageId, "ValidationException", "Die Rechnungsvalidierung ist fehlgeschlagen!", "", oSQLTransaction)
|
||||||
|
|
||||||
@@ -339,7 +233,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
_history.Update_HistoryEntry(oMessageId, String.Empty, oMessage)
|
_history.Update_HistoryEntry(oMessageId, String.Empty, oMessage)
|
||||||
|
|
||||||
Dim oBody = String.Format(EmailStrings.EMAIL_MD5_ERROR, ex.FileName)
|
Dim oBody = String.Format(EmailStrings.EMAIL_MD5_ERROR, ex.FileName)
|
||||||
Dim oEmailData = 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)
|
||||||
AddRejectedState(oMessageId, "MD5HashException", "Die gesendete Rechnung wurde bereits verarbeitet!", "", oSQLTransaction)
|
AddRejectedState(oMessageId, "MD5HashException", "Die gesendete Rechnung wurde bereits verarbeitet!", "", oSQLTransaction)
|
||||||
|
|
||||||
@@ -350,7 +244,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
' That 's why we set it to String.Empty here.
|
' That 's why we set it to String.Empty here.
|
||||||
_history.Update_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but unsupported format")
|
_history.Update_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but unsupported format")
|
||||||
|
|
||||||
Dim oEmailData = 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)
|
||||||
@@ -363,7 +257,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
' That 's why we set it to String.Empty here.
|
' That 's why we set it to String.Empty here.
|
||||||
_history.Update_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but incorrect format")
|
_history.Update_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but incorrect format")
|
||||||
|
|
||||||
Dim oEmailData = 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)
|
||||||
@@ -374,7 +268,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - More than one ZUGFeRD-document in email")
|
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - More than one ZUGFeRD-document in email")
|
||||||
|
|
||||||
Dim oEmailData = 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)
|
||||||
@@ -385,7 +279,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - no ZUGFeRD-Document in email")
|
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - no ZUGFeRD-Document in email")
|
||||||
|
|
||||||
Dim oEmailData = 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)
|
||||||
@@ -395,14 +289,14 @@ Public Class ImportZUGFeRDFiles
|
|||||||
_logger.Error(ex)
|
_logger.Error(ex)
|
||||||
|
|
||||||
Dim oMessage As String = ""
|
Dim oMessage As String = ""
|
||||||
For Each prop In oMissingProperties
|
For Each prop In ex.MissingProperties
|
||||||
oMessage &= $"- {prop}"
|
oMessage &= $"- {prop}"
|
||||||
Next
|
Next
|
||||||
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, $"REJECTED - Missing Required Properties: [{oMessage}]")
|
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, $"REJECTED - Missing Required Properties: [{oMessage}]")
|
||||||
|
|
||||||
Dim oBody = _email.CreateBodyForMissingProperties(ex.File.Name, oMissingProperties)
|
Dim oBody = _email.CreateBodyForMissingProperties(ex.File.Name, ex.MissingProperties)
|
||||||
Dim oEmailData = 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)
|
||||||
AddRejectedState(oMessageId, "MissingValueException", "Es fehlten ZugferdSpezifikationen", oMessage, oSQLTransaction)
|
AddRejectedState(oMessageId, "MissingValueException", "Es fehlten ZugferdSpezifikationen", oMessage, oSQLTransaction)
|
||||||
|
|
||||||
@@ -411,7 +305,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - File size limit reached")
|
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - File size limit reached")
|
||||||
|
|
||||||
Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId)
|
Dim oEmailData = _file.MoveAndRenameEmailToRejected(oArgs, oMessageId)
|
||||||
|
|
||||||
Dim oKey = FileSizeLimitReachedException.KEY_FILENAME
|
Dim oKey = FileSizeLimitReachedException.KEY_FILENAME
|
||||||
Dim oFileExceedingThreshold As String = IIf(ex.Data.Contains(oKey), ex.Data.Item(oKey), "")
|
Dim oFileExceedingThreshold As String = IIf(ex.Data.Contains(oKey), ex.Data.Item(oKey), "")
|
||||||
@@ -522,405 +416,15 @@ Public Class ImportZUGFeRDFiles
|
|||||||
End Try
|
End Try
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
#Region "=== REFACTOR"
|
Private Function ProcessFile(pMessageId As String, pEmailData As EmailData, pZugferdFiles As Integer, oFile As FileInfo, oConnections As DatabaseConnections, pArgs As WorkerArgs) As ProcessFileResult
|
||||||
Private Function ProcessFileGroup(oMessageId As String, pFiles As List(Of FileInfo), oConnections As DatabaseConnections, pArgs As WorkerArgs)
|
|
||||||
Dim oMissingProperties = New List(Of String)
|
|
||||||
Dim oEmailAttachmentFiles = New List(Of FileInfo)
|
|
||||||
Dim oEmbeddedAttachmentFiles = New List(Of PDFEmbeds.EmbeddedFile)
|
|
||||||
Dim oZUGFeRDCount = 0
|
|
||||||
Dim oMD5CheckSum As String = Nothing
|
|
||||||
|
|
||||||
' Set the default Move Directory
|
|
||||||
Dim oMoveDirectory As String = pArgs.ErrorDirectory
|
|
||||||
|
|
||||||
' Flag to save if the whole process was a success.
|
|
||||||
' Will be set only at the end of the function if no error occurred.
|
|
||||||
Dim oIsSuccess As Boolean = False
|
|
||||||
|
|
||||||
' Flag to save if the occurred error (if any) was expected
|
|
||||||
' Used to determine if transactions should be committed or not
|
|
||||||
Dim oExpectedError As Boolean = True
|
|
||||||
|
|
||||||
Try
|
|
||||||
For Each oFile In pFiles
|
|
||||||
' 09.12.2021: oDocument is now an Object, because have different classes corresponding to the
|
|
||||||
' different versions of ZUGFeRD and the type is unknown at compile-time.
|
|
||||||
' 17.11.2022: oDocument is now a Tuple of (String, Object), to be able to return the filename
|
|
||||||
' of the extracted xml file.
|
|
||||||
' 21.12.2022: oDocument is now an object of type ZugferdResult to be able to save
|
|
||||||
' the new meta data, ie. the type of schema (zugferd version)
|
|
||||||
Dim oDocument As ZUGFeRDInterface.ZugferdResult
|
|
||||||
|
|
||||||
' Start a global group counter for each file
|
|
||||||
Dim oGlobalGroupCounter = 0
|
|
||||||
' Clear missing properties for the new file
|
|
||||||
oMissingProperties = New List(Of String)
|
|
||||||
|
|
||||||
' Only pdf files are allowed from here on
|
|
||||||
If Not oFile.Name.ToUpper.EndsWith(".PDF") Then
|
|
||||||
_logger.Debug("Skipping non-pdf file {0}", oFile.Name)
|
|
||||||
oEmailAttachmentFiles.Add(oFile)
|
|
||||||
|
|
||||||
' Checking filesize for attachment files
|
|
||||||
If _filesystem.TestFileSizeIsLessThanMaxFileSize(oFile.FullName, pArgs.MaxAttachmentSizeInMegaBytes) = False Then
|
|
||||||
_logger.Warn("Filesize for File [{0}] exceeded limit of {1} MB", oFile.Name, pArgs.MaxAttachmentSizeInMegaBytes)
|
|
||||||
Throw New FileSizeLimitReachedException(oFile.Name, pArgs.MaxAttachmentSizeInMegaBytes)
|
|
||||||
End If
|
|
||||||
|
|
||||||
Continue For
|
|
||||||
End If
|
|
||||||
|
|
||||||
_logger.Info("Start processing file {0}", oFile.Name)
|
|
||||||
|
|
||||||
' Checking filesize for pdf files
|
|
||||||
If _filesystem.TestFileSizeIsLessThanMaxFileSize(oFile.FullName, pArgs.MaxAttachmentSizeInMegaBytes) = False Then
|
|
||||||
_logger.Warn("Filesize for File [{0}] exceeded limit of {1} MB", oFile.Name, pArgs.MaxAttachmentSizeInMegaBytes)
|
|
||||||
Throw New FileSizeLimitReachedException(oFile.Name, pArgs.MaxAttachmentSizeInMegaBytes)
|
|
||||||
End If
|
|
||||||
|
|
||||||
Try
|
|
||||||
oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oFile.FullName)
|
|
||||||
|
|
||||||
Catch ex As ValidationException
|
|
||||||
Throw ex
|
|
||||||
|
|
||||||
Catch ex As ZUGFeRDExecption
|
|
||||||
Select Case ex.ErrorType
|
|
||||||
Case ZUGFeRDInterface.ErrorType.NoZugferd
|
|
||||||
_logger.Info("File [{0}] is not a valid ZUGFeRD document. Skipping.", oFile.Name)
|
|
||||||
oEmailAttachmentFiles.Add(oFile)
|
|
||||||
Continue For
|
|
||||||
|
|
||||||
Case ZUGFeRDInterface.ErrorType.UnsupportedFormat
|
|
||||||
_logger.Info("File [{0}/{1}] is an unsupported ZUFeRD document format!", oFile.Name, ex.XmlFile)
|
|
||||||
Throw New UnsupportedFerdException(ex.XmlFile)
|
|
||||||
|
|
||||||
Case ZUGFeRDInterface.ErrorType.NoValidZugferd
|
|
||||||
_logger.Info("File [{0}] is an Incorrectly formatted ZUGFeRD document!", oFile.Name)
|
|
||||||
Throw New InvalidFerdException()
|
|
||||||
|
|
||||||
Case Else
|
|
||||||
_logger.Warn("Unexpected Error occurred while extracting ZUGFeRD Information from file {0}", oFile.Name)
|
|
||||||
Throw ex
|
|
||||||
End Select
|
|
||||||
End Try
|
|
||||||
|
|
||||||
' Check if there are more than one ZUGFeRD files
|
|
||||||
If oZUGFeRDCount = 1 Then
|
|
||||||
Throw New TooMuchFerdsException()
|
|
||||||
End If
|
|
||||||
|
|
||||||
' Since extraction went well, increase the amount of ZUGFeRD files
|
|
||||||
oZUGFeRDCount += 1
|
|
||||||
|
|
||||||
' Extract all attachments with the extensions specified in `AllowedExtensions`.
|
|
||||||
' If you need to extract and use embedded xml files, you need to filter out the zugferd-invoice.xml yourself.
|
|
||||||
' Right now the zugferd-invoice.xml is filtered out because `AllowedExtensions` does not contain `xml`.
|
|
||||||
Dim oAttachments = _embeds.Extract(oFile.FullName, AllowedExtensions)
|
|
||||||
If oAttachments Is Nothing Then
|
|
||||||
_logger.Warn("Attachments for file [{0}] could not be extracted", oFile.FullName)
|
|
||||||
Else
|
|
||||||
oEmbeddedAttachmentFiles.AddRange(oAttachments)
|
|
||||||
End If
|
|
||||||
|
|
||||||
' Check the Checksum and rejection status
|
|
||||||
oMD5CheckSum = GenerateAndCheck_MD5Sum(oFile, oMessageId, pArgs.IgnoreRejectionStatus)
|
|
||||||
|
|
||||||
' Check the document against the configured property map and return:
|
|
||||||
' - a List of valid properties
|
|
||||||
' - a List of missing properties
|
|
||||||
|
|
||||||
Dim oPropertyMap = _zugferd.FilterPropertyMap(pArgs.PropertyMap, oDocument.Specification)
|
|
||||||
Dim oCheckResult = _zugferd.PropertyValues.CheckPropertyValues(oDocument.SchemaObject, oPropertyMap, oMessageId)
|
|
||||||
|
|
||||||
_logger.Info("Properties checked: [{0}] missing properties / [{1}] valid properties found.", oCheckResult.MissingProperties.Count, oCheckResult.ValidProperties.Count)
|
|
||||||
|
|
||||||
If oCheckResult.MissingProperties.Count > 0 Then
|
|
||||||
_logger.Warn("[{0}] missing properties found. Exiting.", oCheckResult.MissingProperties.Count)
|
|
||||||
oMissingProperties = oCheckResult.MissingProperties
|
|
||||||
Throw New MissingValueException(oFile)
|
|
||||||
Else
|
|
||||||
_logger.Debug("No missing properties found. Continuing.")
|
|
||||||
|
|
||||||
End If
|
|
||||||
|
|
||||||
DeleteExistingPropertyValues(oMessageId, oConnections)
|
|
||||||
|
|
||||||
Dim oFirstProperty = oCheckResult.ValidProperties.FirstOrDefault()
|
|
||||||
If oFirstProperty IsNot Nothing Then
|
|
||||||
InsertPropertyValue(oMessageId, oConnections, New PropertyValues.ValidProperty() With {
|
|
||||||
.MessageId = oMessageId,
|
|
||||||
.Description = "ZUGFeRDSpezifikation",
|
|
||||||
.GroupCounter = 0,
|
|
||||||
.IsRequired = False,
|
|
||||||
.Value = oDocument.Specification,
|
|
||||||
.TableName = oFirstProperty.TableName,
|
|
||||||
.TableColumn = "ZUGFERD_SPECIFICATION"
|
|
||||||
})
|
|
||||||
End If
|
|
||||||
|
|
||||||
For Each oProperty In oCheckResult.ValidProperties
|
|
||||||
InsertPropertyValue(oMessageId, oConnections, oProperty)
|
|
||||||
Next
|
|
||||||
Next
|
|
||||||
|
|
||||||
'Check if there are no ZUGFeRD files
|
|
||||||
If oZUGFeRDCount = 0 Then
|
|
||||||
|
|
||||||
' If NonZugferdDirectory is not set, a NoFerdsException will be thrown and a rejection will be generated
|
|
||||||
' This is the default/initial behaviour.
|
|
||||||
If pArgs.NonZugferdDirectory Is Nothing OrElse pArgs.NonZugferdDirectory = String.Empty Then
|
|
||||||
Throw New NoFerdsException()
|
|
||||||
End If
|
|
||||||
|
|
||||||
' Also, if the directory is set but does not exist, still a rejection will be generated.
|
|
||||||
If Not IO.Directory.Exists(pArgs.NonZugferdDirectory) Then
|
|
||||||
Throw New NoFerdsException()
|
|
||||||
End If
|
|
||||||
|
|
||||||
' Only if the directory is set and does exist, it will be used and any file groups which
|
|
||||||
' do NOT CONTAIN ANY ZUGFERD DOCUMENTS, are moved to that directory.
|
|
||||||
Throw New NoFerdsAlternateException()
|
|
||||||
|
|
||||||
End If
|
|
||||||
|
|
||||||
'If no errors occurred...
|
|
||||||
'Log the History
|
|
||||||
If oMD5CheckSum <> String.Empty Then
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "SUCCESS")
|
|
||||||
Else
|
|
||||||
_history.Update_HistoryEntry(oMessageId, String.Empty, "SUCCESS (with empty MD5Hash)")
|
|
||||||
End If
|
|
||||||
|
|
||||||
oIsSuccess = True
|
|
||||||
oMoveDirectory = pArgs.SuccessDirectory
|
|
||||||
|
|
||||||
Catch ex As ValidationException
|
|
||||||
_logger.Error(ex)
|
|
||||||
|
|
||||||
Dim oErrors = ex.ValidationErrors
|
|
||||||
Dim oMessage = "REJECTED - ZUGFeRD yes but formal validation failed!"
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, oMessage)
|
|
||||||
|
|
||||||
Dim oErrorList As String = ""
|
|
||||||
For Each oError In oErrors
|
|
||||||
oErrorList += $"<li>Element '{oError.ElementName}' mit Wert '{oError.ElementValue}': {oError.ErrorMessage}</li>"
|
|
||||||
Next
|
|
||||||
|
|
||||||
Dim oBody = String.Format(EmailStrings.EMAIL_VALIDATION_ERROR, oErrorList)
|
|
||||||
Dim oEmailData = MoveAndRenameEmailToRejected(pArgs, oMessageId)
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "ValidationException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
AddRejectedState(oMessageId, "ValidationException", "Die Rechnungsvalidierung ist fehlgeschlagen!", "", oConnections.SQLServerTransaction)
|
|
||||||
|
|
||||||
Catch ex As MD5HashException
|
|
||||||
_logger.Error(ex)
|
|
||||||
|
|
||||||
' When MD5HashException is thrown, we don't have a MD5Hash yet.
|
|
||||||
' That 's why we set it to String.Empty here.
|
|
||||||
Dim oMessage = "REJECTED - Already processed (MD5Hash)"
|
|
||||||
_history.Update_HistoryEntry(oMessageId, String.Empty, oMessage)
|
|
||||||
|
|
||||||
Dim oBody = EmailStrings.EMAIL_MD5_ERROR
|
|
||||||
Dim oEmailData = MoveAndRenameEmailToRejected(pArgs, oMessageId)
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "MD5HashException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
AddRejectedState(oMessageId, "MD5HashException", "Die gesendete Rechnung wurde bereits verarbeitet!", "", oConnections.SQLServerTransaction)
|
|
||||||
|
|
||||||
Catch ex As UnsupportedFerdException
|
|
||||||
_logger.Error(ex)
|
|
||||||
|
|
||||||
' When UnsupportedFerdException is thrown, we don't have a MD5Hash yet.
|
|
||||||
' That 's why we set it to String.Empty here.
|
|
||||||
_history.Update_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but unsupported format")
|
|
||||||
|
|
||||||
Dim oEmailData = MoveAndRenameEmailToRejected(pArgs, oMessageId)
|
|
||||||
Dim oBody As String = String.Format(EmailStrings.EMAIL_UNSUPPORTED_DOCUMENT, oEmailData.Subject, ex.XmlFile)
|
|
||||||
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "UnsupportedFerdException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
AddRejectedState(oMessageId, "UnsupportedFerdException", "Nicht unterstütztes Datenformat", "", oConnections.SQLServerTransaction)
|
|
||||||
|
|
||||||
Catch ex As InvalidFerdException
|
|
||||||
_logger.Error(ex)
|
|
||||||
|
|
||||||
' When InvalidFerdException is thrown, we don't have a MD5Hash yet.
|
|
||||||
' That 's why we set it to String.Empty here.
|
|
||||||
_history.Update_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but incorrect format")
|
|
||||||
|
|
||||||
Dim oEmailData = MoveAndRenameEmailToRejected(pArgs, oMessageId)
|
|
||||||
Dim oBody = String.Format(EmailStrings.EMAIL_INVALID_DOCUMENT, oEmailData.Subject)
|
|
||||||
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "InvalidFerdException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
AddRejectedState(oMessageId, "InvalidFerdException", "Inkorrekte Formate", "", oConnections.SQLServerTransaction)
|
|
||||||
|
|
||||||
Catch ex As TooMuchFerdsException
|
|
||||||
_logger.Error(ex)
|
|
||||||
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - More than one ZUGFeRD-document in email")
|
|
||||||
|
|
||||||
Dim oEmailData = MoveAndRenameEmailToRejected(pArgs, oMessageId)
|
|
||||||
Dim oBody = String.Format(EmailStrings.EMAIL_TOO_MUCH_FERDS, oEmailData.Subject)
|
|
||||||
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "TooMuchFerdsException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
AddRejectedState(oMessageId, "TooMuchFerdsException", "Email enthielt mehr als ein ZUGFeRD-Dokument", "", oConnections.SQLServerTransaction)
|
|
||||||
|
|
||||||
Catch ex As NoFerdsException
|
|
||||||
_logger.Error(ex)
|
|
||||||
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - no ZUGFeRD-Document in email")
|
|
||||||
|
|
||||||
Dim oEmailData = MoveAndRenameEmailToRejected(pArgs, oMessageId)
|
|
||||||
Dim oBody = String.Format(EmailStrings.EMAIL_NO_FERDS, oEmailData.Subject)
|
|
||||||
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "NoFerdsException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
AddRejectedState(oMessageId, "NoFerdsException", " Email enthielt keine ZUGFeRD-Dokumente", "", oConnections.SQLServerTransaction)
|
|
||||||
|
|
||||||
Catch ex As MissingValueException
|
|
||||||
_logger.Error(ex)
|
|
||||||
|
|
||||||
Dim oMessage As String = ""
|
|
||||||
For Each prop In oMissingProperties
|
|
||||||
oMessage &= $"- {prop}"
|
|
||||||
Next
|
|
||||||
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, $"REJECTED - Missing Required Properties: [{oMessage}]")
|
|
||||||
|
|
||||||
Dim oBody = _email.CreateBodyForMissingProperties(ex.File.Name, oMissingProperties)
|
|
||||||
Dim oEmailData = MoveAndRenameEmailToRejected(pArgs, oMessageId)
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "MissingValueException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
AddRejectedState(oMessageId, "MissingValueException", "Es fehlten ZugferdSpezifikationen", oMessage, oConnections.SQLServerTransaction)
|
|
||||||
|
|
||||||
Catch ex As FileSizeLimitReachedException
|
|
||||||
_logger.Error(ex)
|
|
||||||
|
|
||||||
_history.Update_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - File size limit reached")
|
|
||||||
|
|
||||||
Dim oEmailData = MoveAndRenameEmailToRejected(pArgs, oMessageId)
|
|
||||||
|
|
||||||
Dim oKey = FileSizeLimitReachedException.KEY_FILENAME
|
|
||||||
Dim oFileExceedingThreshold As String = IIf(ex.Data.Contains(oKey), ex.Data.Item(oKey), "")
|
|
||||||
Dim oFileWithoutMessageId = oFileExceedingThreshold.
|
|
||||||
Replace(oMessageId, "").
|
|
||||||
Replace("~", "")
|
|
||||||
|
|
||||||
Dim oBody = String.Format(EmailStrings.EMAIL_FILE_SIZE_REACHED, pArgs.MaxAttachmentSizeInMegaBytes, oFileWithoutMessageId)
|
|
||||||
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "FileSizeLimitReachedException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
AddRejectedState(oMessageId, "FileSizeLimitReachedException", "Erlaubte Dateigröße überschritten", "", oConnections.SQLServerTransaction)
|
|
||||||
|
|
||||||
|
|
||||||
Catch ex As NoFerdsAlternateException
|
|
||||||
' TODO: Maybe dont even log this 'error', since it's not really an error and it might happen *A LOT*
|
|
||||||
_logger.Error(ex)
|
|
||||||
oMoveDirectory = pArgs.NonZugferdDirectory
|
|
||||||
|
|
||||||
Catch ex As OutOfMemoryException
|
|
||||||
_logger.Warn("OutOfMemory Error occurred: {0}", ex.Message)
|
|
||||||
_logger.Error(ex)
|
|
||||||
|
|
||||||
' Send Email to Digital Data
|
|
||||||
Dim oBody = _email.CreateBodyForUnhandledException(oMessageId, ex)
|
|
||||||
Dim oEmailData As New EmailData With {
|
|
||||||
.From = pArgs.ExceptionEmailAddress,
|
|
||||||
.Subject = $"OutOfMemoryException im ZUGFeRD-Parser @ {oMessageId}"
|
|
||||||
}
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "OutOfMemoryException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
|
|
||||||
' Rollback Transaction
|
|
||||||
oConnections.SQLServerTransaction.Rollback()
|
|
||||||
|
|
||||||
oMoveDirectory = DIRECTORY_DONT_MOVE
|
|
||||||
|
|
||||||
oExpectedError = False
|
|
||||||
|
|
||||||
Catch ex As Exception
|
|
||||||
_logger.Warn("Unknown Error occurred: {0}", ex.Message)
|
|
||||||
_logger.Error(ex)
|
|
||||||
|
|
||||||
' Send Email to Digital Data
|
|
||||||
Dim oBody = _email.CreateBodyForUnhandledException(oMessageId, ex)
|
|
||||||
Dim oEmailData As New EmailData With {
|
|
||||||
.From = pArgs.ExceptionEmailAddress,
|
|
||||||
.Subject = $"UnhandledException im ZUGFeRD-Parser @ {oMessageId}"
|
|
||||||
}
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "UnhandledException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
|
|
||||||
' Rollback Transaction
|
|
||||||
oConnections.SQLServerTransaction.Rollback()
|
|
||||||
|
|
||||||
oMoveDirectory = DIRECTORY_DONT_MOVE
|
|
||||||
|
|
||||||
oExpectedError = False
|
|
||||||
|
|
||||||
Finally
|
|
||||||
Try
|
|
||||||
' If an application error occurred, dont move files so they will be processed again later
|
|
||||||
If oMoveDirectory = DIRECTORY_DONT_MOVE Then
|
|
||||||
_logger.Info("Application Error occurred. Files for message Id {0} will not be moved.", oMessageId)
|
|
||||||
Else
|
|
||||||
' Move all files of the current group
|
|
||||||
_file.MoveFiles(pArgs, oMessageId, pFiles, oEmailAttachmentFiles, oEmbeddedAttachmentFiles, oMoveDirectory, oIsSuccess)
|
|
||||||
End If
|
|
||||||
_logger.Info("Finished processing file group {0}", oMessageId)
|
|
||||||
Catch ex As Exception
|
|
||||||
' Send Email to Digital Data
|
|
||||||
Dim oBody = _email.CreateBodyForUnhandledException(oMessageId, ex)
|
|
||||||
Dim oEmailData As New EmailData With {
|
|
||||||
.From = pArgs.ExceptionEmailAddress,
|
|
||||||
.Subject = $"FileMoveException im ZUGFeRD-Parser @ {oMessageId}"
|
|
||||||
}
|
|
||||||
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "FileMoveException", _EmailOutAccountId, pArgs.NamePortal)
|
|
||||||
|
|
||||||
_logger.Warn("Could not move files!")
|
|
||||||
_logger.Error(ex)
|
|
||||||
Throw ex
|
|
||||||
End Try
|
|
||||||
|
|
||||||
Try
|
|
||||||
' If everything went OK or an expected error occurred,
|
|
||||||
' finally commit all changes To the Database
|
|
||||||
' ==================================================================
|
|
||||||
If oIsSuccess Or oExpectedError Then
|
|
||||||
' Commit Transaction
|
|
||||||
oConnections.SQLServerTransaction.Commit()
|
|
||||||
End If
|
|
||||||
|
|
||||||
_logger.Info("FileGroup for MessageId [{0}] successfully processed!", oMessageId)
|
|
||||||
Catch ex As Exception
|
|
||||||
_logger.Error(ex)
|
|
||||||
_logger.Warn("Database Transactions were not committed successfully.")
|
|
||||||
End Try
|
|
||||||
|
|
||||||
Try
|
|
||||||
oConnections.SQLServerConnection.Close()
|
|
||||||
Catch ex As Exception
|
|
||||||
_logger.Error(ex)
|
|
||||||
_logger.Warn("Database Connections were not closed successfully.")
|
|
||||||
End Try
|
|
||||||
End Try
|
|
||||||
End Function
|
|
||||||
|
|
||||||
Private Function ProcessFile(pMessageId As String, oFile As FileInfo, oConnections As DatabaseConnections, pArgs As WorkerArgs) As ProcessFileResult
|
|
||||||
' 09.12.2021: oDocument is now an Object, because have different classes corresponding to the
|
|
||||||
' different versions of ZUGFeRD and the type is unknown at compile-time.
|
|
||||||
' 17.11.2022: oDocument is now a Tuple of (String, Object), to be able to return the filename
|
|
||||||
' of the extracted xml file.
|
|
||||||
' 21.12.2022: oDocument is now an object of type ZugferdResult to be able to save
|
|
||||||
' the new meta data, ie. the type of schema (zugferd version)
|
|
||||||
Dim oDocument As ZUGFeRDInterface.ZugferdResult
|
Dim oDocument As ZUGFeRDInterface.ZugferdResult
|
||||||
|
Dim oResult As New ProcessFileResult()
|
||||||
Dim oMissingProperties = New List(Of String)
|
Dim oMissingProperties As New List(Of String)
|
||||||
Dim oEmailAttachmentFiles = New List(Of FileInfo)
|
|
||||||
Dim oEmbeddedAttachmentFiles = New List(Of PDFEmbeds.EmbeddedFile)
|
|
||||||
Dim oZugferdFiles As Integer = 0
|
|
||||||
|
|
||||||
' Start a global group counter for each file
|
|
||||||
Dim oGlobalGroupCounter = 0
|
|
||||||
' Clear missing properties for the new file
|
|
||||||
oMissingProperties = New List(Of String)
|
|
||||||
|
|
||||||
' Only pdf files are allowed from here on
|
' Only pdf files are allowed from here on
|
||||||
If Not oFile.Name.ToUpper.EndsWith(".PDF") Then
|
If Not oFile.Name.ToUpper.EndsWith(".PDF") Then
|
||||||
_logger.Debug("Skipping non-pdf file {0}", oFile.Name)
|
_logger.Debug("Skipping non-pdf file {0}", oFile.Name)
|
||||||
oEmailAttachmentFiles.Add(oFile)
|
oResult.EmailAttachmentFiles.Add(oFile)
|
||||||
|
|
||||||
' Checking filesize for attachment files
|
' Checking filesize for attachment files
|
||||||
If _filesystem.TestFileSizeIsLessThanMaxFileSize(oFile.FullName, pArgs.MaxAttachmentSizeInMegaBytes) = False Then
|
If _filesystem.TestFileSizeIsLessThanMaxFileSize(oFile.FullName, pArgs.MaxAttachmentSizeInMegaBytes) = False Then
|
||||||
@@ -928,8 +432,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
Throw New FileSizeLimitReachedException(oFile.Name, pArgs.MaxAttachmentSizeInMegaBytes)
|
Throw New FileSizeLimitReachedException(oFile.Name, pArgs.MaxAttachmentSizeInMegaBytes)
|
||||||
End If
|
End If
|
||||||
|
|
||||||
'TODO: how to handle?
|
Return oResult
|
||||||
'Continue For
|
|
||||||
End If
|
End If
|
||||||
|
|
||||||
_logger.Info("Start processing file {0}", oFile.Name)
|
_logger.Info("Start processing file {0}", oFile.Name)
|
||||||
@@ -950,10 +453,9 @@ Public Class ImportZUGFeRDFiles
|
|||||||
Select Case ex.ErrorType
|
Select Case ex.ErrorType
|
||||||
Case ZUGFeRDInterface.ErrorType.NoZugferd
|
Case ZUGFeRDInterface.ErrorType.NoZugferd
|
||||||
_logger.Info("File [{0}] is not a valid ZUGFeRD document. Skipping.", oFile.Name)
|
_logger.Info("File [{0}] is not a valid ZUGFeRD document. Skipping.", oFile.Name)
|
||||||
oEmailAttachmentFiles.Add(oFile)
|
|
||||||
|
|
||||||
'TODO: How to handle?
|
oResult.EmailAttachmentFiles.Add(oFile)
|
||||||
'Continue For
|
Return oResult
|
||||||
|
|
||||||
Case ZUGFeRDInterface.ErrorType.UnsupportedFormat
|
Case ZUGFeRDInterface.ErrorType.UnsupportedFormat
|
||||||
_logger.Info("File [{0}/{1}] is an unsupported ZUFeRD document format!", oFile.Name, ex.XmlFile)
|
_logger.Info("File [{0}/{1}] is an unsupported ZUFeRD document format!", oFile.Name, ex.XmlFile)
|
||||||
@@ -970,12 +472,12 @@ Public Class ImportZUGFeRDFiles
|
|||||||
End Try
|
End Try
|
||||||
|
|
||||||
' Check if there are more than one ZUGFeRD files
|
' Check if there are more than one ZUGFeRD files
|
||||||
If oZugferdFiles = 1 Then
|
If pZugferdFiles = 1 Then
|
||||||
Throw New TooMuchFerdsException()
|
Throw New TooMuchFerdsException()
|
||||||
End If
|
End If
|
||||||
|
|
||||||
' Since extraction went well, increase the amount of ZUGFeRD files
|
' Since extraction went well, increase the amount of ZUGFeRD files
|
||||||
oZugferdFiles += 1
|
pZugferdFiles += 1
|
||||||
|
|
||||||
' Extract all attachments with the extensions specified in `AllowedExtensions`.
|
' Extract all attachments with the extensions specified in `AllowedExtensions`.
|
||||||
' If you need to extract and use embedded xml files, you need to filter out the zugferd-invoice.xml yourself.
|
' If you need to extract and use embedded xml files, you need to filter out the zugferd-invoice.xml yourself.
|
||||||
@@ -984,11 +486,11 @@ Public Class ImportZUGFeRDFiles
|
|||||||
If oAttachments Is Nothing Then
|
If oAttachments Is Nothing Then
|
||||||
_logger.Warn("Attachments for file [{0}] could not be extracted", oFile.FullName)
|
_logger.Warn("Attachments for file [{0}] could not be extracted", oFile.FullName)
|
||||||
Else
|
Else
|
||||||
oEmbeddedAttachmentFiles.AddRange(oAttachments)
|
oResult.EmbeddedAttachmentFiles.AddRange(oAttachments)
|
||||||
End If
|
End If
|
||||||
|
|
||||||
' Check the Checksum and rejection status
|
' Check the Checksum and rejection status
|
||||||
Dim oMD5CheckSum = GenerateAndCheck_MD5Sum(oFile, pMessageId, pArgs.IgnoreRejectionStatus)
|
Dim oMD5Checksum = _hash.GenerateAndCheck_MD5Sum(oFile, pMessageId, pArgs.IgnoreRejectionStatus)
|
||||||
|
|
||||||
' Check the document against the configured property map and return:
|
' Check the document against the configured property map and return:
|
||||||
' - a List of valid properties
|
' - a List of valid properties
|
||||||
@@ -1002,7 +504,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
If oCheckResult.MissingProperties.Count > 0 Then
|
If oCheckResult.MissingProperties.Count > 0 Then
|
||||||
_logger.Warn("[{0}] missing properties found. Exiting.", oCheckResult.MissingProperties.Count)
|
_logger.Warn("[{0}] missing properties found. Exiting.", oCheckResult.MissingProperties.Count)
|
||||||
oMissingProperties = oCheckResult.MissingProperties
|
oMissingProperties = oCheckResult.MissingProperties
|
||||||
Throw New MissingValueException(oFile)
|
Throw New MissingValueException(oFile, oCheckResult.MissingProperties)
|
||||||
Else
|
Else
|
||||||
_logger.Debug("No missing properties found. Continuing.")
|
_logger.Debug("No missing properties found. Continuing.")
|
||||||
|
|
||||||
@@ -1027,12 +529,15 @@ Public Class ImportZUGFeRDFiles
|
|||||||
InsertPropertyValue(pMessageId, oConnections, oProperty)
|
InsertPropertyValue(pMessageId, oConnections, oProperty)
|
||||||
Next
|
Next
|
||||||
|
|
||||||
Return New ProcessFileResult With {
|
_logger.Debug("File processed.")
|
||||||
.ZugferdFileCount = oZugferdFiles
|
|
||||||
}
|
|
||||||
End Function
|
|
||||||
#End Region
|
|
||||||
|
|
||||||
|
oResult.ZugferdFileFound = True
|
||||||
|
oResult.MD5Checksum = oMD5Checksum
|
||||||
|
oResult.ZugferdFileCount = pZugferdFiles
|
||||||
|
|
||||||
|
Return oResult
|
||||||
|
|
||||||
|
End Function
|
||||||
|
|
||||||
Private Sub DeleteExistingPropertyValues(pMessageId As String, pConnections As DatabaseConnections)
|
Private Sub DeleteExistingPropertyValues(pMessageId As String, pConnections As DatabaseConnections)
|
||||||
Dim oDelSQL = $"DELETE FROM TBEDMI_ITEM_VALUE where REFERENCE_GUID = '{pMessageId}'"
|
Dim oDelSQL = $"DELETE FROM TBEDMI_ITEM_VALUE where REFERENCE_GUID = '{pMessageId}'"
|
||||||
@@ -1057,182 +562,15 @@ Public Class ImportZUGFeRDFiles
|
|||||||
|
|
||||||
Dim oCommand = $"INSERT INTO {pProperty.TableName} (REFERENCE_GUID, ITEM_DESCRIPTION, ITEM_VALUE, GROUP_COUNTER, SPEC_NAME, IS_REQUIRED) VALUES
|
Dim oCommand = $"INSERT INTO {pProperty.TableName} (REFERENCE_GUID, ITEM_DESCRIPTION, ITEM_VALUE, GROUP_COUNTER, SPEC_NAME, IS_REQUIRED) VALUES
|
||||||
('{pMessageId}', '{pProperty.Description}', '{pProperty.Value.Replace("'", "''")}', {oGroupCounterValue},'{pProperty.TableColumn}','{pProperty.IsRequired}')"
|
('{pMessageId}', '{pProperty.Description}', '{pProperty.Value.Replace("'", "''")}', {oGroupCounterValue},'{pProperty.TableColumn}','{pProperty.IsRequired}')"
|
||||||
_logger.Debug("Mapping Property [{0}] with value [{1}], Will be inserted into table [{2}]", pProperty.TableColumn, pProperty.Value.Replace("'", "''"), pProperty.TableName)
|
_logger.Debug("Mapping Property [{0}] with value [{1}], Will be inserted into table [{2}]", pProperty.TableColumn, pProperty.Value.Replace("'", "''"), pProperty.TableName)
|
||||||
' Insert into SQL Server
|
|
||||||
|
|
||||||
|
' Insert into SQL Server
|
||||||
Dim oResult = _mssql.ExecuteNonQueryWithConnectionObject(oCommand, pConnections.SQLServerConnection, MSSQLServer.TransactionMode.ExternalTransaction, pConnections.SQLServerTransaction)
|
Dim oResult = _mssql.ExecuteNonQueryWithConnectionObject(oCommand, pConnections.SQLServerConnection, MSSQLServer.TransactionMode.ExternalTransaction, pConnections.SQLServerTransaction)
|
||||||
If oResult = False Then
|
If oResult = False Then
|
||||||
_logger.Warn($"SQL Command [{oCommand}] was not successful. Check the log.")
|
_logger.Warn($"SQL Command [{oCommand}] was not successful. Check the log.")
|
||||||
End If
|
End If
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Private Function CreateMD5(pFilename As String) As String
|
|
||||||
Try
|
|
||||||
Dim oMD5 As New MD5CryptoServiceProvider
|
|
||||||
Dim oHash As Byte()
|
|
||||||
Dim oHashString As String
|
|
||||||
Dim oResult As String = ""
|
|
||||||
|
|
||||||
Using oFileStream As New FileStream(pFilename, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
|
|
||||||
oHash = oMD5.ComputeHash(oFileStream)
|
|
||||||
oHashString = BitConverter.ToString(oHash)
|
|
||||||
End Using
|
|
||||||
|
|
||||||
oResult = oHashString.Replace("-", "")
|
|
||||||
Return oResult
|
|
||||||
Catch ex As Exception
|
|
||||||
_logger.Error(ex)
|
|
||||||
Return ""
|
|
||||||
End Try
|
|
||||||
End Function
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
''' <summary>
|
|
||||||
''' Generates the MD5 Checksum of a file and checks it against the histroy table TBEDM_ZUGFERD_HISTORY_IN
|
|
||||||
''' </summary>
|
|
||||||
''' <param name="pFilePath">The path of the file to be checked</param>
|
|
||||||
''' <param name="pIgnoreRejectionStatus">Should the check take into account the rejection status of the file?</param>
|
|
||||||
''' <returns>The MD5 Checksum of the file, or an empty string, if the Checksum could not be created</returns>
|
|
||||||
''' <exception cref="MD5HashException">Throws, when the file should be rejected, ie. if it already exists in the table</exception>
|
|
||||||
Private Function GenerateAndCheck_MD5Sum(pFile As FileInfo, pMessageId As String, pIgnoreRejectionStatus As Boolean) As String
|
|
||||||
Dim oMD5CheckSum = CreateMD5(pFile.FullName)
|
|
||||||
|
|
||||||
' Exit if MD5 could not be created
|
|
||||||
If oMD5CheckSum = String.Empty Then
|
|
||||||
_logger.Warn("MD5 Checksum is nothing for file [{0}]!", pFile.FullName)
|
|
||||||
Return oMD5CheckSum
|
|
||||||
End If
|
|
||||||
|
|
||||||
' Check if Checksum exists in History Table
|
|
||||||
'Dim oCheckCommand = $"SELECT * FROM TBEDM_ZUGFERD_HISTORY_IN WHERE GUID = (SELECT MAX(GUID) FROM TBEDM_ZUGFERD_HISTORY_IN WHERE UPPER(MD5HASH) = UPPER('{oMD5CheckSum}'))"
|
|
||||||
'Dim oTable As DataTable = _firebird.GetDatatable(oCheckCommand, Firebird.TransactionMode.NoTransaction)
|
|
||||||
|
|
||||||
' Check if Checksum exists in History Table
|
|
||||||
' TODO: WHAT THE FUCK IS THIS
|
|
||||||
Dim oCheckCommand = $"SELECT * FROM TBEMLP_HISTORY WHERE GUID = (SELECT MAX(GUID) FROM TBEMLP_HISTORY WHERE UPPER(MD5HASH) = UPPER('{oMD5CheckSum}'))"
|
|
||||||
Dim oTable As DataTable = _mssql.GetDatatable(oCheckCommand, MSSQLServer.TransactionMode.NoTransaction)
|
|
||||||
|
|
||||||
' If History entries could not be fetched, just return the MD5 Checksum
|
|
||||||
If IsNothing(oTable) Then
|
|
||||||
_logger.Warn("Be careful: oExistsDT is nothing for file [{0}]!", pFile.FullName)
|
|
||||||
Return oMD5CheckSum
|
|
||||||
End If
|
|
||||||
|
|
||||||
' If Checksum does not exist in History entries, just return the MD5 Checksum
|
|
||||||
If oTable.Rows.Count = 0 Then
|
|
||||||
_logger.Debug("File [{0}] was not found in History!", pFile.FullName)
|
|
||||||
Return oMD5CheckSum
|
|
||||||
End If
|
|
||||||
|
|
||||||
' ====================================================
|
|
||||||
' Checksum exists in History entries, reject!
|
|
||||||
' ====================================================
|
|
||||||
|
|
||||||
Dim oRejected As Boolean
|
|
||||||
Dim oHistoryId As Integer
|
|
||||||
|
|
||||||
' Try to read Rejected Status and History Id
|
|
||||||
Try
|
|
||||||
Dim oRow As DataRow = oTable.Rows.Item(0)
|
|
||||||
oRejected = oRow.ItemEx("CUST_REJECTED", False)
|
|
||||||
oHistoryId = oRow.Item("GUID")
|
|
||||||
|
|
||||||
Catch ex As Exception
|
|
||||||
_logger.Warn("Error while converting REJECTED: " & ex.Message)
|
|
||||||
oRejected = False
|
|
||||||
|
|
||||||
End Try
|
|
||||||
|
|
||||||
_logger.Info("File with MessageId [{0}] has already been processed.", pMessageId)
|
|
||||||
|
|
||||||
' If the file was already rejected, it is allowed to be processed again,
|
|
||||||
' even if the Checksum exists in the history entries (default case)
|
|
||||||
' Which means, if it was not rejected before, it will be rejected in any case!
|
|
||||||
'
|
|
||||||
' This logic can be overwritten by the IgnoreRejectionStatus parameter.
|
|
||||||
' If it is set to true, the file will be rejected if the file exists in the history entries,
|
|
||||||
' regardless of the rejected parameter.
|
|
||||||
|
|
||||||
If oRejected = True And pIgnoreRejectionStatus = True Then
|
|
||||||
_logger.Info("ZuGFeRDFile already has been processed, but Rejection Status will be ignored!")
|
|
||||||
ElseIf oRejected = False Then
|
|
||||||
_logger.Info("ZuGFeRDFile already has been processed. Will be rejected.")
|
|
||||||
Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{oHistoryId}] - Case 1", pFile.Name)
|
|
||||||
Else
|
|
||||||
_logger.Info("ZuGFeRDFile already has been processed. Will be rejected.")
|
|
||||||
Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{oHistoryId}] - Case 2", pFile.Name)
|
|
||||||
End If
|
|
||||||
|
|
||||||
Return oMD5CheckSum
|
|
||||||
End Function
|
|
||||||
|
|
||||||
Private Function MoveAndRenameEmailToRejected(pArgs As WorkerArgs, pMessageId As String) As EmailData
|
|
||||||
_logger.Info("Moving Mail with MessageId [{0}] to Rejected folder", pMessageId)
|
|
||||||
_logger.Debug("Fetching Email Data")
|
|
||||||
|
|
||||||
Dim oEmailData = _email.GetEmailDataForMessageId(pMessageId)
|
|
||||||
_logger.Debug("Email Data fetched!")
|
|
||||||
|
|
||||||
Dim oSource = _email.GetOriginalEmailPath(pArgs.OriginalEmailDirectory, pMessageId)
|
|
||||||
_logger.Debug("Original email path: [{0}]", oSource)
|
|
||||||
|
|
||||||
Dim oDateSubDirectoryName As String = Now.ToString("yyyy-MM-dd")
|
|
||||||
Dim oDestination As String
|
|
||||||
|
|
||||||
Dim oRejectedDirectory As String = Path.Combine(pArgs.RejectedEmailDirectory, oDateSubDirectoryName)
|
|
||||||
|
|
||||||
' Create the destination directory if it does not exist
|
|
||||||
_logger.Debug("Creating destination directory [{0}]", oRejectedDirectory)
|
|
||||||
If Not Directory.Exists(oRejectedDirectory) Then
|
|
||||||
Try
|
|
||||||
Directory.CreateDirectory(oRejectedDirectory)
|
|
||||||
Catch ex As Exception
|
|
||||||
_logger.Error(ex)
|
|
||||||
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
|
|
||||||
oDestination = _email.GetEmailPathWithSubjectAsName(oRejectedDirectory, pMessageId)
|
|
||||||
Else
|
|
||||||
oDestination = _email.GetEmailPathWithSubjectAsName(oRejectedDirectory, StringEx.ConvertTextToSlug(oEmailData.Subject))
|
|
||||||
End If
|
|
||||||
|
|
||||||
_logger.Debug("Destination for eml file is {0}", oDestination)
|
|
||||||
|
|
||||||
Dim oFinalFileName = _filesystem.GetVersionedFilename(oDestination)
|
|
||||||
|
|
||||||
_logger.Debug("Versioned filename for eml file is {0}", oFinalFileName)
|
|
||||||
|
|
||||||
If oEmailData Is Nothing Then
|
|
||||||
_logger.Warn("Could not get Email Data from firebird-database. File {0} will not be moved!", oSource)
|
|
||||||
Return New EmailData()
|
|
||||||
End If
|
|
||||||
|
|
||||||
'---------------------------
|
|
||||||
|
|
||||||
Try
|
|
||||||
_logger.Info("Moving email from {0} to {1}", oSource, oFinalFileName)
|
|
||||||
File.Move(oSource, oFinalFileName)
|
|
||||||
oEmailData.Attachment = oFinalFileName
|
|
||||||
Catch ex As Exception
|
|
||||||
_logger.Warn("File {0} could not be moved! Original Filename will be used!", oSource)
|
|
||||||
_logger.Error(ex)
|
|
||||||
oEmailData.Attachment = oSource
|
|
||||||
End Try
|
|
||||||
|
|
||||||
_logger.Info("Email [{0}] moved to rejected folder!", pMessageId)
|
|
||||||
|
|
||||||
Return oEmailData
|
|
||||||
End Function
|
|
||||||
|
|
||||||
Private Sub AddRejectedState(pMessageID As String, pTitle As String, pTitle1 As String, pComment As String, pTransaction As SqlTransaction)
|
Private Sub AddRejectedState(pMessageID As String, pTitle As String, pTitle1 As String, pComment As String, pTransaction As SqlTransaction)
|
||||||
Try
|
Try
|
||||||
'PRCUST_ADD_HISTORY_STATE: @MessageID VARCHAR(250), @TITLE1 VARCHAR(250), @TITLE2 VARCHAR(250)
|
'PRCUST_ADD_HISTORY_STATE: @MessageID VARCHAR(250), @TITLE1 VARCHAR(250), @TITLE2 VARCHAR(250)
|
||||||
|
|||||||
Reference in New Issue
Block a user