import dati prodotti + fornitore + produttore
This commit is contained in:
@@ -40,11 +40,11 @@ html
|
||||
- var index = 0
|
||||
|
||||
each product in orders.items
|
||||
- var descr = product.order.product.name
|
||||
- var img = product.order.product.img
|
||||
- var price = product.order.price
|
||||
- var after_price = product.order.after_price
|
||||
- var qty = product.order.quantity
|
||||
- var descr = product.name
|
||||
- var img = product.img
|
||||
- var price = product.price
|
||||
- var after_price = product.after_price
|
||||
- var qty = product.quantity
|
||||
- index = index + 1
|
||||
|
||||
table(cellpadding="0", cellspacing="0", width="100%", summary="", border="0", align="center")
|
||||
|
||||
@@ -25,6 +25,7 @@ const orderSchema = new Schema({
|
||||
idProduct: { type: Schema.Types.ObjectId, ref: 'Product' },
|
||||
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
|
||||
idStorehouse: { type: Schema.Types.ObjectId, ref: 'StoreHouse' },
|
||||
idProvider: { type: Schema.Types.ObjectId, ref: 'Provider' },
|
||||
price: {
|
||||
type: Number
|
||||
},
|
||||
@@ -148,8 +149,17 @@ module.exports.findAllIdApp = async function (idapp) {
|
||||
as: 'producer'
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'providers',
|
||||
localField: 'idProvider',
|
||||
foreignField: '_id',
|
||||
as: 'provider'
|
||||
}
|
||||
},
|
||||
{ $unwind: '$product' },
|
||||
{ $unwind: '$producer' },
|
||||
{ $unwind: '$provider' },
|
||||
];
|
||||
|
||||
return await Order.aggregate(query)
|
||||
@@ -223,9 +233,18 @@ module.exports.getTotalOrderById = async function (id) {
|
||||
as: 'storehouse'
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'providers',
|
||||
localField: 'idProvider',
|
||||
foreignField: '_id',
|
||||
as: 'provider'
|
||||
}
|
||||
},
|
||||
{ $unwind: '$product' },
|
||||
{ $unwind: '$producer' },
|
||||
{ $unwind: '$storehouse' },
|
||||
{ $unwind: '$provider' },
|
||||
];
|
||||
|
||||
return await Order.aggregate(query);
|
||||
|
||||
@@ -7,6 +7,7 @@ const Order = require('../models/order');
|
||||
var { User } = require('../models/user');
|
||||
|
||||
const Storehouse = require('../models/storehouse');
|
||||
const Provider = require('../models/provider');
|
||||
const Product = require('../models/product');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
@@ -189,12 +190,13 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
|
||||
path: 'idProducer',
|
||||
model: 'Producer'
|
||||
},
|
||||
/* transform: function(doc, populated) {
|
||||
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
||||
populated.producer = populated.idProducer;
|
||||
delete populated.idProducer;
|
||||
return populated;
|
||||
}, */
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
populate: {
|
||||
path: 'idProvider',
|
||||
model: 'Provider'
|
||||
},
|
||||
})
|
||||
.populate({
|
||||
path: 'items.order',
|
||||
@@ -219,6 +221,8 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
|
||||
delete item.order.idProducer;
|
||||
item.order.storehouse = item.order.idStorehouse;
|
||||
delete item.order.idStorehouse;
|
||||
item.order.provider = item.order.idProvider;
|
||||
delete item.order.idProvider;
|
||||
return item;
|
||||
});
|
||||
return order;
|
||||
|
||||
@@ -4,9 +4,13 @@ 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 shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
@@ -29,8 +33,10 @@ const productSchema = new Schema({
|
||||
idStorehouses: [
|
||||
{ type: Schema.Types.ObjectId, ref: 'Storehouse' }
|
||||
],
|
||||
idProvider: { type: Schema.Types.ObjectId, ref: 'Provider' },
|
||||
code: {
|
||||
type: String,
|
||||
unique: true,
|
||||
},
|
||||
codice_EAN: {
|
||||
type: String,
|
||||
@@ -139,7 +145,13 @@ const productSchema = new Schema({
|
||||
},
|
||||
producer_name: {
|
||||
type: String,
|
||||
}
|
||||
},
|
||||
provider_name: {
|
||||
type: String,
|
||||
},
|
||||
magazzino_name: {
|
||||
type: String,
|
||||
},
|
||||
});
|
||||
|
||||
var Product = module.exports = mongoose.model('Product', productSchema);
|
||||
@@ -175,6 +187,15 @@ module.exports.findAllIdApp = async function (idapp, code) {
|
||||
}
|
||||
},
|
||||
{ $unwind: '$producer' },
|
||||
{
|
||||
$lookup: {
|
||||
from: 'providers',
|
||||
localField: 'idProvider',
|
||||
foreignField: '_id',
|
||||
as: 'provider'
|
||||
}
|
||||
},
|
||||
{ $unwind: '$provider' },
|
||||
{
|
||||
$lookup: {
|
||||
from: 'storehouses',
|
||||
@@ -278,27 +299,93 @@ module.exports.createIndexes((err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
|
||||
module.exports.convertAfterImport = async function () {
|
||||
module.exports.convertAfterImport = async function (idapp, dataObjects) {
|
||||
|
||||
const arrprod = await Product.find({}).lean();
|
||||
const arrprod = await Product.find({idapp}).lean();
|
||||
for (const prod of arrprod) {
|
||||
let setta = false;
|
||||
|
||||
// Impostazioni Base:
|
||||
let objtoset = {
|
||||
idapp,
|
||||
img: 'upload/products/' + prod.code + '.jpg',
|
||||
}
|
||||
|
||||
if (prod.producer_name) {
|
||||
|
||||
// Cerca il produttore
|
||||
const recproducer = await Producer.findOne({ name: prod.producer_name }).lean();
|
||||
|
||||
const campodarimuovere = 'producer_name';
|
||||
let recproducer = await Producer.findOne({ idapp, name: prod.producer_name }).lean();
|
||||
if (!recproducer) {
|
||||
// Non esiste questo produttore, quindi lo creo !
|
||||
recproducer = await Producer.create({ idapp, name: prod.producer_name }, (err, recordCreato) => {
|
||||
return recordCreato
|
||||
})
|
||||
}
|
||||
|
||||
if (recproducer) {
|
||||
await Product.findOneAndUpdate({ _id: prod._id }, { $set: { idProducer: recproducer._id } })
|
||||
await Product.findOneAndUpdate({ _id: prod._id }, { $unset: { [campodarimuovere]: 1 } })
|
||||
objtoset = {
|
||||
...objtoset,
|
||||
idProducer: recproducer._id,
|
||||
}
|
||||
setta = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (prod.magazzino_name) {
|
||||
// Cerca il produttore
|
||||
let recstorehouse = await Storehouse.findOne({ idapp, name: prod.magazzino_name }).lean();
|
||||
if (!recstorehouse) {
|
||||
// Non esiste questo produttore, quindi lo creo !
|
||||
recstorehouse = await Storehouse.create({ idapp, name: prod.magazzino_name }, (err, recordCreato) => {
|
||||
return recordCreato
|
||||
})
|
||||
}
|
||||
|
||||
if (recstorehouse) {
|
||||
objtoset = {
|
||||
...objtoset,
|
||||
idStorehouses: [recstorehouse._id],
|
||||
}
|
||||
setta = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (prod.provider_name) {
|
||||
// Cerca il produttore
|
||||
let recprovider = await Provider.findOne({ idapp, name: prod.provider_name }).lean();
|
||||
if (!recprovider) {
|
||||
// Non esiste questo produttore, quindi lo creo !
|
||||
recprovider = await Provider.create({ idapp, name: prod.provider_name }, (err, recordCreato) => {
|
||||
return recordCreato
|
||||
})
|
||||
}
|
||||
|
||||
if (recprovider) {
|
||||
objtoset = {
|
||||
...objtoset,
|
||||
idProvider: recprovider._id,
|
||||
}
|
||||
setta = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Aggiorna il prezzo ?
|
||||
const aggiornaprezzo = false;
|
||||
if (aggiornaprezzo) {
|
||||
// cerca il prodotto
|
||||
const myprodinput = dataObjects.find((rec) => rec._id === prod._id)
|
||||
if (myprodinput) {
|
||||
objtoset = {
|
||||
...objtoset,
|
||||
price: myprodinput.price,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (setta) {
|
||||
await Product.findOneAndUpdate({ _id: prod._id }, { $set: objtoset })
|
||||
|
||||
// const Product = mongoose.model('Product', ProductSchema);
|
||||
|
||||
// module.exports = { Product };
|
||||
// PROVA
|
||||
// const campodarimuovere = 'producer_name';
|
||||
// await Product.findOneAndUpdate({ _id: prod._id }, { $unset: { [campodarimuovere]: 1 } })
|
||||
}
|
||||
}
|
||||
}
|
||||
64
src/server/models/provider.js
Executable file
64
src/server/models/provider.js
Executable file
@@ -0,0 +1,64 @@
|
||||
mongoose = require('mongoose').set('debug', false)
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true
|
||||
});
|
||||
|
||||
const providerSchema = new Schema({
|
||||
idapp: {
|
||||
type: String,
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
},
|
||||
referent: {
|
||||
type: String,
|
||||
},
|
||||
address: {
|
||||
type: String,
|
||||
},
|
||||
city: {
|
||||
type: String,
|
||||
},
|
||||
region: {
|
||||
type: String,
|
||||
},
|
||||
img: {
|
||||
type: String,
|
||||
},
|
||||
website: {
|
||||
type: String,
|
||||
},
|
||||
});
|
||||
|
||||
var Provider = module.exports = mongoose.model('Provider', providerSchema);
|
||||
|
||||
module.exports.getFieldsForSearch = function () {
|
||||
return [{ field: 'name', type: tools.FieldType.string }]
|
||||
};
|
||||
|
||||
module.exports.executeQueryTable = function (idapp, params) {
|
||||
params.fieldsearch = this.getFieldsForSearch();
|
||||
return tools.executeQueryTable(this, idapp, params);
|
||||
};
|
||||
|
||||
module.exports.findAllIdApp = async function (idapp) {
|
||||
const myfind = { idapp };
|
||||
|
||||
return await Provider.find(myfind);
|
||||
};
|
||||
|
||||
module.exports.createIndexes((err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
@@ -35,6 +35,7 @@ router.post('/updateval', authenticate, async (req, res) => {
|
||||
|
||||
router.post('/import', authenticate, async (req, res) => {
|
||||
const cmd = req.body.cmd;
|
||||
const idapp = req.body.idapp;
|
||||
const data = req.body.data;
|
||||
|
||||
try {
|
||||
@@ -49,13 +50,21 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
let dataObjects = JSON.parse(`[${data}]`);
|
||||
|
||||
return await Product.insertMany(dataObjects).then((ris) => {
|
||||
// L'opzione ordered: false gestisce gli errori senza interrompere l'inserimento
|
||||
return await Product.insertMany(dataObjects, { ordered: false })
|
||||
.then((ris) => {
|
||||
|
||||
Product.convertAfterImport().then((ris) => {
|
||||
Product.convertAfterImport(idapp, dataObjects).then((ris) => {
|
||||
return res.status(200).send(true);
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
.catch((errors) => {
|
||||
console.error(errors);
|
||||
Product.convertAfterImport(idapp).then((ris) => {
|
||||
return res.status(200).send(true);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -65,6 +65,7 @@ const Producer = require('../models/producer');
|
||||
const Cart = require('../models/cart');
|
||||
const OrdersCart = require('../models/orderscart');
|
||||
const Storehouse = require('../models/storehouse');
|
||||
const Provider = require('../models/provider');
|
||||
const Department = require('../models/department');
|
||||
const { Category } = require('../models/category');
|
||||
const Group = require('../models/group');
|
||||
@@ -1431,6 +1432,7 @@ function load(req, res, version) {
|
||||
let resps = User.getusersRespList(idapp);
|
||||
let workers = User.getusersWorkersList(idapp);
|
||||
let storehouses = Storehouse.findAllIdApp(idapp);
|
||||
let providers = Provider.findAllIdApp(idapp);
|
||||
let departments = Department.findAllIdApp(idapp);
|
||||
let categories = Category.findAllIdApp(idapp);
|
||||
|
||||
@@ -1517,6 +1519,7 @@ function load(req, res, version) {
|
||||
listcircuits, // 37
|
||||
myelems, // 38
|
||||
categories, // 39
|
||||
providers,
|
||||
]).then((arrdata) => {
|
||||
// console.table(arrdata);
|
||||
let myuser = req.user;
|
||||
@@ -1599,6 +1602,7 @@ function load(req, res, version) {
|
||||
listcircuits: arrdata[37],
|
||||
myelems: arrdata[38],
|
||||
categories: arrdata[39],
|
||||
providers: arrdata[40],
|
||||
});
|
||||
|
||||
const prova = 1;
|
||||
|
||||
@@ -60,6 +60,7 @@ const Producer = require('../models/producer');
|
||||
const Cart = require('../models/cart');
|
||||
const OrdersCart = require('../models/orderscart');
|
||||
const Storehouse = require('../models/storehouse');
|
||||
const Provider = require('../models/provider');
|
||||
const Department = require('../models/department');
|
||||
const { Category } = require('../models/category');
|
||||
const ShareWithUs = require('../models/sharewithus');
|
||||
@@ -104,6 +105,8 @@ module.exports = {
|
||||
mytable = Product;
|
||||
else if (tablename === 'storehouses')
|
||||
mytable = Storehouse;
|
||||
else if (tablename === 'providers')
|
||||
mytable = Provider;
|
||||
else if (tablename === 'departments')
|
||||
mytable = Department;
|
||||
else if (tablename === 'categorys')
|
||||
|
||||
Reference in New Issue
Block a user