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

@@ -22,6 +22,12 @@ const CatProdSchema = new Schema({
img: { img: {
type: String, type: String,
}, },
icon: {
type: String,
},
color: {
type: String,
},
}); });
CatProdSchema.statics.getAllCategories = function (callback) { CatProdSchema.statics.getAllCategories = function (callback) {

View File

@@ -66,6 +66,11 @@ const productSchema = new Schema({
default: 1, default: 1,
required: true, required: true,
}, },
minStepQty: { // step quantità acquistabile
type: Number,
default: 1,
required: true,
},
maxBookableQty: { // quantità massima Pre-ordinabile (singolarmente) maxBookableQty: { // quantità massima Pre-ordinabile (singolarmente)
type: Number, type: Number,
default: 0, default: 0,
@@ -113,6 +118,12 @@ const productSchema = new Schema({
cat_name: { cat_name: {
type: String, type: String,
}, },
sconto1: {
type: String,
},
sconto2: {
type: String,
},
}); });
var Product = module.exports = mongoose.model('Product', productSchema); var Product = module.exports = mongoose.model('Product', productSchema);
@@ -502,6 +513,7 @@ module.exports.singlerecconvert_AfterImport = async function (idapp, prod, isnuo
objtoset = { objtoset = {
idapp, idapp,
minBuyQty: 1, minBuyQty: 1,
minStepQty: 1,
maxBookableQty: 0, maxBookableQty: 0,
} }
} }
@@ -563,6 +575,46 @@ module.exports.singlerecconvert_AfterImport = async function (idapp, prod, isnuo
} }
} }
let arrsconti = []
if (prod.sconto1) {
// Cerca la scontistica
let recscontistica = await Scontistica.findOne({ idapp, code: prod.sconto1 }).lean();
if (!recscontistica) {
recscontistica = new Scontistica({ idapp, code: prod.sconto1 });
// Non esiste questa scontistica, quindi lo creo !
ris = await recscontistica.save();
recscontistica = await Scontistica.findOne({ idapp, code: prod.sconto1 }).lean();
}
if (recscontistica) {
arrsconti.push(recscontistica);
}
}
if (prod.sconto2) {
// Cerca la scontistica
let recscontistica = await Scontistica.findOne({ idapp, code: prod.sconto2 }).lean();
if (!recscontistica) {
recscontistica = new Scontistica({ idapp, code: prod.sconto2 });
// Non esiste questa scontistica, quindi lo creo !
ris = await recscontistica.save();
recscontistica = await Scontistica.findOne({ idapp, code: prod.sconto2 }).lean();
}
if (recscontistica) {
arrsconti.push(recscontistica);
}
}
if (arrsconti.length > 0) {
objtoset = {
...objtoset,
idScontisticas: arrsconti,
}
setta = true;
}
// Aggiorna il prezzo ? // Aggiorna il prezzo ?
const aggiornaprezzo = false; const aggiornaprezzo = false;
if (aggiornaprezzo) { if (aggiornaprezzo) {
@@ -584,6 +636,8 @@ module.exports.singlerecconvert_AfterImport = async function (idapp, prod, isnuo
producer_name: 1, producer_name: 1,
provider_name: 1, provider_name: 1,
magazzino_name: 1, magazzino_name: 1,
sconto1: 1,
sconto2: 1,
}; };
ris = await Product.updateOne({ _id: ObjectID(prod._id) }, { $unset: objDelete }) ris = await Product.updateOne({ _id: ObjectID(prod._id) }, { $unset: objDelete })

View File

@@ -287,6 +287,9 @@ const UserSchema = new mongoose.Schema({
manage_telegram: { manage_telegram: {
type: Boolean, type: Boolean,
}, },
admin_telegram: {
type: Boolean,
},
resplist: { resplist: {
type: Boolean, type: Boolean,
}, },
@@ -3606,7 +3609,7 @@ UserSchema.statics.getusersAdmin = async function (idapp) {
// Int32 mongodb 6.0 // Int32 mongodb 6.0
return await User.find({ idapp, 'profile.manage_telegram': true, perm: { $bitsAnySet: 0b001 } }, return await User.find({ idapp, 'profile.admin_telegram': true, perm: { $bitsAnySet: 0b001 } },
{ username: 1, 'profile.teleg_id': 1, perm: 1 }).then((arrrec) => { { username: 1, 'profile.teleg_id': 1, perm: 1 }).then((arrrec) => {
return (!!arrrec) ? arrrec : null; return (!!arrrec) ? arrrec : null;
}).catch((e) => { }).catch((e) => {
@@ -3707,7 +3710,7 @@ UserSchema.statics.isAdminByIdTeleg = async function (idapp, idtelegram) {
return await User.findOne({ return await User.findOne({
idapp, idapp,
username: 'paoloar77', username: 'paoloar77',
'profile.manage_telegram': true, 'profile.admin_telegram': true,
'profile.teleg_id': idtelegram, 'profile.teleg_id': idtelegram,
}, { 'profile.teleg_id': 1 }).then((rec) => { }, { 'profile.teleg_id': 1 }).then((rec) => {
return (!!rec && rec.profile.teleg_id === idtelegram); return (!!rec && rec.profile.teleg_id === idtelegram);
@@ -5407,6 +5410,7 @@ UserSchema.statics.addNewSite = async function (idappPass, body) {
myuser.profile.special_req = true; myuser.profile.special_req = true;
myuser.profile.nationality = 'IT'; myuser.profile.nationality = 'IT';
myuser.profile.manage_telegram = true; myuser.profile.manage_telegram = true;
myuser.profile.admin_telegram = true;
myuser.profile.teleg_id = MyTelegramBot.ADMIN_IDTELEGRAM_SERVER; myuser.profile.teleg_id = MyTelegramBot.ADMIN_IDTELEGRAM_SERVER;
myuser.profile.username_telegram = MyTelegramBot.ADMIN_USERNAME_TELEGRAM; myuser.profile.username_telegram = MyTelegramBot.ADMIN_USERNAME_TELEGRAM;
myuser.lasttimeonline = new Date(); myuser.lasttimeonline = new Date();

View File

@@ -69,23 +69,24 @@ class Cart {
async addqty(itemorder) { async addqty(itemorder) {
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id) const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
if (!!myitem) { if (!!myitem) {
let step = 1; let stepmin = myitem.order.product.minStepQty | 1;
let step = stepmin;
if (this.isAvailableByOrder(myitem.order)) { if (this.isAvailableByOrder(myitem.order)) {
if (myitem.order.quantity === 0) if (myitem.order.quantity === 0)
step = myitem.order.product.minBuyQty | 1 step = myitem.order.product.minBuyQty | stepmin
else if (myitem.order.quantity >= 10) else if (myitem.order.quantity >= 10)
step = 2 step = stepmin < 2 ? 2 : stepmin
else if (myitem.order.quantity >= 20) else if (myitem.order.quantity >= 20)
step = 5 step = stepmin < 5 ? 5 : stepmin
myitem.order.quantity += step; myitem.order.quantity += step;
} else { } else {
if (myitem.order.quantitypreordered === 0) if (myitem.order.quantitypreordered === 0)
step = myitem.order.product.minBuyQty | 1 step = myitem.order.product.minBuyQty | stepmin
else if (myitem.order.quantitypreordered >= 10) else if (myitem.order.quantitypreordered >= 10)
step = 2 step = stepmin < 2 ? 2 : stepmin
else if (myitem.order.quantitypreordered >= 20) else if (myitem.order.quantitypreordered >= 20)
step = 5 step = stepmin < 5 ? 5 : stepmin
myitem.order.quantitypreordered += step; myitem.order.quantitypreordered += step;
} }
@@ -99,11 +100,15 @@ class Cart {
} }
qtaNextSub(myorder, myproduct) { qtaNextSub(myorder, myproduct) {
let step = 1 let step = myproduct.minStepQty | 1
let minqta = myproduct.minBuyQty | 1 let minqta = myproduct.minBuyQty | 1
if (myproduct.quantityAvailable > 0) { if (myproduct.quantityAvailable > 0) {
if (myorder.quantity === minqta) if (myorder.quantity === minqta)
step = minqta step = minqta
else{
if ((myorder.quantity - step) < 0)
step = myorder.quantity - step
}
} else { } else {
if (myorder.quantitypreordered === minqta) if (myorder.quantitypreordered === minqta)
step = minqta step = minqta

View File

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

View File

@@ -87,6 +87,7 @@ const UserCost = {
FIELDS_UPDATE_TELEGRAM_BOT: [ FIELDS_UPDATE_TELEGRAM_BOT: [
'profile.teleg_id', 'profile.teleg_id',
'profile.manage_telegram', 'profile.manage_telegram',
'profile.admin_telegram',
'deleted', 'deleted',
'reported', 'reported',
], ],

View File

@@ -1977,21 +1977,21 @@ class Telegram {
await this.menumsgPaolo(msg); await this.menumsgPaolo(msg);
} else if (cmd1 === Menu.EXECSH) { } else if (cmd1 === Menu.EXECSH) {
if (rec.user && cmd2) { if (rec.user && cmd2) {
let isAdmin = rec.user ? rec.user.profile.manage_telegram : false; let isAdmin = rec.user ? rec.user.profile.admin_telegram : false;
if (isAdmin) { if (isAdmin) {
const ris = await tools.execScript(this.idapp, msg, cmd2, 'Eseguo lo script'); const ris = await tools.execScript(this.idapp, msg, cmd2, 'Eseguo lo script');
} }
} }
} else if (cmd1 === Menu.LOG_SRV) { } else if (cmd1 === Menu.LOG_SRV) {
if (rec.user) { if (rec.user) {
let isAdmin = rec.user ? rec.user.profile.manage_telegram : false; let isAdmin = rec.user ? rec.user.profile.admin_telegram : false;
if (isAdmin) { if (isAdmin) {
await this.menuLogSrv(rec, msg, cmd2); await this.menuLogSrv(rec, msg, cmd2);
} }
} }
} else if (cmd1 === Menu.REBOOT_SRV) { } else if (cmd1 === Menu.REBOOT_SRV) {
if (rec.user) { if (rec.user) {
let isAdmin = rec.user ? rec.user.profile.manage_telegram : false; let isAdmin = rec.user ? rec.user.profile.admin_telegram : false;
if (isAdmin) { if (isAdmin) {
await this.menuRebootSrv(rec, msg, cmd2); await this.menuRebootSrv(rec, msg, cmd2);
} }
@@ -3519,7 +3519,7 @@ class Telegram {
// Check if you are Admin // Check if you are Admin
const user = await User.UserByIdTelegram(idapp, id); const user = await User.UserByIdTelegram(idapp, id);
// let isAdmin = user ? user.profile.manage_telegram && user.username === 'paoloar77' : false; // let isAdmin = user ? user.profile.manage_telegram && user.username === 'paoloar77' : false;
let isAdmin = user ? user.profile.manage_telegram : false; let isAdmin = user ? user.profile.admin_telegram : false;
const isManager = user ? user.profile.manage_telegram : false; const isManager = user ? user.profile.manage_telegram : false;
const isVerified = user ? user.profile.teleg_id > 0 && user.verified_by_aportador : false; const isVerified = user ? user.profile.teleg_id > 0 && user.verified_by_aportador : false;