Neue Art zwischen Gesellschaft und Bereich (CEO, CFO, ...). Damit sie sich auf die bestehenden Einheiten setzen laesst, kann die Art einer Einheit jetzt geaendert werden: change_org_unit_type mit Eintrag im Audit-Log, Stift in der Seitenleiste der Einheit, Spalte "Art" im Import. Die Gesellschaft behaelt ihre Art, keine andere Einheit kann sie werden. Uebersicht, Organigramm-Druck und die Spalte "Bereich" in Berichten zaehlen den Vorstandsbereich wie einen Bereich (istBereich in lib/org-art.ts). Sonst fielen eine als Vorstandsbereich gefuehrte Einheit und alle Personen darunter aus diesen Ansichten heraus. Geprueft: lint, tsc, types:check, 1024 Unit-Tests, Integrationstests Austausch, Waechter; alle Migrationen auf leerer Datenbank (zweiter Lauf nichts anzuwenden), Rauchtest 35/35, vertragsangaben.sql.
1172 lines
65 KiB
PL/PgSQL
1172 lines
65 KiB
PL/PgSQL
-- Rauchtest: der Lebenszyklus, einmal wirklich ausgeführt
|
|
--
|
|
-- psql --dbname=<MIGRATE_DATABASE_URL> -f db/tests/rauchtest.sql
|
|
--
|
|
-- ═══ Warum es das gibt ════════════════════════════════════════════
|
|
--
|
|
-- Ein Fehler, der erst beim Aufruf einer Funktion entsteht — etwa ein
|
|
-- fehlender Umwandlungsschritt beim Status in rehire_employee —, fällt keiner
|
|
-- anderen Prüfung der Anwendung auf: die Selbstprüfungen der Migrationen
|
|
-- lesen den **Text** der Funktionen, die Unit-Tests laufen ohne Datenbank,
|
|
-- und die Integrationstests haben keinen Bestand, gegen den sie liefen.
|
|
--
|
|
-- Diese Datei schliesst genau diese Lücke: sie **ruft** die Funktionen auf,
|
|
-- gegen die echte Datenbank, in der Reihenfolge des Lebenszyklus.
|
|
--
|
|
-- ═══ Zwei Eigenschaften, ohne die es gefährlich wäre ══════════════
|
|
--
|
|
-- 1. **Es wird zurückgerollt.** Alles läuft in einer Transaktion, die am Ende
|
|
-- verworfen wird. Nach dem Lauf steht kein Testmensch, keine Testplanstelle
|
|
-- und keine Testeinheit in der Datenbank, und keine reale Person bleibt
|
|
-- auf einer Testplanstelle zurück.
|
|
--
|
|
-- 2. **Es läuft als Anwendungsrolle.** psql verbindet sich als Superuser, und
|
|
-- der geht an jeder Zeilenschutz-Regel und an require_hr_admin() vorbei.
|
|
-- Ein Lauf als postgres würde beweisen, dass die Funktionen für niemanden
|
|
-- funktionieren, der sie benutzt.
|
|
--
|
|
-- Fehlschläge brechen mit einer Meldung ab, die den Schritt nennt. Kommt der
|
|
-- Lauf bis „Rauchtest bestanden" durch, ist der Lebenszyklus gegangen worden.
|
|
|
|
\set ON_ERROR_STOP on
|
|
|
|
begin;
|
|
|
|
select set_config(
|
|
'app.user_id',
|
|
(select p.id::text from profiles p where p.role = 'hr' and p.is_active limit 1),
|
|
true
|
|
) as angemeldet_als;
|
|
|
|
set local role alpenwerk_app;
|
|
|
|
do $rauchtest$
|
|
declare
|
|
v_wurzel uuid;
|
|
v_einheit uuid;
|
|
v_p1 uuid; v_p2 uuid; v_p3 uuid; v_p4 uuid; v_p5 uuid; v_p6 uuid; v_p7 uuid; v_p8 uuid; v_p9 uuid;
|
|
v_a uuid; v_b uuid; v_c uuid; v_d uuid; v_e uuid;
|
|
v_spaet uuid;
|
|
v_notiz uuid;
|
|
v_ort uuid;
|
|
v_nr int;
|
|
v_status text;
|
|
v_stelle uuid;
|
|
v_offen int;
|
|
v_meldung text;
|
|
v_ging boolean;
|
|
v_titel text;
|
|
v_zu uuid; v_px uuid; v_py uuid; v_job uuid; v_pa uuid; v_x uuid;
|
|
v_psnr text;
|
|
begin
|
|
select id into v_wurzel from org_units where parent_id is null;
|
|
select id into v_ort from locations order by name limit 1;
|
|
select coalesce(max(personnel_number), 0) + 1 into v_nr from employees;
|
|
if v_wurzel is null or v_ort is null then
|
|
raise exception 'Kein Bestand: es fehlt die oberste Einheit oder ein Standort.';
|
|
end if;
|
|
|
|
-- ── Testdaten ───────────────────────────────────────────────────
|
|
v_einheit := create_org_unit(jsonb_build_object(
|
|
'parent_id', v_wurzel, 'org_number', 'RAUCHTEST', 'name', 'Rauchtest',
|
|
'unit_type', 'Team', 'valid_from', (current_date - 30)::text));
|
|
|
|
v_p1 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Eins', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_p2 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Zwei', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_p3 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Drei', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_p4 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Vier', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
raise notice '1/35 Einheit und vier Planstellen angelegt';
|
|
|
|
-- ── Einstellung ─────────────────────────────────────────────────
|
|
v_a := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr, 'first_name', 'Rauch', 'last_name', 'Test-A',
|
|
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_p1, 'entry_date', current_date::text));
|
|
raise notice '2/35 Einstellung auf Planstelle 1';
|
|
|
|
-- ── Versetzung am selben Tag (20260929180000) ───────────────────
|
|
-- Die Besetzung begann heute; sie auf heute zu schliessen wäre ein
|
|
-- Intervall von null Tagen, das chk_assignment_range abweisen würde.
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_a, 'target_position_id', v_p2, 'effective_date', current_date::text));
|
|
|
|
select position_id into v_stelle from position_assignments
|
|
where employee_id = v_a and valid_to is null;
|
|
if v_stelle is distinct from v_p2 then
|
|
raise exception 'Nach der Versetzung sitzt die Person nicht auf Planstelle 2.';
|
|
end if;
|
|
-- Die Historie nennt Nummer und Bezeichnung der Planstelle (20261029200000).
|
|
if not exists (select 1 from employee_history
|
|
where employee_id = v_a and event_type = 'Versetzung'
|
|
and description = 'Versetzung auf Planstelle '
|
|
|| (select position_number from om_positions where id = v_p2) || ' (Rauchtest Zwei)') then
|
|
raise exception 'Die Versetzung steht ohne Bezeichnung der Planstelle in der Historie.';
|
|
end if;
|
|
raise notice '3/35 Versetzung am selben Tag, Planstelle mit Bezeichnung in der Historie';
|
|
|
|
-- ── Beförderung mit Planstellenwechsel, ebenfalls heute ─────────
|
|
perform promote_employee(jsonb_build_object(
|
|
'employee_id', v_a, 'effective_date', current_date::text,
|
|
'new_title', 'Rauchtest Leitung', 'new_paygrade', 'HG15',
|
|
'target_position_id', v_p3));
|
|
|
|
select position_id into v_stelle from position_assignments
|
|
where employee_id = v_a and valid_to is null;
|
|
if v_stelle is distinct from v_p3 then
|
|
raise exception 'Nach der Beförderung sitzt die Person nicht auf Planstelle 3.';
|
|
end if;
|
|
if (select paygrade::text from employees where id = v_a) <> 'HG15' then
|
|
raise exception 'Die Beförderung hat den Hay-Grade nicht gesetzt.';
|
|
end if;
|
|
|
|
-- Die Bezeichnung gehört zur Planstelle: dort lesen sie Akte, Organigramm
|
|
-- und die Exporte. Stünde der neue Titel allein an der Person, wäre er
|
|
-- nirgends zu sehen.
|
|
select j.title into v_titel
|
|
from om_positions p join jobs j on j.id = p.job_id where p.id = v_p3;
|
|
if v_titel <> 'Rauchtest Leitung' then
|
|
raise exception 'Die Zielplanstelle heisst nach der Beförderung „%", erwartet „Rauchtest Leitung".', v_titel;
|
|
end if;
|
|
raise notice '4/35 Beförderung am selben Tag: Hay-Grade und Planstellenbezeichnung';
|
|
|
|
-- Dieselbe Planstelle, nur ein neuer Name.
|
|
perform promote_employee(jsonb_build_object(
|
|
'employee_id', v_a, 'effective_date', current_date::text,
|
|
'new_title', 'Rauchtest Leitung Senior', 'new_paygrade', 'HG16'));
|
|
|
|
select j.title into v_titel
|
|
from om_positions p join jobs j on j.id = p.job_id where p.id = v_p3;
|
|
if v_titel <> 'Rauchtest Leitung Senior' then
|
|
raise exception 'Ohne Stellenwechsel heisst die Planstelle „%", erwartet „Rauchtest Leitung Senior".', v_titel;
|
|
end if;
|
|
|
|
-- Der Katalogeintrag der alten Bezeichnung bleibt stehen: an ihm können
|
|
-- andere Planstellen hängen. Umbenannt wird die Stelle, nicht der Katalog.
|
|
if not exists (select 1 from jobs where title = 'Rauchtest Leitung') then
|
|
raise exception 'Der alte Katalogeintrag wurde umbenannt statt die Planstelle umgehängt.';
|
|
end if;
|
|
raise notice '4b/35 Beförderung ohne Stellenwechsel benennt dieselbe Planstelle um';
|
|
|
|
-- ── Vorgemerkte Planstelle (20260929200000) ─────────────────────
|
|
-- Eine Versetzung in die Zukunft belegt die Zielstelle, obwohl dort noch
|
|
-- keine Besetzung steht. Wer sie vorher besetzt, bringt den Nachtlauf zum
|
|
-- Stehen — deshalb wird das abgewiesen.
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_a, 'target_position_id', v_p4, 'effective_date', (current_date + 10)::text));
|
|
|
|
-- Der Fehlschlag wird **ausserhalb** des Blocks ausgewertet. Ein `raise`
|
|
-- innerhalb landet im eigenen exception-Zweig — und enthält seine Meldung
|
|
-- das Wort „vorgemerkte", bestünde der Schritt auch dann, wenn die
|
|
-- Migration gar nicht angewendet ist. Ein Test, der sich selbst
|
|
-- bestätigt, beweist nichts.
|
|
v_ging := false;
|
|
begin
|
|
perform hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 1, 'first_name', 'Rauch', 'last_name', 'Test-B',
|
|
'gender', 'm', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_p4, 'entry_date', current_date::text));
|
|
v_ging := true;
|
|
exception
|
|
when others then v_meldung := SQLERRM;
|
|
end;
|
|
|
|
if v_ging then
|
|
raise exception 'Die vorgemerkte Planstelle liess sich besetzen — die Pruefung greift nicht.';
|
|
end if;
|
|
if v_meldung not like '%bereits eine Versetzung vorgemerkt%' then
|
|
raise exception 'Erwartet wurde die Meldung zur Vormerkung, kam aber: %', v_meldung;
|
|
end if;
|
|
raise notice '5/35 Vorgemerkte Planstelle wird abgewiesen';
|
|
|
|
-- Die eigene Vormerkung darf nicht im Weg stehen: dieselbe Versetzung noch
|
|
-- einmal zu erfassen ist eine Berichtigung.
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_a, 'target_position_id', v_p4, 'effective_date', (current_date + 20)::text));
|
|
raise notice '6/35 Die eigene Vormerkung steht nicht im Weg';
|
|
|
|
-- ── Austritt am Tag einer Versetzung (20261029180000) ───────────
|
|
-- v_a ist heute versetzt und befördert worden. Ein Austritt heute
|
|
-- widerspräche dem; erst wäre der Vorgang abzubrechen.
|
|
v_ging := false;
|
|
begin
|
|
perform terminate_employee(jsonb_build_object(
|
|
'employee_id', v_a, 'exit_date', current_date::text,
|
|
'exit_reason', 'Einvernehmliche Auflösung', 'austrittsart', 'freiwillig'));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%erst möglich, wenn dieser Vorgang abgebrochen%' then
|
|
raise exception 'Ein Austritt am Tag einer Versetzung liess sich erfassen (%).', v_meldung;
|
|
end if;
|
|
-- v_a räumt Planstelle 3 für die folgenden Schritte; Planstelle 4 hat sie
|
|
-- sich selbst vorgemerkt.
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_a, 'target_position_id', v_p4, 'effective_date', current_date::text));
|
|
|
|
-- ── Austritt am selben Tag (20260929220000) ─────────────────────
|
|
-- Eine heute begonnene Besetzung lässt sich nicht auf heute schliessen
|
|
-- (chk_assignment_range); sie wird entfernt, die Planstelle ist frei.
|
|
v_pa := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Kurz', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_x := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 14, 'first_name', 'Rauch', 'last_name', 'Test-K',
|
|
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_pa, 'entry_date', current_date::text));
|
|
perform terminate_employee(jsonb_build_object(
|
|
'employee_id', v_x, 'exit_date', current_date::text,
|
|
'exit_reason', 'Einvernehmliche Auflösung', 'austrittsart', 'freiwillig'));
|
|
|
|
select count(*) into v_offen from position_assignments
|
|
where employee_id = v_x and valid_to is null;
|
|
if v_offen <> 0 then
|
|
raise exception 'Nach dem Austritt ist die Planstelle nicht frei.';
|
|
end if;
|
|
raise notice '7/35 Austritt am Tag einer Versetzung abgewiesen; Austritt am selben Tag, Planstelle frei';
|
|
|
|
-- ── Wiedereintritt (20260929160000) ─────────────────────────────
|
|
perform rehire_employee(jsonb_build_object(
|
|
'employee_id', v_x, 'rehire_date', current_date::text, 'position_id', v_p1,
|
|
'first_name', 'Rauch', 'last_name', 'Test-K', 'gender', 'w',
|
|
'birth_date', '1990-01-01', 'nationality', 'Österreich', 'location_id', v_ort));
|
|
|
|
select status::text into v_status from employees where id = v_x;
|
|
if v_status <> 'Aktiv' then
|
|
raise exception 'Nach dem Wiedereintritt steht der Status auf %, erwartet Aktiv.', v_status;
|
|
end if;
|
|
select position_id into v_stelle from position_assignments
|
|
where employee_id = v_x and valid_to is null;
|
|
if v_stelle is distinct from v_p1 then
|
|
raise exception 'Nach dem Wiedereintritt sitzt die Person nicht auf Planstelle 1.';
|
|
end if;
|
|
raise notice '8/35 Wiedereintritt';
|
|
|
|
-- ── Rückdatierte Versetzung (20261013140000) ────────────────────
|
|
--
|
|
-- Eine Person darf nie zweimal auf derselben Planstelle sitzen. Hergang:
|
|
-- eine Versetzung auf heute schliesst die laufende Besetzung auf heute;
|
|
-- eine zweite, auf einen vergangenen Tag datierte Versetzung darf danach
|
|
-- nicht nur aufräumen, was **offen** ist — sonst bleibt die eben
|
|
-- geschlossene Zeile stehen und überlappt mit der neuen.
|
|
--
|
|
-- Hier gebraucht wird jemand, dessen Besetzung in der Vergangenheit
|
|
-- beginnt — bei einer heute begonnenen greift schon das delete, und der
|
|
-- Fehler zeigte sich nicht.
|
|
v_b := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 2, 'first_name', 'Rauch', 'last_name', 'Test-C',
|
|
'gender', 'm', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_p2, 'entry_date', (current_date - 10)::text));
|
|
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_b, 'target_position_id', v_p3, 'effective_date', current_date::text));
|
|
|
|
-- Zurück auf die alte Stelle, datiert auf den Eintrittstag — die
|
|
-- Berichtigung einer versehentlich erfassten Versetzung.
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_b, 'target_position_id', v_p2, 'effective_date', (current_date - 10)::text));
|
|
|
|
select count(*) into v_offen from position_assignments where employee_id = v_b;
|
|
if v_offen <> 1 then
|
|
raise exception 'Nach der rückdatierten Versetzung stehen % Besetzungen, erwartet genau eine.', v_offen;
|
|
end if;
|
|
select position_id into v_stelle from position_assignments
|
|
where employee_id = v_b and valid_to is null;
|
|
if v_stelle is distinct from v_p2 then
|
|
raise exception 'Nach der rückdatierten Versetzung sitzt die Person nicht auf Planstelle 2.';
|
|
end if;
|
|
raise notice '9/35 Rückdatierte Versetzung lässt keine zweite Besetzung stehen';
|
|
|
|
-- ── Versetzung bei datiertem Austritt (20261013140000) ──────────
|
|
--
|
|
-- Die zweite Hälfte derselben Regel. Ein Austritt in der Zukunft
|
|
-- befristet die Besetzung auf den Austrittstag. Suchte eine spätere
|
|
-- Versetzung nur nach „valid_to is null", träfe sie nichts und fügte
|
|
-- trotzdem ein — zwei gleichzeitige Besetzungen. Und die neue liefe
|
|
-- unbefristet weiter, obwohl die Person das Haus verlässt.
|
|
perform terminate_employee(jsonb_build_object(
|
|
'employee_id', v_b, 'exit_date', (current_date + 20)::text,
|
|
'exit_reason', 'Einvernehmliche Auflösung', 'austrittsart', 'freiwillig'));
|
|
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_b, 'target_position_id', v_p3, 'effective_date', current_date::text));
|
|
|
|
select count(*) into v_offen from position_assignments where employee_id = v_b;
|
|
if v_offen <> 2 then
|
|
raise exception 'Nach der Versetzung stehen % Besetzungen, erwartet zwei aufeinanderfolgende.', v_offen;
|
|
end if;
|
|
|
|
-- Keine offene Besetzung mehr: die neue endet mit der Person.
|
|
select count(*) into v_offen from position_assignments
|
|
where employee_id = v_b and valid_to is null;
|
|
if v_offen <> 0 then
|
|
raise exception 'Die neue Besetzung läuft unbefristet weiter, obwohl ein Austritt datiert ist.';
|
|
end if;
|
|
|
|
select count(*) into v_offen from position_assignments
|
|
where employee_id = v_b and position_id = v_p3
|
|
and valid_from = current_date and valid_to = current_date + 20;
|
|
if v_offen <> 1 then
|
|
raise exception 'Die neue Besetzung endet nicht am Austrittstag.';
|
|
end if;
|
|
raise notice '10/35 Versetzung bei datiertem Austritt: kein Überlappen, kein Überleben';
|
|
|
|
-- ── Änderung vor dem Eintritt (20261018100000) ──────────────────
|
|
--
|
|
-- Wer noch nicht eingetreten ist, bekommt eine Korrektur sofort in die
|
|
-- Stammdaten. In der Historie steht sie mit dem Eintrittstag, und wandert
|
|
-- der Eintritt, wandert sie mit.
|
|
v_p5 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Fünf', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_c := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 3, 'first_name', 'Rauch', 'last_name', 'Test-D',
|
|
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_p5, 'entry_date', (current_date + 30)::text));
|
|
|
|
perform change_employee_data(jsonb_build_object(
|
|
'employee_id', v_c, 'effective_date', current_date::text,
|
|
'person', jsonb_build_object('city', 'Rauchhausen')));
|
|
|
|
if (select city from employees where id = v_c) is distinct from 'Rauchhausen' then
|
|
raise exception 'Vor dem Eintritt wirkt die Änderung nicht sofort.';
|
|
end if;
|
|
if exists (select 1 from pending_org_changes where employee_id = v_c and status = 'pending') then
|
|
raise exception 'Die Änderung vor dem Eintritt wurde vorgemerkt statt geschrieben.';
|
|
end if;
|
|
if not exists (select 1 from employee_history
|
|
where employee_id = v_c and event_type = 'Stammdatenänderung'
|
|
and event_date = current_date + 30 and pending_id is null) then
|
|
raise exception 'Die Änderung vor dem Eintritt steht nicht mit dem Eintrittstag in der Historie.';
|
|
end if;
|
|
|
|
perform update_history_entry(jsonb_build_object(
|
|
'history_id', (select id from employee_history where employee_id = v_c and event_type = 'Eintritt'),
|
|
'event_date', (current_date + 40)::text));
|
|
|
|
if not exists (select 1 from employee_history
|
|
where employee_id = v_c and event_type = 'Stammdatenänderung'
|
|
and event_date = current_date + 40) then
|
|
raise exception 'Mit dem Eintritt ist die Änderung davor nicht mitgewandert.';
|
|
end if;
|
|
raise notice '11/35 Änderung vor dem Eintritt wirkt sofort und wandert mit dem Eintritt';
|
|
|
|
-- ── Abwesenheit mit geplanter Rückkehr löschen (20261019100000) ─
|
|
--
|
|
-- Eine irrtümlich erfasste Langzeitabwesenheit, deren Rückkehr schon
|
|
-- vorgemerkt ist: die Abwesenheit lässt sich löschen, die Vormerkung fällt
|
|
-- mit, und die Person ist wieder aktiv.
|
|
v_p6 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Sechs', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_d := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 4, 'first_name', 'Rauch', 'last_name', 'Test-E',
|
|
'gender', 'm', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_p6, 'entry_date', (current_date - 20)::text));
|
|
|
|
perform start_karenz(jsonb_build_object(
|
|
'employee_id', v_d, 'karenz_start_date', (current_date - 5)::text,
|
|
'planned_return_date', (current_date + 365)::text, 'absence_type', 'Bildungskarenz'));
|
|
-- Die Rückkehr wird mit der Abwesenheit vorgemerkt (20261019220000).
|
|
if not exists (select 1 from employee_history h join pending_org_changes q on q.id = h.pending_id
|
|
where h.employee_id = v_d and h.event_type = 'Rückkehr' and q.status = 'pending'
|
|
and h.event_date = current_date + 365) then
|
|
raise exception 'Mit der Abwesenheit wurde keine Rückkehr vorgemerkt.';
|
|
end if;
|
|
|
|
perform delete_history_entry(jsonb_build_object(
|
|
'history_id', (select id from employee_history where employee_id = v_d and event_type = 'Karenz'),
|
|
'employee_id', v_d));
|
|
|
|
if exists (select 1 from employee_history where employee_id = v_d and event_type in ('Karenz', 'Rückkehr')) then
|
|
raise exception 'Nach dem Löschen stehen Abwesenheit oder Rückkehr noch in der Historie.';
|
|
end if;
|
|
if exists (select 1 from pending_org_changes where employee_id = v_d and status = 'pending') then
|
|
raise exception 'Die vorgemerkte Rückkehr läuft nach dem Löschen der Abwesenheit weiter.';
|
|
end if;
|
|
select status::text into v_status from employees where id = v_d;
|
|
if v_status <> 'Aktiv' then
|
|
raise exception 'Nach dem Löschen der Abwesenheit steht der Status auf %, erwartet Aktiv.', v_status;
|
|
end if;
|
|
raise notice '12/35 Abwesenheit mit geplanter Rückkehr lässt sich löschen';
|
|
|
|
-- ── Zweite Versetzung zum selben Tag (20261019110000) ───────────
|
|
--
|
|
-- Der Lauf übernähme beide nacheinander; welche Planstelle gilt, hinge an
|
|
-- der Reihenfolge der Erfassung. Deshalb abgewiesen, mit dem Hinweis, die
|
|
-- vorgemerkte zuerst abzubrechen.
|
|
v_p7 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Sieben', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_d, 'target_position_id', v_p7, 'effective_date', (current_date + 5)::text));
|
|
|
|
v_ging := false;
|
|
begin
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_d, 'target_position_id', v_p1, 'effective_date', (current_date + 5)::text));
|
|
v_ging := true;
|
|
exception
|
|
when others then v_meldung := SQLERRM;
|
|
end;
|
|
|
|
if v_ging then
|
|
raise exception 'Eine zweite Versetzung zum selben Tag liess sich vormerken.';
|
|
end if;
|
|
if v_meldung not like '%bereits eine Versetzung zum%' then
|
|
raise exception 'Erwartet wurde die Meldung zur zweiten Versetzung, kam aber: %', v_meldung;
|
|
end if;
|
|
|
|
-- Die geplante Versetzung kennt ihre Vormerkung (20261019120000) und lässt
|
|
-- sich darüber abbrechen.
|
|
if not exists (select 1 from employee_history
|
|
where employee_id = v_d and event_type = 'Versetzung' and pending_id is not null) then
|
|
raise exception 'Die geplante Versetzung steht ohne Verweis auf ihre Vormerkung in der Historie.';
|
|
end if;
|
|
perform delete_history_entry(jsonb_build_object(
|
|
'history_id', (select id from employee_history where employee_id = v_d and event_type = 'Versetzung'),
|
|
'employee_id', v_d));
|
|
if exists (select 1 from pending_org_changes where employee_id = v_d and status = 'pending') then
|
|
raise exception 'Nach dem Abbrechen ist die Versetzung weiter vorgemerkt.';
|
|
end if;
|
|
raise notice '13/35 Zweite Versetzung zum selben Tag abgewiesen, die erste lässt sich abbrechen';
|
|
|
|
-- ── Notiz löschen (20261019130000) ──────────────────────────────
|
|
-- Die eigene lässt sich löschen; das Protokoll behält den Text.
|
|
v_notiz := add_employee_note(jsonb_build_object(
|
|
'employee_id', v_d, 'category', 'Allgemein', 'note_text', 'Rauchtest-Notiz'));
|
|
perform delete_employee_note(jsonb_build_object('note_id', v_notiz));
|
|
if exists (select 1 from employee_notes where id = v_notiz) then
|
|
raise exception 'Die eigene Notiz liess sich nicht löschen.';
|
|
end if;
|
|
if not exists (select 1 from audit_log where action = 'HR-Notiz gelöscht' and details like '%Rauchtest-Notiz%') then
|
|
raise exception 'Das Löschen der Notiz steht nicht im Protokoll.';
|
|
end if;
|
|
raise notice '14/35 Eigene Notiz lässt sich löschen, das Protokoll behält sie';
|
|
|
|
-- ── Austritt zurücknehmen (20261019140000) ──────────────────────
|
|
-- v_b hat seit Schritt 10 einen datierten Austritt; die Besetzung auf
|
|
-- Planstelle 3 endet dort. Nach der Rücknahme läuft sie wieder offen.
|
|
perform austritt_zuruecknehmen(jsonb_build_object('employee_id', v_b));
|
|
if (select exit_date from employees where id = v_b) is not null then
|
|
raise exception 'Nach der Rücknahme steht noch ein Austrittsdatum.';
|
|
end if;
|
|
if not exists (select 1 from position_assignments where employee_id = v_b and position_id = v_p3 and valid_to is null) then
|
|
raise exception 'Nach der Rücknahme läuft die Besetzung nicht wieder offen.';
|
|
end if;
|
|
if exists (select 1 from employee_history where employee_id = v_b and event_type = 'Austritt') then
|
|
raise exception 'Nach der Rücknahme steht der Austritt noch in der Historie.';
|
|
end if;
|
|
raise notice '15/35 Austritt zurückgenommen, Besetzung läuft wieder';
|
|
|
|
-- ── Letzte Versetzung zurücknehmen (20261019140000) ─────────────
|
|
v_p8 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Acht', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_p9 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Neun', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_e := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 5, 'first_name', 'Rauch', 'last_name', 'Test-F',
|
|
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_p8, 'entry_date', (current_date - 10)::text));
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_e, 'target_position_id', v_p9, 'effective_date', (current_date - 5)::text));
|
|
|
|
perform delete_history_entry(jsonb_build_object(
|
|
'history_id', (select id from employee_history where employee_id = v_e and event_type = 'Versetzung')));
|
|
|
|
if not exists (select 1 from position_assignments where employee_id = v_e and position_id = v_p8 and valid_to is null) then
|
|
raise exception 'Nach der Rücknahme sitzt die Person nicht wieder auf ihrer vorherigen Planstelle.';
|
|
end if;
|
|
if exists (select 1 from position_assignments where position_id = v_p9) then
|
|
raise exception 'Nach der Rücknahme steht auf der Zielplanstelle noch eine Besetzung.';
|
|
end if;
|
|
raise notice '16/35 Letzte Versetzung zurückgenommen, vorherige Planstelle wieder besetzt';
|
|
|
|
-- ── Planstelle beim Eintritt berichtigen (20261019140000) ───────
|
|
-- v_c (Schritt 11) ist noch nicht eingetreten und nie versetzt worden.
|
|
perform eintritt_planstelle_berichtigen(jsonb_build_object('employee_id', v_c, 'position_id', v_p9));
|
|
if not exists (select 1 from position_assignments where employee_id = v_c and position_id = v_p9) then
|
|
raise exception 'Die Planstelle beim Eintritt wurde nicht berichtigt.';
|
|
end if;
|
|
if exists (select 1 from position_assignments where employee_id = v_c and position_id = v_p5) then
|
|
raise exception 'Nach der Berichtigung steht die Person auch noch auf der alten Planstelle.';
|
|
end if;
|
|
if exists (select 1 from employee_history where employee_id = v_c and event_type = 'Versetzung') then
|
|
raise exception 'Die Berichtigung hat eine Versetzung hinterlassen.';
|
|
end if;
|
|
raise notice '17/35 Planstelle beim Eintritt berichtigt, ohne Versetzung';
|
|
|
|
-- ── Planstelle nicht vor ihrer Einheit (20261019150000) ─────────
|
|
v_spaet := create_org_unit(jsonb_build_object(
|
|
'parent_id', v_wurzel, 'org_number', 'RAUCHTEST-SPAET', 'name', 'Rauchtest später',
|
|
'unit_type', 'Team', 'valid_from', (current_date + 30)::text));
|
|
v_ging := false;
|
|
begin
|
|
perform create_position(jsonb_build_object('org_unit_id', v_spaet,
|
|
'job_title', 'Rauchtest zu früh', 'is_chief', false, 'valid_from', current_date::text));
|
|
v_ging := true;
|
|
exception
|
|
when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging then
|
|
raise exception 'Eine Planstelle liess sich vor ihrer Einheit anlegen.';
|
|
end if;
|
|
if v_meldung not like '%besteht erst ab%' then
|
|
raise exception 'Erwartet wurde die Meldung zur Einheit, kam aber: %', v_meldung;
|
|
end if;
|
|
raise notice '18/35 Planstelle vor ihrer Einheit wird abgewiesen';
|
|
|
|
-- ── Anschliessende Abwesenheit (20261019220000) ─────────────────
|
|
-- v_e (Schritt 16) geht in Wochenhilfe; eine Elternkarenz wird
|
|
-- anschliessend geplant. Vor dem Ende der Wochenhilfe wird sie abgewiesen.
|
|
perform start_karenz(jsonb_build_object(
|
|
'employee_id', v_e, 'karenz_start_date', (current_date - 2)::text,
|
|
'planned_return_date', (current_date + 60)::text, 'absence_type', 'Wochenhilfe (Mutterschutz)',
|
|
'geburtstermin_voraussichtlich', (current_date + 20)::text));
|
|
if (select geburtstermin_voraussichtlich from employees where id = v_e) is distinct from current_date + 20 then
|
|
raise exception 'Der voraussichtliche Geburtstermin steht nicht an der Person.';
|
|
end if;
|
|
|
|
v_ging := false;
|
|
begin
|
|
perform start_karenz(jsonb_build_object(
|
|
'employee_id', v_e, 'karenz_start_date', (current_date + 30)::text,
|
|
'planned_return_date', (current_date + 400)::text, 'absence_type', 'Elternkarenz (inkl. Väterkarenz)'));
|
|
v_ging := true;
|
|
exception
|
|
when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging then
|
|
raise exception 'Eine zweite Abwesenheit liess sich vor dem Ende der ersten planen.';
|
|
end if;
|
|
if v_meldung not like '%frühestens am geplanten Ende der bestehenden%' then
|
|
raise exception 'Erwartet wurde die Meldung zur anschliessenden Abwesenheit, kam aber: %', v_meldung;
|
|
end if;
|
|
|
|
perform start_karenz(jsonb_build_object(
|
|
'employee_id', v_e, 'karenz_start_date', (current_date + 60)::text,
|
|
'planned_return_date', (current_date + 400)::text, 'absence_type', 'Elternkarenz (inkl. Väterkarenz)'));
|
|
if (select karenz_start_date from employees where id = v_e) <> current_date - 2
|
|
or (select absence_type from employees where id = v_e) <> 'Wochenhilfe (Mutterschutz)' then
|
|
raise exception 'Die anschliessende Abwesenheit hat die laufende an der Person überschrieben.';
|
|
end if;
|
|
if (select count(*) from pending_org_changes
|
|
where employee_id = v_e and status = 'pending' and change_type = 'karenz_return') <> 2 then
|
|
raise exception 'Erwartet wurden zwei vorgemerkte Rückkehren, eine je Abwesenheit.';
|
|
end if;
|
|
raise notice '19/35 Anschliessende Abwesenheit frühestens am Ende der laufenden';
|
|
|
|
-- ── Rückkehr anpassen ───────────────────────────────────────────
|
|
-- Über den Beginn der anschliessenden hinaus geht nicht; früher schon. Die
|
|
-- Rückkehr der anschliessenden bleibt unberührt.
|
|
v_ging := false;
|
|
begin
|
|
perform rueckkehr_anpassen(jsonb_build_object(
|
|
'employee_id', v_e, 'return_date', (current_date + 90)::text, 'employment_mode', 'unverändert'));
|
|
v_ging := true;
|
|
exception
|
|
when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%nach dem Beginn der anschliessend geplanten Abwesenheit%' then
|
|
raise exception 'Die Rückkehr liess sich hinter die anschliessende Abwesenheit schieben (%).', v_meldung;
|
|
end if;
|
|
|
|
perform rueckkehr_anpassen(jsonb_build_object(
|
|
'employee_id', v_e, 'return_date', (current_date + 50)::text, 'employment_mode', 'unverändert',
|
|
'geburtstermin_tatsaechlich', (current_date + 18)::text));
|
|
if not exists (select 1 from pending_org_changes where employee_id = v_e and status = 'pending'
|
|
and change_type = 'karenz_return' and effective_date = current_date + 50) then
|
|
raise exception 'Die angepasste Rückkehr ist nicht vorgemerkt.';
|
|
end if;
|
|
if exists (select 1 from pending_org_changes where employee_id = v_e and status = 'pending'
|
|
and change_type = 'karenz_return' and effective_date = current_date + 60) then
|
|
raise exception 'Die bisherige Rückkehr ist nach dem Anpassen weiter vorgemerkt.';
|
|
end if;
|
|
if not exists (select 1 from pending_org_changes where employee_id = v_e and status = 'pending'
|
|
and change_type = 'karenz_return' and effective_date = current_date + 400) then
|
|
raise exception 'Das Anpassen hat die Rückkehr der anschliessenden Abwesenheit mitgenommen.';
|
|
end if;
|
|
if (select geburtstermin_tatsaechlich from employees where id = v_e) is distinct from current_date + 18 then
|
|
raise exception 'Der tatsächliche Geburtstermin steht nicht an der Person.';
|
|
end if;
|
|
-- Ohne Reduktion steht keine Teilzeitvariante in der Vormerkung — sonst
|
|
-- leerte der Nachtlauf sie am Rückkehrtag.
|
|
if exists (select 1 from pending_org_changes where employee_id = v_e and status = 'pending'
|
|
and change_type = 'karenz_return' and payload ? 'teilzeit_art') then
|
|
raise exception 'Die Rückkehr ohne Reduktion merkt eine Teilzeitvariante vor.';
|
|
end if;
|
|
raise notice '20/35 Rückkehr angepasst, die der anschliessenden Abwesenheit bleibt';
|
|
|
|
-- ── Sofortige Rückkehr ──────────────────────────────────────────
|
|
perform rueckkehr_anpassen(jsonb_build_object(
|
|
'employee_id', v_e, 'return_date', current_date::text, 'employment_mode', 'unverändert'));
|
|
if (select karenz_start_date from employees where id = v_e) is not null then
|
|
raise exception 'Nach der sofortigen Rückkehr steht die Abwesenheit noch an der Person.';
|
|
end if;
|
|
if not exists (select 1 from employee_history where employee_id = v_e and event_type = 'Rückkehr'
|
|
and event_date = current_date and pending_id is null) then
|
|
raise exception 'Die sofortige Rückkehr steht nicht als verbuchte Zeile in der Historie.';
|
|
end if;
|
|
if not exists (select 1 from pending_org_changes where employee_id = v_e and status = 'pending'
|
|
and change_type = 'karenz_start' and effective_date = current_date + 60) then
|
|
raise exception 'Die anschliessend geplante Abwesenheit ist mit der Rückkehr verschwunden.';
|
|
end if;
|
|
raise notice '21/35 Rückkehr heute wird sofort verbucht, die geplante Abwesenheit bleibt';
|
|
|
|
-- ── Checklisten (20261019230000, 20261029140000) ────────────────
|
|
-- Die Rückkehr eben hat eine Onboarding-Liste mit Anlass „Rückkehr"
|
|
-- angelegt. Eine Liste lässt sich abschliessen und sperrt dann; eine neue
|
|
-- beginnt leer, die alte bleibt unverändert.
|
|
if (select string_agg(anlass, ',') from checklisten where employee_id = v_e and art = 'onboarding') is distinct from 'Rückkehr' then
|
|
raise exception 'Die Rückkehr hat keine Onboarding-Liste mit Anlass Rückkehr angelegt.';
|
|
end if;
|
|
perform start_onboarding(jsonb_build_object('employee_id', v_e, 'item_keys', jsonb_build_array('a', 'b')));
|
|
perform set_onboarding_task(jsonb_build_object('employee_id', v_e, 'item_key', 'a', 'erledigt', true));
|
|
perform checkliste_abschliessen(jsonb_build_object('checkliste_id', app_aktuelle_checkliste(v_e, 'onboarding')));
|
|
|
|
v_ging := false;
|
|
begin
|
|
perform set_onboarding_task(jsonb_build_object('employee_id', v_e, 'item_key', 'b', 'erledigt', true));
|
|
v_ging := true;
|
|
exception
|
|
when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%Die Checkliste ist abgeschlossen%' then
|
|
raise exception 'Eine abgeschlossene Checkliste liess sich ändern (%).', v_meldung;
|
|
end if;
|
|
|
|
perform start_onboarding(jsonb_build_object('employee_id', v_e, 'item_keys', jsonb_build_array('a', 'b'),
|
|
'neu', true, 'anlass', 'Rückkehr'));
|
|
perform set_onboarding_task(jsonb_build_object('employee_id', v_e, 'item_key', 'b', 'erledigt', true));
|
|
if (select count(*) from checklisten where employee_id = v_e and art = 'onboarding') <> 2 then
|
|
raise exception 'Erwartet wurden zwei Onboarding-Listen.';
|
|
end if;
|
|
if (select count(*) from onboarding_tasks t join checklisten c on c.id = t.checkliste_id
|
|
where c.employee_id = v_e and c.id <> app_aktuelle_checkliste(v_e, 'onboarding') and t.erledigt) <> 1 then
|
|
raise exception 'Die abgeschlossene Liste hat sich verändert.';
|
|
end if;
|
|
if (select string_agg(item_key, ',') from onboarding_tasks
|
|
where checkliste_id = app_aktuelle_checkliste(v_e, 'onboarding') and erledigt) is distinct from 'b' then
|
|
raise exception 'Der Haken steht nicht in der neuen Liste.';
|
|
end if;
|
|
raise notice '22/35 Rückkehr legt Onboarding-Liste an; abschliessen sperrt, eine neue beginnt leer';
|
|
|
|
-- ── Kostenstelle anlegen (20261020110000) ───────────────────────
|
|
-- Neu angelegt lässt sie sich einer Planstelle zuordnen; dieselbe Nummer
|
|
-- ein zweites Mal wird abgewiesen.
|
|
v_stelle := create_cost_center(jsonb_build_object(
|
|
'code', 'RAUCHTEST-KST', 'name', 'Rauchtest Kostenstelle', 'valid_from', (current_date - 30)::text));
|
|
perform set_position_cost_center(jsonb_build_object(
|
|
'position_id', v_p8, 'cost_center_id', v_stelle, 'valid_from', current_date::text));
|
|
if not exists (select 1 from position_cost_centers where position_id = v_p8 and cost_center_id = v_stelle) then
|
|
raise exception 'Die neue Kostenstelle liess sich keiner Planstelle zuordnen.';
|
|
end if;
|
|
v_ging := false;
|
|
begin
|
|
perform create_cost_center(jsonb_build_object('code', 'RAUCHTEST-KST', 'name', 'Doppelt'));
|
|
v_ging := true;
|
|
exception
|
|
when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%gibt es bereits%' then
|
|
raise exception 'Eine doppelte Kostenstellennummer wurde angenommen (%).', v_meldung;
|
|
end if;
|
|
-- Umbenennen ändert nur die Bezeichnung; Nummer und Kontierung bleiben.
|
|
perform rename_cost_center(jsonb_build_object('cost_center_id', v_stelle, 'name', 'Rauchtest Umbenannt'));
|
|
if (select code <> 'RAUCHTEST-KST' or name <> 'Rauchtest Umbenannt' from cost_centers where id = v_stelle)
|
|
or not exists (select 1 from position_cost_centers where position_id = v_p8 and cost_center_id = v_stelle)
|
|
or not exists (select 1 from audit_log where action = 'Kostenstelle umbenannt'
|
|
and details = 'Bisherige Bezeichnung: Rauchtest Kostenstelle') then
|
|
raise exception 'Die Kostenstelle ist nicht umbenannt, hat ihre Nummer oder Kontierung verloren oder fehlt im Protokoll.';
|
|
end if;
|
|
raise notice '23/35 Kostenstelle angelegt, zugeordnet und umbenannt, doppelte Nummer abgewiesen';
|
|
|
|
-- ── Geplante Abwesenheit berichtigen (20261020120000) ───────────
|
|
-- Über „Eintrag berichtigen": der Beginn wandert, die geplante Rückkehr
|
|
-- bleibt; danach wandert die Rückkehr. Je genau eine Vormerkung.
|
|
perform start_karenz(jsonb_build_object(
|
|
'employee_id', v_d, 'karenz_start_date', (current_date + 40)::text,
|
|
'planned_return_date', (current_date + 100)::text, 'absence_type', 'Bildungskarenz'));
|
|
perform update_history_entry(jsonb_build_object(
|
|
'history_id', (select id from employee_history where employee_id = v_d and event_type = 'Karenz'
|
|
and event_date = current_date + 40),
|
|
'employee_id', v_d, 'event_date', (current_date + 40)::text,
|
|
'werte', jsonb_build_array(jsonb_build_object('feld', 'Abwesend ab', 'nachher', (current_date + 45)::text))));
|
|
if (select count(*) from pending_org_changes where employee_id = v_d and status = 'pending'
|
|
and change_type = 'karenz_start' and effective_date = current_date + 45) <> 1
|
|
or exists (select 1 from pending_org_changes where employee_id = v_d and status = 'pending'
|
|
and change_type = 'karenz_start' and effective_date = current_date + 40) then
|
|
raise exception 'Der Beginn der geplanten Abwesenheit ist nicht gewandert.';
|
|
end if;
|
|
if (select karenz_start_date from employees where id = v_d) is distinct from current_date + 45 then
|
|
raise exception 'An der Person steht nicht der berichtigte Beginn.';
|
|
end if;
|
|
|
|
perform update_history_entry(jsonb_build_object(
|
|
'history_id', (select id from employee_history where employee_id = v_d and event_type = 'Rückkehr'
|
|
and event_date = current_date + 100),
|
|
'employee_id', v_d, 'event_date', (current_date + 120)::text, 'werte', '[]'::jsonb));
|
|
if (select count(*) from pending_org_changes where employee_id = v_d and status = 'pending'
|
|
and change_type = 'karenz_return') <> 1
|
|
or not exists (select 1 from pending_org_changes where employee_id = v_d and status = 'pending'
|
|
and change_type = 'karenz_return' and effective_date = current_date + 120) then
|
|
raise exception 'Die geplante Rückkehr ist nicht gewandert.';
|
|
end if;
|
|
|
|
-- Angehörige: unter „Sonstige" ein freies Verhältnis.
|
|
perform add_employee_dependent(jsonb_build_object(
|
|
'employee_id', v_d, 'first_name', 'Rauch', 'last_name', 'Onkel', 'relationship', 'Onkel',
|
|
'birth_date', '1960-01-01', 'effective_date', current_date::text));
|
|
if not exists (select 1 from employee_dependents where employee_id = v_d and relationship = 'Onkel') then
|
|
raise exception 'Ein frei eingetragenes Verhältnis wurde nicht gespeichert.';
|
|
end if;
|
|
raise notice '24/35 Geplante Abwesenheit berichtigt, freies Verhältnis bei Angehörigen';
|
|
|
|
-- ── Wochenhilfe ohne Ende (20261020130000) ──────────────────────
|
|
-- Ohne Ende keine vorgemerkte Rückkehr; eine anschliessende Abwesenheit
|
|
-- verlangt erst ein Ende.
|
|
perform start_karenz(jsonb_build_object(
|
|
'employee_id', v_b, 'karenz_start_date', (current_date - 1)::text,
|
|
'planned_return_date', '', 'absence_type', 'Wochenhilfe (Mutterschutz)'));
|
|
if (select karenz_start_date from employees where id = v_b) <> current_date - 1
|
|
or (select karenz_return_date from employees where id = v_b) is not null then
|
|
raise exception 'Die Wochenhilfe ohne Ende steht nicht wie erfasst an der Person.';
|
|
end if;
|
|
if exists (select 1 from pending_org_changes where employee_id = v_b and status = 'pending' and change_type = 'karenz_return') then
|
|
raise exception 'Ohne Ende wurde trotzdem eine Rückkehr vorgemerkt.';
|
|
end if;
|
|
v_ging := false;
|
|
begin
|
|
perform start_karenz(jsonb_build_object(
|
|
'employee_id', v_b, 'karenz_start_date', (current_date + 60)::text,
|
|
'planned_return_date', (current_date + 300)::text, 'absence_type', 'Elternkarenz (inkl. Väterkarenz)'));
|
|
v_ging := true;
|
|
exception
|
|
when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%keine Rückkehr geplant%' then
|
|
raise exception 'Eine anschliessende Abwesenheit liess sich ohne Ende der Wochenhilfe planen (%).', v_meldung;
|
|
end if;
|
|
raise notice '25/35 Wochenhilfe ohne Ende, anschliessende Abwesenheit erst mit Ende';
|
|
|
|
-- ── Berichtigte Rückkehr wandert mit (20261020140000) ───────────
|
|
-- v_a geht in eine Abwesenheit mit Rückkehr in 30 Tagen. Über „Eintrag
|
|
-- berichtigen" wird die Rückkehr zweimal verschoben: danach genau eine
|
|
-- vorgemerkte Rückkehr, auf dem Datum an der Person.
|
|
perform start_karenz(jsonb_build_object(
|
|
'employee_id', v_a, 'karenz_start_date', current_date::text,
|
|
'planned_return_date', (current_date + 30)::text, 'absence_type', 'Bildungskarenz'));
|
|
perform update_history_entry(jsonb_build_object(
|
|
'history_id', (select id from employee_history where employee_id = v_a and event_type = 'Karenz'
|
|
and event_date = current_date),
|
|
'employee_id', v_a, 'event_date', current_date::text,
|
|
'werte', jsonb_build_array(jsonb_build_object('feld', 'Geplante Rückkehr', 'nachher', (current_date + 50)::text))));
|
|
perform update_history_entry(jsonb_build_object(
|
|
'history_id', (select id from employee_history where employee_id = v_a and event_type = 'Karenz'
|
|
and event_date = current_date),
|
|
'employee_id', v_a, 'event_date', current_date::text,
|
|
'werte', jsonb_build_array(jsonb_build_object('feld', 'Geplante Rückkehr', 'nachher', (current_date + 70)::text))));
|
|
if (select karenz_return_date from employees where id = v_a) is distinct from current_date + 70 then
|
|
raise exception 'An der Person steht nicht die berichtigte Rückkehr.';
|
|
end if;
|
|
if (select count(*) from pending_org_changes where employee_id = v_a and status = 'pending' and change_type = 'karenz_return') <> 1
|
|
or not exists (select 1 from employee_history h join pending_org_changes q on q.id = h.pending_id
|
|
where h.employee_id = v_a and h.event_type = 'Rückkehr' and q.status = 'pending'
|
|
and h.event_date = current_date + 70) then
|
|
raise exception 'Die vorgemerkte Rückkehr ist der Berichtigung nicht gefolgt.';
|
|
end if;
|
|
raise notice '26/35 Berichtigte Rückkehr: Vormerkung und Historie folgen';
|
|
|
|
-- ── Lehrzeit (20261021100000) ───────────────────────────────────
|
|
-- Ein Lehrling bekommt Beginn und Ende; ein Ende vor dem Beginn wird
|
|
-- abgewiesen; als Angestellter sind beide wieder leer.
|
|
perform change_employee_data(jsonb_build_object(
|
|
'employee_id', v_e, 'effective_date', current_date::text,
|
|
'role', jsonb_build_object('worker_type', 'Lehrling',
|
|
'lehrzeit_von', (current_date - 100)::text, 'lehrzeit_bis', (current_date + 900)::text)));
|
|
if (select lehrzeit_von from employees where id = v_e) is distinct from current_date - 100
|
|
or (select lehrzeit_bis from employees where id = v_e) is distinct from current_date + 900 then
|
|
raise exception 'Die Lehrzeit steht nicht an der Person.';
|
|
end if;
|
|
if not exists (select 1 from employee_history where employee_id = v_e and event_type = 'Vertragsänderung'
|
|
and changes @> '[{"feld": "Lehrzeit von"}]') then
|
|
raise exception 'Die Lehrzeit steht nicht in der Historie.';
|
|
end if;
|
|
v_ging := false;
|
|
begin
|
|
perform change_employee_data(jsonb_build_object(
|
|
'employee_id', v_e, 'effective_date', current_date::text,
|
|
'role', jsonb_build_object('lehrzeit_bis', (current_date - 200)::text)));
|
|
v_ging := true;
|
|
exception
|
|
when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging then
|
|
raise exception 'Ein Ende der Lehrzeit vor ihrem Beginn wurde angenommen.';
|
|
end if;
|
|
perform change_employee_data(jsonb_build_object(
|
|
'employee_id', v_e, 'effective_date', current_date::text,
|
|
'role', jsonb_build_object('worker_type', 'Angestellte:r', 'lehrzeit_von', '', 'lehrzeit_bis', '')));
|
|
if (select lehrzeit_von from employees where id = v_e) is not null then
|
|
raise exception 'Nach dem Wechsel der Gruppe steht noch eine Lehrzeit.';
|
|
end if;
|
|
raise notice '27/35 Lehrzeit beim Lehrling, sonst leer';
|
|
|
|
-- ── Einstellung ohne Geburtsdatum (20261021120000) ──────────────
|
|
v_p6 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Ohne Geburt', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_d := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 9, 'first_name', 'Rauch', 'last_name', 'Test-G',
|
|
'gender', 'w', 'birth_date', '', 'location_id', v_ort,
|
|
'position_id', v_p6, 'entry_date', current_date::text));
|
|
if (select birth_date from employees where id = v_d) is not null then
|
|
raise exception 'Ein leeres Geburtsdatum wurde nicht als leer gespeichert.';
|
|
end if;
|
|
raise notice '28/35 Einstellung ohne Geburtsdatum';
|
|
|
|
-- ── Organisationseinheit umbenennen (20261022100000) ────────────
|
|
perform rename_org_unit(jsonb_build_object('org_unit_id', v_einheit, 'name', 'Rauchtest umbenannt'));
|
|
if (select name from org_units where id = v_einheit) <> 'Rauchtest umbenannt' then
|
|
raise exception 'Die Einheit wurde nicht umbenannt.';
|
|
end if;
|
|
if not exists (select 1 from audit_log where action = 'Organisationseinheit umbenannt'
|
|
and target_label like '%Rauchtest umbenannt') then
|
|
raise exception 'Die Umbenennung steht nicht im Protokoll.';
|
|
end if;
|
|
-- Art ändern (20261030120000): hin zum Vorstandsbereich und zurück.
|
|
perform change_org_unit_type(jsonb_build_object('org_unit_id', v_einheit, 'unit_type', 'Vorstandsbereich'));
|
|
if (select unit_type::text from org_units where id = v_einheit) <> 'Vorstandsbereich' then
|
|
raise exception 'Die Art der Einheit wurde nicht geändert.';
|
|
end if;
|
|
if not exists (select 1 from audit_log where action = 'Art der Organisationseinheit geändert'
|
|
and details = 'Bisherige Art: Team, neue Art: Vorstandsbereich') then
|
|
raise exception 'Die Änderung der Art steht nicht im Protokoll.';
|
|
end if;
|
|
perform change_org_unit_type(jsonb_build_object('org_unit_id', v_einheit, 'unit_type', 'Team'));
|
|
v_meldung := null;
|
|
begin
|
|
perform change_org_unit_type(jsonb_build_object('org_unit_id', v_einheit, 'unit_type', 'Gesellschaft'));
|
|
exception when others then
|
|
v_meldung := SQLERRM;
|
|
end;
|
|
if coalesce(v_meldung, '') not like 'Die Gesellschaft ist die Wurzel; ihre Art lässt sich nicht ändern%' then
|
|
raise exception 'Eine Einheit liess sich zur Gesellschaft machen.';
|
|
end if;
|
|
raise notice '29/35 Organisationseinheit umbenannt, Art geändert, beides im Protokoll';
|
|
|
|
-- ── Standort ändern (20261022110000) ────────────────────────────
|
|
-- Gebraucht wird ein zweiter Standort; der Rauchtest legt ihn an und
|
|
-- verwirft ihn mit allem anderen.
|
|
insert into locations (name, country)
|
|
select 'Rauchtest-Standort', country from locations where id = v_ort
|
|
returning id into v_stelle;
|
|
perform change_employee_data(jsonb_build_object(
|
|
'employee_id', v_d, 'effective_date', current_date::text,
|
|
'role', jsonb_build_object('location_id', v_stelle::text)));
|
|
if (select location_id from employees where id = v_d) <> v_stelle then
|
|
raise exception 'Der Standort wurde nicht geändert.';
|
|
end if;
|
|
if not exists (select 1 from employee_history where employee_id = v_d and event_type = 'Vertragsänderung'
|
|
and changes @> '[{"feld": "Standort"}]') then
|
|
raise exception 'Der Standortwechsel steht nicht in der Historie.';
|
|
end if;
|
|
raise notice '30/35 Standort geändert, in der Historie';
|
|
|
|
-- ── Geplante Abwesenheit ohne Ende, Ende nachgetragen und geleert
|
|
-- (20261029100000) ────────────────────────────────────────────
|
|
perform start_karenz(jsonb_build_object(
|
|
'employee_id', v_d, 'karenz_start_date', (current_date + 30)::text,
|
|
'planned_return_date', '', 'absence_type', 'Bildungskarenz'));
|
|
if exists (select 1 from pending_org_changes where employee_id = v_d and status = 'pending' and change_type = 'karenz_return') then
|
|
raise exception 'Ohne Ende wurde eine Rückkehr vorgemerkt.';
|
|
end if;
|
|
perform update_history_entry(jsonb_build_object(
|
|
'history_id', (select id from employee_history where employee_id = v_d and event_type = 'Karenz'),
|
|
'werte', jsonb_build_array(jsonb_build_object('feld', 'Geplante Rückkehr', 'nachher', (current_date + 90)::text))));
|
|
if not exists (select 1 from pending_org_changes where employee_id = v_d and status = 'pending'
|
|
and change_type = 'karenz_return' and effective_date = current_date + 90) then
|
|
raise exception 'Das nachgetragene Ende wurde nicht vorgemerkt.';
|
|
end if;
|
|
perform update_history_entry(jsonb_build_object(
|
|
'history_id', (select id from employee_history where employee_id = v_d and event_type = 'Karenz'),
|
|
'werte', jsonb_build_array(jsonb_build_object('feld', 'Geplante Rückkehr', 'nachher', null))));
|
|
if exists (select 1 from pending_org_changes where employee_id = v_d and status = 'pending' and change_type = 'karenz_return')
|
|
or (select payload->>'absence_type' from pending_org_changes
|
|
where employee_id = v_d and status = 'pending' and change_type = 'karenz_start') is distinct from 'Bildungskarenz' then
|
|
raise exception 'Das geleerte Ende blieb stehen, oder die Art ging verloren.';
|
|
end if;
|
|
raise notice '31/35 Abwesenheit ohne Ende, Ende nachgetragen und wieder geleert';
|
|
|
|
-- ── Übernahme Leiharbeitskraft (20261029120000) ─────────────────
|
|
v_p9 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Übernahme', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_e := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 10, 'first_name', 'Rauch', 'last_name', 'Test-U',
|
|
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort, 'source', 'Intern',
|
|
'position_id', v_p9, 'entry_date', current_date::text,
|
|
'ist_uebernahme_leiharbeit', true, 'uebernahme_leiharbeit_text', 'Rauchtest Überlasser'));
|
|
if not (select ist_uebernahme_leiharbeit and uebernahme_leiharbeit_text = 'Rauchtest Überlasser' from employees where id = v_e) then
|
|
raise exception 'Die Einstellung hat die Übernahme nicht geschrieben.';
|
|
end if;
|
|
perform change_employee_data(jsonb_build_object(
|
|
'employee_id', v_e, 'effective_date', current_date::text,
|
|
'person', jsonb_build_object('ist_uebernahme_leiharbeit', false, 'uebernahme_leiharbeit_text', 'bleibt nicht')));
|
|
if (select ist_uebernahme_leiharbeit or uebernahme_leiharbeit_text is not null from employees where id = v_e) then
|
|
raise exception 'Ohne Kennzeichen blieben Angaben zur Übernahme stehen.';
|
|
end if;
|
|
if not exists (select 1 from employee_history where employee_id = v_e
|
|
and changes @> '[{"feld": "Übernahme Leiharbeitskraft"}]' and changes @> '[{"feld": "Angaben zur Übernahme"}]') then
|
|
raise exception 'Die Änderung der Übernahme steht nicht in der Historie.';
|
|
end if;
|
|
raise notice '32/35 Übernahme Leiharbeitskraft bei der Einstellung, ohne Kennzeichen geleert';
|
|
|
|
-- ── Struktur anlegen und abschliessen (20261029160000) ──────────
|
|
perform create_location(jsonb_build_object('name', 'Rauchtest Ort', 'country', 'Österreich'));
|
|
v_ging := false;
|
|
begin
|
|
perform create_location(jsonb_build_object('name', 'RAUCHTEST ORT', 'country', 'Österreich'));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%gibt es bereits%' then
|
|
raise exception 'Ein Standort liess sich doppelt anlegen (%).', v_meldung;
|
|
end if;
|
|
|
|
v_job := create_job(jsonb_build_object('code', 'RAUCHTEST-J1', 'title', 'Rauchtest Katalog'));
|
|
v_zu := create_org_unit(jsonb_build_object(
|
|
'parent_id', v_wurzel, 'org_number', 'RAUCHTEST-ZU', 'name', 'Rauchtest zu',
|
|
'unit_type', 'Team', 'valid_from', (current_date - 30)::text));
|
|
v_psnr := next_position_number();
|
|
v_px := create_position(jsonb_build_object('org_unit_id', v_zu, 'position_number', v_psnr,
|
|
'job_title', 'Rauchtest Katalog', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
if (select position_number <> v_psnr or job_id <> v_job from om_positions where id = v_px) then
|
|
raise exception 'Die Planstelle trägt nicht die vorgegebene Nummer oder nicht die angelegte Tätigkeit.';
|
|
end if;
|
|
v_ging := false;
|
|
begin
|
|
perform create_position(jsonb_build_object('org_unit_id', v_zu, 'position_number', 'ID00' || substring(v_psnr from 3),
|
|
'job_title', 'Rauchtest Katalog', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%bereits vergeben%' then
|
|
raise exception 'Eine Planstellennummer liess sich doppelt vergeben (%).', v_meldung;
|
|
end if;
|
|
v_ging := false;
|
|
begin
|
|
perform create_position(jsonb_build_object('org_unit_id', v_zu, 'position_number', 'RAUCHTEST-PS1',
|
|
'job_title', 'Rauchtest Katalog', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%hat die Form ID und eine Zahl%' then
|
|
raise exception 'Eine Planstellennummer ohne die Form ID liess sich vergeben (%).', v_meldung;
|
|
end if;
|
|
-- Vergeben wird die kleinste freie Zahl; alle kleineren sind vergeben.
|
|
if next_position_number() !~ '^ID[0-9]{3,}$'
|
|
or substring(next_position_number() from 3)::numeric <= substring(v_psnr from 3)::numeric
|
|
or exists (select 1 from om_positions where position_number = next_position_number()) then
|
|
raise exception 'Die nächste Planstellennummer ist nicht die nächste freie ID-Nummer (%).', next_position_number();
|
|
end if;
|
|
-- Eine einzelne hohe Nummer verschiebt die nächste freie nicht.
|
|
v_psnr := next_position_number();
|
|
perform create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'position_number', 'ID' || (substring(v_psnr from 3)::numeric + 1000000000),
|
|
'job_title', 'Rauchtest Katalog', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
if next_position_number() <> v_psnr then
|
|
raise exception 'Nach einer hohen Nummer ist die nächste Planstellennummer % statt %.', next_position_number(), v_psnr;
|
|
end if;
|
|
v_py := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Katalog', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
if (select position_number from om_positions where id = v_py) <> v_psnr then
|
|
raise exception 'Eine Planstelle ohne Nummer bekam nicht die nächste freie (%).', v_psnr;
|
|
end if;
|
|
|
|
-- Eine besetzte Planstelle endet nicht vor ihrer Besetzung.
|
|
v_py := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Besetzt', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
perform hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 11, 'first_name', 'Rauch', 'last_name', 'Test-S',
|
|
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_py, 'entry_date', current_date::text));
|
|
v_ging := false;
|
|
begin
|
|
perform update_position(jsonb_build_object('position_id', v_py, 'valid_to', (current_date + 10)::text));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%hinaus eine Besetzung%' then
|
|
raise exception 'Eine besetzte Planstelle liess sich beenden (%).', v_meldung;
|
|
end if;
|
|
|
|
-- Die Einheit endet erst, wenn ihre Planstellen enden.
|
|
v_ging := false;
|
|
begin
|
|
perform set_org_unit_valid_to(jsonb_build_object('org_unit_id', v_zu, 'valid_to', (current_date + 10)::text));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%noch 1 Planstellen%' then
|
|
raise exception 'Eine Einheit mit offener Planstelle liess sich abschliessen (%).', v_meldung;
|
|
end if;
|
|
|
|
perform update_position(jsonb_build_object('position_id', v_px, 'valid_to', (current_date + 10)::text));
|
|
|
|
-- Auf eine endende Planstelle lässt sich niemand unbefristet setzen.
|
|
v_ging := false;
|
|
begin
|
|
perform hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 12, 'first_name', 'Rauch', 'last_name', 'Test-Z',
|
|
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_px, 'entry_date', current_date::text));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%Eine Besetzung darüber hinaus ist nicht möglich%' then
|
|
raise exception 'Jemand liess sich über das Ende der Planstelle hinaus besetzen (%).', v_meldung;
|
|
end if;
|
|
|
|
perform set_org_unit_valid_to(jsonb_build_object('org_unit_id', v_zu, 'valid_to', (current_date + 10)::text));
|
|
if (select valid_to from org_units where id = v_zu) is distinct from current_date + 10 then
|
|
raise exception 'Die Einheit trägt nicht das gesetzte Ende.';
|
|
end if;
|
|
|
|
v_ging := false;
|
|
begin
|
|
perform create_position(jsonb_build_object('org_unit_id', v_zu,
|
|
'job_title', 'Rauchtest Katalog', 'is_chief', false, 'valid_from', current_date::text));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%spätestens dann enden%' then
|
|
raise exception 'In einer endenden Einheit liess sich eine unbefristete Planstelle anlegen (%).', v_meldung;
|
|
end if;
|
|
v_ging := false;
|
|
begin
|
|
perform create_org_unit(jsonb_build_object(
|
|
'parent_id', v_zu, 'org_number', 'RAUCHTEST-UNTER', 'name', 'Rauchtest unter',
|
|
'unit_type', 'Team', 'valid_from', current_date::text));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%keine neue Einheit anlegen%' then
|
|
raise exception 'Unter einer endenden Einheit liess sich eine neue anlegen (%).', v_meldung;
|
|
end if;
|
|
|
|
perform set_org_unit_valid_to(jsonb_build_object('org_unit_id', v_zu, 'valid_to', null));
|
|
if (select valid_to from org_units where id = v_zu) is not null
|
|
or not exists (select 1 from audit_log where action = 'Organisationseinheit wieder geöffnet' and target_label = 'RAUCHTEST-ZU · Rauchtest zu') then
|
|
raise exception 'Die Einheit ist nicht wieder geöffnet oder nicht im Protokoll.';
|
|
end if;
|
|
raise notice '33/35 Standort, Tätigkeit, Planstelle mit Nummer; Planstelle und Einheit enden in der richtigen Reihenfolge';
|
|
|
|
-- ── Kein Vorgang ab dem Austritt (20261029180000) ───────────────
|
|
v_px := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Austritt A', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_py := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Austritt B', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_d := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 13, 'first_name', 'Rauch', 'last_name', 'Test-X',
|
|
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_px, 'entry_date', (current_date - 10)::text));
|
|
perform terminate_employee(jsonb_build_object(
|
|
'employee_id', v_d, 'exit_date', (current_date + 10)::text,
|
|
'exit_reason', 'Einvernehmliche Auflösung', 'austrittsart', 'freiwillig'));
|
|
|
|
-- Versetzung am Austrittstag und Abwesenheit danach: abgewiesen.
|
|
v_ging := false;
|
|
begin
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_d, 'target_position_id', v_py, 'effective_date', (current_date + 10)::text));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%die Person tritt am%aus%' then
|
|
raise exception 'Eine Versetzung am Austrittstag liess sich erfassen (%).', v_meldung;
|
|
end if;
|
|
v_ging := false;
|
|
begin
|
|
perform start_karenz(jsonb_build_object(
|
|
'employee_id', v_d, 'karenz_start_date', (current_date + 12)::text, 'absence_type', 'Bildungskarenz'));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%die Person tritt am%aus%' then
|
|
raise exception 'Eine Abwesenheit nach dem Austritt liess sich erfassen (%).', v_meldung;
|
|
end if;
|
|
|
|
-- Eine vorgemerkte Versetzung vor dem Austritt bleibt möglich …
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_d, 'target_position_id', v_py, 'effective_date', (current_date + 5)::text));
|
|
|
|
-- … aber der Austritt lässt sich nicht vor sie legen.
|
|
v_ging := false;
|
|
begin
|
|
perform austritt_berichtigen(jsonb_build_object(
|
|
'employee_id', v_d, 'exit_date', (current_date + 3)::text,
|
|
'exit_reason', 'Einvernehmliche Auflösung', 'austrittsart', 'freiwillig'));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%erst möglich, wenn dieser Vorgang abgebrochen%' then
|
|
raise exception 'Der Austritt liess sich vor eine vorgemerkte Versetzung legen (%).', v_meldung;
|
|
end if;
|
|
if (select exit_date from employees where id = v_d) <> current_date + 10 then
|
|
raise exception 'Der abgewiesene Austritt hat das Datum trotzdem verschoben.';
|
|
end if;
|
|
raise notice '34/35 Kein Vorgang ab dem Austritt, kein Austritt vor einem Vorgang';
|
|
|
|
-- ── Planstellenende mit Vormerkung (20261029210000) ─────────────
|
|
v_px := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Endet', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_py := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Ziel', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_d := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 20, 'first_name', 'Rauch', 'last_name', 'Test-V',
|
|
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_px, 'entry_date', (current_date - 10)::text));
|
|
perform transfer_employee(jsonb_build_object(
|
|
'employee_id', v_d, 'target_position_id', v_py, 'effective_date', (current_date + 5)::text));
|
|
|
|
-- Mit der vorgemerkten Versetzung ist die Planstelle zum Ende frei …
|
|
perform update_position(jsonb_build_object('position_id', v_px, 'valid_to', (current_date + 10)::text));
|
|
|
|
-- … und die Versetzung lässt sich nicht mehr abbrechen.
|
|
v_ging := false;
|
|
begin
|
|
perform delete_history_entry(jsonb_build_object('history_id',
|
|
(select id from employee_history where employee_id = v_d and event_type = 'Versetzung' and pending_id is not null)));
|
|
v_ging := true;
|
|
exception when others then v_meldung := SQLERRM;
|
|
end;
|
|
if v_ging or v_meldung not like '%lässt sich nicht abbrechen oder dahinter verschieben%' then
|
|
raise exception 'Die Versetzung von einer endenden Planstelle liess sich abbrechen (%).', v_meldung;
|
|
end if;
|
|
|
|
-- Ein vorgemerkter Austritt bis zum Ende genügt ebenso.
|
|
v_px := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
|
'job_title', 'Rauchtest Endet Austritt', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
|
v_e := hire_employee(jsonb_build_object(
|
|
'personnel_number', v_nr + 21, 'first_name', 'Rauch', 'last_name', 'Test-W',
|
|
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
|
'position_id', v_px, 'entry_date', (current_date - 10)::text));
|
|
perform terminate_employee(jsonb_build_object(
|
|
'employee_id', v_e, 'exit_date', (current_date + 5)::text,
|
|
'exit_reason', 'Einvernehmliche Auflösung', 'austrittsart', 'freiwillig'));
|
|
perform update_position(jsonb_build_object('position_id', v_px, 'valid_to', (current_date + 10)::text));
|
|
raise notice '35/35 Planstelle endet nach vorgemerkter Versetzung oder Austritt; die Vormerkung bleibt dann stehen';
|
|
|
|
raise notice '';
|
|
raise notice 'Rauchtest bestanden. Alles Angelegte wird gleich verworfen.';
|
|
end
|
|
$rauchtest$;
|
|
|
|
rollback;
|
|
|
|
-- Beweis, dass nichts geblieben ist. Läuft ausserhalb der verworfenen
|
|
-- Transaktion und muss überall 0 liefern.
|
|
select
|
|
(select count(*) from org_units where org_number like 'RAUCHTEST%') as einheiten,
|
|
(select count(*) from employees where last_name like 'Test-%' and first_name = 'Rauch') as personen,
|
|
(select count(*) from jobs where title like 'Rauchtest %') as taetigkeiten,
|
|
(select count(*) from locations where name like 'Rauchtest %') as standorte;
|