Aggiornamento modifiche preOrdini
This commit is contained in:
@@ -218,6 +218,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)
|
||||
@@ -245,6 +246,71 @@ module.exports.updateStatusOrdersElements = async function (arrOrders, myelement
|
||||
|
||||
}
|
||||
|
||||
module.exports.updateTotals = function (order) {
|
||||
|
||||
try {
|
||||
if (!order) {
|
||||
return;
|
||||
}
|
||||
|
||||
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) } },
|
||||
|
||||
Reference in New Issue
Block a user