From 3f785d3d9e80a56011cda7d3411594b435898365 Mon Sep 17 00:00:00 2001 From: Dicken Date: Tue, 26 May 2026 13:33:38 +0200 Subject: [PATCH] Dateien nach "backend/src/routes" hochladen --- backend/src/routes/dashboard.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/backend/src/routes/dashboard.js b/backend/src/routes/dashboard.js index c602eff..e5b2083 100644 --- a/backend/src/routes/dashboard.js +++ b/backend/src/routes/dashboard.js @@ -89,16 +89,28 @@ router.get('/stats', authenticate, (req, res) => { const totalOrders = db.prepare('SELECT COUNT(*) c FROM orders WHERE user_id=?').get(uid).c; const bezahltOrders = db.prepare('SELECT COUNT(*) c FROM orders WHERE user_id=? AND bezahlt=1').get(uid).c; - const totalRevenue = db.prepare(` - SELECT COALESCE(SUM(oi.custom_price*oi.stueckzahl),0) s - FROM orders o JOIN order_items oi ON oi.order_id=o.id - WHERE o.user_id=? AND o.bezahlt=1 AND oi.custom_price IS NOT NULL - `).get(uid).s; - const offeneRevenue = db.prepare(` - SELECT COALESCE(SUM(oi.custom_price*oi.stueckzahl),0) s - FROM orders o JOIN order_items oi ON oi.order_id=o.id - WHERE o.user_id=? AND o.bezahlt=0 AND oi.custom_price IS NOT NULL AND o.status!='warteliste' - `).get(uid).s; + // Revenue: Gesamt-Festpreis hat Priorität, sonst Summe der Positions-Festpreise + const paidOrders = db.prepare('SELECT * FROM orders WHERE user_id=? AND bezahlt=1').all(uid); + let totalRevenue = 0; + for (const o of paidOrders) { + if (o.custom_price != null) { + totalRevenue += parseFloat(o.custom_price); + } else { + const sum = db.prepare('SELECT COALESCE(SUM(custom_price*stueckzahl),0) s FROM order_items WHERE order_id=? AND custom_price IS NOT NULL').get(o.id).s; + totalRevenue += sum; + } + } + + const openOrders = db.prepare("SELECT * FROM orders WHERE user_id=? AND bezahlt=0 AND status!='warteliste'").all(uid); + let offeneRevenue = 0; + for (const o of openOrders) { + if (o.custom_price != null) { + offeneRevenue += parseFloat(o.custom_price); + } else { + const sum = db.prepare('SELECT COALESCE(SUM(custom_price*stueckzahl),0) s FROM order_items WHERE order_id=? AND custom_price IS NOT NULL').get(o.id).s; + offeneRevenue += sum; + } + } const thisMonth = new Date(); thisMonth.setDate(1); thisMonth.setHours(0,0,0,0); const ordersThisMonth = db.prepare('SELECT COUNT(*) c FROM orders WHERE user_id=? AND created_at>=?').get(uid, thisMonth.toISOString()).c;