Corretto Ordini e visualizzazione dei Totali

This commit is contained in:
Surya Paolo
2023-12-15 21:50:28 +01:00
parent 6a1a1fb249
commit 8399df8663
16 changed files with 376 additions and 170 deletions

View File

@@ -24,6 +24,29 @@ export const useProducts = defineStore('Products', {
return state.products
},
updateDataProduct: (state: IProductsState) => (res: any) => {
if (res && res.data.product) {
// Update product from server
const indelem = state.products.findIndex((prod: IProduct) => prod._id === res.data.product._id)
if (indelem >= 0) {
state.products[indelem] = { ...res.data.product }
}
}
if (res && res.data.orders) {
state.orders = res.data.orders
}
if (res && res.data.cart) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
state.cart = res.data.cart
}
},
getProduct: (state: IProductsState) => (code: string): IProduct => {
const prod = state.products.find((prod: IProduct) => prod.code === code)
return prod ? prod : { storehouses: [] }
},
getCart: (state: IProductsState) => (): ICart => {
return state.cart
},
@@ -69,6 +92,55 @@ export const useProducts = defineStore('Products', {
return null
},
getOrderProductInOrdersCart: (state: IProductsState) => (idordercart: string, idproduct: string): IOrder | null => {
// console.log('.cart.items', this.cart.items)
if (state.orders) {
const orderscart = state.orders.find((rec: IOrderCart) => rec._id === idordercart)
if (orderscart) {
const ris = orderscart.items!.find((item: IBaseOrder) => item.order.idProduct === idproduct)
return ris ? ris.order : null
}
}
return null
},
getSumQtyOrderProductInOrdersCart: (state: IProductsState) => (idproduct: string): number => {
let totalQuantity = 0;
if (state.orders) {
const orderscart = state.orders
if (orderscart) {
for (const myord of orderscart) {
myord.items?.forEach((item: IBaseOrder) => {
if (item.order) {
if (item.order.idProduct === idproduct) {
totalQuantity += item.order.quantity || 0;
}
}
});
}
}
}
return totalQuantity
},
getOrdersCartByIdProduct: (state: IProductsState) => (idproduct: string): IOrderCart[] | [] => {
// console.log('.cart.items', this.cart.items)
try {
if (state.orders) {
const ris = state.orders.filter((ordercart: IOrderCart) => {
return ordercart.items!.some(item => {
if (item.order)
return item.order.idProduct === idproduct
})
})
return ris ? ris : []
}
} catch (e) {
console.error('Err', e)
}
return []
},
getRecordEmpty: (state: IProductsState) => (): IProduct => {
const tomorrow = tools.getDateNow()
@@ -129,7 +201,7 @@ export const useProducts = defineStore('Products', {
quantity: order.quantity,
idStorehouse: order.idStorehouse,
}
if (product.storehouses.length === 1) {
@@ -165,17 +237,21 @@ export const useProducts = defineStore('Products', {
let ris = null
ris = await Api.SendReq('/products', 'POST', null)
ris = await Api.SendReq('/products', 'POST', { userId: userStore.my._id })
.then((res) => {
if (res.data.products) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
if (res.data.products) {
this.products = res.data.products
} else {
this.products = []
}
// console.log('ARRAY PRODUCTS = ', this.products)
if (res.data.orders) {
this.orders = res.data.orders
} else {
this.orders = []
}
if (process.env.DEBUG === '1') {
// console.log('dbLoad', 'this.products', this.products)
}
return res
@@ -212,6 +288,7 @@ export const useProducts = defineStore('Products', {
.then((res) => {
console.log('product', res.data.product)
if (res.data.product) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
this.updateDataProduct(res)
return res.data.product
} else {
return null
@@ -249,6 +326,8 @@ export const useProducts = defineStore('Products', {
this.cart = { items: [], totalPrice: 0, totalQty: 0, userId: '' }
}
this.updateDataProduct(res)
return res
})
.catch((error) => {
@@ -261,17 +340,14 @@ export const useProducts = defineStore('Products', {
return ris
},
async removeFromCart({ order }: { order: IOrder }) {
const userStore = useUserStore()
return Api.SendReq('/cart/' + userStore.my._id, 'DELETE', { orderId: order._id })
.then((res) => {
if (res.data.cart) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
this.cart = res.data.cart
} else {
this.cart = { items: [], totalPrice: 0, totalQty: 0, userId: '' }
}
this.updateDataProduct(res)
return res
})
@@ -308,8 +384,11 @@ export const useProducts = defineStore('Products', {
addqty,
subqty: !addqty,
order: ordcart,
}).then((newqty) => {
order.quantity = newqty
}).then((res: any) => {
if (res && res.risult) {
order.quantity = res.qty
}
return res;
})
}
} else {
@@ -333,8 +412,9 @@ export const useProducts = defineStore('Products', {
} else {
this.cart = { items: [], totalPrice: 0, totalQty: 0, userId: '' }
}
this.updateDataProduct(res)
return res
return { risult: !!res, qty: order.quantity }
})
.catch((error) => {
console.log('error addToCart', error)
@@ -361,15 +441,9 @@ export const useProducts = defineStore('Products', {
ris = await Api.SendReq('/cart/' + userStore.my._id, 'POST', { addqty, subqty, order })
.then((res) => {
this.cart = res.data.cart
if (!!res.data.qty) {
// const ind = this.cart.items.findIndex((rec) => rec.order._id === order._id)
// this.cart.items[ind].order.quantity = res.data.qty
this.updateDataProduct(res)
return res.data.qty
}
return 0
return { risult: !!res, qty: res.data.qty }
})
.catch((error) => {
console.log('error addSubQtyToItem', error)
@@ -382,7 +456,7 @@ export const useProducts = defineStore('Products', {
return ris
},
async CreateOrdersCart({ cart_id, status }: { cart_id: string, status: number }) {
async CreateOrdersCart({ cart_id, status, note }: { cart_id: string, status: number, note: string }) {
const userStore = useUserStore()
const globalStore = useGlobalStore()
@@ -392,15 +466,15 @@ export const useProducts = defineStore('Products', {
let ris = null
ris = await Api.SendReq('/cart/' + userStore.my._id + '/createorderscart', 'POST', { cart_id, status })
ris = await Api.SendReq('/cart/' + userStore.my._id + '/createorderscart', 'POST', { cart_id, status, note })
.then((res) => {
if (res.data.status === shared_consts.OrderStatus.CHECKOUT_SENT) {
// Cancella il Carrello, ho creato l'ordine !
this.cart = {}
}
if (res.data.orders)
this.orders = res.data.orders
this.updateDataProduct(res)
return res.data.recOrderCart
})
@@ -429,8 +503,7 @@ export const useProducts = defineStore('Products', {
if (res.data.status === shared_consts.OrderStatus.CHECKOUT_SENT) {
this.cart = {}
}
if (res.data.orders)
this.orders = res.data.orders
this.updateDataProduct(res)
return res.data.status
})
@@ -455,8 +528,8 @@ export const useProducts = defineStore('Products', {
ris = await Api.SendReq('/cart/' + userStore.my._id + '/ordercartstatus', 'POST', { order_id, status })
.then((res) => {
if (res.data.orders)
this.orders = res.data.orders
this.updateDataProduct(res)
return res.data.status