97 lines
3.6 KiB
TypeScript
97 lines
3.6 KiB
TypeScript
// Einkaufsliste aus dem Plan (Konzept, „Einkaufen“) — rein und testbar.
|
||
// Mengen pro Portion × Portionen je Slot, über alle Slots je Zutat summiert,
|
||
// dann auf Einkaufseinheiten gebracht: Stück aufrunden, Gewicht und Volumen
|
||
// auf die kleinste passende Packung („1 Packung Hack 500 g statt 480 g“).
|
||
// Grundvorrat (Öl, Salz …) kommt nicht auf die Liste, sondern in den Vorrats-Check.
|
||
|
||
export type BaseUnit = "g" | "ml" | "piece";
|
||
|
||
export type PlanIngredient = {
|
||
ingredientId: string;
|
||
name: string;
|
||
unit: BaseUnit;
|
||
qtyPerPortion: number;
|
||
category: string;
|
||
packs: number[]; // übliche Packungsgrößen in der Basiseinheit
|
||
pantry: boolean; // gehört zum Grundvorrat
|
||
};
|
||
|
||
export type PlanSlotInput = { dishTitle: string; portions: number; ingredients: PlanIngredient[] };
|
||
|
||
export type Aisle = { position: number; name: string; categories: string[] };
|
||
|
||
export type ListLine = {
|
||
ingredientId: string;
|
||
name: string;
|
||
unit: BaseUnit;
|
||
needed: number; // was die Rezepte brauchen
|
||
qty: number; // was man kauft
|
||
pack: { count: number; size: number } | null;
|
||
category: string;
|
||
aislePosition: number | null;
|
||
dishes: string[];
|
||
pantry: boolean;
|
||
};
|
||
|
||
/** Kleinste Packungsmenge, die den Bedarf deckt: eine passende Packung oder mehrere der größten. */
|
||
export function choosePack(needed: number, packs: number[]): { count: number; size: number } | null {
|
||
const sizes = [...packs].filter((p) => p > 0).sort((a, b) => a - b);
|
||
if (!sizes.length || needed <= 0) return null;
|
||
const fit = sizes.find((s) => s >= needed - 1e-9);
|
||
if (fit) return { count: 1, size: fit };
|
||
const largest = sizes.at(-1)!;
|
||
// Mehrere Packungen: die Kombination mit dem kleinsten Überschuss aus Vielfachen einer Größe.
|
||
let best = { count: Math.ceil(needed / largest), size: largest };
|
||
for (const s of sizes) {
|
||
const count = Math.ceil(needed / s);
|
||
if (count <= 6 && count * s < best.count * best.size) best = { count, size: s };
|
||
}
|
||
return best;
|
||
}
|
||
|
||
export function aggregate(slots: PlanSlotInput[], aisles: Aisle[]): ListLine[] {
|
||
const lines = new Map<string, ListLine>();
|
||
for (const slot of slots) {
|
||
for (const ing of slot.ingredients) {
|
||
const line =
|
||
lines.get(ing.ingredientId) ??
|
||
({
|
||
ingredientId: ing.ingredientId,
|
||
name: ing.name,
|
||
unit: ing.unit,
|
||
needed: 0,
|
||
qty: 0,
|
||
pack: null,
|
||
category: ing.category,
|
||
aislePosition: aisles.find((a) => a.categories.includes(ing.category))?.position ?? null,
|
||
dishes: [],
|
||
pantry: ing.pantry,
|
||
} satisfies ListLine);
|
||
line.needed += ing.qtyPerPortion * slot.portions;
|
||
if (!line.dishes.includes(slot.dishTitle)) line.dishes.push(slot.dishTitle);
|
||
lines.set(ing.ingredientId, line);
|
||
}
|
||
}
|
||
for (const line of lines.values()) {
|
||
line.needed = Math.round(line.needed * 1000) / 1000;
|
||
if (line.unit === "piece") {
|
||
line.qty = Math.ceil(line.needed - 1e-9);
|
||
} else {
|
||
line.pack = choosePack(line.needed, []);
|
||
line.qty = line.needed;
|
||
}
|
||
}
|
||
return [...lines.values()].sort(
|
||
(a, b) => (a.aislePosition ?? 999) - (b.aislePosition ?? 999) || a.name.localeCompare(b.name),
|
||
);
|
||
}
|
||
|
||
/** Packungen nachtragen (getrennt, damit aggregate() ohne Packungsdaten testbar bleibt). */
|
||
export function withPacks(lines: ListLine[], packsOf: (ingredientId: string) => number[]): ListLine[] {
|
||
return lines.map((l) => {
|
||
if (l.unit === "piece") return l;
|
||
const pack = choosePack(l.needed, packsOf(l.ingredientId));
|
||
return { ...l, pack, qty: pack ? pack.count * pack.size : l.needed };
|
||
});
|
||
}
|