From cc87b93ae4ce39db64a021880d2b15146d348b23 Mon Sep 17 00:00:00 2001 From: Dicken Date: Wed, 17 Jun 2026 22:11:40 +0200 Subject: [PATCH] =?UTF-8?q?debug:=20debug3=20monthlyData+cumulativeData=20?= =?UTF-8?q?direkt=20pr=C3=BCfen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/tools/statistik/routes.js | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/backend/src/tools/statistik/routes.js b/backend/src/tools/statistik/routes.js index 26f5ab9..7c510d5 100644 --- a/backend/src/tools/statistik/routes.js +++ b/backend/src/tools/statistik/routes.js @@ -236,6 +236,51 @@ 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;