import { eq, desc, and, sql, inArray } from "drizzle-orm"; import { db } from "@/db/client"; import { billingReports, usages, editorialAccounts } from "@/db/schema"; import Link from "next/link"; import { formatBerlinDateTime } from "@/modules/downloads/format"; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"; import { FileText } from "lucide-react"; import { requireEditorial } from "../access"; export default async function AbrechnungenPage() { const principal = await requireEditorial(); // 1. Hole alle PDF-Abrechnungen (abgeschlossene Reports) const reports = await db .select({ id: billingReports.id, invoiceNumber: billingReports.invoiceNumber, periodStartsAt: billingReports.periodStartsAt, periodEndsAt: billingReports.periodEndsAt, netTotalCents: billingReports.netTotalCents, finalizedAt: billingReports.finalizedAt, status: billingReports.status, }) .from(billingReports) .where( and( eq(billingReports.editorialAccountId, principal.editorialId), eq(billingReports.status, "final") ) ) .orderBy(desc(billingReports.finalizedAt)); // 2. Berechne den noch offenen Betrag des aktuellen Monats (oder insgesamt offen) const [openUsages] = await db .select({ totalCents: sql`COALESCE(SUM(${usages.priceCents}), 0)::int`, count: sql`COUNT(${usages.id})::int`, }) .from(usages) .where( and( eq(usages.editorialAccountId, principal.editorialId), eq(usages.billingStatus, "open") ) ); const [editorial] = await db .select({ flatrateActive: editorialAccounts.flatrateActive, flatratePriceCents: editorialAccounts.flatratePriceCents, flatrateAssetLimit: editorialAccounts.flatrateAssetLimit, }) .from(editorialAccounts) .where(eq(editorialAccounts.id, principal.editorialId)) .limit(1); // Zähle die bereits bezogenen Flatrate-Bilder (Preis = 0) im aktuellen Kalendermonat let usedFlatrateImages = 0; if (editorial?.flatrateActive && editorial.flatrateAssetLimit !== null) { const now = new Date(); const startOfMonth = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)); const endOfMonth = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 0, 23, 59, 59, 999)); const [{ count }] = await db .select({ count: sql`cast(count(*) as integer)` }) .from(usages) .where( and( eq(usages.editorialAccountId, principal.editorialId), sql`${usages.createdAt} >= ${startOfMonth}`, sql`${usages.createdAt} <= ${endOfMonth}`, eq(usages.priceCents, 0), inArray(usages.billingStatus, ["open", "billed", "reopened"]) ) ); usedFlatrateImages = count; } // Berechne gesamten offenen Betrag inkl. Flatrate (falls noch nicht abgerechnet) // Normalerweise würde die Flatrate beim Report-Generieren hinzugefügt. Hier für die Vorschau: // (Optional: Wenn man hier die Flatrate als "offen" anzeigen will) const openTotalCents = (openUsages?.totalCents ?? 0); return (

Finanzen

Meine Abrechnungen

Offener Betrag (Einzelabrufe)

{new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(openTotalCents / 100)}

Für {openUsages?.count ?? 0} kostenpflichtige Bilder (noch nicht in Rechnung gestellt)

{editorial?.flatrateActive && editorial.flatratePriceCents !== null && ( Aktiver Tarif: Flatrate

{new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(editorial.flatratePriceCents / 100)} / Monat

{editorial.flatrateAssetLimit === null ? (

Unbegrenzte Downloads inklusive

) : (
Inklusiv-Bilder diesen Monat {usedFlatrateImages} / {editorial.flatrateAssetLimit}
)} )}

Bisherige Rechnungen

{reports.length === 0 ? (

Es liegen noch keine abgerechneten Zeiträume vor.

) : (
{reports.map((report, i) => ( ))}
Rechnungsnummer Zeitraum Betrag (Netto) Download
{report.invoiceNumber} {new Intl.DateTimeFormat("de-DE", { month: "long", year: "numeric" }).format(new Date(report.periodStartsAt))} {new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(report.netTotalCents / 100)} PDF
)}
); }