2024-05-19 03:43:59 +00:00
|
|
|
import units from '@/units.js';
|
|
|
|
|
|
2024-05-12 03:32:18 +00:00
|
|
|
export function mealToShoppingListSources(meal) {
|
|
|
|
|
const sources = [];
|
|
|
|
|
for (const recipe of meal.recipes) {
|
|
|
|
|
for (const ingredient of recipe.ingredients) {
|
|
|
|
|
sources.push({ ingredient, meal, recipe, });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const ingredient of meal.extra_ingredients) {
|
|
|
|
|
sources.push({ ingredient, meal, });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sources;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-18 07:05:28 +00:00
|
|
|
export function requestsToSources(requests) {
|
|
|
|
|
const sources = [];
|
|
|
|
|
for (const request of requests) {
|
|
|
|
|
if (request.ingredient) {
|
|
|
|
|
sources.push(request);
|
|
|
|
|
}
|
|
|
|
|
else if (request.meal) {
|
|
|
|
|
sources.push(...mealToShoppingListSources(request.meal));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sources;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-12 03:32:18 +00:00
|
|
|
export function removeMealFromShoppingListSources(sources, meal) {
|
2024-05-12 06:32:28 +00:00
|
|
|
return sources.filter(source => source.meal?.id !== meal.id);
|
2024-05-12 03:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-19 03:43:59 +00:00
|
|
|
export function groupingToIngredients(grouping) {
|
|
|
|
|
const product = grouping.product;
|
|
|
|
|
const totals = [];
|
|
|
|
|
for (const total of grouping.totals) {
|
|
|
|
|
totals.push({ id: 0, line: `${total.quantity} ${total.unit} of ${product.name}`, preparation: '', name: product.name, product, ...total });
|
|
|
|
|
}
|
|
|
|
|
return totals;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function calculateTotals(sources) {
|
|
|
|
|
const totals = {};
|
|
|
|
|
for (const source of sources) {
|
|
|
|
|
const { unit, factor } = units.getConversionFactor(source.ingredient.unit) ?? { unit: source.ingredient.unit, factor: 1 };
|
|
|
|
|
|
|
|
|
|
const quantity = source.ingredient.quantity / factor;
|
|
|
|
|
if (!totals[unit]) {
|
|
|
|
|
totals[unit] = quantity;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
totals[unit] += quantity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = [];
|
|
|
|
|
for (const unit in totals) {
|
|
|
|
|
result.push({ unit, quantity: totals[unit] });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-12 03:32:18 +00:00
|
|
|
export function groupByProduct(sources) {
|
2024-05-12 06:32:28 +00:00
|
|
|
const noProductKey = "no-product";
|
|
|
|
|
const noProduct = { name: "No product", };
|
|
|
|
|
|
2024-05-12 03:32:18 +00:00
|
|
|
const grouped = {};
|
|
|
|
|
for (const source of sources) {
|
2024-05-12 06:32:28 +00:00
|
|
|
const key = source.ingredient?.product?.id ?? noProductKey;
|
|
|
|
|
if (!key) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!grouped[key]) {
|
|
|
|
|
grouped[key] = {
|
|
|
|
|
product: source.ingredient.product ?? noProduct,
|
2024-05-12 03:32:18 +00:00
|
|
|
sources: [],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-12 06:32:28 +00:00
|
|
|
grouped[key].sources.push(source);
|
2024-05-12 03:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-19 03:43:59 +00:00
|
|
|
for (const key in grouped) {
|
|
|
|
|
grouped[key].totals = calculateTotals(grouped[key].sources);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-12 03:32:18 +00:00
|
|
|
return Object.values(grouped);
|
|
|
|
|
}
|