- corretto la gestione degli Sconti
- Duplicare un Catalogo
This commit is contained in:
@@ -2,14 +2,13 @@ const mongoose = require('mongoose').set('debug', false);
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
mongoose.level = 'F';
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true
|
||||
mongoose.plugin((schema) => {
|
||||
schema.options.usePushEach = true;
|
||||
});
|
||||
|
||||
|
||||
const CartSchema = new Schema({
|
||||
idapp: {
|
||||
type: String,
|
||||
@@ -18,13 +17,14 @@ const CartSchema = new Schema({
|
||||
totalQty: { type: Number, default: 0 },
|
||||
totalPrice: { type: Number, default: 0 },
|
||||
totalPriceCalc: { type: Number, default: 0 },
|
||||
totalPriceIntero: { type: Number, default: 0 },
|
||||
department: {
|
||||
type: String, ref: 'Department',
|
||||
type: String,
|
||||
ref: 'Department',
|
||||
},
|
||||
items: [
|
||||
{
|
||||
order:
|
||||
{ type: Schema.Types.ObjectId, ref: 'Order' },
|
||||
order: { type: Schema.Types.ObjectId, ref: 'Order' },
|
||||
},
|
||||
],
|
||||
note: {
|
||||
@@ -33,6 +33,9 @@ const CartSchema = new Schema({
|
||||
codice_sconto: {
|
||||
type: String,
|
||||
},
|
||||
descr_sconto: {
|
||||
type: String,
|
||||
},
|
||||
note_ordine_gas: {
|
||||
type: String,
|
||||
},
|
||||
@@ -41,7 +44,7 @@ const CartSchema = new Schema({
|
||||
},
|
||||
});
|
||||
|
||||
var Cart = module.exports = mongoose.model('Cart', CartSchema);
|
||||
var Cart = (module.exports = mongoose.model('Cart', CartSchema));
|
||||
|
||||
module.exports.findAllIdApp = async function (idapp, userId) {
|
||||
const myfind = { idapp, userId };
|
||||
@@ -51,47 +54,76 @@ module.exports.findAllIdApp = async function (idapp, userId) {
|
||||
|
||||
module.exports.getCartByUserId = async function (uid, idapp) {
|
||||
try {
|
||||
const Order = require('../models/order');
|
||||
const mycart = await getCart(uid, idapp);
|
||||
if (!mycart) return null;
|
||||
|
||||
let query = { userId: uid, idapp };
|
||||
const mycart = await Cart.findOne(query).lean();
|
||||
await updateOrderDetails(mycart.items);
|
||||
filterValidItems(mycart);
|
||||
|
||||
if (!!mycart) {
|
||||
for (const idkey in mycart.items) {
|
||||
try {
|
||||
let idorder = mycart.items[idkey]._id.toString();
|
||||
let myorder = mycart.items[idkey].order;
|
||||
if (!!myorder) {
|
||||
idorder = mycart.items[idkey].order._id.toString();
|
||||
}
|
||||
if (idorder) {
|
||||
let myord = await Order.getTotalOrderById(idorder);
|
||||
if (myord.length > 0) {
|
||||
mycart.items[idkey].order = myord[0];
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('err', e);
|
||||
}
|
||||
}
|
||||
|
||||
mycart.newitems = []
|
||||
for (let item of mycart.items) {
|
||||
if (item.order && item.order.hasOwnProperty('idapp') && (item.order.quantity > 0 || item.order.quantitypreordered > 0))
|
||||
mycart.newitems.push(item)
|
||||
}
|
||||
mycart.items = [...mycart.newitems]
|
||||
mycart.newitems = []
|
||||
|
||||
return mycart;
|
||||
}
|
||||
return null;
|
||||
return mycart;
|
||||
} catch (e) {
|
||||
console.log('getCartByUserId err', e);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports.getCartCompletoByCartId = async function (id_cart, idapp) {
|
||||
try {
|
||||
const mycart = await getCartById(id_cart, idapp);
|
||||
if (!mycart) return null;
|
||||
|
||||
await updateOrderDetails(mycart.items);
|
||||
filterValidItems(mycart);
|
||||
|
||||
return mycart;
|
||||
} catch (e) {
|
||||
console.log('getCartByUserId err', e);
|
||||
}
|
||||
};
|
||||
|
||||
// Recupera il carrello per l'utente e l'app
|
||||
async function getCart(uid, idapp) {
|
||||
const query = { userId: uid, idapp };
|
||||
return await Cart.findOne(query).lean();
|
||||
}
|
||||
|
||||
async function getCartById(id_cart, idapp) {
|
||||
return await Cart.findOne({_id: id_cart}).lean();
|
||||
}
|
||||
|
||||
// Aggiorna i dettagli dell'ordine per ogni articolo nel carrello
|
||||
async function updateOrderDetails(items) {
|
||||
const Order = require('../models/order');
|
||||
|
||||
for (const item of items) {
|
||||
try {
|
||||
const idorder = item.order ? item.order._id.toString() : item._id.toString();
|
||||
const myord = await Order.getTotalOrderById(idorder);
|
||||
|
||||
if (myord.length > 0) {
|
||||
item.order = myord[0];
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('err', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Filtra solo gli articoli validi (con quantità > 0 o pre-ordinati)
|
||||
function filterValidItems(mycart) {
|
||||
mycart.newitems = [];
|
||||
for (let item of mycart.items) {
|
||||
if (
|
||||
item.order &&
|
||||
item.order.hasOwnProperty('idapp') &&
|
||||
(item.order.quantity > 0 || item.order.quantitypreordered > 0)
|
||||
) {
|
||||
mycart.newitems.push(item);
|
||||
}
|
||||
}
|
||||
mycart.items = [...mycart.newitems];
|
||||
mycart.newitems = [];
|
||||
}
|
||||
|
||||
module.exports.updateCartByUserId = async function (userId, newCart) {
|
||||
const query = { userId: userId };
|
||||
|
||||
@@ -108,6 +140,7 @@ module.exports.updateCartByUserId = async function (userId, newCart) {
|
||||
items: newCart.items,
|
||||
totalQty: newCart.totalQty,
|
||||
totalPrice: newCart.totalPrice,
|
||||
totalPriceIntero: newCart.totalPriceIntero,
|
||||
totalPriceCalc: newCart.totalPriceCalc,
|
||||
userId: userId,
|
||||
},
|
||||
@@ -136,30 +169,40 @@ module.exports.updateCartByCartId = async function (cartId, newCart) {
|
||||
const totalQty = newCart.totalQty;
|
||||
const totalPrice = newCart.totalPrice;
|
||||
const totalPriceCalc = newCart.totalPriceCalc;
|
||||
const totalPriceIntero = newCart.totalPriceIntero;
|
||||
const note = newCart.note;
|
||||
const codice_sconto = newCart.codice_sconto;
|
||||
const descr_sconto = newCart.descr_sconto;
|
||||
const note_ordine_gas = newCart.note_ordine_gas;
|
||||
|
||||
const modify_at = new Date();
|
||||
|
||||
return await Cart.findOneAndUpdate({ _id: cartId }, {
|
||||
$set: {
|
||||
items,
|
||||
totalPrice,
|
||||
totalPriceCalc,
|
||||
totalQty,
|
||||
note,
|
||||
codice_sconto,
|
||||
note_ordine_gas,
|
||||
modify_at: new Date(),
|
||||
return await Cart.findOneAndUpdate(
|
||||
{ _id: cartId },
|
||||
{
|
||||
$set: {
|
||||
items,
|
||||
totalPrice,
|
||||
totalPriceCalc,
|
||||
totalPriceIntero,
|
||||
totalQty,
|
||||
note,
|
||||
codice_sconto,
|
||||
descr_sconto,
|
||||
note_ordine_gas,
|
||||
modify_at: new Date(),
|
||||
},
|
||||
},
|
||||
}, { new: false }).lean().then((ris) => {
|
||||
return ris;
|
||||
}).catch(err => {
|
||||
console.log('err', err);
|
||||
return null;
|
||||
});
|
||||
|
||||
{ new: false }
|
||||
)
|
||||
.lean()
|
||||
.then((ris) => {
|
||||
return ris;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log('err', err);
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.deleteCartByCartId = async function (cartId) {
|
||||
@@ -170,8 +213,8 @@ module.exports.createCart = async function (newCart) {
|
||||
return await newCart.save();
|
||||
};
|
||||
|
||||
|
||||
Cart.createIndexes()
|
||||
.then(() => { })
|
||||
.catch((err) => { throw err; });
|
||||
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
@@ -259,12 +259,12 @@ module.exports.getOrderByID = function (id, callback) {
|
||||
Order.findById(id, callback);
|
||||
};
|
||||
|
||||
module.exports.createOrder = async function (order) {
|
||||
module.exports.createOrder = async function (order, codice_sconto) {
|
||||
try {
|
||||
if (order.idGasordine === '') {
|
||||
order.idGasordine = undefined;
|
||||
}
|
||||
await Order.updateTotals(order);
|
||||
await Order.updateTotals(order, codice_sconto);
|
||||
return await Order.create(order).then((ris) => {
|
||||
if (!!ris) return ris._id;
|
||||
return null;
|
||||
@@ -317,15 +317,27 @@ function applyNonCumulativeDiscounts(order, discounts) {
|
||||
|
||||
while (qtadascontare > 0) {
|
||||
let scontoapplicato = null;
|
||||
|
||||
// Filtriamo gli sconti non cumulativi
|
||||
for (const sconto of getNonCumulativeDiscounts(discounts)) {
|
||||
if (qtadascontare >= sconto.qta) {
|
||||
// Se la quantità da scontare è maggiore o uguale alla quantità dello sconto, applica lo sconto completo
|
||||
scontoapplicato = { ...sconto, qtadascontare: sconto.qta };
|
||||
break; // Esci dal ciclo, abbiamo trovato lo sconto applicabile
|
||||
} else if (qtadascontare > 0) {
|
||||
// Se la quantità da scontare è inferiore allo sconto, applica solo la quantità disponibile
|
||||
scontoapplicato = { ...sconto, qtadascontare: qtadascontare };
|
||||
qtadascontare = 0; // Abbiamo finito le quantità da scontare
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Se c'è uno sconto applicato, aggiungilo alla lista e riduci la quantità
|
||||
if (scontoapplicato && scontoapplicato.qtadascontare > 0) {
|
||||
sconti_da_applicare.push(scontoapplicato);
|
||||
qtadascontare -= scontoapplicato.qtadascontare;
|
||||
} else {
|
||||
// Se non ci sono più sconti da applicare, la quantità rimasta è non scontata
|
||||
qtanonscontata = qtadascontare;
|
||||
qtadascontare = 0;
|
||||
}
|
||||
@@ -356,8 +368,10 @@ function calculateFullPrice(order) {
|
||||
return order.price * order.quantity + order.price * order.quantitypreordered;
|
||||
}
|
||||
|
||||
module.exports.updateTotals = async function (order) {
|
||||
module.exports.updateTotals = async function (order, codice_sconto) {
|
||||
try {
|
||||
const CartClass = require('../modules/Cart');
|
||||
|
||||
if (!order) return;
|
||||
|
||||
initOrderTotals(order);
|
||||
@@ -368,12 +382,8 @@ module.exports.updateTotals = async function (order) {
|
||||
|
||||
let recscontisticheTrovate = [];
|
||||
|
||||
if (order?.codice_sconto) {
|
||||
recscontisticheTrovate = await Scontistica.find({
|
||||
idapp: order.idapp,
|
||||
code: order?.codice_sconto?.toUpperCase(),
|
||||
applica: shared_consts.SCONTI_APPLICA.A_TUTTI,
|
||||
}).lean();
|
||||
if (codice_sconto) {
|
||||
recscontisticheTrovate = await CartClass.getRecSconto(order.idapp, codice_sconto, true);
|
||||
}
|
||||
|
||||
// Se ha inserito una scontistica che esiste...
|
||||
@@ -390,6 +400,7 @@ module.exports.updateTotals = async function (order) {
|
||||
order.TotalPriceProductCalc += total;
|
||||
order.TotalPriceProduct += total;
|
||||
order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2));
|
||||
order.codice_sconto = codice_sconto;
|
||||
|
||||
return order;
|
||||
} catch (e) {
|
||||
|
||||
@@ -33,6 +33,7 @@ const OrdersCartSchema = new Schema({
|
||||
totalQtyPreordered: { type: Number, default: 0 },
|
||||
totalPrice: { type: Number, default: 0 },
|
||||
totalPriceCalc: { type: Number, default: 0 },
|
||||
totalPriceIntero: { type: Number, default: 0 },
|
||||
department: {
|
||||
type: String, ref: 'Department'
|
||||
},
|
||||
@@ -94,6 +95,9 @@ const OrdersCartSchema = new Schema({
|
||||
codice_sconto: {
|
||||
type: String
|
||||
},
|
||||
descr_sconto: {
|
||||
type: String
|
||||
},
|
||||
note_per_gestore: {
|
||||
type: String
|
||||
},
|
||||
@@ -571,12 +575,14 @@ module.exports.updateOrdersCartById = async function(id, newOrdersCart, callback
|
||||
totalQtyPreordered: newOrdersCart.totalQtyPreordered,
|
||||
totalPrice: newOrdersCart.totalPrice,
|
||||
totalPriceCalc: newOrdersCart.totalPriceCalc ? newOrdersCart.totalPriceCalc : newOrdersCart.totalPrice,
|
||||
totalPriceIntero: newOrdersCart.totalPriceIntero ? newOrdersCart.totalPriceIntero : newOrdersCart.totalPriceIntero,
|
||||
userId: userId,
|
||||
status: newOrdersCart.status,
|
||||
numorder: newOrdersCart.numorder,
|
||||
numord_pers: newOrdersCart.numord_pers,
|
||||
note: newOrdersCart.note,
|
||||
codice_sconto: newOrdersCart.codice_sconto,
|
||||
descr_sconto: newOrdersCart.descr_sconto,
|
||||
modify_at: new Date(),
|
||||
}
|
||||
},
|
||||
@@ -1090,9 +1096,11 @@ module.exports.updateOrdersCartTotals = async function (idOrdersCart, update) {
|
||||
$set: {
|
||||
totalPrice: newOrdersCart.totalPrice,
|
||||
totalPriceCalc: newOrdersCart.totalPriceCalc,
|
||||
totalPriceIntero: newOrdersCart.totalPriceIntero,
|
||||
totalQty: newOrdersCart.totalQty,
|
||||
note: newOrdersCart.note,
|
||||
codice_sconto: newOrdersCart.codice_sconto,
|
||||
descr_sconto: newOrdersCart.descr_sconto,
|
||||
modify_at: new Date(),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@ const scontisticaSchema = new Schema({
|
||||
idapp: {
|
||||
type: String,
|
||||
},
|
||||
attivo: {
|
||||
type: Boolean,
|
||||
},
|
||||
code: {
|
||||
type: String,
|
||||
},
|
||||
@@ -55,7 +58,7 @@ module.exports.executeQueryTable = function (idapp, params) {
|
||||
};
|
||||
|
||||
module.exports.findAllIdApp = async function (idapp) {
|
||||
const myfind = { idapp };
|
||||
const myfind = { idapp, attivo: true };
|
||||
|
||||
return await Scontistica.find(myfind);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user