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 System.IO;
using System.Linq;
using System.Collections.Generic;
using DigitalData.Modules.Interfaces;
using Microsoft.AspNetCore.Http;
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 System.Xml.Linq;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
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
{
@@ -26,6 +25,29 @@ namespace ZUGFeRDRESTService.Controllers
public const string ADDED_WHO = "ZUGFeRD REST Service";
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 readonly ZUGFeRDInterface _zugferd;
@@ -83,6 +105,28 @@ namespace ZUGFeRDRESTService.Controllers
_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!");
}
@@ -233,10 +277,9 @@ namespace ZUGFeRDRESTService.Controllers
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";
_logger.Info($"Responding with message: [{oMessage}]");
return new ValidationResponse()
@@ -244,10 +287,10 @@ namespace ZUGFeRDRESTService.Controllers
status = RESPONSE_OK,
message = oMessage
};
} else
}
else
{
string oMessage = oValidateResultString;
_logger.Info($"Responding with message: [{oMessage}]");
return new ValidationResponse()
@@ -256,7 +299,8 @@ namespace ZUGFeRDRESTService.Controllers
message = oMessage
};
}
};
}
}
catch (ZUGFeRDExecption ex)
{
@@ -315,6 +359,8 @@ namespace ZUGFeRDRESTService.Controllers
}
catch (ValidationException ex)
{
_logger.Error(ex);
var rejectionCodeNumber = this.GetRejectionCodeNumber(ex.ErrorCode);
// Der gesamte Ausgabetext muss anhand des ErrorCodes ermittelt werden
@@ -348,6 +394,10 @@ 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)
{
var oMessageId = GetMessageId();
@@ -377,6 +427,59 @@ namespace ZUGFeRDRESTService.Controllers
if (_database.MSSQL.ExecuteNonQuery(oCommand))
{
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);
}
@@ -393,6 +496,121 @@ namespace ZUGFeRDRESTService.Controllers
}
}
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()
{
return $"{Guid.NewGuid()}@{MESSAGEID_DOMAIN}";
@@ -463,7 +681,25 @@ namespace ZUGFeRDRESTService.Controllers
// Sprache wird man vielleicht mal auswählen können
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();
if (messageItem != null)

View File

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