fix: Statistik nutzt bezahlt_am statt created_at für Einnahmen-Zeitraum

This commit is contained in:
2026-06-17 13:45:21 +02:00
parent b6840078e2
commit 23f798e1d1

View File

@@ -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