2021-03-16 17:10:37 +00:00
|
|
|
import { reactive, computed, ComputedRef, WritableComputedRef } from '@vue/composition-api';
|
2021-03-15 18:57:42 +00:00
|
|
|
import { axios } from 'src/boot/axios';
|
|
|
|
import { AxiosResponse } from 'axios';
|
|
|
|
|
2021-03-16 22:28:38 +00:00
|
|
|
const state = reactive<{
|
|
|
|
drinks: Drink[];
|
|
|
|
tags: FG.Tag[];
|
|
|
|
drinkTypes: FG.DrinkType[];
|
|
|
|
extraIngredients: FG.ExtraIngredient[];
|
|
|
|
}>({
|
2021-03-15 18:57:42 +00:00
|
|
|
drinks: [],
|
|
|
|
tags: [],
|
2021-03-16 22:28:38 +00:00
|
|
|
drinkTypes: [],
|
|
|
|
extraIngredients: []
|
2021-03-15 18:57:42 +00:00
|
|
|
});
|
|
|
|
|
2021-03-16 17:10:37 +00:00
|
|
|
interface MinPrice extends Omit<FG.DrinkMinPrice, 'price'> {
|
|
|
|
price: WritableComputedRef<number> | null;
|
|
|
|
}
|
|
|
|
interface DrinkPriceVolume extends Omit<Omit<FG.DrinkPriceVolume, 'volume'>, 'min_prices'> {
|
|
|
|
_volume: number;
|
|
|
|
volume: WritableComputedRef<number> | null;
|
|
|
|
min_prices: MinPrice[];
|
|
|
|
}
|
|
|
|
interface Drink extends Omit<FG.Drink, 'volumes'> {
|
|
|
|
volumes: DrinkPriceVolume[];
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:57:42 +00:00
|
|
|
const actions = {
|
|
|
|
getDrinks() {
|
|
|
|
axios
|
|
|
|
.get('pricelist/drinks')
|
|
|
|
.then((response: AxiosResponse<FG.Drink[]>) => {
|
2021-03-16 17:10:37 +00:00
|
|
|
//state.drinks = [...response.data];
|
|
|
|
/*state.drinks.forEach((drink: Drink) => {
|
|
|
|
drink.volumes.forEach((volume: DrinkPriceVolume) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
volume._volume = volume.volume;
|
|
|
|
volume.volume = create_volume(drink, volume);
|
2021-03-15 18:57:42 +00:00
|
|
|
volume.min_prices = [
|
2021-03-16 17:10:37 +00:00
|
|
|
{
|
|
|
|
percentage: 100,
|
|
|
|
price: create_min_prices(drink, volume, 100)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
percentage: 250,
|
|
|
|
price: create_min_prices(drink, volume, 250)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
percentage: 300,
|
|
|
|
price: create_min_prices(drink, volume, 300)
|
|
|
|
}
|
2021-03-15 18:57:42 +00:00
|
|
|
];
|
|
|
|
this.sortPrices(volume);
|
|
|
|
});
|
2021-03-16 17:10:37 +00:00
|
|
|
});*/
|
|
|
|
response.data.forEach(drink => {
|
|
|
|
const newDrink: Drink = { ...drink, volumes: [] };
|
|
|
|
drink.volumes.forEach(volume => {
|
|
|
|
const newVolume: DrinkPriceVolume = {
|
|
|
|
...volume,
|
|
|
|
_volume: volume.volume,
|
|
|
|
min_prices: [],
|
|
|
|
volume: null
|
|
|
|
};
|
|
|
|
newVolume.volume = create_volume(newDrink, newVolume);
|
|
|
|
newVolume.min_prices = [
|
|
|
|
{
|
|
|
|
percentage: 100,
|
|
|
|
price: create_min_prices(newDrink, newVolume, 100)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
percentage: 250,
|
|
|
|
price: create_min_prices(newDrink, newVolume, 250)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
percentage: 300,
|
|
|
|
price: create_min_prices(newDrink, newVolume, 300)
|
|
|
|
}
|
|
|
|
];
|
|
|
|
console.log(newVolume);
|
|
|
|
newDrink.volumes.push(newVolume);
|
|
|
|
});
|
|
|
|
state.drinks.push(newDrink);
|
2021-03-15 18:57:42 +00:00
|
|
|
});
|
|
|
|
})
|
2021-03-15 22:52:40 +00:00
|
|
|
.catch(err => console.warn(err));
|
2021-03-15 18:57:42 +00:00
|
|
|
},
|
2021-03-16 17:10:37 +00:00
|
|
|
setPrice(price: FG.DrinkPrice, volume: DrinkPriceVolume) {
|
2021-03-15 18:57:42 +00:00
|
|
|
axios
|
2021-03-15 22:52:40 +00:00
|
|
|
.post(`pricelist/volumes/${volume.id}/prices`, price)
|
2021-03-15 18:57:42 +00:00
|
|
|
.then((response: AxiosResponse<FG.DrinkPrice>) => {
|
|
|
|
volume.prices.push(response.data);
|
|
|
|
this.sortPrices(volume);
|
|
|
|
})
|
2021-03-15 22:52:40 +00:00
|
|
|
.catch(err => console.warn(err));
|
2021-03-15 18:57:42 +00:00
|
|
|
},
|
2021-03-16 17:10:37 +00:00
|
|
|
sortPrices(volume: DrinkPriceVolume) {
|
2021-03-15 18:57:42 +00:00
|
|
|
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(() => {
|
2021-03-15 22:52:40 +00:00
|
|
|
const index = volume.prices.findIndex(a => a.id == price.id);
|
2021-03-15 18:57:42 +00:00
|
|
|
if (index > -1) {
|
|
|
|
volume.prices.splice(index, 1);
|
|
|
|
}
|
|
|
|
})
|
2021-03-15 22:52:40 +00:00
|
|
|
.catch(err => console.warn(err));
|
2021-03-15 18:57:42 +00:00
|
|
|
},
|
2021-03-16 17:10:37 +00:00
|
|
|
updatePrice(price: FG.DrinkPrice, volume: DrinkPriceVolume) {
|
2021-03-15 18:57:42 +00:00
|
|
|
axios
|
|
|
|
.put(`pricelist/prices/${price.id}`, price)
|
|
|
|
.then((response: AxiosResponse<FG.DrinkPrice>) => {
|
2021-03-15 22:52:40 +00:00
|
|
|
const index = volume.prices.findIndex(a => a.id === price.id);
|
2021-03-15 18:57:42 +00:00
|
|
|
if (index > -1) {
|
|
|
|
this.sortPrices(volume);
|
|
|
|
}
|
|
|
|
})
|
2021-03-15 22:52:40 +00:00
|
|
|
.catch(err => console.log(err));
|
2021-03-15 18:57:42 +00:00
|
|
|
},
|
2021-03-16 17:10:37 +00:00
|
|
|
setVolume(volume: DrinkPriceVolume, drink: Drink) {
|
|
|
|
console.log(volume);
|
2021-03-15 22:52:40 +00:00
|
|
|
axios
|
2021-03-16 17:10:37 +00:00
|
|
|
.post(`pricelist/drinks/${drink.id}/volumes`, { ...volume, volume: volume.volume })
|
2021-03-15 22:52:40 +00:00
|
|
|
.then((response: AxiosResponse<FG.DrinkPriceVolume>) => {
|
2021-03-16 17:10:37 +00:00
|
|
|
const a: DrinkPriceVolume = {
|
|
|
|
...response.data,
|
|
|
|
min_prices: [],
|
|
|
|
_volume: response.data.volume,
|
|
|
|
volume: null
|
|
|
|
};
|
|
|
|
a.volume = create_volume(drink, a);
|
|
|
|
a.min_prices = [
|
|
|
|
{
|
|
|
|
percentage: 100,
|
|
|
|
price: create_min_prices(drink, a, 100)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
percentage: 250,
|
|
|
|
price: create_min_prices(drink, a, 250)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
percentage: 300,
|
|
|
|
price: create_min_prices(drink, a, 300)
|
|
|
|
}
|
|
|
|
];
|
|
|
|
console.log(a);
|
|
|
|
drink.volumes.push(a);
|
2021-03-15 22:52:40 +00:00
|
|
|
})
|
|
|
|
.catch(err => console.warn(err));
|
|
|
|
},
|
2021-03-16 17:10:37 +00:00
|
|
|
updateVolume(volume: DrinkPriceVolume, drink: Drink) {
|
2021-03-15 22:52:40 +00:00
|
|
|
axios
|
2021-03-16 17:10:37 +00:00
|
|
|
.put(`pricelist/volumes/${volume.id}`, { ...volume, volume: volume.volume?.value })
|
|
|
|
.catch(err => console.warn(err));
|
|
|
|
},
|
|
|
|
deleteVolume(volume: FG.DrinkPriceVolume, drink: FG.Drink) {
|
|
|
|
axios
|
|
|
|
.delete(`pricelist/volumes/${volume.id}`)
|
2021-03-15 22:52:40 +00:00
|
|
|
.then(() => {
|
2021-03-16 17:10:37 +00:00
|
|
|
const index = drink.volumes.findIndex(a => a.id === volume.id);
|
|
|
|
if (index > -1) {
|
|
|
|
drink.volumes.splice(index, 1);
|
|
|
|
}
|
2021-03-15 22:52:40 +00:00
|
|
|
})
|
|
|
|
.catch(err => console.warn(err));
|
2021-03-16 22:28:38 +00:00
|
|
|
},
|
|
|
|
getExtraIngredients() {
|
|
|
|
axios
|
|
|
|
.get('pricelist/ingredients/extraIngredients')
|
|
|
|
.then((response: AxiosResponse<FG.ExtraIngredient[]>) => {
|
|
|
|
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<FG.Ingredient>) => {
|
|
|
|
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<FG.Ingredient>) => {
|
|
|
|
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));
|
2021-03-15 22:52:40 +00:00
|
|
|
}
|
2021-03-15 18:57:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getters = {};
|
|
|
|
|
2021-03-16 17:10:37 +00:00
|
|
|
function create_volume(drink: Drink, volume: DrinkPriceVolume) {
|
|
|
|
return computed<number>({
|
|
|
|
get: () => {
|
|
|
|
if (volume.ingredients.some(ingredient => !!ingredient.drink_ingredient)) {
|
|
|
|
let retVal = 0;
|
|
|
|
volume.ingredients.forEach(ingredient => {
|
2021-03-16 22:28:38 +00:00
|
|
|
if (ingredient.drink_ingredient?.volume) {
|
|
|
|
retVal += ingredient.drink_ingredient.volume;
|
2021-03-16 17:10:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
volume._volume = retVal;
|
|
|
|
return retVal;
|
|
|
|
} else {
|
|
|
|
return volume._volume;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
set: val => (volume._volume = val)
|
2021-03-15 22:52:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-16 17:10:37 +00:00
|
|
|
function create_min_prices(drink: Drink, volume: DrinkPriceVolume, percentage: number) {
|
|
|
|
if (drink.cost_price_pro_volume) {
|
|
|
|
if (volume.ingredients.every(ingredient => !!ingredient.drink_ingredient)) {
|
|
|
|
return computed<number>(() => {
|
|
|
|
let retVal = (drink.cost_price_pro_volume || 0) * (volume.volume?.value || 0);
|
2021-03-16 22:28:38 +00:00
|
|
|
volume.ingredients.forEach(ingredient => {
|
|
|
|
if (ingredient.extra_ingredient) {
|
|
|
|
retVal += ingredient.extra_ingredient.price;
|
|
|
|
}
|
2021-03-16 17:10:37 +00:00
|
|
|
});
|
|
|
|
retVal = (retVal * percentage) / 100;
|
|
|
|
return retVal;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return computed<number>(
|
|
|
|
() => ((drink.cost_price_pro_volume || 0) * (volume.volume?.value || 0) * percentage) / 100
|
|
|
|
);
|
2021-03-15 18:57:42 +00:00
|
|
|
}
|
2021-03-16 17:10:37 +00:00
|
|
|
} else {
|
|
|
|
return computed<number>(() => {
|
|
|
|
let retVal = 0;
|
|
|
|
volume.ingredients.forEach(ingredient => {
|
|
|
|
if (ingredient.drink_ingredient) {
|
2021-03-16 22:28:38 +00:00
|
|
|
retVal +=
|
|
|
|
ingredient.drink_ingredient.volume *
|
|
|
|
(ingredient.drink_ingredient.drink_ingredient?.cost_price_pro_volume || 0);
|
2021-03-16 17:10:37 +00:00
|
|
|
}
|
2021-03-16 22:28:38 +00:00
|
|
|
if (ingredient.extra_ingredient) {
|
|
|
|
retVal += ingredient.extra_ingredient.price;
|
2021-03-16 17:10:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log(volume);
|
|
|
|
return (retVal * percentage) / 100;
|
|
|
|
});
|
|
|
|
}
|
2021-03-15 18:57:42 +00:00
|
|
|
}
|
2021-03-16 17:10:37 +00:00
|
|
|
export { create_min_prices, create_volume, DrinkPriceVolume, MinPrice, Drink };
|
2021-03-15 18:57:42 +00:00
|
|
|
export default {
|
|
|
|
state,
|
|
|
|
actions,
|
2021-03-15 22:52:40 +00:00
|
|
|
getters
|
2021-03-15 18:57:42 +00:00
|
|
|
};
|