From 333517fde79209285b03dbbbd204d09e71903d59 Mon Sep 17 00:00:00 2001 From: Andrei Laas Date: Tue, 10 Nov 2026 14:52:32 +0100 Subject: [PATCH] SV-Nummer: Ersatznummern mit Monat 13 sind gueltig Sind fuer ein Geburtsdatum alle laufenden Nummern vergeben, vergibt die Sozialversicherung eine Ersatznummer mit dem Monat 13 im TTMMJJ-Teil. Formular, Import und Datenbank wiesen sie als ungueltiges Datum ab. Monat 13 ist jetzt zulaessig (Tag 1 bis 31), in lib/svnr.ts und in is_valid_svnr (Migration 20261110120000). Die Pruefziffer bleibt Bedingung. Bei einer Ersatznummer entfaellt der Vergleich mit dem Geburtsdatum, der Hinweis waere immer falsch. Geprueft: lint, tsc, types:check, npm test, waechter; Migrationen auf leerer Datenbank zweimal (zweiter Lauf: nichts anzuwenden), Selbstpruefung der Migration, rauchtest.sql und vertragsangaben.sql. --- ...61110120000_svnr_ersatznummer_monat_13.sql | 99 +++++++++++++++++++ lib/svnr.ts | 13 ++- tests/unit/svnr.test.ts | 26 ++++- 3 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 db/migrations/20261110120000_svnr_ersatznummer_monat_13.sql diff --git a/db/migrations/20261110120000_svnr_ersatznummer_monat_13.sql b/db/migrations/20261110120000_svnr_ersatznummer_monat_13.sql new file mode 100644 index 0000000..b9d6d21 --- /dev/null +++ b/db/migrations/20261110120000_svnr_ersatznummer_monat_13.sql @@ -0,0 +1,99 @@ +-- Ersatznummern mit dem Monat 13 sind gültige SV-Nummern. +-- +-- Sind für ein Geburtsdatum alle laufenden Nummern vergeben, vergibt die +-- Sozialversicherung eine Ersatznummer, deren TTMMJJ-Teil den Monat 13 trägt. +-- Die Prüfziffer wird wie bei jeder anderen Nummer gerechnet und bleibt +-- Bedingung; sie fängt Zahlendreher. Der Tag liegt zwischen 1 und 31. +-- +-- Mit Geburtsdatum aufgerufen vergleicht die Funktion den TTMMJJ-Teil mit +-- ihm. Eine Ersatznummer trägt kein Geburtsdatum; bei ihr entfällt der +-- Vergleich, sonst wiese sie jede Person ab, der sie gehört. Dieselbe Regel +-- gilt in lib/svnr.ts (validateSvnr). + +create or replace function is_valid_svnr(p_svnr text, p_birth_date date default null) +returns boolean +language plpgsql +immutable +set search_path = public, pg_temp +as $$ +declare + v text := regexp_replace(coalesce(p_svnr, ''), '[\s./-]', '', 'g'); + v_weights int[] := array[3, 7, 9, 0, 5, 8, 4, 2, 1, 6]; + v_days_in_month int[] := array[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31]; + v_ersatzmonat constant int := 13; + v_sum int := 0; + v_day int; + v_month int; + v_check int; +begin + if v !~ '^\d{10}$' then return false; end if; + -- 000 is never issued as a serial. + if substr(v, 1, 3) = '000' then return false; end if; + + v_day := substr(v, 5, 2)::int; + v_month := substr(v, 7, 2)::int; + if v_month < 1 or v_month > v_ersatzmonat or v_day < 1 then return false; end if; + -- February is allowed 29 days: a two-digit year cannot tell us whether the + -- year was a leap year, so the generous bound is the correct one here. + if v_day > v_days_in_month[v_month] then return false; end if; + + for i in 1..10 loop + if i <> 4 then + v_sum := v_sum + substr(v, i, 1)::int * v_weights[i]; + end if; + end loop; + + v_check := v_sum % 11; + -- A serial whose weighted sum lands on 11 leaves no single digit to use, + -- so that serial is skipped rather than wrapped around. + if v_check = 10 then return false; end if; + if v_check <> substr(v, 4, 1)::int then return false; end if; + + if p_birth_date is not null and v_month <> v_ersatzmonat + and to_char(p_birth_date, 'DDMMYY') <> substr(v, 5, 6) then + return false; + end if; + + return true; +end; +$$; + +comment on function is_valid_svnr(text, date) is + 'Prüft eine österreichische SV-Nummer (10 Ziffern, Prüfziffer mod 11, Monat 1 bis 12 oder 13 bei Ersatznummern). Mit p_birth_date wird zusätzlich der TTMMJJ-Teil gegen das Geburtsdatum geprüft, ausser bei Ersatznummern.'; + +-- ── Selbstprüfung ───────────────────────────────────────────────────────── +do $$ +begin + -- Ersatznummer: 3·1+7·2+9·3 = 44; 5·0+8·1+4·1+2·3+1·8+6·0 = 26; 70 mod 11 = 4. + if not is_valid_svnr('1234 011380') then + raise exception 'is_valid_svnr weist eine Ersatznummer mit Monat 13 ab. Abbruch.'; + end if; + if not is_valid_svnr('1234 011380', date '1975-06-15') then + raise exception 'is_valid_svnr vergleicht eine Ersatznummer mit dem Geburtsdatum. Abbruch.'; + end if; + if is_valid_svnr('1237 011380') then + raise exception 'is_valid_svnr nimmt eine Ersatznummer mit falscher Prüfziffer an. Abbruch.'; + end if; + if is_valid_svnr('1237 011480') or is_valid_svnr('1237 321380') then + raise exception 'is_valid_svnr nimmt Monat 14 oder Tag 32 an. Abbruch.'; + end if; + -- Die übrigen Regeln bleiben. + if not is_valid_svnr('1237 010180') or not is_valid_svnr('1236 290280') then + raise exception 'is_valid_svnr weist eine gültige Nummer ab. Abbruch.'; + end if; + if is_valid_svnr('1230 010180') or is_valid_svnr('1237 310280') or is_valid_svnr('0007 010180') then + raise exception 'is_valid_svnr nimmt eine ungültige Nummer an. Abbruch.'; + end if; + if is_valid_svnr('1237 010180', date '1980-01-02') then + raise exception 'is_valid_svnr vergleicht nicht mehr mit dem Geburtsdatum. Abbruch.'; + end if; + if not exists ( + select 1 from pg_proc p join pg_namespace n on n.oid = p.pronamespace + where n.nspname = 'public' and p.proname = 'is_valid_svnr' + and p.provolatile = 'i' and not p.prosecdef + and p.proconfig @> array['search_path=public, pg_temp'] + ) then + raise exception 'is_valid_svnr hat nicht mehr immutable/search_path. Abbruch.'; + end if; +end; +$$; diff --git a/lib/svnr.ts b/lib/svnr.ts index af8ae22..c3a8c98 100644 --- a/lib/svnr.ts +++ b/lib/svnr.ts @@ -13,6 +13,11 @@ const WEIGHTS = [3, 7, 9, 0, 5, 8, 4, 2, 1, 6] as const; const CHECK_INDEX = 3; +// Sind für ein Geburtsdatum alle laufenden Nummern vergeben, vergibt die +// Sozialversicherung eine Ersatznummer mit dem Monat 13 im TTMMJJ-Teil. Sie ist +// eine gültige Nummer; die Prüfziffer wird wie bei jeder anderen gerechnet. +const ERSATZMONAT = 13; + export type SvnrError = "length" | "serial" | "checksum" | "date" | "birthDateMismatch"; const MESSAGES: Record = { @@ -105,15 +110,17 @@ export function validateSvnr(input: string, birthDate?: string | null): SvnrErro const day = Number(n.slice(4, 6)); const month = Number(n.slice(6, 8)); - if (month < 1 || month > 12 || day < 1 || day > 31) return "date"; + if (month < 1 || month > ERSATZMONAT || day < 1 || day > 31) return "date"; // Without a century the tail cannot be resolved to a real date, so the // day-of-month bound is the generous one; a supplied birthDate settles it. - if (day > [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]) return "date"; + if (month !== ERSATZMONAT && day > [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]) return "date"; const expected = svnrCheckDigit(n); if (expected === null || expected !== Number(n[CHECK_INDEX])) return "checksum"; - if (birthDate) { + // Eine Ersatznummer trägt kein Geburtsdatum, ein Vergleich ergäbe immer + // eine Abweichung. + if (birthDate && month !== ERSATZMONAT) { const [y, m, d] = birthDate.split("-"); if (d !== n.slice(4, 6) || m !== n.slice(6, 8) || y?.slice(-2) !== n.slice(8, 10)) return "birthDateMismatch"; } diff --git a/tests/unit/svnr.test.ts b/tests/unit/svnr.test.ts index 7b6e955..87afc6e 100644 --- a/tests/unit/svnr.test.ts +++ b/tests/unit/svnr.test.ts @@ -73,9 +73,10 @@ describe("validateSvnr", () => { }); it("rejects an impossible date tail", () => { - // Month 13 and day 32 never occur; the checksum is irrelevant once the + // Day 32 and month 14 never occur; the checksum is irrelevant once the // tail cannot be a date at all. - expect(validateSvnr("1237 011380")).toBe("date"); + expect(validateSvnr("1237 011480")).toBe("date"); + expect(validateSvnr("1237 001380")).toBe("date"); expect(validateSvnr("1237 320180")).toBe("date"); expect(validateSvnr("1237 310280")).toBe("date"); }); @@ -86,6 +87,27 @@ describe("validateSvnr", () => { }); }); +// Ersatznummer: Monat 13. Für "123? 011380": +// 44 (laufende Nummer) + 5·0 + 8·1 + 4·1 + 2·3 + 1·8 + 6·0 = 44 + 26 = 70 +// 70 mod 11 = 4 → 1234 011380 +describe("Ersatznummer mit Monat 13", () => { + it("nimmt sie mit richtiger Prüfziffer an", () => { + expect(validateSvnr("1234 011380")).toBeNull(); + // Tag 31 ist bei Monat 13 kein Widerspruch. + // 44 + 5·3 + 8·1 + 4·1 + 2·3 + 1·8 + 6·0 = 44 + 41 = 85; 85 mod 11 = 8 + expect(validateSvnr("1238 311380")).toBeNull(); + }); + + it("prüft die Prüfziffer wie bei jeder Nummer", () => { + expect(validateSvnr("1237 011380")).toBe("checksum"); + }); + + it("vergleicht nicht mit dem Geburtsdatum", () => { + expect(validateSvnr("1234 011380", "1980-01-01")).toBeNull(); + expect(validateSvnr("1234 011380", "1975-06-15")).toBeNull(); + }); +}); + describe("cross-check against the stored birth date", () => { it("passes when the tail matches", () => { expect(validateSvnr(VALID, "1980-01-01")).toBeNull();