import { api } from 'src/boot/axios'; import { defineStore } from 'pinia'; import { AxiosResponse } from 'axios'; import { calc_volume, calc_cost_per_volume, calc_all_min_prices, } from 'src/plugins/pricelist/utils/utils'; interface DrinkPriceVolume extends Omit { _volume: number; volume?: number; } interface Drink extends Omit, 'volumes'> { volumes: DrinkPriceVolume[]; cost_per_volume?: number; _cost_per_volume?: number; } class DrinkPriceVolume implements DrinkPriceVolume { constructor({ id, volume, prices, ingredients }: FG.DrinkPriceVolume) { this.id = id; this._volume = volume; this.prices = prices; this.ingredients = ingredients; this.min_prices = []; this.volume = calc_volume(this); } } class Drink { constructor({ id, article_id, package_size, name, volume, cost_per_volume, cost_per_package, tags, type, uuid, receipt, }: FG.Drink) { this.id = id; this.article_id = article_id; this.package_size = package_size; this.name = name; this.volume = volume; this.cost_per_package = cost_per_package; this._cost_per_volume = cost_per_volume; this.cost_per_volume = calc_cost_per_volume(this); this.tags = tags; this.type = type; this.volumes = []; this.uuid = uuid; this.receipt = receipt || []; } } export const usePricelistStore = defineStore({ id: 'pricelist', state: () => ({ drinkTypes: [] as Array, drinks: [] as Array, extraIngredients: [] as Array, pricecalc_columns: [] as Array, min_prices: [] as Array, tags: [] as Array, }), actions: { async getDrinkTypes(force = false) { if (force || this.drinks.length == 0) { const { data } = await api.get>('/pricelist/drink-types'); this.drinkTypes = data; } return this.drinkTypes; }, async addDrinkType(name: string) { const { data } = await api.post('/pricelist/drink-types', { name: name }); this.drinkTypes.push(data); }, async removeDrinkType(id: number) { await api.delete(`/pricelist/drink-types/${id}`); const idx = this.drinkTypes.findIndex((val) => val.id == id); if (idx >= 0) this.drinkTypes.splice(idx, 1); }, async changeDrinkTypeName(drinkType: FG.DrinkType) { await api.put(`/pricelist/drink-types/${drinkType.id}`, drinkType); const itm = this.drinkTypes.filter((val) => val.id == drinkType.id); if (itm.length > 0) itm[0].name = drinkType.name; }, async getExtraIngredients() { const { data } = await api.get>( 'pricelist/ingredients/extraIngredients' ); this.extraIngredients = data; }, async setExtraIngredient(ingredient: FG.ExtraIngredient) { const { data } = await api.post( 'pricelist/ingredients/extraIngredients', ingredient ); this.extraIngredients.push(data); }, async updateExtraIngredient(ingredient: FG.ExtraIngredient) { const { data } = await api.put( `pricelist/ingredients/extraIngredients/${ingredient.id}`, ingredient ); const index = this.extraIngredients.findIndex((a) => a.id === ingredient.id); if (index > -1) { this.extraIngredients[index] = data; } else { this.extraIngredients.push(data); } }, async deleteExtraIngredient(ingredient: FG.ExtraIngredient) { await api.delete(`pricelist/ingredients/extraIngredients/${ingredient.id}`); const index = this.extraIngredients.findIndex((a) => a.id === ingredient.id); if (index > -1) { this.extraIngredients.splice(index, 1); } }, async getDrinks() { const { data } = await api.get>('pricelist/drinks'); this.drinks = []; data.forEach((drink) => { const _drink = new Drink(drink); drink.volumes.forEach((volume) => { const _volume = new DrinkPriceVolume(volume); _drink.volumes.push(_volume); }); this.drinks.push(_drink); }); calc_all_min_prices(this.drinks, this.min_prices); console.log(this.drinks); }, sortPrices(volume: DrinkPriceVolume) { volume.prices.sort((a, b) => { if (a.price > b.price) return 1; if (b.price > a.price) return -1; return 0; }); }, async deletePrice(price: FG.DrinkPrice) { /*api .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));*/ await api.delete(`pricelist/prices/${price.id}`); }, async deleteVolume(volume: DrinkPriceVolume, drink: Drink) { await api.delete(`pricelist/volumes/${volume.id}`); const index = drink.volumes.findIndex((a) => a.id === volume.id); if (index > -1) { drink.volumes.splice(index, 1); } }, async deleteIngredient(ingredient: FG.Ingredient) { /*api .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)); */ await api.delete(`pricelist/ingredients/${ingredient.id}`); }, async setDrink(drink: FG.Drink) { const { data } = await api.post('pricelist/drinks', { ...drink, }); const _drink = new Drink(data); data.volumes.forEach((volume) => { const _volume = new DrinkPriceVolume(volume); _drink.volumes.push(_volume); }); this.drinks.push(_drink); calc_all_min_prices(this.drinks, this.min_prices); }, async updateDrink(drink: Drink) { const { data } = await api.put(`pricelist/drinks/${drink.id}`, { ...drink, }); const index = this.drinks.findIndex((a) => a.id === data.id); if (index > -1) { const _drink = new Drink(data); data.volumes.forEach((volume) => { const _volume = new DrinkPriceVolume(volume); _drink.volumes.push(_volume); }); this.drinks[index] = _drink; } calc_all_min_prices(this.drinks, this.min_prices); }, deleteDrink(drink: Drink) { api .delete(`pricelist/drinks/${drink.id}`) .then(() => { const index = this.drinks.findIndex((a) => a.id === drink.id); if (index > -1) { this.drinks.splice(index, 1); } }) .catch((err) => console.warn(err)); }, getPriceCalcColumn(userid: string) { api .get(`pricelist/users/${userid}/pricecalc_columns`) .then(({ data }: AxiosResponse>) => { if (data.length > 0) { this.pricecalc_columns = data; } }) .catch((err) => console.log(err)); }, updatePriceCalcColumn(userid: string, data: Array) { api .put(`pricelist/users/${userid}/pricecalc_columns`, data) .then(() => { this.pricecalc_columns = data; }) .catch((err) => console.log(err)); }, async get_min_prices() { const { data } = await api.get>('pricelist/settings/min_prices'); this.min_prices = data; }, async set_min_prices() { await api.post>('pricelist/settings/min_prices', this.min_prices); calc_all_min_prices(this.drinks, this.min_prices); }, async upload_drink_picture(drink: Drink, file: File) { const formData = new FormData(); formData.append('file', file); const { data } = await api.post(`pricelist/drinks/${drink.id}/picture`, formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); drink.uuid = data.uuid; }, async delete_drink_picture(drink: Drink) { await api.delete(`pricelist/drinks/${drink.id}/picture`); drink.uuid = ''; }, async getTags() { const { data } = await api.get>('/pricelist/tags'); this.tags = data; }, async setTag(tag: FG.Tag) { const { data } = await api.post('/pricelist/tags', tag); this.tags.push(data); }, async updateTag(tag: FG.Tag) { const { data } = await api.put(`/pricelist/tags/${tag.id}`, tag); const index = this.tags.findIndex((a) => a.id === data.id); if (index > -1) { this.tags[index] = data; } }, async deleteTag(tag: FG.Tag) { await api.delete(`/pricelist/tags/${tag.id}`); const index = this.tags.findIndex((a) => a.id === tag.id); if (index > -1) { this.tags.splice(index, 1); } }, }, }); export { DrinkPriceVolume, Drink };