sanamare/src/app/admin/(panel)/sales/page.tsx
Claude 21aa679504 Sales: Preisnachlass auf Produkte, Kategorien oder den ganzen Shop
- Neues Modell Sale (Prozent oder fester Betrag, Ziel, Start/Ende in deutscher Zeit, an/aus)
- Admin unter Verkauf → Sales: Liste mit Status (läuft, geplant, beendet, aus), anlegen, bearbeiten, beenden, löschen; Beispielrechnung im Formular
- Shop: Sale-Preis mit Streichpreis in Listen, Suche, Startseite, Produktseite (je Variante, mit „gilt bis“), Warenkorb; Kasse und Bestellung rechnen mit dem Sale-Preis
- Mehrere Sales: der günstigste zählt, keine Addition; Gutscheine weiterhin zusätzlich
- Produktseite im Admin zeigt, wenn ein Sale läuft

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SvuGvHpbAQ4syfuLuRvutL
2026-10-19 22:02:40 +02:00

58 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Link from "next/link";
import { db } from "@/lib/db";
import { requireAdmin } from "@/lib/auth";
import { dateFmt } from "@/lib/status";
import { formatPrice } from "@/lib/money";
import { SALE_STATES as STATE, SCOPE_LABELS, saleState, type SaleScope, type SaleState } from "@/lib/sales";
import { ConfirmButton } from "@/components/admin/AdminForm";
import { Card, Empty, PageHead, Pill } from "@/components/admin/ui";
import { toggleSale } from "./actions";
export default async function SalesPage() {
await requireAdmin("coupons.edit");
const [sales, categories] = await Promise.all([db.sale.findMany({ orderBy: { createdAt: "desc" } }), db.category.findMany({ select: { id: true, name: true } })]);
const catName = new Map(categories.map((c) => [c.id, c.name]));
const order: SaleState[] = ["running", "planned", "off", "ended"];
const sorted = [...sales].sort((a, b) => order.indexOf(saleState(a)) - order.indexOf(saleState(b)));
return (
<div className="ad-wrap">
<PageHead title="Sales" lead="Preisnachlässe auf einzelne Produkte, ganze Kategorien oder den ganzen Shop. Sie gelten automatisch, Kunden sehen den alten Preis durchgestrichen.">
<Link className="sm-btn sm-btn--primary" href="/admin/sales/neu">Neuer Sale</Link>
</PageHead>
<Card flush>
{sales.length === 0 ? (
<Empty title="Noch keine Sales" text="Lege z. B. 20 % auf eine Kategorie an, mit Start und Ende.">
<Link className="sm-btn sm-btn--secondary" href="/admin/sales/neu">Ersten Sale anlegen</Link>
</Empty>
) : (
<div className="ad-table-wrap">
<table className="ad-table">
<thead><tr><th>Sale</th><th>Rabatt</th><th>Gilt für</th><th>Zeitraum</th><th>Status</th><th /></tr></thead>
<tbody>
{sorted.map((s) => {
const st = saleState(s);
const target = s.scope === "categories" ? s.categoryIds.map((id) => catName.get(id) ?? "?").join(", ") : s.scope === "products" ? `${s.productIds.length} Produkt${s.productIds.length === 1 ? "" : "e"}` : SCOPE_LABELS.shop;
return (
<tr key={s.id}>
<td><Link className="ad-rowlink" href={`/admin/sales/${s.id}`}>{s.name}</Link></td>
<td style={{ whiteSpace: "nowrap" }}>{s.type === "percent" ? `−${s.value} %` : `−${formatPrice(s.value)}`}</td>
<td className="body-sm">{SCOPE_LABELS[s.scope as SaleScope] && s.scope !== "shop" ? `${SCOPE_LABELS[s.scope as SaleScope]}: ` : ""}{target}</td>
<td className="body-sm" style={{ whiteSpace: "nowrap" }}>{s.startsAt ? dateFmt.format(s.startsAt) : "sofort"} – {s.endsAt ? dateFmt.format(s.endsAt) : "offen"}</td>
<td><Pill tone={STATE[st].tone}>{STATE[st].label}</Pill></td>
<td className="num">
{st !== "ended" && (
<ConfirmButton action={toggleSale} hidden={{ id: s.id }} confirm={s.active ? "Sale beenden?" : "Sale starten?"} className="sm-btn sm-btn--ghost sm-btn--sm">{s.active ? "Beenden" : "Starten"}</ConfirmButton>
)}
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
</Card>
<p className="ad-help" style={{ marginTop: "var(--space-4)" }}>Gelten mehrere Sales für ein Produkt, zählt der günstigste. Gutscheine lassen sich zusätzlich einlösen.</p>
</div>
);
}