- aggiornamento cataloghi.
possibilità di estrapolare i dati da GM direttamente - migrazione delle tabelle di GM in locale - corretto l'ordinamento del Catalogo
This commit is contained in:
133
src/server/models/t_web_articolifatturati.js
Executable file
133
src/server/models/t_web_articolifatturati.js
Executable file
@@ -0,0 +1,133 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
// 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.getStatistics = 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);
|
||||
|
||||
try {
|
||||
// Query di aggregazione per calcolare le statistiche
|
||||
const myquery = [
|
||||
{
|
||||
$match: {
|
||||
DataOra: { $gte: oneYearAgo } // Filtra solo i record degli ultimi 12 mesi
|
||||
}
|
||||
},
|
||||
{
|
||||
$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
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0, // Rimuove il campo _id dal risultato
|
||||
sku: "$_id", // Rinomina _id in sku (equivalente a IdArticolo)
|
||||
fatLast3M: 1,
|
||||
fatLast6M: 1,
|
||||
fatLast1Y: 1
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
const statistics = await articoliFatturati.aggregate(myquery);
|
||||
|
||||
return statistics;
|
||||
} catch (error) {
|
||||
console.error("Errore durante il calcolo delle statistiche:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user