using BS.Shared.Exceptions; using BS.Shared.Extensions; using BS.Shared.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace BS.Shared.Core { public static class ValidatorExtended { public static void ValidateGkvAbrechnung(IGkvAbrechnung gkvAbrechnung, IEnumerable 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}"; else if (GkvValidator.ValidateInvoiceBases(invoiceBases) is string rtn_error) error = rtn_error; if(error is string) throw new GkvException(error, "Validierung"); } public static bool TryValidateGkvAbrechnung(IGkvAbrechnung gkvAbrechnung, IEnumerable invoiceBases, out string error, bool testInvoicesEmpty = true) { error = "default"; try { ValidateGkvAbrechnung(gkvAbrechnung, invoiceBases, testInvoicesEmpty); return true; } catch (GkvException gkv) { error = gkv.Message; } catch (Exception) { } return false; } public static bool AreAllAddressSame(IEnumerable invoiceBases) => invoiceBases.Select(x => x.SenderAddressToString()).AreAllTheSame(); public static IInvoiceItem GetInvalidUnitDescription(IEnumerable invoiceBases){ foreach(var invoiceBase in invoiceBases) { if (invoiceBase.IInvoiceItems is null && !invoiceBase.IInvoiceItems.Any()) continue; foreach (var invoiceitem in invoiceBase.IInvoiceItems) { // GPos. 123456 // 0123456 var desc = invoiceitem.UnitDescription; if (desc.Length < 8) return invoiceitem; } } return null; } } }