Interfaces/Job: Add check for currencyId format in ZUGFeRD documents

This commit is contained in:
Jonathan Jenne
2023-05-26 15:04:44 +02:00
parent f491d4dd24
commit 76cba215fe
4 changed files with 126 additions and 3 deletions

View File

@@ -1,4 +1,6 @@
Imports System.IO
Imports System.Collections.Generic
Imports System.IO
Imports System.Reflection.Emit
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.XPath
@@ -54,6 +56,13 @@ Public Class ZUGFeRDInterface
Public Property XPathObject As XPathDocument
Public Property SchemaObject As Object
Public Property Specification As String
Public Property ValidationErrors As New List(Of ZugferdValidationError)
End Class
Public Class ZugferdValidationError
Public ElementName As String
Public ElementValue As String
Public ErrorMessage As String
End Class
''' <summary>
@@ -125,9 +134,10 @@ Public Class ZUGFeRDInterface
''' <exception cref="ZUGFeRDExecption"></exception>
Public Function ExtractZUGFeRDFileWithGDPicture(Path As String) As ZugferdResult
Dim oResult = ValidateZUGFeRDFileWithGDPicture(Path)
oResult = ValidateZUGFeRDDocument(oResult)
'If IsNothing(oResult.SchemaObject) Then
' Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
'End If
Return SerializeZUGFeRDDocument(oResult)
@@ -140,6 +150,7 @@ Public Class ZUGFeRDInterface
''' <exception cref="ZUGFeRDExecption"></exception>
Public Function ExtractZUGFeRDFileWithGDPicture(Stream As Stream) As ZugferdResult
Dim oResult = ValidateZUGFeRDFileWithGDPicture(Stream)
oResult = ValidateZUGFeRDDocument(oResult)
'If IsNothing(oResult.SchemaObject) Then
' Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
@@ -256,6 +267,72 @@ Public Class ZUGFeRDInterface
Public Specification As String
End Class
Public Function ValidateZUGFeRDDocument(pResult As ZugferdResult) As ZugferdResult
Dim oNavigator As XPathNavigator = pResult.XPathObject.CreateNavigator()
Dim oNamespaceManager As New XmlNamespaceManager(oNavigator.NameTable)
oNamespaceManager.AddNamespace("ram", "urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:12")
oNamespaceManager.AddNamespace("rsm", "urn:ferd:CrossIndustryDocument:invoice:1p0")
' CurrencyCode Nodes
Try
Dim oCurrencyCodeIterator As XPathNodeIterator = oNavigator.
Select("//ram:InvoiceCurrencyCode | //ram:TaxCurrencyCode | //ram:TaxCurrencyCode | //ram:SourceCurrencyCode", oNamespaceManager)
While oCurrencyCodeIterator.MoveNext()
Dim oNode As XPathNavigator = oCurrencyCodeIterator.Current
Dim oValid = ValidateCurrencyCode(oNode.Value)
If oValid = False Then
pResult.ValidationErrors.Add(New ZugferdValidationError() With {
.ElementName = oNode.Name,
.ElementValue = oNode.Value,
.ErrorMessage = "Invalid CurrencyCode. Only 3-Character codes are allowed."
})
End If
End While
Catch ex As Exception
_logger.Error(ex)
End Try
' currencyID
Try
Dim oCurrencyIDIterator As XPathNodeIterator = oNavigator.Select("//*[@currencyID]")
While oCurrencyIDIterator.MoveNext()
Dim oNode As XPathNavigator = oCurrencyIDIterator.Current
Dim oCurrencyID As String = oNode.GetAttribute("currencyID", "")
' CurrencyID is optional per spec
If String.IsNullOrWhiteSpace(oCurrencyID) Then
Continue While
End If
Dim oValid = ValidateCurrencyCode(oCurrencyID)
If oValid = False Then
pResult.ValidationErrors.Add(New ZugferdValidationError() With {
.ElementName = oNode.Name,
.ElementValue = oCurrencyID,
.ErrorMessage = "Invalid currencyID. Only 3-Character codes or empty values are allowed."
})
End If
End While
Catch ex As Exception
_logger.Error(ex)
End Try
Return pResult
End Function
Private Function ValidateCurrencyCode(pValue As String) As Boolean
Dim oValueRegex As New Text.RegularExpressions.Regex("[A-Z]{3}")
If oValueRegex.IsMatch(pValue) = False Then
Return False
End If
Return True
End Function
Public Function SerializeZUGFeRDDocument(pResult As ZugferdResult) As ZugferdResult
Try
Dim oNavigator As XPathNavigator = pResult.XPathObject.CreateNavigator()