feat: Add flatrate billing model for editorial accounts
This commit is contained in:
1 parent
699580a6a0
commit
0b1ffdfdfc
39 files changed
+4900
-57
No files matched your search
@@ -0,0 +1,167 @@
|
||||
import { eq, desc, and, sql, inArray } from "drizzle-orm";
|
||||
import { db } from "@/db/client";
|
||||
import { billingReports, usages, editorialAccounts } from "@/db/schema";
|
||||
import { requireActiveEditorial } from "@/modules/auth/principal";
|
||||
import { headers } from "next/headers";
|
||||
import Link from "next/link";
|
||||
import { formatBerlinDateTime } from "@/modules/downloads/format";
|
||||
|
||||
export default async function AbrechnungenPage() {
|
||||
const principal = await requireActiveEditorial(new Headers(await headers()));
|
||||
|
||||
// 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<number>`COALESCE(SUM(${usages.priceCents}), 0)::int`,
|
||||
count: sql<number>`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<number>`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 (
|
||||
<section className="editorial-section">
|
||||
<p className="editorial-kicker">Finanzen</p>
|
||||
<h1>Meine Abrechnungen</h1>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-8 my-8">
|
||||
<div className="bg-blue-50 p-6 rounded-xl border border-blue-100">
|
||||
<h2 className="text-xl font-semibold text-blue-900 mb-2">Offener Betrag (Einzelabrufe)</h2>
|
||||
<p className="text-3xl font-bold text-blue-900">
|
||||
{new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(openTotalCents / 100)}
|
||||
</p>
|
||||
<p className="text-sm text-blue-700 mt-2">
|
||||
Für {openUsages?.count ?? 0} kostenpflichtige Bilder (noch nicht in Rechnung gestellt)
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{editorial?.flatrateActive && editorial.flatratePriceCents !== null && (
|
||||
<div className="bg-green-50 p-6 rounded-xl border border-green-100">
|
||||
<h2 className="text-xl font-semibold text-green-900 mb-2">Aktiver Tarif: Flatrate</h2>
|
||||
<p className="text-3xl font-bold text-green-900">
|
||||
{new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(editorial.flatratePriceCents / 100)} <span className="text-lg font-normal">/ Monat</span>
|
||||
</p>
|
||||
{editorial.flatrateAssetLimit === null ? (
|
||||
<p className="text-sm text-green-700 mt-2">
|
||||
Unbegrenzte Downloads inklusive
|
||||
</p>
|
||||
) : (
|
||||
<div className="mt-2">
|
||||
<p className="text-sm text-green-700 font-semibold mb-1">
|
||||
Inklusiv-Bilder diesen Monat: {usedFlatrateImages} / {editorial.flatrateAssetLimit}
|
||||
</p>
|
||||
<div className="w-full bg-green-200 rounded-full h-2.5">
|
||||
<div className="bg-green-600 h-2.5 rounded-full" style={{ width: `${Math.min(100, (usedFlatrateImages / editorial.flatrateAssetLimit) * 100)}%` }}></div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<h2 className="text-2xl font-bold mb-4 mt-12">Bisherige Rechnungen</h2>
|
||||
|
||||
{reports.length === 0 ? (
|
||||
<p className="text-gray-500">Es liegen noch keine abgerechneten Zeiträume vor.</p>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-left border-collapse">
|
||||
<thead>
|
||||
<tr className="border-b-2 border-gray-200">
|
||||
<th className="py-3 px-4 font-semibold text-gray-700">Rechnungsnummer</th>
|
||||
<th className="py-3 px-4 font-semibold text-gray-700">Zeitraum</th>
|
||||
<th className="py-3 px-4 font-semibold text-gray-700">Betrag (Netto)</th>
|
||||
<th className="py-3 px-4 font-semibold text-gray-700">Download</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{reports.map((report) => (
|
||||
<tr key={report.id} className="border-b border-gray-100 hover:bg-gray-50">
|
||||
<td className="py-4 px-4 font-medium">{report.invoiceNumber}</td>
|
||||
<td className="py-4 px-4 text-gray-600">
|
||||
{new Intl.DateTimeFormat("de-DE", { month: "long", year: "numeric" }).format(report.periodStartsAt)}
|
||||
</td>
|
||||
<td className="py-4 px-4 font-medium">
|
||||
{new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(report.netTotalCents / 100)}
|
||||
</td>
|
||||
<td className="py-4 px-4">
|
||||
<Link
|
||||
href={`/api/editorials/reports/${report.id}/pdf`}
|
||||
target="_blank"
|
||||
className="inline-flex items-center gap-2 text-blue-600 hover:text-blue-800 transition-colors"
|
||||
>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={2} stroke="currentColor" className="w-5 h-5">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 0 0-3.375-3.375h-1.5A1.125 1.125 0 0 1 13.5 7.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 0 0-9-9Z" />
|
||||
</svg>
|
||||
PDF anzeigen
|
||||
</Link>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user