- miglioramenti ricerca titoli e modifica del trafiletto
- miglior visualizzazione delle liste
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const ProductInfo = require('../models/productInfo');
|
||||
|
||||
// Definizione dello schema
|
||||
const articoliFatturatiSchema = new mongoose.Schema({
|
||||
Codice: {
|
||||
@@ -59,7 +61,7 @@ const articoliFatturatiSchema = new mongoose.Schema({
|
||||
var articoliFatturati = module.exports = mongoose.model('T_WEB_ArticoliFatturati', articoliFatturatiSchema);
|
||||
|
||||
// Funzione per calcolare le statistiche
|
||||
module.exports.getStatistics = async function () {
|
||||
module.exports.updateStatisticsFatt = async function (CodArticolo, idapp, update) {
|
||||
const currentDate = new Date();
|
||||
|
||||
// Calcola le date limite per i periodi di 3 mesi, 6 mesi e 1 anno
|
||||
@@ -71,15 +73,30 @@ module.exports.getStatistics = async function () {
|
||||
|
||||
const oneYearAgo = new Date(currentDate);
|
||||
oneYearAgo.setFullYear(currentDate.getFullYear() - 1);
|
||||
const twoYearAgo = new Date(currentDate);
|
||||
twoYearAgo.setFullYear(currentDate.getFullYear() - 2);
|
||||
|
||||
const fiveYearAgo = new Date(currentDate);
|
||||
fiveYearAgo.setFullYear(currentDate.getFullYear() - 5);
|
||||
|
||||
try {
|
||||
let myquery = [];
|
||||
|
||||
// Query di aggregazione per calcolare le statistiche
|
||||
const myquery = [
|
||||
myquery.push(
|
||||
{
|
||||
$match: {
|
||||
DataOra: { $gte: oneYearAgo } // Filtra solo i record degli ultimi 12 mesi
|
||||
DataOra: { $gte: fiveYearAgo } // Filtra solo i record degli ultimi 12 mesi
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (CodArticolo) {
|
||||
myquery.push({
|
||||
$match: { $expr: { $eq: ["$CodArticolo", CodArticolo] } }
|
||||
})
|
||||
}
|
||||
|
||||
myquery.push(
|
||||
{
|
||||
$group: {
|
||||
_id: "$CodArticolo", // Raggruppa per CodArticolo
|
||||
@@ -109,7 +126,25 @@ module.exports.getStatistics = async function () {
|
||||
0 // Altrimenti, somma 0
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
fatLast2Y: {
|
||||
$sum: {
|
||||
$cond: [
|
||||
{ $gte: ["$DataOra", twoYearAgo] }, // Condizione: DataOra >= 1 anno fa
|
||||
{ $toInt: "$Qta" }, // Se vero, somma la quantità
|
||||
0 // Altrimenti, somma 0
|
||||
]
|
||||
}
|
||||
},
|
||||
totFat: {
|
||||
$sum: {
|
||||
$cond: [
|
||||
{ $gte: ["$DataOra", fiveYearAgo] }, //
|
||||
{ $toInt: "$Qta" }, // Se vero, somma la quantità
|
||||
0 // Altrimenti, somma 0
|
||||
]
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -119,16 +154,45 @@ module.exports.getStatistics = async function () {
|
||||
fatLast3M: 1,
|
||||
fatLast6M: 1,
|
||||
fatLast1Y: 1,
|
||||
fatLast2Y: 1,
|
||||
totFat: 1,
|
||||
}
|
||||
}
|
||||
];
|
||||
);
|
||||
|
||||
const statistics = await articoliFatturati.aggregate(myquery);
|
||||
|
||||
return statistics;
|
||||
let countUpdate = 0;
|
||||
|
||||
if (update) {
|
||||
for (const stat of statistics) {
|
||||
const result = await ProductInfo.updateOne(
|
||||
{
|
||||
sku: stat.sku,
|
||||
idapp
|
||||
}, // Cerca il documento con lo stesso sku
|
||||
{
|
||||
$set: {
|
||||
fatLast3M: stat.fatLast3M,
|
||||
fatLast6M: stat.fatLast6M,
|
||||
fatLast1Y: stat.fatLast1Y,
|
||||
fatLast2Y: stat.fatLast2Y,
|
||||
totFat: stat.totFat,
|
||||
}
|
||||
},
|
||||
{ upsert: false } // Non crea il documento se non esiste
|
||||
);
|
||||
if (result.modifiedCount > 0) {
|
||||
countUpdate++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return countUpdate;
|
||||
} catch (error) {
|
||||
console.error("Errore durante il calcolo delle statistiche:", error);
|
||||
throw error;
|
||||
// throw error;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user