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, };