- 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

@@ -259,12 +259,12 @@ module.exports.getOrderByID = function (id, callback) {
Order.findById(id, callback);
};
module.exports.createOrder = async function (order) {
module.exports.createOrder = async function (order, codice_sconto) {
try {
if (order.idGasordine === '') {
order.idGasordine = undefined;
}
await Order.updateTotals(order);
await Order.updateTotals(order, codice_sconto);
return await Order.create(order).then((ris) => {
if (!!ris) return ris._id;
return null;
@@ -317,15 +317,27 @@ function applyNonCumulativeDiscounts(order, discounts) {
while (qtadascontare > 0) {
let scontoapplicato = null;
// Filtriamo gli sconti non cumulativi
for (const sconto of getNonCumulativeDiscounts(discounts)) {
if (qtadascontare >= sconto.qta) {
// Se la quantità da scontare è maggiore o uguale alla quantità dello sconto, applica lo sconto completo
scontoapplicato = { ...sconto, qtadascontare: sconto.qta };
break; // Esci dal ciclo, abbiamo trovato lo sconto applicabile
} else if (qtadascontare > 0) {
// Se la quantità da scontare è inferiore allo sconto, applica solo la quantità disponibile
scontoapplicato = { ...sconto, qtadascontare: qtadascontare };
qtadascontare = 0; // Abbiamo finito le quantità da scontare
break;
}
}
// Se c'è uno sconto applicato, aggiungilo alla lista e riduci la quantità
if (scontoapplicato && scontoapplicato.qtadascontare > 0) {
sconti_da_applicare.push(scontoapplicato);
qtadascontare -= scontoapplicato.qtadascontare;
} else {
// Se non ci sono più sconti da applicare, la quantità rimasta è non scontata
qtanonscontata = qtadascontare;
qtadascontare = 0;
}
@@ -356,8 +368,10 @@ function calculateFullPrice(order) {
return order.price * order.quantity + order.price * order.quantitypreordered;
}
module.exports.updateTotals = async function (order) {
module.exports.updateTotals = async function (order, codice_sconto) {
try {
const CartClass = require('../modules/Cart');
if (!order) return;
initOrderTotals(order);
@@ -368,12 +382,8 @@ module.exports.updateTotals = async function (order) {
let recscontisticheTrovate = [];
if (order?.codice_sconto) {
recscontisticheTrovate = await Scontistica.find({
idapp: order.idapp,
code: order?.codice_sconto?.toUpperCase(),
applica: shared_consts.SCONTI_APPLICA.A_TUTTI,
}).lean();
if (codice_sconto) {
recscontisticheTrovate = await CartClass.getRecSconto(order.idapp, codice_sconto, true);
}
// Se ha inserito una scontistica che esiste...
@@ -390,6 +400,7 @@ module.exports.updateTotals = async function (order) {
order.TotalPriceProductCalc += total;
order.TotalPriceProduct += total;
order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2));
order.codice_sconto = codice_sconto;
return order;
} catch (e) {