Aggiungi il codice alla cassa in automatico

This commit is contained in:
Surya Paolo
2023-12-18 15:21:12 +01:00
parent aaa1f619a5
commit 6d4f3f9f0b
14 changed files with 145 additions and 59 deletions

View File

@@ -42,7 +42,12 @@ export const useProducts = defineStore('Products', {
},
getProduct: (state: IProductsState) => (code: string): IProduct => {
getProductById: (state: IProductsState) => (id: string): IProduct => {
const prod = state.products.find((prod: IProduct) => prod._id === id)
return prod ? prod : { active: false, img: '', code: '', name: '', storehouses: [], scontisticas: [], price: 0 }
},
getProductByCode: (state: IProductsState) => (code: string): IProduct => {
const prod = state.products.find((prod: IProduct) => prod.code === code)
return prod ? prod : { active: false, img: '', code: '', name: '', storehouses: [], scontisticas: [], price: 0 }
},
@@ -114,7 +119,7 @@ export const useProducts = defineStore('Products', {
if (myord.items) {
for (const item of myord.items) {
if (item.order) {
if (item.order.idProduct === idproduct) {
if ((item.order.idProduct === idproduct) && (item.order.status! < shared_consts.OrderStatus.CHECKOUT_SENT)) {
totalQuantity += item.order.quantity || 0;
}
}
@@ -132,7 +137,8 @@ export const useProducts = defineStore('Products', {
const ris = state.orders.filter((ordercart: IOrderCart) => {
return ordercart.items!.some(item => {
if (item.order)
return item.order.idProduct === idproduct
return (item.order.idProduct === idproduct)
&& (item.order.status! < shared_consts.OrderStatus.CHECKOUT_SENT)
})
})
// console.log('Ordini ', ris)
@@ -432,6 +438,14 @@ export const useProducts = defineStore('Products', {
if (order.quantity === 0)
order.quantity = 1
if (!order.idStorehouse) {
if (product.storehouses.length === 1) {
order.idStorehouse = product.storehouses[0]._id
} else {
order.idStorehouse = globalStore.storehouses ? globalStore.storehouses[0]._id : ''
}
}
if (order.idStorehouse) {
neworder = this.createOrderByProduct(product, order)
}
@@ -581,6 +595,38 @@ export const useProducts = defineStore('Products', {
return ris
},
async addtoCartBase({ $q, t, code, order, addqty }: { $q: any, t: any, code: string, order: IOrder, addqty: boolean }) {
let product = this.getProductByCode(code)
return this.addToCart({ product, order, addqty }).then((ris) => {
let strprod = t('ecomm.prodotto')
if (order.quantity > 1 || order.quantity === 0)
strprod = t('ecomm.prodotti')
let msg = ''
if (ris === null)
msg = t('ecomm.error_cart')
else {
if (order.quantity === 0) {
msg = t('ecomm.prodotto_tolto')
} else {
msg = t('ecomm.prod_sul_carrello', { strprod, qty: order.quantity })
}
}
this.updateQuantityAvailable(product._id)
if (ris === null || order.quantity === 0)
tools.showNotif($q, msg)
else
tools.showPositiveNotif($q, msg)
})
}
},
})