From d049424615622eaf1648b75597e52c0d0d1c6e7f Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sun, 16 Apr 2023 22:35:23 +0200 Subject: [PATCH] Implement UID check --- Elwig/Helpers/Validator.cs | 62 +++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/Elwig/Helpers/Validator.cs b/Elwig/Helpers/Validator.cs index 599e7e9..96a51d1 100644 --- a/Elwig/Helpers/Validator.cs +++ b/Elwig/Helpers/Validator.cs @@ -247,20 +247,72 @@ namespace Elwig.Helpers { } // https://statistik.at/fileadmin/shared/QM/Standarddokumentationen/RW/std_r_land-forstw_register.pdf#page=41 - int s = 0; + int s = 0, v = 0; for (int i = 0; i < 6; i++) s += (input.Text[i] - '0') * (7 - i); - s = (11 - (s % 11)) % 10; + v = (11 - (s % 11)) % 10; - if (s != (input.Text[6] - '0')) + if (v != (input.Text[6] - '0')) return new(false, "Prüfsumme der Betriebsnummer ist falsch"); return new(true, null); } public static ValidationResult CheckUstId(TextBox input, bool required) { - // TODO - return new(true, "Not implemented yet"); + string text = ""; + int pos = input.CaretIndex; + for (int i = 0, v = 0; i < input.Text.Length; i++) { + char ch = input.Text[i]; + if (v < 2 && char.IsAscii(ch) && char.IsLetter(ch)) { + v++; + text += char.ToUpper(ch); + } else if (v >= 2 && char.IsAscii(ch) && char.IsLetterOrDigit(ch)) { + if (text.StartsWith("AT")) { + if (v == 2 && (ch == 'u' || ch == 'U')) { + v++; + text += 'U'; + } else if (v > 2 && char.IsDigit(ch)) { + v++; + text += ch; + } + } else { + v++; + text += char.ToUpper(ch); + } + } + + if (i == input.CaretIndex - 1) { + pos = text.Length; + } else if (v >= 14) { + break; + } else if (text.StartsWith("AT") && v >= 11) { + break; + } + } + + input.Text = text; + input.CaretIndex = pos; + + if (text.Length == 0) + return required ? new(false, "UID ist nicht optional") : new(true, null); + + if (text.StartsWith("AT")) { + if (text.Length != 11 || text[2] != 'U') + return new(false, "UID ist ungültig"); + + // http://www.pruefziffernberechnung.de/U/USt-IdNr.shtml + int s = 0, v = 0; + for (int i = 0; i < 8; i++) + s += ((text[i + 3] - '0') * (i % 2 + 1)).ToString().Select(ch => ch - '0').Sum(); + v = (96 - s) % 10; + + if (v != (text[10] - '0')) + return new(false, "Prüfsumme der UID ist falsch"); + } else { + return new(false, "Not implemented yet"); + } + + return new(true, null); } public static ValidationResult CheckMgNr(TextBox input, bool required, AppDbContext ctx, Member? m) {