diff --git a/db/migrations/20261201100000_rueckkehr_im_nachtlauf_mit_vorher_werten.sql b/db/migrations/20261201100000_rueckkehr_im_nachtlauf_mit_vorher_werten.sql new file mode 100644 index 0000000..7b1c102 --- /dev/null +++ b/db/migrations/20261201100000_rueckkehr_im_nachtlauf_mit_vorher_werten.sql @@ -0,0 +1,188 @@ +-- Eine im Nachtlauf verbuchte Rückkehr hält ihre Vorher-Werte fest +-- +-- Eine Rückkehr, die sofort erfasst wird (record_karenz_return mit einem +-- Datum heute oder früher), schreibt in ihre Historienzeile, was vorher galt: +-- Status, Art der Abwesenheit, Beginn, Ende, gegebenenfalls Ausmass und +-- Stunden. Daran setzt delete_history_entry beim Zurücknehmen an. +-- +-- Eine geplante Rückkehr bekommt ihre Zeile schon beim Planen, zu dem +-- Zeitpunkt ohne Werte — geändert ist noch nichts. Verbucht der Nachtlauf sie +-- am Stichtag, müssen die Werte von diesem Augenblick in die Zeile. Sonst +-- liesse sie sich nicht zurücknehmen, und die Abwesenheit davor ebenfalls +-- nicht, weil eine Rückkehr zuerst gelöscht werden muss. Ein Austritt, der +-- vor einer solchen Rückkehr liegt, liesse sich dann gar nicht erfassen. +-- +-- 1. app_rueckkehr_aenderungen(): die Vorher-Werte aus der Personenzeile +-- und dem geplanten Vorgang, in derselben Form wie record_karenz_return. +-- 2. apply_due_pending_changes schreibt sie in die Historienzeile, bevor er +-- die Person zurücksetzt. Ergänzt an der laufenden Definition, an genau +-- einem Anker. +-- 3. Bestand: bereits im Nachtlauf verbuchte Rückkehren ohne Werte bekommen +-- sie nachgetragen, aus der Abwesenheit davor. Kam die Person mit anderem +-- Ausmass zurück, sind die Stunden von davor nicht sicher bekannt; solche +-- Zeilen bleiben unverändert und werden gezählt. + +create or replace function public.app_rueckkehr_aenderungen(p_alt employees, p_plan jsonb) +returns jsonb +language plpgsql +stable +set search_path = public, pg_temp +as $$ +declare + v jsonb := '[]'::jsonb; + v_ausmass employment_type := nullif(p_plan->>'employment_type', '')::employment_type; + v_stunden numeric := nullif(p_plan->>'weekly_hours', '')::numeric; +begin + v := app_aenderung(v, 'Status', p_alt.status::text, 'Aktiv'); + v := app_aenderung(v, 'Art der Abwesenheit', p_alt.absence_type, null); + v := app_aenderung(v, 'Abwesend ab', p_alt.karenz_start_date::text, null); + v := app_aenderung(v, 'Geplante Rückkehr', p_alt.karenz_return_date::text, null); + if v_ausmass is not null then + v := app_aenderung(v, 'Beschäftigungsausmaß', p_alt.employment_type::text, v_ausmass::text); + v := app_aenderung(v, 'Wochenstunden', trim_scale(p_alt.weekly_hours)::text, trim_scale(v_stunden::numeric(4,2))::text); + end if; + -- Dieselbe Bedingung wie im Nachtlauf: steht die Teilzeitvariante im + -- Vorgang, setzt er Variante und Ende. + if p_plan ? 'teilzeit_art' then + v := app_aenderung(v, 'Teilzeitvariante', p_alt.teilzeit_art, nullif(p_plan->>'teilzeit_art', '')); + v := app_aenderung(v, 'Teilzeit bis', p_alt.teilzeit_bis::text, nullif(p_plan->>'teilzeit_bis', '')); + end if; + return v; +end; +$$; + +revoke execute on function public.app_rueckkehr_aenderungen(employees, jsonb) from public; + +comment on function public.app_rueckkehr_aenderungen(employees, jsonb) is + 'Vorher-Werte einer Rückkehr für die Historie: aus der Personenzeile vor dem Verbuchen und dem geplanten Vorgang.'; + +-- ── Nachtlauf ──────────────────────────────────────────────────────────── +do $migration$ +declare + v_anker constant text := $a$ elsif v_rec.change_type = 'karenz_return' then +$a$; + v_neu constant text := $a$ elsif v_rec.change_type = 'karenz_return' then + -- Die Werte von davor, wie bei einer sofort erfassten Rückkehr: ohne + -- sie liesse sich die Rückkehr in der Historie nicht zurücknehmen. + update employee_history h + set changes = app_rueckkehr_aenderungen(e, v_rec.payload) + from employees e + where e.id = v_rec.employee_id and h.pending_id = v_rec.id and h.event_type = 'Rückkehr'; +$a$; + v_def text; + v_treffer integer; +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.prokind = 'f' and p.proname = 'apply_due_pending_changes'; + if v_def is null then + raise exception 'apply_due_pending_changes ist nicht vorhanden.'; + end if; + if position('app_rueckkehr_aenderungen(e, v_rec.payload)' in v_def) > 0 then + raise notice 'apply_due_pending_changes ist bereits angepasst.'; + return; + end if; + v_treffer := (length(v_def) - length(replace(v_def, v_anker, ''))) / length(v_anker); + if v_treffer <> 1 then + raise exception 'apply_due_pending_changes: der Anker kommt % mal vor, erwartet genau einmal.', v_treffer; + end if; + execute replace(v_def, v_anker, v_neu); +end +$migration$; + +-- ── Bestand ────────────────────────────────────────────────────────────── +do $bestand$ +declare + v_zeile record; + v_karenz record; + v_art text; + v_nachgetragen integer := 0; + v_ausmass integer := 0; + v_ohne_abwesenheit integer := 0; +begin + for v_zeile in + select h.id, h.employee_id, h.event_date, h.created_at, p.payload + from employee_history h + join pending_org_changes p on p.id = h.pending_id + where h.event_type = 'Rückkehr' and p.status = 'applied' + and coalesce(h.changes, '[]'::jsonb) = '[]'::jsonb + loop + if nullif(v_zeile.payload->>'employment_type', '') is not null then + v_ausmass := v_ausmass + 1; + continue; + end if; + + select k.event_date, k.changes into v_karenz + from employee_history k + where k.employee_id = v_zeile.employee_id and k.event_type = 'Karenz' + and (k.event_date, k.created_at) < (v_zeile.event_date, v_zeile.created_at) + order by k.event_date desc, k.created_at desc + limit 1; + if not found then + v_ohne_abwesenheit := v_ohne_abwesenheit + 1; + continue; + end if; + select a->>'nachher' into v_art + from jsonb_array_elements(coalesce(v_karenz.changes, '[]'::jsonb)) a + where a->>'feld' = 'Art der Abwesenheit'; + + update employee_history set changes = + app_aenderung(app_aenderung(app_aenderung(app_aenderung('[]'::jsonb, + 'Status', 'Karenz', 'Aktiv'), + 'Art der Abwesenheit', v_art, null), + 'Abwesend ab', v_karenz.event_date::text, null), + 'Geplante Rückkehr', v_zeile.event_date::text, null) + where id = v_zeile.id; + v_nachgetragen := v_nachgetragen + 1; + end loop; + + raise notice 'Rückkehren aus dem Nachtlauf: % nachgetragen, % mit geändertem Ausmass unverändert, % ohne Abwesenheit davor unverändert.', + v_nachgetragen, v_ausmass, v_ohne_abwesenheit; +end +$bestand$; + +-- ── Prüfung ────────────────────────────────────────────────────────────── +do $pruefung$ +declare + v_def text; + v_offen integer; +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.prokind = 'f' and p.proname = 'apply_due_pending_changes'; + if (length(v_def) - length(replace(v_def, 'app_rueckkehr_aenderungen(e, v_rec.payload)', ''))) + / length('app_rueckkehr_aenderungen(e, v_rec.payload)') <> 1 then + raise exception 'apply_due_pending_changes schreibt die Vorher-Werte der Rückkehr nicht genau einmal.'; + end if; + -- Was der Nachtlauf schon konnte, bleibt: Onboarding-Liste und + -- Teilzeitvariante bei der Rückkehr, je Vormerkung ein eigener Block. + if position($a$'onboarding', 'Rückkehr'$a$ in v_def) = 0 + or position($a$v_rec.payload ? 'teilzeit_art'$a$ in v_def) = 0 + or position('exception when others' in v_def) = 0 then + raise exception 'apply_due_pending_changes hat bei der Anpassung Verhalten verloren.'; + 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 'apply_due_pending_changes ist nicht mehr SECURITY DEFINER.'; + end if; + if exists (select 1 from pg_proc p join pg_namespace n on n.oid = p.pronamespace + where n.nspname = 'public' and p.proname = 'app_rueckkehr_aenderungen' + and (p.prosecdef or p.proconfig is null)) then + raise exception 'app_rueckkehr_aenderungen muss ohne SECURITY DEFINER und mit festem search_path laufen.'; + end if; + + -- Nach dem Nachtrag ohne Werte nur noch die gezählten Ausnahmen. + select count(*) into v_offen + from employee_history h + join pending_org_changes p on p.id = h.pending_id + where h.event_type = 'Rückkehr' and p.status = 'applied' + and coalesce(h.changes, '[]'::jsonb) = '[]'::jsonb + and nullif(p.payload->>'employment_type', '') is null + and exists (select 1 from employee_history k + where k.employee_id = h.employee_id and k.event_type = 'Karenz' + and (k.event_date, k.created_at) < (h.event_date, h.created_at)); + if v_offen > 0 then + raise exception 'Es gibt noch % im Nachtlauf verbuchte Rückkehren ohne Vorher-Werte.', v_offen; + end if; +end +$pruefung$;