From b40b1064e7e24f7bcc06208448eedee50f6cf45a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Sun, 11 Apr 2021 23:11:43 +0200 Subject: [PATCH] [pricelist][calculation] can add new drink --- .../pricelist/components/CalculationTable.vue | 84 +++++----- .../CalculationTable/BuildManual.vue | 4 +- .../components/CalculationTable/NewDrink.vue | 143 ------------------ .../pricelist/components/DrinkModify.vue | 73 ++++++++- src/plugins/pricelist/store.ts | 22 +-- src/plugins/pricelist/utils/clone.js | 0 6 files changed, 123 insertions(+), 203 deletions(-) delete mode 100644 src/plugins/pricelist/components/CalculationTable/NewDrink.vue delete mode 100644 src/plugins/pricelist/utils/clone.js diff --git a/src/plugins/pricelist/components/CalculationTable.vue b/src/plugins/pricelist/components/CalculationTable.vue index 7098cb2..52db3e6 100644 --- a/src/plugins/pricelist/components/CalculationTable.vue +++ b/src/plugins/pricelist/components/CalculationTable.vue @@ -11,14 +11,6 @@ grid :rows-per-page-options="[0]" > - - + @@ -154,7 +152,6 @@ import { defineComponent, onBeforeMount, ComputedRef, computed, ref } from 'vue' import { useMainStore } from 'src/stores'; import { Drink, usePricelistStore, DrinkPriceVolume } from 'src/plugins/pricelist/store'; import MinPriceSetting from 'src/plugins/pricelist/components/MinPriceSetting.vue'; -import NewDrink from 'src/plugins/pricelist/components/CalculationTable/NewDrink.vue'; import SearchInput from './SearchInput.vue'; import DrinkPriceVolumes from 'src/plugins/pricelist/components/CalculationTable/DrinkPriceVolumes.vue'; import DrinkModify from './DrinkModify.vue'; @@ -168,7 +165,6 @@ export default defineComponent({ components: { SearchInput, MinPriceSetting, - NewDrink, DrinkPriceVolumes, DrinkModify, }, @@ -349,8 +345,11 @@ export default defineComponent({ void store.updateDrink(drink); } - function deleteDrink(drink: Drink) { - store.deleteDrink(drink); + function deleteDrink() { + if (editDrink.value) { + store.deleteDrink(editDrink.value); + } + editDrink.value = undefined; } const showNewDrink = ref(false); @@ -390,7 +389,26 @@ export default defineComponent({ key: '', label: '', }); - const editDrink = ref(); + + const emptyDrink: Drink = { + id: -1, + article_id: undefined, + package_size: undefined, + name: '', + volume: undefined, + cost_per_volume: undefined, + cost_per_package: undefined, + tags: [], + type: undefined, + volumes: [], + uuid: '', + }; + + function newDrink() { + editDrink.value = Object.assign({}, emptyDrink); + } + + const editDrink = ref(); async function editing_drink( drink: Drink, @@ -409,22 +427,19 @@ export default defineComponent({ await store.deleteVolume(volume, drink); } console.log(drink); - await store.updateDrink(drink); - if (deletePic || drinkPic) { - let loading = imageloading.value.find((a) => a.id === drink.id); - if (loading) { - loading.loading = true; - } else { - loading = { id: drink.id, loading: true }; - imageloading.value.push(loading); + if (drink.id > 0) { + await store.updateDrink(drink); + } else { + const _drink = await store.setDrink(drink); + if (editDrink.value) { + editDrink.value.id = _drink.id; } - if (deletePic) { - await deletePicture(); - } - if (drinkPic instanceof File) { - await savePicture(drinkPic); - } - loading.loading = false; + } + if (deletePic) { + await deletePicture(); + } + if (drinkPic instanceof File) { + await savePicture(drinkPic); } editDrink.value = undefined; notLoading.value = true; @@ -469,6 +484,7 @@ export default defineComponent({ get_volumes, notLoading, getImageLoading, + newDrink, }; }, }); diff --git a/src/plugins/pricelist/components/CalculationTable/BuildManual.vue b/src/plugins/pricelist/components/CalculationTable/BuildManual.vue index 4d462bb..f4fbb68 100644 --- a/src/plugins/pricelist/components/CalculationTable/BuildManual.vue +++ b/src/plugins/pricelist/components/CalculationTable/BuildManual.vue @@ -20,8 +20,8 @@ />
- - + +
diff --git a/src/plugins/pricelist/components/CalculationTable/NewDrink.vue b/src/plugins/pricelist/components/CalculationTable/NewDrink.vue deleted file mode 100644 index 5a638ee..0000000 --- a/src/plugins/pricelist/components/CalculationTable/NewDrink.vue +++ /dev/null @@ -1,143 +0,0 @@ - - - - - diff --git a/src/plugins/pricelist/components/DrinkModify.vue b/src/plugins/pricelist/components/DrinkModify.vue index dd46aa4..c155f4a 100644 --- a/src/plugins/pricelist/components/DrinkModify.vue +++ b/src/plugins/pricelist/components/DrinkModify.vue @@ -3,6 +3,26 @@
Getränk Bearbeiten
+ +
+ + +
+
@@ -90,9 +110,11 @@ dense /> + @@ -123,7 +146,7 @@ import { defineComponent, PropType, ref, onBeforeMount, computed } from 'vue'; import { Drink, DrinkPriceVolume, usePricelistStore } from '../store'; import DrinkPriceVolumes from './CalculationTable/DrinkPriceVolumes.vue'; -import { clone, calc_min_prices, DeleteObjects } from '../utils/utils'; +import { clone, calc_min_prices, DeleteObjects, calc_cost_per_volume } from '../utils/utils'; import BuildManual from 'src/plugins/pricelist/components/CalculationTable/BuildManual.vue'; import config from 'src/config'; export default defineComponent({ @@ -141,7 +164,8 @@ export default defineComponent({ toDeleteObjects: DeleteObjects, drinkPic: File | undefined, deletePic: boolean - ) => drink && toDeleteObjects || drinkPic || deletePic, + ) => (drink && toDeleteObjects) || drinkPic || deletePic, + delete: () => true, cancel: () => true, }, setup(props, { emit }) { @@ -235,6 +259,42 @@ export default defineComponent({ return 'no-image.svg'; }); + const can_delete = computed(() => { + if (edit_drink.value) { + if (edit_drink.value.id < 0) { + return false; + } + const _edit_drink = edit_drink.value; + const test = _edit_drink.volumes ? _edit_drink.volumes.length === 0 : true; + console.log(test); + return test; + } + return false; + }); + + function delete_drink() { + emit('delete'); + } + + const auto_cost_per_volume = computed( + () => + !!( + edit_drink.value?.cost_per_package && + edit_drink.value?.package_size && + edit_drink.value?.volume + ) + ); + + const cost_per_volume = computed({ + get: () => { + if (auto_cost_per_volume.value) { + return calc_cost_per_volume(edit_drink.value); + } + return (edit_drink.value).cost_per_volume; + }, + set: (val: number) => ((edit_drink.value).cost_per_volume = val), + }); + return { edit_drink, save, @@ -251,6 +311,11 @@ export default defineComponent({ drinkPic, imagePreview, delete_pic, + types: computed(() => store.drinkTypes), + can_delete, + delete_drink, + auto_cost_per_volume, + cost_per_volume, }; }, }); diff --git a/src/plugins/pricelist/store.ts b/src/plugins/pricelist/store.ts index b44979d..86ebc66 100644 --- a/src/plugins/pricelist/store.ts +++ b/src/plugins/pricelist/store.ts @@ -147,15 +147,6 @@ export const usePricelistStore = defineStore({ }); }, 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) { @@ -166,19 +157,9 @@ export const usePricelistStore = defineStore({ } }, 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) { + async setDrink(drink: Drink) { const { data } = await api.post('pricelist/drinks', { ...drink, }); @@ -189,6 +170,7 @@ export const usePricelistStore = defineStore({ }); this.drinks.push(_drink); calc_all_min_prices(this.drinks, this.min_prices); + return _drink; }, async updateDrink(drink: Drink) { const { data } = await api.put(`pricelist/drinks/${drink.id}`, { diff --git a/src/plugins/pricelist/utils/clone.js b/src/plugins/pricelist/utils/clone.js deleted file mode 100644 index e69de29..0000000