From 951fafd1fa7c936622e7342eb43d05b83c88f1cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Fri, 12 Nov 2021 10:42:52 +0100 Subject: [PATCH] [fix] update minprices if cost_per_volume change --- src/components/DrinkModify.vue | 4 +++- src/store.ts | 28 +++++++++++++++++++--------- src/utils/utils.ts | 11 ++++++++--- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/components/DrinkModify.vue b/src/components/DrinkModify.vue index ef7565d..0b01792 100644 --- a/src/components/DrinkModify.vue +++ b/src/components/DrinkModify.vue @@ -192,6 +192,8 @@ export default defineComponent({ const edit_volumes = ref>([]); function save() { (edit_drink.value).volumes = edit_volumes.value; + edit_drink.value.cost_per_volume = calc_cost_per_volume(edit_drink.value); + edit_drink.value._cost_per_volume = edit_drink.value.cost_per_volume; emit('save', edit_drink.value, toDeleteObjects.value, drinkPic.value, deletePic.value); } @@ -272,7 +274,7 @@ export default defineComponent({ return imgsrc.value; } if (edit_drink.value?.uuid) { - return `${api.defaults.baseURL||''}/pricelist/picture/${edit_drink.value.uuid}?size=256`; + return `${api.defaults.baseURL || ''}/pricelist/picture/${edit_drink.value.uuid}?size=256`; } return 'no-image.svg'; }); diff --git a/src/store.ts b/src/store.ts index e45dfef..34bfc76 100644 --- a/src/store.ts +++ b/src/store.ts @@ -107,11 +107,16 @@ export const usePricelistStore = defineStore({ if (itm.length > 0) itm[0].name = drinkType.name; }, async getExtraIngredients() { - const { data } = await api.get>('pricelist/ingredients/extraIngredients'); + const { data } = await api.get>( + 'pricelist/ingredients/extraIngredients' + ); this.extraIngredients = data; }, async setExtraIngredient(ingredient: FG.ExtraIngredient) { - const { data } = await api.post('pricelist/ingredients/extraIngredients', ingredient); + const { data } = await api.post( + 'pricelist/ingredients/extraIngredients', + ingredient + ); this.extraIngredients.push(data); }, async updateExtraIngredient(ingredient: FG.ExtraIngredient) { @@ -196,13 +201,16 @@ export const usePricelistStore = defineStore({ 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 + 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); }, @@ -281,7 +289,9 @@ export const usePricelistStore = defineStore({ this.pricelist_view = data; }, async getPriceListColumnOrder(userid: string) { - const { data } = await api.get>(`pricelist/users/${userid}/pricecalc_columns_order`); + const { data } = await api.get>( + `pricelist/users/${userid}/pricecalc_columns_order` + ); this.pricelist_columns_order = data; }, async updatePriceListColumnOrder(userid: string, data: Array) { diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 78940e9..6ebfb18 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -17,7 +17,8 @@ function calc_volume(volume: DrinkPriceVolume) { function calc_cost_per_volume(drink: Drink) { let retVal = drink._cost_per_volume; if (!!drink.volume && !!drink.package_size && !!drink.cost_per_package) { - retVal = ((drink.cost_per_package || 0) / ((drink.volume || 0) * (drink.package_size || 0))) * 1.19; + retVal = + ((drink.cost_per_package || 0) / ((drink.volume || 0) * (drink.package_size || 0))) * 1.19; } return retVal ? Math.round(retVal * 1000) / 1000 : retVal; @@ -38,7 +39,7 @@ function helper(volume: DrinkPriceVolume, min_price: number) { 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); - retVal += ingredient.drink_ingredient.volume * ingredient.drink_ingredient.cost_per_volume + retVal += ingredient.drink_ingredient.volume * ingredient.drink_ingredient.cost_per_volume; } if (ingredient.extra_ingredient) { extraIngredientPrice += ingredient.extra_ingredient.price; @@ -47,7 +48,11 @@ function helper(volume: DrinkPriceVolume, min_price: number) { return (retVal * min_price) / 100 + extraIngredientPrice; } -function calc_min_prices(volume: DrinkPriceVolume, cost_per_volume: number | undefined, min_prices: Array) { +function calc_min_prices( + volume: DrinkPriceVolume, + cost_per_volume: number | undefined, + min_prices: Array +) { const retVal: Array = []; volume.min_prices = []; if (min_prices) {