fix: Statistik nutzt bezahlt_am statt created_at für Einnahmen-Zeitraum
This commit is contained in:
@@ -36,6 +36,9 @@ router.get('/overview', authenticate, (req, res) => {
|
|||||||
const { from, to } = req.query;
|
const { from, to } = req.query;
|
||||||
const u = uid(req);
|
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.*,
|
let oq = `SELECT o.*,
|
||||||
COALESCE(o.custom_price,
|
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)
|
(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 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=?`;
|
FROM orders o WHERE o.user_id=?`;
|
||||||
const op = [u];
|
const op = [u];
|
||||||
if (from) { oq += ' AND DATE(o.created_at)>=?'; op.push(from); }
|
if (from && to) {
|
||||||
if (to) { oq += ' AND DATE(o.created_at)<=?'; op.push(to); }
|
oq += ` AND (
|
||||||
oq += ' ORDER BY o.created_at DESC';
|
(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 orders = db.prepare(oq).all(...op);
|
||||||
|
|
||||||
const paidOrders = orders.filter(o => !!o.bezahlt);
|
const paidOrders = orders.filter(o => !!o.bezahlt);
|
||||||
@@ -79,7 +88,7 @@ router.get('/overview', authenticate, (req, res) => {
|
|||||||
|
|
||||||
// Monatliche Daten
|
// Monatliche Daten
|
||||||
const monthlyRev = db.prepare(`
|
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,
|
SUM(CASE WHEN bezahlt=1 THEN COALESCE(custom_price,0) ELSE 0 END) as revenue,
|
||||||
COUNT(*) as orders
|
COUNT(*) as orders
|
||||||
FROM orders WHERE user_id=? GROUP BY month ORDER BY month
|
FROM orders WHERE user_id=? GROUP BY month ORDER BY month
|
||||||
|
|||||||
Reference in New Issue
Block a user