ZUGFeRdRESTService - Validation Controller: REFERENCES_Rejection-Messages auswerten und als WebMessage ausgeben

This commit is contained in:
2026-02-02 14:40:55 +01:00
parent e1d5c2961d
commit 24dbed32cc
2 changed files with 269 additions and 33 deletions

View File

@@ -1,18 +1,17 @@
using System; using DigitalData.Modules.Interfaces;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using DigitalData.Modules.Interfaces;
using static DigitalData.Modules.Interfaces.Exceptions;
using static DigitalData.Modules.Interfaces.ZUGFeRDInterface;
using static DigitalData.Modules.Interfaces.PropertyValues;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using System.Xml.Linq; using System;
using Newtonsoft.Json.Linq; using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Xml.Linq;
using static DigitalData.Modules.Interfaces.Exceptions;
using static DigitalData.Modules.Interfaces.PropertyValues;
using static DigitalData.Modules.Interfaces.ZUGFeRDInterface;
namespace ZUGFeRDRESTService.Controllers namespace ZUGFeRDRESTService.Controllers
{ {
@@ -26,6 +25,29 @@ namespace ZUGFeRDRESTService.Controllers
public const string ADDED_WHO = "ZUGFeRD REST Service"; public const string ADDED_WHO = "ZUGFeRD REST Service";
public const string MESSAGEID_DOMAIN = "test.wisag.de"; public const string MESSAGEID_DOMAIN = "test.wisag.de";
public const string VALIDATION_SUCCESS = "VALIDATION SUCCESS";
public const string REFERENCES_Rejection_30001 = "REFERENCES_Rejection_30001";
public const string REFERENCES_Rejection_30002 = "REFERENCES_Rejection_30002";
public const string REFERENCES_Rejection_30003_1 = "REFERENCES_Rejection_30003_1";
public const string REFERENCES_Rejection_30003_2 = "REFERENCES_Rejection_30003_2";
public const string REFERENCES_Rejection_30003_3 = "REFERENCES_Rejection_30003_3";
public const string REFERENCES_Rejection_30004_1 = "REFERENCES_Rejection_30004_1";
public const string REFERENCES_Rejection_30004_2 = "REFERENCES_Rejection_30004_2";
public const string REFERENCES_Rejection_30004_3 = "REFERENCES_Rejection_30004_3";
public const string REFERENCES_Rejection_30005_1 = "REFERENCES_Rejection_30005_1";
public const string REFERENCES_Rejection_30005_2 = "REFERENCES_Rejection_30005_2";
public const string REFERENCES_Rejection_30006 = "REFERENCES_Rejection_30006" ;
public const string REFERENCES_Rejection_30007 = "REFERENCES_Rejection_30007" ;
public const string REFERENCES_Rejection_30007_1 = "REFERENCES_Rejection_30007_1";
public const string REFERENCES_Rejection_30008 = "REFERENCES_Rejection_30008" ;
public const string REFERENCES_Rejection_30009 = "REFERENCES_Rejection_30009" ;
public const string REFERENCES_Rejection_30010 = "REFERENCES_Rejection_30010" ;
public const string REFERENCES_Rejection_30011 = "REFERENCES_Rejection_30011" ;
public const string REFERENCES_Rejection_30012 = "REFERENCES_Rejection_30012" ;
public const string AMOUNT_CALC_REJECTION = "AMOUNT_CALC_REJECTION";
private List<string> _ValidationErrors;
private const int MAX_FILE_SIZE_DEFAULT = 25; private const int MAX_FILE_SIZE_DEFAULT = 25;
private readonly ZUGFeRDInterface _zugferd; private readonly ZUGFeRDInterface _zugferd;
@@ -82,7 +104,29 @@ namespace ZUGFeRDRESTService.Controllers
_logger.Debug("Property Map list initial: [{0}] entries found. [{1}] entries will be available.", oPropertyMapList.Count, _propertyMapList.Count); _logger.Debug("Property Map list initial: [{0}] entries found. [{1}] entries will be available.", oPropertyMapList.Count, _propertyMapList.Count);
_RecjectionMessageList = database.GetRejectionMessageList(); _RecjectionMessageList = database.GetRejectionMessageList();
_ValidationErrors = new List<String>() {
REFERENCES_Rejection_30001,
REFERENCES_Rejection_30002,
REFERENCES_Rejection_30003_1,
REFERENCES_Rejection_30003_2,
REFERENCES_Rejection_30003_3,
REFERENCES_Rejection_30004_1,
REFERENCES_Rejection_30004_2,
REFERENCES_Rejection_30004_3,
REFERENCES_Rejection_30005_1,
REFERENCES_Rejection_30005_2,
REFERENCES_Rejection_30006,
REFERENCES_Rejection_30007,
REFERENCES_Rejection_30007_1,
REFERENCES_Rejection_30008,
REFERENCES_Rejection_30009,
REFERENCES_Rejection_30010,
REFERENCES_Rejection_30011,
REFERENCES_Rejection_30012,
AMOUNT_CALC_REJECTION
};
_logger.Debug("Validation Controller initialized!"); _logger.Debug("Validation Controller initialized!");
} }
@@ -189,19 +233,19 @@ namespace ZUGFeRDRESTService.Controllers
_logger.Info("Detected Specification was: [{0}]", oZugferdResult.Specification); _logger.Info("Detected Specification was: [{0}]", oZugferdResult.Specification);
var oFilteredPropertyMap = _zugferd.FilterPropertyMap(_propertyMapList, oZugferdResult.Specification); var oFilteredPropertyMap = _zugferd.FilterPropertyMap(_propertyMapList, oZugferdResult.Specification);
if (oFilteredPropertyMap.Count == 0) if (oFilteredPropertyMap.Count == 0)
{ {
_logger.Warn("No properties found in property map for specification [{0}]", oZugferdResult.Specification); _logger.Warn("No properties found in property map for specification [{0}]", oZugferdResult.Specification);
//throw new ZUGFeRDExecption(ErrorType.UnsupportedFormat, "Unsupported Format"); //throw new ZUGFeRDExecption(ErrorType.UnsupportedFormat, "Unsupported Format");
throw new ZUGFeRDExecption(ErrorCodes.UnsupportedFerdException, "Unsupported Format"); throw new ZUGFeRDExecption(ErrorCodes.UnsupportedFerdException, "Unsupported Format");
} }
else else
{ {
_logger.Debug("Property map contains [{0}] entries for specification [{1}]", oFilteredPropertyMap.Count, oZugferdResult.Specification); _logger.Debug("Property map contains [{0}] entries for specification [{1}]", oFilteredPropertyMap.Count, oZugferdResult.Specification);
} }
_logger.Info("Starting structural check against the database."); _logger.Info("Starting structural check against the database.");
oPropertyResult = _props.CheckPropertyValues(oZugferdResult.SchemaObject, oFilteredPropertyMap, "MESSAGEID"); oPropertyResult = _props.CheckPropertyValues(oZugferdResult.SchemaObject, oFilteredPropertyMap, "MESSAGEID");
@@ -221,7 +265,7 @@ namespace ZUGFeRDRESTService.Controllers
{ {
//throw new ZUGFeRDExecption(ErrorType.MissingProperties, "Missing Properties"); //throw new ZUGFeRDExecption(ErrorType.MissingProperties, "Missing Properties");
throw new ZUGFeRDExecption(ErrorCodes.MissingValueException, "Missing Properties"); throw new ZUGFeRDExecption(ErrorCodes.MissingValueException, "Missing Properties");
} }
Tuple<bool, string> oValidateResult = ValidateBuyerOrderReference(oPropertyResult.ValidProperties); Tuple<bool, string> oValidateResult = ValidateBuyerOrderReference(oPropertyResult.ValidProperties);
@@ -233,10 +277,9 @@ namespace ZUGFeRDRESTService.Controllers
string oValidateResultString = oValidateResult.Item2; string oValidateResultString = oValidateResult.Item2;
if (oValidateResultString == "ALL REFERENCES CHECKED POSITIVE") if (oValidateResultString.Equals(VALIDATION_SUCCESS))
{ {
string oMessage = "Die hochgeladene Datei ist eine gültige-ZUGFeRD Rechnung"; string oMessage = "Die hochgeladene Datei ist eine gültige-ZUGFeRD Rechnung";
_logger.Info($"Responding with message: [{oMessage}]"); _logger.Info($"Responding with message: [{oMessage}]");
return new ValidationResponse() return new ValidationResponse()
@@ -244,10 +287,10 @@ namespace ZUGFeRDRESTService.Controllers
status = RESPONSE_OK, status = RESPONSE_OK,
message = oMessage message = oMessage
}; };
} else }
else
{ {
string oMessage = oValidateResultString; string oMessage = oValidateResultString;
_logger.Info($"Responding with message: [{oMessage}]"); _logger.Info($"Responding with message: [{oMessage}]");
return new ValidationResponse() return new ValidationResponse()
@@ -256,7 +299,8 @@ namespace ZUGFeRDRESTService.Controllers
message = oMessage message = oMessage
}; };
} }
}; }
} }
catch (ZUGFeRDExecption ex) catch (ZUGFeRDExecption ex)
{ {
@@ -299,9 +343,9 @@ namespace ZUGFeRDRESTService.Controllers
break; break;
} }
} }
else else
{ {
oMessage = "Alte Logik. Meldung nicht gefunden"; oMessage = "Alte Logik. Meldung nicht gefunden";
} }
_logger.Info($"Responding with message: [{oMessage}]"); _logger.Info($"Responding with message: [{oMessage}]");
@@ -315,6 +359,8 @@ namespace ZUGFeRDRESTService.Controllers
} }
catch (ValidationException ex) catch (ValidationException ex)
{ {
_logger.Error(ex);
var rejectionCodeNumber = this.GetRejectionCodeNumber(ex.ErrorCode); var rejectionCodeNumber = this.GetRejectionCodeNumber(ex.ErrorCode);
// Der gesamte Ausgabetext muss anhand des ErrorCodes ermittelt werden // Der gesamte Ausgabetext muss anhand des ErrorCodes ermittelt werden
@@ -348,10 +394,14 @@ namespace ZUGFeRDRESTService.Controllers
} }
} }
/// <summary>
/// Hier wird eine externe Prozedur gerufen, PRCUST_INV_CHECK_FROM_PORTAL,
/// die das Ergebnis der Referenzpruefung liefert.
/// </summary>
private Tuple<bool, string> ValidateBuyerOrderReference(List<ValidProperty> pProperties) private Tuple<bool, string> ValidateBuyerOrderReference(List<ValidProperty> pProperties)
{ {
var oMessageId = GetMessageId(); var oMessageId = GetMessageId();
_logger.Debug("Created new MessageId: [{0}]", oMessageId); _logger.Debug("Created new MessageId: [{0}]", oMessageId);
_logger.Debug("Inserting properties into database."); _logger.Debug("Inserting properties into database.");
foreach (var oItem in pProperties) foreach (var oItem in pProperties)
@@ -377,6 +427,59 @@ namespace ZUGFeRDRESTService.Controllers
if (_database.MSSQL.ExecuteNonQuery(oCommand)) if (_database.MSSQL.ExecuteNonQuery(oCommand))
{ {
string oReturnValue = (string)oCommand.Parameters["@MSG_OUTPUT"].Value; string oReturnValue = (string)oCommand.Parameters["@MSG_OUTPUT"].Value;
_logger.Debug("Validation Result message from DB: " + oReturnValue);
if (oReturnValue.Equals("ALL REFERENCES CHECKED POSITIVE", StringComparison.OrdinalIgnoreCase))
{
_logger.Debug("Validation Success");
oReturnValue = VALIDATION_SUCCESS;
}
else
{
// Gehe durch die möglichen Fehler, und ermittle Rückmeldung
foreach (var oRejectionItem in _ValidationErrors)
{
if (oReturnValue.Contains(oRejectionItem, StringComparison.OrdinalIgnoreCase))
{
_logger.Debug("oRejectionItem match: " + oRejectionItem);
var oDbMessage = this.GetRejectionMessage(oRejectionItem);
//Jetzt müssen ggf Platzhalter ersetzt werden.
if (oRejectionItem == REFERENCES_Rejection_30003_2)
{
string oReplaceParam1 = GetReplaceText1_30003_2(pProperties);
string oReplaceParam2 = GetReplaceText2_30003_2(pProperties);
oReturnValue = oDbMessage.Replace("@REPLACE_PARAM1", oReplaceParam1);
oReturnValue = oReturnValue.Replace("@REPLACE_PARAM2", oReplaceParam2);
}
else if (oRejectionItem == REFERENCES_Rejection_30003_3)
{
string oReplaceParam1 = GetReplaceText1_30003_3(pProperties);
oReturnValue = oDbMessage.Replace("@REPLACE_PARAM1", oReplaceParam1);
}
else if (oRejectionItem == REFERENCES_Rejection_30001)
{
// TODO - BUKR?
oReturnValue = oDbMessage.Replace("@REPLACE_PARAM1", "9999");
}
else
{
oReturnValue = oDbMessage;
}
// Für alle den RejectionCode setzen
oReturnValue = oReturnValue.Replace("@REJECTION_CODE", oRejectionItem);
break;
}
}
}
_logger.Debug("Validation terminal Result message: " + oReturnValue);
return new Tuple<bool, string>(true, oReturnValue); return new Tuple<bool, string>(true, oReturnValue);
} }
@@ -390,7 +493,122 @@ namespace ZUGFeRDRESTService.Controllers
{ {
_logger.Error(e); _logger.Error(e);
return new Tuple<bool, string>(false, string.Empty); return new Tuple<bool, string>(false, string.Empty);
} }
}
private string GetReplaceText1_30003_3(List<ValidProperty> pProperties)
{
string oReplaceParam1 = string.Empty;
// BuyerOrderReferencedDocument
var itemBT13 = pProperties.Where(i => i.TableColumn == "INVOICE_REFERENCE").FirstOrDefault();
string valueBt13 = "-";
if (itemBT13 != null)
{
valueBt13 = string.IsNullOrEmpty(itemBT13.Value) ? "-" : itemBT13.Value;
}
oReplaceParam1 += "<li>[BuyerOrderReferencedDocument] (BT-13) = [" + valueBt13 + "]</li>";
// BuyerReference
var itemBT10 = pProperties.Where(i => i.TableColumn == "INVOICE_REFERENCE3").FirstOrDefault();
string valueBt10 = "-";
if (itemBT10 != null)
{
valueBt10 = string.IsNullOrEmpty(itemBT10.Value) ? "-" : itemBT10.Value;
}
oReplaceParam1 += "<li>[BuyerReference] (BT-10) = [" + valueBt10 + "]</li>";
// CostCenter
var itemBT19 = pProperties.Where(i => i.TableColumn == "INVOICE_COST_CENTER").FirstOrDefault();
string valueBt19 = "-";
if (itemBT19 != null)
{
valueBt19 = string.IsNullOrEmpty(itemBT19.Value) ? "-" : itemBT19.Value;
}
oReplaceParam1 += "<li>[CostCenter] (BT-19) = [" + valueBt19 + "]</li>";
// BuyerID
var itemBT46 = pProperties.Where(i => i.TableColumn == "INVOICE_BUYER_ID").FirstOrDefault();
string valueBt46 = "-";
if (itemBT46 != null)
{
valueBt46 = string.IsNullOrEmpty(itemBT46.Value) ? "-" : itemBT46.Value;
}
oReplaceParam1 += "<li>[BuyerTradeParty] (BT-46) = [" + valueBt46 + "]</li>";
oReplaceParam1 = "<ul>" + oReplaceParam1 + "</ul>";
_logger.Debug("oReplaceParam1-Text: " + oReplaceParam1);
return oReplaceParam1;
}
private string GetReplaceText2_30003_2(List<ValidProperty> pProperties)
{
string oReplaceParam2 = string.Empty;
// BuyerTradeParty.Name
var itemBT44 = pProperties.Where(i => i.TableColumn == "INVOICE_BUYER_NAME").FirstOrDefault();
string valueBt44 = "-";
if (itemBT44 != null)
{
valueBt44 = string.IsNullOrEmpty(itemBT44.Value) ? "-" : itemBT44.Value;
}
oReplaceParam2 += "<li>[BuyerTradeParty.Name] (BT-44) = [" + valueBt44 + "]</li>";
// BuyerTradeParty.PostalTradeAddress.LineTwo
var itemBT51 = pProperties.Where(i => i.TableColumn == "INVOICE_BUYER_ADRESS2").FirstOrDefault();
string valueBt51 = "-";
if (itemBT51 != null)
{
valueBt51 = string.IsNullOrEmpty(itemBT51.Value) ? "-" : itemBT51.Value;
}
oReplaceParam2 += "<li>[BuyerTradeParty.PostalTradeAddress.LineTwo] (BT-51) = [" + valueBt51 + "]</li>";
oReplaceParam2 = "<ul>" + oReplaceParam2 + "</ul>";
_logger.Debug("oReplaceParam2-Text: " + oReplaceParam2);
return oReplaceParam2;
}
private string GetReplaceText1_30003_2(List<ValidProperty> pProperties)
{
string oReplaceParam1 = string.Empty;
// BuyerOrderReferencedDocument
var itemBT13 = pProperties.Where(i => i.TableColumn == "INVOICE_REFERENCE").FirstOrDefault();
string valueBt13 = "-";
if (itemBT13 != null)
{
valueBt13 = string.IsNullOrEmpty(itemBT13.Value) ? "-" : itemBT13.Value;
}
oReplaceParam1 += "<li>[BuyerOrderReferencedDocument] (BT-13) = [" + valueBt13 + "]</li>";
// BuyerReference
var itemBT10 = pProperties.Where(i => i.TableColumn == "INVOICE_REFERENCE3").FirstOrDefault();
string valueBt10 = "-";
if (itemBT10 != null)
{
valueBt10 = string.IsNullOrEmpty(itemBT10.Value) ? "-" : itemBT10.Value;
}
oReplaceParam1 += "<li>[BuyerReference] (BT-10) = [" + valueBt10 + "]</li>";
// CostCenter
var itemBT19 = pProperties.Where(i => i.TableColumn == "INVOICE_COST_CENTER").FirstOrDefault();
string valueBt19 = "-";
if (itemBT19 != null)
{
valueBt19 = string.IsNullOrEmpty(itemBT19.Value) ? "-" : itemBT19.Value;
}
oReplaceParam1 += "<li>[CostCenter] (BT-19) = [" + valueBt19 + "]</li>";
oReplaceParam1 = "<ul>" + oReplaceParam1 + "</ul>";
_logger.Debug("oReplaceParam1-Text: " + oReplaceParam1);
return oReplaceParam1;
} }
public string GetMessageId() public string GetMessageId()
@@ -428,7 +646,7 @@ namespace ZUGFeRDRESTService.Controllers
{ {
itemValue = pProperty.Value; itemValue = pProperty.Value;
} }
var oParams = new SqlParameter[] var oParams = new SqlParameter[]
{ {
new SqlParameter("@REFERENCE_GUID", pMessageId), new SqlParameter("@REFERENCE_GUID", pMessageId),
@@ -449,7 +667,7 @@ namespace ZUGFeRDRESTService.Controllers
{ {
_logger.Error(ex); _logger.Error(ex);
return false; return false;
} }
} }
/// <summary> /// <summary>
@@ -463,7 +681,25 @@ namespace ZUGFeRDRESTService.Controllers
// Sprache wird man vielleicht mal auswählen können // Sprache wird man vielleicht mal auswählen können
var language = "de-DE"; var language = "de-DE";
var searchTitle = "ZUGFERD_Rejection_" + pErrorCode + "_Web"; var searchTitle = string.Empty;
if (pErrorCode.Contains("2000"))
{
searchTitle = "ZUGFERD_Rejection_" + pErrorCode + "_Web";
}
else if (pErrorCode.Contains("REFERENCES_Rejection_3000"))
{
searchTitle = pErrorCode + "_Web";
}
else if (pErrorCode.Contains("AMOUNT_CALC"))
{
searchTitle = "AMOUNT_CALC_REJECTION_Web";
}
else
{
// else-Fall muss noch geklärt werden.
searchTitle = "AMOUNT_CALC_REJECTION_Web";
}
var messageItem = _RecjectionMessageList.Where(i => i.Title.Equals(searchTitle, StringComparison.OrdinalIgnoreCase) && i.Language.Equals(language, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); var messageItem = _RecjectionMessageList.Where(i => i.Title.Equals(searchTitle, StringComparison.OrdinalIgnoreCase) && i.Language.Equals(language, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (messageItem != null) if (messageItem != null)

View File

@@ -20,11 +20,11 @@
}, },
"Zugferd": { "Zugferd": {
"AllowZugferd10": true, "AllowZugferd10": true,
"AllowZugferd2x": false, "AllowZugferd2x": true,
"AllowZugferd23x": false, "AllowZugferd23x": true,
"AllowFacturX": false, "AllowFacturX": true,
"AllowXRechnung": false, "AllowXRechnung": true,
"AllowPeppolBISBill3x": false "AllowPeppolBISBill3x": true
}, },
"GDPictureVersion": "", "GDPictureVersion": "",
"MaxFileSizeInMegabytes": 25 "MaxFileSizeInMegabytes": 25