Struktur anlegen und abschliessen: Standort, Taetigkeit, Planstellennummer, Ende einer Einheit
Grundlage fuer den Import. Standort und Taetigkeit entstehen ueber eigene Funktionen mit Protokoll. create_position uebernimmt eine vorgegebene Planstellennummer. set_org_unit_valid_to setzt, verschiebt oder hebt das Ende einer Einheit auf; abgeschlossen wird nur, wenn darunter keine Einheit und keine Planstelle ueber das Ende hinaus gilt. update_position prueft das Ende einer Planstelle gegen Besetzungen und Vormerkungen zu diesem Datum statt gegen heute und vergibt Jobnummern ueber job_fuer_titel. Zwei Trigger halten fest: eine Besetzung endet spaetestens mit ihrer Planstelle, eine Planstelle spaetestens mit ihrer Einheit. Unter einer Einheit mit Ende entsteht keine neue Einheit und keine Planstelle; die Oberflaeche bietet das dort nicht mehr an. Geprueft: alle Migrationen auf leerer Datenbank, zweiter Lauf ohne Aenderung; rauchtest.sql 33/33, vertragsangaben.sql 9/9; lint, tsc, types:check, 961 Tests, Uebergabe-Waechter.
This commit is contained in:
@@ -19,7 +19,7 @@ import { loadDashboardData } from "@/lib/dashboard-data";
|
||||
import { KPI_ERKLAERUNGEN } from "@/lib/kpi-erklaerungen";
|
||||
import { addDaysIso, fmtName, todayIso } from "@/lib/format";
|
||||
import { anzeige } from "@/lib/anzeige";
|
||||
import { divisionOf } from "@/lib/org";
|
||||
import { bestehtAm, divisionOf } from "@/lib/org";
|
||||
import { deriveStatusAsOf } from "@/lib/reports";
|
||||
import { currentUserId } from "@/lib/auth/session";
|
||||
import { withUser } from "@/lib/db";
|
||||
@@ -154,7 +154,7 @@ export default async function DashboardPage({
|
||||
);
|
||||
}
|
||||
const divisionBars = orgMaps.unitList
|
||||
.filter((u) => u.unit_type === "Bereich")
|
||||
.filter((u) => u.unit_type === "Bereich" && bestehtAm(u, today))
|
||||
.map((d) => ({ name: d.name, count: headcountByDivision.get(d.id) ?? 0 }))
|
||||
.sort((a, b) => b.count - a.count);
|
||||
const maxDivisionCount = Math.max(1, ...divisionBars.map((d) => d.count));
|
||||
|
||||
@@ -5,7 +5,7 @@ import { kostenstellenAbfrage } from "@/lib/cost-centers";
|
||||
import { withUser } from "@/lib/db";
|
||||
import { jsonArrayFrom } from "@/lib/db/json";
|
||||
import { daysBetweenIso, todayIso } from "@/lib/format";
|
||||
import { buildOrgMaps, orgMapsAbfragen } from "@/lib/org";
|
||||
import { buildOrgMaps, nimmtNeuesAuf, orgMapsAbfragen } from "@/lib/org";
|
||||
import { offeneStellenAbfrage, resolveOpenPositions, type OffeneStelle } from "@/lib/positions";
|
||||
|
||||
export default async function PositionsPage() {
|
||||
@@ -43,6 +43,7 @@ export default async function PositionsPage() {
|
||||
unit_type: u.unit_type,
|
||||
depth: orgMaps.depthOf.get(u.id) ?? 0,
|
||||
hasChief: withChief.has(u.id),
|
||||
endet: !nimmtNeuesAuf(u),
|
||||
}));
|
||||
|
||||
const openPositionsWithDays = openPositions.map((p) => ({ ...p, daysOpen: daysBetweenIso(p.vacantSince) }));
|
||||
|
||||
@@ -286,10 +286,14 @@ export function OrgUnitPanel({
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap gap-2 border-t border-border pt-4">
|
||||
<Button variant="secondary" size="sm" onClick={() => onAddUnit(unit.id)}>
|
||||
<Plus className="h-4 w-4" />
|
||||
Einheit darunter anlegen
|
||||
</Button>
|
||||
{/* Unter einer Einheit mit Ende entsteht nichts Unbefristetes mehr;
|
||||
die Datenbank wiese es ab. */}
|
||||
{!unit.valid_to && (
|
||||
<Button variant="secondary" size="sm" onClick={() => onAddUnit(unit.id)}>
|
||||
<Plus className="h-4 w-4" />
|
||||
Einheit darunter anlegen
|
||||
</Button>
|
||||
)}
|
||||
{entfernbar && (
|
||||
<Button variant="secondary" size="sm" onClick={() => onDeleteUnit(unit.id)}>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
|
||||
@@ -122,6 +122,7 @@ export function PositionTree({ employees, units, vacancies }: PositionTreeProps)
|
||||
unit_type: u.unit_type,
|
||||
depth: berechne(u, new Set()),
|
||||
hasChief: mitLeitung.has(u.id),
|
||||
endet: !!u.valid_to,
|
||||
}));
|
||||
}, [units, employees, vacancies]);
|
||||
|
||||
|
||||
@@ -10,7 +10,15 @@ import { useToast } from "@/components/ui/Toast";
|
||||
import { todayIso } from "@/lib/format";
|
||||
import { EinheitAuswahl, KostenstellenAuswahl } from "./StammAuswahl";
|
||||
|
||||
export type UnitOption = { id: string; name: string; unit_type: string; depth: number; hasChief: boolean };
|
||||
export type UnitOption = {
|
||||
id: string;
|
||||
name: string;
|
||||
unit_type: string;
|
||||
depth: number;
|
||||
hasChief: boolean;
|
||||
/** Die Einheit hat ein Ende und nimmt keine neue Planstelle mehr auf. */
|
||||
endet?: boolean;
|
||||
};
|
||||
|
||||
// Eine Planstelle gehört zu genau einer Organisationseinheit — mehr braucht
|
||||
// es nicht. Die vorgesetzte Person ergibt sich aus der Einheit und wird
|
||||
@@ -39,7 +47,10 @@ export function CreatePositionModal({
|
||||
const [kostenstelleId, setKostenstelleId] = useState("");
|
||||
const [pending, setPending] = useState(false);
|
||||
|
||||
const unit = units.find((u) => u.id === orgUnitId);
|
||||
// Eine neue Planstelle ist unbefristet; in einer Einheit mit Ende wiese die
|
||||
// Datenbank sie ab.
|
||||
const auswahl = units.filter((u) => !u.endet);
|
||||
const unit = auswahl.find((u) => u.id === orgUnitId);
|
||||
// Der Unique-Index lässt nur eine gültige Leitung je Einheit zu. Das hier
|
||||
// vorwegzunehmen erspart eine Fehlermeldung, die in der Oberfläche nichts
|
||||
// erklärt.
|
||||
@@ -95,7 +106,7 @@ export function CreatePositionModal({
|
||||
>
|
||||
<div className="flex flex-col gap-4">
|
||||
<EinheitAuswahl
|
||||
units={units}
|
||||
units={auswahl}
|
||||
value={orgUnitId}
|
||||
onChange={(id) => {
|
||||
setOrgUnitId(id);
|
||||
|
||||
@@ -60,7 +60,10 @@ export function EditPositionModal({
|
||||
const kostenstelleGeaendert =
|
||||
kostenstelleId !== "" && kostenstellen.find((k) => k.id === kostenstelleId)?.code !== position.kostenstelle?.code;
|
||||
|
||||
const unit = units.find((u) => u.id === orgUnitId);
|
||||
// Die eigene Einheit bleibt wählbar, auch wenn sie ein Ende hat; wohin die
|
||||
// Stelle wechseln kann, ist nur eine Einheit ohne Ende.
|
||||
const auswahl = units.filter((u) => !u.endet || u.id === position.org_unit_id);
|
||||
const unit = auswahl.find((u) => u.id === orgUnitId);
|
||||
// Die eigene Leitung zählt nicht als Hindernis für sich selbst.
|
||||
const chiefTaken = Boolean(unit?.hasChief) && !(position.is_chief && orgUnitId === position.org_unit_id);
|
||||
|
||||
@@ -153,7 +156,7 @@ export function EditPositionModal({
|
||||
<div className="flex flex-col gap-3">
|
||||
<TextField label="Tätigkeit" required value={jobTitle} onChange={setJobTitle} />
|
||||
|
||||
<EinheitAuswahl units={units} value={orgUnitId} onChange={setOrgUnitId} />
|
||||
<EinheitAuswahl units={auswahl} value={orgUnitId} onChange={setOrgUnitId} />
|
||||
|
||||
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
|
||||
<DateField label="Gültig ab" required value={validFrom} onChange={setValidFrom} />
|
||||
|
||||
@@ -0,0 +1,381 @@
|
||||
-- Struktur anlegen und abschliessen
|
||||
--
|
||||
-- Standort und Tätigkeit entstehen über eigene Funktionen mit Protokoll,
|
||||
-- nicht über Einfügen in die Tabelle. Eine Planstelle kann mit einer
|
||||
-- vorgegebenen Nummer angelegt werden; ohne Nummer vergibt das System sie
|
||||
-- selbst. Eine Organisationseinheit wird zu einem Datum
|
||||
-- abgeschlossen statt gelöscht, damit sie für frühere Stichtage erhalten
|
||||
-- bleibt.
|
||||
--
|
||||
-- Abgeschlossen wird von unten nach oben: eine Einheit erst, wenn keine
|
||||
-- Einheit und keine Planstelle unter ihr über das Ende hinaus gilt; eine
|
||||
-- Planstelle erst, wenn keine Besetzung über ihr Ende hinaus reicht und
|
||||
-- keine Versetzung oder Beförderung auf sie vorgemerkt ist. Untergeordnetes
|
||||
-- wird nicht mit abgeschlossen: wer eine Einheit schliesst, soll sehen, was
|
||||
-- darunter noch lebt, statt dass es lautlos mitverschwindet.
|
||||
--
|
||||
-- Zwei Regeln stehen zusätzlich in Triggern, weil sie Eigenschaften der
|
||||
-- Daten sind und nicht einer Eingabemaske: eine Besetzung endet spätestens
|
||||
-- mit ihrer Planstelle, eine Planstelle spätestens mit ihrer Einheit. Sonst
|
||||
-- sässe jemand auf einer Stelle, die es laut Organigramm nicht mehr gibt.
|
||||
|
||||
-- Bestehende Daten müssen beide Regeln schon erfüllen. Sonst scheiterte
|
||||
-- später jede Versetzung der betroffenen Person an einem Zustand, den sie
|
||||
-- nicht verursacht hat.
|
||||
do $vorpruefung$
|
||||
declare
|
||||
v_besetzungen integer;
|
||||
v_stellen integer;
|
||||
begin
|
||||
select count(*) into v_besetzungen
|
||||
from position_assignments a join om_positions p on p.id = a.position_id
|
||||
where p.valid_to is not null and (a.valid_to is null or a.valid_to > p.valid_to);
|
||||
select count(*) into v_stellen
|
||||
from om_positions p join org_units u on u.id = p.org_unit_id
|
||||
where u.valid_to is not null and (p.valid_to is null or p.valid_to > u.valid_to);
|
||||
if v_besetzungen > 0 or v_stellen > 0 then
|
||||
raise exception '% Besetzungen reichen über das Ende ihrer Planstelle, % Planstellen über das Ende ihrer Einheit. Erst bereinigen.',
|
||||
v_besetzungen, v_stellen;
|
||||
end if;
|
||||
end
|
||||
$vorpruefung$;
|
||||
|
||||
create or replace function public.create_location(payload jsonb)
|
||||
returns uuid
|
||||
language plpgsql
|
||||
set search_path = public, pg_temp
|
||||
as $$
|
||||
declare
|
||||
v_name text := nullif(btrim(payload->>'name'), '');
|
||||
v_land text := nullif(btrim(payload->>'country'), '');
|
||||
v_id uuid;
|
||||
begin
|
||||
perform require_hr_admin();
|
||||
|
||||
if v_name is null or v_land is null then
|
||||
raise exception 'Bezeichnung und Land des Standorts sind anzugeben.';
|
||||
end if;
|
||||
-- Ohne Rücksicht auf Gross- und Kleinschreibung: sonst stünden „Wien" und
|
||||
-- „WIEN" als zwei Standorte nebeneinander.
|
||||
if exists (select 1 from locations where lower(name) = lower(v_name)) then
|
||||
raise exception 'Den Standort % gibt es bereits.', v_name;
|
||||
end if;
|
||||
|
||||
begin
|
||||
insert into locations (name, country) values (v_name, v_land) returning id into v_id;
|
||||
exception when check_violation then
|
||||
raise exception 'Das Land % ist nicht vorgesehen.', v_land;
|
||||
end;
|
||||
|
||||
insert into audit_log (actor_user_id, actor_name, action, target_label, details)
|
||||
values (app_current_user_id(), current_actor_name(), 'Standort angelegt', v_name, 'Land: ' || v_land);
|
||||
|
||||
return v_id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
grant execute on function public.create_location(jsonb) to alpenwerk_app;
|
||||
|
||||
create or replace function public.create_job(payload jsonb)
|
||||
returns uuid
|
||||
language plpgsql
|
||||
set search_path = public, pg_temp
|
||||
as $$
|
||||
declare
|
||||
v_code text := nullif(btrim(payload->>'code'), '');
|
||||
v_titel text := nullif(btrim(payload->>'title'), '');
|
||||
v_id uuid;
|
||||
begin
|
||||
perform require_hr_admin();
|
||||
|
||||
if v_code is null or v_titel is null then
|
||||
raise exception 'Kennung und Bezeichnung der Tätigkeit sind anzugeben.';
|
||||
end if;
|
||||
if v_code ~ '\s' then
|
||||
raise exception 'Die Kennung der Tätigkeit darf keine Leerzeichen enthalten.';
|
||||
end if;
|
||||
if exists (select 1 from jobs where code = v_code) then
|
||||
raise exception 'Die Kennung % ist bereits vergeben.', v_code;
|
||||
end if;
|
||||
-- Dieselbe Regel wie in job_fuer_titel: gleiche Bezeichnung, ein Eintrag.
|
||||
if exists (select 1 from jobs where lower(title) = lower(v_titel)) then
|
||||
raise exception 'Die Tätigkeit „%" gibt es bereits.', v_titel;
|
||||
end if;
|
||||
|
||||
insert into jobs (code, title) values (v_code, v_titel) returning id into v_id;
|
||||
|
||||
insert into audit_log (actor_user_id, actor_name, action, target_label, details)
|
||||
values (app_current_user_id(), current_actor_name(), 'Tätigkeit angelegt', v_code || ' · ' || v_titel, null);
|
||||
|
||||
return v_id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
grant execute on function public.create_job(jsonb) to alpenwerk_app;
|
||||
|
||||
-- Ende der Gültigkeit einer Einheit setzen, verschieben oder aufheben
|
||||
-- (valid_to leer). Das Ende ist wie überall im Modell ausschliessend.
|
||||
create or replace function public.set_org_unit_valid_to(payload jsonb)
|
||||
returns void
|
||||
language plpgsql
|
||||
set search_path = public, pg_temp
|
||||
as $$
|
||||
declare
|
||||
v_id uuid := (payload->>'org_unit_id')::uuid;
|
||||
v_bis date := nullif(payload->>'valid_to', '')::date;
|
||||
v_unit org_units%rowtype;
|
||||
v_parent org_units%rowtype;
|
||||
v_anzahl integer;
|
||||
begin
|
||||
perform require_hr_admin();
|
||||
|
||||
-- Ein fehlender Schlüssel heisst sonst „wieder öffnen" — eine Folge, die
|
||||
-- niemand mit einem vergessenen Feld meint.
|
||||
if not (payload ? 'valid_to') then
|
||||
raise exception 'Es muss angegeben werden, wann die Einheit endet.';
|
||||
end if;
|
||||
|
||||
select * into v_unit from org_units where id = v_id for update;
|
||||
if not found then
|
||||
raise exception 'Die Organisationseinheit existiert nicht.';
|
||||
end if;
|
||||
if v_unit.parent_id is null then
|
||||
raise exception 'Die Gesellschaft ist die Wurzel und kann nicht abgeschlossen werden.';
|
||||
end if;
|
||||
if v_bis is not distinct from v_unit.valid_to then
|
||||
raise exception 'Es wurde nichts geändert.';
|
||||
end if;
|
||||
|
||||
select * into v_parent from org_units where id = v_unit.parent_id;
|
||||
if v_parent.valid_to is not null and (v_bis is null or v_bis > v_parent.valid_to) then
|
||||
raise exception 'Die übergeordnete Einheit endet am %. Diese Einheit kann nicht länger bestehen.',
|
||||
to_char(v_parent.valid_to, 'DD.MM.YYYY');
|
||||
end if;
|
||||
|
||||
if v_bis is not null then
|
||||
if v_bis <= v_unit.valid_from then
|
||||
raise exception 'Die Einheit beginnt am %. Das Ende muss danach liegen.', to_char(v_unit.valid_from, 'DD.MM.YYYY');
|
||||
end if;
|
||||
|
||||
select count(*) into v_anzahl
|
||||
from org_units where parent_id = v_id and (valid_to is null or valid_to > v_bis);
|
||||
if v_anzahl > 0 then
|
||||
raise exception 'Unter dieser Einheit bestehen über den % hinaus noch % Einheiten. Sie müssen zuerst abgeschlossen oder verschoben werden.',
|
||||
to_char(v_bis, 'DD.MM.YYYY'), v_anzahl;
|
||||
end if;
|
||||
|
||||
select count(*) into v_anzahl
|
||||
from om_positions where org_unit_id = v_id and (valid_to is null or valid_to > v_bis);
|
||||
if v_anzahl > 0 then
|
||||
raise exception 'In dieser Einheit bestehen über den % hinaus noch % Planstellen. Sie müssen zuerst beendet oder verschoben werden.',
|
||||
to_char(v_bis, 'DD.MM.YYYY'), v_anzahl;
|
||||
end if;
|
||||
end if;
|
||||
|
||||
update org_units set valid_to = v_bis where id = v_id;
|
||||
|
||||
insert into audit_log (actor_user_id, actor_name, action, target_label, details)
|
||||
values (app_current_user_id(), current_actor_name(),
|
||||
case when v_bis is null then 'Organisationseinheit wieder geöffnet' else 'Organisationseinheit abgeschlossen' end,
|
||||
v_unit.org_number || ' · ' || v_unit.name,
|
||||
case when v_bis is null then 'Ohne Ende'
|
||||
else 'Endet am ' || to_char(v_bis, 'DD.MM.YYYY') end ||
|
||||
case when v_unit.valid_to is not null then ', vorher Ende am ' || to_char(v_unit.valid_to, 'DD.MM.YYYY') else '' end);
|
||||
end;
|
||||
$$;
|
||||
|
||||
grant execute on function public.set_org_unit_valid_to(jsonb) to alpenwerk_app;
|
||||
|
||||
create or replace function public.fn_besetzung_in_planstelle()
|
||||
returns trigger
|
||||
language plpgsql
|
||||
set search_path = public, pg_temp
|
||||
as $$
|
||||
declare
|
||||
v_ende date;
|
||||
v_nummer text;
|
||||
begin
|
||||
if tg_op = 'UPDATE'
|
||||
and new.position_id is not distinct from old.position_id
|
||||
and new.valid_to is not distinct from old.valid_to then
|
||||
return new;
|
||||
end if;
|
||||
|
||||
select valid_to, position_number into v_ende, v_nummer from om_positions where id = new.position_id;
|
||||
if v_ende is not null and (new.valid_to is null or new.valid_to > v_ende) then
|
||||
raise exception 'Die Planstelle % endet am %. Eine Besetzung darüber hinaus ist nicht möglich.',
|
||||
v_nummer, to_char(v_ende, 'DD.MM.YYYY');
|
||||
end if;
|
||||
return new;
|
||||
end;
|
||||
$$;
|
||||
|
||||
drop trigger if exists trg_besetzung_in_planstelle on position_assignments;
|
||||
create trigger trg_besetzung_in_planstelle
|
||||
before insert or update on position_assignments
|
||||
for each row execute function fn_besetzung_in_planstelle();
|
||||
|
||||
-- Geändert wird an den laufenden Definitionen, an je einem Anker, der genau
|
||||
-- einmal vorkommen muss.
|
||||
do $migration$
|
||||
declare
|
||||
v_ziele text[][] := array[
|
||||
array['create_position',
|
||||
$a$ v_unit_name text;$a$,
|
||||
$a$ v_unit_name text;
|
||||
v_nummer text := nullif(btrim(payload->>'position_number'), '');$a$],
|
||||
array['create_position',
|
||||
$a$ raise exception 'Es muss eine Tätigkeit angegeben werden.';
|
||||
end if;$a$,
|
||||
$a$ raise exception 'Es muss eine Tätigkeit angegeben werden.';
|
||||
end if;
|
||||
|
||||
-- Eine vorgegebene Nummer kommt aus einem Fremdsystem und muss dort
|
||||
-- wiederzufinden sein; eine zweite Planstelle unter derselben Nummer
|
||||
-- machte jede Zuordnung über sie mehrdeutig.
|
||||
if v_nummer is not null then
|
||||
if v_nummer ~ '\s' then
|
||||
raise exception 'Die Planstellennummer darf keine Leerzeichen enthalten.';
|
||||
end if;
|
||||
if exists (select 1 from om_positions where position_number = v_nummer) then
|
||||
raise exception 'Die Planstellennummer % ist bereits vergeben.', v_nummer;
|
||||
end if;
|
||||
end if;$a$],
|
||||
array['create_position',
|
||||
$a$ values (next_position_number(), v_org_unit_id, v_job_id, v_is_chief, v_valid_from)$a$,
|
||||
$a$ values (coalesce(v_nummer, next_position_number()), v_org_unit_id, v_job_id, v_is_chief, v_valid_from)$a$],
|
||||
array['update_position',
|
||||
$a$ if v_valid_to is not null and v_valid_to < v_valid_from then
|
||||
raise exception 'Das Ende der Gültigkeit liegt vor ihrem Beginn.';$a$,
|
||||
$a$ if v_valid_to is not null and v_valid_to <= v_valid_from then
|
||||
raise exception 'Das Ende der Gültigkeit muss nach ihrem Beginn liegen.';$a$],
|
||||
array['update_position',
|
||||
$a$ if v_besetzt and v_valid_to is not null then
|
||||
raise exception 'Diese Planstelle ist vergeben und kann kein Ende der Gültigkeit bekommen.';
|
||||
end if;$a$,
|
||||
$a$ -- Massgeblich ist das Ende, nicht der heutige Tag: eine Planstelle kann
|
||||
-- zum Austritt ihrer Inhaberin enden, und eine heute freie Stelle darf
|
||||
-- nicht enden, wenn ab nächstem Monat jemand auf ihr sitzt.
|
||||
if v_valid_to is not null and v_valid_to is distinct from v_alt.valid_to then
|
||||
if exists (select 1 from position_assignments
|
||||
where position_id = v_id and (valid_to is null or valid_to > v_valid_to)) then
|
||||
raise exception 'Auf dieser Planstelle besteht über den % hinaus eine Besetzung.', to_char(v_valid_to, 'DD.MM.YYYY');
|
||||
end if;
|
||||
if planstelle_vorgemerkt(v_id, '-infinity'::date, null) is not null then
|
||||
raise exception 'Für diese Planstelle ist zum % eine Versetzung oder Beförderung vorgemerkt. Sie kann erst enden, wenn dieser Vorgang abgebrochen ist.',
|
||||
to_char(planstelle_vorgemerkt(v_id, '-infinity'::date, null), 'DD.MM.YYYY');
|
||||
end if;
|
||||
end if;$a$],
|
||||
array['update_position',
|
||||
$a$ select id into v_job_id from jobs where lower(title) = lower(v_job_title);
|
||||
if v_job_id is null then
|
||||
insert into jobs (code, title)
|
||||
values ('J' || lpad((select count(*) + 1 from jobs)::text, 4, '0'), v_job_title)
|
||||
returning id into v_job_id;
|
||||
end if;$a$,
|
||||
$a$ v_job_id := job_fuer_titel(v_job_title);$a$],
|
||||
array['create_org_unit',
|
||||
$a$ if v_parent.valid_to is not null and v_valid_from >= v_parent.valid_to then
|
||||
raise exception 'Die übergeordnete Einheit gilt nur bis %.', v_parent.valid_to;
|
||||
end if;$a$,
|
||||
$a$ -- Eine neue Einheit hat kein Ende. Unter einer endenden stünde sie über
|
||||
-- deren Ende hinaus ohne Vorfahren da.
|
||||
if v_parent.valid_to is not null then
|
||||
raise exception 'Die übergeordnete Einheit endet am %. Darunter lässt sich keine neue Einheit anlegen.',
|
||||
to_char(v_parent.valid_to, 'DD.MM.YYYY');
|
||||
end if;$a$],
|
||||
array['fn_planstelle_in_einheit',
|
||||
$a$ return new;
|
||||
end;$a$,
|
||||
$a$ if v_einheit.valid_to is not null and (new.valid_to is null or new.valid_to > v_einheit.valid_to) then
|
||||
raise exception 'Die Organisationseinheit „%" besteht nur bis %. Die Planstelle muss spätestens dann enden.',
|
||||
v_einheit.name, to_char(v_einheit.valid_to - 1, 'DD.MM.YYYY');
|
||||
end if;
|
||||
return new;
|
||||
end;$a$]
|
||||
];
|
||||
v_def text;
|
||||
v_treffer integer;
|
||||
i integer;
|
||||
begin
|
||||
for i in 1 .. array_length(v_ziele, 1) 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_ziele[i][1];
|
||||
if v_def is null then
|
||||
raise exception '% ist nicht vorhanden.', v_ziele[i][1];
|
||||
end if;
|
||||
if position(v_ziele[i][3] in v_def) > 0 then
|
||||
continue;
|
||||
end if;
|
||||
v_treffer := (length(v_def) - length(replace(v_def, v_ziele[i][2], ''))) / length(v_ziele[i][2]);
|
||||
if v_treffer <> 1 then
|
||||
raise exception '% (Anker %): der Anker kommt % mal vor, erwartet genau einmal.', v_ziele[i][1], i, v_treffer;
|
||||
end if;
|
||||
execute replace(v_def, v_ziele[i][2], v_ziele[i][3]);
|
||||
end loop;
|
||||
end
|
||||
$migration$;
|
||||
|
||||
do $pruefung$
|
||||
declare
|
||||
v_fn text;
|
||||
v_def text;
|
||||
begin
|
||||
foreach v_fn in array array['create_location', 'create_job', 'set_org_unit_valid_to', 'create_position', 'update_position'] 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(E'begin\n perform require_hr_admin();' in v_def) = 0 then
|
||||
raise exception '% prüft die Rechte nicht in der ersten Zeile.', 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 exists (select 1 from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.proname = v_fn and p.prosecdef) then
|
||||
raise exception '% darf nicht SECURITY DEFINER sein.', 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 = 'create_position';
|
||||
if position('coalesce(v_nummer, next_position_number())' in v_def) = 0
|
||||
or position('job_fuer_titel(v_job_title)' in v_def) = 0
|
||||
or position('bereits eine Leitungsplanstelle' in v_def) = 0 then
|
||||
raise exception 'create_position übernimmt die Nummer nicht oder hat Jobkatalog oder Leitungsprüfung verloren.';
|
||||
end if;
|
||||
|
||||
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 = 'update_position';
|
||||
if position('count(*) + 1 from jobs' in v_def) > 0
|
||||
or position('job_fuer_titel(v_job_title)' in v_def) = 0
|
||||
or position('planstelle_vorgemerkt(v_id' in v_def) = 0
|
||||
or position('Für einen Wechsel der Einheit die Versetzung benutzen' in v_def) = 0 then
|
||||
raise exception 'update_position prüft Ende oder Vormerkung nicht oder hat die Versetzungsregel verloren.';
|
||||
end if;
|
||||
|
||||
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 = 'fn_planstelle_in_einheit';
|
||||
if position('Die Planstelle muss spätestens dann enden' in v_def) = 0
|
||||
or position('Die Planstelle kann nicht früher beginnen' in v_def) = 0 then
|
||||
raise exception 'fn_planstelle_in_einheit prüft Beginn oder Ende der Einheit nicht.';
|
||||
end if;
|
||||
|
||||
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 = 'create_org_unit';
|
||||
if position('Darunter lässt sich keine neue Einheit anlegen' in v_def) = 0
|
||||
or position('Ein früherer Beginn ist nicht möglich' in v_def) = 0
|
||||
or position('perform create_position(' in v_def) = 0 then
|
||||
raise exception 'create_org_unit prüft Beginn oder Ende der übergeordneten Einheit nicht oder legt keine Leitungsplanstelle an.';
|
||||
end if;
|
||||
|
||||
if not exists (select 1 from pg_trigger where tgname = 'trg_besetzung_in_planstelle' and not tgisinternal) then
|
||||
raise exception 'Der Trigger trg_besetzung_in_planstelle fehlt.';
|
||||
end if;
|
||||
end
|
||||
$pruefung$;
|
||||
@@ -56,6 +56,7 @@ declare
|
||||
v_meldung text;
|
||||
v_ging boolean;
|
||||
v_titel text;
|
||||
v_zu uuid; v_px uuid; v_py uuid; v_job uuid;
|
||||
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;
|
||||
@@ -77,14 +78,14 @@ begin
|
||||
'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/32 Einheit und vier Planstellen angelegt';
|
||||
raise notice '1/33 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/32 Einstellung auf Planstelle 1';
|
||||
raise notice '2/33 Einstellung auf Planstelle 1';
|
||||
|
||||
-- ── Versetzung am selben Tag (20260929180000) ───────────────────
|
||||
-- Die Besetzung begann heute; sie auf heute zu schliessen wäre ein
|
||||
@@ -97,7 +98,7 @@ begin
|
||||
if v_stelle is distinct from v_p2 then
|
||||
raise exception 'Nach der Versetzung sitzt die Person nicht auf Planstelle 2.';
|
||||
end if;
|
||||
raise notice '3/32 Versetzung am selben Tag';
|
||||
raise notice '3/33 Versetzung am selben Tag';
|
||||
|
||||
-- ── Beförderung mit Planstellenwechsel, ebenfalls heute ─────────
|
||||
perform promote_employee(jsonb_build_object(
|
||||
@@ -122,7 +123,7 @@ begin
|
||||
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/32 Beförderung am selben Tag: Hay-Grade und Planstellenbezeichnung';
|
||||
raise notice '4/33 Beförderung am selben Tag: Hay-Grade und Planstellenbezeichnung';
|
||||
|
||||
-- Dieselbe Planstelle, nur ein neuer Name.
|
||||
perform promote_employee(jsonb_build_object(
|
||||
@@ -140,7 +141,7 @@ begin
|
||||
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/32 Beförderung ohne Stellenwechsel benennt dieselbe Planstelle um';
|
||||
raise notice '4b/33 Beförderung ohne Stellenwechsel benennt dieselbe Planstelle um';
|
||||
|
||||
-- ── Vorgemerkte Planstelle (20260929200000) ─────────────────────
|
||||
-- Eine Versetzung in die Zukunft belegt die Zielstelle, obwohl dort noch
|
||||
@@ -171,13 +172,13 @@ begin
|
||||
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/32 Vorgemerkte Planstelle wird abgewiesen';
|
||||
raise notice '5/33 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/32 Die eigene Vormerkung steht nicht im Weg';
|
||||
raise notice '6/33 Die eigene Vormerkung steht nicht im Weg';
|
||||
|
||||
-- ── Austritt am selben Tag (20260929220000) ─────────────────────
|
||||
perform terminate_employee(jsonb_build_object(
|
||||
@@ -189,7 +190,7 @@ begin
|
||||
if v_offen <> 0 then
|
||||
raise exception 'Nach dem Austritt ist die Planstelle nicht frei.';
|
||||
end if;
|
||||
raise notice '7/32 Austritt am selben Tag, Planstelle frei';
|
||||
raise notice '7/33 Austritt am selben Tag, Planstelle frei';
|
||||
|
||||
-- ── Wiedereintritt (20260929160000) ─────────────────────────────
|
||||
perform rehire_employee(jsonb_build_object(
|
||||
@@ -206,7 +207,7 @@ begin
|
||||
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/32 Wiedereintritt';
|
||||
raise notice '8/33 Wiedereintritt';
|
||||
|
||||
-- ── Rückdatierte Versetzung (20261013140000) ────────────────────
|
||||
--
|
||||
@@ -241,7 +242,7 @@ begin
|
||||
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/32 Rückdatierte Versetzung lässt keine zweite Besetzung stehen';
|
||||
raise notice '9/33 Rückdatierte Versetzung lässt keine zweite Besetzung stehen';
|
||||
|
||||
-- ── Versetzung bei datiertem Austritt (20261013140000) ──────────
|
||||
--
|
||||
@@ -275,7 +276,7 @@ begin
|
||||
if v_offen <> 1 then
|
||||
raise exception 'Die neue Besetzung endet nicht am Austrittstag.';
|
||||
end if;
|
||||
raise notice '10/32 Versetzung bei datiertem Austritt: kein Überlappen, kein Überleben';
|
||||
raise notice '10/33 Versetzung bei datiertem Austritt: kein Überlappen, kein Überleben';
|
||||
|
||||
-- ── Änderung vor dem Eintritt (20261018100000) ──────────────────
|
||||
--
|
||||
@@ -314,7 +315,7 @@ begin
|
||||
and event_date = current_date + 40) then
|
||||
raise exception 'Mit dem Eintritt ist die Änderung davor nicht mitgewandert.';
|
||||
end if;
|
||||
raise notice '11/32 Änderung vor dem Eintritt wirkt sofort und wandert mit dem Eintritt';
|
||||
raise notice '11/33 Änderung vor dem Eintritt wirkt sofort und wandert mit dem Eintritt';
|
||||
|
||||
-- ── Abwesenheit mit geplanter Rückkehr löschen (20261019100000) ─
|
||||
--
|
||||
@@ -352,7 +353,7 @@ begin
|
||||
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/32 Abwesenheit mit geplanter Rückkehr lässt sich löschen';
|
||||
raise notice '12/33 Abwesenheit mit geplanter Rückkehr lässt sich löschen';
|
||||
|
||||
-- ── Zweite Versetzung zum selben Tag (20261019110000) ───────────
|
||||
--
|
||||
@@ -392,7 +393,7 @@ begin
|
||||
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/32 Zweite Versetzung zum selben Tag abgewiesen, die erste lässt sich abbrechen';
|
||||
raise notice '13/33 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.
|
||||
@@ -405,7 +406,7 @@ begin
|
||||
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/32 Eigene Notiz lässt sich löschen, das Protokoll behält sie';
|
||||
raise notice '14/33 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
|
||||
@@ -420,7 +421,7 @@ begin
|
||||
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/32 Austritt zurückgenommen, Besetzung läuft wieder';
|
||||
raise notice '15/33 Austritt zurückgenommen, Besetzung läuft wieder';
|
||||
|
||||
-- ── Letzte Versetzung zurücknehmen (20261019140000) ─────────────
|
||||
v_p8 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
@@ -443,7 +444,7 @@ begin
|
||||
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/32 Letzte Versetzung zurückgenommen, vorherige Planstelle wieder besetzt';
|
||||
raise notice '16/33 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.
|
||||
@@ -457,7 +458,7 @@ begin
|
||||
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/32 Planstelle beim Eintritt berichtigt, ohne Versetzung';
|
||||
raise notice '17/33 Planstelle beim Eintritt berichtigt, ohne Versetzung';
|
||||
|
||||
-- ── Planstelle nicht vor ihrer Einheit (20261019150000) ─────────
|
||||
v_spaet := create_org_unit(jsonb_build_object(
|
||||
@@ -477,7 +478,7 @@ begin
|
||||
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/32 Planstelle vor ihrer Einheit wird abgewiesen';
|
||||
raise notice '18/33 Planstelle vor ihrer Einheit wird abgewiesen';
|
||||
|
||||
-- ── Anschliessende Abwesenheit (20261019220000) ─────────────────
|
||||
-- v_e (Schritt 16) geht in Wochenhilfe; eine Elternkarenz wird
|
||||
@@ -517,7 +518,7 @@ begin
|
||||
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/32 Anschliessende Abwesenheit frühestens am Ende der laufenden';
|
||||
raise notice '19/33 Anschliessende Abwesenheit frühestens am Ende der laufenden';
|
||||
|
||||
-- ── Rückkehr anpassen ───────────────────────────────────────────
|
||||
-- Über den Beginn der anschliessenden hinaus geht nicht; früher schon. Die
|
||||
@@ -558,7 +559,7 @@ begin
|
||||
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/32 Rückkehr angepasst, die der anschliessenden Abwesenheit bleibt';
|
||||
raise notice '20/33 Rückkehr angepasst, die der anschliessenden Abwesenheit bleibt';
|
||||
|
||||
-- ── Sofortige Rückkehr ──────────────────────────────────────────
|
||||
perform rueckkehr_anpassen(jsonb_build_object(
|
||||
@@ -574,7 +575,7 @@ begin
|
||||
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/32 Rückkehr heute wird sofort verbucht, die geplante Abwesenheit bleibt';
|
||||
raise notice '21/33 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"
|
||||
@@ -612,7 +613,7 @@ begin
|
||||
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/32 Rückkehr legt Onboarding-Liste an; abschliessen sperrt, eine neue beginnt leer';
|
||||
raise notice '22/33 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
|
||||
@@ -634,7 +635,7 @@ begin
|
||||
if v_ging or v_meldung not like '%gibt es bereits%' then
|
||||
raise exception 'Eine doppelte Kostenstellennummer wurde angenommen (%).', v_meldung;
|
||||
end if;
|
||||
raise notice '23/32 Kostenstelle angelegt und zugeordnet, doppelte Nummer abgewiesen';
|
||||
raise notice '23/33 Kostenstelle angelegt und zugeordnet, doppelte Nummer abgewiesen';
|
||||
|
||||
-- ── Geplante Abwesenheit berichtigen (20261020120000) ───────────
|
||||
-- Über „Eintrag berichtigen": der Beginn wandert, die geplante Rückkehr
|
||||
@@ -675,7 +676,7 @@ begin
|
||||
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/32 Geplante Abwesenheit berichtigt, freies Verhältnis bei Angehörigen';
|
||||
raise notice '24/33 Geplante Abwesenheit berichtigt, freies Verhältnis bei Angehörigen';
|
||||
|
||||
-- ── Wochenhilfe ohne Ende (20261020130000) ──────────────────────
|
||||
-- Ohne Ende keine vorgemerkte Rückkehr; eine anschliessende Abwesenheit
|
||||
@@ -702,7 +703,7 @@ begin
|
||||
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/32 Wochenhilfe ohne Ende, anschliessende Abwesenheit erst mit Ende';
|
||||
raise notice '25/33 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
|
||||
@@ -730,7 +731,7 @@ begin
|
||||
and h.event_date = current_date + 70) then
|
||||
raise exception 'Die vorgemerkte Rückkehr ist der Berichtigung nicht gefolgt.';
|
||||
end if;
|
||||
raise notice '26/32 Berichtigte Rückkehr: Vormerkung und Historie folgen';
|
||||
raise notice '26/33 Berichtigte Rückkehr: Vormerkung und Historie folgen';
|
||||
|
||||
-- ── Lehrzeit (20261021100000) ───────────────────────────────────
|
||||
-- Ein Lehrling bekommt Beginn und Ende; ein Ende vor dem Beginn wird
|
||||
@@ -765,7 +766,7 @@ begin
|
||||
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/32 Lehrzeit beim Lehrling, sonst leer';
|
||||
raise notice '27/33 Lehrzeit beim Lehrling, sonst leer';
|
||||
|
||||
-- ── Einstellung ohne Geburtsdatum (20261021120000) ──────────────
|
||||
v_p6 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
@@ -777,7 +778,7 @@ begin
|
||||
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/32 Einstellung ohne Geburtsdatum';
|
||||
raise notice '28/33 Einstellung ohne Geburtsdatum';
|
||||
|
||||
-- ── Organisationseinheit umbenennen (20261022100000) ────────────
|
||||
perform rename_org_unit(jsonb_build_object('org_unit_id', v_einheit, 'name', 'Rauchtest umbenannt'));
|
||||
@@ -788,7 +789,7 @@ begin
|
||||
and target_label like '%Rauchtest umbenannt') then
|
||||
raise exception 'Die Umbenennung steht nicht im Protokoll.';
|
||||
end if;
|
||||
raise notice '29/32 Organisationseinheit umbenannt, im Protokoll';
|
||||
raise notice '29/33 Organisationseinheit umbenannt, im Protokoll';
|
||||
|
||||
-- ── Standort ändern (20261022110000) ────────────────────────────
|
||||
-- Gebraucht wird ein zweiter Standort; der Rauchtest legt ihn an und
|
||||
@@ -806,7 +807,7 @@ begin
|
||||
and changes @> '[{"feld": "Standort"}]') then
|
||||
raise exception 'Der Standortwechsel steht nicht in der Historie.';
|
||||
end if;
|
||||
raise notice '30/32 Standort geändert, in der Historie';
|
||||
raise notice '30/33 Standort geändert, in der Historie';
|
||||
|
||||
-- ── Geplante Abwesenheit ohne Ende, Ende nachgetragen und geleert
|
||||
-- (20261029100000) ────────────────────────────────────────────
|
||||
@@ -831,7 +832,7 @@ begin
|
||||
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/32 Abwesenheit ohne Ende, Ende nachgetragen und wieder geleert';
|
||||
raise notice '31/33 Abwesenheit ohne Ende, Ende nachgetragen und wieder geleert';
|
||||
|
||||
-- ── Übernahme Leiharbeitskraft (20261029120000) ─────────────────
|
||||
v_p9 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
@@ -854,7 +855,117 @@ begin
|
||||
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/32 Übernahme Leiharbeitskraft bei der Einstellung, ohne Kennzeichen geleert';
|
||||
raise notice '32/33 Ü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_px := 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));
|
||||
if (select position_number <> 'RAUCHTEST-PS1' 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', '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 '%bereits vergeben%' then
|
||||
raise exception 'Eine Planstellennummer liess sich doppelt vergeben (%).', v_meldung;
|
||||
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/33 Standort, Tätigkeit, Planstelle mit Nummer; Planstelle und Einheit enden in der richtigen Reihenfolge';
|
||||
|
||||
raise notice '';
|
||||
raise notice 'Rauchtest bestanden. Alles Angelegte wird gleich verworfen.';
|
||||
@@ -868,4 +979,5 @@ rollback;
|
||||
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 jobs where title like 'Rauchtest %') as taetigkeiten,
|
||||
(select count(*) from locations where name like 'Rauchtest %') as standorte;
|
||||
|
||||
23
lib/org.ts
23
lib/org.ts
@@ -22,8 +22,24 @@ export type OrgUnit = {
|
||||
name: string;
|
||||
parent_id: string | null;
|
||||
unit_type: OrgUnitType;
|
||||
/** Ausschliessendes Ende; leer heisst unbefristet. */
|
||||
valid_to?: string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Ob unter einer Einheit noch etwas Unbefristetes entstehen darf. Eine
|
||||
* Einheit mit Ende — auch einem künftigen — nimmt keine neue Planstelle und
|
||||
* keine neue Untereinheit mehr auf; die Datenbank wiese beides ab.
|
||||
*/
|
||||
export function nimmtNeuesAuf(unit: OrgUnit): boolean {
|
||||
return !unit.valid_to;
|
||||
}
|
||||
|
||||
/** Ob die Einheit am Stichtag noch besteht. */
|
||||
export function bestehtAm(unit: OrgUnit, asOf: string): boolean {
|
||||
return !unit.valid_to || unit.valid_to > asOf;
|
||||
}
|
||||
|
||||
export type Location = Database["public"]["Tables"]["locations"]["Row"];
|
||||
|
||||
export type OrgMaps = {
|
||||
@@ -50,7 +66,12 @@ export type OrgMaps = {
|
||||
export function orgMapsAbfragen(eb: OrgEb) {
|
||||
return [
|
||||
jsonArrayFrom(
|
||||
eb.selectFrom("org_units").select(["id", "org_number", "name", "parent_id", "unit_type"]).orderBy("org_number")
|
||||
// Alle Einheiten, auch beendete: frühere Besetzungen und Berichte zu
|
||||
// einem früheren Stichtag nennen sie weiter beim Namen.
|
||||
eb
|
||||
.selectFrom("org_units")
|
||||
.select(["id", "org_number", "name", "parent_id", "unit_type", "valid_to"])
|
||||
.orderBy("org_number")
|
||||
).as("units"),
|
||||
jsonArrayFrom(eb.selectFrom("locations").selectAll().orderBy("name")).as("locations"),
|
||||
] as const;
|
||||
|
||||
@@ -928,6 +928,9 @@ export type Database = {
|
||||
update_position: { Args: { payload: Record<string, unknown> }; Returns: void };
|
||||
set_position_cost_center: { Args: { payload: Record<string, unknown> }; Returns: void };
|
||||
create_cost_center: { Args: { payload: Record<string, unknown> }; Returns: string };
|
||||
create_location: { Args: { payload: Record<string, unknown> }; Returns: string };
|
||||
create_job: { Args: { payload: Record<string, unknown> }; Returns: string };
|
||||
set_org_unit_valid_to: { Args: { payload: Record<string, unknown> }; Returns: void };
|
||||
set_onboarding_task: { Args: { payload: Record<string, unknown> }; Returns: void };
|
||||
start_onboarding: { Args: { payload: Record<string, unknown> }; Returns: number };
|
||||
set_offboarding_task: { Args: { payload: Record<string, unknown> }; Returns: void };
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ancestorsOf, breadcrumbLabel, buildOrgMaps, divisionOf, subtreeOf, type OrgUnit } from "@/lib/org";
|
||||
import { ancestorsOf, bestehtAm, breadcrumbLabel, buildOrgMaps, divisionOf, nimmtNeuesAuf, subtreeOf, type OrgUnit } from "@/lib/org";
|
||||
|
||||
// Die Organisation ist ein Baum beliebiger Tiefe, und an dieser Datei hängt
|
||||
// entsprechend viel: eine Einheit falsch verkettet, und ein Filter
|
||||
@@ -128,3 +128,20 @@ describe("breadcrumbLabel", () => {
|
||||
expect(breadcrumbLabel(maps, "gmbh")).toBe("–");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Ende einer Einheit", () => {
|
||||
const endet: OrgUnit = { ...units[4], valid_to: "2026-12-01" };
|
||||
|
||||
it("besteht bis zum Tag vor ihrem Ende", () => {
|
||||
// valid_to ist ausschliessend: am Endtag selbst gibt es die Einheit nicht mehr.
|
||||
expect(bestehtAm(endet, "2026-11-30")).toBe(true);
|
||||
expect(bestehtAm(endet, "2026-12-01")).toBe(false);
|
||||
expect(bestehtAm(units[4], "2099-01-01")).toBe(true);
|
||||
});
|
||||
|
||||
it("nimmt mit einem Ende nichts Neues mehr auf, auch wenn es noch in der Zukunft liegt", () => {
|
||||
expect(nimmtNeuesAuf(endet)).toBe(false);
|
||||
expect(nimmtNeuesAuf(units[4])).toBe(true);
|
||||
expect(nimmtNeuesAuf({ ...units[4], valid_to: null })).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user