diff --git a/src/server/models/mypage.js b/src/server/models/mypage.js index 531a5de..8ae5cd8 100755 --- a/src/server/models/mypage.js +++ b/src/server/models/mypage.js @@ -28,6 +28,9 @@ const MyPageSchema = new Schema({ subtitle: { type: String, }, + isTemplate: { + type: Boolean, + }, icon: { type: String, }, diff --git a/src/server/models/order.js b/src/server/models/order.js index b5b8fc5..00e51c9 100755 --- a/src/server/models/order.js +++ b/src/server/models/order.js @@ -6,6 +6,7 @@ const shared_consts = require('../tools/shared_nodejs'); const { ObjectId } = require('mongodb'); const Scontistica = require('../models/scontistica'); +const OrderClass = require('../modules/OrderClass'); mongoose.Promise = global.Promise; mongoose.level = 'F'; @@ -300,81 +301,13 @@ module.exports.updateOrderByParams = async function (idOrder, paramstoupdate) { return myorder; }; -function initOrderTotals(order) { - order.TotalPriceProduct = 0; - order.TotalPriceProductCalc = 0; - order.modify_at = new Date(); -} - -function getNonCumulativeDiscounts(discounts) { - return discounts.filter((rec) => !rec.cumulativo); -} - -function applyNonCumulativeDiscounts(order, discounts) { - let qtadascontare = order.quantity + order.quantitypreordered; - let qtanonscontata = 0; - let sconti_da_applicare = []; - - 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; - } - } - - return { sconti_da_applicare, qtanonscontata }; -} - -function calculateDiscountedPrice(order, sconti_da_applicare, qtanonscontata) { - let total = 0; - - for (const sconto of sconti_da_applicare) { - if (sconto.perc_sconto > 0) { - total += sconto.qtadascontare * order.price * (1 - sconto.perc_sconto / 100); - } else { - total += sconto.price; - } - } - - if (qtanonscontata > 0) { - total += order.price * qtanonscontata; - } - - return total; -} - -function calculateFullPrice(order) { - return order.price * order.quantity + order.price * order.quantitypreordered; -} - module.exports.updateTotals = async function (order, codice_sconto) { try { const CartClass = require('../modules/Cart'); if (!order) return; - initOrderTotals(order); + OrderClass.initOrderTotals(order); let total = 0; @@ -388,13 +321,13 @@ module.exports.updateTotals = async function (order, codice_sconto) { // Se ha inserito una scontistica che esiste... if (recscontisticheTrovate && recscontisticheTrovate.length > 0) { - const { sconti_da_applicare, qtanonscontata } = applyNonCumulativeDiscounts(order, recscontisticheTrovate); - total = calculateDiscountedPrice(order, sconti_da_applicare, qtanonscontata); + const { sconti_da_applicare, qtanonscontata } = OrderClass.applyNonCumulativeDiscounts(order, recscontisticheTrovate); + total = OrderClass.calculateDiscountedPrice(order, sconti_da_applicare, qtanonscontata); } else if (order.scontisticas && order.scontisticas.length > 0) { - const { sconti_da_applicare, qtanonscontata } = applyNonCumulativeDiscounts(order, order.scontisticas); - total = calculateDiscountedPrice(order, sconti_da_applicare, qtanonscontata); + const { sconti_da_applicare, qtanonscontata } = OrderClass.applyNonCumulativeDiscounts(order, order.scontisticas); + total = OrderClass.calculateDiscountedPrice(order, sconti_da_applicare, qtanonscontata); } else { - total = calculateFullPrice(order); + total = OrderClass.calculateFullPrice(order); } order.TotalPriceProductCalc += total; diff --git a/src/server/modules/Cart.js b/src/server/modules/Cart.js index 870de38..d9ce1b8 100755 --- a/src/server/modules/Cart.js +++ b/src/server/modules/Cart.js @@ -6,6 +6,7 @@ const { ObjectId } = require('mongodb'); const Gasordine = require('../models/gasordine'); const Order = require('../models/order'); +const OrderClass = require('../modules/orderclass'); const Scontistica = require('../models/scontistica'); @@ -264,47 +265,9 @@ class Cart { return order.price * qtyTotale; } - let scontiApplicati = []; - let qtaRimanente = qtyTotale; - let qtaNonScontata = 0; + const { sconti_da_applicare, qtanonscontata } = OrderClass.applyNonCumulativeDiscounts(order, sconti); - // Applica sconti non cumulativi - while (qtaRimanente > 0) { - let scontoScelto = null; - let scontoVantaggioso = 0; - for (const sconto of sconti.filter((s) => !s.cumulativo)) { - if (qtaRimanente >= sconto.qta) { - const scontoApplicato = - sconto.perc_sconto > 0 ? qtaRimanente * order.price * (1 - sconto.perc_sconto / 100) : sconto.price; - if (scontoApplicato > scontoVantaggioso) { - scontoVantaggioso = scontoApplicato; - scontoScelto = { ...sconto, qtadascontare: qtaRimanente }; - } - } - } - - if (scontoScelto) { - scontiApplicati.push(scontoScelto); - qtaRimanente -= scontoScelto.qtadascontare; - } else { - qtaNonScontata = qtaRimanente; - qtaRimanente = 0; - } - } - - let prezzoTotale = 0; - - for (const sconto of scontiApplicati) { - if (sconto.perc_sconto > 0) { - prezzoTotale += sconto.qtadascontare * order.price * (1 - sconto.perc_sconto / 100); - } else if (sconto.price > 0) { - prezzoTotale += sconto.price; - } - } - - if (qtaNonScontata > 0) { - prezzoTotale += qtaNonScontata * order.price; - } + const prezzoTotale = OrderClass.calculateDiscountedPrice(order, sconti_da_applicare, qtanonscontata); return prezzoTotale; } diff --git a/src/server/modules/GenPdf.js b/src/server/modules/GenPdf.js index ca8b1c7..71a9aa3 100644 --- a/src/server/modules/GenPdf.js +++ b/src/server/modules/GenPdf.js @@ -48,7 +48,7 @@ class GenPdf { } async autoScroll(page) { - console.log('inizia a scrollare'); + console.log('inizia a scrollare...'); // Esegui lo scroll fino a quando tutta la pagina non è stata scrollata await page.evaluate(async () => { @@ -97,9 +97,11 @@ class GenPdf { await tools.attendiNSecondi(6); let success = false; let numTentativi1 = 0; + console.log(`Cerco .pdf-section...`); while (numTentativi1 < maxTentativi) { try { await page.waitForSelector('.pdf-section', { timeout: 10000 }); + console.log(` .pdf-section trovato !...`); success = true; break; } catch (e) { diff --git a/src/server/modules/OrderClass.js b/src/server/modules/OrderClass.js new file mode 100755 index 0000000..76bec03 --- /dev/null +++ b/src/server/modules/OrderClass.js @@ -0,0 +1,84 @@ +const shared_consts = require('../tools/shared_nodejs'); +const cartModel = require('../models/cart'); + +const { ObjectId } = require('mongodb'); + +const Gasordine = require('../models/gasordine'); + +const Order = require('../models/order'); + +const Scontistica = require('../models/scontistica'); + +class OrderClass { + constructor() {} + + static initOrderTotals(order) { + order.TotalPriceProduct = 0; + order.TotalPriceProductCalc = 0; + order.modify_at = new Date(); + } + + static getNonCumulativeDiscounts(discounts) { + return discounts.filter((rec) => !rec.cumulativo); + } + + static applyNonCumulativeDiscounts(order, discounts) { + let qtadascontare = order.quantity + order.quantitypreordered; + let qtanonscontata = 0; + let sconti_da_applicare = []; + + while (qtadascontare > 0) { + let scontoapplicato = null; + + // Filtriamo gli sconti non cumulativi + for (const sconto of OrderClass.getNonCumulativeDiscounts(discounts)) { + if (qtadascontare >= sconto.qta && sconto.qta > 0) { + // 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; + } + } + + return { sconti_da_applicare, qtanonscontata }; + } + + static calculateDiscountedPrice(order, sconti_da_applicare, qtanonscontata) { + let total = 0; + + for (const sconto of sconti_da_applicare) { + if (sconto.perc_sconto > 0) { + total += sconto.qtadascontare * order.price * (1 - sconto.perc_sconto / 100); + } else { + total += sconto.price; + } + } + + if (qtanonscontata > 0) { + total += order.price * qtanonscontata; + } + + return total; + } + + static calculateFullPrice(order) { + return order.price * order.quantity + order.price * order.quantitypreordered; + } +} + +module.exports = OrderClass; diff --git a/src/server/router/index_router.js b/src/server/router/index_router.js index 9db2bff..92ee5e8 100755 --- a/src/server/router/index_router.js +++ b/src/server/router/index_router.js @@ -430,7 +430,7 @@ router.post('/settable', authenticate, async (req, res) => { } else { if ( (mydata['_id'] === undefined || mydata['_id'] === '' || (mytablerec.isNew && mydata['_id'] === 0)) && - (mytablerec._id === undefined || mytablerec._id === '0') + (mytablerec._id === undefined || mytablerec._id === '0'|| mytablerec._id === 0) ) { mytablerec._id = new ObjectId(); mydata._id = new ObjectId(); diff --git a/src/server/version.txt b/src/server/version.txt index efa753a..66472fc 100644 --- a/src/server/version.txt +++ b/src/server/version.txt @@ -1 +1 @@ -1.2.53 \ No newline at end of file +1.2.54 \ No newline at end of file