Austritt und letzte Versetzung zuruecknehmen, Planstelle beim Eintritt berichtigen, Uebersicht Anstehend, Teilzeitvariante, Notiz erledigt von
Migration 20261019140000: - austritt_zuruecknehmen: Austrittsangaben leeren, Status neu ableiten, beendete Besetzung wieder oeffnen, wenn die Planstelle frei ist (sonst ohne Besetzung und mit Hinweis, dass eine andere gebraucht wird), Offboarding-Checkliste loeschen, Austritt aus der Historie nehmen und die Ruecknahme als eigene Zeile vermerken. Nichtantritt ausgenommen. - versetzung_zuruecknehmen: nur die letzte wirksame Versetzung, ohne spaetere Befoerderung; die Person kehrt auf die vorherige Planstelle zurueck, wenn diese frei ist, sonst Abweisung. delete_history_entry leitet die wirksame Versetzung dorthin um (Papierkorb in der Historie). - eintritt_planstelle_berichtigen: beim Eintritt gewaehlte Planstelle richtigstellen, solange keine Versetzung/Befoerderung folgte; keine Versetzung in der Historie. - app_planstelle_frei_ab: gemeinsame Pruefung (besetzt oder vorgemerkt). Oberflaeche: - Daten aendern: "Austritt zuruecknehmen" mit Rueckfrage; Abschnitt "Planstelle beim Eintritt". - Uebersicht, Karte Anstehend: Art "Anstehend" mit der vorgemerkten Historie aller Personen. - Teilzeitvariante auch im Reiter Stammdaten, "Keine" statt Strich; in den Formularen heisst der leere Eintrag "Keine (vertragliche Stundenaenderung)". - Notizen: bei erledigten steht, von wem und wann. Rauchtest Schritte 15-17. Geprueft: lint, tsc, types:check, 919 Tests, Waechter
This commit is contained in:
@@ -0,0 +1,343 @@
|
||||
-- Drei Vorgänge, die sich zurücknehmen oder berichtigen lassen:
|
||||
--
|
||||
-- 1. Ein erfasster Austritt wird vollständig zurückgenommen
|
||||
-- (austritt_zuruecknehmen).
|
||||
-- 2. Die letzte wirksame Versetzung wird gelöscht; die Person sitzt danach
|
||||
-- wieder auf der vorherigen Planstelle (versetzung_zuruecknehmen, über
|
||||
-- den Papierkorb in der Historie).
|
||||
-- 3. Die beim Eintritt gewählte Planstelle wird berichtigt, ohne dass eine
|
||||
-- Versetzung entsteht (eintritt_planstelle_berichtigen).
|
||||
--
|
||||
-- Alle drei bewegen Besetzungen. Gemeinsam ist ihnen die Frage, ob die
|
||||
-- Planstelle, auf die die Person zurückkehrt oder kommt, frei ist — besetzt
|
||||
-- von jemand anderem oder vorgemerkt (planstelle_vorgemerkt). Ist sie es
|
||||
-- nicht, wird abgewiesen bzw. beim Austritt ohne Besetzung zurückgenommen
|
||||
-- und das gesagt.
|
||||
|
||||
-- ── Hilfsfunktion: frei ab einem Tag? ────────────────────────────────────
|
||||
create or replace function public.app_planstelle_frei_ab(p_position_id uuid, p_ab date, p_employee_id uuid)
|
||||
returns boolean
|
||||
language sql
|
||||
stable
|
||||
set search_path = public, pg_temp
|
||||
as $$
|
||||
select not exists (
|
||||
select 1 from position_assignments pa
|
||||
where pa.position_id = p_position_id
|
||||
and pa.employee_id <> p_employee_id
|
||||
and (pa.valid_to is null or pa.valid_to > p_ab))
|
||||
and planstelle_vorgemerkt(p_position_id, p_ab, p_employee_id) is null
|
||||
$$;
|
||||
|
||||
grant execute on function public.app_planstelle_frei_ab(uuid, date, uuid) to alpenwerk_app;
|
||||
|
||||
-- ── 1. Austritt zurücknehmen ─────────────────────────────────────────────
|
||||
--
|
||||
-- Fachlich vorgegeben: eine begonnene Offboarding-Checkliste wird gelöscht;
|
||||
-- ist die Planstelle inzwischen neu besetzt oder vorgemerkt, sagt die
|
||||
-- Rückmeldung, dass für die Person eine andere gebraucht wird; ein
|
||||
-- Nichtantritt ist ausgenommen; die Rücknahme bleibt in der Historie
|
||||
-- sichtbar.
|
||||
--
|
||||
-- Die Zeile „Austritt" verschwindet aus der Historie — sonst zählten
|
||||
-- Berichte einen Austritt, der nicht stattfindet. An ihrer Stelle steht eine
|
||||
-- Zeile, die die Rücknahme nennt, ohne Feldwerte: sie lässt sich nicht über
|
||||
-- den Papierkorb „zurücknehmen", denn das hiesse, den Austritt ohne seine
|
||||
-- Besetzung wiederherzustellen.
|
||||
--
|
||||
-- Gibt einen Hinweis zurück, wenn die Planstelle nicht wiederhergestellt
|
||||
-- werden konnte; sonst null.
|
||||
create or replace function public.austritt_zuruecknehmen(payload jsonb)
|
||||
returns text
|
||||
language plpgsql
|
||||
set search_path = public, pg_temp
|
||||
as $$
|
||||
declare
|
||||
v_employee_id uuid := (payload->>'employee_id')::uuid;
|
||||
v_person employees%rowtype;
|
||||
v_besetzung position_assignments%rowtype;
|
||||
v_nummer text;
|
||||
v_hinweis text;
|
||||
begin
|
||||
perform require_hr_admin();
|
||||
|
||||
select * into v_person from employees where id = v_employee_id for update;
|
||||
if not found then
|
||||
raise exception 'Die Person existiert nicht.';
|
||||
end if;
|
||||
if v_person.exit_date is null then
|
||||
raise exception 'Für diese Person ist kein Austritt erfasst.';
|
||||
end if;
|
||||
if v_person.exit_reason = 'No Show' then
|
||||
raise exception 'Ein Nichtantritt lässt sich hier nicht zurücknehmen.';
|
||||
end if;
|
||||
|
||||
-- Die Besetzung, die der Austritt beendet hat.
|
||||
select * into v_besetzung from position_assignments
|
||||
where employee_id = v_employee_id and valid_to = v_person.exit_date
|
||||
order by valid_from desc
|
||||
limit 1;
|
||||
|
||||
if found then
|
||||
select position_number into v_nummer from om_positions where id = v_besetzung.position_id;
|
||||
if app_planstelle_frei_ab(v_besetzung.position_id, v_person.exit_date, v_employee_id) then
|
||||
update position_assignments set valid_to = null where id = v_besetzung.id;
|
||||
else
|
||||
v_hinweis := 'Die Planstelle ' || coalesce(v_nummer, '') || ' ist inzwischen neu besetzt oder vorgemerkt. '
|
||||
|| 'Für diese Person wird eine andere benötigt — bitte über „Versetzen" zuweisen.';
|
||||
end if;
|
||||
else
|
||||
v_hinweis := 'Zu diesem Austritt war keine Planstelle beendet worden. Bitte über „Versetzen" eine zuweisen.';
|
||||
end if;
|
||||
|
||||
update employees set
|
||||
exit_date = null,
|
||||
exit_reason = null,
|
||||
austrittsart = null,
|
||||
status = (case
|
||||
when entry_date > current_date then 'Geplant'
|
||||
when karenz_start_date is not null and karenz_start_date <= current_date
|
||||
and (karenz_return_date is null or karenz_return_date > current_date) then 'Karenz'
|
||||
else 'Aktiv' end)::employment_status
|
||||
where id = v_employee_id;
|
||||
|
||||
delete from offboarding_tasks where employee_id = v_employee_id;
|
||||
|
||||
delete from employee_history
|
||||
where employee_id = v_employee_id and event_type = 'Austritt' and event_date = v_person.exit_date;
|
||||
|
||||
insert into employee_history (employee_id, event_date, event_type, description)
|
||||
values (v_employee_id, app_stichtag_vor_eintritt(v_employee_id, current_date), 'Stammdatenänderung',
|
||||
'Austritt zum ' || to_char(v_person.exit_date, 'DD.MM.YYYY') || ' zurückgenommen ('
|
||||
|| coalesce(v_person.exit_reason, '-') || ')'
|
||||
|| case when v_hinweis is not null then ' — ohne Planstelle' else '' end);
|
||||
|
||||
insert into audit_log (actor_user_id, actor_name, action, target_label, target_employee_id, details)
|
||||
values (app_current_user_id(), current_actor_name(), 'Austritt zurückgenommen',
|
||||
v_person.first_name || ' ' || v_person.last_name, v_employee_id,
|
||||
'Austritt zum ' || v_person.exit_date || ' (' || coalesce(v_person.exit_reason, '-') || ') zurückgenommen'
|
||||
|| coalesce('. ' || v_hinweis, ''));
|
||||
|
||||
return v_hinweis;
|
||||
end;
|
||||
$$;
|
||||
|
||||
grant execute on function public.austritt_zuruecknehmen(jsonb) to alpenwerk_app;
|
||||
|
||||
-- ── 2. Letzte Versetzung zurücknehmen ────────────────────────────────────
|
||||
--
|
||||
-- Nur die letzte, fachlich so vorgegeben; nach ihr darf auch keine
|
||||
-- Beförderung stehen, sonst bezöge sich diese auf eine Planstelle, die die
|
||||
-- Person danach nicht mehr hat. Ist die vorherige Planstelle inzwischen
|
||||
-- besetzt oder vorgemerkt, wird abgewiesen.
|
||||
create or replace function public.versetzung_zuruecknehmen(p_history_id uuid)
|
||||
returns void
|
||||
language plpgsql
|
||||
set search_path = public, pg_temp
|
||||
as $$
|
||||
declare
|
||||
v_eintrag employee_history%rowtype;
|
||||
v_neu position_assignments%rowtype;
|
||||
v_alt position_assignments%rowtype;
|
||||
v_alt_nummer text;
|
||||
v_neu_nummer text;
|
||||
begin
|
||||
perform require_hr_admin();
|
||||
|
||||
select * into v_eintrag from employee_history where id = p_history_id;
|
||||
if not found or v_eintrag.event_type <> 'Versetzung' then
|
||||
raise exception 'Das ist keine Versetzung.';
|
||||
end if;
|
||||
if v_eintrag.event_date > current_date or app_vormerkung_offen(v_eintrag.pending_id) then
|
||||
raise exception 'Diese Versetzung ist nicht wirksam — sie wird abgebrochen, nicht zurückgenommen.';
|
||||
end if;
|
||||
if exists (
|
||||
select 1 from employee_history h
|
||||
where h.employee_id = v_eintrag.employee_id
|
||||
and h.event_type in ('Versetzung', 'Beförderung')
|
||||
and (h.event_date, h.created_at) > (v_eintrag.event_date, v_eintrag.created_at)
|
||||
) then
|
||||
raise exception 'Nur die letzte Versetzung lässt sich zurücknehmen; danach steht noch eine Versetzung oder Beförderung.';
|
||||
end if;
|
||||
|
||||
select * into v_neu from position_assignments
|
||||
where employee_id = v_eintrag.employee_id and valid_from = v_eintrag.event_date
|
||||
order by valid_from desc
|
||||
limit 1;
|
||||
if not found then
|
||||
raise exception 'Die Besetzung dieser Versetzung ist nicht mehr vorhanden.';
|
||||
end if;
|
||||
|
||||
select * into v_alt from position_assignments
|
||||
where employee_id = v_eintrag.employee_id and valid_to = v_eintrag.event_date and id <> v_neu.id
|
||||
order by valid_from desc
|
||||
limit 1;
|
||||
if not found then
|
||||
raise exception 'Vor dieser Versetzung gab es keine Besetzung, auf die die Person zurückkehren könnte.';
|
||||
end if;
|
||||
|
||||
select position_number into v_alt_nummer from om_positions where id = v_alt.position_id;
|
||||
select position_number into v_neu_nummer from om_positions where id = v_neu.position_id;
|
||||
|
||||
if not app_planstelle_frei_ab(v_alt.position_id, v_eintrag.event_date, v_eintrag.employee_id) then
|
||||
raise exception 'Die vorherige Planstelle % ist inzwischen besetzt oder vorgemerkt — die Versetzung lässt sich nicht löschen.', v_alt_nummer;
|
||||
end if;
|
||||
|
||||
delete from position_assignments where id = v_neu.id;
|
||||
update position_assignments set valid_to = v_neu.valid_to where id = v_alt.id;
|
||||
update employees
|
||||
set job_title = (select j.title from om_positions p join jobs j on j.id = p.job_id where p.id = v_alt.position_id)
|
||||
where id = v_eintrag.employee_id;
|
||||
|
||||
delete from employee_history where id = v_eintrag.id;
|
||||
|
||||
insert into audit_log (actor_user_id, actor_name, action, target_label, target_employee_id, details)
|
||||
values (app_current_user_id(), current_actor_name(), 'Versetzung zurückgenommen',
|
||||
(select first_name || ' ' || last_name from employees where id = v_eintrag.employee_id), v_eintrag.employee_id,
|
||||
'Versetzung vom ' || v_eintrag.event_date || ' auf ' || coalesce(v_neu_nummer, '-')
|
||||
|| ' zurückgenommen, wieder auf ' || coalesce(v_alt_nummer, '-'));
|
||||
end;
|
||||
$$;
|
||||
|
||||
grant execute on function public.versetzung_zuruecknehmen(uuid) to alpenwerk_app;
|
||||
|
||||
-- delete_history_entry leitet die wirksame Versetzung dorthin um, statt sie
|
||||
-- abzuweisen. So bleibt der Papierkorb in der Historie der eine Weg.
|
||||
do $migration$
|
||||
declare
|
||||
v_def text;
|
||||
v_treffer integer;
|
||||
v_anker constant text :=
|
||||
E' if v_eintrag.event_type in (''Versetzung'', ''Beförderung'') and v_eintrag.event_date <= current_date\n'
|
||||
|| E' and not app_vormerkung_offen(v_eintrag.pending_id) then\n';
|
||||
v_neu constant text :=
|
||||
E' if v_eintrag.event_type = ''Versetzung'' and v_eintrag.event_date <= current_date\n'
|
||||
|| E' and not app_vormerkung_offen(v_eintrag.pending_id) then\n'
|
||||
|| E' perform versetzung_zuruecknehmen(v_eintrag.id);\n'
|
||||
|| E' return;\n'
|
||||
|| E' end if;\n'
|
||||
|| v_anker;
|
||||
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 = 'delete_history_entry';
|
||||
|
||||
if position('versetzung_zuruecknehmen' in v_def) > 0 then
|
||||
raise notice 'delete_history_entry leitet die Versetzung bereits um — nichts zu tun.';
|
||||
else
|
||||
v_treffer := (length(v_def) - length(replace(v_def, v_anker, ''))) / length(v_anker);
|
||||
if v_treffer <> 1 then
|
||||
raise exception 'Der Anker in delete_history_entry kommt % mal vor, erwartet genau einmal.', v_treffer;
|
||||
end if;
|
||||
execute replace(v_def, v_anker, v_neu);
|
||||
end if;
|
||||
end
|
||||
$migration$;
|
||||
|
||||
-- ── 3. Planstelle beim Eintritt berichtigen ──────────────────────────────
|
||||
--
|
||||
-- Fachlich vorgegeben: eine beim Eintritt falsch gewählte Planstelle wird
|
||||
-- über „Daten ändern" berichtigt, nicht über eine Versetzung — es hat nie
|
||||
-- eine stattgefunden. Möglich, solange nach dem Eintritt weder Versetzung
|
||||
-- noch Beförderung steht; danach ist „Versetzen" der Weg.
|
||||
create or replace function public.eintritt_planstelle_berichtigen(payload jsonb)
|
||||
returns void
|
||||
language plpgsql
|
||||
set search_path = public, pg_temp
|
||||
as $$
|
||||
declare
|
||||
v_employee_id uuid := (payload->>'employee_id')::uuid;
|
||||
v_ziel uuid := (payload->>'position_id')::uuid;
|
||||
v_person employees%rowtype;
|
||||
v_besetzung position_assignments%rowtype;
|
||||
v_alt_nummer text;
|
||||
v_neu_nummer text;
|
||||
v_titel text;
|
||||
begin
|
||||
perform require_hr_admin();
|
||||
|
||||
select * into v_person from employees where id = v_employee_id;
|
||||
if not found then
|
||||
raise exception 'Die Person existiert nicht.';
|
||||
end if;
|
||||
if exists (select 1 from employee_history
|
||||
where employee_id = v_employee_id and event_type in ('Versetzung', 'Beförderung')) then
|
||||
raise exception 'Nach dem Eintritt gab es bereits eine Versetzung oder Beförderung — die Planstelle wird über „Versetzen" geändert.';
|
||||
end if;
|
||||
|
||||
select * into v_besetzung from position_assignments
|
||||
where employee_id = v_employee_id and valid_from = v_person.entry_date
|
||||
order by valid_from
|
||||
limit 1;
|
||||
if not found then
|
||||
raise exception 'Zu dieser Person ist keine Besetzung ab dem Eintritt erfasst.';
|
||||
end if;
|
||||
if v_besetzung.position_id = v_ziel then
|
||||
raise exception 'Das ist bereits die Planstelle dieser Person.';
|
||||
end if;
|
||||
|
||||
select p.position_number, j.title into v_neu_nummer, v_titel
|
||||
from om_positions p join jobs j on j.id = p.job_id
|
||||
where p.id = v_ziel
|
||||
and p.valid_from <= v_besetzung.valid_from
|
||||
and (p.valid_to is null or p.valid_to > v_besetzung.valid_from);
|
||||
if not found then
|
||||
raise exception 'Diese Planstelle gilt zum Eintritt am % nicht.', to_char(v_person.entry_date, 'DD.MM.YYYY');
|
||||
end if;
|
||||
if not app_planstelle_frei_ab(v_ziel, v_besetzung.valid_from, v_employee_id) then
|
||||
raise exception 'Die Planstelle % ist ab dem Eintritt besetzt oder vorgemerkt.', v_neu_nummer;
|
||||
end if;
|
||||
|
||||
select position_number into v_alt_nummer from om_positions where id = v_besetzung.position_id;
|
||||
|
||||
update position_assignments set position_id = v_ziel where id = v_besetzung.id;
|
||||
update employees set job_title = v_titel where id = v_employee_id;
|
||||
|
||||
insert into employee_history (employee_id, event_date, event_type, description)
|
||||
values (v_employee_id, app_stichtag_vor_eintritt(v_employee_id, current_date), 'Stammdatenänderung',
|
||||
'Planstelle beim Eintritt berichtigt: ' || coalesce(v_alt_nummer, '-') || ' → ' || v_neu_nummer);
|
||||
|
||||
insert into audit_log (actor_user_id, actor_name, action, target_label, target_employee_id, details)
|
||||
values (app_current_user_id(), current_actor_name(), 'Planstelle beim Eintritt berichtigt',
|
||||
v_person.first_name || ' ' || v_person.last_name, v_employee_id,
|
||||
coalesce(v_alt_nummer, '-') || ' → ' || v_neu_nummer);
|
||||
end;
|
||||
$$;
|
||||
|
||||
grant execute on function public.eintritt_planstelle_berichtigen(jsonb) to alpenwerk_app;
|
||||
|
||||
-- ── Selbstprüfung ─────────────────────────────────────────────────────────
|
||||
do $pruefung$
|
||||
declare
|
||||
v_fn text;
|
||||
v_def text;
|
||||
begin
|
||||
foreach v_fn in array array['austritt_zuruecknehmen', 'versetzung_zuruecknehmen', 'eintritt_planstelle_berichtigen'] loop
|
||||
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 = v_fn;
|
||||
if position('require_hr_admin()' in v_def) = 0 then
|
||||
raise exception '% prüft die Rechte nicht.', v_fn;
|
||||
end if;
|
||||
if position('insert into audit_log' in v_def) = 0 then
|
||||
raise exception '% schreibt kein Protokoll.', v_fn;
|
||||
end if;
|
||||
if position('app_planstelle_frei_ab(' in v_def) = 0 then
|
||||
raise exception '% prüft nicht, ob die Planstelle frei ist.', v_fn;
|
||||
end if;
|
||||
if position('auth.uid()' in v_def) > 0 then
|
||||
raise exception '% ruft auth.uid().', v_fn;
|
||||
end if;
|
||||
end loop;
|
||||
|
||||
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 = 'delete_history_entry';
|
||||
-- Die wirksame Versetzung geht zur Rücknahme, die wirksame Beförderung
|
||||
-- bleibt abgewiesen.
|
||||
if position('perform versetzung_zuruecknehmen(v_eintrag.id)' in v_def) = 0
|
||||
or position('Rückgängig macht sie nur eine neue Versetzung' in v_def) = 0 then
|
||||
raise exception 'delete_history_entry leitet die Versetzung nicht wie erwartet um.';
|
||||
end if;
|
||||
end
|
||||
$pruefung$;
|
||||
Reference in New Issue
Block a user