Gestione Ordini: evaso...
This commit is contained in:
@@ -21,6 +21,7 @@ const productSchema = new Schema({
|
||||
},
|
||||
active: {
|
||||
type: Boolean,
|
||||
default: true,
|
||||
},
|
||||
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
|
||||
idStorehouses: [
|
||||
@@ -172,50 +173,44 @@ module.exports.findAllIdApp = async function (idapp, code) {
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "orders",
|
||||
localField: "_id",
|
||||
foreignField: "idProduct",
|
||||
as: "productOrders",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: "$productOrders",
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: "$_id",
|
||||
products: { $push: "$$ROOT" },
|
||||
totalQty: {
|
||||
$sum: {
|
||||
$cond: {
|
||||
if: { $lt: ["$productOrders.status", 4] }, // Include stati minori di 4
|
||||
then: "$productOrders.quantity",
|
||||
else: 0
|
||||
}
|
||||
from: 'orders',
|
||||
let: { productId: '$_id' },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{ $eq: ['$idProduct', '$$productId'] },
|
||||
{ $lt: ['$status', shared_consts.OrderStatus.ORDER_CONFIRMED] }
|
||||
]
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0,
|
||||
products: {
|
||||
$map: {
|
||||
input: "$products",
|
||||
as: "product",
|
||||
in: {
|
||||
$mergeObjects: [
|
||||
"$$product",
|
||||
{ totalQty: { quantity: "$totalQty" } }
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
totalQty: { $sum: '$quantity' }
|
||||
}
|
||||
}
|
||||
],
|
||||
as: 'productOrders'
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
totalQty: {
|
||||
$cond: {
|
||||
if: { $isArray: '$productOrders' },
|
||||
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
|
||||
else: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$unset: 'productOrders'
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
let ris = await Product.aggregate(query)
|
||||
|
||||
Reference in New Issue
Block a user