feat: show outstanding unbilled amount next to net price in editorial overview

Grouped SUM over usages with billingStatus open/reopened (same selectable
set as the billing page; voided excluded) per editorial, rendered as an
amber-highlighted 'Offen' suffix in each card footer.
This commit is contained in:
Leon Tonhäuser committed 2026-09-01 17:23:38 +02:00
1 parent 0931ffc777
commit 2a5a2f3815
2 files changed
+39 -16

No files matched your search

+38 -16
View File
@@ -1,24 +1,40 @@
import { desc } from "drizzle-orm";
import { desc, inArray, sql } from "drizzle-orm";
import Link from "next/link";
import { db } from "@/db/client";
import { editorialAccounts } from "@/db/schema";
import { editorialAccounts, usages } from "@/db/schema";
import { PageHeader } from "@/components/ui/page-header";
import { Card, CardContent } from "@/components/ui/card";
export default async function EditorialsPage() {
const editorials = await db
.select({
id: editorialAccounts.id,
name: editorialAccounts.name,
contactName: editorialAccounts.contactName,
loginEmail: editorialAccounts.loginEmail,
status: editorialAccounts.status,
currentPriceCents: editorialAccounts.currentPriceCents,
createdAt: editorialAccounts.createdAt,
archivedAt: editorialAccounts.archivedAt,
})
.from(editorialAccounts)
.orderBy(desc(editorialAccounts.createdAt));
const [editorials, openUsageTotals] = await Promise.all([
db
.select({
id: editorialAccounts.id,
name: editorialAccounts.name,
contactName: editorialAccounts.contactName,
loginEmail: editorialAccounts.loginEmail,
status: editorialAccounts.status,
currentPriceCents: editorialAccounts.currentPriceCents,
createdAt: editorialAccounts.createdAt,
archivedAt: editorialAccounts.archivedAt,
})
.from(editorialAccounts)
.orderBy(desc(editorialAccounts.createdAt)),
// Nicht abgerechnete Nutzungen je Redaktion — dieselben Status, die auch
// in der Abrechnung wählbar sind (voided zählt nicht).
db
.select({
editorialId: usages.editorialAccountId,
openCents: sql<number>`COALESCE(SUM(${usages.priceCents}), 0)::int`,
})
.from(usages)
.where(inArray(usages.billingStatus, ["open", "reopened"]))
.groupBy(usages.editorialAccountId),
]);
const openCentsByEditorial = new Map(
openUsageTotals.map((total) => [total.editorialId, total.openCents]),
);
const activeEditorials = editorials.filter((e) => !e.archivedAt);
const archivedEditorials = editorials.filter((e) => e.archivedAt);
@@ -67,6 +83,12 @@ export default async function EditorialsPage() {
{editorial.currentPriceCents === null
? "Preis noch nicht festgelegt"
: `Netto ${formatCents(editorial.currentPriceCents)}`}
<span
className={`ml-2 ${(openCentsByEditorial.get(editorial.id) ?? 0) > 0 ? "text-amber-600 dark:text-amber-400" : "text-muted-foreground"}`}
title="Summe der noch nicht abgerechneten Bildnutzungen"
>
· Offen {formatCents(openCentsByEditorial.get(editorial.id) ?? 0)}
</span>
</p>
</div>
</CardContent>
@@ -111,4 +133,4 @@ export default async function EditorialsPage() {
function formatCents(cents: number): string {
return new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(cents / 100);
}
}