Corretto Ordini e visualizzazione dei Totali
This commit is contained in:
@@ -98,7 +98,6 @@ MovementSchema.statics.addMov = async function (idapp, accountFromIdTable, accou
|
||||
|
||||
let mymov = await Movement.create(
|
||||
{
|
||||
_id: new ObjectID().toString(),
|
||||
idapp,
|
||||
transactionDate: new Date(),
|
||||
accountFromId: accountFromIdTable._id,
|
||||
|
||||
@@ -180,30 +180,33 @@ module.exports.getOrderByID = function (id, callback) {
|
||||
}
|
||||
|
||||
module.exports.createOrder = async function (order) {
|
||||
const orderModel = new Order(order);
|
||||
|
||||
return await orderModel.save(order)
|
||||
.then((ris) => {
|
||||
if (!!ris)
|
||||
return ris._id;
|
||||
return null;
|
||||
});
|
||||
try {
|
||||
return await Order.create(order)
|
||||
.then((ris) => {
|
||||
if (!!ris)
|
||||
return ris._id;
|
||||
return null;
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('err', e);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.updateStatusOrders = async function (arrOrders, status) {
|
||||
|
||||
|
||||
for (const order of arrOrders) {
|
||||
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: status });
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports.updateStatusOrdersElements = async function (arrOrders, myelements) {
|
||||
|
||||
|
||||
for (const order of arrOrders) {
|
||||
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: myelements });
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports.getTotalOrderById = async function (id) {
|
||||
|
||||
@@ -6,8 +6,8 @@ const shared_consts = require('../tools/shared_nodejs');
|
||||
const Order = require('../models/order');
|
||||
var { User } = require('../models/user');
|
||||
|
||||
const Storehouse = require('../models/storehouse');
|
||||
const Provider = require('../models/provider');
|
||||
const Storehouse = require('../models/storehouse');
|
||||
const Provider = require('../models/provider');
|
||||
const Product = require('../models/product');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
@@ -35,7 +35,7 @@ const OrdersCartSchema = new Schema({
|
||||
type: Number,
|
||||
Default: 0,
|
||||
},
|
||||
evaso: { // e quindi è stato tolto dal magazzino (aggiornando il campo StockQty)
|
||||
evaso: { // e quindi è stato tolto dal magazzino (aggiornando il campo stockQty)
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
@@ -141,10 +141,10 @@ module.exports.getStatusCartByUserId = async function (uid, idapp, numorder) {
|
||||
}
|
||||
|
||||
module.exports.getRecCartByUserId = async function (uid, idapp, numorder) {
|
||||
let query = { userId: uid, idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
||||
let query = { userId: uid, idapp, status: { $lt: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
||||
let myorderscart = null;
|
||||
if (numorder > 0) {
|
||||
query = { userId: uid, idapp, numorder, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
||||
query = { userId: uid, idapp, numorder, status: { $lt: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
||||
}
|
||||
|
||||
myorderscart = await OrdersCart.findOne(query).lean();
|
||||
@@ -160,171 +160,91 @@ module.exports.getOrdersCartById = async function (id) {
|
||||
}
|
||||
module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
|
||||
|
||||
let query = { idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT }, deleted: false }
|
||||
let myorderscart = null;
|
||||
if (numorder > 0) {
|
||||
query.numorder = numorder;
|
||||
}
|
||||
try {
|
||||
let query = { idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT }, deleted: false }
|
||||
let myorderscart = null;
|
||||
if (numorder > 0) {
|
||||
query.numorder = numorder;
|
||||
}
|
||||
|
||||
if (uid !== 'ALL') {
|
||||
query.userId = uid;
|
||||
}
|
||||
if (uid !== 'ALL') {
|
||||
query.userId = uid;
|
||||
}
|
||||
|
||||
myorderscart = await OrdersCart.find(query).populate('items.order')
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProduct',
|
||||
model: 'Product'
|
||||
},
|
||||
/*transform: function(doc, populated) {
|
||||
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
||||
populated.product = populated.idProduct;
|
||||
delete populated.idProduct;
|
||||
return populated;
|
||||
},*/
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProducer',
|
||||
model: 'Producer'
|
||||
},
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProvider',
|
||||
model: 'Provider'
|
||||
},
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idStorehouse',
|
||||
model: 'Storehouse'
|
||||
},
|
||||
/*transform: function(doc, populated) {
|
||||
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
||||
populated.storehouse = populated.idStorehouse;
|
||||
delete populated.idStorehouse;
|
||||
return populated;
|
||||
},*/
|
||||
})
|
||||
.populate('userId').lean();
|
||||
/*transform: function(doc, populated) {
|
||||
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
||||
populated.product = populated.idProduct;
|
||||
delete populated.idProduct;
|
||||
return populated;
|
||||
},*/
|
||||
|
||||
myorderscart = await OrdersCart.find(query)
|
||||
.populate('items.order')
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProduct',
|
||||
model: 'Product'
|
||||
},
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProducer',
|
||||
model: 'Producer'
|
||||
},
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProvider',
|
||||
model: 'Provider'
|
||||
},
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idStorehouse',
|
||||
model: 'Storehouse'
|
||||
},
|
||||
})
|
||||
.populate({
|
||||
path: 'userId',
|
||||
model: 'User',
|
||||
select: '_id name surname username profile', // Specify the fields you want to retrieve
|
||||
})
|
||||
.lean();
|
||||
|
||||
myorderscart = myorderscart.map(order => {
|
||||
order.user = order.userId
|
||||
order.userId = order.user._id
|
||||
order.items = order.items.map(item => {
|
||||
item.order.product = item.order.idProduct;
|
||||
delete item.order.idProduct;
|
||||
item.order.producer = item.order.idProducer;
|
||||
delete item.order.idProducer;
|
||||
item.order.storehouse = item.order.idStorehouse;
|
||||
delete item.order.idStorehouse;
|
||||
item.order.provider = item.order.idProvider;
|
||||
delete item.order.idProvider;
|
||||
if (item.order) {
|
||||
try {
|
||||
item.order.product = item.order.idProduct;
|
||||
item.order.idProduct = item.order.product._id;
|
||||
item.order.producer = item.order.idProducer;
|
||||
item.order.idProducer = item.order.producer._id;
|
||||
item.order.storehouse = item.order.idStorehouse;
|
||||
item.order.idStorehouse = item.order.storehouse._id;
|
||||
item.order.provider = item.order.idProvider;
|
||||
item.order.idProvider = item.order.provider._id;
|
||||
} catch (e) {
|
||||
console.error('Err: ', e);
|
||||
}
|
||||
}
|
||||
return item;
|
||||
});
|
||||
return order;
|
||||
});
|
||||
|
||||
/*for (let ind = 0; ind < myorderscart.length; ind++) {
|
||||
for (const idkey in myorderscart[ind].items) {
|
||||
try {
|
||||
let idorder = myorderscart[ind].items[idkey]._id.toString();
|
||||
const myorder = myorderscart[ind].items[idkey].order;
|
||||
if (!!myorder) {
|
||||
idorder = myorderscart[ind].items[idkey].order._id.toString();
|
||||
}
|
||||
// myorderscart[ind].nameSurname = await User.getNameSurnameById(idapp, myorderscart[ind].userId);
|
||||
// const myord = await Order.getTotalOrderById(idorder);
|
||||
// if (myord.length > 0) {
|
||||
// myorderscart[ind].items[idkey].order = myord[0];
|
||||
// }
|
||||
} catch (e) {
|
||||
console.log('err', e);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
/* if (!!mycart) {
|
||||
for (const idkey in mycart.items) {
|
||||
try {
|
||||
idorder = mycart.items[idkey]._id.toString();
|
||||
const myorder = mycart.items[idkey].order;
|
||||
if (!!myorder) {
|
||||
idorder = mycart.items[idkey].order._id.toString();
|
||||
}
|
||||
const myord = await Order.getTotalOrderById(idorder);
|
||||
if (myord.length > 0) {
|
||||
mycart.items[idkey]._doc.order = myord[0];
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('err', e);
|
||||
}
|
||||
}
|
||||
return mycart;
|
||||
}*/
|
||||
return myorderscart
|
||||
return myorderscart
|
||||
} catch (e) {
|
||||
console.error('Err:', e);
|
||||
}
|
||||
// return null;
|
||||
}
|
||||
|
||||
module.exports.getOrdersCartByDepartmentId = async function (depId, idapp) {
|
||||
let query = {
|
||||
idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT },
|
||||
deleted: false,
|
||||
}
|
||||
const myorderscart = await OrdersCart.find(query).lean();
|
||||
|
||||
for (let ind = 0; ind < myorderscart.length; ind++) {
|
||||
for (const idkey in myorderscart[ind].items) {
|
||||
try {
|
||||
let idorder = myorderscart[ind].items[idkey]._id.toString();
|
||||
const myorder = myorderscart[ind].items[idkey].order;
|
||||
if (!!myorder) {
|
||||
idorder = myorderscart[ind].items[idkey].order._id.toString();
|
||||
}
|
||||
myorderscart[ind].nameSurname = await User.getNameSurnameById(idapp, myorderscart[ind].userId);
|
||||
const myord = await Order.getTotalOrderById(idorder);
|
||||
if (myord.length > 0) {
|
||||
myorderscart[ind].items[idkey].order = myord[0];
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('err', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return myorderscart
|
||||
// return null;
|
||||
}
|
||||
|
||||
module.exports.getOrderById = async function (Id, idapp) {
|
||||
let query = { _id: Id, idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
||||
const myorderscart = await OrdersCart.find(query).lean();
|
||||
|
||||
for (let ind = 0; ind < myorderscart.length; ind++) {
|
||||
for (const idkey in myorderscart[ind].items) {
|
||||
try {
|
||||
let idorder = myorderscart[ind].items[idkey]._id.toString();
|
||||
const myorder = myorderscart[ind].items[idkey].order;
|
||||
if (!!myorder) {
|
||||
idorder = myorderscart[ind].items[idkey].order._id.toString();
|
||||
}
|
||||
myorderscart[ind].nameSurname = await User.getNameSurnameById(idapp, myorderscart[ind].userId);
|
||||
const myord = await Order.getTotalOrderById(idorder);
|
||||
if (myord.length > 0) {
|
||||
myorderscart[ind].items[idkey].order = myord[0];
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('err', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return myorderscart
|
||||
// return null;
|
||||
}
|
||||
|
||||
module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
|
||||
let query = {
|
||||
@@ -362,23 +282,30 @@ module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
|
||||
|
||||
module.exports.setFieldInOrdersById = async function (objtoset, myOrderCart) {
|
||||
|
||||
const ris = await OrdersCart.findOneAndUpdate(
|
||||
{ _id: myOrderCart._id },
|
||||
{
|
||||
$set: objtoset
|
||||
},
|
||||
{ new: false }
|
||||
)
|
||||
try {
|
||||
let ris2 = null;
|
||||
// Imposta su tutti i singoli prodotti ordinati (Order)
|
||||
for (const recitem of myOrderCart.items) {
|
||||
ris2 = await Order.findOneAndUpdate(
|
||||
{ _id: recitem.order._id },
|
||||
{
|
||||
$set: objtoset
|
||||
},
|
||||
{ new: false }
|
||||
)
|
||||
// console.log('ris', ris2);
|
||||
}
|
||||
|
||||
// Imposta su tutti i singoli prodotti ordinati (Order)
|
||||
for (const order of myOrderCart.items) {
|
||||
await Order.findOneAndUpdate(
|
||||
{ _id: order._id },
|
||||
const ris = await OrdersCart.findOneAndUpdate(
|
||||
{ _id: myOrderCart._id },
|
||||
{
|
||||
$set: objtoset
|
||||
},
|
||||
{ new: false }
|
||||
)
|
||||
|
||||
} catch (e) {
|
||||
console.log('Err', e);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -463,7 +390,6 @@ module.exports.updateStockQtaDalMagazzino = async function (idorderscart) {
|
||||
|
||||
if (myorderscart) {
|
||||
for (const idkey in myorderscart.items) {
|
||||
let idorder = myorderscart.items[idkey]._id;
|
||||
let order = myorderscart.items[idkey].order;
|
||||
|
||||
if (!order.evaso) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user