2025-04-18 13:23:59 +02:00
|
|
|
const mongoose = require('mongoose');
|
|
|
|
|
|
2025-04-30 13:27:54 +02:00
|
|
|
const ProductInfo = require('../models/productInfo');
|
|
|
|
|
|
2025-04-18 13:23:59 +02:00
|
|
|
// Definizione dello schema
|
|
|
|
|
const articoliFatturatiSchema = new mongoose.Schema({
|
|
|
|
|
Codice: {
|
|
|
|
|
type: Number, // Decimal in MongoDB è rappresentato come Number
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
AnnoDoc: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 4,
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
NumeroDoc: {
|
|
|
|
|
type: Number, // Decimal in MongoDB è rappresentato come Number
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
TipoDoc: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 20,
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
CodArticolo: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 50,
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
Qta: {
|
|
|
|
|
type: String, // Mantenuto come stringa per flessibilità (può essere convertito in Number se necessario)
|
|
|
|
|
maxlength: 50,
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
DataOra: {
|
|
|
|
|
type: Date, // Datetime in MongoDB è rappresentato come Date
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
CodTrackingCorriere: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 50
|
|
|
|
|
},
|
|
|
|
|
Stato: {
|
|
|
|
|
type: Number, // Int in MongoDB è rappresentato come Number
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
Note: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 250
|
|
|
|
|
},
|
|
|
|
|
IdInternet: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 50
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
timestamps: true, // Aggiunge automaticamente i campi createdAt e updatedAt
|
|
|
|
|
collection: 't_web_articolifatturatis',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Creazione del modello
|
|
|
|
|
var articoliFatturati = module.exports = mongoose.model('T_WEB_ArticoliFatturati', articoliFatturatiSchema);
|
|
|
|
|
|
|
|
|
|
// Funzione per calcolare le statistiche
|
2025-04-30 13:27:54 +02:00
|
|
|
module.exports.updateStatisticsFatt = async function (CodArticolo, idapp, update) {
|
2025-04-18 13:23:59 +02:00
|
|
|
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);
|
2025-04-30 13:27:54 +02:00
|
|
|
const twoYearAgo = new Date(currentDate);
|
|
|
|
|
twoYearAgo.setFullYear(currentDate.getFullYear() - 2);
|
|
|
|
|
|
|
|
|
|
const fiveYearAgo = new Date(currentDate);
|
|
|
|
|
fiveYearAgo.setFullYear(currentDate.getFullYear() - 5);
|
2025-04-18 13:23:59 +02:00
|
|
|
|
|
|
|
|
try {
|
2025-04-30 13:27:54 +02:00
|
|
|
let myquery = [];
|
|
|
|
|
|
2025-04-18 13:23:59 +02:00
|
|
|
// Query di aggregazione per calcolare le statistiche
|
2025-04-30 13:27:54 +02:00
|
|
|
myquery.push(
|
2025-04-18 13:23:59 +02:00
|
|
|
{
|
|
|
|
|
$match: {
|
2025-04-30 13:27:54 +02:00
|
|
|
DataOra: { $gte: fiveYearAgo } // Filtra solo i record degli ultimi 12 mesi
|
2025-04-18 13:23:59 +02:00
|
|
|
}
|
2025-04-30 13:27:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (CodArticolo) {
|
|
|
|
|
myquery.push({
|
|
|
|
|
$match: { $expr: { $eq: ["$CodArticolo", CodArticolo] } }
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
myquery.push(
|
2025-04-18 13:23:59 +02:00
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: "$CodArticolo", // Raggruppa per CodArticolo
|
|
|
|
|
fatLast3M: {
|
|
|
|
|
$sum: {
|
|
|
|
|
$cond: [
|
|
|
|
|
{ $gte: ["$DataOra", threeMonthsAgo] }, // Condizione: DataOra >= 3 mesi fa
|
|
|
|
|
{ $toInt: "$Qta" }, // Se vero, somma la quantità
|
|
|
|
|
0 // Altrimenti, somma 0
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fatLast6M: {
|
|
|
|
|
$sum: {
|
|
|
|
|
$cond: [
|
|
|
|
|
{ $gte: ["$DataOra", sixMonthsAgo] }, // Condizione: DataOra >= 6 mesi fa
|
|
|
|
|
{ $toInt: "$Qta" }, // Se vero, somma la quantità
|
|
|
|
|
0 // Altrimenti, somma 0
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fatLast1Y: {
|
|
|
|
|
$sum: {
|
|
|
|
|
$cond: [
|
|
|
|
|
{ $gte: ["$DataOra", oneYearAgo] }, // Condizione: DataOra >= 1 anno fa
|
|
|
|
|
{ $toInt: "$Qta" }, // Se vero, somma la quantità
|
|
|
|
|
0 // Altrimenti, somma 0
|
|
|
|
|
]
|
|
|
|
|
}
|
2025-04-30 13:27:54 +02:00
|
|
|
},
|
|
|
|
|
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
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-04-18 13:23:59 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
_id: 0, // Rimuove il campo _id dal risultato
|
|
|
|
|
sku: "$_id", // Rinomina _id in sku (equivalente a IdArticolo)
|
|
|
|
|
fatLast3M: 1,
|
|
|
|
|
fatLast6M: 1,
|
2025-04-29 02:30:19 +02:00
|
|
|
fatLast1Y: 1,
|
2025-04-30 13:27:54 +02:00
|
|
|
fatLast2Y: 1,
|
|
|
|
|
totFat: 1,
|
2025-04-18 13:23:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-30 13:27:54 +02:00
|
|
|
);
|
2025-04-18 13:23:59 +02:00
|
|
|
|
|
|
|
|
const statistics = await articoliFatturati.aggregate(myquery);
|
|
|
|
|
|
2025-04-30 13:27:54 +02:00
|
|
|
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;
|
2025-04-18 13:23:59 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Errore durante il calcolo delle statistiche:", error);
|
2025-04-30 13:27:54 +02:00
|
|
|
// throw error;
|
|
|
|
|
return 0;
|
2025-04-18 13:23:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-29 02:30:19 +02:00
|
|
|
|
|
|
|
|
|