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

@ -7,6 +7,11 @@
public string MSSQLConnectionString { get; set; } public string MSSQLConnectionString { get; set; }
public string MaxFileSizeInMegabytes { get; set; } public string MaxFileSizeInMegabytes { get; set; }
public bool AllowFacturX { get; set; } = false;
public bool AllowXRechnung { get; set; } = false;
public bool AllowZugferd10 { get; set; } = true;
public bool AllowZugferd2x { get; set; } = true;
public FirebirdConfig Firebird { get; set; } public FirebirdConfig Firebird { get; set; }
} }

View File

@ -33,9 +33,13 @@ namespace ZUGFeRDRESTService.Controllers
private readonly DigitalData.Modules.Filesystem.File _file; private readonly DigitalData.Modules.Filesystem.File _file;
private readonly PropertyValues _props; 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) public ValidationController(ILogging logging, IDatabase database, IConfiguration Config)
{ {
@ -49,20 +53,69 @@ namespace ZUGFeRDRESTService.Controllers
var oGDPictureKey = database.GetGDPictureKey(); var oGDPictureKey = database.GetGDPictureKey();
var oPropertyMap = database.GetPropertyMap(); 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); _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); _logger.Info("Configuration MaxFileSizeInMegabytes was not set. Using default value [{0}]", MAX_FILE_SIZE_DEFAULT);
_MaxFileSizeInMegabytes = MAX_FILE_SIZE_DEFAULT; _MaxFileSizeInMegabytes = MAX_FILE_SIZE_DEFAULT;
} }
_logger.Debug("Validation Controller initialized!");
} }
/// <summary> /// <summary>
@ -75,8 +128,8 @@ namespace ZUGFeRDRESTService.Controllers
{ {
_logger.Info("Start processing request to ValidationController"); _logger.Info("Start processing request to ValidationController");
object oDocument; ZugferdResult oZugferdResult;
CheckPropertyValuesResult oResult = new CheckPropertyValuesResult(); CheckPropertyValuesResult oPropertyResult = new CheckPropertyValuesResult();
try try
{ {
@ -89,40 +142,49 @@ namespace ZUGFeRDRESTService.Controllers
if (oFileSizeIsOK == false) if (oFileSizeIsOK == false)
{ {
throw new ZUGFeRDExecption(ErrorType.FileTooBig, throw new ZUGFeRDExecption(ErrorType.FileTooBig, "FileTooBig");
string.Format("Die hochgeladene Datei überschreitet die zulässige Dateigröße [{0}].", _MaxFileSizeInMegabytes));
} }
_logger.Info("Extracting ZUGFeRD Data from file [{0}]", file.FileName); _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."); _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; }). Where(prop => { return prop.Value.IsRequired == true; }).
Select(prop => { return prop.Value.Description; }) Select(prop => { return prop.Value.Description; }).
.ToList(); ToList();
_logger.Debug("Found [{0}] required properties", oRequiredProperties.Count); _logger.Debug("Found [{0}] required properties", oRequiredProperties.Count);
_logger.Debug(string.Join(",", oRequiredProperties.ToArray())); _logger.Debug(string.Join(",", oRequiredProperties.ToArray()));
_logger.Info("Result of checking against the database: {0} valid properties, {1} missing properties", _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, throw new ZUGFeRDExecption(ErrorType.MissingProperties, "Missing Properties");
"Die hochgeladene Datei ist eine gültige ZUGFeRD-Rechnung, allerdings fehlen benötigte Daten.");
} }
Tuple<bool, string> oValidateResult = ValidateBuyerOrderReference(oResult.ValidProperties); Tuple<bool, string> oValidateResult = ValidateBuyerOrderReference(oPropertyResult.ValidProperties);
if (oValidateResult.Item1 == false) 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; string oValidateResultString = oValidateResult.Item2;
@ -162,8 +224,9 @@ namespace ZUGFeRDRESTService.Controllers
ErrorType.NoValidFile => "Die hochgeladene Datei ist keine gültige Datei.", ErrorType.NoValidFile => "Die hochgeladene Datei ist keine gültige Datei.",
ErrorType.NoZugferd => "Die hochgeladene Datei ist keine ZUGFeRD-Rechnung.", ErrorType.NoZugferd => "Die hochgeladene Datei ist keine ZUGFeRD-Rechnung.",
ErrorType.NoValidZugferd => "Die hochgeladene Datei ist keine gültige 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.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.", _ => "Die hochgeladene Datei kann nicht validiert werden.",
}; };
@ -171,7 +234,7 @@ namespace ZUGFeRDRESTService.Controllers
List<string> oErrors = ex.ErrorType switch List<string> oErrors = ex.ErrorType switch
{ {
// Errors contains the list of missing fields // Errors contains the list of missing fields
ErrorType.MissingProperties => oResult.MissingProperties, ErrorType.MissingProperties => oPropertyResult.MissingProperties,
_ => new List<string>() _ => new List<string>()
}; };

View File

@ -18,7 +18,7 @@ namespace ZUGFeRDRESTService
private Dictionary<string, XmlItemProperty> _propertyMap = null; private Dictionary<string, XmlItemProperty> _propertyMap = null;
private const string QUERY_GET_GDPICTURE_KEY = "SELECT LICENSE FROM TBDD_3RD_PARTY_MODULES WHERE NAME = 'GDPICTURE'"; private const string QUERY_GET_GDPICTURE_KEY = "SELECT LICENSE FROM TBDD_3RD_PARTY_MODULES WHERE NAME = 'GDPICTURE'";
private const string QUERY_GET_PROPERTY_MAP = "SELECT * FROM TBEDM_XML_ITEMS WHERE SPECIFICATION = '{0}' AND ACTIVE = True ORDER BY XML_PATH"; private const string QUERY_GET_PROPERTY_MAP = "SELECT * FROM TBEDM_XML_ITEMS WHERE ACTIVE = True ORDER BY XML_PATH";
public MSSQLServer MSSQL { get; set; } public MSSQLServer MSSQL { get; set; }
public Firebird Firebird { get; set; } public Firebird Firebird { get; set; }

View File

@ -33,7 +33,10 @@ namespace ZUGFeRDRESTService
services.AddControllers().AddNewtonsoftJson(); services.AddControllers().AddNewtonsoftJson();
services.AddRazorPages(); services.AddRazorPages();
services.Configure<Config>(Configuration.GetSection("Config"));
// brauchen sie das überhaupt???!!!?!?!!
//services.Configure<Config>(Configuration.GetSection("Config"));
services.AddSingleton<ILogging>(sp => new Logging(oLogPath, oLogDebug)); services.AddSingleton<ILogging>(sp => new Logging(oLogPath, oLogDebug));
services.AddSingleton((Func<IServiceProvider, IDatabase>)(sp => { services.AddSingleton((Func<IServiceProvider, IDatabase>)(sp => {
var logging = sp.GetService<ILogging>(); var logging = sp.GetService<ILogging>();

View File

@ -18,6 +18,12 @@
"Username": "<FIREBIRDSERVER-USERNAME>", "Username": "<FIREBIRDSERVER-USERNAME>",
"Password": "<FIREBIRDSERVER-PASSWORD>" "Password": "<FIREBIRDSERVER-PASSWORD>"
}, },
"Zugferd": {
"AllowZugferd10": true,
"AllowZugferd2x": false,
"AllowFacturX": false,
"AllowXRechnung": false
},
"MaxFileSizeInMegabytes": 25 "MaxFileSizeInMegabytes": 25
} }
} }