aggiornamento Ordini GAS filtri
This commit is contained in:
@@ -9,6 +9,7 @@ const { ObjectID } = require('mongodb');
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
@@ -362,7 +363,7 @@ module.exports.updateTotals = function (order) {
|
||||
} else {
|
||||
mypricecalc = (order.price * order.quantity) + (order.price * order.quantitypreordered);
|
||||
}
|
||||
|
||||
|
||||
order.TotalPriceProductCalc += mypricecalc;
|
||||
order.TotalPriceProduct += mypricecalc;
|
||||
order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2));
|
||||
@@ -607,6 +608,127 @@ module.exports.getTotalOrderById = async function (id) {
|
||||
return await Order.aggregate(query);
|
||||
}
|
||||
|
||||
module.exports.GeneraCSVOrdineProdotti = async function () {
|
||||
|
||||
const myidGasordine = '65c2a8cc379ee4f57e865ee7';
|
||||
|
||||
const myquery = [
|
||||
{ $match: { idGasordine: ObjectID(myidGasordine) } },
|
||||
{
|
||||
$lookup: {
|
||||
from: 'products',
|
||||
localField: 'idProduct',
|
||||
foreignField: '_id',
|
||||
as: 'product'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$product',
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'productinfos',
|
||||
localField: 'product.idProductInfo',
|
||||
foreignField: '_id',
|
||||
as: 'productInfo'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$productInfo',
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'gasordines',
|
||||
localField: 'idGasordine',
|
||||
foreignField: '_id',
|
||||
as: 'gasordine'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$gasordine',
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
$or: [
|
||||
{ 'gasordine.active': true }, // Include documents where gasordines.active is true
|
||||
{ 'gasordine': { $exists: false } } // Include documents where gasordines array doesn't exist
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
$match: {
|
||||
$or: [
|
||||
{ quantity: { $gt: 0 }, },
|
||||
{ quantitypreordered: { $gt: 0 }, }
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
$group: {
|
||||
_id: "$product._id",
|
||||
name: { $first: "$productInfo.name" },
|
||||
weight: { $first: "$productInfo.weight" },
|
||||
price_acquistato: { $first: "$product.price_acquistato" },
|
||||
totalQuantity: { $sum: { $add: ["$quantity", "$quantitypreordered"] } },
|
||||
totalPrice_acquistato: { $sum: { $multiply: ["$product.price_acquistato", { $add: ["$quantity", "$quantitypreordered"] }] } },
|
||||
count: { $sum: 1 }
|
||||
}
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
name: 1 // Sort in ascending order based on the "date_created" field
|
||||
},
|
||||
}
|
||||
];
|
||||
|
||||
let myorderscart = await Order.aggregate(myquery);
|
||||
|
||||
console.log(myorderscart)
|
||||
|
||||
return generateCSV(myorderscart, 'outout.csv');
|
||||
|
||||
}
|
||||
|
||||
function generateCSV(data, outputPath) {
|
||||
const headers = ['Num', 'Nome', 'Peso', 'Prezzo', 'Quantita', 'Totale', 'Ordini'];
|
||||
|
||||
const rows = data.map(item => {
|
||||
const formattedPrice = item.price_acquistato.toString().replace(/\./g, ','); // Converti "." in ","
|
||||
const total = item.totalPrice_acquistato.toString().replace(/\./g, ','); // Converti "." in ","
|
||||
return [
|
||||
0,
|
||||
`"${item.name}"`,
|
||||
item.weight,
|
||||
formattedPrice,
|
||||
item.totalQuantity,
|
||||
total,
|
||||
item.count
|
||||
];
|
||||
});
|
||||
|
||||
rows.unshift(headers);
|
||||
|
||||
const csvData = rows.map(row => row.join('|'));
|
||||
|
||||
fs.writeFile(outputPath, csvData.join('\n'), (err) => {
|
||||
if (err) {
|
||||
console.error('Error writing CSV file:', err);
|
||||
} else {
|
||||
console.log('CSV file has been successfully generated:', outputPath);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
// const Order = mongoose.model('Order', OrderSchema);
|
||||
|
||||
Reference in New Issue
Block a user