feat(admin): filter dashboard revenue KPI by current month and completed status

This commit is contained in:
DanielS
2026-06-30 15:23:28 +02:00
parent e25344a185
commit bdc9e6541c

View File

@@ -14,12 +14,22 @@ async function getKpis() {
{ count: userCount }, { count: userCount },
{ count: productCount }, { count: productCount },
] = await Promise.all([ ] = await Promise.all([
adminDb.from('orders').select('total_price, status'), adminDb.from('orders').select('total_price, status, created_at'),
adminDb.from('profiles').select('*', { count: 'exact', head: true }), adminDb.from('profiles').select('*', { count: 'exact', head: true }),
supabase.from('products').select('*', { count: 'exact', head: true }), supabase.from('products').select('*', { count: 'exact', head: true }),
]) ])
const totalRevenue = (orders ?? []).reduce((sum, o) => sum + Number(o.total_price), 0) const now = new Date()
const currentYear = now.getFullYear()
const currentMonth = now.getMonth()
const completedOrdersThisMonth = (orders ?? []).filter(o => {
if (o.status !== 'completed') return false
const orderDate = new Date(o.created_at)
return orderDate.getFullYear() === currentYear && orderDate.getMonth() === currentMonth
})
const totalRevenue = completedOrdersThisMonth.reduce((sum, o) => sum + Number(o.total_price), 0)
const orderCount = (orders ?? []).length const orderCount = (orders ?? []).length
return { totalRevenue, orderCount, userCount: userCount ?? 0, productCount: productCount ?? 0 } return { totalRevenue, orderCount, userCount: userCount ?? 0, productCount: productCount ?? 0 }
@@ -36,14 +46,14 @@ export default async function AdminDashboard() {
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4"> <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
<Card className="glass-dark border-white/5"> <Card className="glass-dark border-white/5">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium text-slate-300">Umsatz gesamt</CardTitle> <CardTitle className="text-sm font-medium text-slate-300">Umsatz aktueller Monat</CardTitle>
<TrendingUp className="h-4 w-4 text-primary" /> <TrendingUp className="h-4 w-4 text-primary" />
</CardHeader> </CardHeader>
<CardContent> <CardContent>
<div className="text-2xl font-bold text-white"> <div className="text-2xl font-bold text-white">
{new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(totalRevenue)} {new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(totalRevenue)}
</div> </div>
<p className="text-xs text-slate-400">Aus allen Bestellungen</p> <p className="text-xs text-slate-400">Nur fertige Bestellungen</p>
</CardContent> </CardContent>
</Card> </Card>