diff --git a/backend/src/tools/statistik/routes.js b/backend/src/tools/statistik/routes.js index 7c510d5..0ad8e80 100644 --- a/backend/src/tools/statistik/routes.js +++ b/backend/src/tools/statistik/routes.js @@ -126,7 +126,8 @@ router.get('/overview', authenticate, (req, res) => { if (from && to) { const days = (new Date(to) - new Date(from)) / 86400000; if (days <= 14) granularity = 'day'; - else if (days <= 92) granularity = 'week'; + else if (days <= 31) granularity = 'week'; + // > 31 Tage → month (Monatsauswahl, Jahresauswahl, Gesamt) } function getBucketKey(dateStr) { @@ -236,79 +237,6 @@ router.get('/month-orders', authenticate, (req, res) => { }))); }); -// Debug3: monthlyData direkt ausgeben -router.get('/debug3', authenticate, (req, res) => { - const { from, to } = req.query; - const u = uid(req); - const allOrders = db.prepare('SELECT * FROM orders WHERE user_id=? ORDER BY created_at DESC').all(u); - - const inRange = (dateStr) => { - if (!dateStr) return !from && !to; - const d = dateStr.slice(0, 10); - if (from && d < from) return false; - if (to && d > to) return false; - return true; - }; - - const filteredOrders = allOrders.filter(o => { - if (!from && !to) return true; - if (o.bezahlt) return inRange(o.bezahlt_am || o.created_at); - return inRange(o.created_at); - }); - - const buckets = {}; - for (const o of filteredOrders) { - const dateStr = o.bezahlt ? (o.bezahlt_am || o.created_at) : o.created_at; - const key = dateStr?.slice(0,7); - if (!key) continue; - if (!buckets[key]) buckets[key] = { month:key, revenue:0, base_cost:0, expenses:0 }; - if (o.bezahlt) { - const rev = getOrderRevenue(o); - const cost = getOrderBaseCost(o.id); - buckets[key].revenue += rev; - buckets[key].base_cost += cost; - } - } - - const monthlyData = Object.values(buckets).sort((a,b)=>a.month.localeCompare(b.month)); - let cum = 0; - const cumulativeData = monthlyData.map(m => { - cum += m.revenue - m.base_cost - m.expenses; - return { month:m.month, value:round2(cum), revenue:m.revenue, base_cost:m.base_cost }; - }); - - res.json({ monthlyData, cumulativeData }); -}); - -// Debug2: monthlyData mit base_cost prüfen -router.get('/debug2', authenticate, (req, res) => { - const { from, to } = req.query; - const u = uid(req); - const allOrders = db.prepare('SELECT * FROM orders WHERE user_id=? AND bezahlt=1').all(u); - const result = allOrders.map(o => { - const rev = getOrderRevenue(o); - const cost = getOrderBaseCost(o.id); - return { id:o.id, name:o.name, bezahlt_am:o.bezahlt_am, revenue:rev, base_cost:cost, profit: rev-cost }; - }); - res.json({ orders: result, totals: { - revenue: result.reduce((s,o)=>s+o.revenue,0), - base_cost: result.reduce((s,o)=>s+o.base_cost,0), - profit: result.reduce((s,o)=>s+o.profit,0), - }}); -}); - -// Debug: rohe Auftragsdaten mit Revenue -router.get('/debug', authenticate, (req, res) => { - const u = uid(req); - const orders = db.prepare('SELECT id, name, status, bezahlt, bezahlt_am, created_at, custom_price FROM orders WHERE user_id=? ORDER BY created_at DESC LIMIT 20').all(u); - const result = orders.map(o => { - const items = db.prepare('SELECT custom_price, stueckzahl, preis_freundschaft FROM order_items WHERE order_id=?').all(o.id); - const itemRevenue = items.reduce((s,i) => s + (i.custom_price||0)*(i.stueckzahl||1), 0); - const baseCost = items.reduce((s,i) => s + (i.preis_freundschaft||0)*(i.stueckzahl||1), 0); - return { ...o, itemRevenue, baseCost, totalRevenue: o.custom_price || itemRevenue, items }; - }); - res.json(result); -}); module.exports = router;