- aggiornato scheda e rigenera lista
- corretto filtro sulla Collana
This commit is contained in:
@@ -29,8 +29,7 @@ const CatalogSchema = new Schema({
|
||||
},
|
||||
foto_collana: IImg,
|
||||
idCollane: [{
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'Collana',
|
||||
type: String,
|
||||
}],
|
||||
argomenti: [{
|
||||
type: String,
|
||||
@@ -129,15 +128,15 @@ CatalogSchema.statics.findAllIdApp = async function (idapp) {
|
||||
|
||||
let arrrec = await Catalog.find({ idapp })
|
||||
.sort({ title: 1 }) // Ordina i risultati per titolo
|
||||
.populate({
|
||||
/*.populate({
|
||||
path: "idCollane", // Popola il campo idCollane
|
||||
model: "Collana" // Specifica il modello della collezione Collana
|
||||
})
|
||||
})*/
|
||||
.populate({
|
||||
path: "lista_prodotti", // Popola il campo lista_prodotti
|
||||
populate: {
|
||||
path: "idProductInfo", // Popola il campo idProductInfo dentro ogni prodotto
|
||||
model: "ProductInfo", // Specifica il modello della collezione ProductInfo
|
||||
path: "idProductInfo",
|
||||
model: "ProductInfo",
|
||||
populate: [
|
||||
{
|
||||
path: "idCatProds",
|
||||
|
||||
@@ -64,7 +64,7 @@ CatProdSchema.statics.updateCatDeleteEmpty = async function (idapp) {
|
||||
{ $match: { idapp } },
|
||||
{
|
||||
$lookup: {
|
||||
from: 'productinfos', // Nome della tua collezione productInfo
|
||||
from: 'productinfos',
|
||||
localField: '_id',
|
||||
foreignField: 'idCatProds',
|
||||
as: 'products'
|
||||
|
||||
@@ -61,7 +61,7 @@ const catalogo = new Schema(
|
||||
excludeproductTypes: [{ type: Number }],
|
||||
editore: [{ type: String }],
|
||||
argomenti: [{ type: String }],
|
||||
idCollane: [{ type: Number }],
|
||||
idCollane: [{ type: String }],
|
||||
idTipologia: [{ type: Number }],
|
||||
sort_field: { type: String },
|
||||
sort_dir: { type: Number },
|
||||
|
||||
@@ -133,7 +133,7 @@ const scheletroScheda = {
|
||||
idTipologia: [{ type: Number }],
|
||||
editore: [{ type: String }],
|
||||
argomenti: [{ type: String }],
|
||||
idCollane: [{ type: Number }],
|
||||
idCollane: [{ type: String }],
|
||||
author: { type: String },
|
||||
sort_field: { type: String },
|
||||
sort_dir: { type: Number },
|
||||
|
||||
@@ -277,7 +277,7 @@ module.exports.executeQueryPickup = async function (idapp, params) {
|
||||
const escapeRegex = w => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
|
||||
// 🔹 Pattern per productInfo.name: tutte le parole devono essere presenti
|
||||
const patternAllWords = words.map(w => `(?=.*\\b${escapeRegex(w)}\\b)`).join('') + '.*';
|
||||
const patternAllWords = words.map(w => `(?=.*\\b${escapeRegex(w)})`).join('') + '.*';
|
||||
|
||||
// 🔹 Condizioni per autori
|
||||
let authorConditions = [];
|
||||
@@ -394,13 +394,13 @@ module.exports.executeQueryPickup = async function (idapp, params) {
|
||||
name: '$productInfo.name', // Nome dell'autore
|
||||
authors: '$productInfo.authors',
|
||||
idStatoProdotto: "$productInfo.idStatoProdotto",
|
||||
date_pub: "$productInfo.date_pub",
|
||||
},
|
||||
arrvariazioni: "$arrvariazioni",
|
||||
}
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
'arrvariazioni.0.quantita': -1, // Ordina per arrvariazioni[0].quantita , decrescente
|
||||
'productInfo.date_pub': -1,
|
||||
'productInfo.name': 1 // Ordina per name in ordine crescente
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ const productInfoSchema = new Schema({
|
||||
type: String,
|
||||
},
|
||||
idAuthors: [{ type: Schema.Types.ObjectId, ref: 'Author' }],
|
||||
idCollana: { type: Number },
|
||||
idCollana: { type: Schema.Types.ObjectId, ref: 'Collana' },
|
||||
idPublisher: { type: Schema.Types.ObjectId, ref: 'Publisher' },
|
||||
collezione: {
|
||||
type: String,
|
||||
@@ -452,15 +452,18 @@ module.exports.correggiProductTypes = async function () {
|
||||
|
||||
module.exports.updateProductInfoByStats = async function (idapp) {
|
||||
let mylog = '';
|
||||
let mylog2 = '';
|
||||
let mylogtot = '';
|
||||
try {
|
||||
const ProductInfo = this;
|
||||
|
||||
const T_WEB_ArticoliFatturati = require('./t_web_articolifatturati')
|
||||
const T_WEB_ArticoliFatturati = require('./t_web_articolifatturati');
|
||||
const T_WEB_Ordini = require('./t_web_ordini');
|
||||
|
||||
// Ottieni le statistiche dalla query
|
||||
const statistics = await T_WEB_ArticoliFatturati.getStatistics();
|
||||
|
||||
mylog = "Inizio Aggiornamento Statistiche... \n";
|
||||
mylogtot += mylog;
|
||||
console.log(mylog);
|
||||
|
||||
// Itera sui risultati e aggiorna productInfo
|
||||
@@ -486,15 +489,51 @@ module.exports.updateProductInfoByStats = async function (idapp) {
|
||||
}
|
||||
|
||||
mylog = `Aggiornati ${countUpdate} record di productInfo`;
|
||||
|
||||
mylogtot += mylog;
|
||||
console.log(mylog);
|
||||
|
||||
// Ottieni le statistiche dalla query
|
||||
const statisticsordini = await T_WEB_Ordini.getStatisticsFromOrders();
|
||||
|
||||
mylog2 = "Inizio Aggiornamento Statistiche Ordini ... \n";
|
||||
mylogtot += mylog2;
|
||||
console.log(mylog2);
|
||||
|
||||
// Itera sui risultati e aggiorna productInfo
|
||||
countUpdate = 0;
|
||||
for (const stat of statisticsordini) {
|
||||
const result = await ProductInfo.updateOne(
|
||||
{
|
||||
sku: stat.sku,
|
||||
idapp
|
||||
}, // Cerca il documento con lo stesso sku
|
||||
{
|
||||
$set: {
|
||||
totVen: stat.totVen,
|
||||
vLast3M: stat.vLast3M,
|
||||
vLast6M: stat.vLast6M,
|
||||
vLastY: stat.vLastY,
|
||||
vLast2Y: stat.vLast2Y,
|
||||
}
|
||||
},
|
||||
{ upsert: false } // Non crea il documento se non esiste
|
||||
);
|
||||
if (result.modifiedCount > 0) {
|
||||
countUpdate++;
|
||||
}
|
||||
}
|
||||
|
||||
mylog2 = `Aggiornati ${countUpdate} record di productInfo`;
|
||||
mylogtot += mylog2;
|
||||
|
||||
console.log(mylog2);
|
||||
|
||||
} catch (error) {
|
||||
mylog = "Errore durante l'aggiornamento di productInfo:" + error;
|
||||
console.error(mylog);
|
||||
}
|
||||
|
||||
return mylog;
|
||||
return mylogtot;
|
||||
}
|
||||
|
||||
module.exports.createIndexes()
|
||||
|
||||
@@ -118,7 +118,7 @@ module.exports.getStatistics = async function () {
|
||||
sku: "$_id", // Rinomina _id in sku (equivalente a IdArticolo)
|
||||
fatLast3M: 1,
|
||||
fatLast6M: 1,
|
||||
fatLast1Y: 1
|
||||
fatLast1Y: 1,
|
||||
}
|
||||
}
|
||||
];
|
||||
@@ -131,3 +131,5 @@ module.exports.getStatistics = async function () {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ const TWebDisponibileSchema = new mongoose.Schema({
|
||||
default: 0,
|
||||
},
|
||||
}, {
|
||||
collection: 't_web_disponibiles', // nome della collezione esatto
|
||||
collection: 't_web_disponibiles',
|
||||
timestamps: false,
|
||||
});
|
||||
|
||||
|
||||
161
src/server/models/t_web_ordini.js
Executable file
161
src/server/models/t_web_ordini.js
Executable file
@@ -0,0 +1,161 @@
|
||||
// allo stesso modo di t_web_articolifatturati.js
|
||||
// creami il modello t_web_ordini:
|
||||
|
||||
// Tabella: T_WEB_Ordini
|
||||
/*Codice - decimal ()
|
||||
IdInternet - varchar (50)
|
||||
CodArticoloGM - varchar (50)
|
||||
Qta - decimal ()
|
||||
PrezzoLordo - decimal ()
|
||||
PercSconto - decimal ()
|
||||
Enabled - int ()
|
||||
DataOra - datetime ()
|
||||
SovraSconto - decimal ()
|
||||
Descrizione - varchar (250)
|
||||
PrimaCopiaDaSpedire - int ()
|
||||
|
||||
*/
|
||||
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
// Definizione dello schema
|
||||
const ordiniSchema = new mongoose.Schema({
|
||||
Codice: {
|
||||
type: Number, // Decimal in MongoDB è rappresentato come Number
|
||||
},
|
||||
IdInternet: {
|
||||
type: String,
|
||||
maxlength: 50,
|
||||
},
|
||||
CodArticoloGM: {
|
||||
type: String,
|
||||
maxlength: 50,
|
||||
},
|
||||
Qta: {
|
||||
type: Number, // Decimal in MongoDB è rappresentato come Number
|
||||
},
|
||||
PrezzoLordo: {
|
||||
type: Number, // Decimal in MongoDB è rappresentato come Number
|
||||
},
|
||||
PercSconto: {
|
||||
type: Number, // Decimal in MongoDB è rappresentato come Number
|
||||
},
|
||||
Enabled: {
|
||||
type: Number, // Int in MongoDB è rappresentato come Number
|
||||
},
|
||||
DataOra: {
|
||||
type: Date, // Datetime in MongoDB è rappresentato come Date
|
||||
},
|
||||
SovraSconto: {
|
||||
type: Number, // Decimal in MongoDB è rappresentato come Number
|
||||
},
|
||||
Descrizione: {
|
||||
type: String,
|
||||
maxlength: 250
|
||||
},
|
||||
PrimaCopiaDaSpedire: {
|
||||
type: Number, // Int in MongoDB è rappresentato come Number
|
||||
}
|
||||
}, {
|
||||
timestamps: true, // Aggiunge automaticamente i campi createdAt e updatedAt
|
||||
collection: 't_web_ordinis',
|
||||
});
|
||||
|
||||
var T_WEB_Ordini = module.exports = mongoose.model('T_WEB_Ordini', ordiniSchema);
|
||||
|
||||
module.exports.getStatisticsFromOrders = async function () {
|
||||
const currentDate = new Date();
|
||||
|
||||
// Calcola le date limite per i periodi di 3 mesi, 6 mesi e 1 anno
|
||||
const threeMonthsAgo = new Date(currentDate);
|
||||
threeMonthsAgo.setMonth(currentDate.getMonth() - 3);
|
||||
|
||||
const sixMonthsAgo = new Date(currentDate);
|
||||
sixMonthsAgo.setMonth(currentDate.getMonth() - 6);
|
||||
|
||||
const oneYearAgo = new Date(currentDate);
|
||||
oneYearAgo.setFullYear(currentDate.getFullYear() - 1);
|
||||
|
||||
const twoYearAgo = new Date(currentDate);
|
||||
twoYearAgo.setFullYear(currentDate.getFullYear() - 2);
|
||||
|
||||
const allYear = new Date(currentDate);
|
||||
allYear.setFullYear(currentDate.getFullYear() - 20);
|
||||
|
||||
try {
|
||||
|
||||
// Query di aggregazione per calcolare le statistiche
|
||||
const myquery = [
|
||||
{
|
||||
$group: {
|
||||
_id: "$CodArticoloGM", // Raggruppa per CodArticolo
|
||||
totVen: {
|
||||
$sum: {
|
||||
$cond: [
|
||||
{ $gte: ["$DataOra", allYear] }, // Condizione: DataOra totale
|
||||
{ $toInt: "$Qta" }, // Se vero, somma la quantità
|
||||
0 // Altrimenti, somma 0
|
||||
]
|
||||
}
|
||||
},
|
||||
vLast3M: {
|
||||
$sum: {
|
||||
$cond: [
|
||||
{ $gte: ["$DataOra", threeMonthsAgo] }, // Condizione: DataOra >= 3 mesi fa
|
||||
{ $toInt: "$Qta" }, // Se vero, somma la quantità
|
||||
0 // Altrimenti, somma 0
|
||||
]
|
||||
}
|
||||
},
|
||||
vLast6M: {
|
||||
$sum: {
|
||||
$cond: [
|
||||
{ $gte: ["$DataOra", sixMonthsAgo] }, // Condizione: DataOra >= 6 mesi fa
|
||||
{ $toInt: "$Qta" }, // Se vero, somma la quantità
|
||||
0 // Altrimenti, somma 0
|
||||
]
|
||||
}
|
||||
},
|
||||
vLast1Y: {
|
||||
$sum: {
|
||||
$cond: [
|
||||
{ $gte: ["$DataOra", oneYearAgo] }, // Condizione: DataOra >= 1 anno fa
|
||||
{ $toInt: "$Qta" }, // Se vero, somma la quantità
|
||||
0 // Altrimenti, somma 0
|
||||
]
|
||||
}
|
||||
},
|
||||
vLast2Y: {
|
||||
$sum: {
|
||||
$cond: [
|
||||
{ $gte: ["$DataOra", twoYearAgo] }, // Condizione: DataOra >= 1 anno fa
|
||||
{ $toInt: "$Qta" }, // Se vero, somma la quantità
|
||||
0 // Altrimenti, somma 0
|
||||
]
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0, // Rimuove il campo _id dal risultato
|
||||
sku: "$_id", // Rinomina _id in sku (equivalente a IdArticolo)
|
||||
totVen: 1,
|
||||
vLast3M: 1,
|
||||
vLast6M: 1,
|
||||
vLast1Y: 1,
|
||||
vLast2Y: 1,
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
const statistics = await T_WEB_Ordini.aggregate(myquery);
|
||||
|
||||
return statistics;
|
||||
} catch (error) {
|
||||
console.error("Errore durante il calcolo delle statistiche:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user