Corretto incongruenze OrdersCart

This commit is contained in:
Surya Paolo
2023-12-21 01:34:33 +01:00
parent 7af909d2be
commit 2ab4bd4e2a
4 changed files with 110 additions and 18 deletions

View File

@@ -260,7 +260,7 @@ module.exports.updateTotals = function (order) {
let sconti_da_applicare = []; let sconti_da_applicare = [];
if (order.scontisticas) { if (order.scontisticas) {
let qtadascontare = order.quantity let qtadascontare = order.quantity + order.quantitypreordered
let qtanonscontata = 0 let qtanonscontata = 0
while (qtadascontare > 0) { while (qtadascontare > 0) {
@@ -300,7 +300,7 @@ module.exports.updateTotals = function (order) {
} }
} else { } else {
mypricecalc = order.price * order.quantity; mypricecalc = (order.price * order.quantity) + (order.price * order.quantitypreordered);
} }
order.TotalPriceProduct += mypricecalc; order.TotalPriceProduct += mypricecalc;

View File

@@ -22,7 +22,6 @@ 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 },
totalQtyPreordered: { type: Number, default: 0 }, totalQtyPreordered: { type: Number, default: 0 },
totalPrice: { type: Number, default: 0 }, totalPrice: { type: Number, default: 0 },
department: { department: {
@@ -130,10 +129,10 @@ module.exports.getLastNumOrder = async function (idapp) {
module.exports.getLastNumOrdPers = async function (uid, idapp) { module.exports.getLastNumOrdPers = async function (uid, idapp) {
let query = { userId: uid, idapp, deleted: false } let query = { userId: uid, idapp, deleted: false }
let numorder = 1; let numorder = 1;
let numorderrec = await OrdersCart.find(query).sort({ numorder: -1 }).limit(1); let numorderrec = await OrdersCart.find(query).sort({ numord_pers: -1 }).limit(1);
if (numorderrec && numorderrec.length > 0) if (numorderrec && numorderrec.length > 0)
numorder = numorderrec[0].numorder; numorder = numorderrec[0].numord_pers;
else else
numorder = 0; numorder = 0;
@@ -178,7 +177,7 @@ module.exports.getOrdersCartById = async function (id) {
} }
module.exports.getOrdersCartByQuery = async function (query) { module.exports.getOLDOrdersCartByQuery = async function (query) {
let myorderscart = await OrdersCart.find(query) let myorderscart = await OrdersCart.find(query)
.populate('items.order') .populate('items.order')
@@ -252,6 +251,82 @@ module.exports.getOrdersCartByQuery = async function (query) {
return myorderscart; return myorderscart;
} }
module.exports.getOrdersCartByQuery = async function (query) {
let myorderscart = await OrdersCart.find(query)
.populate('items.order')
.populate({
path: 'items.order',
populate: {
path: 'idProduct',
model: 'Product'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idProducer',
model: 'Producer'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idProvider',
model: 'Provider'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idStorehouse',
model: 'Storehouse'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idScontisticas',
model: 'Scontistica'
},
})
.populate({
path: 'userId',
model: 'User',
select: '_id name surname username profile', // Specify the fields you want to retrieve
})
.lean();
myorderscart = myorderscart.map(order => {
order.user = order.userId
order.userId = order.user._id
order.items = order.items.map(item => {
if (item.order) {
try {
item.order.product = item.order.idProduct;
item.order.idProduct = item.order.product ? item.order.product._id : '';
item.order.producer = item.order.idProducer;
item.order.idProducer = item.order.producer ? item.order.producer._id : '';
item.order.storehouse = item.order.idStorehouse;
item.order.idStorehouse = item.order.storehouse ? item.order.storehouse._id : '';
item.order.provider = item.order.idProvider;
item.order.idProvider = item.order.provider ? item.order.provider._id : '';
item.order.scontisticas = item.order.scontisticas;
item.order.idScontisticas = item.order.idScontisticas ? item.order.idScontisticas._id : '';
} catch (e) {
console.error('Err: ', e);
}
}
return item;
});
return order;
});
return myorderscart;
}
module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder, filterStatus) { module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder, filterStatus) {
try { try {
@@ -306,7 +381,6 @@ module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
totalQty: newOrdersCart.totalQty, totalQty: newOrdersCart.totalQty,
totalQtyPreordered: newOrdersCart.totalQtyPreordered, totalQtyPreordered: newOrdersCart.totalQtyPreordered,
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,
@@ -559,7 +633,7 @@ module.exports.getmsgorderTelegram = async function (ordersCart) {
if (ord.order.quantity > 0) if (ord.order.quantity > 0)
qtystr += 'Ordinate: ' + ord.order.quantity qtystr += 'Ordinate: ' + ord.order.quantity
if (ord.order.quantitypreordered > 0) if (ord.order.quantitypreordered > 0)
qtystr += 'Prenotate: ' + ord.order.quantitypreordered qtystr += ' Pre-Ordinate: ' + ord.order.quantitypreordered
msg += '✅ [' + qtystr + '] ' + ord.order.product.name + ' (' + ord.order.price + ' € ' + (ord.order.after_price ? ord.order.after_price : '') + ' Tot=' + ord.order.TotalPriceProduct + '€ )'; msg += '✅ [' + qtystr + '] ' + ord.order.product.name + ' (' + ord.order.price + ' € ' + (ord.order.after_price ? ord.order.after_price : '') + ' Tot=' + ord.order.TotalPriceProduct + '€ )';
} }

View File

@@ -6,18 +6,25 @@ const { ObjectID } = require('mongodb');
const Order = require('../models/order'); const Order = require('../models/order');
class Cart { class Cart {
constructor(order) { constructor(order, arrorders) {
this.items = {};
if (!!order) {
this.idapp = order.idapp || 0;
this.items[order._id] = order;
this.userId = order.userId || "";
}
this.modify_at = new Date(); this.modify_at = new Date();
this.items = {};
if (!!order) {
this.initializeFromOrder(order);
} else if (!!arrorders) {
for (const ord of arrorders) {
this.items.push(ord)
}
}
this.updatetotals(); this.updatetotals();
} }
initializeFromOrder(order) {
this.idapp = order.idapp || 0;
this.userId = order.userId || "";
this.items[order._id] = order;
}
static constructByCart(cart) { static constructByCart(cart) {
try { try {
const mynewcart = new Cart(null); const mynewcart = new Cart(null);

View File

@@ -90,11 +90,13 @@ router.post('/:userId', authenticate, async function (req, res, next) {
myord = await newCart.subqty(order); myord = await newCart.subqty(order);
} else { } else {
const ind = newCart.addItem(order); const ind = newCart.addItem(order);
myord = order; const arrord = await Order.getTotalOrderById(order._id);
myord = arrord ? arrord[0] : null;
} }
} else { } else {
await newCart.updatetotals(); await newCart.updatetotals();
myord = order; const arrord = await Order.getTotalOrderById(order._id);
myord = arrord ? arrord[0] : null;
} }
cart = await Cart.updateCartByCartId(mycart._id, newCart.generateModel()); cart = await Cart.updateCartByCartId(mycart._id, newCart.generateModel());
@@ -311,6 +313,15 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
}) })
} }
}) })
} else if (status < shared_consts.OrderStatus.CHECKOUT_SENT) {
// Aggiorna il carrello !
/*const cartpao = await Cart.getCartByUserId(userId, idapp);
let newCart = CartClass.constructByCart(cartpao);
if (newCart)
await newCart.updatetotals();
newCart;*/
} }
} }