- 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:
Surya Paolo
2025-04-18 13:23:59 +02:00
parent fba2ebd710
commit ad45ce60ee
13 changed files with 988 additions and 106 deletions

View File

@@ -152,6 +152,7 @@ const productInfoSchema = new Schema({
vLast3M: Number,
fatLast3M: Number,
fatLast6M: Number,
fatLast1Y: Number,
vLast6M: Number,
vLastY: Number,
vLast2Y: Number,
@@ -445,6 +446,52 @@ module.exports.correggiProductTypes = async function () {
}
}
module.exports.updateProductInfoByStats = async function (idapp) {
try {
const ProductInfo = this;
const T_WEB_ArticoliFatturati = require('./t_web_articolifatturati')
// Ottieni le statistiche dalla query
const statistics = await T_WEB_ArticoliFatturati.getStatistics();
let log = "Inizio Aggiornamento Statistiche... \n";
console.log(mylog);
// Itera sui risultati e aggiorna productInfo
let countUpdate = 0;
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
}
},
{ upsert: false } // Non crea il documento se non esiste
);
if (result.modifiedCount > 0) {
countUpdate++;
}
}
mylog = `Aggiornati ${countUpdate} record di productInfo`;
console.log(mylog);
} catch (error) {
mylog = "Errore durante l'aggiornamento di productInfo:" + error;
console.error(mylog);
}
return mylog;
}
module.exports.createIndexes()
.then(() => { })
.catch((err) => { throw err; });

View File

@@ -0,0 +1,107 @@
mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;
const tools = require('../tools/general');
mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
/**
* @typedef {Object} Article
* @property {bigint} Id
* @property {number} IdArticolo
* @property {string} Ean13
* @property {string} Titolo
* @property {string} ListaAutori
* @property {string} ListaArgomenti
* @property {number} IdStatoProdotto
* @property {number} PrezzoIvato
* @property {number} IdMarchioEditoriale
* @property {number} IdCollana
* @property {Date} DataPubblicazione
* @property {number} IdTipologia
* @property {number} IdTipoFormato
* @property {string} Misure
* @property {string} Pagine
* @property {string} Sottotitolo
* @property {string} Durata
* @property {string} Numero
* @property {string} Edizione
* @property {string} Ristampa
* @property {Date} DataInizioCampagna
* @property {Date} DataFineCampagna
* @property {number} ScontoCampagna
* @property {number} PrezzoIvatoScontatoCampagna
* @property {Date} DataOra
* @property {boolean} Enabled
* @property {number} IDTagGruppo
* @property {string} Utente
* @property {number} PercIva
* @property {number} IdTitoloOriginale
* @property {boolean} EnabledAlFresco
* @property {number} CodEdizione
* @property {string} FasciaEta
* @property {string} DescrizioneStatoProdotto
* @property {string} DescrizioneTipologia
* @property {string} DescrizioneFormato
* @property {string} DescrizioneCollana
* @property {string} DescrArgomento
* @property {string} AutoriCompleti
* @property {string} CasaEditrice
*/
const T_WEB_ArticoliSchema = new Schema({
IdArticolo: { type: Number, index: true },
Ean13: { type: String, index: true },
Titolo: { type: String, index: true },
ListaAutori: String,
ListaArgomenti: String,
IdStatoProdotto: Number,
PrezzoIvato: Number,
IdMarchioEditoriale: Number,
IdCollana: Number,
DataPubblicazione: Date,
IdTipologia: Number,
IdTipoFormato: Number,
Misure: String,
Pagine: String,
Sottotitolo: String,
Durata: String,
Numero: String,
Edizione: String,
Ristampa: String,
DataInizioCampagna: Date,
DataFineCampagna: Date,
ScontoCampagna: Number,
PrezzoIvatoScontatoCampagna: Number,
DataOra: Date,
Enabled: Boolean,
IDTagGruppo: Number,
Utente: String,
PercIva: Number,
IdTitoloOriginale: Number,
EnabledAlFresco: Boolean,
CodEdizione: Number,
FasciaEta: String,
DescrizioneStatoProdotto: String,
DescrizioneTipologia: String,
DescrizioneFormato: String,
DescrizioneCollana: String,
DescrArgomento: String,
AutoriCompleti: String,
CasaEditrice: String,
}, { collection: 't_web_articolis' });
module.exports = mongoose.model('T_WEB_Articoli', T_WEB_ArticoliSchema);
module.exports.createIndexes()
.then(() => { })
.catch((err) => { throw err; });

View 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;
}
}

View File

@@ -0,0 +1,58 @@
const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.Promise = global.Promise;
mongoose.level = "F";
/**
* @typedef {Object} StatoProdotto
* @property {bigint} Id
* @property {number} IdStatoProdotto
* @property {string} Descrizione
* @property {Date} DataOra
* @property {boolean} Enabled
* @property {boolean} EnabledAlFresco
*/
const StatoProdottoSchema = new Schema({
IdStatoProdotto: Number,
Descrizione: { type: String, maxlength: 100 },
DataOra: Date,
Enabled: Boolean,
EnabledAlFresco: Boolean
}, { collection: 't_web_statiprodottos' });
const T_WEB_StatiProdotto = module.exports = mongoose.model('T_WEB_StatiProdotto', StatoProdottoSchema);
module.exports.findAllIdApp = async function () {
const myfind = {};
const myquery = [
{
$group: {
_id: "$IdStatoProdotto",
record: { $max: "$DataOra" }
}
},
{
$lookup: {
from: 't_web_statiprodottos',
localField: '_id',
foreignField: 'IdStatoProdotto',
as: 'record'
}
},
{
$replaceRoot: { newRoot: { $arrayElemAt: ["$record", 0] } }
},
{
$sort: { IdStatoProdotto: 1 }
}
];
return await T_WEB_StatiProdotto.aggregate(myquery);
};

View File

@@ -0,0 +1,23 @@
// t_web_tipiformato.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
/**
* @typedef {Object} TipoFormato
* @property {bigint} Id
* @property {number} IdTipoFormato
* @property {string} Descrizione
* @property {Date} DataOra
* @property {boolean} Enabled
* @property {boolean} EnabledAlFresco
*/
const TipoFormatoSchema = new Schema({
IdTipoFormato: Number,
Descrizione: { type: String, maxlength: 100 },
DataOra: Date,
Enabled: Boolean,
EnabledAlFresco: Boolean
}, { collection: 't_web_tipiformatos' });
module.exports = mongoose.model('T_WEB_TipiFormato', TipoFormatoSchema);