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) {
|
module.exports.createOrder = async function (order) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
Order.updateTotals(order);
|
||||||
return await Order.create(order)
|
return await Order.create(order)
|
||||||
.then((ris) => {
|
.then((ris) => {
|
||||||
if (!!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) {
|
module.exports.getTotalOrderById = async function (id) {
|
||||||
const query = [
|
const query = [
|
||||||
{ $match: { _id: ObjectID(id) } },
|
{ $match: { _id: ObjectID(id) } },
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ const OrdersCartSchema = new Schema({
|
|||||||
numord_pers: { type: Number },
|
numord_pers: { type: Number },
|
||||||
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
||||||
totalQty: { type: Number, default: 0 },
|
totalQty: { type: Number, default: 0 },
|
||||||
|
TotalPriceProduct: { type: Number, default: 0 },
|
||||||
totalPrice: { type: Number, default: 0 },
|
totalPrice: { type: Number, default: 0 },
|
||||||
department: {
|
department: {
|
||||||
type: String, ref: 'Department'
|
type: String, ref: 'Department'
|
||||||
@@ -265,9 +266,6 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
|
|||||||
|
|
||||||
myorderscart = await OrdersCart.getOrdersCartByQuery(query);
|
myorderscart = await OrdersCart.getOrdersCartByQuery(query);
|
||||||
|
|
||||||
if (myorderscart)
|
|
||||||
console.log('*** Num myorderscart ', myorderscart.length);
|
|
||||||
|
|
||||||
|
|
||||||
/*transform: function(doc, populated) {
|
/*transform: function(doc, populated) {
|
||||||
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
||||||
@@ -302,6 +300,7 @@ module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
|
|||||||
items: newOrdersCart.items,
|
items: newOrdersCart.items,
|
||||||
totalQty: newOrdersCart.totalQty,
|
totalQty: newOrdersCart.totalQty,
|
||||||
totalPrice: newOrdersCart.totalPrice,
|
totalPrice: newOrdersCart.totalPrice,
|
||||||
|
totalPriceProduct: newOrdersCart.totalPriceProduct,
|
||||||
userId: userId,
|
userId: userId,
|
||||||
status: newOrdersCart.status,
|
status: newOrdersCart.status,
|
||||||
numorder: newOrdersCart.numorder,
|
numorder: newOrdersCart.numorder,
|
||||||
|
|||||||
@@ -360,8 +360,16 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
|
|||||||
|
|
||||||
const arrprod = await Product.find({ idapp }).lean();
|
const arrprod = await Product.find({ idapp }).lean();
|
||||||
for (const prod of arrprod) {
|
for (const prod of arrprod) {
|
||||||
|
await this.singlerecconvert_AfterImport(prod);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.singlerecconvert_AfterImport = async function (idapp, prod) {
|
||||||
|
|
||||||
let setta = false;
|
let setta = false;
|
||||||
|
|
||||||
|
try {
|
||||||
// Impostazioni Base:
|
// Impostazioni Base:
|
||||||
let objtoset = {
|
let objtoset = {
|
||||||
idapp,
|
idapp,
|
||||||
@@ -439,10 +447,17 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (setta) {
|
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';
|
// const campodarimuovere = 'producer_name';
|
||||||
// await Product.findOneAndUpdate({ _id: prod._id }, { $unset: { [campodarimuovere]: 1 } })
|
// await Product.findOneAndUpdate({ _id: prod._id }, { $unset: { [campodarimuovere]: 1 } })
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Err', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -38,6 +38,7 @@ class Cart {
|
|||||||
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
|
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
|
||||||
if (!!myitem) {
|
if (!!myitem) {
|
||||||
myitem.order.quantity++;
|
myitem.order.quantity++;
|
||||||
|
myitem.order = Order.updateTotals(myitem.order);
|
||||||
this.updatetotals();
|
this.updatetotals();
|
||||||
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
|
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
|
||||||
return myitem.order.quantity;
|
return myitem.order.quantity;
|
||||||
@@ -49,6 +50,7 @@ class Cart {
|
|||||||
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
|
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
|
||||||
if (!!myitem && myitem.order.quantity > 0) {
|
if (!!myitem && myitem.order.quantity > 0) {
|
||||||
myitem.order.quantity--;
|
myitem.order.quantity--;
|
||||||
|
myitem.order = Order.updateTotals(myitem.order);
|
||||||
this.updatetotals();
|
this.updatetotals();
|
||||||
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
|
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
|
||||||
return myitem.order.quantity;
|
return myitem.order.quantity;
|
||||||
@@ -64,6 +66,7 @@ class Cart {
|
|||||||
let ind = this.items.length;
|
let ind = this.items.length;
|
||||||
this.items[ind] = {};
|
this.items[ind] = {};
|
||||||
this.items[ind].order = itemorder;
|
this.items[ind].order = itemorder;
|
||||||
|
this.items[ind].order = Order.updateTotals(this.items[ind].order);
|
||||||
this.updatetotals();
|
this.updatetotals();
|
||||||
|
|
||||||
return ind;
|
return ind;
|
||||||
@@ -86,6 +89,7 @@ class Cart {
|
|||||||
note: this.note,
|
note: this.note,
|
||||||
modify_at: this.modify_at
|
modify_at: this.modify_at
|
||||||
})
|
})
|
||||||
|
|
||||||
return newCart
|
return newCart
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,64 +97,12 @@ class Cart {
|
|||||||
try {
|
try {
|
||||||
this.totalQty = 0;
|
this.totalQty = 0;
|
||||||
this.totalPrice = 0;
|
this.totalPrice = 0;
|
||||||
for (const rec in this.items) {
|
if (this.items && this.items.length > 0) {
|
||||||
let mypricecalc = 0;
|
for (const rec of this.items) {
|
||||||
|
let ord = Order.updateTotals(rec.order);
|
||||||
let order = this.items[rec].order;
|
this.totalQty += ord.quantity;
|
||||||
if (!order) {
|
this.totalPrice += ord.TotalPriceProduct;
|
||||||
order = this.items[rec];
|
|
||||||
}
|
}
|
||||||
order.TotalPriceProduct = 0;
|
|
||||||
this.totalQty += order.quantity;
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
this.totalPrice += order.TotalPriceProduct;
|
|
||||||
}
|
}
|
||||||
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
|
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -50,8 +50,41 @@ router.post('/import', authenticate, async (req, res) => {
|
|||||||
|
|
||||||
let dataObjects = JSON.parse(`[${data}]`);
|
let dataObjects = JSON.parse(`[${data}]`);
|
||||||
|
|
||||||
|
let updated = 0;
|
||||||
|
let imported = 0;
|
||||||
|
let errors = 0;
|
||||||
|
|
||||||
|
for (const product of dataObjects) {
|
||||||
|
if (!product.hasOwnProperty('active')) {
|
||||||
|
product.active = true;
|
||||||
|
}
|
||||||
|
let risrec = await Product.findOneAndUpdate({ code: product.code }, { $set: product }, { new: true, upsert: true });
|
||||||
|
|
||||||
|
let recnew = await Product.findOne({ code: product.code }).lean();
|
||||||
|
|
||||||
|
if (risrec) {
|
||||||
|
if (risrec._id) {
|
||||||
|
// Record existed, so it was updated
|
||||||
|
let arrfieldchange = tools.differentObjects(product, recnew);
|
||||||
|
if (arrfieldchange.length > 0) {
|
||||||
|
updated++;
|
||||||
|
console.log('Changed: ', product.name + ': ' + arrfieldchange);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Record didn't exist, so it was created
|
||||||
|
imported++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// risrec is null or undefined, indicating an error
|
||||||
|
console.error('Error: ', product.name);
|
||||||
|
errors++;
|
||||||
|
}
|
||||||
|
|
||||||
|
await Product.singlerecconvert_AfterImport(idapp, recnew);
|
||||||
|
}
|
||||||
|
|
||||||
// L'opzione ordered: false gestisce gli errori senza interrompere l'inserimento
|
// L'opzione ordered: false gestisce gli errori senza interrompere l'inserimento
|
||||||
return await Product.insertMany(dataObjects, { ordered: false })
|
/*return await Product.insertMany(dataObjects, { ordered: false })
|
||||||
.then((ris) => {
|
.then((ris) => {
|
||||||
|
|
||||||
Product.convertAfterImport(idapp, dataObjects).then((ris) => {
|
Product.convertAfterImport(idapp, dataObjects).then((ris) => {
|
||||||
@@ -65,7 +98,9 @@ router.post('/import', authenticate, async (req, res) => {
|
|||||||
Product.convertAfterImport(idapp).then((ris) => {
|
Product.convertAfterImport(idapp).then((ris) => {
|
||||||
return res.status(200).send(true);
|
return res.status(200).send(true);
|
||||||
});
|
});
|
||||||
});
|
});*/
|
||||||
|
|
||||||
|
return res.status(200).send({ updated, imported, errors });
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -58,8 +58,12 @@ router.post('/:userId', authenticate, async function (req, res, next) {
|
|||||||
let mycart = await Cart.getCartByUserId(userId, idapp);
|
let mycart = await Cart.getCartByUserId(userId, idapp);
|
||||||
|
|
||||||
// const myorder = Order.getOrderByID(order._id);
|
// const myorder = Order.getOrderByID(order._id);
|
||||||
if (!addqty && !subqty)
|
if (!addqty && !subqty) {
|
||||||
order._id = await Order.createOrder(order);
|
order._id = await Order.createOrder(order);
|
||||||
|
if (!order._id) {
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_ERR, cart: 0 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let cart = null;
|
let cart = null;
|
||||||
let product = null;
|
let product = null;
|
||||||
@@ -222,7 +226,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
|
|||||||
let note = req.body.note;
|
let note = req.body.note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const mycart = await Cart.findOne({ _id: cart_id });
|
let mycart = await Cart.findOne({ _id: cart_id });
|
||||||
|
|
||||||
let numorder = await OrdersCart.getLastNumOrder(idapp);
|
let numorder = await OrdersCart.getLastNumOrder(idapp);
|
||||||
let numord_pers = await OrdersCart.getLastNumOrdPers(userId, idapp);
|
let numord_pers = await OrdersCart.getLastNumOrdPers(userId, idapp);
|
||||||
@@ -301,6 +305,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
|
|||||||
recOrderCart: myorderCart
|
recOrderCart: myorderCart
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error('Err', e);
|
||||||
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0, recOrderCart: null });
|
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0, recOrderCart: null });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4344,6 +4344,26 @@ module.exports = {
|
|||||||
return { msg: msg1 + ' ' + msg2 };
|
return { msg: msg1 + ' ' + msg2 };
|
||||||
},
|
},
|
||||||
|
|
||||||
|
differentObjects(obj1, obj2) {
|
||||||
|
try {
|
||||||
|
const fieldsUpdated = [];
|
||||||
|
const obj1Fields = obj1 ? Object.keys(obj1) : [];
|
||||||
|
const obj2Fields = obj2 ? Object.keys(obj2) : [];
|
||||||
|
|
||||||
|
const commonFields = obj1Fields.filter(field => obj2Fields.includes(field));
|
||||||
|
|
||||||
|
|
||||||
|
commonFields.forEach((field) => {
|
||||||
|
if (obj1 && obj2 && obj1 && obj2 && obj1[field].toString() !== obj2[field].toString()) {
|
||||||
|
fieldsUpdated.push(field);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return fieldsUpdated
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Err:', e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user