From 38d4ce4dec63c443af24cf85db2e8fa86ecdf87b Mon Sep 17 00:00:00 2001 From: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com> Date: Mon, 20 Jul 2026 17:36:39 +0200 Subject: [PATCH] fix(zeiterfassung): mark today, honest interim reports, approver feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Submitter side: - Month grid now marks TODAY (Europe/Zurich) with a filled day-badge + ring + aria-current; it was invisible before (focus sat on the 1st). - Absence requests now confirm on submit ("Antrag eingereicht — wartet auf Genehmigung") instead of silently clearing the form. Monatsrapport: - A running month reports AS OF today, not month-end — pre-filled future days no longer show as "worked" and Soll/Ist/Saldo match the live dashboard. Interim sheets are labelled "Zwischenstand per " and note how many planned days are still excluded. - Drop the internal "Aus offiziellem Standardschedule" marker from the Bemerkung column so only real remarks show on the document. Approver side: - Single approve/reject/reopen/edit now show a positive acknowledgement in the review drawer (was silent) and the drawer stays open on the just-acted card — no more hunting for what you approved. - Add a Monatsrapport link in the drawer (footer + after approval) — the concrete next step for sending the sheet to the social worker. Companion data fix applied on prod: Georgy's Pensum reconciled to the Arbeitsplan (21h) and the unsourced −37h opening zeroed, so the saldo no longer manufactures a deficit. Co-Authored-By: Claude Fable 5 --- .../team/report/[userId]/[month]/page.tsx | 28 ++++++-- .../admin/timecards/TimecardReviewDrawer.tsx | 67 ++++++++++++++----- src/components/timecards/TimeOffPanel.tsx | 9 ++- .../timecards/TimecardMonthGrid.tsx | 39 ++++++++--- src/lib/services/report.ts | 22 +++++- 5 files changed, 129 insertions(+), 36 deletions(-) diff --git a/src/app/admin/team/report/[userId]/[month]/page.tsx b/src/app/admin/team/report/[userId]/[month]/page.tsx index de5ccddb6..eab6f691b 100644 --- a/src/app/admin/team/report/[userId]/[month]/page.tsx +++ b/src/app/admin/team/report/[userId]/[month]/page.tsx @@ -16,6 +16,7 @@ import { canAccessSection } from '@/lib/permissions' import { getMonthlyReport, REPORT_MONTH_REGEX } from '@/lib/services/report' import { getTimecardMonthLabel } from '@/lib/team/timecard-period-label' import { getDisplayDate } from '@/lib/team/timecard-utils' +import { SCHEDULE_FILL_DESCRIPTION } from '@/lib/team/schedule' import { formatTimecardDuration, getTimecardEntryCategoryLabel, getTimecardStatusLabel, isAbsenceCategory } from '@/config/timecards' import { ORG, LOCATIONS } from '@/config/org' import { PrintButton } from './PrintButton' @@ -45,7 +46,12 @@ export default async function MonthlyReportPage({ if (!report) notFound() const monthLabel = getTimecardMonthLabel(`${month}-01`) - const entries = report.card?.entries ?? [] + // Only days up to the as-of date are "worked" — a running month must not + // present pre-filled future days as done (they'd also inflate the totals). + const allEntries = report.card?.entries ?? [] + const entries = allEntries.filter(e => e.work_date <= report.asOfDate) + const futureCount = allEntries.length - entries.length + const asOfLabel = getDisplayDate(report.asOfDate) const workedMinutes = entries.filter(e => !isAbsenceCategory(e.category)).reduce((s, e) => s + e.duration_minutes, 0) const absenceMinutes = entries.filter(e => isAbsenceCategory(e.category)).reduce((s, e) => s + e.duration_minutes, 0) const absenceDaysByCategory = new Map() @@ -72,6 +78,11 @@ export default async function MonthlyReportPage({

Monatsrapport Arbeitszeit — {monthLabel}

+ {report.isInterim && ( +

+ Zwischenstand per {asOfLabel} — der Monat läuft noch; Zahlen bis zu diesem Tag. +

+ )} {/* Person + status */} @@ -88,15 +99,15 @@ export default async function MonthlyReportPage({ {/* Summary */}
-

Soll (Monat)

+

Soll {report.isInterim ? `(bis ${asOfLabel})` : '(Monat)'}

{report.saldo ? formatTimecardDuration(report.saldo.time.monthSollMinutes) : '—'}

-

Ist (Monat)

+

Ist {report.isInterim ? `(bis ${asOfLabel})` : '(Monat)'}

{report.saldo ? formatTimecardDuration(report.saldo.time.monthIstMinutes) : formatTimecardDuration(workedMinutes + absenceMinutes)}

-

Zeitsaldo (Ende Monat)

+

Zeitsaldo {report.isInterim ? `(per ${asOfLabel})` : '(Ende Monat)'}

{report.saldo ? `${report.saldo.time.saldoMinutes < 0 ? '−' : '+'}${formatTimecardDuration(Math.abs(report.saldo.time.saldoMinutes))}` @@ -145,11 +156,18 @@ export default async function MonthlyReportPage({ {e.start_time && e.end_time ? `${e.start_time}–${e.end_time}${e.break_minutes ? ` (${e.break_minutes}′ Pause)` : ''}` : '—'} {formatTimecardDuration(e.duration_minutes)} - {e.description ?? ''} + {/* The plan-fill source marker is internal noise on a document — + show only genuine, hand-written remarks. */} + {e.description === SCHEDULE_FILL_DESCRIPTION ? '' : (e.description ?? '')} ))} + {futureCount > 0 && ( +

+ {futureCount} geplante {futureCount === 1 ? 'Tag' : 'Tage'} nach dem {asOfLabel} noch nicht berücksichtigt (Monat läuft noch). +

+ )} {report.holidays.length > 0 && (

Feiertage im {monthLabel}: {report.holidays.map(h => `${getDisplayDate(h.date)} (${h.name})`).join(', ')} — reduzieren die Sollzeit automatisch. diff --git a/src/components/admin/timecards/TimecardReviewDrawer.tsx b/src/components/admin/timecards/TimecardReviewDrawer.tsx index b833b3c34..d8844527b 100644 --- a/src/components/admin/timecards/TimecardReviewDrawer.tsx +++ b/src/components/admin/timecards/TimecardReviewDrawer.tsx @@ -1,9 +1,10 @@ 'use client' -import { useEffect, useState } from 'react' +import { useCallback, useEffect, useState } from 'react' import { createPortal } from 'react-dom' import { useTranslations } from 'next-intl' -import { X, Check, Ban, Pencil, Save, RotateCcw, Send } from 'lucide-react' +import Link from 'next/link' +import { X, Check, Ban, Pencil, Save, RotateCcw, Send, FileText } from 'lucide-react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Select } from '@/components/ui/select' @@ -69,20 +70,23 @@ export function TimecardReviewDrawer({ const [note, setNote] = useState('') const [busy, setBusy] = useState(null) const [error, setError] = useState(null) + // Positive acknowledgement after an action — the drawer stays open showing + // this + the next step (the report), so the approver never has to hunt for + // the card they just acted on. + const [flash, setFlash] = useState(null) // eslint-disable-next-line react-hooks/set-state-in-effect -- portal mount guard (SSR-safe) useEffect(() => { setMounted(true) }, []) - useEffect(() => { - let active = true - apiFetch(`/api/admin/timecards/${cardId}`).then(r => { - if (!active) return - if (r.success && r.data) { setCard(r.data); setEntries(r.data.entries ?? []) } - else setError(r.error || t('draftLoadError')) - }) - return () => { active = false } + const load = useCallback(async () => { + const r = await apiFetch(`/api/admin/timecards/${cardId}`) + if (r.success && r.data) { setCard(r.data); setEntries(r.data.entries ?? []) } + else setError(r.error || t('draftLoadError')) }, [cardId, t]) + // eslint-disable-next-line react-hooks/set-state-in-effect -- async fetch → setState resolves off-render + useEffect(() => { void load() }, [load]) + const total = entries.reduce((s, e) => s + (Number(e.duration_minutes) || 0), 0) const isApproved = card?.status === 'approved' const isDraft = card?.status === 'draft' || card?.status === 'rejected' @@ -95,7 +99,7 @@ export function TimecardReviewDrawer({ const save = async () => { if (!card) return - setBusy('save'); setError(null) + setBusy('save'); setError(null); setFlash(null) const r = await apiFetch(`/api/admin/timecards/${cardId}`, { method: 'PUT', body: { @@ -108,41 +112,51 @@ export function TimecardReviewDrawer({ setBusy(null) if (!r.success) { setError(r.error || t('saveFailed')); return } setEditing(false) + await load() + setFlash(t('reviewDoneSaved')) onChanged() } const review = async (status: 'approved' | 'rejected') => { - setBusy(status); setError(null) + setBusy(status); setError(null); setFlash(null) const r = await apiFetch(`/api/admin/timecards/${cardId}`, { method: 'PATCH', body: { status, review_notes: note.trim() || null }, }) setBusy(null) if (!r.success) { setError(r.error || t('actionFailed')); return } + setNote('') + await load() + setFlash(status === 'approved' ? t('reviewDoneApproved') : t('reviewDoneRejected')) onChanged() - onClose() } // Proxy path: submit a draft on the owner's behalf (audited, owner notified). const submitOnBehalf = async () => { - setBusy('submit'); setError(null) + setBusy('submit'); setError(null); setFlash(null) const r = await apiFetch(`/api/admin/timecards/${cardId}/submit`, { method: 'POST' }) setBusy(null) if (!r.success) { setError(r.error || t('actionFailed')); return } + await load() + setFlash(t('reviewDoneSubmitted')) onChanged() - onClose() } // Un-approve: back to draft so the owner (or approver) can rework it. const reopen = async () => { - setBusy('reopen'); setError(null) + setBusy('reopen'); setError(null); setFlash(null) const r = await apiFetch(`/api/admin/timecards/${cardId}/reopen`, { method: 'POST' }) setBusy(null) if (!r.success) { setError(r.error || t('actionFailed')); return } + await load() + setFlash(t('reviewDoneReopened')) onChanged() - onClose() } + // Monatsrapport for this card's owner + month (the sheet the approver sends + // to the social worker) — the concrete next step after approving. + const reportHref = card ? `/admin/team/report/${card.user_id}/${card.period_start.slice(0, 7)}` : null + if (!mounted) return null return createPortal( @@ -179,6 +193,16 @@ export function TimecardReviewDrawer({

{error &&

{error}

} + {flash && ( +
+ {flash} + {isApproved && reportHref && ( + + {t('openReport')} + + )} +
+ )} {!card ? (

{t('loading')}

) : entries.length === 0 ? ( @@ -299,6 +323,15 @@ export function TimecardReviewDrawer({ )}
+ {!editing && reportHref && ( + + {t('openReport')} + + )}
, diff --git a/src/components/timecards/TimeOffPanel.tsx b/src/components/timecards/TimeOffPanel.tsx index 7da623712..662844586 100644 --- a/src/components/timecards/TimeOffPanel.tsx +++ b/src/components/timecards/TimeOffPanel.tsx @@ -41,6 +41,7 @@ export function TimeOffPanel() { const [note, setNote] = useState('') const [submitting, setSubmitting] = useState(false) const [error, setError] = useState(null) + const [success, setSuccess] = useState(null) const load = useCallback(async () => { const res = await apiFetch('/api/time-off') @@ -53,7 +54,7 @@ export function TimeOffPanel() { }, [open, load]) const submit = async () => { - setError(null) + setError(null); setSuccess(null) if (!startsOn || !endsOn) { setError(t('missingDates')) return @@ -71,6 +72,7 @@ export function TimeOffPanel() { setStartsOn('') setEndsOn('') setNote('') + setSuccess(t('requestSubmitted')) void load() } @@ -98,6 +100,11 @@ export function TimeOffPanel() { {error}

)} + {success && ( +

+ {success} +

+ )} {/* Request form */}
diff --git a/src/components/timecards/TimecardMonthGrid.tsx b/src/components/timecards/TimecardMonthGrid.tsx index 0032d666b..05671e8b7 100644 --- a/src/components/timecards/TimecardMonthGrid.tsx +++ b/src/components/timecards/TimecardMonthGrid.tsx @@ -106,6 +106,10 @@ export function TimecardMonthGrid({ return d === 0 ? 6 : d - 1 })() + // "Today" in Europe/Zurich (server saldo uses the same tz) — so the grid + // marks the real current day, not a UTC-shifted one near midnight. + const todayIso = new Intl.DateTimeFormat('en-CA', { timeZone: 'Europe/Zurich' }).format(new Date()) + return (
{isSelected && ( @@ -204,14 +212,23 @@ export function TimecardMonthGrid({