From f6951bdf0b0bab3284d00b52ab49f8e72c93935b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Mon, 15 Mar 2021 19:57:42 +0100 Subject: [PATCH] [pricelist] with backend, with errors price can be add and deleted --- src/flaschengeist.d.ts | 218 ++++++----- .../pricelist/components/CalculationTable.vue | 362 ++++++++---------- src/plugins/pricelist/store/altStore.ts | 100 +++++ 3 files changed, 373 insertions(+), 307 deletions(-) create mode 100644 src/plugins/pricelist/store/altStore.ts diff --git a/src/flaschengeist.d.ts b/src/flaschengeist.d.ts index ec9cc9d..a69c5f1 100644 --- a/src/flaschengeist.d.ts +++ b/src/flaschengeist.d.ts @@ -1,101 +1,121 @@ declare namespace FG { - interface Session { - expires: Date; - token: string; - lifetime: number; - browser: string; - platform: string; - userid: string; - } - interface User { - userid: string; - display_name: string; - firstname: string; - lastname: string; - mail: string; - birthday?: any; - roles: Array; - permissions?: any; - avatar_url?: any; - } - type Permission = string; - interface Role { - id: number; - name: string; - permissions: Array; - } - interface Transaction { - id: number; - time: Date; - amount: number; - reversal_id: number; - sender_id?: any; - receiver_id?: any; - author_id?: any; - original_id?: any; - } - interface Drink { - id: number; - name: string; - volume: number; - cost_price: number; - discount: number; - extra_charge?: any; - prices: Array; - ingredients: Array; - tags: Array; - } - interface DrinkPrice { - id: number; - volume: number; - price: number; - no_auto: boolean; - public: boolean; - description?: any; - round_step: number; - } - interface DrinkType { - id: number; - name: string; - } - interface Ingredient { - id: number; - volume: number; - drink_parent_id: number; - drink_ingredient_id: number; - drink_ingredient?: any; - } - interface Tag { - id: number; - name: string; - } - interface Event { - id: number; - start: Date; - end: Date; - description?: any; - type: EventType; - jobs: Array; - } - interface EventType { - id: number; - name: string; - } - interface Job { - id: number; - start: Date; - end?: any; - comment: string; - type: JobType; - services: Array; - required_services: number; - } - interface JobType { - id: number; - name: string; - } - interface Service { - userid: string; - value: number; - } + interface Session { + expires: Date; + token: string; + lifetime: number; + browser: string; + platform: string; + userid: string; + } + interface User { + userid: string; + display_name: string; + firstname: string; + lastname: string; + mail: string; + birthday?: Date; + roles: Array; + permissions?: string[]; + avatar_url?: string; + } + type Permission = string; + interface Role { + id: number; + name: string; + permissions: Array; + } + interface Transaction { + id: number; + time: Date; + amount: number; + reversal_id: number; + sender_id?: number; + receiver_id?: number; + author_id?: number; + original_id?: number; + } + interface Drink { + id: number; + article_id?: string; + package_size?: number; + name: string; + volume?: number; + cost_price_pro_volume?: number; + cost_price_package_netto?: number; + tags: Array; + type: DrinkType; + volumes: DrinkPriceVolume[]; + } + interface DrinkIngredient { + id: number; + volume: number; + drink_ingredient_id: number; + drink_ingredient?: Drink; + price: number; + } + interface DrinkPrice { + id: number; + price: number; + public: boolean; + description?: string; + } + interface DrinkMinPrice { + percentage: number; + price: number; + } + interface DrinkPriceVolume { + id: number; + volume: number; + min_prices: DrinkMinPrice[]; + prices: Array; + ingredients: Array; + } + interface DrinkType { + id: number; + name: string; + } + interface ExtraIngredient { + id: number; + name: string; + price: number; + } + interface Ingredient { + id: number; + volume_id: number; + drink_ingredient: DrinkIngredient | null; + extra_ingredient: ExtraIngredient | null; + } + interface Tag { + id: number; + name: string; + } + interface Event { + id: number; + start: Date; + end: Date; + description?: string; + type: EventType; + jobs: Array; + } + interface EventType { + id: number; + name: string; + } + interface Job { + id: number; + start: Date; + end?: Date; + comment: string; + type: JobType; + services: Array; + required_services: number; + } + interface JobType { + id: number; + name: string; + } + interface Service { + userid: string; + value: number; + } } diff --git a/src/plugins/pricelist/components/CalculationTable.vue b/src/plugins/pricelist/components/CalculationTable.vue index 2bfd96d..5ce69a9 100644 --- a/src/plugins/pricelist/components/CalculationTable.vue +++ b/src/plugins/pricelist/components/CalculationTable.vue @@ -3,7 +3,7 @@ @@ -22,10 +22,10 @@ options-cover /> - @@ -187,280 +232,181 @@ diff --git a/src/plugins/pricelist/store/altStore.ts b/src/plugins/pricelist/store/altStore.ts new file mode 100644 index 0000000..5430a8a --- /dev/null +++ b/src/plugins/pricelist/store/altStore.ts @@ -0,0 +1,100 @@ +import { reactive } from '@vue/composition-api'; +import { axios } from 'src/boot/axios'; +import { AxiosResponse } from 'axios'; +import ExtraIngredient = FG.ExtraIngredient; + +const state = reactive<{ drinks: FG.Drink[]; tags: FG.Tag[]; drinkTypes: FG.DrinkType[] }>({ + drinks: [], + tags: [], + drinkTypes: [], +}); + +const actions = { + getDrinks() { + axios + .get('pricelist/drinks') + .then((response: AxiosResponse) => { + state.drinks = response.data; + console.log(state.drinks); + state.drinks.forEach((drink: FG.Drink) => { + drink.volumes.forEach((volume: FG.DrinkPriceVolume) => { + volume.min_prices = [ + { percentage: 100, price: (drink.cost_price_pro_volume || 0) * volume.volume * 1 }, + { percentage: 250, price: (drink.cost_price_pro_volume || 0) * volume.volume * 2.5 }, + { percentage: 300, price: (drink.cost_price_pro_volume || 0) * volume.volume * 3 }, + ]; + if (volume.ingredients.length > 0) { + calc_min_prices(volume); + } + this.sortPrices(volume); + }); + }); + }) + .catch((err) => console.warn(err)); + }, + setPrice(price: FG.DrinkPrice, volume: FG.DrinkPriceVolume) { + axios + .post(`pricelist/prices/volumes/${volume.id}`, price) + .then((response: AxiosResponse) => { + volume.prices.push(response.data); + this.sortPrices(volume); + }) + .catch((err) => console.warn(err)); + }, + sortPrices(volume: FG.DrinkPriceVolume) { + volume.prices.sort((a, b) => { + if (a.price > b.price) return 1; + if (b.price > a.price) return -1; + return 0; + }); + }, + deletePrice(price: FG.DrinkPrice, volume: FG.DrinkPriceVolume) { + axios + .delete(`pricelist/prices/${price.id}`) + .then(() => { + const index = volume.prices.findIndex((a) => a.id == price.id); + if (index > -1) { + volume.prices.splice(index, 1); + } + }) + .catch((err) => console.warn(err)); + }, + updatePrice(price: FG.DrinkPrice, volume: FG.DrinkPriceVolume) { + axios + .put(`pricelist/prices/${price.id}`, price) + .then((response: AxiosResponse) => { + const index = volume.prices.findIndex((a) => a.id === price.id); + if (index > -1) { + volume.prices[index] = response.data; + this.sortPrices(volume); + } + }) + .catch((err) => console.log(err)); + }, +}; + +const getters = {}; + +function calc_min_prices(row: FG.DrinkPriceVolume) { + row.volume = 0; + let cost_price = 0; + row.ingredients.forEach((ingredient: FG.DrinkIngredient & FG.ExtraIngredient) => { + console.log(ingredient); + if (ingredient.drink_ingredient) { + row.volume = row.volume + ingredient.volume; + cost_price += ingredient.volume * (ingredient.drink_ingredient.cost_price_pro_volume || 0); + } else if (ingredient.name && ingredient.price) { + cost_price += ingredient.price; + } + }); + row.min_prices.forEach((min_price) => { + min_price.price = (cost_price * min_price.percentage) / 100; + }); +} + +export { calc_min_prices }; +export default { + state, + actions, + getters, +};