- corretto la gestione degli Sconti

- Duplicare un Catalogo
This commit is contained in:
Surya Paolo
2025-06-11 01:05:25 +02:00
parent d1d4b73da0
commit 25377090c1
6 changed files with 242 additions and 106 deletions

View File

@@ -27,6 +27,7 @@ class Cart {
this.totalQty = 0;
this.totalPrice = 0;
this.totalPriceCalc = 0;
this.totalPriceIntero = 0;
for (const key in this.items) {
const item = this.items[key];
@@ -37,6 +38,7 @@ class Cart {
this.totalPrice = parseFloat(this.totalPrice.toFixed(2));
this.totalPriceCalc = parseFloat(this.totalPriceCalc.toFixed(2));
this.totalPriceIntero = parseFloat(this.totalPriceIntero.toFixed(2));
} catch (e) {
console.error("Errore durante l'aggiornamento del carrello:", e);
}
@@ -62,6 +64,7 @@ class Cart {
mynewcart.modify_at = new Date();
mynewcart.note_ordine_gas = '';
mynewcart.codice_sconto = cart.codice_sconto;
mynewcart.descr_sconto = cart.descr_sconto;
return mynewcart;
} catch (e) {
@@ -118,7 +121,7 @@ class Cart {
myitem.order.modify_at = new Date();
myitem.order = await Order.updateTotals(myitem.order);
myitem.order = await Order.updateTotals(myitem.order, this.codice_sconto);
await this.updatecarttotals(false);
await this.updateExtraOrder();
@@ -164,7 +167,7 @@ class Cart {
myitem.order.quantity -= step;
}
}
myitem.order = await Order.updateTotals(myitem.order);
myitem.order = await Order.updateTotals(myitem.order, this.codice_sconto);
await this.updatecarttotals(false);
await this.updateExtraOrder();
@@ -182,7 +185,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.items[ind].order = await Order.updateTotals(this.items[ind].order, this.codice_sconto);
await this.updatecarttotals(false);
await this.updateExtraOrder();
@@ -203,11 +206,13 @@ class Cart {
items: this.generateArray(),
totalQty: this.totalQty,
totalPriceCalc: this.totalPriceCalc,
totalPriceIntero: this.totalPriceIntero,
totalPrice: this.totalPrice,
userId: this.userId,
department: this.department,
note: this.note,
codice_sconto: this.codice_sconto,
descr_sconto: this.descr_sconto,
note_ordine_gas: this.note_ordine_gas,
modify_at: this.modify_at,
});
@@ -220,6 +225,10 @@ class Cart {
async updateOrderTotals(order, updateCalcPrice) {
order.TotalPriceProductCalc = 0;
// PROVO A METTERE SEMPRE CHE MI RICALCOLA IL PREZZO !
// updateCalcPrice = true;
if (updateCalcPrice) {
order.TotalPriceProduct = 0;
}
@@ -227,14 +236,11 @@ class Cart {
const qty = order.quantity + order.quantitypreordered;
this.totalQty += qty;
let recscontisticheTrovate = await Scontistica.find({
idapp: order.idapp,
code: this.codice_sconto?.toUpperCase(),
applica: shared_consts.SCONTI_APPLICA.A_TUTTI,
}).lean();
const recscontisticheTrovate = await Cart.getRecSconto(this.idapp, this.codice_sconto, true);
const scontiDaUsare = recscontisticheTrovate?.length ? recscontisticheTrovate : order.scontisticas || [];
const priceCalc = this.calcolaPrezzoScontato(order, qty, scontiDaUsare);
const priceintero = this.calcolaPrezzoScontato(order, qty, []);
order.TotalPriceProductCalc += priceCalc;
@@ -243,8 +249,14 @@ class Cart {
order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2));
}
this.totalPriceCalc += priceCalc;
this.totalPrice += order.TotalPriceProduct;
this.totalPriceCalc += priceCalc;
this.totalPriceIntero += priceintero;
// if (updateCalcPrice) {
// Aggiorna anche l'ordine associato
await Order.findOneAndUpdate({ _id: order._id }, { $set: order }, { new: false });
// }
}
calcolaPrezzoScontato(order, qtyTotale, sconti = []) {
@@ -259,11 +271,15 @@ class Cart {
// Applica sconti non cumulativi
while (qtaRimanente > 0) {
let scontoScelto = null;
let scontoVantaggioso = 0;
for (const sconto of sconti.filter((s) => !s.cumulativo)) {
if (qtaRimanente >= s.qta) {
scontoScelto = { ...sconto, qtadascontare: s.qta };
break; // prendi il primo valido (puoi migliorare scegliendo il più vantaggioso)
if (qtaRimanente >= sconto.qta) {
const scontoApplicato =
sconto.perc_sconto > 0 ? qtaRimanente * order.price * (1 - sconto.perc_sconto / 100) : sconto.price;
if (scontoApplicato > scontoVantaggioso) {
scontoVantaggioso = scontoApplicato;
scontoScelto = { ...sconto, qtadascontare: qtaRimanente };
}
}
}
@@ -330,6 +346,53 @@ class Cart {
}
return arr;
}
static async getRecSconto(idapp, codice_sconto, soloAttivi) {
const condSconto = { $regex: new RegExp(`^${codice_sconto}$`, 'i') };
const query = {
idapp: idapp,
code: condSconto,
applica: shared_consts.SCONTI_APPLICA.A_TUTTI,
};
if (soloAttivi) {
query.attivo = true;
}
const recscontisticheTrovate = await Scontistica.find(query).lean();
return recscontisticheTrovate;
}
async updateCartIntoDB() {
const result = await cartModel.updateCartByUserId(this.userId, {
items: this.items,
totalQty: this.totalQty,
totalPrice: this.totalPrice,
totalPriceCalc: this.totalPriceCalc,
totalPriceIntero: this.totalPriceIntero,
userId: this.userId,
});
return result;
}
async aggiornaCarrello() {
try {
// Rifai i calcoli !
await this.updatecarttotals(true);
await this.updateExtraOrder();
// Aggiorna i calcoli sul DB:
const mycart = await this.updateCartIntoDB();
// ritorna il carrello aggiornato !
return await cartModel.getCartCompletoByCartId(mycart._id, this.idapp);
} catch (e) {
console.error("Errore durante l'aggiornamento del carrello:", e);
}
}
}
module.exports = Cart;