VL, Bonusanspruch, zustaendige HR und getrennte Suesswaren-Kollektivvertraege
Vertrag: VL (1-8, freiwillig) und Bonusanspruch (Vorgabe Nein; bei Ja eine Bonusregelung: RL ab 01.01.2027, RL bis 31.12.2026, Aussendienst, Shop) direkt nach dem Hay-Grade -- im Einstellungsassistenten, im Wiedereintritt und in "Daten aendern", sichtbar im Reiter Vertrag und im Datenexport. Stammdaten: zustaendige HR. Auswaehlbar sind alle aktiven HR-Konten; die Datenbank prueft das. Historie und Audit-Log zeigen den Namen. Kollektivvertraege: Suessware Arbeiter, Suessware Angestellter, Handel. collective_agreement wird wie der Hay-Grade eine Domain ueber text. Der Bestand mit "Suesswaren" wird nach der Beschaeftigtengruppe aufgeteilt (Angestellte -> Angestellter, Arbeiter:innen und Lehrlinge -> Arbeiter), ebenso geplante Aenderungen und Entwuerfe. Die Funktionen werden an einzeiligen Ankern der laufenden Definition ergaenzt (Einstellung, Wiedereintritt, Daten aendern, Nachtlauf, Feldkarte). app_feld_karte hatte mit 100 Argumenten die Grenze von jsonb_build_object erreicht; die neuen Felder stehen in einem zweiten Objekt. Gegen eine frische Datenbank geprueft: alle Migrationen samt Selbstpruefung, zweiter Lauf leer, db/tests/rauchtest.sql und der neue db/tests/vertragsangaben.sql (9 Schritte) bestanden, Umstellung des Suesswaren-Bestands nachgestellt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
164
db/migrations/20261007090000_kollektivvertraege.sql
Normal file
164
db/migrations/20261007090000_kollektivvertraege.sql
Normal file
@@ -0,0 +1,164 @@
|
||||
-- Kollektivverträge: Süßwaren wird nach Arbeitern und Angestellten getrennt
|
||||
--
|
||||
-- Die Liste der Personalabteilung:
|
||||
--
|
||||
-- Süßware Arbeiter
|
||||
-- Süßware Angestellter
|
||||
-- Handel
|
||||
--
|
||||
-- Bisher gab es „Süßwaren" als einen Wert. Für die Süßwarenindustrie gelten
|
||||
-- aber zwei Kollektivverträge, einer für Arbeiter:innen und einer für
|
||||
-- Angestellte — und welcher gilt, ist eine eigene Angabe, keine Ableitung:
|
||||
-- ein Lehrling oder ein Sonderfall kann unter beiden stehen.
|
||||
--
|
||||
-- ═══ Warum eine Domain und kein Aufzählungstyp ════════════════════
|
||||
--
|
||||
-- Wie beim Hay-Grade (20260928140000): Aus einem Enum lässt sich ein Wert
|
||||
-- nicht entfernen, und `alter type … add value` darf im selben Vorgang, der
|
||||
-- den neuen Wert schreibt, nicht benutzt werden. Der Typ **heisst** deshalb
|
||||
-- weiter `collective_agreement`, ist aber jetzt eine Domain über text mit
|
||||
-- CHECK. Alle Funktionen, die `(…)::collective_agreement` schreiben, bleiben
|
||||
-- so unverändert gültig; nur hire_employee trägt den alten Wert als Vorgabe
|
||||
-- im Rumpf und wird unten an genau dieser Stelle geändert.
|
||||
--
|
||||
-- ═══ Der Bestand ══════════════════════════════════════════════════
|
||||
--
|
||||
-- „Süßwaren" wird nach der Beschäftigtengruppe aufgeteilt — so von der
|
||||
-- Personalabteilung entschieden (07.10.2026):
|
||||
--
|
||||
-- Angestellte:r → Süßware Angestellter
|
||||
-- Arbeiter:in, Lehrling → Süßware Arbeiter
|
||||
--
|
||||
-- Lehrlinge landen bei den Arbeitern; wo das nicht stimmt, wird es in der
|
||||
-- Akte korrigiert. Umgestellt werden die Personen, die geplanten Änderungen
|
||||
-- und die Entwürfe des Einstellungsassistenten — überall dort, wo der Wert
|
||||
-- später noch geschrieben würde.
|
||||
--
|
||||
-- Die Historie bleibt, wie sie ist: dort steht, was damals galt. Wer einen
|
||||
-- alten Eintrag mit „Süßwaren" löschen und zurücksetzen will, bekommt eine
|
||||
-- Ablehnung der Datenbank — der alte Wert ist kein gültiger mehr. Das ist
|
||||
-- richtig so; zurückgesetzt wird auf einen der neuen Werte über
|
||||
-- „Daten ändern".
|
||||
|
||||
-- ═══ 1. Hängt der Typ noch woanders? ══════════════════════════════
|
||||
do $$
|
||||
declare
|
||||
v_spalten text;
|
||||
begin
|
||||
select string_agg(c.relname || '.' || a.attname, ', ')
|
||||
into v_spalten
|
||||
from pg_attribute a
|
||||
join pg_class c on c.oid = a.attrelid
|
||||
join pg_namespace n on n.oid = c.relnamespace
|
||||
where a.atttypid = 'collective_agreement'::regtype
|
||||
and a.attnum > 0
|
||||
and not a.attisdropped
|
||||
and n.nspname = 'public'
|
||||
and not (c.relname = 'employees' and a.attname = 'collective_agreement');
|
||||
|
||||
if v_spalten is not null then
|
||||
raise exception 'collective_agreement wird noch benutzt von: %. Umstellung abgebrochen.', v_spalten;
|
||||
end if;
|
||||
end
|
||||
$$;
|
||||
|
||||
-- ═══ 2. Der Typwechsel ════════════════════════════════════════════
|
||||
alter table employees alter column collective_agreement drop default;
|
||||
alter table employees alter column collective_agreement type text using collective_agreement::text;
|
||||
|
||||
drop type collective_agreement;
|
||||
|
||||
create domain collective_agreement as text
|
||||
constraint chk_kollektivvertrag check (value in ('Handel', 'Süßware Arbeiter', 'Süßware Angestellter'));
|
||||
|
||||
update employees
|
||||
set collective_agreement = case when worker_type = 'Angestellte:r' then 'Süßware Angestellter' else 'Süßware Arbeiter' end
|
||||
where collective_agreement = 'Süßwaren';
|
||||
|
||||
alter table employees alter column collective_agreement type collective_agreement using collective_agreement::collective_agreement;
|
||||
alter table employees alter column collective_agreement set default 'Handel';
|
||||
|
||||
-- ═══ 3. Geplante Änderungen ═══════════════════════════════════════
|
||||
--
|
||||
-- Eine datierte Änderung mit „Süßwaren" scheiterte sonst Wochen später im
|
||||
-- Nachtlauf an der Umwandlung — um drei Uhr früh und ohne jemanden, dem die
|
||||
-- Meldung zugestellt würde. Massgeblich ist die Beschäftigtengruppe, die mit
|
||||
-- derselben Änderung kommt, sonst die der Person.
|
||||
update pending_org_changes poc
|
||||
set payload = jsonb_set(
|
||||
poc.payload,
|
||||
'{role,collective_agreement}',
|
||||
to_jsonb(case
|
||||
when coalesce(poc.payload->'role'->>'worker_type', e.worker_type::text) = 'Angestellte:r'
|
||||
then 'Süßware Angestellter' else 'Süßware Arbeiter' end))
|
||||
from employees e
|
||||
where e.id = poc.employee_id
|
||||
and poc.status = 'pending'
|
||||
and poc.payload->'role'->>'collective_agreement' = 'Süßwaren';
|
||||
|
||||
-- ═══ 4. Entwürfe des Einstellungsassistenten ═════════════════════
|
||||
--
|
||||
-- Der Entwurf trägt die Felder des Formulars (collectiveAgreement,
|
||||
-- workerType). Ein fortgesetzter Entwurf mit dem alten Wert zeigte im
|
||||
-- Auswahlfeld nichts an und scheiterte beim Anlegen.
|
||||
update hire_drafts
|
||||
set payload = jsonb_set(
|
||||
payload,
|
||||
'{collectiveAgreement}',
|
||||
to_jsonb(case when payload->>'workerType' = 'Angestellte:r' then 'Süßware Angestellter' else 'Süßware Arbeiter' end))
|
||||
where payload->>'collectiveAgreement' = 'Süßwaren';
|
||||
|
||||
-- ═══ 5. hire_employee: Vorgabewert ════════════════════════════════
|
||||
--
|
||||
-- Fehlt der Kollektivvertrag im payload, setzte die Funktion „Süßwaren" —
|
||||
-- der Wert besteht den CHECK nicht mehr. Die neue Vorgabe folgt derselben
|
||||
-- Regel wie der Bestand. Aus der laufenden Definition gelesen, nicht aus
|
||||
-- einer Datei kopiert; der Anker steht auf einer Zeile und muss genau
|
||||
-- einmal vorkommen.
|
||||
do $migration$
|
||||
declare
|
||||
v_alt constant text := $anker$coalesce((payload->>'collective_agreement')::collective_agreement, 'Süßwaren'),$anker$;
|
||||
v_neu constant text := $anker$coalesce((payload->>'collective_agreement')::collective_agreement,
|
||||
case when payload->>'worker_type' = 'Angestellte:r' then 'Süßware Angestellter' else 'Süßware Arbeiter' end::collective_agreement),$anker$;
|
||||
v_def text;
|
||||
begin
|
||||
select pg_get_functiondef(p.oid) into v_def
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = 'hire_employee' and p.prokind = 'f';
|
||||
|
||||
if v_def is null then
|
||||
raise exception 'hire_employee ist nicht vorhanden.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt, ''))) / length(v_alt) <> 1 then
|
||||
raise exception 'hire_employee: der Vorgabewert kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
|
||||
execute replace(v_def, v_alt, v_neu);
|
||||
end
|
||||
$migration$;
|
||||
|
||||
-- ═══ 6. Selbstprüfung ═════════════════════════════════════════════
|
||||
do $$
|
||||
declare
|
||||
v_reste text;
|
||||
begin
|
||||
-- Kein Funktionsrumpf darf den alten Wert noch als Zeichenkette schreiben.
|
||||
select string_agg(p.proname, ', ')
|
||||
into v_reste
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public'
|
||||
and p.prosrc like '%''Süßwaren''%';
|
||||
if v_reste is not null then
|
||||
raise exception 'Diese Funktionen schreiben noch „Süßwaren": %', v_reste;
|
||||
end if;
|
||||
|
||||
if exists (select 1 from employees where collective_agreement::text not in ('Handel', 'Süßware Arbeiter', 'Süßware Angestellter')) then
|
||||
raise exception 'Es gibt noch Personen mit einem alten Kollektivvertrag.';
|
||||
end if;
|
||||
if exists (select 1 from pending_org_changes where status = 'pending' and payload->'role'->>'collective_agreement' = 'Süßwaren') then
|
||||
raise exception 'Es warten noch geplante Änderungen auf „Süßwaren".';
|
||||
end if;
|
||||
|
||||
raise notice 'Kollektivverträge: Handel, Süßware Arbeiter, Süßware Angestellter.';
|
||||
end
|
||||
$$;
|
||||
402
db/migrations/20261007100000_vl_bonus_zustaendige_hr.sql
Normal file
402
db/migrations/20261007100000_vl_bonus_zustaendige_hr.sql
Normal file
@@ -0,0 +1,402 @@
|
||||
-- VL, Bonusanspruch und zuständige HR
|
||||
--
|
||||
-- Drei neue Angaben, alle aus der Personalabteilung:
|
||||
--
|
||||
-- VL Stufe 1 bis 8, steht im Formular direkt nach dem
|
||||
-- Hay-Grade. Freiwillig: leer heisst „nicht erfasst",
|
||||
-- nicht „Stufe 0".
|
||||
--
|
||||
-- Bonusanspruch Ja oder Nein, Vorgabe Nein. Bei Ja kommt die
|
||||
-- Bonusregelung dazu: „RL ab 01.01.2027",
|
||||
-- „RL bis 31.12.2026", „Außendienst" oder „Shop".
|
||||
--
|
||||
-- Zuständige HR Wer in der Personalabteilung für die Person zuständig
|
||||
-- ist. Auswählbar sind alle, die HR sind und ein Konto
|
||||
-- haben (profiles, role = 'hr', aktiv). Gespeichert wird
|
||||
-- die Kennung, nicht der Name: ein Name ändert sich, und
|
||||
-- dann zeigte die Akte jemanden, den es so nicht mehr gibt.
|
||||
-- Gehört zu den Stammdaten (Gruppe `person`), nicht zum
|
||||
-- Vertrag — es ist eine Zuständigkeit, keine Vereinbarung.
|
||||
--
|
||||
-- Der Bonusanspruch ist gebaut wie der Dienstwagen: ein Kennzeichen und eine
|
||||
-- Angabe, die per CHECK daran hängt. Eine einzige Spalte „Bonusregelung, leer
|
||||
-- heisst Nein" wäre kürzer, sähe in der Historie aber anders aus als jedes
|
||||
-- andere Ja/Nein-Feld dieser Anwendung — und „Bonusanspruch: Nein → Ja" ist
|
||||
-- genau der Satz, den die Personalabteilung dort lesen will.
|
||||
--
|
||||
-- Beide gehören zur Gruppe `contract` — wie der Hay-Grade, neben dem sie im
|
||||
-- Formular stehen — und sind damit **datiert**: eine Änderung gilt ab einem
|
||||
-- Tag und kann in pending_org_changes landen. Deshalb stehen sie unten auch
|
||||
-- im Nachtlauf; diese zweite Stelle ist in diesem Projekt schon einmal
|
||||
-- vergessen worden (siehe 20260929100000).
|
||||
--
|
||||
-- Alle Funktionen werden **nicht** aus einer Datei kopiert, sondern aus der
|
||||
-- laufenden Definition gelesen und an genau einem Anker ergänzt. Jeder Anker
|
||||
-- steht auf einer Zeile — die Rümpfe stammen aus Dateien mit wechselnden
|
||||
-- Zeilenenden, ein mehrzeiliger Anker träfe je nach Rechner nicht — und muss
|
||||
-- genau einmal vorkommen; sonst bricht die Migration ab, statt an einer
|
||||
-- Stelle zu schreiben, die hier niemand angesehen hat.
|
||||
--
|
||||
-- Eine Besonderheit bei app_feld_karte: jsonb_build_object nimmt höchstens
|
||||
-- 100 Argumente, und mit dem Hay-Grade waren es genau 100 (50 Felder). Die
|
||||
-- neuen Felder stehen deshalb in einem zweiten Objekt, das mit `||`
|
||||
-- angehängt wird. Ein viertes Feld in das erste Objekt zu schreiben, hätte
|
||||
-- die Funktion beim Anlegen scheitern lassen.
|
||||
|
||||
-- ═══ 1. Die Spalten ════════════════════════════════════════════════
|
||||
alter table employees add column if not exists vl smallint;
|
||||
comment on column employees.vl is
|
||||
'VL, Stufe 1 bis 8. Null heisst nicht erfasst.';
|
||||
alter table employees drop constraint if exists chk_vl;
|
||||
alter table employees add constraint chk_vl check (vl is null or vl between 1 and 8);
|
||||
|
||||
alter table employees add column if not exists has_bonusanspruch boolean not null default false;
|
||||
comment on column employees.has_bonusanspruch is
|
||||
'Bonusanspruch ja/nein. Vorgabe nein.';
|
||||
alter table employees add column if not exists bonusanspruch_art text;
|
||||
comment on column employees.bonusanspruch_art is
|
||||
'Bonusregelung bei Bonusanspruch; null genau dann, wenn kein Anspruch besteht.';
|
||||
|
||||
-- `is not null and` steht bewusst davor: `in (…)` ergibt bei null nicht
|
||||
-- false, sondern null — und ein CHECK lässt null durch.
|
||||
alter table employees drop constraint if exists chk_bonusanspruch_art;
|
||||
alter table employees add constraint chk_bonusanspruch_art check (
|
||||
(has_bonusanspruch and bonusanspruch_art is not null
|
||||
and bonusanspruch_art in ('RL ab 01.01.2027', 'RL bis 31.12.2026', 'Außendienst', 'Shop'))
|
||||
or (not has_bonusanspruch and bonusanspruch_art is null)
|
||||
);
|
||||
|
||||
-- on delete set null: wird ein Konto gelöscht, verliert die Person ihre
|
||||
-- Zuständige, statt dass das Löschen des Kontos scheitert.
|
||||
alter table employees add column if not exists zustaendige_hr_id uuid references profiles(id) on delete set null;
|
||||
comment on column employees.zustaendige_hr_id is
|
||||
'Zuständige Person in der Personalabteilung (profiles.id). Null heisst nicht zugeordnet.';
|
||||
|
||||
-- ═══ 2. hire_employee ═════════════════════════════════════════════
|
||||
do $migration$
|
||||
declare
|
||||
-- Die Spaltenliste …
|
||||
v_alt_spalten constant text := $anker$employment_type, weekly_hours, contract_type, contract_end_date, paygrade,$anker$;
|
||||
v_neu_spalten constant text := $anker$employment_type, weekly_hours, contract_type, contract_end_date, paygrade, vl, has_bonusanspruch, bonusanspruch_art, zustaendige_hr_id,$anker$;
|
||||
|
||||
-- … die Werte an derselben Stelle …
|
||||
v_alt_werte constant text := $anker$coalesce((payload->>'paygrade')::paygrade_type, '-'),$anker$;
|
||||
v_neu_werte constant text := $anker$coalesce((payload->>'paygrade')::paygrade_type, '-'),
|
||||
nullif(payload->>'vl', '')::smallint,
|
||||
coalesce((payload->>'has_bonusanspruch')::boolean, false),
|
||||
case when coalesce((payload->>'has_bonusanspruch')::boolean, false) then nullif(payload->>'bonusanspruch_art', '') else null end,
|
||||
nullif(payload->>'zustaendige_hr_id', '')::uuid,$anker$;
|
||||
|
||||
-- … und eine Prüfung davor: ohne sie meldete der CHECK das fehlende
|
||||
-- Feld in der Sprache der Datenbank, nach allen Schritten des Assistenten.
|
||||
v_alt_pruef constant text := $anker$insert into employees ($anker$;
|
||||
v_neu_pruef constant text := $anker$if coalesce((payload->>'has_bonusanspruch')::boolean, false) and nullif(payload->>'bonusanspruch_art', '') is null then
|
||||
raise exception 'Bei Bonusanspruch „Ja" ist eine Bonusregelung zu wählen.';
|
||||
end if;
|
||||
if nullif(payload->>'zustaendige_hr_id', '') is not null and not exists (
|
||||
select 1 from profiles where id = (payload->>'zustaendige_hr_id')::uuid and role = 'hr' and is_active
|
||||
) then
|
||||
raise exception 'Als zuständige HR ist nur auswählbar, wer HR ist und ein aktives Konto hat.';
|
||||
end if;
|
||||
|
||||
insert into employees ($anker$;
|
||||
|
||||
v_def text;
|
||||
begin
|
||||
select pg_get_functiondef(p.oid) into v_def
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = 'hire_employee' and p.prokind = 'f';
|
||||
|
||||
if v_def is null then
|
||||
raise exception 'hire_employee ist nicht vorhanden.';
|
||||
end if;
|
||||
if v_def like '%bonusanspruch%' then
|
||||
raise exception 'hire_employee kennt den Bonusanspruch bereits.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt_spalten, ''))) / length(v_alt_spalten) <> 1 then
|
||||
raise exception 'hire_employee: der Anker für die Spalten kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt_werte, ''))) / length(v_alt_werte) <> 1 then
|
||||
raise exception 'hire_employee: der Anker für die Werte kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt_pruef, ''))) / length(v_alt_pruef) <> 1 then
|
||||
raise exception 'hire_employee: der Anker für die Prüfung kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
|
||||
execute replace(replace(replace(v_def, v_alt_spalten, v_neu_spalten), v_alt_werte, v_neu_werte), v_alt_pruef, v_neu_pruef);
|
||||
end
|
||||
$migration$;
|
||||
|
||||
-- ═══ 3. rehire_employee ═══════════════════════════════════════════
|
||||
--
|
||||
-- Der Wiedereintritt übernimmt die Angaben aus der Akte und lässt sie im
|
||||
-- Assistenten ändern. Was das payload nicht nennt, bleibt, wie es ist.
|
||||
do $migration$
|
||||
declare
|
||||
v_alt constant text := $anker$paygrade = coalesce((payload->>'paygrade')::paygrade_type, paygrade),$anker$;
|
||||
v_neu constant text := $anker$paygrade = coalesce((payload->>'paygrade')::paygrade_type, paygrade),
|
||||
vl = case when payload ? 'vl' then nullif(payload->>'vl', '')::smallint else vl end,
|
||||
has_bonusanspruch = coalesce((payload->>'has_bonusanspruch')::boolean, has_bonusanspruch),
|
||||
bonusanspruch_art = case
|
||||
when coalesce((payload->>'has_bonusanspruch')::boolean, has_bonusanspruch)
|
||||
then case when payload ? 'bonusanspruch_art' then nullif(payload->>'bonusanspruch_art', '') else bonusanspruch_art end
|
||||
else null
|
||||
end,
|
||||
zustaendige_hr_id = case when payload ? 'zustaendige_hr_id' then nullif(payload->>'zustaendige_hr_id', '')::uuid else zustaendige_hr_id end,$anker$;
|
||||
v_def text;
|
||||
begin
|
||||
select pg_get_functiondef(p.oid) into v_def
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = 'rehire_employee' and p.prokind = 'f';
|
||||
|
||||
if v_def is null then
|
||||
raise exception 'rehire_employee ist nicht vorhanden.';
|
||||
end if;
|
||||
if v_def like '%bonusanspruch%' then
|
||||
raise exception 'rehire_employee kennt den Bonusanspruch bereits.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt, ''))) / length(v_alt) <> 1 then
|
||||
raise exception 'rehire_employee: der Anker kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
|
||||
execute replace(v_def, v_alt, v_neu);
|
||||
end
|
||||
$migration$;
|
||||
|
||||
-- ═══ 4. change_employee_data ══════════════════════════════════════
|
||||
do $migration$
|
||||
declare
|
||||
-- Der Eintrag im Änderungsprotokoll, davor die Prüfung. Die Bonusregelung
|
||||
-- wird mit dem Wert protokolliert, den die Spalte danach tatsächlich trägt:
|
||||
-- bei „Ja → Nein" verschwindet sie, und genau das soll in der Historie
|
||||
-- stehen — sonst liesse sich der Eintrag nicht vollständig zurücknehmen.
|
||||
v_alt_log constant text := $anker$if v_contract ? 'paygrade' then v_contract_changes := app_aenderung(v_contract_changes, 'Hay-Grade', v_old.paygrade::text, v_contract->>'paygrade'); end if;$anker$;
|
||||
v_neu_log constant text := $anker$if v_contract ? 'paygrade' then v_contract_changes := app_aenderung(v_contract_changes, 'Hay-Grade', v_old.paygrade::text, v_contract->>'paygrade'); end if;
|
||||
if coalesce((v_contract->>'has_bonusanspruch')::boolean, v_old.has_bonusanspruch)
|
||||
and coalesce(nullif(v_contract->>'bonusanspruch_art', ''), v_old.bonusanspruch_art) is null then
|
||||
raise exception 'Bei Bonusanspruch „Ja" ist eine Bonusregelung zu wählen.';
|
||||
end if;
|
||||
if v_contract ? 'vl' then v_contract_changes := app_aenderung(v_contract_changes, 'VL', v_old.vl::text, (nullif(v_contract->>'vl', '')::smallint)::text); end if;
|
||||
if v_contract ? 'has_bonusanspruch' then v_contract_changes := app_aenderung(v_contract_changes, 'Bonusanspruch', v_old.has_bonusanspruch::text, v_contract->>'has_bonusanspruch'); end if;
|
||||
if v_contract ? 'has_bonusanspruch' or v_contract ? 'bonusanspruch_art' then v_contract_changes := app_aenderung(v_contract_changes, 'Bonusregelung', v_old.bonusanspruch_art,
|
||||
case when coalesce((v_contract->>'has_bonusanspruch')::boolean, v_old.has_bonusanspruch)
|
||||
then coalesce(nullif(v_contract->>'bonusanspruch_art', ''), v_old.bonusanspruch_art) else null end); end if;$anker$;
|
||||
|
||||
-- Die zuständige HR steht in der Gruppe person, neben der Cornerstone-ID.
|
||||
-- Protokolliert wird die Kennung — sie ist es, die sich zurücksetzen lässt;
|
||||
-- den Namen dazu schlägt die Anzeige nach. Geprüft wird nur ein neuer
|
||||
-- Wert: wer schon zugeordnet ist, deren Konto aber inzwischen abgeschaltet
|
||||
-- wurde, soll eine Adressänderung nicht blockieren.
|
||||
v_alt_person_log constant text := $anker$if v_person ? 'cornerstone_id' then v_person_changes := app_aenderung(v_person_changes, 'Cornerstone-ID', v_old.cornerstone_id, v_person->>'cornerstone_id'); end if;$anker$;
|
||||
v_neu_person_log constant text := $anker$if v_person ? 'cornerstone_id' then v_person_changes := app_aenderung(v_person_changes, 'Cornerstone-ID', v_old.cornerstone_id, v_person->>'cornerstone_id'); end if;
|
||||
if nullif(v_person->>'zustaendige_hr_id', '') is not null
|
||||
and (v_person->>'zustaendige_hr_id')::uuid is distinct from v_old.zustaendige_hr_id
|
||||
and not exists (
|
||||
select 1 from profiles where id = (v_person->>'zustaendige_hr_id')::uuid and role = 'hr' and is_active
|
||||
) then
|
||||
raise exception 'Als zuständige HR ist nur auswählbar, wer HR ist und ein aktives Konto hat.';
|
||||
end if;
|
||||
if v_person ? 'zustaendige_hr_id' then v_person_changes := app_aenderung(v_person_changes, 'Zuständige HR', v_old.zustaendige_hr_id::text, (nullif(v_person->>'zustaendige_hr_id', '')::uuid)::text); end if;$anker$;
|
||||
v_alt_person_upd constant text := $anker$cornerstone_id = case when v_person ? 'cornerstone_id' then nullif(v_person->>'cornerstone_id', '') else cornerstone_id end,$anker$;
|
||||
v_neu_person_upd constant text := $anker$cornerstone_id = case when v_person ? 'cornerstone_id' then nullif(v_person->>'cornerstone_id', '') else cornerstone_id end,
|
||||
zustaendige_hr_id = case when v_person ? 'zustaendige_hr_id' then nullif(v_person->>'zustaendige_hr_id', '')::uuid else zustaendige_hr_id end,$anker$;
|
||||
|
||||
-- … und die Spalten selbst.
|
||||
v_alt_upd constant text := $anker$paygrade = coalesce((v_contract->>'paygrade')::paygrade_type, paygrade),$anker$;
|
||||
v_neu_upd constant text := $anker$paygrade = coalesce((v_contract->>'paygrade')::paygrade_type, paygrade),
|
||||
vl = case when v_contract ? 'vl' then nullif(v_contract->>'vl', '')::smallint else vl end,
|
||||
has_bonusanspruch = coalesce((v_contract->>'has_bonusanspruch')::boolean, has_bonusanspruch),
|
||||
bonusanspruch_art = case
|
||||
when coalesce((v_contract->>'has_bonusanspruch')::boolean, has_bonusanspruch)
|
||||
then coalesce(nullif(v_contract->>'bonusanspruch_art', ''), bonusanspruch_art)
|
||||
else null
|
||||
end,$anker$;
|
||||
|
||||
v_def text;
|
||||
begin
|
||||
select pg_get_functiondef(p.oid) into v_def
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = 'change_employee_data' and p.prokind = 'f';
|
||||
|
||||
if v_def is null then
|
||||
raise exception 'change_employee_data ist nicht vorhanden.';
|
||||
end if;
|
||||
if v_def like '%bonusanspruch%' then
|
||||
raise exception 'change_employee_data kennt den Bonusanspruch bereits.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt_log, ''))) / length(v_alt_log) <> 1 then
|
||||
raise exception 'change_employee_data: der Anker für das Protokoll kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt_upd, ''))) / length(v_alt_upd) <> 1 then
|
||||
raise exception 'change_employee_data: der Anker für die Spalten kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt_person_log, ''))) / length(v_alt_person_log) <> 1 then
|
||||
raise exception 'change_employee_data: der Anker für das Protokoll der Stammdaten kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt_person_upd, ''))) / length(v_alt_person_upd) <> 1 then
|
||||
raise exception 'change_employee_data: der Anker für die Stammdaten-Spalten kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
|
||||
execute replace(replace(replace(replace(v_def, v_alt_log, v_neu_log), v_alt_upd, v_neu_upd),
|
||||
v_alt_person_log, v_neu_person_log), v_alt_person_upd, v_neu_person_upd);
|
||||
end
|
||||
$migration$;
|
||||
|
||||
-- ═══ 5. apply_due_pending_changes ═════════════════════════════════
|
||||
--
|
||||
-- Eine datierte Änderung landet als `contract`-Gruppe im payload und wird in
|
||||
-- der Nacht ihres Stichtags hier geschrieben.
|
||||
do $migration$
|
||||
declare
|
||||
v_alt constant text := $anker$paygrade = coalesce((v_rec.payload->'contract'->>'paygrade')::paygrade_type, paygrade),$anker$;
|
||||
v_neu constant text := $anker$paygrade = coalesce((v_rec.payload->'contract'->>'paygrade')::paygrade_type, paygrade),
|
||||
vl = case when (v_rec.payload->'contract') ? 'vl' then nullif(v_rec.payload->'contract'->>'vl', '')::smallint else vl end,
|
||||
has_bonusanspruch = coalesce((v_rec.payload->'contract'->>'has_bonusanspruch')::boolean, has_bonusanspruch),
|
||||
bonusanspruch_art = case
|
||||
when coalesce((v_rec.payload->'contract'->>'has_bonusanspruch')::boolean, has_bonusanspruch)
|
||||
then coalesce(nullif(v_rec.payload->'contract'->>'bonusanspruch_art', ''), bonusanspruch_art)
|
||||
else null
|
||||
end,$anker$;
|
||||
v_alt_person constant text := $anker$cornerstone_id = case when v_rec.payload->'person' ? 'cornerstone_id' then nullif(v_rec.payload->'person'->>'cornerstone_id', '') else cornerstone_id end,$anker$;
|
||||
v_neu_person constant text := $anker$cornerstone_id = case when v_rec.payload->'person' ? 'cornerstone_id' then nullif(v_rec.payload->'person'->>'cornerstone_id', '') else cornerstone_id end,
|
||||
zustaendige_hr_id = case when (v_rec.payload->'person') ? 'zustaendige_hr_id' then nullif(v_rec.payload->'person'->>'zustaendige_hr_id', '')::uuid else zustaendige_hr_id end,$anker$;
|
||||
v_def text;
|
||||
begin
|
||||
select pg_get_functiondef(p.oid) into v_def
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = 'apply_due_pending_changes' and p.prokind = 'f';
|
||||
|
||||
if v_def is null then
|
||||
raise exception 'apply_due_pending_changes ist nicht vorhanden.';
|
||||
end if;
|
||||
if v_def like '%bonusanspruch%' then
|
||||
raise exception 'apply_due_pending_changes kennt den Bonusanspruch bereits.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt, ''))) / length(v_alt) <> 1 then
|
||||
raise exception 'apply_due_pending_changes: der Anker kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt_person, ''))) / length(v_alt_person) <> 1 then
|
||||
raise exception 'apply_due_pending_changes: der Anker für die Stammdaten kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
|
||||
execute replace(replace(v_def, v_alt, v_neu), v_alt_person, v_neu_person);
|
||||
end
|
||||
$migration$;
|
||||
|
||||
-- ═══ 6. app_feld_karte ════════════════════════════════════════════
|
||||
--
|
||||
-- Ohne Eintrag hier liesse sich der Eintrag in der Historie hinterher weder
|
||||
-- richtigstellen noch zurücknehmen. Das erste Objekt ist mit 100 Argumenten
|
||||
-- voll (siehe oben); die neuen Felder stehen in einem zweiten.
|
||||
do $migration$
|
||||
declare
|
||||
v_alt constant text := $anker$jsonb_build_array('karenz_return_date', 'date', null)$anker$;
|
||||
v_neu constant text := $anker$jsonb_build_array('karenz_return_date', 'date', null)
|
||||
) || jsonb_build_object(
|
||||
-- 'smallint' wie die Spalte: der Typ geht in update_history_entry
|
||||
-- unmittelbar als Umwandlung ins SQL.
|
||||
'VL', jsonb_build_array('vl', 'smallint', 'contract'),
|
||||
'Bonusanspruch', jsonb_build_array('has_bonusanspruch', 'boolean', 'contract'),
|
||||
'Bonusregelung', jsonb_build_array('bonusanspruch_art', 'text', 'contract'),
|
||||
'Zuständige HR', jsonb_build_array('zustaendige_hr_id', 'uuid', 'person')$anker$;
|
||||
v_def text;
|
||||
begin
|
||||
select pg_get_functiondef(p.oid) into v_def
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = 'app_feld_karte' and p.prokind = 'f';
|
||||
|
||||
if v_def is null then
|
||||
raise exception 'app_feld_karte ist nicht vorhanden.';
|
||||
end if;
|
||||
if v_def like '%bonusanspruch%' then
|
||||
raise exception 'app_feld_karte kennt den Bonusanspruch bereits.';
|
||||
end if;
|
||||
if (length(v_def) - length(replace(v_def, v_alt, ''))) / length(v_alt) <> 1 then
|
||||
raise exception 'app_feld_karte: der Anker kommt nicht genau einmal vor.';
|
||||
end if;
|
||||
|
||||
execute replace(v_def, v_alt, v_neu);
|
||||
end
|
||||
$migration$;
|
||||
|
||||
-- ═══ 7. Selbstprüfung ═════════════════════════════════════════════
|
||||
--
|
||||
-- Geprüft wird nicht nur das Neue, sondern auch das, was bei früheren
|
||||
-- Neuerzeugungen in diesem Projekt schon einmal still verschwunden ist.
|
||||
do $$
|
||||
declare
|
||||
v_def text;
|
||||
v_karte jsonb;
|
||||
v_anz int;
|
||||
begin
|
||||
-- hire_employee
|
||||
select pg_get_functiondef(p.oid) into v_def
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = 'hire_employee' and p.prokind = 'f';
|
||||
if v_def not like '%has_bonusanspruch, bonusanspruch_art, zustaendige_hr_id%' or v_def not like '%payload->>''vl''%' then
|
||||
raise exception 'hire_employee schreibt VL oder Bonusanspruch nicht.';
|
||||
end if;
|
||||
if v_def not like '%::paygrade_type, ''-'')%' then
|
||||
raise exception 'hire_employee hat die Vorgabe für den Hay-Grade verloren.';
|
||||
end if;
|
||||
if v_def not like '%cornerstone_id%' or v_def not like '%require_hr_admin()%' then
|
||||
raise exception 'hire_employee hat Cornerstone-ID oder Rechtepruefung verloren.';
|
||||
end if;
|
||||
|
||||
-- rehire_employee: der Typcast des Status ist hier zweimal verloren gegangen.
|
||||
select pg_get_functiondef(p.oid) into v_def
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = 'rehire_employee' and p.prokind = 'f';
|
||||
if v_def not like '%payload ? ''bonusanspruch_art''%' or v_def not like '%payload ? ''vl''%' or v_def not like '%payload ? ''zustaendige_hr_id''%' then
|
||||
raise exception 'rehire_employee schreibt VL oder Bonusanspruch nicht.';
|
||||
end if;
|
||||
if v_def not like '%::employment_status%' then
|
||||
raise exception 'rehire_employee hat die Umwandlung des Status verloren.';
|
||||
end if;
|
||||
|
||||
-- change_employee_data
|
||||
select pg_get_functiondef(p.oid) into v_def
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = 'change_employee_data' and p.prokind = 'f';
|
||||
if v_def not like '%''Bonusregelung''%' or v_def not like '%''VL''%' or v_def not like '%''Bonusanspruch''%' or v_def not like '%''Zuständige HR''%' then
|
||||
raise exception 'change_employee_data protokolliert VL oder Bonusanspruch nicht.';
|
||||
end if;
|
||||
if v_def not like '%v_contract->>''paygrade''%' or v_def not like '%require_hr_admin()%'
|
||||
or v_def not like '%search_path%' or v_def not like '%cornerstone_id%' then
|
||||
raise exception 'change_employee_data hat Hay-Grade, Rechtepruefung, search_path oder Cornerstone-ID verloren.';
|
||||
end if;
|
||||
|
||||
-- apply_due_pending_changes
|
||||
select pg_get_functiondef(p.oid) into v_def
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = 'apply_due_pending_changes' and p.prokind = 'f';
|
||||
if v_def not like '%''contract''->>''has_bonusanspruch''%' or v_def not like '%''contract''->>''vl''%' or v_def not like '%''person''->>''zustaendige_hr_id''%' then
|
||||
raise exception 'Der Nachtlauf wendet VL oder Bonusanspruch nicht an.';
|
||||
end if;
|
||||
if v_def not like '%''contract''->>''paygrade''%' or v_def not like '%v_rec.payload->''role''%'
|
||||
or v_def not like '%emergency_contact_name%' or v_def not like '%target_position_id%' then
|
||||
raise exception 'Der Nachtlauf hat Hay-Grade, Gruppe role, Notfallkontakt oder Zielplanstelle verloren.';
|
||||
end if;
|
||||
v_anz := (length(v_def) - length(replace(v_def, 'insert into position_assignments', '')))
|
||||
/ length('insert into position_assignments');
|
||||
if v_anz <> 2 then
|
||||
raise exception 'Der Nachtlauf haengt die Besetzung % mal um, erwartet zweimal.', v_anz;
|
||||
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 = 'apply_due_pending_changes' and p.prosecdef
|
||||
) then
|
||||
raise exception 'Der Nachtlauf ist nicht mehr SECURITY DEFINER.';
|
||||
end if;
|
||||
|
||||
-- app_feld_karte: ausgeführt, nicht nur gelesen — ein Objekt mit zu vielen
|
||||
-- Argumenten fiele erst beim Aufruf auf.
|
||||
v_karte := app_feld_karte();
|
||||
if not (v_karte ? 'VL' and v_karte ? 'Bonusanspruch' and v_karte ? 'Bonusregelung' and v_karte ? 'Zuständige HR'
|
||||
and v_karte ? 'Hay-Grade' and v_karte ? 'Cornerstone-ID' and v_karte ? 'Geplante Rückkehr') then
|
||||
raise exception 'Die Feldkarte ist unvollständig.';
|
||||
end if;
|
||||
|
||||
raise notice 'VL, Bonusanspruch und zustaendige HR stehen in Einstellung, Wiedereintritt, Daten aendern, Nachtlauf und Feldkarte.';
|
||||
end
|
||||
$$;
|
||||
273
db/tests/vertragsangaben.sql
Normal file
273
db/tests/vertragsangaben.sql
Normal file
@@ -0,0 +1,273 @@
|
||||
-- Rauchtest: VL, Bonusanspruch, zuständige HR und die neuen Kollektivverträge
|
||||
--
|
||||
-- psql -f db/tests/vertragsangaben.sql
|
||||
--
|
||||
-- Dieselben zwei Eigenschaften wie db/tests/rauchtest.sql: alles läuft in
|
||||
-- einer Transaktion, die am Ende verworfen wird, und es läuft als
|
||||
-- Anwendungsrolle — ein Lauf als Superuser ginge an require_hr_admin() und
|
||||
-- am Zeilenschutz vorbei und bewiese nichts.
|
||||
--
|
||||
-- Die Migrationen 20261007090000 und 20261007100000 prüfen sich selbst, aber
|
||||
-- nur am **Text** der Funktionen. Hier werden sie aufgerufen: Einstellung,
|
||||
-- „Daten ändern" sofort und datiert, der Nachtlauf, Löschen und Berichtigen
|
||||
-- in der Historie, Austritt und Wiedereintritt.
|
||||
--
|
||||
-- Voraussetzung: zwei aktive HR-Konten und ein abgeschaltetes, ein Standort.
|
||||
|
||||
\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 order by p.id limit 1),
|
||||
true
|
||||
) as angemeldet_als;
|
||||
|
||||
-- Die Kennungen der Konten für den Test, vor dem Rollenwechsel gelesen.
|
||||
create temp table hr_konten on commit drop as
|
||||
select
|
||||
(select id from profiles where role = 'hr' and is_active order by id limit 1) as hr1,
|
||||
(select id from profiles where role = 'hr' and is_active order by id offset 1 limit 1) as hr2,
|
||||
(select id from profiles where role = 'hr' and not is_active order by id limit 1) as hr_aus;
|
||||
grant select on hr_konten to alpenwerk_app;
|
||||
|
||||
set local role alpenwerk_app;
|
||||
|
||||
do $test$
|
||||
declare
|
||||
v_hr1 uuid; v_hr2 uuid; v_hr_aus uuid;
|
||||
v_wurzel uuid; v_einheit uuid; v_ort uuid;
|
||||
v_p1 uuid; v_p2 uuid; v_p3 uuid;
|
||||
v_nr int;
|
||||
v_a uuid; v_b uuid;
|
||||
v_e employees%rowtype;
|
||||
v_hist uuid;
|
||||
v_changes jsonb;
|
||||
v_meldung text;
|
||||
v_ging boolean;
|
||||
begin
|
||||
select hr1, hr2, hr_aus into v_hr1, v_hr2, v_hr_aus from hr_konten;
|
||||
if v_hr1 is null or v_hr2 is null or v_hr_aus is null then
|
||||
raise exception 'Voraussetzung fehlt: zwei aktive und ein abgeschaltetes HR-Konto.';
|
||||
end if;
|
||||
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;
|
||||
|
||||
v_einheit := create_org_unit(jsonb_build_object(
|
||||
'parent_id', v_wurzel, 'org_number', 'VERTRAGSTEST', 'name', 'Vertragstest',
|
||||
'unit_type', 'Team', 'valid_from', (current_date - 30)::text));
|
||||
v_p1 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
'job_title', 'Vertragstest Eins', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
||||
v_p2 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
'job_title', 'Vertragstest Zwei', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
||||
v_p3 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
'job_title', 'Vertragstest Drei', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
||||
|
||||
-- ── 1. Einstellung mit allen neuen Angaben ─────────────────────
|
||||
v_a := hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr, 'first_name', 'Vertrag', 'last_name', 'Test-A',
|
||||
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
||||
'position_id', v_p1, 'entry_date', (current_date - 10)::text,
|
||||
'worker_type', 'Arbeiter:in', 'collective_agreement', 'Süßware Arbeiter',
|
||||
'vl', 3, 'has_bonusanspruch', true, 'bonusanspruch_art', 'Shop',
|
||||
'zustaendige_hr_id', v_hr1));
|
||||
select * into v_e from employees where id = v_a;
|
||||
if v_e.vl <> 3 or not v_e.has_bonusanspruch or v_e.bonusanspruch_art <> 'Shop'
|
||||
or v_e.zustaendige_hr_id <> v_hr1 or v_e.collective_agreement::text <> 'Süßware Arbeiter' then
|
||||
raise exception '1: Einstellung hat VL/Bonus/HR/KV nicht geschrieben: % % % % %',
|
||||
v_e.vl, v_e.has_bonusanspruch, v_e.bonusanspruch_art, v_e.zustaendige_hr_id, v_e.collective_agreement;
|
||||
end if;
|
||||
raise notice '1/9 Einstellung mit VL 3, Bonus Shop, zuständiger HR, Süßware Arbeiter';
|
||||
|
||||
-- ── 2. Einstellung ohne Angaben: die Vorgaben ──────────────────
|
||||
v_b := hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 1, 'first_name', 'Vertrag', 'last_name', 'Test-B',
|
||||
'gender', 'm', 'birth_date', '1985-05-05', 'location_id', v_ort,
|
||||
'position_id', v_p2, 'entry_date', current_date::text, 'worker_type', 'Angestellte:r'));
|
||||
select * into v_e from employees where id = v_b;
|
||||
if v_e.vl is not null or v_e.has_bonusanspruch or v_e.bonusanspruch_art is not null or v_e.zustaendige_hr_id is not null then
|
||||
raise exception '2: Ohne Angaben müssen VL, Bonus und HR leer bzw. Nein sein.';
|
||||
end if;
|
||||
if v_e.collective_agreement::text <> 'Süßware Angestellter' then
|
||||
raise exception '2: Vorgabe-Kollektivvertrag für Angestellte ist „%", erwartet „Süßware Angestellter".', v_e.collective_agreement;
|
||||
end if;
|
||||
raise notice '2/9 Vorgaben: VL leer, Bonus Nein, HR leer, KV nach Beschäftigtengruppe';
|
||||
|
||||
-- ── 3. Abweisungen mit lesbarer Meldung ────────────────────────
|
||||
begin
|
||||
perform hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 2, 'first_name', 'X', 'last_name', 'Y', 'gender', 'm',
|
||||
'birth_date', '1980-01-01', 'location_id', v_ort, 'position_id', v_p3,
|
||||
'entry_date', current_date::text, 'has_bonusanspruch', true));
|
||||
v_ging := true;
|
||||
exception when others then v_ging := false; v_meldung := sqlerrm;
|
||||
end;
|
||||
if v_ging or v_meldung not like '%Bonusregelung%' then
|
||||
raise exception '3: Bonus Ja ohne Regelung wurde nicht verständlich abgewiesen: %', v_meldung;
|
||||
end if;
|
||||
|
||||
begin
|
||||
perform hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 2, 'first_name', 'X', 'last_name', 'Y', 'gender', 'm',
|
||||
'birth_date', '1980-01-01', 'location_id', v_ort, 'position_id', v_p3,
|
||||
'entry_date', current_date::text, 'zustaendige_hr_id', v_hr_aus));
|
||||
v_ging := true;
|
||||
exception when others then v_ging := false; v_meldung := sqlerrm;
|
||||
end;
|
||||
if v_ging or v_meldung not like '%zuständige HR%' then
|
||||
raise exception '3: Abgeschaltetes HR-Konto wurde nicht abgewiesen: %', v_meldung;
|
||||
end if;
|
||||
|
||||
begin
|
||||
perform hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 2, 'first_name', 'X', 'last_name', 'Y', 'gender', 'm',
|
||||
'birth_date', '1980-01-01', 'location_id', v_ort, 'position_id', v_p3,
|
||||
'entry_date', current_date::text, 'collective_agreement', 'Süßwaren'));
|
||||
v_ging := true;
|
||||
exception when others then v_ging := false;
|
||||
end;
|
||||
if v_ging then
|
||||
raise exception '3: Der alte Kollektivvertrag „Süßwaren" wurde angenommen.';
|
||||
end if;
|
||||
|
||||
begin
|
||||
perform hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 2, 'first_name', 'X', 'last_name', 'Y', 'gender', 'm',
|
||||
'birth_date', '1980-01-01', 'location_id', v_ort, 'position_id', v_p3,
|
||||
'entry_date', current_date::text, 'vl', 9));
|
||||
v_ging := true;
|
||||
exception when others then v_ging := false;
|
||||
end;
|
||||
if v_ging then
|
||||
raise exception '3: VL 9 wurde angenommen.';
|
||||
end if;
|
||||
raise notice '3/9 Abgewiesen: Bonus ohne Regelung, abgeschaltete HR, „Süßwaren", VL 9';
|
||||
|
||||
-- ── 4. Daten ändern, sofort ────────────────────────────────────
|
||||
perform change_employee_data(jsonb_build_object(
|
||||
'employee_id', v_a, 'effective_date', current_date::text,
|
||||
'person', jsonb_build_object('zustaendige_hr_id', v_hr2),
|
||||
'contract', jsonb_build_object('vl', '5', 'has_bonusanspruch', false, 'bonusanspruch_art', ''),
|
||||
'role', jsonb_build_object('collective_agreement', 'Handel')));
|
||||
select * into v_e from employees where id = v_a;
|
||||
if v_e.vl <> 5 or v_e.has_bonusanspruch or v_e.bonusanspruch_art is not null
|
||||
or v_e.zustaendige_hr_id <> v_hr2 or v_e.collective_agreement::text <> 'Handel' then
|
||||
raise exception '4: Daten ändern hat nicht geschrieben: % % % % %',
|
||||
v_e.vl, v_e.has_bonusanspruch, v_e.bonusanspruch_art, v_e.zustaendige_hr_id, v_e.collective_agreement;
|
||||
end if;
|
||||
|
||||
-- Die Historie nennt jede Änderung mit vorher und nachher.
|
||||
select id, changes into v_hist, v_changes from employee_history
|
||||
where employee_id = v_a and event_type = 'Vertragsänderung' order by created_at desc limit 1;
|
||||
if not (v_changes @> '[{"feld":"VL","vorher":"3","nachher":"5"}]'
|
||||
and v_changes @> '[{"feld":"Bonusanspruch","vorher":"true","nachher":"false"}]'
|
||||
and v_changes @> '[{"feld":"Bonusregelung","vorher":"Shop","nachher":null}]') then
|
||||
raise exception '4: Historie der Vertragsänderung unvollständig: %', v_changes;
|
||||
end if;
|
||||
if not exists (select 1 from employee_history where employee_id = v_a and event_type = 'Stammdatenänderung'
|
||||
and changes @> jsonb_build_array(jsonb_build_object('feld', 'Zuständige HR', 'vorher', v_hr1::text, 'nachher', v_hr2::text))) then
|
||||
raise exception '4: Die Historie nennt den Wechsel der zuständigen HR nicht.';
|
||||
end if;
|
||||
raise notice '4/9 Daten ändern sofort: VL 3→5, Bonus Ja→Nein, HR gewechselt, KV Handel — alles in der Historie';
|
||||
|
||||
-- ── 5. Eintrag löschen und zurücksetzen ───────────────────────
|
||||
perform delete_history_entry(jsonb_build_object('history_id', v_hist));
|
||||
select * into v_e from employees where id = v_a;
|
||||
if v_e.vl <> 3 or not v_e.has_bonusanspruch or v_e.bonusanspruch_art <> 'Shop' then
|
||||
raise exception '5: Zurücksetzen hat VL/Bonus nicht wiederhergestellt: % % %', v_e.vl, v_e.has_bonusanspruch, v_e.bonusanspruch_art;
|
||||
end if;
|
||||
raise notice '5/9 Löschen und zurücksetzen: wieder VL 3, Bonus Ja · Shop';
|
||||
|
||||
-- ── 6. Bonus Ja ohne Regelung über Daten ändern ───────────────
|
||||
perform change_employee_data(jsonb_build_object(
|
||||
'employee_id', v_b, 'effective_date', current_date::text,
|
||||
'person', '{}'::jsonb, 'contract', jsonb_build_object('vl', '1'), 'role', '{}'::jsonb));
|
||||
begin
|
||||
perform change_employee_data(jsonb_build_object(
|
||||
'employee_id', v_b, 'effective_date', current_date::text,
|
||||
'person', '{}'::jsonb, 'contract', jsonb_build_object('has_bonusanspruch', true, 'bonusanspruch_art', ''), 'role', '{}'::jsonb));
|
||||
v_ging := true;
|
||||
exception when others then v_ging := false; v_meldung := sqlerrm;
|
||||
end;
|
||||
if v_ging or v_meldung not like '%Bonusregelung%' then
|
||||
raise exception '6: Daten ändern nahm Bonus Ja ohne Regelung an: %', v_meldung;
|
||||
end if;
|
||||
raise notice '6/9 Daten ändern weist Bonus Ja ohne Regelung verständlich ab';
|
||||
|
||||
-- ── 7. Berichtigen (smallint aus der Feldkarte) ───────────────
|
||||
select id into v_hist from employee_history
|
||||
where employee_id = v_b and event_type = 'Vertragsänderung' order by created_at desc limit 1;
|
||||
perform update_history_entry(jsonb_build_object('history_id', v_hist,
|
||||
'werte', jsonb_build_array(jsonb_build_object('feld', 'VL', 'nachher', '2'))));
|
||||
if (select vl from employees where id = v_b) <> 2 then
|
||||
raise exception '7: Berichtigen hat VL nicht auf 2 gesetzt.';
|
||||
end if;
|
||||
raise notice '7/9 Berichtigen in der Historie: VL 1 → 2';
|
||||
|
||||
-- ── 8. Datiert ändern und der Nachtlauf ───────────────────────
|
||||
perform change_employee_data(jsonb_build_object(
|
||||
'employee_id', v_b, 'effective_date', (current_date + 5)::text,
|
||||
'person', jsonb_build_object('zustaendige_hr_id', v_hr1),
|
||||
'contract', jsonb_build_object('vl', '7', 'has_bonusanspruch', true, 'bonusanspruch_art', 'Außendienst'),
|
||||
'role', '{}'::jsonb));
|
||||
select * into v_e from employees where id = v_b;
|
||||
if v_e.vl <> 2 or v_e.has_bonusanspruch or v_e.zustaendige_hr_id is not null then
|
||||
raise exception '8: Eine datierte Änderung wurde sofort geschrieben.';
|
||||
end if;
|
||||
end
|
||||
$test$;
|
||||
|
||||
-- Den Stichtag der vorgemerkten Änderung auf heute ziehen — als Eigentümer,
|
||||
-- die Anwendungsrolle darf pending_org_changes nicht so ändern.
|
||||
reset role;
|
||||
update pending_org_changes set effective_date = current_date
|
||||
where status = 'pending' and employee_id = (select id from employees where last_name = 'Test-B' and first_name = 'Vertrag');
|
||||
set local role alpenwerk_app;
|
||||
|
||||
do $test2$
|
||||
declare
|
||||
v_b uuid := (select id from employees where last_name = 'Test-B' and first_name = 'Vertrag');
|
||||
v_hr1 uuid := (select hr1 from hr_konten);
|
||||
v_e employees%rowtype;
|
||||
v_a uuid := (select id from employees where last_name = 'Test-A' and first_name = 'Vertrag');
|
||||
v_p3 uuid := (select p.id from om_positions p join jobs j on j.id = p.job_id where j.title = 'Vertragstest Drei');
|
||||
v_ort uuid := (select id from locations order by name limit 1);
|
||||
begin
|
||||
perform apply_due_pending_changes();
|
||||
select * into v_e from employees where id = v_b;
|
||||
if v_e.vl <> 7 or not v_e.has_bonusanspruch or v_e.bonusanspruch_art <> 'Außendienst' or v_e.zustaendige_hr_id <> v_hr1 then
|
||||
raise exception '8: Der Nachtlauf hat VL/Bonus/HR nicht angewendet: % % % %',
|
||||
v_e.vl, v_e.has_bonusanspruch, v_e.bonusanspruch_art, v_e.zustaendige_hr_id;
|
||||
end if;
|
||||
raise notice '8/9 Datierte Änderung: erst nichts, nach dem Nachtlauf VL 7, Bonus Außendienst, HR gesetzt';
|
||||
|
||||
-- ── 9. Austritt und Wiedereintritt ────────────────────────────
|
||||
perform terminate_employee(jsonb_build_object(
|
||||
'employee_id', v_a, 'exit_date', current_date::text,
|
||||
'exit_reason', 'Einvernehmliche Auflösung', 'austrittsart', 'freiwillig'));
|
||||
perform rehire_employee(jsonb_build_object(
|
||||
'employee_id', v_a, 'rehire_date', current_date::text, 'position_id', v_p3,
|
||||
'first_name', 'Vertrag', 'last_name', 'Test-A', 'gender', 'w',
|
||||
'birth_date', '1990-01-01', 'nationality', 'Österreich', 'location_id', v_ort,
|
||||
'vl', 8, 'has_bonusanspruch', false, 'zustaendige_hr_id', v_hr1,
|
||||
'collective_agreement', 'Süßware Angestellter'));
|
||||
select * into v_e from employees where id = v_a;
|
||||
if v_e.vl <> 8 or v_e.has_bonusanspruch or v_e.bonusanspruch_art is not null
|
||||
or v_e.zustaendige_hr_id <> v_hr1 or v_e.collective_agreement::text <> 'Süßware Angestellter' then
|
||||
raise exception '9: Wiedereintritt hat VL/Bonus/HR/KV nicht übernommen: % % % % %',
|
||||
v_e.vl, v_e.has_bonusanspruch, v_e.bonusanspruch_art, v_e.zustaendige_hr_id, v_e.collective_agreement;
|
||||
end if;
|
||||
raise notice '9/9 Wiedereintritt übernimmt VL 8, Bonus Nein, HR und Süßware Angestellter';
|
||||
|
||||
raise notice 'Vertragsangaben-Rauchtest bestanden. Alles Angelegte wird gleich verworfen.';
|
||||
end
|
||||
$test2$;
|
||||
|
||||
rollback;
|
||||
|
||||
-- Gegenprobe: nichts ist geblieben.
|
||||
select
|
||||
(select count(*) from org_units where org_number = 'VERTRAGSTEST') as einheiten,
|
||||
(select count(*) from employees where first_name = 'Vertrag') as personen;
|
||||
Reference in New Issue
Block a user