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

View File

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

View File

@@ -17,7 +17,7 @@ class Cart {
this.items.push(ord) this.items.push(ord)
} }
} }
this.updatetotals(); this.updatecarttotals(false);
} }
initializeFromOrder(order) { initializeFromOrder(order) {
@@ -62,7 +62,7 @@ class Cart {
} else { } else {
return true; return true;
} }
} }
@@ -79,7 +79,7 @@ class Cart {
else if (myitem.order.quantity >= 20) else if (myitem.order.quantity >= 20)
step = stepmin < 5 ? 5 : stepmin step = stepmin < 5 ? 5 : stepmin
myitem.order.quantity += step; myitem.order.quantity += step;
} else { } else {
if (myitem.order.quantitypreordered === 0) if (myitem.order.quantitypreordered === 0)
step = myitem.order.product.minBuyQty ?? stepmin step = myitem.order.product.minBuyQty ?? stepmin
@@ -88,12 +88,14 @@ class Cart {
else if (myitem.order.quantitypreordered >= 20) else if (myitem.order.quantitypreordered >= 20)
step = stepmin < 5 ? 5 : stepmin step = stepmin < 5 ? 5 : stepmin
myitem.order.quantitypreordered += step; myitem.order.quantitypreordered += step;
} }
myitem.order.modify_at = new Date(); 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 }); await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order; return myitem.order;
} }
@@ -105,7 +107,7 @@ class Cart {
if (myproduct.quantityAvailable > 0) { if (myproduct.quantityAvailable > 0) {
if (myorder.quantity === minqta) if (myorder.quantity === minqta)
step = minqta step = minqta
else{ else {
if ((myorder.quantity - step) < 0) if ((myorder.quantity - step) < 0)
step = myorder.quantity - step step = myorder.quantity - step
} }
@@ -130,7 +132,9 @@ class Cart {
myitem.order.quantity -= step; 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 }); await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order; return myitem.order;
} }
@@ -147,7 +151,7 @@ class Cart {
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.items[ind].order = Order.updateTotals(this.items[ind].order);
this.updatetotals(); this.updatecarttotals(false);
return ind; return ind;
} }
@@ -155,7 +159,7 @@ class Cart {
removeItem(orderId) { removeItem(orderId) {
// this.items.push(itemorder); // this.items.push(itemorder);
this.items = this.items.filter(item => item.order._id.toString() !== orderId.toString()); this.items = this.items.filter(item => item.order._id.toString() !== orderId.toString());
this.updatetotals(); this.updatecarttotals(false);
} }
generateModel() { generateModel() {
@@ -164,6 +168,7 @@ class Cart {
idapp: this.idapp, idapp: this.idapp,
items: this.generateArray(), items: this.generateArray(),
totalQty: this.totalQty, totalQty: this.totalQty,
totalPriceCalc: this.totalPriceCalc,
totalPrice: this.totalPrice, totalPrice: this.totalPrice,
userId: this.userId, userId: this.userId,
department: this.department, department: this.department,
@@ -177,10 +182,11 @@ class Cart {
return null; return null;
} }
updatetotals() { updatecarttotals(updatecalcprice) {
try { try {
this.totalQty = 0; this.totalQty = 0;
this.totalPrice = 0; this.totalPrice = 0;
this.totalPriceCalc = 0;
for (const rec in this.items) { for (const rec in this.items) {
let mypricecalc = 0; let mypricecalc = 0;
@@ -188,8 +194,11 @@ class Cart {
if (!order) { if (!order) {
order = this.items[rec]; order = this.items[rec];
} }
order.TotalPriceProduct = 0; order.TotalPriceProductCalc = 0;
this.totalQty += order.quantity + order.quantitypreordered; if (updatecalcprice) {
order.TotalPriceProduct = 0;
this.totalQty += order.quantity + order.quantitypreordered;
}
// Calcolo Sconto // Calcolo Sconto
let sconti_da_applicare = []; let sconti_da_applicare = [];
@@ -233,14 +242,21 @@ class Cart {
if (qtanonscontata > 0) { if (qtanonscontata > 0) {
mypricecalc += order.price * qtanonscontata; mypricecalc += order.price * qtanonscontata;
} }
} else { } else {
mypricecalc = (order.price * order.quantity) + (order.price * order.quantitypreordered); mypricecalc = (order.price * order.quantity) + (order.price * order.quantitypreordered);
order.TotalPriceProductCalc += mypricecalc;
if (updatecalcprice) {
order.TotalPriceProduct += mypricecalc;
}
} }
order.TotalPriceProduct += mypricecalc; // Qui lo calcolo sempre, anche se lo cambio manualmente
this.totalPriceCalc += mypricecalc;
this.totalPrice += order.TotalPriceProduct; this.totalPrice += order.TotalPriceProduct;
} }
this.totalPrice = parseFloat(this.totalPrice.toFixed(2)) this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
} catch (e) { } catch (e) {
console.error('Err: ', 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!' }); return res.send({ code: server_constants.RIS_CODE_ERR, cart: null, myord: null, msgerr: 'Non è possibile acquistare nello stesso ordine, su negozi differenti!' });
} }
} else { } else {
await newCart.updatetotals(); await newCart.updatecarttotals(true);
const arrord = await Order.getTotalOrderById(order._id); const arrord = await Order.getTotalOrderById(order._id);
myord = arrord ? arrord[0] : null; myord = arrord ? arrord[0] : null;
} }
@@ -270,6 +270,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
items: mycart.items, items: mycart.items,
totalQty: mycart.totalQty, totalQty: mycart.totalQty,
totalPrice: mycart.totalPrice, totalPrice: mycart.totalPrice,
totalPriceCalc: mycart.totalPriceCalc,
userId, userId,
status, status,
note, 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,21 +66,31 @@ router.post('/updateord', authenticate, async (req, res) => {
router.post('/update', authenticate, async (req, res) => { router.post('/update', authenticate, async (req, res) => {
let orderscart = null;
const idOrdersCart = req.body.idOrdersCart; const idOrdersCart = req.body.idOrdersCart;
const paramstoupdate = req.body.paramstoupdate; const paramstoupdate = req.body.paramstoupdate;
try { try {
// Aggiorno Orderscart con i parametri passati // Aggiorno Orderscart con i parametri passati
await OrdersCart.updateOrdersCartByParams(idOrdersCart, paramstoupdate); orderscart = await OrdersCart.updateOrdersCartByParams(idOrdersCart, paramstoupdate);
// Aggiorno il Totale degli Ordini (OrdersCart) let updatetotals = true;
await OrdersCart.updateOrdersCartTotals(idOrdersCart, true) if (paramstoupdate && paramstoupdate.hasOwnProperty('totalPrice')) {
.then((orderscart) => { updatetotals = false;
return res.send({ code: server_constants.RIS_CODE_OK, orderscart }); }
}).catch(err => { if (updatetotals) {
console.log('ERR:', err);
res.status(400).send(); // Aggiorno il Totale degli Ordini (OrdersCart)
}); await OrdersCart.updateOrdersCartTotals(idOrdersCart, true)
.then((orderscart) => {
return res.send({ code: server_constants.RIS_CODE_OK, orderscart });
}).catch(err => {
console.log('ERR:', err);
res.status(400).send();
});
} else {
return res.send({ code: server_constants.RIS_CODE_OK, orderscart });
}
} catch (e) { } catch (e) {
console.error('Err', e); console.error('Err', e);
} }

View File

@@ -1330,6 +1330,22 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) {
} catch (e) { } catch (e) {
console.error('Err:', 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') { } else if (mydata.dbop === 'dropAllCarts') {

View File

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