87 lines
3.7 KiB
TypeScript
87 lines
3.7 KiB
TypeScript
// 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<string, PricePoint> {
|
||
const best = new Map<string, PricePoint>();
|
||
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;
|
||
}
|
||
|
||
export function dishCost(lines: CostLine[], prices: Map<string, PricePoint>): 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<string, number | null>): Map<string, PriceClass> {
|
||
const known = [...costs].filter((e): e is [string, number] => e[1] !== null).sort((a, b) => a[1] - b[1]);
|
||
const out = new Map<string, PriceClass>();
|
||
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)));
|