- Modifiche a ProductInfo... Continua
This commit is contained in:
@@ -17,7 +17,6 @@ const Inventariogm = require('../models/inventariogm');
|
||||
const Importamacro = require('../models/importamacro');
|
||||
const ImportaDescr = require('../models/importadescr');
|
||||
const ImportaIsbn = require('../models/importaisbn');
|
||||
const ProductInfo = require('../models/productInfo');
|
||||
const CatProd = require('../models/catprod');
|
||||
const Collana = require('../models/collana');
|
||||
const Author = require('../models/author');
|
||||
@@ -72,7 +71,7 @@ function launchGenPdfIfNeeded() {
|
||||
}
|
||||
}
|
||||
|
||||
async function updateProductInfo(recproductInfoAttuale, product, idapp, mycatstr) {
|
||||
/*async function updateProductInfo(recproductInfoAttuale, product, idapp, mycatstr) {
|
||||
if (!recproductInfoAttuale || !mycatstr) return recproductInfoAttuale;
|
||||
|
||||
let idArgomentoNum = null;
|
||||
@@ -93,7 +92,7 @@ async function updateProductInfo(recproductInfoAttuale, product, idapp, mycatstr
|
||||
}
|
||||
|
||||
return productInfo;
|
||||
}
|
||||
}*/
|
||||
|
||||
async function findOrCreateCatProd(idapp, idArgomento, DescrArgomento) {
|
||||
let reccatprod = null;
|
||||
@@ -1240,28 +1239,28 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
code: product.code,
|
||||
};
|
||||
|
||||
const myproductInfo = await ProductInfo.findOne({ code: productInfo.code });
|
||||
const myproduct = await Product.findOne({ 'productInfo.code': productInfo.code });
|
||||
|
||||
// IMPOSTA I VALORI SOLO SE NON ESISTONO ! ALTRIMENTI LASCIA QUELLI GIA' IMPORTATI (O MODIFICATI)
|
||||
|
||||
if (getvalueByJsonText(product.url)) {
|
||||
if ((myproductInfo && !myproductInfo.link_macro) || !myproductInfo)
|
||||
if ((myproduct && !myproduct.productInfo.link_macro) || !myproduct.productInfo)
|
||||
productInfo.link_macro = getvalueByJsonText(product.url);
|
||||
}
|
||||
if (getvalueByJsonText(product.descrizione)) {
|
||||
if ((myproductInfo && !myproductInfo.descrizione_breve_macro) || !myproductInfo)
|
||||
if ((myproduct && !myproduct.productInfo.descrizione_breve_macro) || !myproduct.productInfo)
|
||||
productInfo.descrizione_breve_macro = getvalueByJsonText(product.descrizione);
|
||||
}
|
||||
if (getvalueByJsonText(product.descrizione_completa)) {
|
||||
if ((myproductInfo && !myproductInfo.descrizione_completa_macro) || !myproductInfo)
|
||||
if ((myproduct && !myproduct.productInfo.descrizione_completa_macro) || !myproduct.productInfo)
|
||||
productInfo.descrizione_completa_macro = getvalueByJsonText(product.descrizione_completa);
|
||||
}
|
||||
if (getvalueByJsonText(product.sottotitolo)) {
|
||||
if ((myproductInfo && !myproductInfo.sottotitolo) || !myproductInfo)
|
||||
if ((myproduct && !myproduct.productInfo.sottotitolo) || !myproduct.productInfo)
|
||||
productInfo.sottotitolo = getvalueByJsonText(product.sottotitolo);
|
||||
}
|
||||
if (getvalueByJsonText(product.titolo)) {
|
||||
if ((myproductInfo && !myproductInfo.name) || !myproductInfo)
|
||||
if ((myproduct && !myproduct.productInfo.name) || !myproduct.productInfo)
|
||||
productInfo.name = getvalueByJsonText(product.titolo);
|
||||
}
|
||||
|
||||
@@ -1520,27 +1519,27 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
let imported = 0;
|
||||
let errors = 0;
|
||||
|
||||
for (const product of dataObjects) {
|
||||
for (const productImported of dataObjects) {
|
||||
let isnuovo = false;
|
||||
let setta = false;
|
||||
|
||||
let productInfo = {
|
||||
idapp: product.idapp,
|
||||
code: product.code,
|
||||
name: product.name,
|
||||
link: product.link,
|
||||
idapp: productImported.idapp,
|
||||
code: productImported.code,
|
||||
name: productImported.name,
|
||||
link: productImported.link,
|
||||
idCatProds: [],
|
||||
idSubCatProds: [],
|
||||
// img: 'upload/products/' + product.code + '.jpg',
|
||||
imagefile: product.code + '.jpg',
|
||||
weight: product.weight,
|
||||
unit: tools.getIdUnitsByText(product.unit),
|
||||
imagefile: productImported.code + '.jpg',
|
||||
weight: productImported.weight,
|
||||
unit: tools.getIdUnitsByText(productImported.unit),
|
||||
productTypes: shared_consts.PRODUCTTYPE.PRODUCT,
|
||||
};
|
||||
|
||||
let reccateg = null;
|
||||
if (product.cat_name) {
|
||||
arrcat = product.cat_name.trim().split(',');
|
||||
if (productImported.cat_name) {
|
||||
arrcat = productImported.cat_name.trim().split(',');
|
||||
for (const mycat of arrcat) {
|
||||
let mycatstr = mycat.trim();
|
||||
|
||||
@@ -1559,8 +1558,8 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (product.subcat_name) {
|
||||
arrsubcat = product.subcat_name.trim().split(',');
|
||||
if (productImported.subcat_name) {
|
||||
arrsubcat = productImported.subcat_name.trim().split(',');
|
||||
productInfo.idSubCatProds = [];
|
||||
for (const mysubcat of arrsubcat) {
|
||||
let mysubcatstr = mysubcat.trim();
|
||||
@@ -1581,24 +1580,26 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (!product.hasOwnProperty('active')) {
|
||||
product.active = true;
|
||||
if (!productImported.hasOwnProperty('active')) {
|
||||
productImported.active = true;
|
||||
}
|
||||
|
||||
if (product.code) delete product.code;
|
||||
/*if (product.code) delete product.code;
|
||||
if (product.name) delete product.name;
|
||||
if (product.link) delete product.link;
|
||||
if (product.link) delete product.link;*/
|
||||
|
||||
let esisteindb = await ProductInfo.findOne({ code: productInfo.code }).lean();
|
||||
let esisteindb = await Product.findOne({ 'productInfo.code': productInfo.code }).lean();
|
||||
|
||||
// Update ProductInfo
|
||||
let risrecInfo = await ProductInfo.findOneAndUpdate(
|
||||
{ code: productInfo.code },
|
||||
{ $set: productInfo },
|
||||
let risrecInfo = await Product.findOneAndUpdate(
|
||||
{ 'productInfo.code': productInfo.code },
|
||||
{ $set: { productInfo: productInfo } },
|
||||
{ new: true, upsert: true }
|
||||
);
|
||||
|
||||
// .... ANDARE AVANTI DA QUI... .productInfo.... SISTEMARE!
|
||||
if (risrecInfo) {
|
||||
product.idProductInfo = risrecInfo._id;
|
||||
productImported.productInfo = risrecInfo._id;
|
||||
|
||||
recnewInfo = await ProductInfo.findOne({ code: productInfo.code }).lean();
|
||||
|
||||
@@ -1613,20 +1614,20 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
// Cerca il GAS
|
||||
let recGas = null;
|
||||
if (product.gas_name) {
|
||||
if (productImported.gas_name) {
|
||||
// Cerca il GAS
|
||||
recGas = await Gasordine.findOne({ idapp, name: product.gas_name }).lean();
|
||||
recGas = await Gasordine.findOne({ idapp, name: productImported.gas_name }).lean();
|
||||
}
|
||||
|
||||
if (!recGas && !!product.gas_name) {
|
||||
recGas = new Gasordine({ idapp, name: product.gas_name, active: true });
|
||||
if (!recGas && !!productImported.gas_name) {
|
||||
recGas = new Gasordine({ idapp, name: productImported.gas_name, active: true });
|
||||
// Non esiste questo GAS, quindi lo creo !
|
||||
ris = await recGas.save();
|
||||
recGas = await Gasordine.findOne({ idapp, name: product.gas_name }).lean();
|
||||
recGas = await Gasordine.findOne({ idapp, name: productImported.gas_name }).lean();
|
||||
}
|
||||
|
||||
let recProductExist = null;
|
||||
let queryprod = { idProductInfo: product.idProductInfo };
|
||||
let queryprod = { idProductInfo: productImported.idProductInfo };
|
||||
|
||||
if (recGas) {
|
||||
queryprod = { ...queryprod, idGasordine: recGas._id };
|
||||
@@ -1639,22 +1640,22 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
}
|
||||
|
||||
if (!options.aggiornaStockQty && esisteindb && !isnuovo) {
|
||||
delete product.stockQty;
|
||||
delete product.maxbookableGASQty;
|
||||
delete productImported.stockQty;
|
||||
delete productImported.maxbookableGASQty;
|
||||
}
|
||||
|
||||
// AGGIORNA PRODUCT
|
||||
let risrec = await Product.findOneAndUpdate(queryprod, { $set: product }, { new: true, upsert: true });
|
||||
let risrec = await Product.findOneAndUpdate(queryprod, { $set: productImported }, { new: true, upsert: true });
|
||||
|
||||
let recnew = await Product.findOne(queryprod).lean();
|
||||
|
||||
if (risrec) {
|
||||
if (risrec._id) {
|
||||
// Record existed, so it was updated
|
||||
let arrfieldchange = tools.differentObjects(product, recnew);
|
||||
let arrfieldchange = tools.differentObjects(productImported, recnew);
|
||||
if (arrfieldchange.length > 0) {
|
||||
updated++;
|
||||
console.log('Changed:', product.idProductInfo + ': ' + arrfieldchange);
|
||||
console.log('Changed:', productImported.idProductInfo + ': ' + arrfieldchange);
|
||||
}
|
||||
} else {
|
||||
// Record didn't exist, so it was created
|
||||
@@ -1662,13 +1663,13 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
}
|
||||
} else {
|
||||
// risrec is null or undefined, indicating an error
|
||||
console.error('Error: ', product.productInfo.name);
|
||||
console.error('Error: ', productImported.productInfo.name);
|
||||
errors++;
|
||||
}
|
||||
|
||||
await Product.singlerecconvert_AfterImport_AndSave(idapp, recnew, isnuovo);
|
||||
} else {
|
||||
console.error('Error ProductInfo: ', product.code);
|
||||
console.error('Error ProductInfo: ', productImported.code);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
@@ -1871,6 +1872,7 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
return res.status(400).send();
|
||||
});
|
||||
|
||||
/*
|
||||
async function importaCatalogo(data) {
|
||||
let updated = 0;
|
||||
let imported = 0;
|
||||
@@ -2059,11 +2061,6 @@ async function importaCatalogo(data) {
|
||||
productInfo.productTypes.push(vers);
|
||||
}
|
||||
|
||||
/*if (product.Data) {
|
||||
productInfo.date_pub = new Date(product.Data * 1000);
|
||||
// convert data to timestamp
|
||||
productInfo.date_pub_ts = productInfo.date_pub.getTime();
|
||||
}*/
|
||||
|
||||
productInfo.name = productInfo.name.replace(
|
||||
/ - Usato$| - Nuovo$| - Epub$| - Ebook$| - Mobi$| - DVD$| - Streaming$| - Download$/,
|
||||
@@ -2113,7 +2110,7 @@ async function importaCatalogo(data) {
|
||||
}
|
||||
|
||||
if (reccateg) {
|
||||
productInfo.idCatProds.push(reccateg._id);
|
||||
product.productInfo.idCatProds.push(reccateg._id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2149,7 +2146,7 @@ async function importaCatalogo(data) {
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
||||
productInfo.idAuthors = [];
|
||||
product.productInfo.idAuthors = [];
|
||||
|
||||
for (const myauthor of arrAuthor) {
|
||||
// Cerca l'Autore
|
||||
@@ -2162,7 +2159,7 @@ async function importaCatalogo(data) {
|
||||
}
|
||||
|
||||
if (recauthor) {
|
||||
productInfo.idAuthors.push(recauthor._id);
|
||||
product.productInfo.idAuthors.push(recauthor._id);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -2173,7 +2170,7 @@ async function importaCatalogo(data) {
|
||||
|
||||
if (product.subcat_name) {
|
||||
arrsubcat = product.subcat_name.trim().split(',');
|
||||
productInfo.idSubCatProds = [];
|
||||
product.productInfo.idSubCatProds = [];
|
||||
for (const mysubcat of arrsubcat) {
|
||||
let mysubcatstr = mysubcat.trim();
|
||||
|
||||
@@ -2188,13 +2185,13 @@ async function importaCatalogo(data) {
|
||||
}
|
||||
|
||||
if (recsubcateg) {
|
||||
productInfo.idSubCatProds.push(recsubcateg._id);
|
||||
product.productInfo.idSubCatProds.push(recsubcateg._id);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (productInfo.publisher) {
|
||||
if (product.productInfo.publisher) {
|
||||
try {
|
||||
publisher = productInfo.publisher.trim();
|
||||
publisher = product.productInfo.publisher.trim();
|
||||
// Cerca la Sotto Categoria
|
||||
let recpublisher = await Publisher.findOne({ idapp, name: publisher }).lean();
|
||||
if (!recpublisher) {
|
||||
@@ -2205,7 +2202,7 @@ async function importaCatalogo(data) {
|
||||
}
|
||||
|
||||
if (recpublisher) {
|
||||
productInfo.idPublisher = recpublisher._id;
|
||||
product.productInfo.idPublisher = recpublisher._id;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
@@ -2213,19 +2210,19 @@ async function importaCatalogo(data) {
|
||||
}
|
||||
// Aggiorna la collana solo se non è stata già impostata nel record attuale
|
||||
//if (recproductInfoAttuale && !recproductInfoAttuale.idCollana && product.DescrizioneCollana) {
|
||||
if (productInfo.collezione && productInfo.numCollana) {
|
||||
if (product.productInfo.collezione && product.productInfo.numCollana) {
|
||||
reccollana = await Collana.findOne({ idapp, title: collezione }).lean();
|
||||
if (!reccollana) {
|
||||
try {
|
||||
// Non esiste questa collana, quindi la creo !
|
||||
reccollana = new Collana({ idapp, idCollana: productInfo.numCollana, title: product.DescrizioneCollana });
|
||||
reccollana = new Collana({ idapp, idCollana: product.productInfo.numCollana, title: product.DescrizioneCollana });
|
||||
ris = await reccoll.save();
|
||||
} catch (e) {
|
||||
console.error('Err', e);
|
||||
}
|
||||
}
|
||||
|
||||
if (reccollana) productInfo.idCollana = reccollana._id;
|
||||
if (reccollana) product.productInfo.idCollana = reccollana._id;
|
||||
}
|
||||
|
||||
if (!product.hasOwnProperty('active')) {
|
||||
@@ -2497,7 +2494,7 @@ async function importaCatalogo(data) {
|
||||
console.log(e.message);
|
||||
return res.status(400).send(e);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
router.post('/exec', authenticate, async (req, res) => {
|
||||
try {
|
||||
|
||||
@@ -10,7 +10,6 @@ const XLSX = require('xlsx');
|
||||
const upload = multer({ dest: 'uploads/' });
|
||||
|
||||
const Product = require('../models/product');
|
||||
const ProductInfo = require('../models/productInfo');
|
||||
const Author = require('../models/author');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
@@ -370,8 +369,8 @@ router.post('/search-books', authenticate, async (req, res) => {
|
||||
// Cerca il primo record che corrisponde per ISBN o titolo
|
||||
if (true) {
|
||||
if (!trovatoISBN) {
|
||||
let productInfoarrISBN = await ProductInfo.find({
|
||||
code: field.toUpperCase(),
|
||||
let productInfoarrISBN = await Product.find({
|
||||
'productInfo.code': field.toUpperCase(),
|
||||
$or: [{ deleted: false }, { deleted: { $exists: false } }],
|
||||
}).exec();
|
||||
|
||||
@@ -384,33 +383,33 @@ router.post('/search-books', authenticate, async (req, res) => {
|
||||
}
|
||||
if (!trovatoISBN && !trovato) {
|
||||
// Prima cerca se è esattamente cosi
|
||||
let productInfoarrTitle = await ProductInfo.find({
|
||||
let productarrTitle = await Product.find({
|
||||
$or: [{ deleted: false }, { deleted: { $exists: false } }],
|
||||
name: field,
|
||||
'productInfo.name': field,
|
||||
}).exec();
|
||||
if (productInfoarrTitle.length === 1) {
|
||||
productInfo = productInfoarrTitle[0];
|
||||
if (productarrTitle.length === 1) {
|
||||
productInfo = productarrTitle[0];
|
||||
trovato = true;
|
||||
} else {
|
||||
if (productInfoarrTitle.length > 1) {
|
||||
if (productarrTitle.length > 1) {
|
||||
// Prendi l'Ultimo !
|
||||
productInfo = productInfoarrTitle[productInfoarrTitle.length - 1];
|
||||
productInfo = productarrTitle[productarrTitle.length - 1];
|
||||
trovato = true;
|
||||
}
|
||||
}
|
||||
if (!trovato) {
|
||||
// Altrimenti per Titolo
|
||||
productInfoarrTitle = await ProductInfo.find({
|
||||
productarrTitle = await Product.find({
|
||||
$or: [{ deleted: false }, { deleted: { $exists: false } }],
|
||||
name: new RegExp(`.*${escapeRegExp(tools.removeAccents(field.toUpperCase()))}.*`, 'i'),
|
||||
'productInfo.name': new RegExp(`.*${escapeRegExp(tools.removeAccents(field.toUpperCase()))}.*`, 'i'),
|
||||
}).exec();
|
||||
if (productInfoarrTitle.length === 1) {
|
||||
productInfo = productInfoarrTitle[0];
|
||||
if (productarrTitle.length === 1) {
|
||||
productInfo = productarrTitle[0];
|
||||
trovato = true;
|
||||
} else {
|
||||
if (productInfoarrTitle.length > 1) {
|
||||
if (productarrTitle.length > 1) {
|
||||
// Prendi l'Ultimo !
|
||||
productInfo = productInfoarrTitle[productInfoarrTitle.length - 1];
|
||||
productInfo = productarrTitle[productarrTitle.length - 1];
|
||||
trovato = true;
|
||||
}
|
||||
}
|
||||
@@ -426,15 +425,14 @@ router.post('/search-books', authenticate, async (req, res) => {
|
||||
if (product) {
|
||||
const existingResult = results.find((r) => r._id.toString() === product._id.toString());
|
||||
if (!existingResult) {
|
||||
let titolo = productInfo.name;
|
||||
let titolo = product.productInfo.name;
|
||||
results.push({
|
||||
...product,
|
||||
productInfo,
|
||||
_id: product._id,
|
||||
title: titolo,
|
||||
isbn: product.isbn,
|
||||
authors: await Promise.all(
|
||||
productInfo.idAuthors.map(async (authorId) => {
|
||||
product.productInfo.idAuthors.map(async (authorId) => {
|
||||
const author = await Author.findById(authorId).exec();
|
||||
return author ? `${author.name} ${author.surname}`.trim() : '';
|
||||
})
|
||||
|
||||
@@ -526,19 +526,6 @@ router.post('/:userId/gestord', authenticate, async function (req, res, next) {
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'productinfos',
|
||||
localField: 'product.idProductInfo',
|
||||
foreignField: '_id',
|
||||
as: 'productInfo',
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$productInfo',
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'gasordines',
|
||||
@@ -588,13 +575,13 @@ router.post('/:userId/gestord', authenticate, async function (req, res, next) {
|
||||
$group: {
|
||||
_id: '$product._id',
|
||||
name: {
|
||||
$first: '$productInfo.name',
|
||||
$first: '$product.productInfo.name',
|
||||
},
|
||||
weight: {
|
||||
$first: '$productInfo.weight',
|
||||
$first: '$product.productInfo.weight',
|
||||
},
|
||||
unit: {
|
||||
$first: '$productInfo.unit',
|
||||
$first: '$product.productInfo.unit',
|
||||
},
|
||||
price_acquistato: {
|
||||
$first: '$product.price_acquistato',
|
||||
|
||||
@@ -84,7 +84,6 @@ const Collana = require('../models/collana');
|
||||
const { Catalog } = require('../models/catalog');
|
||||
const { RaccoltaCataloghi } = require('../models/raccoltacataloghi');
|
||||
const Publisher = require('../models/publisher');
|
||||
const ProductInfo = require('../models/productInfo');
|
||||
const Scontistica = require('../models/scontistica');
|
||||
const Department = require('../models/department');
|
||||
const { Category } = require('../models/category');
|
||||
@@ -1953,7 +1952,6 @@ async function load(req, res, version = '0') {
|
||||
? Product.findAllIdApp(idapp, undefined, undefined, req.user ? User.isManager(req.user.perm) : false)
|
||||
: Promise.resolve([]),*/
|
||||
products: Promise.resolve([]),
|
||||
// productInfos: version >= 91 ? ProductInfo.findAllIdApp(idapp) : Promise.resolve([]),
|
||||
productInfos: Promise.resolve([]),
|
||||
catprods: version >= 91 ? Product.getArrCatProds(idapp, shared_consts.PROD.BOTTEGA) : Promise.resolve([]),
|
||||
subcatprods: version >= 91 ? SubCatProd.findAllIdApp(idapp) : Promise.resolve([]),
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user