|
|
|
|
@@ -12,6 +12,13 @@ Public Class ZUGFeRDInterface
|
|
|
|
|
Private ReadOnly _logger As Logger
|
|
|
|
|
Private ReadOnly _Options As ZugferdOptions
|
|
|
|
|
|
|
|
|
|
' These constants define the specification markers for the different
|
|
|
|
|
' zugferd document schema versions. These markers need to be used to
|
|
|
|
|
' define the property map in the database (column SPECIFICATION).
|
|
|
|
|
Public Const ZUGFERD_SPEC_DEFAULT = "DEFAULT"
|
|
|
|
|
Public Const ZUGFERD_SPEC_10 = "ZUGFERD_10"
|
|
|
|
|
Public Const ZUGFERD_SPEC_2x = "ZUGFERD_2x"
|
|
|
|
|
|
|
|
|
|
Private ReadOnly ValidFilenames As New List(Of String) From {
|
|
|
|
|
PDFEmbeds.ZUGFERD_XML_FILENAME.ToUpper,
|
|
|
|
|
PDFEmbeds.FACTUR_X_XML_FILENAME_DE.ToUpper,
|
|
|
|
|
@@ -35,6 +42,15 @@ Public Class ZUGFeRDInterface
|
|
|
|
|
Public Class ZugferdOptions
|
|
|
|
|
Public Property AllowFacturX_Filename As Boolean = True
|
|
|
|
|
Public Property AllowXRechnung_Filename As Boolean = True
|
|
|
|
|
Public Property AllowZugferd_1_0_Schema As Boolean = True
|
|
|
|
|
Public Property AllowZugferd_2_x_Schema As Boolean = True
|
|
|
|
|
End Class
|
|
|
|
|
|
|
|
|
|
Public Class ZugferdResult
|
|
|
|
|
Public Property DataFileName As String
|
|
|
|
|
Public Property XPathObject As XPathDocument
|
|
|
|
|
Public Property SchemaObject As Object
|
|
|
|
|
Public Property Specification As String
|
|
|
|
|
End Class
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
@@ -89,14 +105,14 @@ Public Class ZUGFeRDInterface
|
|
|
|
|
''' </summary>
|
|
|
|
|
''' <param name="Path"></param>
|
|
|
|
|
''' <exception cref="ZUGFeRDExecption"></exception>
|
|
|
|
|
Public Function ExtractZUGFeRDFileWithGDPicture(Path As String) As Tuple(Of String, Object)
|
|
|
|
|
Dim oXmlDocument = ValidateZUGFeRDFileWithGDPicture(Path)
|
|
|
|
|
Public Function ExtractZUGFeRDFileWithGDPicture(Path As String) As ZugferdResult
|
|
|
|
|
Dim oResult = ValidateZUGFeRDFileWithGDPicture(Path)
|
|
|
|
|
|
|
|
|
|
If IsNothing(oXmlDocument.Item2) Then
|
|
|
|
|
If IsNothing(oResult.SchemaObject) Then
|
|
|
|
|
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
Return SerializeZUGFeRDDocument(oXmlDocument)
|
|
|
|
|
Return SerializeZUGFeRDDocument(oResult)
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
@@ -104,14 +120,14 @@ Public Class ZUGFeRDInterface
|
|
|
|
|
''' </summary>
|
|
|
|
|
''' <param name="Stream"></param>
|
|
|
|
|
''' <exception cref="ZUGFeRDExecption"></exception>
|
|
|
|
|
Public Function ExtractZUGFeRDFileWithGDPicture(Stream As Stream) As Tuple(Of String, Object)
|
|
|
|
|
Dim oXmlDocument = ValidateZUGFeRDFileWithGDPicture(Stream)
|
|
|
|
|
Public Function ExtractZUGFeRDFileWithGDPicture(Stream As Stream) As ZugferdResult
|
|
|
|
|
Dim oResult = ValidateZUGFeRDFileWithGDPicture(Stream)
|
|
|
|
|
|
|
|
|
|
If IsNothing(oXmlDocument.Item2) Then
|
|
|
|
|
If IsNothing(oResult.SchemaObject) Then
|
|
|
|
|
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
Return SerializeZUGFeRDDocument(oXmlDocument)
|
|
|
|
|
Return SerializeZUGFeRDDocument(oResult)
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
@@ -120,7 +136,7 @@ Public Class ZUGFeRDInterface
|
|
|
|
|
''' <param name="pStream"></param>
|
|
|
|
|
''' <exception cref="ZUGFeRDExecption"></exception>
|
|
|
|
|
''' <returns>The embedded xml data as an XPath document</returns>
|
|
|
|
|
Public Function ValidateZUGFeRDFileWithGDPicture(pStream As Stream) As Tuple(Of String, XPathDocument)
|
|
|
|
|
Public Function ValidateZUGFeRDFileWithGDPicture(pStream As Stream) As ZugferdResult
|
|
|
|
|
Dim oEmbedExtractor = New PDFEmbeds(_logConfig)
|
|
|
|
|
|
|
|
|
|
Try
|
|
|
|
|
@@ -148,7 +164,7 @@ Public Class ZUGFeRDInterface
|
|
|
|
|
''' <param name="pPath"></param>
|
|
|
|
|
''' <exception cref="ZUGFeRDExecption"></exception>
|
|
|
|
|
''' <returns>The embedded xml data as an XPath document</returns>
|
|
|
|
|
Public Function ValidateZUGFeRDFileWithGDPicture(pPath As String) As Tuple(Of String, XPathDocument)
|
|
|
|
|
Public Function ValidateZUGFeRDFileWithGDPicture(pPath As String) As ZugferdResult
|
|
|
|
|
Dim oEmbedExtractor = New PDFEmbeds(_logConfig)
|
|
|
|
|
|
|
|
|
|
Try
|
|
|
|
|
@@ -170,7 +186,7 @@ Public Class ZUGFeRDInterface
|
|
|
|
|
End Try
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
Private Function HandleEmbeddedFiles(pResults As List(Of PDFEmbeds.EmbeddedFile)) As Tuple(Of String, XPathDocument)
|
|
|
|
|
Private Function HandleEmbeddedFiles(pResults As List(Of PDFEmbeds.EmbeddedFile)) As ZugferdResult
|
|
|
|
|
If pResults Is Nothing Then
|
|
|
|
|
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei, weil die Attachments nicht gelesen werden konnten.")
|
|
|
|
|
End If
|
|
|
|
|
@@ -200,7 +216,10 @@ Public Class ZUGFeRDInterface
|
|
|
|
|
|
|
|
|
|
Try
|
|
|
|
|
Using oStream As New MemoryStream(oAllowedResult.FileContents)
|
|
|
|
|
Return New Tuple(Of String, XPathDocument)(oAllowedResult.FileName, New XPathDocument(oStream))
|
|
|
|
|
Return New ZugferdResult With {
|
|
|
|
|
.DataFileName = oAllowedResult.FileName,
|
|
|
|
|
.XPathObject = New XPathDocument(oStream)
|
|
|
|
|
}
|
|
|
|
|
End Using
|
|
|
|
|
|
|
|
|
|
Catch ex As ZUGFeRDExecption
|
|
|
|
|
@@ -214,44 +233,59 @@ Public Class ZUGFeRDInterface
|
|
|
|
|
End Try
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
Public Function SerializeZUGFeRDDocument(pDocument As Tuple(Of String, XPathDocument)) As Tuple(Of String, Object)
|
|
|
|
|
Public Function SerializeZUGFeRDDocument(pResult As ZugferdResult) As ZugferdResult
|
|
|
|
|
Try
|
|
|
|
|
Dim oNavigator As XPathNavigator = pDocument.Item2.CreateNavigator()
|
|
|
|
|
Dim oNavigator As XPathNavigator = pResult.XPathObject.CreateNavigator()
|
|
|
|
|
Dim oReader As XmlReader
|
|
|
|
|
Dim oResult = Nothing
|
|
|
|
|
|
|
|
|
|
Dim oTypes As New List(Of Type) From {
|
|
|
|
|
GetType(ZUGFeRD.Version1_0.CrossIndustryDocumentType),
|
|
|
|
|
GetType(ZUGFeRD.Version2_0.CrossIndustryInvoiceType),
|
|
|
|
|
GetType(ZUGFeRD.Version2_1_1.CrossIndustryInvoiceType),
|
|
|
|
|
GetType(ZUGFeRD.Version2_2_FacturX.CrossIndustryInvoiceType)
|
|
|
|
|
}
|
|
|
|
|
Dim oObject As Object = Nothing
|
|
|
|
|
Dim oSpecification As String = Nothing
|
|
|
|
|
|
|
|
|
|
For Each oType In oTypes
|
|
|
|
|
_logger.Debug("Trying Type [{0}]", oType.FullName)
|
|
|
|
|
Dim oSerializer As New XmlSerializer(oType)
|
|
|
|
|
Dim oAllowedTypes As New Dictionary(Of String, Type)
|
|
|
|
|
|
|
|
|
|
If _Options.AllowZugferd_1_0_Schema Then
|
|
|
|
|
oAllowedTypes.Add(ZUGFERD_SPEC_10, GetType(ZUGFeRD.Version1_0.CrossIndustryDocumentType))
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
If _Options.AllowZugferd_2_x_Schema Then
|
|
|
|
|
oAllowedTypes.Add(ZUGFERD_SPEC_2x, GetType(ZUGFeRD.Version2_0.CrossIndustryInvoiceType))
|
|
|
|
|
oAllowedTypes.Add(ZUGFERD_SPEC_2x, GetType(ZUGFeRD.Version2_1_1.CrossIndustryInvoiceType))
|
|
|
|
|
oAllowedTypes.Add(ZUGFERD_SPEC_2x, GetType(ZUGFeRD.Version2_2_FacturX.CrossIndustryInvoiceType))
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
For Each oType In oAllowedTypes
|
|
|
|
|
Dim oTypeName As String = oType.Value.FullName
|
|
|
|
|
Dim oSerializer As New XmlSerializer(oType.Value)
|
|
|
|
|
_logger.Debug("Trying Type [{0}]", oTypeName)
|
|
|
|
|
|
|
|
|
|
Try
|
|
|
|
|
oReader = oNavigator.ReadSubtree()
|
|
|
|
|
oResult = oSerializer.Deserialize(oReader)
|
|
|
|
|
_logger.Debug("Serializing with type [{0}] succeeded", oType.FullName)
|
|
|
|
|
|
|
|
|
|
oObject = oSerializer.Deserialize(oReader)
|
|
|
|
|
oSpecification = oType.Key
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Serializing with type [{0}] succeeded", oTypeName)
|
|
|
|
|
Exit For
|
|
|
|
|
Catch ex As Exception
|
|
|
|
|
_logger.Debug("Serializing with type [{0}] failed", oType.FullName)
|
|
|
|
|
_logger.Debug("Serializing with type [{0}] failed", oTypeName)
|
|
|
|
|
_logger.Debug(ex.Message)
|
|
|
|
|
_logger.Error(ex.InnerException?.Message)
|
|
|
|
|
End Try
|
|
|
|
|
Next
|
|
|
|
|
|
|
|
|
|
If oResult Is Nothing Then
|
|
|
|
|
If oObject Is Nothing Then
|
|
|
|
|
Throw New ApplicationException("No Types matched the given document. Document could not be serialized.")
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
Return oResult
|
|
|
|
|
pResult.Specification = oSpecification
|
|
|
|
|
pResult.SchemaObject = oObject
|
|
|
|
|
|
|
|
|
|
Return pResult
|
|
|
|
|
|
|
|
|
|
Catch ex As Exception
|
|
|
|
|
_logger.Error(ex)
|
|
|
|
|
Throw New ZUGFeRDExecption(ErrorType.NoValidZugferd, "Datei ist eine ungültige ZUGFeRD Datei.")
|
|
|
|
|
Dim oMessage = "Datei ist eine ungültige ZUGFeRD Datei oder das Format wird nicht unterstüzt, oder das Format ist deaktiviert."
|
|
|
|
|
Throw New ZUGFeRDExecption(ErrorType.NoValidZugferd, oMessage)
|
|
|
|
|
End Try
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|