- newsletter: prende la lista utenti (flag news_on)
- Abilita a Tutti la Newsletter news_on - isCommerciale - JobsInProgress - PCB: Corretto Totali che era a zero
This commit is contained in:
@@ -36,16 +36,16 @@ const productSchema = new Schema({
|
||||
isbn: {
|
||||
type: String,
|
||||
},
|
||||
idProductInfo: { type: Schema.Types.ObjectId, ref: 'ProductInfo' },
|
||||
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
|
||||
idProductInfo: { type: Schema.Types.ObjectId, ref: 'ProductInfo', index: true },
|
||||
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer', index: true },
|
||||
idStorehouses: [
|
||||
{ type: Schema.Types.ObjectId, ref: 'Storehouse' }
|
||||
{ type: Schema.Types.ObjectId, ref: 'Storehouse', index: true }
|
||||
],
|
||||
idGasordine: { type: Schema.Types.ObjectId, ref: 'Gasordine' },
|
||||
idGasordine: { type: Schema.Types.ObjectId, ref: 'Gasordine', index: true },
|
||||
idScontisticas: [
|
||||
{ type: Schema.Types.ObjectId, ref: 'Scontistica' }
|
||||
{ type: Schema.Types.ObjectId, ref: 'Scontistica', index: true }
|
||||
],
|
||||
idProvider: { type: Schema.Types.ObjectId, ref: 'Provider' },
|
||||
idProvider: { type: Schema.Types.ObjectId, ref: 'Provider', index: true },
|
||||
prezzo_ivato: { // Con IVA
|
||||
type: Number
|
||||
},
|
||||
@@ -137,6 +137,7 @@ const productSchema = new Schema({
|
||||
stockQty: { // in magazzino
|
||||
type: Number,
|
||||
default: 0,
|
||||
index: true,
|
||||
},
|
||||
stockBloccatiQty: { // Prenotati Bloccati
|
||||
type: Number,
|
||||
@@ -220,7 +221,7 @@ const productSchema = new Schema({
|
||||
},
|
||||
validaprod: {
|
||||
esito: {
|
||||
type: Number,
|
||||
type: Number,
|
||||
},
|
||||
data: {
|
||||
type: Date,
|
||||
@@ -468,176 +469,33 @@ module.exports.findAllIdApp = async function (idapp, code, id, all) {
|
||||
// return await Product.find(myfind);
|
||||
|
||||
query.push(
|
||||
// PRIMO FILTRO: riduce subito il numero di documenti
|
||||
{ $match: myfind },
|
||||
|
||||
// UNICO LOOKUP ORDERS CON FACET PER RIDURRE I DOPPIONI
|
||||
{
|
||||
$lookup: {
|
||||
from: 'producers',
|
||||
localField: 'idProducer',
|
||||
foreignField: '_id',
|
||||
as: 'producer'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$producer',
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'productinfos',
|
||||
localField: 'idProductInfo',
|
||||
foreignField: '_id',
|
||||
as: 'productInfo'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$productInfo',
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'gasordines',
|
||||
localField: 'idGasordine',
|
||||
foreignField: '_id',
|
||||
as: 'gasordine'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$gasordine',
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
$or: [
|
||||
{ 'gasordine.active': true }, // Include documents where gasordines.active is true
|
||||
{ 'gasordine': { $exists: false } } // Include documents where gasordines array doesn't exist
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$_id',
|
||||
gasordine: { $first: '$gasordine' },
|
||||
originalFields: { $first: '$$ROOT' } // Preserve all existing fields
|
||||
}
|
||||
},
|
||||
{
|
||||
$replaceRoot: {
|
||||
newRoot: {
|
||||
$mergeObjects: ['$originalFields', { gasordine: '$gasordine' }]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'providers',
|
||||
localField: 'idProvider',
|
||||
foreignField: '_id',
|
||||
as: 'provider'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$provider',
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'authors',
|
||||
localField: 'productInfo.idAuthors',
|
||||
foreignField: '_id',
|
||||
as: 'productInfo.authors'
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'publishers',
|
||||
localField: 'productInfo.idPublisher',
|
||||
foreignField: '_id',
|
||||
as: 'productInfo.publisher'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$productInfo.publisher',
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'collanas',
|
||||
localField: 'productInfo.idCollana',
|
||||
foreignField: '_id',
|
||||
as: 'productInfo.collana'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$productInfo.collana',
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'catprods',
|
||||
localField: 'productInfo.idCatProds',
|
||||
foreignField: '_id',
|
||||
as: 'productInfo.catprods'
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'subcatprods',
|
||||
localField: 'productInfo.idSubCatProds',
|
||||
foreignField: '_id',
|
||||
as: 'productInfo.subcatprods'
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'scontisticas',
|
||||
localField: 'idScontisticas',
|
||||
foreignField: '_id',
|
||||
as: 'scontisticas'
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'storehouses',
|
||||
localField: 'idStorehouses',
|
||||
foreignField: '_id',
|
||||
as: 'storehouses'
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'orders',
|
||||
let: { productId: '$_id' },
|
||||
from: "orders",
|
||||
let: { productId: "$_id" },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{ $eq: ['$idProduct', '$$productId'] },
|
||||
{ $eq: ["$idProduct", "$$productId"] },
|
||||
{
|
||||
$or: [
|
||||
{ $eq: ["$status", shared_consts.OrderStatus.CHECKOUT_SENT] },
|
||||
{
|
||||
$eq: ['$status', shared_consts.OrderStatus.CHECKOUT_SENT]
|
||||
},
|
||||
{
|
||||
$and: [{ $lt: ['$status', shared_consts.OrderStatus.CHECKOUT_SENT] },
|
||||
{
|
||||
$gt: [
|
||||
'$modify_at',
|
||||
{ $subtract: [new Date(), 60 * 60 * 1000] } // 1 hour in milliseconds 60 * 60
|
||||
]
|
||||
}]
|
||||
$and: [
|
||||
{ $lt: ["$status", shared_consts.OrderStatus.CHECKOUT_SENT] },
|
||||
{
|
||||
$gt: [
|
||||
"$modify_at",
|
||||
{ $subtract: [new Date(), 60 * 60 * 1000] }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -648,102 +506,162 @@ module.exports.findAllIdApp = async function (idapp, code, id, all) {
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
totalQty: { $sum: '$quantity' },
|
||||
totalQty: { $sum: "$quantity" },
|
||||
totalQtyPreordered: { $sum: "$quantitypreordered" }
|
||||
}
|
||||
}
|
||||
],
|
||||
as: 'productOrders'
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'orders',
|
||||
let: { productId: '$_id' },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{ $eq: ['$idProduct', '$$productId'] },
|
||||
{
|
||||
$or: [
|
||||
{
|
||||
$eq: ['$status', shared_consts.OrderStatus.CHECKOUT_SENT]
|
||||
},
|
||||
{
|
||||
$and: [{ $lt: ['$status', shared_consts.OrderStatus.CHECKOUT_SENT] },
|
||||
{
|
||||
$gt: [
|
||||
'$modify_at',
|
||||
{ $subtract: [new Date(), 60 * 60 * 1000] } // 1 hour in milliseconds 60 * 60
|
||||
]
|
||||
}]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
totalQtyPreordered: { $sum: '$quantitypreordered' }
|
||||
}
|
||||
}
|
||||
],
|
||||
as: 'productPreOrders'
|
||||
as: "orderSummary"
|
||||
}
|
||||
},
|
||||
|
||||
// ESTRAGGO LE QUANTITÀ IN CAMPI AGGIUNTIVI
|
||||
{
|
||||
$addFields: {
|
||||
QuantitaOrdinateInAttesa: {
|
||||
$ifNull: [
|
||||
{
|
||||
$cond: {
|
||||
if: { $isArray: '$productOrders' },
|
||||
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
|
||||
else: 0
|
||||
}
|
||||
},
|
||||
0
|
||||
]
|
||||
$ifNull: [{ $arrayElemAt: ["$orderSummary.totalQty", 0] }, 0]
|
||||
},
|
||||
QuantitaPrenotateInAttesa: {
|
||||
$ifNull: [
|
||||
{
|
||||
$cond: {
|
||||
if: { $isArray: '$productPreOrders' },
|
||||
then: { $arrayElemAt: ['$productPreOrders.totalQtyPreordered', 0] },
|
||||
else: 0
|
||||
}
|
||||
},
|
||||
0
|
||||
]
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
quantityAvailable: {
|
||||
$subtract: ["$stockQty", "$QuantitaOrdinateInAttesa"],
|
||||
},
|
||||
bookableAvailableQty: {
|
||||
$subtract: ["$maxbookableGASQty", "$QuantitaPrenotateInAttesa"],
|
||||
$ifNull: [{ $arrayElemAt: ["$orderSummary.totalQtyPreordered", 0] }, 0]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// CALCOLO DELLE DISPONIBILITÀ
|
||||
{
|
||||
$unset: 'productOrders'
|
||||
},
|
||||
{
|
||||
$unset: 'productPreOrders'
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
'productInfo.name': 1 // 1 for ascending order, -1 for descending order
|
||||
$addFields: {
|
||||
quantityAvailable: {
|
||||
$subtract: ["$stockQty", "$QuantitaOrdinateInAttesa"]
|
||||
},
|
||||
bookableAvailableQty: {
|
||||
$subtract: ["$maxbookableGASQty", "$QuantitaPrenotateInAttesa"]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// ELIMINO IL RISULTATO TEMPORANEO
|
||||
{ $unset: "orderSummary" },
|
||||
|
||||
// LOOKUP MULTIPLI MA ORGANIZZATI
|
||||
{
|
||||
$lookup: {
|
||||
from: "producers",
|
||||
localField: "idProducer",
|
||||
foreignField: "_id",
|
||||
as: "producer"
|
||||
}
|
||||
},
|
||||
{ $unwind: { path: "$producer", preserveNullAndEmptyArrays: true } },
|
||||
|
||||
{
|
||||
$lookup: {
|
||||
from: "productinfos",
|
||||
localField: "idProductInfo",
|
||||
foreignField: "_id",
|
||||
as: "productInfo"
|
||||
}
|
||||
},
|
||||
{ $unwind: { path: "$productInfo", preserveNullAndEmptyArrays: true } },
|
||||
|
||||
{
|
||||
$lookup: {
|
||||
from: "gasordines",
|
||||
localField: "idGasordine",
|
||||
foreignField: "_id",
|
||||
as: "gasordine"
|
||||
}
|
||||
},
|
||||
{ $unwind: { path: "$gasordine", preserveNullAndEmptyArrays: true } },
|
||||
|
||||
// FILTRO DOPO LOOKUP SU GASORDINE
|
||||
{
|
||||
$match: {
|
||||
$or: [
|
||||
{ "gasordine.active": true },
|
||||
{ gasordine: { $exists: false } }
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
// LOOKUP SU AUTHORS
|
||||
{
|
||||
$lookup: {
|
||||
from: "authors",
|
||||
localField: "productInfo.idAuthors",
|
||||
foreignField: "_id",
|
||||
as: "productInfo.authors"
|
||||
}
|
||||
},
|
||||
|
||||
// LOOKUP PUBBLICATORI, COLLANE, CATEGORIE, ECC.
|
||||
{
|
||||
$lookup: {
|
||||
from: "publishers",
|
||||
localField: "productInfo.idPublisher",
|
||||
foreignField: "_id",
|
||||
as: "productInfo.publisher"
|
||||
}
|
||||
},
|
||||
{ $unwind: { path: "$productInfo.publisher", preserveNullAndEmptyArrays: true } },
|
||||
|
||||
{
|
||||
$lookup: {
|
||||
from: "collanas",
|
||||
localField: "productInfo.idCollana",
|
||||
foreignField: "_id",
|
||||
as: "productInfo.collana"
|
||||
}
|
||||
},
|
||||
{ $unwind: { path: "$productInfo.collana", preserveNullAndEmptyArrays: true } },
|
||||
|
||||
{
|
||||
$lookup: {
|
||||
from: "catprods",
|
||||
localField: "productInfo.idCatProds",
|
||||
foreignField: "_id",
|
||||
as: "productInfo.catprods"
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "subcatprods",
|
||||
localField: "productInfo.idSubCatProds",
|
||||
foreignField: "_id",
|
||||
as: "productInfo.subcatprods"
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "scontisticas",
|
||||
localField: "idScontisticas",
|
||||
foreignField: "_id",
|
||||
as: "scontisticas"
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "storehouses",
|
||||
localField: "idStorehouses",
|
||||
foreignField: "_id",
|
||||
as: "storehouses"
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "providers",
|
||||
localField: "idProvider",
|
||||
foreignField: "_id",
|
||||
as: "provider"
|
||||
}
|
||||
},
|
||||
{ $unwind: { path: "$provider", preserveNullAndEmptyArrays: true } },
|
||||
|
||||
// ORDINAMENTO FINALE
|
||||
{
|
||||
$sort: {
|
||||
"productInfo.name": 1
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// console.log('query=', query);
|
||||
|
||||
Reference in New Issue
Block a user