Aggiornamento modifiche preOrdini
This commit is contained in:
@@ -2,7 +2,7 @@ DATABASE=test_PiuCheBuono
|
||||
UDB=paofreeplanet
|
||||
PDB=mypassword@1A
|
||||
SEND_EMAIL=0
|
||||
SEND_EMAIL_ORDERS=0
|
||||
SEND_EMAIL_ORDERS=1
|
||||
PORT=3000
|
||||
appTelegram_TEST=["1","13"]
|
||||
appTelegram=["1","13"]
|
||||
|
||||
@@ -19,7 +19,7 @@ html
|
||||
}
|
||||
|
||||
body(yahoofix, style="background: #ffffff")
|
||||
- var baseimg = baseurl + '/statics/'
|
||||
- var baseimg = baseurl + '/'
|
||||
span(id='body_style', style='display:block')
|
||||
table(cellpadding="10", cellspacing="0", width="600", align="center")
|
||||
tr
|
||||
|
||||
@@ -19,7 +19,7 @@ html
|
||||
}
|
||||
|
||||
body(yahoofix, style="background: #ffffff")
|
||||
- var baseimg = baseurl + '/statics/'
|
||||
- var baseimg = baseurl + '/'
|
||||
span(id='body_style', style='display:block')
|
||||
table(cellpadding="10", cellspacing="0", width="600", align="center")
|
||||
tr
|
||||
|
||||
@@ -19,7 +19,7 @@ html
|
||||
}
|
||||
|
||||
body(yahoofix, style="background: #ffffff")
|
||||
- var baseimg = baseurl + '/statics/'
|
||||
- var baseimg = baseurl + '/';
|
||||
span(id='body_style', style='display:block')
|
||||
table(cellpadding="10", cellspacing="0", width="600", align="center")
|
||||
tr
|
||||
@@ -40,11 +40,11 @@ html
|
||||
- var index = 0
|
||||
|
||||
each product in orders.items
|
||||
- var descr = product.name
|
||||
- var img = product.img
|
||||
- var price = product.price
|
||||
- var after_price = product.after_price
|
||||
- var qty = product.quantity
|
||||
- var descr = product.order.product.name
|
||||
- var img = product.order.product.img
|
||||
- var price = product.order.product.price
|
||||
- var after_price = product.order.product.after_price
|
||||
- var qty = product.order.product.quantity
|
||||
- var qtypreordered = product.order.quantitypreordered
|
||||
- index = index + 1
|
||||
|
||||
|
||||
@@ -11,11 +11,11 @@
|
||||
-}
|
||||
|
||||
- var urlcal = baseurl + '/calendario-eventi/'
|
||||
- var imginstagram = baseurl + '/statics/images/images/footer-instagram-icon.png'
|
||||
- var imgtwitter = baseurl + '/statics/images/images/footer-twitter-icon.png'
|
||||
- var imgyoutube = baseurl + '/statics/images/images/footer-youtube-icon.png'
|
||||
- var imgfb = baseurl + '/statics/images/images/footer-facebook-icon.png'
|
||||
- var baseimg = baseurl + '/statics/'
|
||||
- var imginstagram = baseurl + '/images/images/footer-instagram-icon.png'
|
||||
- var imgtwitter = baseurl + '/images/images/footer-twitter-icon.png'
|
||||
- var imgyoutube = baseurl + '/images/images/footer-youtube-icon.png'
|
||||
- var imgfb = baseurl + '/images/images/footer-facebook-icon.png'
|
||||
- var baseimg = baseurl + '/'
|
||||
doctype html
|
||||
html
|
||||
head
|
||||
@@ -50,7 +50,7 @@ html
|
||||
tr
|
||||
td.logoContainer
|
||||
a(href=baseurl, title='logo')
|
||||
img.logo(src=baseurl+"/statics/images/logo.png", height=dataemail.height_logo)
|
||||
img.logo(src=baseurl+"/images/logo.png", height=dataemail.height_logo)
|
||||
|
||||
tr
|
||||
td.testomail
|
||||
|
||||
@@ -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) } },
|
||||
|
||||
@@ -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 },
|
||||
totalQtyPreordered: { type: Number, default: 0 },
|
||||
totalPrice: { type: Number, default: 0 },
|
||||
department: {
|
||||
@@ -270,9 +271,6 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder, fil
|
||||
|
||||
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
|
||||
@@ -308,6 +306,7 @@ module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
|
||||
totalQty: newOrdersCart.totalQty,
|
||||
totalQtyPreordered: newOrdersCart.totalQtyPreordered,
|
||||
totalPrice: newOrdersCart.totalPrice,
|
||||
totalPriceProduct: newOrdersCart.totalPriceProduct,
|
||||
userId: userId,
|
||||
status: newOrdersCart.status,
|
||||
numorder: newOrdersCart.numorder,
|
||||
@@ -475,7 +474,7 @@ module.exports.updateCmd = async function (ordersCart, status, value) {
|
||||
|
||||
// Aggiorna anche il Magazzino, togliendo le quantità in Stock
|
||||
if (value) {
|
||||
OrdersCart.updateStockQtaDalMagazzino(id);
|
||||
await OrdersCart.updateStockQtaDalMagazzino(id);
|
||||
}
|
||||
ris = await OrdersCart.setEvasoById(value, myOrderCart);
|
||||
|
||||
|
||||
@@ -427,8 +427,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,
|
||||
@@ -506,10 +514,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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -89,6 +89,7 @@ class Cart {
|
||||
let ind = this.items.length;
|
||||
this.items[ind] = {};
|
||||
this.items[ind].order = itemorder;
|
||||
this.items[ind].order = Order.updateTotals(this.items[ind].order);
|
||||
this.updatetotals();
|
||||
|
||||
return ind;
|
||||
|
||||
@@ -50,8 +50,41 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
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
|
||||
return await Product.insertMany(dataObjects, { ordered: false })
|
||||
/*return await Product.insertMany(dataObjects, { ordered: false })
|
||||
.then((ris) => {
|
||||
|
||||
Product.convertAfterImport(idapp, dataObjects).then((ris) => {
|
||||
@@ -65,7 +98,9 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
Product.convertAfterImport(idapp).then((ris) => {
|
||||
return res.status(200).send(true);
|
||||
});
|
||||
});
|
||||
});*/
|
||||
|
||||
return res.status(200).send({ updated, imported, errors });
|
||||
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
@@ -57,9 +57,17 @@ router.post('/:userId', authenticate, async function (req, res, next) {
|
||||
try {
|
||||
let mycart = await Cart.getCartByUserId(userId, idapp);
|
||||
|
||||
if (!order) {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, cart: null });
|
||||
}
|
||||
|
||||
// const myorder = Order.getOrderByID(order._id);
|
||||
if (!addqty && !subqty)
|
||||
if (!addqty && !subqty && 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 product = null;
|
||||
@@ -225,7 +233,15 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
|
||||
let note = req.body.note;
|
||||
|
||||
try {
|
||||
const mycart = await Cart.findOne({ _id: cart_id });
|
||||
let mycart = await Cart.findOne({ _id: cart_id });
|
||||
|
||||
if (!mycart) {
|
||||
return res.send({
|
||||
code: server_constants.RIS_CODE_OK,
|
||||
status: 0,
|
||||
recOrderCart: null,
|
||||
});
|
||||
}
|
||||
|
||||
let numorder = await OrdersCart.getLastNumOrder(idapp);
|
||||
let numord_pers = await OrdersCart.getLastNumOrdPers(userId, idapp);
|
||||
@@ -304,6 +320,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
|
||||
recOrderCart: myorderCart
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('Err', e);
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0, recOrderCart: null });
|
||||
}
|
||||
|
||||
|
||||
@@ -4344,6 +4344,26 @@ module.exports = {
|
||||
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