ZUGFeRDRESTService: Prepare Zugferd 2x

This commit is contained in:
Jonathan Jenne
2023-02-28 12:56:43 +01:00
parent bd03e5b925
commit 1c428b7db6
5 changed files with 104 additions and 27 deletions

View File

@@ -33,9 +33,13 @@ namespace ZUGFeRDRESTService.Controllers
private readonly DigitalData.Modules.Filesystem.File _file;
private readonly PropertyValues _props;
private readonly Dictionary<string, XmlItemProperty> _propertyMap;
private readonly Dictionary<string, XmlItemProperty> _propertyMap = new Dictionary<string, XmlItemProperty>();
private readonly int _MaxFileSizeInMegabytes;
private int _MaxFileSizeInMegabytes;
private bool _AllowFacturX;
private bool _AllowXRechnung;
private bool _AllowZugferd2x;
private bool _AllowZugferd10;
public ValidationController(ILogging logging, IDatabase database, IConfiguration Config)
{
@@ -49,20 +53,69 @@ namespace ZUGFeRDRESTService.Controllers
var oGDPictureKey = database.GetGDPictureKey();
var oPropertyMap = database.GetPropertyMap();
_propertyMap = oPropertyMap;
// Read config file and assign all option flags related to
// - Zugferd files
// - Filesizes
ParseConfig(Config);
_zugferd = new ZUGFeRDInterface(_logConfig, oGDPictureKey);
_zugferd = new ZUGFeRDInterface(_logConfig, oGDPictureKey, new ZugferdOptions()
{
AllowFacturX_Filename = _AllowFacturX,
AllowXRechnung_Filename = _AllowXRechnung,
AllowZugferd_1_0_Schema = _AllowZugferd10,
AllowZugferd_2_x_Schema = _AllowZugferd2x
});
_props = new PropertyValues(_logConfig);
var oAppConfig = Config.GetSection("Config");
if (_AllowZugferd10 == true)
_propertyMap = oPropertyMap.
Where(kv => kv.Value.Specification == ZUGFERD_SPEC_10 || kv.Value.Specification == ZUGFERD_SPEC_DEFAULT).
Concat(_propertyMap).
ToDictionary(kv => kv.Key, kv => kv.Value);
if (int.TryParse(oAppConfig["MaxFileSizeInMegabytes"], out _MaxFileSizeInMegabytes))
if (_AllowZugferd2x == true)
_propertyMap = oPropertyMap.
Where(kv => kv.Value.Specification == ZUGFERD_SPEC_2x).
Concat(_propertyMap).
ToDictionary(kv => kv.Key, kv => kv.Value);
_logger.Debug("Validation Controller initialized!");
}
private void ParseConfig(IConfiguration Config)
{
var oAppConfig = Config.GetSection("Config");
var oZugferdConfig = oAppConfig.GetSection("Zugferd");
if (!bool.TryParse(oZugferdConfig["AllowFacturX"], out _AllowFacturX))
{
_logger.Info("Configuration AllowFacturX was not set. Using default value [{0}]", false);
_AllowFacturX = false;
}
if (!bool.TryParse(oZugferdConfig["AllowXRechnung"], out _AllowXRechnung))
{
_logger.Info("Configuration AllowXRechnung was not set. Using default value [{0}]", false);
_AllowXRechnung = false;
}
if (!bool.TryParse(oZugferdConfig["Zugferd2x"], out _AllowZugferd2x))
{
_logger.Info("Configuration Zugferd2x was not set. Using default value [{0}]", false);
_AllowZugferd2x = false;
}
if (!bool.TryParse(oZugferdConfig["Zugferd10"], out _AllowZugferd10))
{
_logger.Info("Configuration Zugferd10 was not set. Using default value [{0}]", false);
_AllowZugferd10 = false;
}
if (!int.TryParse(oAppConfig["MaxFileSizeInMegabytes"], out _MaxFileSizeInMegabytes))
{
_logger.Info("Configuration MaxFileSizeInMegabytes was not set. Using default value [{0}]", MAX_FILE_SIZE_DEFAULT);
_MaxFileSizeInMegabytes = MAX_FILE_SIZE_DEFAULT;
}
_logger.Debug("Validation Controller initialized!");
}
/// <summary>
@@ -75,8 +128,8 @@ namespace ZUGFeRDRESTService.Controllers
{
_logger.Info("Start processing request to ValidationController");
object oDocument;
CheckPropertyValuesResult oResult = new CheckPropertyValuesResult();
ZugferdResult oZugferdResult;
CheckPropertyValuesResult oPropertyResult = new CheckPropertyValuesResult();
try
{
@@ -89,40 +142,49 @@ namespace ZUGFeRDRESTService.Controllers
if (oFileSizeIsOK == false)
{
throw new ZUGFeRDExecption(ErrorType.FileTooBig,
string.Format("Die hochgeladene Datei überschreitet die zulässige Dateigröße [{0}].", _MaxFileSizeInMegabytes));
throw new ZUGFeRDExecption(ErrorType.FileTooBig, "FileTooBig");
}
_logger.Info("Extracting ZUGFeRD Data from file [{0}]", file.FileName);
oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oStream);
oZugferdResult = _zugferd.ExtractZUGFeRDFileWithGDPicture(oStream);
_logger.Info("Detected Specification was: [{0}]", oZugferdResult.Specification);
var oFilteredPropertyMap = _propertyMap.
Where(kv => kv.Value.Specification == oZugferdResult.Specification).
ToDictionary(kv => kv.Key, kv => kv.Value);
if (oFilteredPropertyMap.Count == 0)
{
throw new ZUGFeRDExecption(ErrorType.UnsupportedFormat, "Unsupported Format");
}
_logger.Info("Starting structural check against the database.");
oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID");
oPropertyResult = _props.CheckPropertyValues(oZugferdResult.SchemaObject, oFilteredPropertyMap, "MESSAGEID");
var oRequiredProperties = _propertyMap.
var oRequiredProperties = oFilteredPropertyMap.
Where(prop => { return prop.Value.IsRequired == true; }).
Select(prop => { return prop.Value.Description; })
.ToList();
Select(prop => { return prop.Value.Description; }).
ToList();
_logger.Debug("Found [{0}] required properties", oRequiredProperties.Count);
_logger.Debug(string.Join(",", oRequiredProperties.ToArray()));
_logger.Info("Result of checking against the database: {0} valid properties, {1} missing properties",
oResult.ValidProperties.Count, oResult.MissingProperties.Count);
oPropertyResult.ValidProperties.Count, oPropertyResult.MissingProperties.Count);
if (oResult.MissingProperties.Count > 0)
if (oPropertyResult.MissingProperties.Count > 0)
{
throw new ZUGFeRDExecption(ErrorType.MissingProperties,
"Die hochgeladene Datei ist eine gültige ZUGFeRD-Rechnung, allerdings fehlen benötigte Daten.");
throw new ZUGFeRDExecption(ErrorType.MissingProperties, "Missing Properties");
}
Tuple<bool, string> oValidateResult = ValidateBuyerOrderReference(oResult.ValidProperties);
Tuple<bool, string> oValidateResult = ValidateBuyerOrderReference(oPropertyResult.ValidProperties);
if (oValidateResult.Item1 == false)
{
throw new ZUGFeRDExecption(ErrorType.UnknownError, "Die hochgeladene Datei kann nicht validiert werden, weil ein unbekannter Fehler aufgetreten ist.");
throw new ZUGFeRDExecption(ErrorType.UnknownError, "Unknown Error");
}
string oValidateResultString = oValidateResult.Item2;
@@ -162,8 +224,9 @@ namespace ZUGFeRDRESTService.Controllers
ErrorType.NoValidFile => "Die hochgeladene Datei ist keine gültige Datei.",
ErrorType.NoZugferd => "Die hochgeladene Datei ist keine ZUGFeRD-Rechnung.",
ErrorType.NoValidZugferd => "Die hochgeladene Datei ist keine gültige ZUGFeRD-Rechnung.",
ErrorType.MissingProperties => "Die hochgeladene Datei ist keine gültige ZUGFeRD-Rechnung, es fehlen einige Metadaten",
ErrorType.MissingProperties => "Die hochgeladene Datei ist keine gültige ZUGFeRD-Rechnung, es fehlen einige Metadaten.",
ErrorType.FileTooBig => string.Format("Die hochgeladene Datei überschreitet die zulässige Dateigröße [{0}].", _MaxFileSizeInMegabytes),
ErrorType.UnsupportedFormat => "Die hochgeladene Datei enthält ein falsches oder nicht unterstütztes ZUGFeRD Format.",
_ => "Die hochgeladene Datei kann nicht validiert werden.",
};
@@ -171,7 +234,7 @@ namespace ZUGFeRDRESTService.Controllers
List<string> oErrors = ex.ErrorType switch
{
// Errors contains the list of missing fields
ErrorType.MissingProperties => oResult.MissingProperties,
ErrorType.MissingProperties => oPropertyResult.MissingProperties,
_ => new List<string>()
};