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

@@ -213,6 +213,7 @@ module.exports.getOrderByID = function (id, callback) {
module.exports.createOrder = async function (order) {
try {
Order.updateTotals(order);
return await Order.create(order)
.then((ris) => {
if (!!ris)
@@ -240,6 +241,67 @@ module.exports.updateStatusOrdersElements = async function (arrOrders, myelement
}
module.exports.updateTotals = function (order) {
try {
let mypricecalc = 0;
order.TotalPriceProduct = 0;
// 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;
return order;
} catch (e) {
console.error('Err:', e);
}
}
module.exports.getTotalOrderById = async function (id) {
const query = [
{ $match: { _id: ObjectID(id) } },