Files
BeWoPlaner/Shared/Core/ValidatorExtended.cs

81 lines
2.1 KiB
C#
Raw Normal View History

2024-12-06 13:05:28 +01:00
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.GkvAbrechnung;
using BS.Shared.Exceptions;
using BS.Shared.Extensions;
using BS.Shared.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
2025-11-10 11:21:11 +01:00
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace BS.Shared.Core
{
public static class ValidatorExtended
{
2024-12-06 13:05:28 +01:00
public static void ValidateGkvAbrechnung(IGkvAbrechnung gkvAbrechnung, IEnumerable<IInvoiceBase> invoiceBases, bool testInvoicesEmpty = true)
{
string error = null;
if (testInvoicesEmpty && !invoiceBases.Any())
error = "Keine Rechnungen gefunden";
else if (!AreAllAddressSame(invoiceBases))
error = "Unterschiedliche Rechnungspositionen";
else if (GetInvalidUnitDescription(invoiceBases) is IInvoiceItem invalid)
error = $"Ungültige Rechnungsposition: {invalid.ItemDescription}:{invalid.UnitDescription}";
2025-11-12 12:15:31 +01:00
else if (GkvValidator.ValidateInvoiceBases(invoiceBases) is string rtn_error)
error = rtn_error;
2024-12-06 13:05:28 +01:00
if(error is string)
throw new GkvException(error, "Validierung");
}
2025-06-23 16:35:33 +02:00
public static bool TryValidateGkvAbrechnung(IGkvAbrechnung gkvAbrechnung, IEnumerable<IInvoiceBase> invoiceBases, out string error, bool testInvoicesEmpty = true)
2024-12-06 13:05:28 +01:00
{
2025-06-23 16:35:33 +02:00
error = "default";
2024-12-06 13:05:28 +01:00
try
{
ValidateGkvAbrechnung(gkvAbrechnung, invoiceBases, testInvoicesEmpty);
2025-06-23 16:35:33 +02:00
return true;
}
catch (GkvException gkv)
{
error = gkv.Message;
2024-12-06 13:05:28 +01:00
}
catch (Exception)
{
2025-06-23 16:35:33 +02:00
2024-12-06 13:05:28 +01:00
}
2025-06-23 16:35:33 +02:00
return false;
2024-12-06 13:05:28 +01:00
}
public static bool AreAllAddressSame(IEnumerable<IInvoiceBase> invoiceBases)
=> invoiceBases.Select(x => x.SenderAddressToString()).AreAllTheSame();
public static IInvoiceItem GetInvalidUnitDescription(IEnumerable<IInvoiceBase> invoiceBases){
foreach(var invoiceBase in invoiceBases)
{
if (invoiceBase.IInvoiceItems is null && !invoiceBase.IInvoiceItems.Any())
2025-06-23 16:35:33 +02:00
continue;
foreach (var invoiceitem in invoiceBase.IInvoiceItems)
{
// GPos. 123456
// 0123456
var desc = invoiceitem.UnitDescription;
if (desc.Length < 8)
return invoiceitem;
}
}
return null;
}
2025-11-10 11:21:11 +01:00
}
}