possibilità di modificare un ordine, e anche i totali

This commit is contained in:
Surya Paolo
2024-01-15 22:19:26 +01:00
parent 79ca364e84
commit 3183825137
7 changed files with 121 additions and 49 deletions

View File

@@ -17,7 +17,7 @@ class Cart {
this.items.push(ord)
}
}
this.updatetotals();
this.updatecarttotals(false);
}
initializeFromOrder(order) {
@@ -62,7 +62,7 @@ class Cart {
} else {
return true;
}
}
@@ -79,7 +79,7 @@ class Cart {
else if (myitem.order.quantity >= 20)
step = stepmin < 5 ? 5 : stepmin
myitem.order.quantity += step;
myitem.order.quantity += step;
} else {
if (myitem.order.quantitypreordered === 0)
step = myitem.order.product.minBuyQty ?? stepmin
@@ -88,12 +88,14 @@ class Cart {
else if (myitem.order.quantitypreordered >= 20)
step = stepmin < 5 ? 5 : stepmin
myitem.order.quantitypreordered += step;
myitem.order.quantitypreordered += step;
}
myitem.order.modify_at = new Date();
this.updatetotals();
myitem.order = Order.updateTotals(myitem.order);
this.updatecarttotals(false);
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order;
}
@@ -105,7 +107,7 @@ class Cart {
if (myproduct.quantityAvailable > 0) {
if (myorder.quantity === minqta)
step = minqta
else{
else {
if ((myorder.quantity - step) < 0)
step = myorder.quantity - step
}
@@ -130,7 +132,9 @@ class Cart {
myitem.order.quantity -= step;
}
}
this.updatetotals();
myitem.order = Order.updateTotals(myitem.order);
this.updatecarttotals(false);
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order;
}
@@ -147,7 +151,7 @@ class Cart {
this.items[ind] = {};
this.items[ind].order = itemorder;
this.items[ind].order = Order.updateTotals(this.items[ind].order);
this.updatetotals();
this.updatecarttotals(false);
return ind;
}
@@ -155,7 +159,7 @@ class Cart {
removeItem(orderId) {
// this.items.push(itemorder);
this.items = this.items.filter(item => item.order._id.toString() !== orderId.toString());
this.updatetotals();
this.updatecarttotals(false);
}
generateModel() {
@@ -164,6 +168,7 @@ class Cart {
idapp: this.idapp,
items: this.generateArray(),
totalQty: this.totalQty,
totalPriceCalc: this.totalPriceCalc,
totalPrice: this.totalPrice,
userId: this.userId,
department: this.department,
@@ -177,10 +182,11 @@ class Cart {
return null;
}
updatetotals() {
updatecarttotals(updatecalcprice) {
try {
this.totalQty = 0;
this.totalPrice = 0;
this.totalPriceCalc = 0;
for (const rec in this.items) {
let mypricecalc = 0;
@@ -188,8 +194,11 @@ class Cart {
if (!order) {
order = this.items[rec];
}
order.TotalPriceProduct = 0;
this.totalQty += order.quantity + order.quantitypreordered;
order.TotalPriceProductCalc = 0;
if (updatecalcprice) {
order.TotalPriceProduct = 0;
this.totalQty += order.quantity + order.quantitypreordered;
}
// Calcolo Sconto
let sconti_da_applicare = [];
@@ -233,14 +242,21 @@ class Cart {
if (qtanonscontata > 0) {
mypricecalc += order.price * qtanonscontata;
}
} else {
mypricecalc = (order.price * order.quantity) + (order.price * order.quantitypreordered);
order.TotalPriceProductCalc += mypricecalc;
if (updatecalcprice) {
order.TotalPriceProduct += mypricecalc;
}
}
order.TotalPriceProduct += mypricecalc;
// Qui lo calcolo sempre, anche se lo cambio manualmente
this.totalPriceCalc += mypricecalc;
this.totalPrice += order.TotalPriceProduct;
}
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
} catch (e) {
console.error('Err: ', e);
}