Dateien nach "backend/src/tools/bestellungen" hochladen
This commit is contained in:
@@ -11,13 +11,14 @@ const loadOrder = (id, userId) => {
|
|||||||
return order;
|
return order;
|
||||||
};
|
};
|
||||||
|
|
||||||
// GET / – alle Bestellungen mit Zähler + Summen
|
// GET / – Liste mit Zählern
|
||||||
router.get('/', authenticate, (req, res) => {
|
router.get('/', authenticate, (req, res) => {
|
||||||
const orders = db.prepare(`
|
const orders = db.prepare(`
|
||||||
SELECT o.*,
|
SELECT o.*,
|
||||||
COUNT(oi.id) AS item_count,
|
COUNT(oi.id) AS item_count,
|
||||||
SUM(CASE WHEN oi.status='fertig' THEN 1 ELSE 0 END) AS items_done,
|
SUM(COALESCE(oi.qty_fertig,0)) AS qty_fertig_total,
|
||||||
SUM(COALESCE(oi.custom_price,0) * oi.stueckzahl) AS custom_price_sum
|
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
|
FROM orders o
|
||||||
LEFT JOIN order_items oi ON oi.order_id = o.id
|
LEFT JOIN order_items oi ON oi.order_id = o.id
|
||||||
WHERE o.user_id = ?
|
WHERE o.user_id = ?
|
||||||
@@ -27,14 +28,12 @@ router.get('/', authenticate, (req, res) => {
|
|||||||
res.json(orders);
|
res.json(orders);
|
||||||
});
|
});
|
||||||
|
|
||||||
// GET /:id
|
|
||||||
router.get('/:id', authenticate, (req, res) => {
|
router.get('/:id', authenticate, (req, res) => {
|
||||||
const order = loadOrder(req.params.id, uid(req));
|
const order = loadOrder(req.params.id, uid(req));
|
||||||
if (!order) return res.status(404).json({ error: 'Nicht gefunden' });
|
if (!order) return res.status(404).json({ error: 'Nicht gefunden' });
|
||||||
res.json(order);
|
res.json(order);
|
||||||
});
|
});
|
||||||
|
|
||||||
// POST / – neue Bestellung
|
|
||||||
router.post('/', authenticate, (req, res) => {
|
router.post('/', authenticate, (req, res) => {
|
||||||
const { name, bemerkung='', status='warteliste', custom_price=null } = req.body;
|
const { name, bemerkung='', status='warteliste', custom_price=null } = req.body;
|
||||||
if (!name?.trim()) return res.status(400).json({ error: 'Name erforderlich' });
|
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)));
|
res.json(loadOrder(r.lastInsertRowid, uid(req)));
|
||||||
});
|
});
|
||||||
|
|
||||||
// PUT /:id
|
|
||||||
router.put('/:id', authenticate, (req, res) => {
|
router.put('/:id', authenticate, (req, res) => {
|
||||||
const ex = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req));
|
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' });
|
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)));
|
res.json(loadOrder(req.params.id, uid(req)));
|
||||||
});
|
});
|
||||||
|
|
||||||
// DELETE /:id
|
|
||||||
router.delete('/:id', authenticate, (req, res) => {
|
router.delete('/:id', authenticate, (req, res) => {
|
||||||
const r = db.prepare('DELETE FROM orders WHERE id=? AND user_id=?').run(req.params.id, uid(req));
|
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' });
|
if (!r.changes) return res.status(404).json({ error: 'Nicht gefunden' });
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
// POST /:id/items
|
|
||||||
router.post('/:id/items', authenticate, (req, res) => {
|
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));
|
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' });
|
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));
|
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' });
|
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);
|
const existing = db.prepare('SELECT * FROM order_items WHERE order_id=? AND calculation_id=?').get(req.params.id, calculation_id);
|
||||||
if (existing) {
|
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 {
|
} else {
|
||||||
db.prepare(`INSERT INTO order_items (order_id,calculation_id,calc_name,preis_freundschaft,preis_normal,preis_auftrag,stueckzahl,custom_price,status)
|
db.prepare(`INSERT INTO order_items
|
||||||
VALUES (?,?,?,?,?,?,?,?,?)`)
|
(order_id,calculation_id,calc_name,preis_freundschaft,preis_normal,preis_auftrag,
|
||||||
.run(req.params.id, calc.id, calc.name, calc.preis_freundschaft, calc.preis_normal, calc.preis_auftrag, stueckzahl, custom_price, status);
|
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)));
|
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) => {
|
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));
|
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' });
|
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);
|
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' });
|
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;
|
// Stückzahl geändert → Warteliste anpassen
|
||||||
const status = req.body.status ?? item.status;
|
if (req.body.stueckzahl !== undefined) {
|
||||||
db.prepare('UPDATE order_items SET stueckzahl=?,custom_price=?,status=? WHERE id=?')
|
const newQty = Math.max(1, req.body.stueckzahl);
|
||||||
.run(stueckzahl, custom_price, status, req.params.itemId);
|
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)));
|
res.json(loadOrder(req.params.id, uid(req)));
|
||||||
});
|
});
|
||||||
|
|
||||||
// DELETE /:id/items/:itemId
|
|
||||||
router.delete('/:id/items/:itemId', authenticate, (req, res) => {
|
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));
|
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' });
|
if (!order) return res.status(404).json({ error: 'Nicht gefunden' });
|
||||||
|
|||||||
Reference in New Issue
Block a user