// Kosten (Konzept, „Kosten, Preisgedächtnis und Kassenbons“) — rein und testbar. // // Kosten pro Portion = Σ (Menge pro Portion × Preis je Basiseinheit) über die // Zutaten, die nicht zum Grundvorrat gehören, plus 5 % Pauschale für den // Grundvorrat. Welcher Preis gilt: Bon (im Hauptladen vor anderen Läden), dann // eigener Preis, dann Richtpreis — jeweils der jüngste. Die Konfidenz ist der // Anteil der Zutaten mit Bonpreis (Punkte: 3 von 3 heißt sicher). export type Source = "receipt" | "manual" | "default"; export type PricePoint = { ingredientId: string; pricePerUnit: number; unit: "g" | "ml" | "piece"; source: Source; observedAt: string; // ISO mainStore: boolean; // aus dem Hauptladen (nur bei Bons bedeutsam) }; export type CostLine = { ingredientId: string; qtyPerPortion: number; unit: "g" | "ml" | "piece"; staple: boolean }; export type DishCost = { perPortion: number | null; // null: keine Zutat mit Preis priced: number; // Zutaten mit Preis (ohne Grundvorrat) total: number; // Zutaten ohne Grundvorrat fromReceipts: number; // davon mit Bonpreis confidence: 0 | 1 | 2 | 3; // Punkte }; export const STAPLE_SHARE = 0.05; const rank = (p: PricePoint) => (p.source === "receipt" ? (p.mainStore ? 4 : 3) : p.source === "manual" ? 2 : 1); /** Der geltende Preis je Zutat (Bon im Hauptladen > Bon > eigener > Richtpreis; bei Gleichstand der jüngste). */ export function currentPrices(points: PricePoint[]): Map { const best = new Map(); for (const p of points) { const cur = best.get(p.ingredientId); if (!cur || rank(p) > rank(cur) || (rank(p) === rank(cur) && p.observedAt > cur.observedAt)) best.set(p.ingredientId, p); } return best; } /** * Produktgruppen (26.11.38): Hat ein Oberbegriff („Pasta“) keinen besseren Preis als * seine Sorten, gilt der Schnitt der Sorten mit dem besten Rang (Bon vor Richtpreis), * in derselben Einheit. Wer Spaghetti per Bon kennt, kennt damit auch „Pasta“. */ export function withGroupPrices(prices: Map, members: Map): Map { const out = new Map(prices); for (const [groupId, ids] of members) { const own = prices.get(groupId); const known = ids.map((id) => prices.get(id)).filter((p): p is PricePoint => !!p && (!own || p.unit === own.unit)); if (!known.length) continue; const best = Math.max(...known.map(rank)); if (own && rank(own) >= best) continue; const top = known.filter((p) => rank(p) === best); out.set(groupId, { ...top[0]!, ingredientId: groupId, pricePerUnit: top.reduce((sum, p) => sum + p.pricePerUnit, 0) / top.length, observedAt: top.map((p) => p.observedAt).sort().at(-1)!, }); } return out; } export function dishCost(lines: CostLine[], prices: Map): DishCost { const own = lines.filter((l) => !l.staple); let sum = 0; let priced = 0; let fromReceipts = 0; for (const l of own) { const p = prices.get(l.ingredientId); if (!p || p.unit !== l.unit) continue; // ohne passenden Preis: nicht raten sum += l.qtyPerPortion * p.pricePerUnit; priced++; if (p.source === "receipt") fromReceipts++; } const total = own.length; const share = total ? fromReceipts / total : 0; const confidence = (share >= 0.99 ? 3 : share >= 0.5 ? 2 : share > 0 ? 1 : 0) as DishCost["confidence"]; return { perPortion: priced ? Math.round(sum * (1 + STAPLE_SHARE) * 100) / 100 : null, priced, total, fromReceipts, confidence }; } export type PriceClass = "cheap" | "normal" | "expensive"; /** Klassen aus den Terzilen der eigenen Kosten pro Portion — keine festen Eurobeträge. */ export function priceClasses(costs: Map): Map { const known = [...costs].filter((e): e is [string, number] => e[1] !== null).sort((a, b) => a[1] - b[1]); const out = new Map(); if (known.length < 3) return out; // zu wenig für Terzile const at = (q: number) => known[Math.min(known.length - 1, Math.floor(q * known.length))]![1]; const low = at(1 / 3); const high = at(2 / 3); for (const [id, c] of known) out.set(id, c < low ? "cheap" : c >= high ? "expensive" : "normal"); return out; } export type Traffic = "green" | "yellow" | "red"; /** Budget-Ampel: grün bis 100 %, gelb bis 115 %, rot darüber. */ export function traffic(total: number, budget: number | null): Traffic | null { if (!budget || budget <= 0) return null; const share = total / budget; return share <= 1 ? "green" : share <= 1.15 ? "yellow" : "red"; } /** Erlaubte teure Gerichte: höchstens eines je `per` Slots (mindestens eines). */ export const expensiveAllowed = (slots: number, per: number) => Math.max(1, Math.floor(slots / Math.max(1, per)));