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/volumes/${volume.id}/prices`, 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)); }, setVolume(volume: FG.DrinkPriceVolume, drink: FG.Drink) { axios .post(`pricelist/drinks/${drink.id}/volumes`, volume) .then((response: AxiosResponse) => { drink.volumes.push(response.data); }) .catch(err => console.warn(err)); }, updateVolume(volume: FG.DrinkPriceVolume, drink: FG.Drink) { axios .put(`pricelist/volumes/${volume.id}`, volume) .then(() => { //calc_min_prices_by_drink(volume, drink); }) .catch(err => console.warn(err)); } }; const getters = {}; function calc_min_prices_by_drink(volume: FG.DrinkPriceVolume, drink: FG.Drink) { volume.min_prices.forEach(min_price => { min_price.price = ((drink.cost_price_pro_volume || 0) * volume.volume * min_price.percentage) / 100; }); } 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 };