diff --git a/src/flaschengeist.d.ts b/src/flaschengeist.d.ts index 3fe8aaa..8325379 100644 --- a/src/flaschengeist.d.ts +++ b/src/flaschengeist.d.ts @@ -49,9 +49,7 @@ declare namespace FG { interface DrinkIngredient { id: number; volume: number; - drink_ingredient_id: number; drink_ingredient?: Drink; - price: number; } interface DrinkPrice { id: number; @@ -70,7 +68,7 @@ declare namespace FG { //computed_volume: ComputedRef; min_prices: DrinkMinPrice[]; prices: Array; - ingredients: Array; + ingredients: Array; } interface DrinkType { id: number; @@ -83,7 +81,6 @@ declare namespace FG { } interface Ingredient { id: number; - volume_id: number; drink_ingredient: DrinkIngredient | null; extra_ingredient: ExtraIngredient | null; } diff --git a/src/plugins/pricelist/components/CalculationTable.vue b/src/plugins/pricelist/components/CalculationTable.vue index 108c8ea..32e9694 100644 --- a/src/plugins/pricelist/components/CalculationTable.vue +++ b/src/plugins/pricelist/components/CalculationTable.vue @@ -112,48 +112,7 @@ -
-
- - -
-
- {{ ingredient.name }} {{ ingredient.price }} -
-
-
- -
+
@@ -168,7 +127,13 @@ >
- +
@@ -200,12 +165,14 @@ import { import { v4 } from 'uuid'; import store, { create_volume, DrinkPriceVolume, Drink } from '../store/altStore'; import PriceTable from 'src/plugins/pricelist/components/CalculationTable/PriceTable.vue'; +import Ingredients from 'src/plugins/pricelist/components/CalculationTable/Ingredients.vue'; export default defineComponent({ name: 'CalculationTable', - components: { PriceTable }, + components: { PriceTable, Ingredients }, setup(_, { root }) { onBeforeMount(() => { store.actions.getDrinks(); + store.actions.getExtraIngredients(); }); const columns = [ @@ -334,7 +301,7 @@ export default defineComponent({ } function addIngredient(ingredients: FG.Ingredient[]) { - ingredients.push({ id: -1, volume_id: 0, drink_ingredient: null, extra_ingredient: null }); + ingredients.push({ id: -1, drink_ingredient: null, extra_ingredient: null }); } return { diff --git a/src/plugins/pricelist/components/CalculationTable/Ingredients.vue b/src/plugins/pricelist/components/CalculationTable/Ingredients.vue new file mode 100644 index 0000000..d459109 --- /dev/null +++ b/src/plugins/pricelist/components/CalculationTable/Ingredients.vue @@ -0,0 +1,219 @@ + + + + + diff --git a/src/plugins/pricelist/store/altStore.ts b/src/plugins/pricelist/store/altStore.ts index 1124713..3d1922e 100644 --- a/src/plugins/pricelist/store/altStore.ts +++ b/src/plugins/pricelist/store/altStore.ts @@ -2,10 +2,16 @@ import { reactive, computed, ComputedRef, WritableComputedRef } from '@vue/compo import { axios } from 'src/boot/axios'; import { AxiosResponse } from 'axios'; -const state = reactive<{ drinks: Drink[]; tags: FG.Tag[]; drinkTypes: FG.DrinkType[] }>({ +const state = reactive<{ + drinks: Drink[]; + tags: FG.Tag[]; + drinkTypes: FG.DrinkType[]; + extraIngredients: FG.ExtraIngredient[]; +}>({ drinks: [], tags: [], - drinkTypes: [] + drinkTypes: [], + extraIngredients: [] }); interface MinPrice extends Omit { @@ -165,6 +171,44 @@ const actions = { } }) .catch(err => console.warn(err)); + }, + getExtraIngredients() { + axios + .get('pricelist/ingredients/extraIngredients') + .then((response: AxiosResponse) => { + state.extraIngredients = response.data; + }) + .catch(err => console.log(err)); + }, + setIngredient(ingredient: FG.Ingredient, volume: DrinkPriceVolume) { + axios + .post(`pricelist/volumes/${volume.id}/ingredients`, ingredient) + .then((response: AxiosResponse) => { + volume.ingredients.push(response.data); + }) + .catch(err => console.warn(err)); + }, + updateIngredient(ingredient: FG.Ingredient, volume: DrinkPriceVolume) { + axios + .put(`pricelist/ingredients/${ingredient.id}`, ingredient) + .then((response: AxiosResponse) => { + const index = volume.ingredients.findIndex(a => a.id === response.data.id); + if (index > -1) { + volume.ingredients[index] = response.data; + } + }) + .catch(err => console.warn(err)); + }, + deleteIngredient(ingredient: FG.Ingredient, volume: DrinkPriceVolume) { + axios + .delete(`pricelist/ingredients/${ingredient.id}`) + .then(() => { + const index = volume.ingredients.findIndex(a => a.id === ingredient.id); + if (index > -1) { + volume.ingredients.splice(index, 1); + } + }) + .catch(err => console.warn(err)); } }; @@ -176,8 +220,8 @@ function create_volume(drink: Drink, volume: DrinkPriceVolume) { if (volume.ingredients.some(ingredient => !!ingredient.drink_ingredient)) { let retVal = 0; volume.ingredients.forEach(ingredient => { - if (ingredient.volume) { - retVal += ingredient.volume; + if (ingredient.drink_ingredient?.volume) { + retVal += ingredient.drink_ingredient.volume; } }); volume._volume = retVal; @@ -195,8 +239,10 @@ function create_min_prices(drink: Drink, volume: DrinkPriceVolume, percentage: n if (volume.ingredients.every(ingredient => !!ingredient.drink_ingredient)) { return computed(() => { let retVal = (drink.cost_price_pro_volume || 0) * (volume.volume?.value || 0); - volume.ingredients.forEach((ingredient: FG.ExtraIngredient) => { - retVal += ingredient.price; + volume.ingredients.forEach(ingredient => { + if (ingredient.extra_ingredient) { + retVal += ingredient.extra_ingredient.price; + } }); retVal = (retVal * percentage) / 100; return retVal; @@ -211,10 +257,12 @@ function create_min_prices(drink: Drink, volume: DrinkPriceVolume, percentage: n let retVal = 0; volume.ingredients.forEach(ingredient => { if (ingredient.drink_ingredient) { - retVal += ingredient.volume * (ingredient.drink_ingredient.cost_price_pro_volume || 0); + retVal += + ingredient.drink_ingredient.volume * + (ingredient.drink_ingredient.drink_ingredient?.cost_price_pro_volume || 0); } - if (ingredient.name) { - retVal += ingredient.price; + if (ingredient.extra_ingredient) { + retVal += ingredient.extra_ingredient.price; } }); console.log(volume);