Aggiornamento modifiche preOrdini
This commit is contained in:
@@ -96,6 +96,10 @@ const productSchema = new Schema({
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
bookableQty: { // Quantità prenotabili
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
quantityLow: { //Soglia disponibilità bassa
|
||||
type: Number,
|
||||
default: 0,
|
||||
@@ -275,27 +279,82 @@ module.exports.findAllIdApp = async function (idapp, code, id) {
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
totalQty: { $sum: '$quantity' }
|
||||
totalQty: { $sum: '$quantity' },
|
||||
}
|
||||
}
|
||||
],
|
||||
as: 'productOrders'
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'orders',
|
||||
let: { productId: '$_id' },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{ $eq: ['$idProduct', '$$productId'] },
|
||||
{ $lt: ['$status', shared_consts.OrderStatus.ORDER_CONFIRMED] }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
totalQtyPreordered: { $sum: '$quantitypreordered' }
|
||||
}
|
||||
}
|
||||
],
|
||||
as: 'productPreOrders'
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
QuantitaOrdinateInAttesa: {
|
||||
$cond: {
|
||||
if: { $isArray: '$productOrders' },
|
||||
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
|
||||
else: 0
|
||||
}
|
||||
$ifNull: [
|
||||
{
|
||||
$cond: {
|
||||
if: { $isArray: '$productOrders' },
|
||||
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
|
||||
else: 0
|
||||
}
|
||||
},
|
||||
0
|
||||
]
|
||||
},
|
||||
QuantitaPrenotateInAttesa: {
|
||||
$ifNull: [
|
||||
{
|
||||
$cond: {
|
||||
if: { $isArray: '$productPreOrders' },
|
||||
then: { $arrayElemAt: ['$productPreOrders.totalQtyPreordered', 0] },
|
||||
else: 0
|
||||
}
|
||||
},
|
||||
0
|
||||
]
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
quantityAvailable: {
|
||||
$subtract: ["$stockQty", "$QuantitaOrdinateInAttesa"],
|
||||
},
|
||||
bookableAvailableQty: {
|
||||
$subtract: ["$bookableQty", "$QuantitaPrenotateInAttesa"],
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$unset: 'productOrders'
|
||||
},
|
||||
{
|
||||
$unset: 'productPreOrders'
|
||||
},
|
||||
|
||||
);
|
||||
|
||||
@@ -352,6 +411,14 @@ module.exports.getProductByID = function (id, callback) {
|
||||
Product.findById(id, callback);
|
||||
}
|
||||
|
||||
module.exports.updateProductInOrder = async function (order) {
|
||||
|
||||
if (order.product)
|
||||
order.product = await Product.getProductById(order.product._id);
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
module.exports.createIndexes((err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user