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[];
|
|
|
|
}
|
2021-03-17 20:36:26 +00:00
|
|
|
interface Drink extends Omit<Omit<FG.Drink, 'cost_price_pro_volume'>, 'volumes'> {
|
2021-03-16 17:10:37 +00:00
|
|
|
volumes: DrinkPriceVolume[];
|
2021-03-17 20:36:26 +00:00
|
|
|
cost_price_pro_volume: WritableComputedRef<number | undefined>;
|
|
|
|
_cost_price_pro_volume?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DrinkPriceVolume {
|
|
|
|
constructor({ id, volume, min_prices, prices, ingredients }: FG.DrinkPriceVolume, drink: Drink) {
|
|
|
|
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.volume = computed<number>({
|
|
|
|
get: () => {
|
|
|
|
if (this.ingredients.some(ingredient => !!ingredient.drink_ingredient)) {
|
|
|
|
let retVal = 0;
|
|
|
|
this.ingredients.forEach(ingredient => {
|
|
|
|
if (ingredient.drink_ingredient?.volume) {
|
|
|
|
retVal += ingredient.drink_ingredient.volume;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this._volume = retVal;
|
|
|
|
return retVal;
|
|
|
|
} else {
|
|
|
|
return this._volume;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
set: val => (this._volume = val)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Drink {
|
|
|
|
constructor({
|
|
|
|
id,
|
|
|
|
article_id,
|
|
|
|
package_size,
|
|
|
|
name,
|
|
|
|
volume,
|
|
|
|
cost_price_pro_volume,
|
|
|
|
cost_price_package_netto,
|
|
|
|
tags,
|
|
|
|
type,
|
|
|
|
volumes
|
|
|
|
}: FG.Drink) {
|
|
|
|
this.id = id;
|
|
|
|
this.article_id = article_id;
|
|
|
|
this.package_size = package_size;
|
|
|
|
this.name = name;
|
|
|
|
this.volume = volume;
|
|
|
|
this.cost_price_package_netto = cost_price_package_netto;
|
|
|
|
this._cost_price_pro_volume = cost_price_pro_volume;
|
|
|
|
this.cost_price_pro_volume = computed({
|
|
|
|
get: () => {
|
|
|
|
if (!!this.volume && !!this.package_size && !!this.cost_price_package_netto) {
|
|
|
|
const retVal =
|
|
|
|
((this.cost_price_package_netto || 0) /
|
|
|
|
((this.volume || 0) * (this.package_size || 0))) *
|
|
|
|
1.19;
|
|
|
|
this._cost_price_pro_volume = Math.round(retVal * 1000) / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._cost_price_pro_volume;
|
|
|
|
},
|
|
|
|
set: val => (this._cost_price_pro_volume = val)
|
|
|
|
});
|
|
|
|
this.tags = tags;
|
|
|
|
this.type = type;
|
|
|
|
this.volumes = [];
|
|
|
|
/*volumes.forEach(volume => {
|
|
|
|
this.volumes.push(new DrinkPriceVolume(volume, this));
|
|
|
|
});*/
|
|
|
|
}
|
2021-03-16 17:10:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 18:57:42 +00:00
|
|
|
const actions = {
|
|
|
|
getDrinks() {
|
|
|
|
axios
|
|
|
|
.get('pricelist/drinks')
|
|
|
|
.then((response: AxiosResponse<FG.Drink[]>) => {
|
2021-03-17 20:36:26 +00:00
|
|
|
state.drinks = [];
|
2021-03-16 17:10:37 +00:00
|
|
|
response.data.forEach(drink => {
|
2021-03-17 20:36:26 +00:00
|
|
|
state.drinks.push(new Drink(drink));
|
|
|
|
});
|
|
|
|
state.drinks.forEach(drink => {
|
|
|
|
const _drink = response.data.find(a => a.id === drink.id);
|
|
|
|
_drink?.volumes.forEach(volume => {
|
|
|
|
drink.volumes.push(new DrinkPriceVolume(volume, drink));
|
2021-03-16 17:10:37 +00:00
|
|
|
});
|
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-17 20:36:26 +00:00
|
|
|
drink.volumes.push(new DrinkPriceVolume(response.data, drink));
|
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-17 20:36:26 +00:00
|
|
|
},
|
|
|
|
getDrinkTypes() {
|
|
|
|
axios
|
|
|
|
.get('pricelist/drink-types')
|
|
|
|
.then((response: AxiosResponse<FG.DrinkType[]>) => {
|
|
|
|
state.drinkTypes = response.data;
|
|
|
|
})
|
|
|
|
.catch(err => console.warn(err));
|
|
|
|
},
|
|
|
|
setDrink(drink: FG.Drink) {
|
|
|
|
axios
|
|
|
|
.post('pricelist/drinks', drink)
|
|
|
|
.then((response: AxiosResponse<FG.Drink>) => {
|
|
|
|
state.drinks.push(new Drink(response.data));
|
|
|
|
const drink = state.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));
|
|
|
|
},
|
|
|
|
updateDrink(drink: Drink) {
|
|
|
|
axios
|
|
|
|
.put(`pricelist/drinks/${drink.id}`, {
|
|
|
|
...drink,
|
|
|
|
cost_price_pro_volume: drink.cost_price_pro_volume?.value
|
|
|
|
})
|
|
|
|
.catch(err => console.warn(err));
|
|
|
|
},
|
|
|
|
deleteDrink(drink: Drink) {
|
|
|
|
axios
|
|
|
|
.delete(`pricelist/drinks/${drink.id}`)
|
|
|
|
.then(() => {
|
|
|
|
const index = state.drinks.findIndex(a => a.id === drink.id);
|
|
|
|
if (index > -1) {
|
|
|
|
state.drinks.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_min_prices(drink: Drink, volume: DrinkPriceVolume, percentage: number) {
|
2021-03-17 20:36:26 +00:00
|
|
|
if (drink.cost_price_pro_volume?.value) {
|
2021-03-16 17:10:37 +00:00
|
|
|
if (volume.ingredients.every(ingredient => !!ingredient.drink_ingredient)) {
|
|
|
|
return computed<number>(() => {
|
2021-03-17 20:36:26 +00:00
|
|
|
let retVal = (drink.cost_price_pro_volume?.value || 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>(
|
2021-03-17 20:36:26 +00:00
|
|
|
() =>
|
|
|
|
((drink.cost_price_pro_volume?.value || 0) * (volume.volume?.value || 0) * percentage) /
|
|
|
|
100
|
2021-03-16 17:10:37 +00:00
|
|
|
);
|
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-17 20:36:26 +00:00
|
|
|
export { create_min_prices, 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
|
|
|
};
|