possibilità di modificare un ordine, e anche i totali

This commit is contained in:
Surya Paolo
2024-01-15 22:19:26 +01:00
parent 79ca364e84
commit 3183825137
7 changed files with 121 additions and 49 deletions

View File

@@ -54,6 +54,10 @@ const orderSchema = new Schema({
type: Number,
default: 0,
},
TotalPriceProductCalc: {
type: Number,
default: 0,
},
confermato: { // e quindi è stato tolto dal magazzino (aggiornando il campo stockQty)
type: Boolean,
default: false,
@@ -288,9 +292,11 @@ module.exports.updateOrderByParams = async function (idOrder, paramstoupdate) {
let myorder = await Order.findOne({ _id: idOrder }).lean();
if (paramstoupdate && !paramstoupdate.hasOwnProperty('TotalPriceProduct')) {
this.updateTotals(myorder);
await Order.findOneAndUpdate({ _id: idOrder }, { $set: myorder });
}
return myorder;
}
@@ -304,6 +310,7 @@ module.exports.updateTotals = function (order) {
let mypricecalc = 0;
order.TotalPriceProduct = 0;
order.TotalPriceProductCalc = 0;
order.modify_at = new Date();
// Calcolo Sconto
@@ -352,6 +359,8 @@ module.exports.updateTotals = function (order) {
} else {
mypricecalc = (order.price * order.quantity) + (order.price * order.quantitypreordered);
}
order.TotalPriceProductCalc += mypricecalc;
order.TotalPriceProduct += mypricecalc;
return order;

View File

@@ -31,6 +31,7 @@ const OrdersCartSchema = new Schema({
totalQty: { type: Number, default: 0 },
totalQtyPreordered: { type: Number, default: 0 },
totalPrice: { type: Number, default: 0 },
totalPriceCalc: { type: Number, default: 0 },
department: {
type: String, ref: 'Department'
},
@@ -72,11 +73,11 @@ const OrdersCartSchema = new Schema({
date_consegnato: {
type: Date
},
consegnato: {
preparato: {
type: Boolean,
default: false,
},
date_consegnato: {
date_preparato: {
type: Date
},
ricevuto: {
@@ -109,6 +110,16 @@ const OrdersCartSchema = new Schema({
var OrdersCart = module.exports = mongoose.model('OrdersCart', OrdersCartSchema);
function fixupdated(myrec) {
return myrec;
/*if (myrec) {
if (myrec.totalPriceCalc === undefined) {
myrec.totalPriceCalc = myrec.totalPrice;
}
}
return myrec;*/
}
module.exports.findAllIdApp = async function (idapp, userId) {
const myfind = { idapp, userId, deleted: false };
@@ -186,7 +197,9 @@ module.exports.getOrdersCartById = async function (id) {
let query = { _id: ObjectID(id) };
const arrris = await OrdersCart.getOrdersCartByQuery(query);
return arrris && arrris.length > 0 ? arrris[0] : null;
let myrec = arrris && arrris.length > 0 ? arrris[0] : null;
myrec = fixupdated(myrec);
return myrec;
}
@@ -279,6 +292,7 @@ module.exports.getOrdersCartByQuery = async function (query) {
return order;
});
myorderscart = fixupdated(myorderscart);
return myorderscart;
}
@@ -302,7 +316,6 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder, fil
myorderscart = await OrdersCart.getOrdersCartByQuery(query);
/*transform: function(doc, populated) {
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
populated.product = populated.idProduct;
@@ -337,6 +350,7 @@ module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
totalQty: newOrdersCart.totalQty,
totalQtyPreordered: newOrdersCart.totalQtyPreordered,
totalPrice: newOrdersCart.totalPrice,
totalPriceCalc: newOrdersCart.totalPriceCalc ? newOrdersCart.totalPriceCalc : newOrdersCart.totalPrice,
userId: userId,
status: newOrdersCart.status,
numorder: newOrdersCart.numorder,
@@ -413,6 +427,17 @@ module.exports.setConsegnatoById = async function (value, myOrderCart) {
}
module.exports.setPreparatoById = async function (value, myOrderCart) {
let objtoset = {
preparato: value,
date_preparato: new Date(),
};
return await OrdersCart.setFieldInOrdersById(objtoset, myOrderCart);
}
module.exports.setSpeditoById = async function (value, myOrderCart) {
let objtoset = {
@@ -731,6 +756,8 @@ module.exports.updateCmd = async function (ordersCart, status, value, req, optio
}
}
} else if (status === shared_consts.OrderStatus.PREPARED) {
ris = await OrdersCart.setPreparatoById(value, myOrderCart);
} else if (status === shared_consts.OrderStatus.DELIVERED) { // Consegnato
if (value) {
@@ -743,6 +770,8 @@ module.exports.updateCmd = async function (ordersCart, status, value, req, optio
} else if (status === shared_consts.OrderStatus.RECEIVED) {
ris = await OrdersCart.setRicevutoById(value, myOrderCart);
} else if (status === shared_consts.OrderStatus.PREPARED) {
ris = await OrdersCart.setPreparatoById(value, myOrderCart);
} else if (status === shared_consts.OrderStatus.CANCELED) {
await OrdersCart.updateStockQtaPerCancellazioneOrdine(id)
@@ -807,7 +836,7 @@ module.exports.updateOrdersCartByParams = async function (idOrdersCart, paramsto
const ris = await OrdersCart.findOneAndUpdate({ _id: idOrdersCart }, { $set: paramstoupdate });
let myorderscart = await Order.findOne({ _id: idOrder });
let myorderscart = await OrdersCart.getOrdersCartById(idOrdersCart);
return myorderscart;
}
@@ -821,12 +850,13 @@ module.exports.updateOrdersCartTotals = async function (idOrdersCart, update) {
let newOrdersCart = CartClass.constructByCart(orderscart);
await newOrdersCart.updatetotals();
await newOrdersCart.updatecarttotals(false);
if (update) {
await OrdersCart.findOneAndUpdate({ _id: idOrdersCart }, {
$set: {
totalPrice: newOrdersCart.totalPrice,
totalPriceCalc: newOrdersCart.totalPriceCalc,
totalQty: newOrdersCart.totalQty,
note: newOrdersCart.note,
modify_at: new Date(),

View File

@@ -17,7 +17,7 @@ class Cart {
this.items.push(ord)
}
}
this.updatetotals();
this.updatecarttotals(false);
}
initializeFromOrder(order) {
@@ -93,7 +93,9 @@ class Cart {
myitem.order.modify_at = new Date();
this.updatetotals();
myitem.order = Order.updateTotals(myitem.order);
this.updatecarttotals(false);
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order;
}
@@ -130,7 +132,9 @@ class Cart {
myitem.order.quantity -= step;
}
}
this.updatetotals();
myitem.order = Order.updateTotals(myitem.order);
this.updatecarttotals(false);
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order;
}
@@ -147,7 +151,7 @@ class Cart {
this.items[ind] = {};
this.items[ind].order = itemorder;
this.items[ind].order = Order.updateTotals(this.items[ind].order);
this.updatetotals();
this.updatecarttotals(false);
return ind;
}
@@ -155,7 +159,7 @@ class Cart {
removeItem(orderId) {
// this.items.push(itemorder);
this.items = this.items.filter(item => item.order._id.toString() !== orderId.toString());
this.updatetotals();
this.updatecarttotals(false);
}
generateModel() {
@@ -164,6 +168,7 @@ class Cart {
idapp: this.idapp,
items: this.generateArray(),
totalQty: this.totalQty,
totalPriceCalc: this.totalPriceCalc,
totalPrice: this.totalPrice,
userId: this.userId,
department: this.department,
@@ -177,10 +182,11 @@ class Cart {
return null;
}
updatetotals() {
updatecarttotals(updatecalcprice) {
try {
this.totalQty = 0;
this.totalPrice = 0;
this.totalPriceCalc = 0;
for (const rec in this.items) {
let mypricecalc = 0;
@@ -188,8 +194,11 @@ class Cart {
if (!order) {
order = this.items[rec];
}
order.TotalPriceProductCalc = 0;
if (updatecalcprice) {
order.TotalPriceProduct = 0;
this.totalQty += order.quantity + order.quantitypreordered;
}
// Calcolo Sconto
let sconti_da_applicare = [];
@@ -233,14 +242,21 @@ class Cart {
if (qtanonscontata > 0) {
mypricecalc += order.price * qtanonscontata;
}
} else {
mypricecalc = (order.price * order.quantity) + (order.price * order.quantitypreordered);
}
order.TotalPriceProductCalc += mypricecalc;
if (updatecalcprice) {
order.TotalPriceProduct += mypricecalc;
}
}
// Qui lo calcolo sempre, anche se lo cambio manualmente
this.totalPriceCalc += mypricecalc;
this.totalPrice += order.TotalPriceProduct;
}
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
} catch (e) {
console.error('Err: ', e);
}

View File

@@ -101,7 +101,7 @@ router.post('/:userId', authenticate, async function (req, res, next) {
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null, myord: null, msgerr: 'Non è possibile acquistare nello stesso ordine, su negozi differenti!' });
}
} else {
await newCart.updatetotals();
await newCart.updatecarttotals(true);
const arrord = await Order.getTotalOrderById(order._id);
myord = arrord ? arrord[0] : null;
}
@@ -270,6 +270,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
items: mycart.items,
totalQty: mycart.totalQty,
totalPrice: mycart.totalPrice,
totalPriceCalc: mycart.totalPriceCalc,
userId,
status,
note,
@@ -324,14 +325,6 @@ 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;*/
}

View File

@@ -66,12 +66,19 @@ router.post('/updateord', authenticate, async (req, res) => {
router.post('/update', authenticate, async (req, res) => {
let orderscart = null;
const idOrdersCart = req.body.idOrdersCart;
const paramstoupdate = req.body.paramstoupdate;
try {
// Aggiorno Orderscart con i parametri passati
await OrdersCart.updateOrdersCartByParams(idOrdersCart, paramstoupdate);
orderscart = await OrdersCart.updateOrdersCartByParams(idOrdersCart, paramstoupdate);
let updatetotals = true;
if (paramstoupdate && paramstoupdate.hasOwnProperty('totalPrice')) {
updatetotals = false;
}
if (updatetotals) {
// Aggiorno il Totale degli Ordini (OrdersCart)
await OrdersCart.updateOrdersCartTotals(idOrdersCart, true)
@@ -81,6 +88,9 @@ router.post('/update', authenticate, async (req, res) => {
console.log('ERR:', err);
res.status(400).send();
});
} else {
return res.send({ code: server_constants.RIS_CODE_OK, orderscart });
}
} catch (e) {
console.error('Err', e);
}

View File

@@ -1330,6 +1330,22 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) {
} catch (e) {
console.error('Err:', e);
}
} else if (mydata.dbop === 'CopyPriceToCalc') {
try {
const arrrec = await OrdersCart.find({}).lean();
for (const rec of arrrec) {
await OrdersCart.findByIdAndUpdate(rec._id, { $set: { totalPriceCalc: rec.totalPrice } })
}
const arrrec2 = await Order.find({}).lean();
for (const rec of arrrec2) {
await Order.findByIdAndUpdate(rec._id, { $set: { TotalPriceProductCalc: rec.TotalPriceProduct } })
}
} catch (e) {
console.error('Err:', e);
}
} else if (mydata.dbop === 'dropAllCarts') {

View File

@@ -429,6 +429,7 @@ module.exports = {
SHIPPED: 6, //Spedito
RECEIVED: 7,
CANCELED: 10,
PREPARED: 15,
DELETE_REALLY: 20,
},
@@ -442,6 +443,7 @@ module.exports = {
RECEIVED: { label: 'Ricevuti', value: 7, icon: '', color: 'text-blue' }, //RECEIVED
COMPLETATI: { label: 'Completati', value: 8, icon: 'fas fa-check', color: 'text-blue' }, //COMPLETED
CANCELLATI: { label: 'Cancellati', value: 10, icon: 'delete', color: 'text-red' }, //CANCELED
PREPARED: { label: 'Preparati', value: 15, icon: 'fas fa-archive', color: 'text-blue' },
},
OrderStatusStr: [
@@ -457,6 +459,10 @@ module.exports = {
label: 'Ordine in Lavorazione',
value: 2,
},
{
label: 'Preparato',
value: 15,
},
{
label: 'Ordine Confermato',
value: 3,
@@ -484,14 +490,6 @@ module.exports = {
],
OrderStatusView: {
CHECKOUT_SENT: 2,
ORDER_CONFIRMED: 3,
PAYED: 4,
RECEIVED: 6,
CANCELED: 10,
},
ConfSite: {
Notif_Reg_Bot_ToManagers: 1,
Notif_Reg_Push_Admin: 2,