Gestione Ordini: evaso...

This commit is contained in:
Surya Paolo
2023-12-13 19:17:53 +01:00
parent a2bd4f6e97
commit fcdd826c54
11 changed files with 314 additions and 125 deletions

View File

@@ -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)