Dateien nach "backend/src/routes" hochladen
This commit is contained in:
@@ -68,3 +68,46 @@ router.put('/note', authenticate, (req, res) => {
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
// ── Statistiken ───────────────────────────────────────────────────────────────
|
||||
router.get('/stats', authenticate, (req, res) => {
|
||||
const uid = req.user.id;
|
||||
|
||||
const orders = db.prepare(`
|
||||
SELECT status, bezahlt,
|
||||
SUM(COALESCE(custom_price_sum_view,0)) AS revenue
|
||||
FROM (
|
||||
SELECT o.status, o.bezahlt,
|
||||
SUM(COALESCE(oi.custom_price,0)*COALESCE(oi.stueckzahl,0)) AS custom_price_sum_view
|
||||
FROM orders o
|
||||
LEFT JOIN order_items oi ON oi.order_id=o.id
|
||||
WHERE o.user_id=?
|
||||
GROUP BY o.id
|
||||
)
|
||||
GROUP BY status, bezahlt
|
||||
`).all(uid);
|
||||
|
||||
const totalOrders = db.prepare('SELECT COUNT(*) c FROM orders WHERE user_id=?').get(uid).c;
|
||||
const bezahltOrders = db.prepare('SELECT COUNT(*) c FROM orders WHERE user_id=? AND bezahlt=1').get(uid).c;
|
||||
const totalRevenue = db.prepare(`
|
||||
SELECT COALESCE(SUM(oi.custom_price*oi.stueckzahl),0) s
|
||||
FROM orders o JOIN order_items oi ON oi.order_id=o.id
|
||||
WHERE o.user_id=? AND o.bezahlt=1 AND oi.custom_price IS NOT NULL
|
||||
`).get(uid).s;
|
||||
const offeneRevenue = db.prepare(`
|
||||
SELECT COALESCE(SUM(oi.custom_price*oi.stueckzahl),0) s
|
||||
FROM orders o JOIN order_items oi ON oi.order_id=o.id
|
||||
WHERE o.user_id=? AND o.bezahlt=0 AND oi.custom_price IS NOT NULL AND o.status!='warteliste'
|
||||
`).get(uid).s;
|
||||
const thisMonth = new Date();
|
||||
thisMonth.setDate(1); thisMonth.setHours(0,0,0,0);
|
||||
const ordersThisMonth = db.prepare('SELECT COUNT(*) c FROM orders WHERE user_id=? AND created_at>=?').get(uid, thisMonth.toISOString()).c;
|
||||
const totalCalcs = db.prepare('SELECT COUNT(*) c FROM calculations WHERE user_id=?').get(uid).c;
|
||||
|
||||
const byStatus = { warteliste:0, in_arbeit:0, fertig:0 };
|
||||
for (const r of db.prepare('SELECT status, COUNT(*) c FROM orders WHERE user_id=? AND bezahlt=0 GROUP BY status').all(uid)) {
|
||||
byStatus[r.status] = r.c;
|
||||
}
|
||||
|
||||
res.json({ totalOrders, bezahltOrders, totalRevenue, offeneRevenue, ordersThisMonth, totalCalcs, byStatus });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user