From 23f798e1d1d31de823512f25da5f637a7f3ea731 Mon Sep 17 00:00:00 2001 From: Dicken Date: Wed, 17 Jun 2026 13:45:21 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20Statistik=20nutzt=20bezahlt=5Fam=20statt?= =?UTF-8?q?=20created=5Fat=20f=C3=BCr=20Einnahmen-Zeitraum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/tools/statistik/routes.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/backend/src/tools/statistik/routes.js b/backend/src/tools/statistik/routes.js index e3c050c..3be54c1 100644 --- a/backend/src/tools/statistik/routes.js +++ b/backend/src/tools/statistik/routes.js @@ -36,6 +36,9 @@ router.get('/overview', authenticate, (req, res) => { const { from, to } = req.query; const u = uid(req); + // Bezahlte Aufträge: nach bezahlt_am filtern; offene: nach created_at + // Zeitraum-Filter: zeige Auftrag wenn er im Zeitraum bezahlt wurde (bezahlt) + // ODER noch offen ist und im Zeitraum erstellt wurde let oq = `SELECT o.*, COALESCE(o.custom_price, (SELECT SUM(oi.custom_price * oi.stueckzahl) FROM order_items oi WHERE oi.order_id=o.id AND oi.custom_price IS NOT NULL) @@ -44,9 +47,15 @@ router.get('/overview', authenticate, (req, res) => { FROM order_items oi LEFT JOIN calculations c ON c.id=oi.calculation_id WHERE oi.order_id=o.id) as base_cost FROM orders o WHERE o.user_id=?`; const op = [u]; - if (from) { oq += ' AND DATE(o.created_at)>=?'; op.push(from); } - if (to) { oq += ' AND DATE(o.created_at)<=?'; op.push(to); } - oq += ' ORDER BY o.created_at DESC'; + if (from && to) { + oq += ` AND ( + (o.bezahlt=1 AND DATE(o.bezahlt_am)>=? AND DATE(o.bezahlt_am)<=?) + OR + (o.bezahlt=0 AND DATE(o.created_at)>=? AND DATE(o.created_at)<=?) + )`; + op.push(from, to, from, to); + } + oq += ' ORDER BY COALESCE(o.bezahlt_am, o.created_at) DESC'; const orders = db.prepare(oq).all(...op); const paidOrders = orders.filter(o => !!o.bezahlt); @@ -79,7 +88,7 @@ router.get('/overview', authenticate, (req, res) => { // Monatliche Daten const monthlyRev = db.prepare(` - SELECT strftime('%Y-%m', created_at) as month, + SELECT strftime('%Y-%m', CASE WHEN bezahlt=1 THEN bezahlt_am ELSE created_at END) as month, SUM(CASE WHEN bezahlt=1 THEN COALESCE(custom_price,0) ELSE 0 END) as revenue, COUNT(*) as orders FROM orders WHERE user_id=? GROUP BY month ORDER BY month