Carrello con scontistica aggiornata
This commit is contained in:
@@ -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) } },
|
||||
|
||||
@@ -22,6 +22,7 @@ const OrdersCartSchema = new Schema({
|
||||
numord_pers: { type: Number },
|
||||
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
||||
totalQty: { type: Number, default: 0 },
|
||||
TotalPriceProduct: { type: Number, default: 0 },
|
||||
totalPrice: { type: Number, default: 0 },
|
||||
department: {
|
||||
type: String, ref: 'Department'
|
||||
@@ -265,9 +266,6 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
|
||||
|
||||
myorderscart = await OrdersCart.getOrdersCartByQuery(query);
|
||||
|
||||
if (myorderscart)
|
||||
console.log('*** Num myorderscart ', myorderscart.length);
|
||||
|
||||
|
||||
/*transform: function(doc, populated) {
|
||||
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
||||
@@ -302,6 +300,7 @@ module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
|
||||
items: newOrdersCart.items,
|
||||
totalQty: newOrdersCart.totalQty,
|
||||
totalPrice: newOrdersCart.totalPrice,
|
||||
totalPriceProduct: newOrdersCart.totalPriceProduct,
|
||||
userId: userId,
|
||||
status: newOrdersCart.status,
|
||||
numorder: newOrdersCart.numorder,
|
||||
|
||||
@@ -360,8 +360,16 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
|
||||
|
||||
const arrprod = await Product.find({ idapp }).lean();
|
||||
for (const prod of arrprod) {
|
||||
let setta = false;
|
||||
await this.singlerecconvert_AfterImport(prod);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports.singlerecconvert_AfterImport = async function (idapp, prod) {
|
||||
|
||||
let setta = false;
|
||||
|
||||
try {
|
||||
// Impostazioni Base:
|
||||
let objtoset = {
|
||||
idapp,
|
||||
@@ -439,10 +447,17 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
|
||||
}
|
||||
|
||||
if (setta) {
|
||||
await Product.findOneAndUpdate({ _id: prod._id }, { $set: objtoset })
|
||||
const ris = await Product.findOneAndUpdate({ _id: ObjectID(prod._id) }, { $set: objtoset })
|
||||
|
||||
if (ris) {
|
||||
console.log('ris', ris);
|
||||
}
|
||||
|
||||
// const campodarimuovere = 'producer_name';
|
||||
// await Product.findOneAndUpdate({ _id: prod._id }, { $unset: { [campodarimuovere]: 1 } })
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Err', e);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user