From 4ba72ad3a892607587a8b5ba30185288e57de4a0 Mon Sep 17 00:00:00 2001 From: Dicken Date: Tue, 26 May 2026 00:37:32 +0200 Subject: [PATCH] Dateien nach "backend/src/tools/bestellungen" hochladen --- backend/src/tools/bestellungen/routes.js | 62 ++++++++++++++++-------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/backend/src/tools/bestellungen/routes.js b/backend/src/tools/bestellungen/routes.js index 2dbcc8f..895dfd6 100644 --- a/backend/src/tools/bestellungen/routes.js +++ b/backend/src/tools/bestellungen/routes.js @@ -11,13 +11,14 @@ const loadOrder = (id, userId) => { return order; }; -// GET / – alle Bestellungen mit Zähler + Summen +// GET / – Liste mit Zählern router.get('/', authenticate, (req, res) => { const orders = db.prepare(` SELECT o.*, - COUNT(oi.id) AS item_count, - SUM(CASE WHEN oi.status='fertig' THEN 1 ELSE 0 END) AS items_done, - SUM(COALESCE(oi.custom_price,0) * oi.stueckzahl) AS custom_price_sum + COUNT(oi.id) AS item_count, + SUM(COALESCE(oi.qty_fertig,0)) AS qty_fertig_total, + SUM(COALESCE(oi.stueckzahl,0)) AS qty_total, + SUM(COALESCE(oi.custom_price,0) * COALESCE(oi.stueckzahl,0)) AS custom_price_sum FROM orders o LEFT JOIN order_items oi ON oi.order_id = o.id WHERE o.user_id = ? @@ -27,14 +28,12 @@ router.get('/', authenticate, (req, res) => { res.json(orders); }); -// GET /:id router.get('/:id', authenticate, (req, res) => { const order = loadOrder(req.params.id, uid(req)); if (!order) return res.status(404).json({ error: 'Nicht gefunden' }); res.json(order); }); -// POST / – neue Bestellung router.post('/', authenticate, (req, res) => { const { name, bemerkung='', status='warteliste', custom_price=null } = req.body; if (!name?.trim()) return res.status(400).json({ error: 'Name erforderlich' }); @@ -43,7 +42,6 @@ router.post('/', authenticate, (req, res) => { res.json(loadOrder(r.lastInsertRowid, uid(req))); }); -// PUT /:id router.put('/:id', authenticate, (req, res) => { const ex = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req)); if (!ex) return res.status(404).json({ error: 'Nicht gefunden' }); @@ -53,46 +51,68 @@ router.put('/:id', authenticate, (req, res) => { res.json(loadOrder(req.params.id, uid(req))); }); -// DELETE /:id router.delete('/:id', authenticate, (req, res) => { const r = db.prepare('DELETE FROM orders WHERE id=? AND user_id=?').run(req.params.id, uid(req)); if (!r.changes) return res.status(404).json({ error: 'Nicht gefunden' }); res.json({ success: true }); }); -// POST /:id/items router.post('/:id/items', authenticate, (req, res) => { const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req)); if (!order) return res.status(404).json({ error: 'Nicht gefunden' }); - const { calculation_id, stueckzahl=1, custom_price=null, status='warteliste' } = req.body; + const { calculation_id, stueckzahl=1, custom_price=null } = req.body; const calc = db.prepare('SELECT * FROM calculations WHERE id=? AND user_id=?').get(calculation_id, uid(req)); if (!calc) return res.status(404).json({ error: 'Archiv-Eintrag nicht gefunden' }); const existing = db.prepare('SELECT * FROM order_items WHERE order_id=? AND calculation_id=?').get(req.params.id, calculation_id); if (existing) { - db.prepare('UPDATE order_items SET stueckzahl=? WHERE id=?').run(existing.stueckzahl + stueckzahl, existing.id); + const newQty = existing.stueckzahl + stueckzahl; + db.prepare('UPDATE order_items SET stueckzahl=?,qty_warteliste=qty_warteliste+? WHERE id=?').run(newQty, stueckzahl, existing.id); } else { - db.prepare(`INSERT INTO order_items (order_id,calculation_id,calc_name,preis_freundschaft,preis_normal,preis_auftrag,stueckzahl,custom_price,status) - VALUES (?,?,?,?,?,?,?,?,?)`) - .run(req.params.id, calc.id, calc.name, calc.preis_freundschaft, calc.preis_normal, calc.preis_auftrag, stueckzahl, custom_price, status); + db.prepare(`INSERT INTO order_items + (order_id,calculation_id,calc_name,preis_freundschaft,preis_normal,preis_auftrag, + stueckzahl,custom_price,status,qty_warteliste,qty_in_arbeit,qty_fertig) + VALUES (?,?,?,?,?,?,?,?,'warteliste',?,0,0)`) + .run(req.params.id, calc.id, calc.name, calc.preis_freundschaft, calc.preis_normal, calc.preis_auftrag, + stueckzahl, custom_price, stueckzahl); } res.json(loadOrder(req.params.id, uid(req))); }); -// PUT /:id/items/:itemId – Stückzahl, Preis und Status router.put('/:id/items/:itemId', authenticate, (req, res) => { const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req)); if (!order) return res.status(404).json({ error: 'Nicht gefunden' }); const item = db.prepare('SELECT * FROM order_items WHERE id=? AND order_id=?').get(req.params.itemId, req.params.id); if (!item) return res.status(404).json({ error: 'Position nicht gefunden' }); - const stueckzahl = req.body.stueckzahl ?? item.stueckzahl; - const custom_price = req.body.custom_price !== undefined ? req.body.custom_price : item.custom_price; - const status = req.body.status ?? item.status; - db.prepare('UPDATE order_items SET stueckzahl=?,custom_price=?,status=? WHERE id=?') - .run(stueckzahl, custom_price, status, req.params.itemId); + + // Stückzahl geändert → Warteliste anpassen + if (req.body.stueckzahl !== undefined) { + const newQty = Math.max(1, req.body.stueckzahl); + const diff = newQty - item.stueckzahl; + const newWarte = Math.max(0, item.qty_warteliste + diff); + db.prepare('UPDATE order_items SET stueckzahl=?,qty_warteliste=? WHERE id=?').run(newQty, newWarte, item.id); + } + + // Status-Zähler direkt setzen + if (req.body.qty_warteliste !== undefined || req.body.qty_in_arbeit !== undefined || req.body.qty_fertig !== undefined) { + const fresh = db.prepare('SELECT * FROM order_items WHERE id=?').get(item.id); + const w = req.body.qty_warteliste ?? fresh.qty_warteliste; + const a = req.body.qty_in_arbeit ?? fresh.qty_in_arbeit; + const f = req.body.qty_fertig ?? fresh.qty_fertig; + // Summe darf stueckzahl nicht überschreiten + const total = w + a + f; + if (total <= fresh.stueckzahl) { + db.prepare('UPDATE order_items SET qty_warteliste=?,qty_in_arbeit=?,qty_fertig=? WHERE id=?').run(w, a, f, item.id); + } + } + + // Festpreis + if (req.body.custom_price !== undefined) { + db.prepare('UPDATE order_items SET custom_price=? WHERE id=?').run(req.body.custom_price, item.id); + } + res.json(loadOrder(req.params.id, uid(req))); }); -// DELETE /:id/items/:itemId router.delete('/:id/items/:itemId', authenticate, (req, res) => { const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req)); if (!order) return res.status(404).json({ error: 'Nicht gefunden' });