Files
freeplanet_serverside/src/server/models/t_web_articolifatturati.js
Surya Paolo e40fbd550b - miglioramenti ricerca titoli e modifica del trafiletto
- miglior visualizzazione delle liste
2025-04-30 13:27:54 +02:00

200 lines
5.2 KiB
JavaScript
Executable File

const mongoose = require('mongoose');
const ProductInfo = require('../models/productInfo');
// 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
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
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 fiveYearAgo = new Date(currentDate);
fiveYearAgo.setFullYear(currentDate.getFullYear() - 5);
try {
let myquery = [];
// Query di aggregazione per calcolare le statistiche
myquery.push(
{
$match: {
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
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
]
}
},
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
]
}
},
}
},
{
$project: {
_id: 0, // Rimuove il campo _id dal risultato
sku: "$_id", // Rinomina _id in sku (equivalente a IdArticolo)
fatLast3M: 1,
fatLast6M: 1,
fatLast1Y: 1,
fatLast2Y: 1,
totFat: 1,
}
}
);
const statistics = await articoliFatturati.aggregate(myquery);
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;
return 0;
}
}