Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6 ajout dun objet prix sur chaque produit #10

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
39 changes: 39 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,45 @@ export default {
getCurrentProductPriceDate(productId) {
return this.getProduct(productId)?.prices?.[0]?.date ?? null;
},
addProductPrice(price, product) {
if (!Array.isArray(product.prices)) {
product.prices = [];
}
// Convert price.date from database which is String to javascript Date
product.prices.forEach(price => {
price.date = new Date(price.date)
});

const newPrice = {
date: new Date(),
value: price,
};

const mostRecentPrice = product.prices[0]

if (this.isSameDay(newPrice.date, mostRecentPrice.date)) {
mostRecentPrice.value = newPrice.value;
} else {
product.prices.push(newPrice);
}

product.prices = product.prices
.filter((p) => p.date)
.sort((a, b) => b.date - a.date);
},
isSameDay(date1, date2) {
Focus-Pacifique marked this conversation as resolved.
Show resolved Hide resolved
return (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
);
},
isFutureDate(date) {
const today = new Date()
today.setHours(0, 0, 0, 0)
date.setHours(0, 0, 0, 0)
return date > today
},
computePrice(quantity, productId) {
if (!quantity)
return null
Expand Down
19 changes: 1 addition & 18 deletions src/views/products/ProductForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default {
async saveProduct() {
if (this.product.name) {
if (this.productPriceValue == null || !(this.productPriceValue === this.$root.getCurrentProductPriceValue(this.product.id))) {
this.addProductPrice(this.productPriceValue, this.product)
this.$root.addProductPrice(this.productPriceValue, this.product)
}
if (this.product.id) {
this.dbUpdate('products', this.product)
Expand All @@ -125,23 +125,6 @@ export default {
this.product = {}
}
},
addProductPrice(price, product) {
if (!Array.isArray(product.prices)) {
product.prices = [];
}
const newPrice = {
date: new Date(),
value: price,
};

const mostRecentPrice = product.prices[0]

if (mostRecentPrice) {
mostRecentPrice.value = newPrice.value;
} else {
product.prices.push(newPrice);
}
},
handleUpdatedPrices(updatedProductPrices) {
this.product.prices = updatedProductPrices
this.productPriceValue = this.$root.getCurrentProductPriceValue(this.product.id)
Expand Down
4 changes: 4 additions & 0 deletions src/views/products/ProductsIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<InputNumber
v-if="data && data.prices && data.prices.length > 0"
v-model="data.prices[0].value"
@blur="handleBlur(data)"
:maxFractionDigits="2" class="w-50" />
<div class="w-50">{{ "€/" + data.unit }}</div>
</template>
Expand Down Expand Up @@ -163,6 +164,9 @@ export default {
recipiesUsingProduct(product) {
return this.$root.recipiesArray.filter((r) => r.products.some((p) => p.id == product.id))
},
handleBlur(product) {
this.$root.addProductPrice(product.prices[0].value, product)
},
}
}
</script>
Expand Down
11 changes: 11 additions & 0 deletions src/views/products/ProductsPriceHistory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ export default {
});
return;
}
for (const element of this.product.prices) {
if (this.$root.isFutureDate(element.date)) {
this.$toast.add({
severity: 'error',
summary: 'Error',
detail: `Dates can not be in the future`,
life: 4000,
});
return;
}
}
if (this.product.id) {
await this.dbUpdate('products', this.product)
this.$emit('updatedPrices', this.product.prices)
Expand Down