diff --git a/src/app/admin/abrechnungen/[editorialId]/BillingSelection.test.tsx b/src/app/admin/abrechnungen/[editorialId]/BillingSelection.test.tsx index 46ef1ed..bda4e34 100644 --- a/src/app/admin/abrechnungen/[editorialId]/BillingSelection.test.tsx +++ b/src/app/admin/abrechnungen/[editorialId]/BillingSelection.test.tsx @@ -4,6 +4,7 @@ import { render, screen, fireEvent, cleanup } from "@testing-library/react"; import { afterEach, expect, test } from "vitest"; import { BillingSelection } from "./BillingSelection"; +import type { FinalizedReportSummary } from "./page-content"; import type { BillingPosition, RepeatDownloadPosition } from "@/modules/billing/queries"; afterEach(cleanup); @@ -70,6 +71,7 @@ const defaultProps = { quarter: null as number | null, isQuarter: false, periodLabel: "August 2026", + reports: [] as FinalizedReportSummary[], }; function getCheckbox(filename: string): HTMLInputElement { diff --git a/src/app/admin/abrechnungen/[editorialId]/BillingSelection.tsx b/src/app/admin/abrechnungen/[editorialId]/BillingSelection.tsx index d540857..72cd94b 100644 --- a/src/app/admin/abrechnungen/[editorialId]/BillingSelection.tsx +++ b/src/app/admin/abrechnungen/[editorialId]/BillingSelection.tsx @@ -5,6 +5,7 @@ import Link from "next/link"; import type { BillingPosition, RepeatDownloadPosition } from "@/modules/billing/queries"; import { formatCents } from "@/modules/billing/format"; +import type { FinalizedReportSummary } from "./page-content"; type BillingSelectionProps = { editorialId: string; @@ -15,6 +16,7 @@ type BillingSelectionProps = { quarter: number | null; isQuarter: boolean; periodLabel: string; + reports: FinalizedReportSummary[]; }; const MONTHS = [ @@ -38,6 +40,12 @@ const STATUS_LABELS: Record = { reopened: "Wiedereröffnet", }; +const REPORT_STATUS_LABELS: Record = { + draft: "Entwurf", + final: "Erstellt", + cancelled: "Storniert", +}; + export function BillingSelection({ editorialId, positions, @@ -47,8 +55,14 @@ export function BillingSelection({ quarter, isQuarter, periodLabel, + reports, }: BillingSelectionProps) { const [selectedIds, setSelectedIds] = useState>(new Set()); + const [actionState, setActionState] = useState<{ + loading: boolean; + message: string | null; + isError: boolean; + }>({ loading: false, message: null, isError: false }); const selectablePositions = useMemo( () => positions.filter((p) => p.selectable), @@ -82,6 +96,72 @@ export function BillingSelection({ setSelectedIds(new Set()); } + const periodPayload = { year, ...(isQuarter ? { quarter: quarter ?? 1 } : { month: month ?? 1 }) }; + + async function handlePreview() { + if (selectedIds.size === 0) return; + setActionState({ loading: true, message: null, isError: false }); + try { + const response = await fetch("/api/billing-reports/preview", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + editorialId, + usageIds: [...selectedIds], + ...periodPayload, + }), + }); + if (!response.ok) { + const body = await response.json().catch(() => ({ code: "ERROR" })); + throw new Error(body.code ?? "Vorschau fehlgeschlagen"); + } + const blob = await response.blob(); + const url = URL.createObjectURL(blob); + window.open(url, "_blank"); + URL.revokeObjectURL(url); + setActionState({ loading: false, message: null, isError: false }); + } catch (error) { + setActionState({ + loading: false, + message: error instanceof Error ? error.message : "Vorschau fehlgeschlagen", + isError: true, + }); + } + } + + async function handleFinalize() { + if (selectedIds.size === 0) return; + setActionState({ loading: true, message: null, isError: false }); + try { + const response = await fetch("/api/billing-reports", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + editorialId, + usageIds: [...selectedIds], + ...periodPayload, + }), + }); + if (!response.ok) { + const body = await response.json().catch(() => ({ code: "ERROR" })); + throw new Error(body.code ?? "Abrechnung konnte nicht erstellt werden"); + } + const result = await response.json(); + setActionState({ + loading: false, + message: `Abrechnung Nr. ${result.reportNumber} wurde erstellt.`, + isError: false, + }); + window.location.reload(); + } catch (error) { + setActionState({ + loading: false, + message: error instanceof Error ? error.message : "Abrechnung fehlgeschlagen", + isError: true, + }); + } + } + const quarterParam = isQuarter ? null : Math.ceil((month ?? 1) / 3); const quarterHref = quarter ? `/admin/abrechnungen/${editorialId}?year=${year}&quarter=${quarter}` @@ -209,6 +289,57 @@ export function BillingSelection({ Summe: {formatCents(selectedTotalCents)}

+ +
+ + +
+ + {actionState.message && ( +

+ {actionState.message} +

+ )} + + {reports.length > 0 && ( +
+

Erstellte Abrechnungen

+
    + {reports.map((report) => ( +
  • +
    + Bericht Nr. {report.reportNumber} + {report.invoiceNumber && {report.invoiceNumber}} + Summe: {formatCents(report.netTotalCents)} + Status: {REPORT_STATUS_LABELS[report.status] ?? report.status} + + Erstellt am:{" "} + + +
    + {report.status === "final" && ( + + )} +
  • + ))} +
+
+ )} ); } @@ -261,4 +392,77 @@ function PeriodSelector({ year, month, quarter, isQuarter }: PeriodSelectorProps ); +} + +type CancelReportFormProps = { + reportId: string; + reportNumber: number; +}; + +function CancelReportForm({ reportId, reportNumber }: CancelReportFormProps) { + const [reason, setReason] = useState(""); + const [confirmed, setConfirmed] = useState(false); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const trimmed = reason.trim(); + const valid = trimmed.length >= 5 && trimmed.length <= 500 && confirmed; + + async function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + if (!valid) return; + setLoading(true); + setError(null); + try { + const response = await fetch(`/api/billing-reports/${encodeURIComponent(reportId)}/cancel`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ reason: trimmed, confirm: true }), + }); + if (!response.ok) { + const body = await response.json().catch(() => ({ code: "ERROR" })); + throw new Error(body.code ?? "Stornierung fehlgeschlagen"); + } + window.location.reload(); + } catch (err) { + setError(err instanceof Error ? err.message : "Stornierung fehlgeschlagen"); + setLoading(false); + } + } + + return ( +
+