aggiornato prodotti e scontistica

This commit is contained in:
Surya Paolo
2023-12-29 15:19:15 +01:00
parent 3d1dddba39
commit 633b9a2d8c
7 changed files with 117 additions and 34 deletions

View File

@@ -39,6 +39,7 @@ router.post('/import', authenticate, async (req, res) => {
const cmd = req.body.cmd;
const idapp = req.body.idapp;
const data = req.body.data;
const options = req.body.data.options;
try {
const liste = require('../data/liste');
@@ -49,8 +50,7 @@ router.post('/import', authenticate, async (req, res) => {
});
} else if (cmd === shared_consts.Cmd.PRODUCTS) {
let dataObjects = JSON.parse(`[${data}]`);
let dataObjects = JSON.parse(`[${data.arrdata}]`);
let updated = 0;
let imported = 0;
@@ -59,7 +59,7 @@ router.post('/import', authenticate, async (req, res) => {
for (const product of dataObjects) {
let isnuovo = false
let setta = false
let productInfo = {
idapp: product.idapp,
code: product.code,
@@ -72,27 +72,35 @@ router.post('/import', authenticate, async (req, res) => {
}
if (product.cat_name) {
// Cerca la Categoria
let reccateg = await CatProd.findOne({ idapp, name: product.cat_name }).lean();
if (!reccateg) {
// Non esiste questo produttore, quindi lo creo !
reccateg = new CatProd({ idapp, name: product.cat_name });
ris = await reccateg.save();
reccateg = await CatProd.findOne({ idapp, name: product.cat_name }).lean();
}
if (reccateg) {
productInfo.idCatProds.push(reccateg._id);
arrcat = product.cat_name.trim().split(',');
for (const mycat of arrcat) {
// Cerca la Categoria
let reccateg = await CatProd.findOne({ idapp, name: mycat }).lean();
if (!reccateg) {
// Non esiste questo produttore, quindi lo creo !
reccateg = new CatProd({ idapp, name: mycat });
ris = await reccateg.save();
reccateg = await CatProd.findOne({ idapp, name: mycat }).lean();
}
if (reccateg) {
productInfo.idCatProds.push(reccateg._id);
}
}
}
if (!product.hasOwnProperty('active')) {
product.active = true;
}
delete product.code;
delete product.name;
delete product.link;
if (product.code)
delete product.code;
if (product.name)
delete product.name;
if (product.link)
delete product.link;
let esisteindb = await ProductInfo.findOne({ code: productInfo.code }).lean();
// Update ProductInfo
let risrecInfo = await ProductInfo.findOneAndUpdate({ code: productInfo.code }, { $set: productInfo }, { new: true, upsert: true });
@@ -109,12 +117,17 @@ router.post('/import', authenticate, async (req, res) => {
console.log('Changed: ', recnewInfo.name + ': ' + arrfieldchange);
}
}
let recProductExist = await Product.findOne({ idProductInfo: product.idProductInfo }).lean();
if (!recProductExist) {
isnuovo = true;
}
if (!options.aggiornaStockQta && esisteindb) {
delete product.stockQty;
delete product.bookableQty;
}
let risrec = await Product.findOneAndUpdate({ idProductInfo: product.idProductInfo }, { $set: product }, { new: true, upsert: true });
let recnew = await Product.findOne({ idProductInfo: product.idProductInfo }).lean();