Carrello con scontistica aggiornata

This commit is contained in:
Surya Paolo
2023-12-18 12:11:12 +01:00
parent 56b5bac5f0
commit 7628fb97fc
7 changed files with 154 additions and 66 deletions

View File

@@ -38,6 +38,7 @@ class Cart {
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
if (!!myitem) {
myitem.order.quantity++;
myitem.order = Order.updateTotals(myitem.order);
this.updatetotals();
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order.quantity;
@@ -49,6 +50,7 @@ class Cart {
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
if (!!myitem && myitem.order.quantity > 0) {
myitem.order.quantity--;
myitem.order = Order.updateTotals(myitem.order);
this.updatetotals();
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order.quantity;
@@ -64,6 +66,7 @@ class Cart {
let ind = this.items.length;
this.items[ind] = {};
this.items[ind].order = itemorder;
this.items[ind].order = Order.updateTotals(this.items[ind].order);
this.updatetotals();
return ind;
@@ -86,6 +89,7 @@ class Cart {
note: this.note,
modify_at: this.modify_at
})
return newCart
}
@@ -93,64 +97,12 @@ class Cart {
try {
this.totalQty = 0;
this.totalPrice = 0;
for (const rec in this.items) {
let mypricecalc = 0;
let order = this.items[rec].order;
if (!order) {
order = this.items[rec];
if (this.items && this.items.length > 0) {
for (const rec of this.items) {
let ord = Order.updateTotals(rec.order);
this.totalQty += ord.quantity;
this.totalPrice += ord.TotalPriceProduct;
}
order.TotalPriceProduct = 0;
this.totalQty += order.quantity;
// Calcolo Sconto
let sconti_da_applicare = [];
if (order.scontisticas) {
let qtadascontare = order.quantity
let qtanonscontata = 0
while (qtadascontare > 0) {
let scontoapplicato = null
for (const sconto of order.scontisticas.filter((rec) => !rec.cumulativo)) {
if (qtadascontare >= sconto.qta) {
scontoapplicato = sconto
scontoapplicato.qtadascontare = sconto.qta
}
}
if (scontoapplicato && scontoapplicato.qtadascontare > 0) {
sconti_da_applicare.push(scontoapplicato)
qtadascontare -= scontoapplicato.qtadascontare
} else {
qtanonscontata = qtadascontare
qtadascontare = 0
}
}
/*for (const sconto of order.scontisticas.filter((rec) => rec.cumulativo)) {
if ((sconto.qta % order.quantity) === 0) {
sconti_da_applicare.push(sconto)
}
}*/
if (sconti_da_applicare.length > 0) {
for (const sconto of sconti_da_applicare) {
if (sconto.perc_sconto > 0) {
mypricecalc += (sconto.qtadascontare * order.price) * (1 - (sconto.perc_sconto / 100))
} else {
mypricecalc += sconto.price
}
}
}
if (qtanonscontata > 0) {
mypricecalc += order.price * qtanonscontata;
}
} else {
mypricecalc = order.price * order.quantity;
}
order.TotalPriceProduct += mypricecalc;
this.totalPrice += order.TotalPriceProduct;
}
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
} catch (e) {