diff --git a/actions/positions.ts b/actions/positions.ts index 2360422..21dcefd 100644 --- a/actions/positions.ts +++ b/actions/positions.ts @@ -19,7 +19,8 @@ async function callRpc( | "update_position" | "delete_position" | "set_position_cost_center" - | "create_cost_center", + | "create_cost_center" + | "rename_cost_center", payload: Record, revalidate: string[] ): Promise { @@ -34,6 +35,11 @@ export async function createCostCenter(payload: { code: string; name: string; va return callRpc("create_cost_center", payload, POSITION_PATHS); } +/** Die Bezeichnung einer Kostenstelle ändern; die Nummer bleibt. */ +export async function renameCostCenter(payload: { cost_center_id: string; name: string }): Promise { + return callRpc("rename_cost_center", payload, POSITION_PATHS); +} + /** * Die Planstellen, die zu einem Eintrittstag frei sind — für den * Einstellungsassistenten. Fachlich vorgegeben: auch eine Stelle, die heute diff --git a/components/positions/PositionsPageClient.tsx b/components/positions/PositionsPageClient.tsx index 8566242..82a0067 100644 --- a/components/positions/PositionsPageClient.tsx +++ b/components/positions/PositionsPageClient.tsx @@ -1,6 +1,6 @@ "use client"; -import { Copy, Plus, Trash2 } from "lucide-react"; +import { Copy, Pencil, Plus, Trash2 } from "lucide-react"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { clonePosition, deletePosition } from "@/actions/positions"; @@ -12,6 +12,7 @@ import type { OpenPositionResolved } from "@/lib/positions"; import { CreateKostenstelleModal } from "./CreateKostenstelleModal"; import { CreatePositionModal, type UnitOption } from "./CreatePositionModal"; import { EditPositionModal } from "./EditPositionModal"; +import { RenameKostenstelleModal } from "./RenameKostenstelleModal"; type OpenPositionWithDays = OpenPositionResolved & { daysOpen: number }; @@ -26,6 +27,7 @@ export function PositionsPageClient({ openPositions, units, kostenstellen }: Pos const router = useRouter(); const [createOpen, setCreateOpen] = useState(false); const [kostenstelleOpen, setKostenstelleOpen] = useState(false); + const [umbenennenOpen, setUmbenennenOpen] = useState(false); const [editing, setEditing] = useState(null); const [deletingId, setDeletingId] = useState(null); const [cloningId, setCloningId] = useState(null); @@ -137,6 +139,10 @@ export function PositionsPageClient({ openPositions, units, kostenstellen }: Pos Kostenstelle anlegen + + + + } + > +
+ + +
+ + ); +} diff --git a/db/migrations/20261030100000_kostenstelle_umbenennen.sql b/db/migrations/20261030100000_kostenstelle_umbenennen.sql new file mode 100644 index 0000000..12a05a4 --- /dev/null +++ b/db/migrations/20261030100000_kostenstelle_umbenennen.sql @@ -0,0 +1,60 @@ +-- Eine Kostenstelle umbenennen — nur die Bezeichnung. +-- +-- Die Nummer bleibt: sie verbindet die Kostenstelle mit dem führenden System +-- und mit jeder Kontierung. Eine Umbenennung hat keinen Stichtag, sie gilt +-- auch rückwirkend für jede Anzeige. Die bisherige Bezeichnung steht im +-- Protokoll. +create or replace function public.rename_cost_center(payload jsonb) +returns void +language plpgsql +set search_path = public, pg_temp +as $$ +declare + v_id uuid := (payload->>'cost_center_id')::uuid; + v_name text := nullif(btrim(payload->>'name'), ''); + v_alt cost_centers%rowtype; +begin + perform require_hr_admin(); + + if v_name is null then + raise exception 'Bitte eine Bezeichnung angeben.'; + end if; + + select * into v_alt from cost_centers where id = v_id for update; + if not found then + raise exception 'Die Kostenstelle existiert nicht.'; + end if; + if v_alt.name = v_name then + raise exception 'Die Bezeichnung ist unverändert.'; + end if; + + update cost_centers set name = v_name 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(), 'Kostenstelle umbenannt', + v_alt.code || ' · ' || v_name, + 'Bisherige Bezeichnung: ' || v_alt.name); +end; +$$; + +grant execute on function public.rename_cost_center(jsonb) to alpenwerk_app; + +do $pruefung$ +declare + 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.prokind = 'f' and p.proname = 'rename_cost_center'; + if position(E'begin\n perform require_hr_admin();' in v_def) = 0 + or position('insert into audit_log' in v_def) = 0 then + raise exception 'rename_cost_center prüft die Rechte nicht oder schreibt kein Protokoll.'; + end if; + if position('set code' in v_def) > 0 then + raise exception 'rename_cost_center darf die Nummer nicht ändern.'; + end if; + if (select prosecdef from pg_proc where oid = 'public.rename_cost_center'::regproc) then + raise exception 'rename_cost_center darf nicht SECURITY DEFINER sein.'; + end if; +end +$pruefung$; diff --git a/db/tests/rauchtest.sql b/db/tests/rauchtest.sql index e0234b1..70dccb7 100644 --- a/db/tests/rauchtest.sql +++ b/db/tests/rauchtest.sql @@ -670,7 +670,15 @@ 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/35 Kostenstelle angelegt und zugeordnet, doppelte Nummer abgewiesen'; + -- Umbenennen ändert nur die Bezeichnung; Nummer und Kontierung bleiben. + perform rename_cost_center(jsonb_build_object('cost_center_id', v_stelle, 'name', 'Rauchtest Umbenannt')); + if (select code <> 'RAUCHTEST-KST' or name <> 'Rauchtest Umbenannt' from cost_centers where id = v_stelle) + or not exists (select 1 from position_cost_centers where position_id = v_p8 and cost_center_id = v_stelle) + or not exists (select 1 from audit_log where action = 'Kostenstelle umbenannt' + and details = 'Bisherige Bezeichnung: Rauchtest Kostenstelle') then + raise exception 'Die Kostenstelle ist nicht umbenannt, hat ihre Nummer oder Kontierung verloren oder fehlt im Protokoll.'; + end if; + raise notice '23/35 Kostenstelle angelegt, zugeordnet und umbenannt, doppelte Nummer abgewiesen'; -- ── Geplante Abwesenheit berichtigen (20261020120000) ─────────── -- Über „Eintrag berichtigen": der Beginn wandert, die geplante Rückkehr diff --git a/lib/austausch/importieren.ts b/lib/austausch/importieren.ts index 91ca39e..488a4d4 100644 --- a/lib/austausch/importieren.ts +++ b/lib/austausch/importieren.ts @@ -430,8 +430,13 @@ function kostenstelleZeile(tx: Tx, zeile: Zeile, bestand: Bestand, heute: string const code = String(zeile.z.schluessel); const alt = bestand.kostenstellen.get(code); if (alt) { - ohneWirksamAb(zeile, heute, "eine bestehende Kostenstelle ändert sich hier nicht."); - return nurNeuanlage(zeile, { name: alt.name }, "Bestehende Kostenstellen"); + const g = zeile.geaendert({ name: alt.name }); + if (g.size === 0) return; + ohneWirksamAb(zeile, heute, "eine neue Bezeichnung gilt sofort."); + zeile.schritt("Kostenstelle umbenannt", "name", async () => { + await rufe(tx, "rename_cost_center", { cost_center_id: alt.id, name: g.get("name") }); + }); + return; } if (!pflichtNeu(zeile)) return; zeile.geaendert(null); diff --git a/lib/austausch/katalog.ts b/lib/austausch/katalog.ts index 27116e9..9731562 100644 --- a/lib/austausch/katalog.ts +++ b/lib/austausch/katalog.ts @@ -603,10 +603,26 @@ export const BLATT_PLANSTELLEN: Blatt = { export const BLATT_KOSTENSTELLEN: Blatt = { name: "Kostenstellen", - zweck: "Kostenstellen. Neue werden angelegt; bestehende ändern sich hier nicht.", - schluessel: { titel: "Kostenstellennummer", feld: "code", format: TEXT, pflichtNeu: true, pflicht: true }, - wirksamAb: "Bei einer neuen Kostenstelle ihr Beginn (leer = heute).", - spalten: [{ titel: "Bezeichnung", feld: "name", format: TEXT, pflichtNeu: true, pflicht: true }], + zweck: "Kostenstellen. Neue werden angelegt; bei bestehenden lässt sich die Bezeichnung ändern, die Nummer nicht.", + schluessel: { + titel: "Kostenstellennummer", + feld: "code", + format: TEXT, + hinweis: "Vorhandene Nummer = Änderung der Bezeichnung, neue Nummer = Neuanlage. Die Nummer selbst lässt sich nicht ändern.", + pflichtNeu: true, + pflicht: true, + }, + wirksamAb: "Bei einer neuen Kostenstelle ihr Beginn (leer = heute). Eine neue Bezeichnung gilt sofort; dann leer lassen.", + spalten: [ + { + titel: "Bezeichnung", + feld: "name", + format: TEXT, + hinweis: "Eine andere Bezeichnung benennt die Kostenstelle um; die bisherige steht im Audit-Log.", + pflichtNeu: true, + pflicht: true, + }, + ], }; export const BLATT_STANDORTE: Blatt = { diff --git a/lib/types.ts b/lib/types.ts index e7c49e0..d694e47 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -931,6 +931,7 @@ export type Database = { update_position: { Args: { payload: Record }; Returns: void }; set_position_cost_center: { Args: { payload: Record }; Returns: void }; create_cost_center: { Args: { payload: Record }; Returns: string }; + rename_cost_center: { Args: { payload: Record }; Returns: void }; create_location: { Args: { payload: Record }; Returns: string }; create_job: { Args: { payload: Record }; Returns: string }; set_org_unit_valid_to: { Args: { payload: Record }; Returns: void }; diff --git a/tests/integration/austausch-importieren.test.ts b/tests/integration/austausch-importieren.test.ts index 86f499a..0ac4700 100644 --- a/tests/integration/austausch-importieren.test.ts +++ b/tests/integration/austausch-importieren.test.ts @@ -273,7 +273,6 @@ describe.skipIf(!process.env.DATABASE_URL)("Import gegen die Datenbank", () => { const heute = todayIso(); const { nr, a, b } = await bestand(tx); const kst = await rufe(tx, "create_cost_center", { code: "IMPORT-K", name: "Import Kostenstelle", valid_from: "2020-01-01" }); - void kst; const kontakt = await rufe(tx, "add_emergency_contact", { employee_id: a, name: "Alt Kontakt", phone: "+43 5", relation: "Kind" }); await rufe(tx, "add_emergency_contact", { employee_id: a, name: "Weg Kontakt", phone: "+43 6", relation: "Kind" }); await rufe(tx, "add_employee_dependent", { employee_id: a, first_name: "Weg", last_name: "Import", relationship: "Kind" }); @@ -283,6 +282,7 @@ describe.skipIf(!process.env.DATABASE_URL)("Import gegen die Datenbank", () => { const mappe = await herunterladen(tx, heute); setze(mappe, "Organisationseinheiten", "IMPORT-1", { Bezeichnung: "Import umbenannt" }); + setze(mappe, "Kostenstellen", "IMPORT-K", { Bezeichnung: "Import Kostenstelle umbenannt" }); setze(mappe, "Planstellen", "ID9204", { Tätigkeit: "Import Ganz Neu", Kostenstelle: "IMPORT-K", "Gültig bis": new Date(Date.UTC(2099, 0, 1)) }); const notfall = mappe.getWorksheet("Notfallkontakte")!; notfall.eachRow((row, z) => { @@ -307,6 +307,10 @@ describe.skipIf(!process.env.DATABASE_URL)("Import gegen die Datenbank", () => { .where("p.position_number", "=", "ID9204") .executeTakeFirstOrThrow(); expect(ps4).toEqual({ title: "Import Ganz Neu", valid_to: "2099-01-01" }); + expect(await tx.selectFrom("cost_centers").select(["code", "name"]).where("id", "=", kst).executeTakeFirstOrThrow()).toEqual({ + code: "IMPORT-K", + name: "Import Kostenstelle umbenannt", + }); const kontakte = await tx.selectFrom("employee_emergency_contacts").select(["name", "phone", "relation"]).where("employee_id", "=", a).orderBy("name").execute(); expect(kontakte).toEqual([ { name: "Alt Kontakt", phone: "+43 7", relation: "Kind" },