Skip to content

Commit 4708e68

Browse files
JiroMusikclaude
andcommitted
Fix unit conversion: EL/TL/Prise/Bund support, proper kg↔g deduction
units.ts: - EL/Esslöffel → 15ml, TL/Teelöffel → 5ml, Prise → 0.5g, Bund → 1 Stück - Now "2 EL Olivenöl" correctly matches inventory "500 ml Olivenöl" /api/recipes/cook: - Full unit conversion before deduction (was comparing raw numbers) - 1kg recipe vs 1000g inventory now correctly deducts 1000g - Convert back to original unit after deduction for DB consistency Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 836c086 commit 4708e68

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

server.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,16 +1872,41 @@ app.post('/api/recipes/cook', (req, res) => {
18721872
db.prepare('UPDATE items SET quantity = ? WHERE id = ?').run(newQty, item.id);
18731873
item.quantity = newQty;
18741874
deducted.push({ ...item, quantity_deducted: pctDeduct });
1875-
} else if (item.quantity >= ing.amount) {
1876-
const newQty = item.quantity - ing.amount;
1877-
db.prepare('UPDATE items SET quantity = ? WHERE id = ?').run(newQty, item.id);
1878-
item.quantity = newQty; // update in-memory for subsequent matches
1879-
deducted.push({ ...item, quantity_deducted: ing.amount });
18801875
} else {
1881-
db.prepare('UPDATE items SET quantity = 0 WHERE id = ?').run(item.id);
1882-
deducted.push({ ...item, quantity_deducted: item.quantity });
1883-
missing.push(`${ing.name} (Fehlt: ${ing.amount - item.quantity} ${ing.unit || item.unit || ''})`.trim());
1884-
item.quantity = 0;
1876+
// Convert both to smallest unit for correct comparison (e.g. 1kg vs 1000g)
1877+
const reqSmall = convertToSmallestUnit(ing.amount, ing.unit || item.unit);
1878+
const invSmall = convertToSmallestUnit(item.quantity, item.unit);
1879+
1880+
if (reqSmall.unit === invSmall.unit) {
1881+
// Units compatible — deduct with conversion
1882+
if (invSmall.amount >= reqSmall.amount) {
1883+
const remaining = invSmall.amount - reqSmall.amount;
1884+
// Convert back to original unit
1885+
const newQty = (remaining / invSmall.amount) * item.quantity;
1886+
db.prepare('UPDATE items SET quantity = ? WHERE id = ?').run(newQty, item.id);
1887+
item.quantity = newQty;
1888+
deducted.push({ ...item, quantity_deducted: ing.amount });
1889+
} else {
1890+
db.prepare('UPDATE items SET quantity = 0 WHERE id = ?').run(item.id);
1891+
deducted.push({ ...item, quantity_deducted: item.quantity });
1892+
const missingSmall = reqSmall.amount - invSmall.amount;
1893+
missing.push(`${ing.name} (Fehlt: ${Math.round(missingSmall * 100) / 100} ${reqSmall.unit})`.trim());
1894+
item.quantity = 0;
1895+
}
1896+
} else {
1897+
// Units incompatible (shouldn't happen often) — try raw deduction as fallback
1898+
if (item.quantity >= ing.amount) {
1899+
const newQty = item.quantity - ing.amount;
1900+
db.prepare('UPDATE items SET quantity = ? WHERE id = ?').run(newQty, item.id);
1901+
item.quantity = newQty;
1902+
deducted.push({ ...item, quantity_deducted: ing.amount });
1903+
} else {
1904+
db.prepare('UPDATE items SET quantity = 0 WHERE id = ?').run(item.id);
1905+
deducted.push({ ...item, quantity_deducted: item.quantity });
1906+
missing.push(`${ing.name} (Fehlt: ${ing.amount - item.quantity} ${ing.unit || item.unit || ''})`.trim());
1907+
item.quantity = 0;
1908+
}
1909+
}
18851910
}
18861911
} else {
18871912
missing.push(ing.name);

server/utils/units.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export const convertToSmallestUnit = (amount: number, unit: string): { amount: n
1313
const norm = normalizeUnit(unit);
1414
if (norm === 'kg') return { amount: amount * 1000, unit: 'g' };
1515
if (norm === 'l') return { amount: amount * 1000, unit: 'ml' };
16+
// Cooking units → approximate g/ml conversion
17+
const u = unit.toLowerCase().trim();
18+
if (u === 'el' || u === 'esslöffel' || u === 'tbsp') return { amount: amount * 15, unit: 'ml' };
19+
if (u === 'tl' || u === 'teelöffel' || u === 'tsp') return { amount: amount * 5, unit: 'ml' };
20+
if (u === 'prise' || u === 'pinch') return { amount: amount * 0.5, unit: 'g' };
21+
if (u === 'bund' || u === 'bunch') return { amount: amount, unit: 'Stück' };
1622
return { amount, unit: norm };
1723
};
1824

0 commit comments

Comments
 (0)