feat(admin): filter dashboard revenue KPI by current month and completed status
This commit is contained in:
@@ -14,12 +14,22 @@ async function getKpis() {
|
||||
{ count: userCount },
|
||||
{ count: productCount },
|
||||
] = 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 }),
|
||||
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
|
||||
|
||||
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">
|
||||
<Card className="glass-dark border-white/5">
|
||||
<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" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold text-white">
|
||||
{new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(totalRevenue)}
|
||||
</div>
|
||||
<p className="text-xs text-slate-400">Aus allen Bestellungen</p>
|
||||
<p className="text-xs text-slate-400">Nur fertige Bestellungen</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user