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
88 changes: 88 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,95 @@ export default {
getSupplier(id) {
return this.suppliers[id] || {}
},
getCurrentProductPriceValue(productId) {
return this.getProduct(productId)?.prices?.[0]?.value ?? null;
},
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 (newPrice.date.equals(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);
},
computePrice(quantity, productId) {
if (!quantity)
return null
const price = this.getCurrentProductPriceValue(productId)
return price ? (Number(quantity) * Number(price)).toFixed(2) : null
},
getRecipiePrice(recipieId) {
const recipie = this.getRecipie(recipieId)
let totalPrice = 0, price = 0
if (recipie && recipie.products) {
recipie.products.forEach(product => {
price = this.$root.computePrice(product.amount, product.id)
if (price && !isNaN(price)) {
totalPrice = Number(totalPrice) + Number(price)
}
});
}
return (totalPrice / recipie.people_count).toFixed(2)
},
getRecipieMissingProductPrices(recipieId) {
const recipie = this.getRecipie(recipieId)
let missingProductsMessage = ""
let count = 0, price = 0

if (recipie && recipie.products) {
recipie.products.forEach(product => {
price = this.$root.computePrice(product.amount, product.id)
if (!price || isNaN(price) || price == 0) {
missingProductsMessage = missingProductsMessage + "- " + this.$root.getProduct(product.id).name + "<br/>"
count++
}
});
}

return missingProductsMessage.length > 0
? count + " product(s) do(es) not have a price : <br/>" + missingProductsMessage
: ''

},
getOrderMissingProductPrices(orderValues) {
let missingProductsMessage = ""
let count = 0

orderValues.forEach(item => {
if (!item.total || isNaN(item.total) || item.total == 0) {
missingProductsMessage = missingProductsMessage + "- " + item.name + "<br/>"
count++
}
});

return missingProductsMessage.length > 0
? count + " product(s) do(es) not have a price : <br/>" + missingProductsMessage
: ''

},
},

}
</script>

Expand Down
45 changes: 42 additions & 3 deletions src/views/products/ProductForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
<InputSupplier v-model="product.supplier_id" />
</div>

<div class="p-field">
<label>Price <span v-if="productPriceDate">({{ productPriceDate }})</span></label>
<div class="p-inputgroup">
<InputNumber v-model="productPriceValue"
placeholder="Price"
:maxFractionDigits="2" />
<span class="p-inputgroup-addon" style="width: 6rem;">€ / {{ product.unit
}}</span>
<Button icon="pi pi-history" class="p-button-text"
v-if="product.id && product.prices && product.prices.length > 0"
@click="$refs.productsPriceHistoryForm.show(this.product)" />
</div>
</div>

<div class="p-field">
<label>Packaging Name / Reference</label>
<InputText v-model="product.packaging_reference" placeholder="Packaging Name / Reference" />
Expand All @@ -38,15 +52,18 @@

<div class="p-field-checkbox w-100 mb-3" v-if="product.packaging_conditioning">
<Checkbox id="convert" v-model="product.packaging_convert_to_piece" :binary="true" />
<label for="convert" class="ms-2">Convert "{{ product.packaging_conditioning }}{{ product.unit
<label for="convert" class="ms-2">Convert "{{ product.packaging_conditioning }}{{
product.unit
}}" to 1 piece in orders</label>
</div>

<div class="p-field w-100 mt-0">
<div class="p-field w-100 mt-0 mb-3">
<label>Storage Areas</label>
<InputCategory type="StorageArea" :multiple="true" v-model="product.storage_area_ids"
placeholder="Storage Areas" />
</div>


</div>

<template #footer>
Expand All @@ -55,6 +72,9 @@
@click="saveProduct" />
</template>
</Dialog>

<ProductsPriceHistory @updatedPrices="handleUpdatedPrices" ref="productsPriceHistoryForm">
</ProductsPriceHistory>
</template>

<script>
Expand All @@ -65,25 +85,36 @@ import Checkbox from 'primevue/checkbox'
import InputUnit from '@/components/InputUnit.vue'
import InputSupplier from '@/components/InputSupplier.vue'
import InputCategory from '@/components/InputCategory.vue'
import Calendar from 'primevue/calendar';
import ProductsPriceHistory from './ProductsPriceHistory.vue'


export default {
components: {
InputUnit, InputSupplier, InputNumber, InputCategory, Divider, Checkbox,
InputUnit, InputSupplier, InputNumber, InputCategory, Divider, Checkbox, Calendar, ProductsPriceHistory,
},
emits: ['created'],
data() {
return {
visible: false,
loading: false,
product: {},
productPriceValue: null,
productPriceDate: null,
}
},
methods: {
show(object = {}) {
this.product = { ...object }
this.visible = true
this.productPriceValue = this.$root.getCurrentProductPriceValue(this.product.id)
this.productPriceDate = this.formatDate(this.$root.getCurrentProductPriceDate(this.product.id))
},
async saveProduct() {
if (this.product.name) {
if (this.productPriceValue == null || !(this.productPriceValue === this.$root.getCurrentProductPriceValue(this.product.id))) {
this.$root.addProductPrice(this.productPriceValue, this.product)
}
if (this.product.id) {
this.dbUpdate('products', this.product)
} else {
Expand All @@ -94,6 +125,14 @@ export default {
this.product = {}
}
},
handleUpdatedPrices(updatedProductPrices) {
this.product.prices = updatedProductPrices
this.productPriceValue = this.$root.getCurrentProductPriceValue(this.product.id)
this.productPriceDate = this.formatDate(this.$root.getCurrentProductPriceDate(this.product.id))
},
formatDate(dateString) {
return dateString ? new Intl.DateTimeFormat('default', { dateStyle: 'short' }).format(new Date(dateString)) : null;
},
},
watch: {
'product.packaging_conditioning': function (newVal, oldVal) {
Expand Down
23 changes: 21 additions & 2 deletions src/views/products/ProductsIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@
</template>
</Column>

<!-- Price -->
<Column field="price" header="Price" style="max-width: 10rem">
<template #body="{ data }">
<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>
</Column>


<!-- Actions -->
<Column class="text-end" style="max-width: 40px" header="Actions">
<template #body="{ data }">
Expand Down Expand Up @@ -90,20 +103,23 @@
<script>
import { FilterMatchMode } from 'primevue/api'
import Chip from 'primevue/chip'
import InputText from 'primevue/inputtext'
import InputNumber from 'primevue/inputnumber'
import ProductForm from './ProductForm.vue'
import InputSupplier from '@/components/InputSupplier.vue'
import InputCategory from '@/components/InputCategory.vue'
import RecipieForm from '@/views/recipies/RecipieForm.vue'

export default {
components: {
ProductForm, RecipieForm, InputSupplier, InputCategory, Chip,
ProductForm, RecipieForm, InputSupplier, InputCategory, Chip, InputText, InputNumber,
},
data() {
return {
loading: false,
filters: {},
productsChanged: [],
productPriceValue: null,
}
},
created() {
Expand Down Expand Up @@ -148,7 +164,10 @@ 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
Loading