From 482c094511adcd82a1691daf0e3355d024b2d172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 30 Jun 2021 10:42:42 +0200 Subject: [PATCH] better drinks dependencies #2 drink_ingredients has name and cost_per_volume --- src/api.d.ts | 2 ++ src/components/CalculationTable/Ingredients.vue | 2 ++ src/store.ts | 13 +++++++++++++ src/utils/utils.ts | 5 +++-- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/api.d.ts b/src/api.d.ts index 645bf29..aa17e08 100644 --- a/src/api.d.ts +++ b/src/api.d.ts @@ -17,6 +17,8 @@ declare namespace FG { id: number; volume: number; ingredient_id: number; + cost_per_volume: number; + name: string; } interface DrinkPrice { id: number; diff --git a/src/components/CalculationTable/Ingredients.vue b/src/components/CalculationTable/Ingredients.vue index 7090199..e431ca2 100644 --- a/src/components/CalculationTable/Ingredients.vue +++ b/src/components/CalculationTable/Ingredients.vue @@ -196,6 +196,8 @@ export default defineComponent({ id: -1, ingredient_id: newIngredient.value.id, volume: newIngredientVolume.value, + cost_per_volume: newIngredient.value.cost_per_volume, + name: newIngredient.value.name, }, extra_ingredient: undefined, }; diff --git a/src/store.ts b/src/store.ts index b91d9d8..e45dfef 100644 --- a/src/store.ts +++ b/src/store.ts @@ -1,6 +1,7 @@ import { api } from '@flaschengeist/api'; import { defineStore } from 'pinia'; import { calc_volume, calc_cost_per_volume, calc_all_min_prices } from './utils/utils'; +import Ingredient = FG.Ingredient; interface DrinkPriceVolume extends Omit { _volume: number; @@ -191,6 +192,18 @@ export const usePricelistStore = defineStore({ }); this.drinks[index] = _drink; } + if (!!drink.cost_per_volume) { + this.drinks.forEach((_drink: Drink) => { + _drink.volumes.forEach((_volume: DrinkPriceVolume) => { + _volume.ingredients.forEach((_ingredient: Ingredient) => { + if (_ingredient.drink_ingredient && _ingredient.drink_ingredient.ingredient_id === drink.id) { + _ingredient.drink_ingredient.cost_per_volume = drink.cost_per_volume + _ingredient.drink_ingredient.name = drink.name + } + }) + }) + }) + } calc_all_min_prices(this.drinks, this.min_prices); }, deleteDrink(drink: Drink) { diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 0f29143..78940e9 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -36,8 +36,9 @@ function helper(volume: DrinkPriceVolume, min_price: number) { let extraIngredientPrice = 0; volume.ingredients.forEach((ingredient) => { if (ingredient.drink_ingredient) { - const _drink = usePricelistStore().drinks.find((a) => a.id === ingredient.drink_ingredient?.ingredient_id); - retVal += ingredient.drink_ingredient.volume * (_drink?.cost_per_volume); + //const _drink = usePricelistStore().drinks.find((a) => a.id === ingredient.drink_ingredient?.ingredient_id); + //retVal += ingredient.drink_ingredient.volume * (_drink?.cost_per_volume); + retVal += ingredient.drink_ingredient.volume * ingredient.drink_ingredient.cost_per_volume } if (ingredient.extra_ingredient) { extraIngredientPrice += ingredient.extra_ingredient.price;