From cd937f111c0275635129320d59264032a21f726d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Sun, 21 Mar 2021 22:07:12 +0100 Subject: [PATCH] [pricelist][vue3] now it works --- .../pricelist/components/CalculationTable.vue | 80 ++++++--- .../DrinkPriceVolumeTable/NewVolume.vue | 2 +- .../DrinkPriceVolumesTable.vue | 4 +- .../CalculationTable/PriceTable.vue | 18 +- .../pricelist/components/ExtraIngredients.vue | 7 +- .../pricelist/components/MinPriceSetting.vue | 55 ++++++ src/plugins/pricelist/pages/Settings.vue | 4 +- src/plugins/pricelist/store.ts | 166 ++++++++++-------- 8 files changed, 230 insertions(+), 106 deletions(-) create mode 100644 src/plugins/pricelist/components/MinPriceSetting.vue diff --git a/src/plugins/pricelist/components/CalculationTable.vue b/src/plugins/pricelist/components/CalculationTable.vue index 190011d..1f33be0 100644 --- a/src/plugins/pricelist/components/CalculationTable.vue +++ b/src/plugins/pricelist/components/CalculationTable.vue @@ -20,6 +20,11 @@ @@ -281,9 +317,9 @@ + + diff --git a/src/plugins/pricelist/pages/Settings.vue b/src/plugins/pricelist/pages/Settings.vue index 6343add..afe4675 100644 --- a/src/plugins/pricelist/pages/Settings.vue +++ b/src/plugins/pricelist/pages/Settings.vue @@ -55,7 +55,6 @@ import { usePricelistStore } from 'src/plugins/pricelist/store'; export default defineComponent({ name: 'Settings', - //components: { DrinkTypes, ExtraIngredients, CalculationTable }, components: { ExtraIngredients, DrinkTypes, CalculationTable }, setup() { interface Tab { @@ -71,7 +70,8 @@ export default defineComponent({ }) .catch((err) => console.log(err)); void store.getDrinkTypes(); - store.getDrinks(); + void store.getDrinks(); + void store.get_min_prices(); }); const drawer = ref(false); diff --git a/src/plugins/pricelist/store.ts b/src/plugins/pricelist/store.ts index c6517c7..af6b06d 100644 --- a/src/plugins/pricelist/store.ts +++ b/src/plugins/pricelist/store.ts @@ -1,7 +1,7 @@ import { api } from 'src/boot/axios'; import { defineStore } from 'pinia'; import { AxiosResponse } from 'axios'; -import { computed, WritableComputedRef } from 'vue'; +import { computed, ComputedRef, WritableComputedRef } from 'vue'; interface MinPrice extends Omit { price?: WritableComputedRef; @@ -18,28 +18,12 @@ interface Drink extends Omit, 'volumes'> } class DrinkPriceVolume implements DrinkPriceVolume { - constructor( - { id, volume, /*min_prices,*/ prices, ingredients }: FG.DrinkPriceVolume, - drink: Drink - ) { + constructor({ id, volume, prices, ingredients }: FG.DrinkPriceVolume) { this.id = id; this._volume = volume; this.prices = prices; this.ingredients = ingredients; - this.min_prices = [ - { - percentage: 100, - price: create_min_prices(drink, this, 100), - }, - { - percentage: 250, - price: create_min_prices(drink, this, 250), - }, - { - percentage: 300, - price: create_min_prices(drink, this, 300), - }, - ]; + this.min_prices = []; this.volume = computed({ get: () => { if (this.ingredients.some((ingredient) => !!ingredient.drink_ingredient)) { @@ -71,8 +55,7 @@ class Drink { cost_price_package_netto, tags, type, - }: /*volumes,*/ - FG.Drink) { + }: FG.Drink) { this.id = id; this.article_id = article_id; this.package_size = package_size; @@ -97,13 +80,13 @@ class Drink { this.tags = tags; this.type = type; this.volumes = []; - //volumes.forEach(volume => { - // this.volumes.push(new DrinkPriceVolume(volume, this)); - //}); } } function create_min_prices(drink: Drink, volume: DrinkPriceVolume, percentage: number) { + if (drink.name == 'Elbhang Rot') { + console.log(drink.name, drink.cost_price_pro_volume, volume.volume, drink, volume); + } if (drink.cost_price_pro_volume?.value) { if (volume.ingredients.every((ingredient) => !!ingredient.drink_ingredient)) { return computed(() => { @@ -126,6 +109,7 @@ function create_min_prices(drink: Drink, volume: DrinkPriceVolume, percentage: n } else { return computed(() => { let retVal = 0; + let extraIngredientPrice = 0; volume.ingredients.forEach((ingredient) => { if (ingredient.drink_ingredient) { const _drink = usePricelistStore().drinks.find( @@ -135,11 +119,10 @@ function create_min_prices(drink: Drink, volume: DrinkPriceVolume, percentage: n ingredient.drink_ingredient.volume * (_drink?.cost_price_pro_volume?.value || 0); } if (ingredient.extra_ingredient) { - retVal += ingredient.extra_ingredient.price; + extraIngredientPrice += ingredient.extra_ingredient.price; } }); - console.log(volume); - return (retVal * percentage) / 100; + return (retVal * percentage) / 100 + extraIngredientPrice; }); } } @@ -152,6 +135,7 @@ export const usePricelistStore = defineStore({ drinks: [] as Array, extraIngredients: [] as Array, pricecalc_columns: [] as Array, + min_prices: [] as Array, }), actions: { @@ -208,23 +192,18 @@ export const usePricelistStore = defineStore({ this.extraIngredients.splice(index, 1); } }, - getDrinks() { - api - .get('pricelist/drinks') - .then((response: AxiosResponse) => { - this.drinks = []; - response.data.forEach((drink) => { - this.drinks.push(new Drink(drink)); - }); - this.drinks.forEach((drink) => { - const _drink = response.data.find((a) => a.id === drink.id); - _drink?.volumes.forEach((volume) => { - drink.volumes.push(new DrinkPriceVolume(volume, drink)); - }); - }); - console.log(this.drinks); - }) - .catch((err) => console.warn(err)); + async getDrinks() { + const { data } = await api.get>('pricelist/drinks'); + this.drinks = []; + data.forEach((drink) => { + const _drink = new Drink(drink); + drink.volumes.forEach((volume) => { + const _volume = new DrinkPriceVolume(volume); + _drink.volumes.push(_volume); + }); + this.drinks.push(_drink); + }); + this.create_min_prices(); }, sortPrices(volume: DrinkPriceVolume) { volume.prices.sort((a, b) => { @@ -266,35 +245,28 @@ export const usePricelistStore = defineStore({ }) .catch((err) => console.warn(err)); }, - setDrink(drink: FG.Drink) { - api - .post('pricelist/drinks', drink) - .then((response: AxiosResponse) => { - this.drinks.push(new Drink(response.data)); - const drink = this.drinks.find((a) => a.id === response.data.id); - response.data.volumes.forEach((volume) => { - drink?.volumes.push(new DrinkPriceVolume(volume, drink)); - }); - }) - .catch((err) => console.warn(err)); + async setDrink(drink: FG.Drink) { + const { data } = await api.post('pricelist/drinks', drink); + const _drink = new Drink(data); + data.volumes.forEach((volume) => { + const _volume = new DrinkPriceVolume(volume); + _drink.volumes.push(_volume); + }); + this.drinks.push(_drink); + this.create_min_prices(); }, - updateDrink(drink: Drink) { - api - .put(`pricelist/drinks/${drink.id}`, { - ...drink, - cost_price_pro_volume: drink.cost_price_pro_volume?.value, - }) - .then(({ data }: AxiosResponse) => { - const index = this.drinks.findIndex((a) => a.id === data.id); - if (index > -1) { - this.drinks[index] = new Drink(data); - const drink = this.drinks.find((a) => a.id === data.id); - data.volumes.forEach((volume) => { - drink?.volumes.push(new DrinkPriceVolume(volume, drink)); - }); - } - }) - .catch((err) => console.warn(err)); + async updateDrink(drink: Drink) { + const { data } = await api.put(`pricelist/drinks/${drink.id}`, drink); + const index = this.drinks.findIndex((a) => a.id === data.id); + if (index > -1) { + const _drink = new Drink(data); + data.volumes.forEach((volume) => { + const _volume = new DrinkPriceVolume(volume); + _drink.volumes.push(_volume); + }); + this.drinks[index] = _drink; + } + this.create_min_prices(); }, deleteDrink(drink: Drink) { api @@ -325,6 +297,56 @@ export const usePricelistStore = defineStore({ }) .catch((err) => console.log(err)); }, + async get_min_prices() { + const { data } = await api.get>('pricelist/settings/min_prices'); + this.min_prices = data; + }, + async set_min_prices() { + await api.post>('pricelist/settings/min_prices', this.min_prices); + this.create_min_prices(); + }, + create_min_prices() { + this.drinks.forEach((drink) => { + drink.volumes.forEach((volume) => { + if (drink.name == 'Elbhang Rot2') { + console.log(drink.name, drink.cost_price_pro_volume, volume, drink); + } + volume.min_prices = []; + this.min_prices.forEach((min_price) => { + let computedMinPrice: ComputedRef; + if (drink.cost_price_pro_volume) { + computedMinPrice = computed( + () => + ((drink.cost_price_pro_volume) * + (volume.volume) * + min_price) / + 100 + ); + } else { + computedMinPrice = computed(() => { + let retVal = 0; + let extraIngredientPrice = 0; + volume.ingredients.forEach((ingredient) => { + if (ingredient.drink_ingredient) { + const _drink = usePricelistStore().drinks.find( + (a) => a.id === ingredient.drink_ingredient?.drink_ingredient_id + ); + retVal += + ingredient.drink_ingredient.volume * + (_drink?.cost_price_pro_volume); + } + if (ingredient.extra_ingredient) { + extraIngredientPrice += ingredient.extra_ingredient.price; + } + }); + return (retVal * min_price) / 100 + extraIngredientPrice; + }); + } + volume.min_prices.push({ percentage: min_price, price: computedMinPrice }); + }); + }); + }); + }, }, });