- corretto la gestione degli Sconti

- Duplicare un Catalogo
This commit is contained in:
Surya Paolo
2025-06-11 01:05:25 +02:00
parent d1d4b73da0
commit 25377090c1
6 changed files with 242 additions and 106 deletions

View File

@@ -2,14 +2,13 @@ const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
mongoose.Promise = global.Promise; mongoose.Promise = global.Promise;
mongoose.level = "F"; mongoose.level = 'F';
// Resolving error Unknown modifier: $pushAll // Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => { mongoose.plugin((schema) => {
schema.options.usePushEach = true schema.options.usePushEach = true;
}); });
const CartSchema = new Schema({ const CartSchema = new Schema({
idapp: { idapp: {
type: String, type: String,
@@ -18,13 +17,14 @@ const CartSchema = new Schema({
totalQty: { type: Number, default: 0 }, totalQty: { type: Number, default: 0 },
totalPrice: { type: Number, default: 0 }, totalPrice: { type: Number, default: 0 },
totalPriceCalc: { type: Number, default: 0 }, totalPriceCalc: { type: Number, default: 0 },
totalPriceIntero: { type: Number, default: 0 },
department: { department: {
type: String, ref: 'Department', type: String,
ref: 'Department',
}, },
items: [ items: [
{ {
order: order: { type: Schema.Types.ObjectId, ref: 'Order' },
{ type: Schema.Types.ObjectId, ref: 'Order' },
}, },
], ],
note: { note: {
@@ -33,6 +33,9 @@ const CartSchema = new Schema({
codice_sconto: { codice_sconto: {
type: String, type: String,
}, },
descr_sconto: {
type: String,
},
note_ordine_gas: { note_ordine_gas: {
type: String, 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) { module.exports.findAllIdApp = async function (idapp, userId) {
const myfind = { idapp, userId }; const myfind = { idapp, userId };
@@ -51,47 +54,76 @@ module.exports.findAllIdApp = async function (idapp, userId) {
module.exports.getCartByUserId = async function (uid, idapp) { module.exports.getCartByUserId = async function (uid, idapp) {
try { try {
const mycart = await getCart(uid, idapp);
if (!mycart) return null;
await updateOrderDetails(mycart.items);
filterValidItems(mycart);
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'); const Order = require('../models/order');
let query = { userId: uid, idapp }; for (const item of items) {
const mycart = await Cart.findOne(query).lean();
if (!!mycart) {
for (const idkey in mycart.items) {
try { try {
let idorder = mycart.items[idkey]._id.toString(); const idorder = item.order ? item.order._id.toString() : item._id.toString();
let myorder = mycart.items[idkey].order; const myord = await Order.getTotalOrderById(idorder);
if (!!myorder) {
idorder = mycart.items[idkey].order._id.toString();
}
if (idorder) {
let myord = await Order.getTotalOrderById(idorder);
if (myord.length > 0) { if (myord.length > 0) {
mycart.items[idkey].order = myord[0]; item.order = myord[0];
}
} }
} catch (e) { } catch (e) {
console.log('err', e); console.log('err', e);
} }
} }
}
mycart.newitems = [] // Filtra solo gli articoli validi (con quantità > 0 o pre-ordinati)
function filterValidItems(mycart) {
mycart.newitems = [];
for (let item of mycart.items) { for (let item of mycart.items) {
if (item.order && item.order.hasOwnProperty('idapp') && (item.order.quantity > 0 || item.order.quantitypreordered > 0)) if (
mycart.newitems.push(item) 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; mycart.items = [...mycart.newitems];
} catch (e) { mycart.newitems = [];
console.log('getCartByUserId err', e);
} }
};
module.exports.updateCartByUserId = async function (userId, newCart) { module.exports.updateCartByUserId = async function (userId, newCart) {
const query = { userId: userId }; const query = { userId: userId };
@@ -108,6 +140,7 @@ module.exports.updateCartByUserId = async function (userId, newCart) {
items: newCart.items, items: newCart.items,
totalQty: newCart.totalQty, totalQty: newCart.totalQty,
totalPrice: newCart.totalPrice, totalPrice: newCart.totalPrice,
totalPriceIntero: newCart.totalPriceIntero,
totalPriceCalc: newCart.totalPriceCalc, totalPriceCalc: newCart.totalPriceCalc,
userId: userId, userId: userId,
}, },
@@ -136,30 +169,40 @@ module.exports.updateCartByCartId = async function (cartId, newCart) {
const totalQty = newCart.totalQty; const totalQty = newCart.totalQty;
const totalPrice = newCart.totalPrice; const totalPrice = newCart.totalPrice;
const totalPriceCalc = newCart.totalPriceCalc; const totalPriceCalc = newCart.totalPriceCalc;
const totalPriceIntero = newCart.totalPriceIntero;
const note = newCart.note; const note = newCart.note;
const codice_sconto = newCart.codice_sconto; const codice_sconto = newCart.codice_sconto;
const descr_sconto = newCart.descr_sconto;
const note_ordine_gas = newCart.note_ordine_gas; const note_ordine_gas = newCart.note_ordine_gas;
const modify_at = new Date(); const modify_at = new Date();
return await Cart.findOneAndUpdate({ _id: cartId }, { return await Cart.findOneAndUpdate(
{ _id: cartId },
{
$set: { $set: {
items, items,
totalPrice, totalPrice,
totalPriceCalc, totalPriceCalc,
totalPriceIntero,
totalQty, totalQty,
note, note,
codice_sconto, codice_sconto,
descr_sconto,
note_ordine_gas, note_ordine_gas,
modify_at: new Date(), modify_at: new Date(),
}, },
}, { new: false }).lean().then((ris) => { },
{ new: false }
)
.lean()
.then((ris) => {
return ris; return ris;
}).catch(err => { })
.catch((err) => {
console.log('err', err); console.log('err', err);
return null; return null;
}); });
}; };
module.exports.deleteCartByCartId = async function (cartId) { module.exports.deleteCartByCartId = async function (cartId) {
@@ -170,8 +213,8 @@ module.exports.createCart = async function (newCart) {
return await newCart.save(); return await newCart.save();
}; };
Cart.createIndexes() Cart.createIndexes()
.then(() => {}) .then(() => {})
.catch((err) => { throw err; }); .catch((err) => {
throw err;
});

View File

@@ -259,12 +259,12 @@ module.exports.getOrderByID = function (id, callback) {
Order.findById(id, callback); Order.findById(id, callback);
}; };
module.exports.createOrder = async function (order) { module.exports.createOrder = async function (order, codice_sconto) {
try { try {
if (order.idGasordine === '') { if (order.idGasordine === '') {
order.idGasordine = undefined; order.idGasordine = undefined;
} }
await Order.updateTotals(order); await Order.updateTotals(order, codice_sconto);
return await Order.create(order).then((ris) => { return await Order.create(order).then((ris) => {
if (!!ris) return ris._id; if (!!ris) return ris._id;
return null; return null;
@@ -317,15 +317,27 @@ function applyNonCumulativeDiscounts(order, discounts) {
while (qtadascontare > 0) { while (qtadascontare > 0) {
let scontoapplicato = null; let scontoapplicato = null;
// Filtriamo gli sconti non cumulativi
for (const sconto of getNonCumulativeDiscounts(discounts)) { for (const sconto of getNonCumulativeDiscounts(discounts)) {
if (qtadascontare >= sconto.qta) { 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 }; 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) { if (scontoapplicato && scontoapplicato.qtadascontare > 0) {
sconti_da_applicare.push(scontoapplicato); sconti_da_applicare.push(scontoapplicato);
qtadascontare -= scontoapplicato.qtadascontare; qtadascontare -= scontoapplicato.qtadascontare;
} else { } else {
// Se non ci sono più sconti da applicare, la quantità rimasta è non scontata
qtanonscontata = qtadascontare; qtanonscontata = qtadascontare;
qtadascontare = 0; qtadascontare = 0;
} }
@@ -356,8 +368,10 @@ function calculateFullPrice(order) {
return order.price * order.quantity + order.price * order.quantitypreordered; return order.price * order.quantity + order.price * order.quantitypreordered;
} }
module.exports.updateTotals = async function (order) { module.exports.updateTotals = async function (order, codice_sconto) {
try { try {
const CartClass = require('../modules/Cart');
if (!order) return; if (!order) return;
initOrderTotals(order); initOrderTotals(order);
@@ -368,12 +382,8 @@ module.exports.updateTotals = async function (order) {
let recscontisticheTrovate = []; let recscontisticheTrovate = [];
if (order?.codice_sconto) { if (codice_sconto) {
recscontisticheTrovate = await Scontistica.find({ recscontisticheTrovate = await CartClass.getRecSconto(order.idapp, codice_sconto, true);
idapp: order.idapp,
code: order?.codice_sconto?.toUpperCase(),
applica: shared_consts.SCONTI_APPLICA.A_TUTTI,
}).lean();
} }
// Se ha inserito una scontistica che esiste... // Se ha inserito una scontistica che esiste...
@@ -390,6 +400,7 @@ module.exports.updateTotals = async function (order) {
order.TotalPriceProductCalc += total; order.TotalPriceProductCalc += total;
order.TotalPriceProduct += total; order.TotalPriceProduct += total;
order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2)); order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2));
order.codice_sconto = codice_sconto;
return order; return order;
} catch (e) { } catch (e) {

View File

@@ -33,6 +33,7 @@ const OrdersCartSchema = new Schema({
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 }, totalPriceCalc: { type: Number, default: 0 },
totalPriceIntero: { type: Number, default: 0 },
department: { department: {
type: String, ref: 'Department' type: String, ref: 'Department'
}, },
@@ -94,6 +95,9 @@ const OrdersCartSchema = new Schema({
codice_sconto: { codice_sconto: {
type: String type: String
}, },
descr_sconto: {
type: String
},
note_per_gestore: { note_per_gestore: {
type: String type: String
}, },
@@ -571,12 +575,14 @@ module.exports.updateOrdersCartById = async function(id, newOrdersCart, callback
totalQtyPreordered: newOrdersCart.totalQtyPreordered, totalQtyPreordered: newOrdersCart.totalQtyPreordered,
totalPrice: newOrdersCart.totalPrice, totalPrice: newOrdersCart.totalPrice,
totalPriceCalc: newOrdersCart.totalPriceCalc ? newOrdersCart.totalPriceCalc : newOrdersCart.totalPrice, totalPriceCalc: newOrdersCart.totalPriceCalc ? newOrdersCart.totalPriceCalc : newOrdersCart.totalPrice,
totalPriceIntero: newOrdersCart.totalPriceIntero ? newOrdersCart.totalPriceIntero : newOrdersCart.totalPriceIntero,
userId: userId, userId: userId,
status: newOrdersCart.status, status: newOrdersCart.status,
numorder: newOrdersCart.numorder, numorder: newOrdersCart.numorder,
numord_pers: newOrdersCart.numord_pers, numord_pers: newOrdersCart.numord_pers,
note: newOrdersCart.note, note: newOrdersCart.note,
codice_sconto: newOrdersCart.codice_sconto, codice_sconto: newOrdersCart.codice_sconto,
descr_sconto: newOrdersCart.descr_sconto,
modify_at: new Date(), modify_at: new Date(),
} }
}, },
@@ -1090,9 +1096,11 @@ module.exports.updateOrdersCartTotals = async function (idOrdersCart, update) {
$set: { $set: {
totalPrice: newOrdersCart.totalPrice, totalPrice: newOrdersCart.totalPrice,
totalPriceCalc: newOrdersCart.totalPriceCalc, totalPriceCalc: newOrdersCart.totalPriceCalc,
totalPriceIntero: newOrdersCart.totalPriceIntero,
totalQty: newOrdersCart.totalQty, totalQty: newOrdersCart.totalQty,
note: newOrdersCart.note, note: newOrdersCart.note,
codice_sconto: newOrdersCart.codice_sconto, codice_sconto: newOrdersCart.codice_sconto,
descr_sconto: newOrdersCart.descr_sconto,
modify_at: new Date(), modify_at: new Date(),
}, },
} }

View File

@@ -16,6 +16,9 @@ const scontisticaSchema = new Schema({
idapp: { idapp: {
type: String, type: String,
}, },
attivo: {
type: Boolean,
},
code: { code: {
type: String, type: String,
}, },
@@ -55,7 +58,7 @@ module.exports.executeQueryTable = function (idapp, params) {
}; };
module.exports.findAllIdApp = async function (idapp) { module.exports.findAllIdApp = async function (idapp) {
const myfind = { idapp }; const myfind = { idapp, attivo: true };
return await Scontistica.find(myfind); return await Scontistica.find(myfind);
}; };

View File

@@ -27,6 +27,7 @@ class Cart {
this.totalQty = 0; this.totalQty = 0;
this.totalPrice = 0; this.totalPrice = 0;
this.totalPriceCalc = 0; this.totalPriceCalc = 0;
this.totalPriceIntero = 0;
for (const key in this.items) { for (const key in this.items) {
const item = this.items[key]; const item = this.items[key];
@@ -37,6 +38,7 @@ class Cart {
this.totalPrice = parseFloat(this.totalPrice.toFixed(2)); this.totalPrice = parseFloat(this.totalPrice.toFixed(2));
this.totalPriceCalc = parseFloat(this.totalPriceCalc.toFixed(2)); this.totalPriceCalc = parseFloat(this.totalPriceCalc.toFixed(2));
this.totalPriceIntero = parseFloat(this.totalPriceIntero.toFixed(2));
} catch (e) { } catch (e) {
console.error("Errore durante l'aggiornamento del carrello:", e); console.error("Errore durante l'aggiornamento del carrello:", e);
} }
@@ -62,6 +64,7 @@ class Cart {
mynewcart.modify_at = new Date(); mynewcart.modify_at = new Date();
mynewcart.note_ordine_gas = ''; mynewcart.note_ordine_gas = '';
mynewcart.codice_sconto = cart.codice_sconto; mynewcart.codice_sconto = cart.codice_sconto;
mynewcart.descr_sconto = cart.descr_sconto;
return mynewcart; return mynewcart;
} catch (e) { } catch (e) {
@@ -118,7 +121,7 @@ class Cart {
myitem.order.modify_at = new Date(); myitem.order.modify_at = new Date();
myitem.order = await Order.updateTotals(myitem.order); myitem.order = await Order.updateTotals(myitem.order, this.codice_sconto);
await this.updatecarttotals(false); await this.updatecarttotals(false);
await this.updateExtraOrder(); await this.updateExtraOrder();
@@ -164,7 +167,7 @@ class Cart {
myitem.order.quantity -= step; myitem.order.quantity -= step;
} }
} }
myitem.order = await Order.updateTotals(myitem.order); myitem.order = await Order.updateTotals(myitem.order, this.codice_sconto);
await this.updatecarttotals(false); await this.updatecarttotals(false);
await this.updateExtraOrder(); await this.updateExtraOrder();
@@ -182,7 +185,7 @@ class Cart {
let ind = this.items.length; let ind = this.items.length;
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 = await Order.updateTotals(this.items[ind].order, this.codice_sconto);
await this.updatecarttotals(false); await this.updatecarttotals(false);
await this.updateExtraOrder(); await this.updateExtraOrder();
@@ -203,11 +206,13 @@ class Cart {
items: this.generateArray(), items: this.generateArray(),
totalQty: this.totalQty, totalQty: this.totalQty,
totalPriceCalc: this.totalPriceCalc, totalPriceCalc: this.totalPriceCalc,
totalPriceIntero: this.totalPriceIntero,
totalPrice: this.totalPrice, totalPrice: this.totalPrice,
userId: this.userId, userId: this.userId,
department: this.department, department: this.department,
note: this.note, note: this.note,
codice_sconto: this.codice_sconto, codice_sconto: this.codice_sconto,
descr_sconto: this.descr_sconto,
note_ordine_gas: this.note_ordine_gas, note_ordine_gas: this.note_ordine_gas,
modify_at: this.modify_at, modify_at: this.modify_at,
}); });
@@ -220,6 +225,10 @@ class Cart {
async updateOrderTotals(order, updateCalcPrice) { async updateOrderTotals(order, updateCalcPrice) {
order.TotalPriceProductCalc = 0; order.TotalPriceProductCalc = 0;
// PROVO A METTERE SEMPRE CHE MI RICALCOLA IL PREZZO !
// updateCalcPrice = true;
if (updateCalcPrice) { if (updateCalcPrice) {
order.TotalPriceProduct = 0; order.TotalPriceProduct = 0;
} }
@@ -227,14 +236,11 @@ class Cart {
const qty = order.quantity + order.quantitypreordered; const qty = order.quantity + order.quantitypreordered;
this.totalQty += qty; this.totalQty += qty;
let recscontisticheTrovate = await Scontistica.find({ const recscontisticheTrovate = await Cart.getRecSconto(this.idapp, this.codice_sconto, true);
idapp: order.idapp,
code: this.codice_sconto?.toUpperCase(),
applica: shared_consts.SCONTI_APPLICA.A_TUTTI,
}).lean();
const scontiDaUsare = recscontisticheTrovate?.length ? recscontisticheTrovate : order.scontisticas || []; const scontiDaUsare = recscontisticheTrovate?.length ? recscontisticheTrovate : order.scontisticas || [];
const priceCalc = this.calcolaPrezzoScontato(order, qty, scontiDaUsare); const priceCalc = this.calcolaPrezzoScontato(order, qty, scontiDaUsare);
const priceintero = this.calcolaPrezzoScontato(order, qty, []);
order.TotalPriceProductCalc += priceCalc; order.TotalPriceProductCalc += priceCalc;
@@ -243,8 +249,14 @@ class Cart {
order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2)); order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2));
} }
this.totalPriceCalc += priceCalc;
this.totalPrice += order.TotalPriceProduct; this.totalPrice += order.TotalPriceProduct;
this.totalPriceCalc += priceCalc;
this.totalPriceIntero += priceintero;
// if (updateCalcPrice) {
// Aggiorna anche l'ordine associato
await Order.findOneAndUpdate({ _id: order._id }, { $set: order }, { new: false });
// }
} }
calcolaPrezzoScontato(order, qtyTotale, sconti = []) { calcolaPrezzoScontato(order, qtyTotale, sconti = []) {
@@ -259,11 +271,15 @@ class Cart {
// Applica sconti non cumulativi // Applica sconti non cumulativi
while (qtaRimanente > 0) { while (qtaRimanente > 0) {
let scontoScelto = null; let scontoScelto = null;
let scontoVantaggioso = 0;
for (const sconto of sconti.filter((s) => !s.cumulativo)) { for (const sconto of sconti.filter((s) => !s.cumulativo)) {
if (qtaRimanente >= s.qta) { if (qtaRimanente >= sconto.qta) {
scontoScelto = { ...sconto, qtadascontare: s.qta }; const scontoApplicato =
break; // prendi il primo valido (puoi migliorare scegliendo il più vantaggioso) sconto.perc_sconto > 0 ? qtaRimanente * order.price * (1 - sconto.perc_sconto / 100) : sconto.price;
if (scontoApplicato > scontoVantaggioso) {
scontoVantaggioso = scontoApplicato;
scontoScelto = { ...sconto, qtadascontare: qtaRimanente };
}
} }
} }
@@ -330,6 +346,53 @@ class Cart {
} }
return arr; return arr;
} }
static async getRecSconto(idapp, codice_sconto, soloAttivi) {
const condSconto = { $regex: new RegExp(`^${codice_sconto}$`, 'i') };
const query = {
idapp: idapp,
code: condSconto,
applica: shared_consts.SCONTI_APPLICA.A_TUTTI,
};
if (soloAttivi) {
query.attivo = true;
}
const recscontisticheTrovate = await Scontistica.find(query).lean();
return recscontisticheTrovate;
}
async updateCartIntoDB() {
const result = await cartModel.updateCartByUserId(this.userId, {
items: this.items,
totalQty: this.totalQty,
totalPrice: this.totalPrice,
totalPriceCalc: this.totalPriceCalc,
totalPriceIntero: this.totalPriceIntero,
userId: this.userId,
});
return result;
}
async aggiornaCarrello() {
try {
// Rifai i calcoli !
await this.updatecarttotals(true);
await this.updateExtraOrder();
// Aggiorna i calcoli sul DB:
const mycart = await this.updateCartIntoDB();
// ritorna il carrello aggiornato !
return await cartModel.getCartCompletoByCartId(mycart._id, this.idapp);
} catch (e) {
console.error("Errore durante l'aggiornamento del carrello:", e);
}
}
} }
module.exports = Cart; module.exports = Cart;

View File

@@ -55,14 +55,12 @@ async function aggiornaCarrello(mycartpar, userId, idapp) {
if (!mycart) mycart = await Cart.getCartByUserId(userId, idapp); if (!mycart) mycart = await Cart.getCartByUserId(userId, idapp);
if (!mycart) { if (!mycart) return null;
return null;
} mycart = await Cart.getCartCompletoByCartId(mycart._id, idapp);
let newCart = await CartClass.constructByCart(mycart); let newCart = await CartClass.constructByCart(mycart);
// order = await Product.updateProductInOrder(order); if (newCart) return newCart.aggiornaCarrello();
await newCart.updatecarttotals(true);
await newCart.updateExtraOrder();
return newCart; return newCart;
} catch (e) { } catch (e) {
@@ -88,7 +86,7 @@ router.post('/:userId', authenticate, async function (req, res, next) {
// const myorder = Order.getOrderByID(order._id); // const myorder = Order.getOrderByID(order._id);
if (!addqty && !subqty && order) { if (!addqty && !subqty && order) {
order._id = await Order.createOrder(order); order._id = await Order.createOrder(order, mycart.codice_sconto);
if (!order._id) { if (!order._id) {
return res.send({ code: server_constants.RIS_CODE_ERR, cart: 0 }); return res.send({ code: server_constants.RIS_CODE_ERR, cart: 0 });
} }
@@ -210,6 +208,7 @@ router.put('/:userId', authenticate, async function (req, res, next) {
totalQty: newCart.totalQty, totalQty: newCart.totalQty,
totalPrice: newCart.totalPrice, totalPrice: newCart.totalPrice,
totalPriceCalc: newCart.totalPriceCalc, totalPriceCalc: newCart.totalPriceCalc,
totalPriceIntero: newCart.totalPriceIntero,
userId: userId, userId: userId,
}); });
res.json(result); res.json(result);
@@ -223,6 +222,7 @@ router.put('/:userId', authenticate, async function (req, res, next) {
totalQty: newCart.totalQty, totalQty: newCart.totalQty,
totalPrice: newCart.totalPrice, totalPrice: newCart.totalPrice,
totalPriceCalc: newCart.totalPriceCalc, totalPriceCalc: newCart.totalPriceCalc,
totalPriceIntero: newCart.totalPriceIntero,
userId: userId, userId: userId,
}; };
try { try {
@@ -260,12 +260,15 @@ router.post('/:userId/app_sc', authenticate, async function (req, res, next) {
let mycart = null; let mycart = null;
let valido = false; let valido = false;
let errmsg = ''; let errmsg = '';
let recscontisticaTrovata = null;
try { try {
let mycart = await Cart.findOne({ _id: cart_id }).lean(); let mycart = await Cart.getCartCompletoByCartId(cart_id, idapp);
// let mycart = await Cart.findOne({ _id: cart_id }).lean();
if (codice_sconto === 'RIMUOVI') { if (codice_sconto === 'RIMUOVI') {
mycart = await Cart.findOneAndUpdate({ _id: cart_id }, { $set: { codice_sconto: '' } }, { new: true }).lean(); mycart = await Cart.findOneAndUpdate({ _id: cart_id }, { $set: { codice_sconto: '' } }, { new: true }).lean();
mycart = await aggiornaCarrello(mycart, userId, idapp);
return res.send({ mycart, valido, msg: 'Codice Sconto rimosso', rimuovi: true }); return res.send({ mycart, valido, msg: 'Codice Sconto rimosso', rimuovi: true });
} }
@@ -273,17 +276,16 @@ router.post('/:userId/app_sc', authenticate, async function (req, res, next) {
await tools.attendiNSecondi(1); await tools.attendiNSecondi(1);
if (codice_sconto) { if (codice_sconto) {
const condSconto = { $regex: new RegExp(`^${codice_sconto}$`, 'i') }; const recscontisticheTrovate = await CartClass.getRecSconto(idapp, codice_sconto, true);
recscontisticheTrovate = await Scontistica.find({
idapp: idapp,
code: condSconto,
applica: shared_consts.SCONTI_APPLICA.A_TUTTI,
}).lean();
if (recscontisticheTrovate && recscontisticheTrovate.length > 0) { if (recscontisticheTrovate && recscontisticheTrovate.length > 0) {
recscontisticaTrovata = recscontisticheTrovate[0];
}
if (recscontisticaTrovata) {
if (mycart.codice_sconto !== codice_sconto) { if (mycart.codice_sconto !== codice_sconto) {
mycart.codice_sconto = codice_sconto; mycart.codice_sconto = codice_sconto;
await Cart.updateOne({ _id: cart_id }, { $set: { codice_sconto: codice_sconto } }); mycart.descr_sconto = recscontisticaTrovata.description;
await Cart.updateOne({ _id: cart_id }, { $set: { codice_sconto, descr_sconto: mycart.descr_sconto } });
} }
valido = true; valido = true;
@@ -293,7 +295,7 @@ router.post('/:userId/app_sc', authenticate, async function (req, res, next) {
} }
} }
return res.send({ mycart, valido, errmsg, recsconto: recscontisticheTrovate }); return res.send({ mycart, valido, errmsg, recsconto: recscontisticaTrovata });
} catch (e) { } catch (e) {
console.error('Err Applica Sconto', e); console.error('Err Applica Sconto', e);
return res.send({ valido: false, mycart: null, errmsg: e.message }); return res.send({ valido: false, mycart: null, errmsg: e.message });
@@ -321,11 +323,13 @@ async function createOrUpdateOrderFromCart({ idapp, cart, userId, status, note }
totalQty: cart.totalQty, totalQty: cart.totalQty,
totalPrice: cart.totalPrice, totalPrice: cart.totalPrice,
totalPriceCalc: cart.totalPriceCalc, totalPriceCalc: cart.totalPriceCalc,
totalPriceIntero: cart.totalPriceIntero,
note_ordine_gas: cart.note_ordine_gas, note_ordine_gas: cart.note_ordine_gas,
userId, userId,
status, status,
note, note,
codice_sconto: cart.codice_sconto, codice_sconto: cart.codice_sconto,
descr_sconto: cart.descr_sconto,
numorder, numorder,
numord_pers, numord_pers,
created_at: new Date(), created_at: new Date(),
@@ -360,10 +364,12 @@ async function handleCheckout({ myorderCart, mycart, userId, idapp, req, options
//POST cart //POST cart
router.post('/:userId/createorderscart', authenticate, async function (req, res) { router.post('/:userId/createorderscart', authenticate, async function (req, res) {
try {
const { idapp, cart_id, status, note, options } = req.body; const { idapp, cart_id, status, note, options } = req.body;
const userId = req.params.userId; const userId = req.params.userId;
try { // console.log('createorderscart', cart_id);
const mycart = await getCartById(cart_id); const mycart = await getCartById(cart_id);
if (!mycart) { if (!mycart) {
return res.send({ return res.send({
@@ -401,6 +407,8 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res)
} }
} }
// console.log('SEND OK', statusOrderCart);
return res.send({ return res.send({
code: server_constants.RIS_CODE_OK, code: server_constants.RIS_CODE_OK,
status: statusOrderCart, status: statusOrderCart,