Corretto Ordini e visualizzazione dei Totali

This commit is contained in:
Surya Paolo
2023-12-15 21:50:21 +01:00
parent 05ec283882
commit 4d9eccd1ae
10 changed files with 326 additions and 314 deletions

View File

@@ -4,8 +4,8 @@ const Schema = mongoose.Schema;
const tools = require('../tools/general');
const Producer = require('../models/producer');
const Storehouse = require('../models/storehouse');
const Provider = require('../models/provider');
const Storehouse = require('../models/storehouse');
const Provider = require('../models/provider');
const shared_consts = require('../tools/shared_nodejs');
@@ -92,10 +92,6 @@ const productSchema = new Schema({
type: Number,
default: 0,
},
quantityAvailable: {
type: Number,
default: 0,
},
quantityLow: { //Soglia disponibilità bassa
type: Number,
default: 0,
@@ -167,88 +163,124 @@ module.exports.executeQueryTable = function (idapp, params) {
return tools.executeQueryTable(this, idapp, params);
};
module.exports.findAllIdApp = async function (idapp, code) {
let myfind = { idapp, active: true };
module.exports.getProductByCode = function (idapp, code) {
return Product.findAllIdApp(idapp, code);
}
if (code) {
myfind = { ...myfind, code }
}
module.exports.getProductById = async function (id) {
const arrris = await Product.findAllIdApp('', '', id);
return arrris && arrris.length > 0 ? arrris[0] : null
}
// return await Product.find(myfind);
module.exports.findAllIdApp = async function (idapp, code, id) {
let myfind = {};
let myqueryadd = {};
let query = [];
const query = [
{ $match: myfind },
{
$lookup: {
from: 'producers',
localField: 'idProducer',
foreignField: '_id',
as: 'producer'
try {
if (idapp)
myfind = { idapp, active: true };
if (code) {
myfind = { ...myfind, code }
}
if (id) {
myqueryadd = {
$addFields: {
myId1: {
$toObjectId: id,
},
},
}
},
{ $unwind: '$producer' },
{
$lookup: {
from: 'providers',
localField: 'idProvider',
foreignField: '_id',
as: 'provider'
myfind = {
$expr: {
$eq: ["$_id", "$myId1"],
},
}
},
{ $unwind: '$provider' },
{
$lookup: {
from: 'storehouses',
localField: 'idStorehouses',
foreignField: '_id',
as: 'storehouses'
}
},
{
$lookup: {
from: 'orders',
let: { productId: '$_id' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$idProduct', '$$productId'] },
{ $lt: ['$status', shared_consts.OrderStatus.ORDER_CONFIRMED] }
]
query.push(myqueryadd);
}
// return await Product.find(myfind);
query.push(
{ $match: myfind },
{
$lookup: {
from: 'producers',
localField: 'idProducer',
foreignField: '_id',
as: 'producer'
}
},
{ $unwind: '$producer' },
{
$lookup: {
from: 'providers',
localField: 'idProvider',
foreignField: '_id',
as: 'provider'
}
},
{ $unwind: '$provider' },
{
$lookup: {
from: 'storehouses',
localField: 'idStorehouses',
foreignField: '_id',
as: 'storehouses'
}
},
{
$lookup: {
from: 'orders',
let: { productId: '$_id' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$idProduct', '$$productId'] },
{ $lt: ['$status', shared_consts.OrderStatus.ORDER_CONFIRMED] }
]
}
}
},
{
$group: {
_id: null,
totalQty: { $sum: '$quantity' }
}
}
},
{
$group: {
_id: null,
totalQty: { $sum: '$quantity' }
],
as: 'productOrders'
}
},
{
$addFields: {
QuantitaOrdinateInAttesa: {
$cond: {
if: { $isArray: '$productOrders' },
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
else: 0
}
}
],
as: 'productOrders'
}
},
{
$addFields: {
QuantitaOrdinateInAttesa: {
$cond: {
if: { $isArray: '$productOrders' },
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
else: 0
}
}
}
},
{
$unset: 'productOrders'
},
},
{
$unset: 'productOrders'
},
];
);
let ris = await Product.aggregate(query)
let ris = await Product.aggregate(query)
return ris;
return ris;
} catch (e) {
console.error('E', e);
}
};
@@ -269,10 +301,6 @@ module.exports.getProductByTitle = function (query, sort, callback) {
Product.find(query, null, sort, callback)
}
module.exports.getProductByCode = function (idapp, code) {
return Product.findOne({ idapp, code })
}
module.exports.filterProductByDepartment = function (department, callback) {
let regexp = new RegExp(`^${department}$`, 'i')
var query = { department: { $regex: regexp } };
@@ -301,7 +329,7 @@ module.exports.createIndexes((err) => {
module.exports.convertAfterImport = async function (idapp, dataObjects) {
const arrprod = await Product.find({idapp}).lean();
const arrprod = await Product.find({ idapp }).lean();
for (const prod of arrprod) {
let setta = false;