@@ -1,49 +1,77 @@
"use client" ;
import { Download } from "lucide-react" ;
import Link from "next/link" ;
import { useRef , useState } from "react" ;
import { Button } from "@/components/ui/Button" ;
import { CARD_CLASS } from "@/components/ui/Card" ;
import type { Aenderung } from "@/lib/austausch/importieren" ;
import type { Meldung } from "@/lib/austausch/lesen" ;
// Der Ablauf hat bewusst zwei Schritte : prüfen, dann übernehme n.
// „Änderungen importieren" : prüfen, dann speicher n.
//
// Ein Import ist nicht rückgängig zu machen. Wer 800 Zeilen schickt, soll
// vorher sehen, was entstehen wü rde — und bei einem Fehler die Zeilennummer
// lesen, nicht „Import fehlgeschlagen".
type Befund = { blatt : string ; zeile : number | null ; spalte : string | null ; wert? : string ; meldung : string } ;
// Beide Schritte schicken dieselbe Datei; der Server merkt si ch zwischen
// ihnen nichts. Gespeichert wi rd nur, was beim Speichern vollständig gültig
// ist — alles oder nichts. Bei Fehlern gibt es die Datei zurück, mit roten
// Zellen und der Meldung als Kommentar: darin korrigieren und erneut
// importieren.
type Antwort = {
ok : boolean ;
gepruef t : boolean ;
blaetter : string [ ] ;
fehler : Befund [ ] ;
hinweise : Bef und [ ] ;
anzahl : Record < string , number> ;
bericht? : Record < string , number > ;
meldung? : string ;
modus : "vorschau" | "speichern" ;
gespeicher t : boolean ;
fehler : number ;
warnungen : number ;
meldungen : Meld ung [ ] ;
meldungenGesamt : number;
aenderungen : Aenderung [ ] ;
unveraendert : number ;
fehlerdatei : string | null ;
dateiname : string ;
error? : string ;
} ;
/** Mehr als das zeigt niemand durch; der Rest steht in der Anzahl. */
const MAX_ANZEIGE = 200 ;
function spaltenname ( nummer : number ) : string {
let n = nummer ;
let text = "" ;
while ( n > 0 ) {
text = String . fromCharCode ( 65 + ( ( n - 1 ) % 26 ) ) + text ;
n = Math . floor ( ( n - 1 ) / 26 ) ;
}
return text ;
}
function fehlerdateiLaden ( antwort : Antwort ) {
if ( ! antwort . fehlerdatei ) return ;
const bytes = Uint8Array . from ( atob ( antwort . fehlerdatei ) , ( c ) = > c . charCodeAt ( 0 ) ) ;
const url = URL . createObjectURL (
new Blob ( [ bytes ] , { type : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" } )
) ;
const a = document . createElement ( "a" ) ;
a . href = url ;
a . download = antwort . dateiname . replace ( /\.xlsx$/i , "" ) + "-fehler.xlsx" ;
a . click ( ) ;
URL . revokeObjectURL ( url ) ;
}
export function ImportWorkbench() {
const [ dateien , setDateien ] = useState < File [ ] > ( [ ] ) ;
const [ datei , setDatei ] = useState < File | null > ( null ) ;
const [ antwort , setAntwort ] = useState < Antwort | null > ( null ) ;
const [ laeuft , setLaeuft ] = useState < "pruefen " | "uebernehme n" | null > ( null ) ;
const [ laeuft , setLaeuft ] = useState < "vorschau " | "speicher n" | null > ( null ) ;
const [ fehlschlag , setFehlschlag ] = useState < string | null > ( null ) ;
const eingabe = useRef < HTMLInputElement > ( null ) ;
async function senden ( nurPruefen : boolean ) {
if ( dateien . length === 0 ) return ;
setLaeuft ( nurPruefen ? "pruefen" : "uebernehmen" ) ;
async function senden ( modus : "vorschau" | "speichern" ) {
if ( ! datei ) return ;
setLaeuft ( modus ) ;
setFehlschlag ( null ) ;
try {
const daten = new FormData ( ) ;
for ( const d of dateien ) daten . append ( "datei" , d ) ;
if ( nurPruefen ) daten . append ( "pruefen" , "1" ) ;
const antwort = await fetch ( "/api/import" , { method : "POST" , body : daten } ) ;
const inhalt = ( await antwort . json ( ) ) as Antwort ;
setAntwort ( inhalt ) ;
daten . append ( "datei" , datei ) ;
daten . append ( "modus" , modus ) ;
const r = await fetch ( "/api/austausch/ importieren " , { method : "POST" , body : daten } ) ;
const inhalt = ( await r . json ( ) ) as Antwort ;
if ( ! r . ok ) setFehlschlag ( inhalt . error ? ? "Die Datei konnte nicht verarbeitet werden." ) ;
else setAntwort ( inhalt ) ;
} catch {
setFehlschlag ( "Die Datei konnte nicht übertragen werden. Bitte erneut versuchen." ) ;
} finally {
@@ -52,56 +80,37 @@ export function ImportWorkbench() {
}
function neueAuswahl ( liste : FileList | null ) {
setDateien ( liste ? Array . from ( liste ) : [ ] ) ;
setDatei ( liste ? . [ 0 ] ? ? null ) ;
// Ein alter Bericht zu einer neuen Datei ist schlimmer als keiner.
setAntwort ( null ) ;
setFehlschlag ( null ) ;
}
const uebernommen = antwort ? . berich t ! == undefined ;
const berei t = antwort ? . ok === true && antwort . geprueft && ! uebernommen ;
const neu = antwort ? . aenderungen . filter ( ( a ) = > a . ar t = == "neu" ) . length ? ? 0 ;
const geaender t = ( antwort ? . aenderungen . length ? ? 0 ) - neu ;
const speicherbar = antwort ? . modus === "vorschau" && antwort . fehler === 0 && antwort . aenderungen . length > 0 ;
return (
< div className = "flex flex-col gap-4" >
< section className = { ` ${ CARD_CLASS } p-5 ` } >
< div className = "flex flex-wrap items-start justify-between gap-4" >
< div >
< h2 className = "text-sm font-bold text-ink" > Datei wählen < / h2 >
< p className = "mt-1 max-w-prose text-sm text-ink-muted" >
Eine Excel - Mappe mit den Blättern < strong > Standorte < / strong > , < strong > Organisation < / strong > , { " " }
< strong > Jobkatalog < / strong > , < strong > Planstellen < / strong > , < strong > Personen < / strong > , { " " }
< strong > Historie < / strong > und < strong > Angehörige < / strong > — oder je Blatt eine CSV - Datei , deren Name dem
Blatt entspricht . Nicht jedes Blatt muss dabei sein .
< / p >
< / div >
< a
href = "/api/import/template"
className = "shrink-0 rounded-md border border-border px-3 py-2 text-sm font-semibold text-ink hover:bg-brand-50
focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-brand-500"
>
Vorlage herunterladen
< / a >
< / div >
< h2 className = "text-sm font-bold text-ink" > Ä nderungen importieren < / h2 >
< p className = "mt-1 max-w-prose text-sm text-ink-muted" >
Die heruntergeladene Datei nach der Bearbeitung — oder eine Fehlerdatei nach der Korrektur . Vor dem Speichern
sehen Sie alle Ä nderungen .
< / p >
< div className = "mt-4 flex flex-wrap items-center gap-3" >
< input
ref = { eingabe }
type = "file"
multiple
accept = ".xlsx,.xlsm,.csv"
accept = ".xlsx"
onChange = { ( e ) = > neueAuswahl ( e . target . files ) }
className = "block w-full max-w-md text-sm text-ink-muted file:mr-3 file:rounded-md file:border file:border-border
file:bg-surface file:px-3 file:py-2 file:text-sm file:font-semibold file:text-ink hover:file:bg-brand-50"
/ >
< Button onClick = { ( ) = > senden ( true ) } disabled = { dateien . length === 0 || laeuft !== null } >
{ laeuft === "pruefen " ? "Wird geprüft…" : "Prüfen" }
< Button onClick = { ( ) = > senden ( "vorschau" ) } disabled = { ! datei || laeuft !== null } >
{ laeuft === "vorschau " ? "Wird geprüft…" : "Prüfen" }
< / Button >
< / div >
{ dateien . length > 0 && (
< p className = "mt-2 text-xs text-ink-muted" >
{ dateien . length === 1 ? dateien [ 0 ] . name : ` ${ dateien . length } Dateien ` } ausgewählt
< / p >
) }
< / section >
{ fehlschlag && (
@@ -110,101 +119,161 @@ export function ImportWorkbench() {
< / p >
) }
{ antwort ? . meldung && (
< p role = "alert" className = "rounded-md border border-danger-text/20 bg-danger-bg px-4 py-3 text-sm text-danger-text" >
{ antwort . meldung }
< / p >
) }
{ antwort && ! antwort . meldung && (
< section className = { ` ${ CARD_CLASS } p-5 ` } >
< h2 className = "text-sm font-bold text-ink" > { uebernommen ? "Übernommen" : "Ergebnis der Prüfung" } < / h2 >
< table className = "mt-3 w-full max-w-md text-sm" >
< tbody >
{ Object . entries ( uebernommen ? antwort . bericht ! : antwort . anzahl )
. filter ( ( [ , n ] ) = > n > 0 )
. map ( ( [ blatt , n ] ) = > (
< tr key = { blatt } className = "border-b border-border-subtle last:border-0" >
< td className = "py-1.5 text-ink-body " > { blatt } < / td >
< td className = "py-1.5 text-right font-semibold tabular-nums text-ink" > { n } < / td >
< / tr >
) ) }
< / tbody >
< / table >
{ uebernommen ? (
< p className = "mt-4 text-sm text-ink-muted" >
Die Daten stehen jetzt in der Anwendung . Der Vorgang ist im Audit - Log vermerkt .
{ antwort && (
< section className = { ` ${ CARD_CLASS } flex flex-col gap-3 p-5 ` } >
< h2 className = "text-sm font-bold text-ink" >
{ antwort . gespeichert ? "Gespeichert" : antwort . modus === "speichern" ? "Nicht gespeichert" : "Vorschau" }
< / h2 >
< p className = "text-sm text-ink-body" >
< strong > { neu } < / strong > neu · < strong > { geaendert } < / strong > geändert · { antwort . unveraendert } unverändert
{ antwort . fehler > 0 && (
< span className = "font-semi bold text-danger-text" >
{ " " }
· { antwort . fehler } Fehler
< / span >
) }
{ antwort . warnungen > 0 && < span className = "text-warning-text" > · { antwort . warnungen } Warnungen < / span > }
< / p >
{ antwort . gespeichert ? (
< p className = "text-sm text-ink-muted " > Die Ä nderungen stehen in der Historie und im Audit - Log . < / p >
) : antwort . fehler > 0 ? (
< p className = "text-sm text-ink-body" >
Es wurde nichts gespeichert . Die Fehlerdatei enthält Ihre Datei mit den betroffenen Zellen rot markiert ;
darin korrigieren und erneut importieren .
< / p >
) : antwort . ok ? (
< >
< p className = "mt-4 text-sm text-ink-body" >
Keine Beanstandung . Beim Ü bernehmen entsteht genau das oben Gezeigte — alles in einem Zug oder gar
nichts .
< / p >
< div className = "mt-3" >
< Button onClick = { ( ) = > senden ( false ) } disabled = { laeuft !== null || ! bereit } >
{ laeuft === "uebernehmen" ? "Wird übernommen…" : "Übernehmen" }
< / Button >
< / div >
< / >
) : antwort . aenderungen . length === 0 ? (
< p className = "text-sm text-ink-muted" > Die Datei enthält keine Ä nderung . < / p >
) : (
< p className = "mt-4 text-sm font-semibold text-danger-text " >
{ antwort . fehler . length } Beanstandung { antwort . fehler . length === 1 ? "" : "en" } — es wurde nichts
geschr ieb en.
< p className = "text-sm text-ink-body " >
Keine Fehler . Beim Speichern entsteht genau das unten Gezeigte — all es in einem Zug .
{ antwort . warnungen > 0 && " Zeilen mit Warnung werden beim Speichern abgew ies en." }
< / p >
) }
< div className = "flex flex-wrap gap-2" >
{ antwort . fehlerdatei && (
< Button variant = "secondary" onClick = { ( ) = > fehlerdateiLaden ( antwort ) } >
< Download className = "h-4 w-4" / >
Fehlerdatei herunterladen
< / Button >
) }
{ speicherbar && (
< Button onClick = { ( ) = > senden ( "speichern" ) } disabled = { laeuft !== null } >
{ laeuft === "speichern" ? "Wird gespeichert…" : "Speichern" }
< / Button >
) }
< / div >
< / section >
) }
{ antwort && antwort . fehler . length > 0 && (
< BefundListe titel = "Zu beheb en" befund e= { antwort . fehler } art = "fehler" / >
) }
{ antwort && antwort . hinweise . length > 0 && (
< BefundListe titel = "Hinweise" befunde = { antwort . hinweise } art = "hinweis" / >
) }
{ antwort && antwort . meldungen . length > 0 && < Meldungen antwort = { antwort } / > }
{ antwort && antwort . aenderungen . length > 0 && < Aenderung en list e= { antwort . aenderungen } / > }
< / div >
) ;
}
function BefundListe ( { titel , befunde , art } : { titel : string ; befunde : Befund [ ] ; art : "fehler" | "hinweis" } ) {
const sichtbar = befunde . slice ( 0 , MAX_ANZEIGE ) ;
function Meldungen ( { antwort } : { antwort : Antwort } ) {
return (
< section className = { ` overflow-x-auto overflow-y-hidden ${ CARD_CLASS } ` } >
< h2 className = "px-5 pt-5 text-sm font-bold text-ink" >
{ titel } < span className = "font-normal text-ink-muted" > ( { befunde . length } ) < / span >
Meldungen < span className = "font-normal text-ink-muted" > ( { antwort . meldungenGesamt } ) < / span >
< / h2 >
< table className = "mt-3 w-full min-w-[640px] text-sm" >
< thead >
< tr className = "border-y border-border bg-surface text-left text-[11px] font-bold uppercase tracking-wider text-ink-muted" >
< th className = "px-5 py-2" > Blat t< / th >
< th className = "px-3 py-2" > Zeile < / th >
< th className = "px-3 py-2" > Spalt e< / th >
< th className = "px-3 py-2" > Wert < / th >
< th className = "px-5 py-2" > Was zu tun ist < / th >
< th className = "px-5 py-2" > Ar t< / th >
< th className = "px-3 py-2" > Blatt < / th >
< th className = "px-3 py-2" > Zell e< / th >
< th className = "px-5 py-2" > Meldung < / th >
< / tr >
< / thead >
< tbody >
{ sichtbar . map ( ( b , i ) = > (
{ antwort . meldungen . map ( ( m , i ) = > (
< tr key = { i } className = "border-b border-border-subtle last:border-0" >
< td className = "whitespace-nowrap px-5 py-2 text-ink-body" > { b . blatt } < / td >
< td className = " px-3 py-2 tabular-nums text-ink-muted" > { b . zeile ? ? "– " } < / td >
< td className = "px-3 py-2 text-ink-body" > { b . spalte ? ? "– " } < / td >
< td className = "max-w-[16rem] truncate px-3 py-2 text-ink-muted" title = { b . wert } >
{ b . wert ? ? "" }
< td
className = { ` whitespace-nowrap px-5 py-2 font-semibold ${
m . stufe === "fehler" ? "text-danger-text" : m . stufe === "warnung" ? "text-warning-text" : "text-ink-muted"
} ` }
>
{ m . stufe === "fehler" ? "Fehler" : m . stufe === "warnung" ? "Warnung" : "Hinweis" }
< / td >
< td className = "whitespace-nowrap px-3 py-2 text-ink-body" > { m . ort ? . blatt ? ? "Datei" } < / td >
< td className = "whitespace-nowrap px-3 py-2 tabular-nums text-ink-muted" >
{ m . ort ? . zeile ? ` ${ m . ort . spalte ? spaltenname ( m . ort . spalte ) : "" } ${ m . ort . zeile } ` : "– " }
< / td >
< td className = "px-5 py-2 text-ink-body" >
{ m . text }
{ m . akte && (
< >
{ " " }
< Link href = { ` /employees/ ${ m . akte } ` } className = "font-semibold text-brand-700 hover:underline" >
Zur Personalakte
< / Link >
< / >
) }
< / td >
< td className = { ` px-5 py-2 ${ art === "fehler" ? "text-danger-text" : "text-ink-muted" } ` } > { b . meldung } < / td >
< / tr >
) ) }
< / tbody >
< / table >
{ befunde . length > sichtbar . length && (
{ antwort . meldungenGesamt > antwort . meldungen . length && (
< p className = "px-5 py-3 text-xs text-ink-muted" >
Weitere { befunde . length - sichtbar . length } nicht angezeigt . Oft hängen viele Meldungen an einer Ursache —
nach der Korrektur erneut prüfen .
Weitere { antwort . meldungenGesamt - antwort . meldungen . length } Meldungen stehen in der Fehlerdatei .
< / p >
) }
< / section >
) ;
}
function Aenderungen ( { liste } : { liste : Aenderung [ ] } ) {
return (
< section className = { ` overflow-x-auto overflow-y-hidden ${ CARD_CLASS } ` } >
< h2 className = "px-5 pt-5 text-sm font-bold text-ink" >
Ä nderungen < span className = "font-normal text-ink-muted" > ( { liste . length } ) < / span >
< / h2 >
< table className = "mt-3 w-full min-w-[720px] text-sm" >
< thead >
< tr className = "border-y border-border bg-surface text-left text-[11px] font-bold uppercase tracking-wider text-ink-muted" >
< th className = "px-5 py-2" > Blatt / Zeile < / th >
< th className = "px-3 py-2" > Datensatz < / th >
< th className = "px-3 py-2" > Vorgang < / th >
< th className = "px-5 py-2" > Feld : alt → neu < / th >
< / tr >
< / thead >
< tbody >
{ liste . map ( ( a ) = > (
< tr key = { ` ${ a . blatt } - ${ a . zeile } ` } className = "border-b border-border-subtle align-top last:border-0" >
< td className = "whitespace-nowrap px-5 py-2 text-ink-muted" >
{ a . blatt } · { a . zeile }
< / td >
< td className = "px-3 py-2 text-ink-body" >
{ a . employeeId ? (
< Link href = { ` /employees/ ${ a . employeeId } ` } className = "font-semibold text-brand-700 hover:underline" >
{ a . name || a . schluessel }
< / Link >
) : (
< span className = "font-semibold" > { a . name || a . schluessel } < / span >
) }
< span className = "text-ink-muted" > ( { a . schluessel } ) < / span >
< / td >
< td className = "px-3 py-2 text-ink-body" >
{ a . vorgaenge . join ( ", " ) }
{ a . geplant && (
< span className = "ml-1 rounded bg-warning-bg px-1.5 py-0.5 text-xs font-semibold text-warning-text" > geplant < / span >
) }
< / td >
< td className = "px-5 py-2 text-ink-body" >
< ul className = "flex flex-col gap-0.5" >
{ a . felder . map ( ( f , i ) = > (
< li key = { i } >
< span className = "text-ink-muted" > { f . titel } : < / span > { a . art === "neu" ? f . neu : ` ${ f . alt } → ${ f . neu } ` }
< / li >
) ) }
< / ul >
< / td >
< / tr >
) ) }
< / tbody >
< / table >
< / section >
) ;
}