feat: add billing period selection
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
ba673ad732
commit
d37affc80f
15 files changed
+1625
No files matched your search
@@ -0,0 +1,264 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo, useState } from "react";
|
||||
import Link from "next/link";
|
||||
|
||||
import type { BillingPosition, RepeatDownloadPosition } from "@/modules/billing/queries";
|
||||
import { formatCents } from "@/modules/billing/format";
|
||||
|
||||
type BillingSelectionProps = {
|
||||
editorialId: string;
|
||||
positions: BillingPosition[];
|
||||
repeatDownloads: RepeatDownloadPosition[];
|
||||
year: number;
|
||||
month: number | null;
|
||||
quarter: number | null;
|
||||
isQuarter: boolean;
|
||||
periodLabel: string;
|
||||
};
|
||||
|
||||
const MONTHS = [
|
||||
"Januar",
|
||||
"Februar",
|
||||
"März",
|
||||
"April",
|
||||
"Mai",
|
||||
"Juni",
|
||||
"Juli",
|
||||
"August",
|
||||
"September",
|
||||
"Oktober",
|
||||
"November",
|
||||
"Dezember",
|
||||
];
|
||||
|
||||
const STATUS_LABELS: Record<BillingPosition["status"], string> = {
|
||||
open: "Offen",
|
||||
billed: "Abgerechnet",
|
||||
reopened: "Wiedereröffnet",
|
||||
};
|
||||
|
||||
export function BillingSelection({
|
||||
editorialId,
|
||||
positions,
|
||||
repeatDownloads,
|
||||
year,
|
||||
month,
|
||||
quarter,
|
||||
isQuarter,
|
||||
periodLabel,
|
||||
}: BillingSelectionProps) {
|
||||
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());
|
||||
|
||||
const selectablePositions = useMemo(
|
||||
() => positions.filter((p) => p.selectable),
|
||||
[positions],
|
||||
);
|
||||
|
||||
const selectedCount = selectedIds.size;
|
||||
const selectedTotalCents = useMemo(
|
||||
() =>
|
||||
positions.reduce(
|
||||
(sum, p) => (selectedIds.has(p.id) ? sum + p.priceCents : sum),
|
||||
0,
|
||||
),
|
||||
[positions, selectedIds],
|
||||
);
|
||||
|
||||
function togglePosition(id: string) {
|
||||
setSelectedIds((prev) => {
|
||||
const next = new Set(prev);
|
||||
if (next.has(id)) next.delete(id);
|
||||
else next.add(id);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
function selectAllOpen() {
|
||||
setSelectedIds(new Set(selectablePositions.map((p) => p.id)));
|
||||
}
|
||||
|
||||
function clearSelection() {
|
||||
setSelectedIds(new Set());
|
||||
}
|
||||
|
||||
const quarterParam = isQuarter ? null : Math.ceil((month ?? 1) / 3);
|
||||
const quarterHref = quarter
|
||||
? `/admin/abrechnungen/${editorialId}?year=${year}&quarter=${quarter}`
|
||||
: `/admin/abrechnungen/${editorialId}?year=${year}&quarter=${quarterParam}`;
|
||||
const monthHref = `/admin/abrechnungen/${editorialId}?year=${year}&month=${month ?? (quarter ? (quarter - 1) * 3 + 1 : new Date().getUTCMonth() + 1)}`;
|
||||
|
||||
return (
|
||||
<>
|
||||
<PeriodSelector year={year} month={month} quarter={quarter} isQuarter={isQuarter} />
|
||||
|
||||
<div className="billing-selection-controls">
|
||||
<button type="button" onClick={selectAllOpen} disabled={selectablePositions.length === 0}>
|
||||
{isQuarter ? "Alle offenen im Quartal" : "Alle offenen im Monat"}
|
||||
</button>
|
||||
{isQuarter ? (
|
||||
<Link href={monthHref} className="billing-nav-link">
|
||||
Monatsansicht
|
||||
</Link>
|
||||
) : (
|
||||
<Link href={quarterHref} className="billing-nav-link">
|
||||
Ganzes Quartal
|
||||
</Link>
|
||||
)}
|
||||
<button type="button" onClick={clearSelection} disabled={selectedCount === 0}>
|
||||
Auswahl aufheben
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p className="billing-period-label" aria-live="polite">
|
||||
Zeitraum: {periodLabel}
|
||||
</p>
|
||||
|
||||
{positions.length === 0 ? (
|
||||
<p className="billing-empty">Keine abrechenbaren Nutzungen in diesem Zeitraum.</p>
|
||||
) : (
|
||||
<table className="billing-table">
|
||||
<caption className="billing-caption">
|
||||
Abrechenbare Nutzungen
|
||||
</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Auswahl</th>
|
||||
<th scope="col">Datei</th>
|
||||
<th scope="col">Album</th>
|
||||
<th scope="col">Zweck</th>
|
||||
<th scope="col">Referenz</th>
|
||||
<th scope="col">Preis</th>
|
||||
<th scope="col">Status</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{positions.map((position) => (
|
||||
<tr key={position.id} className={position.selectable ? "" : "billing-row-billed"}>
|
||||
<td>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selectedIds.has(position.id)}
|
||||
onChange={() => togglePosition(position.id)}
|
||||
disabled={!position.selectable}
|
||||
aria-label={`${position.filename} auswählen`}
|
||||
/>
|
||||
</td>
|
||||
<td>{position.filename}</td>
|
||||
<td>{position.albumName}</td>
|
||||
<td>{position.purpose}</td>
|
||||
<td>{position.reference ?? "—"}</td>
|
||||
<td className="billing-price">{formatCents(position.priceCents)}</td>
|
||||
<td>
|
||||
<span className="billing-status">
|
||||
{STATUS_LABELS[position.status]}
|
||||
{position.status === "billed" && position.reportNumber !== null && (
|
||||
<span className="billing-report-ref">
|
||||
{" "}· Bericht Nr. {position.reportNumber}
|
||||
{position.invoiceNumber ? ` · ${position.invoiceNumber}` : ""}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
|
||||
{repeatDownloads.length > 0 && (
|
||||
<section aria-labelledby="repeat-downloads-title" className="billing-repeat-section">
|
||||
<h2 id="repeat-downloads-title">Kostenlose Wiederabrufe</h2>
|
||||
<p className="form-note">
|
||||
Wiederholte Downloads rufen bereits abgerechnete Nutzungen erneut ab und sind nicht
|
||||
abrechenbar.
|
||||
</p>
|
||||
<table className="billing-table billing-table-repeat">
|
||||
<caption className="billing-caption">Wiederabrufe im Zeitraum</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Datei</th>
|
||||
<th scope="col">Datum</th>
|
||||
<th scope="col">Status</th>
|
||||
<th scope="col">Notiz</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{repeatDownloads.map((download) => (
|
||||
<tr key={download.id}>
|
||||
<td>{download.assetId}</td>
|
||||
<td>
|
||||
<time dateTime={download.authorizedAt.toISOString()}>
|
||||
{download.authorizedAt.toLocaleDateString("de-DE")}
|
||||
</time>
|
||||
</td>
|
||||
<td>{download.status}</td>
|
||||
<td>{download.repeatDownloadNote ?? "—"}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
)}
|
||||
|
||||
<div className="billing-summary" aria-live="polite">
|
||||
<p>
|
||||
<strong>{selectedCount}</strong> {selectedCount === 1 ? "Nutzung" : "Nutzungen"} ausgewählt
|
||||
</p>
|
||||
<p className="billing-summary-total">
|
||||
Summe: <strong>{formatCents(selectedTotalCents)}</strong>
|
||||
</p>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
type PeriodSelectorProps = {
|
||||
year: number;
|
||||
month: number | null;
|
||||
quarter: number | null;
|
||||
isQuarter: boolean;
|
||||
};
|
||||
|
||||
function PeriodSelector({ year, month, quarter, isQuarter }: PeriodSelectorProps) {
|
||||
const years = [2025, 2026, 2027];
|
||||
|
||||
return (
|
||||
<form className="billing-period-selector" method="get">
|
||||
<label>
|
||||
<span>Jahr</span>
|
||||
<select name="year" defaultValue={year}>
|
||||
{years.map((y) => (
|
||||
<option key={y} value={y}>
|
||||
{y}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{isQuarter ? (
|
||||
<label>
|
||||
<span>Quartal</span>
|
||||
<select name="quarter" defaultValue={quarter ?? 1}>
|
||||
{[1, 2, 3, 4].map((q) => (
|
||||
<option key={q} value={q}>
|
||||
Q{q}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
) : (
|
||||
<label>
|
||||
<span>Monat</span>
|
||||
<select name="month" defaultValue={month ?? 8}>
|
||||
{MONTHS.map((name, index) => (
|
||||
<option key={index} value={index + 1}>
|
||||
{name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
<button type="submit">Zeitraum laden</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user