aggiornamento scontistica, corretto errori

This commit is contained in:
Surya Paolo
2023-12-16 18:40:17 +01:00
parent ab3a31d4fb
commit 4290895a97
11 changed files with 132 additions and 44 deletions

View File

@@ -4,7 +4,10 @@ module.exports = {
name: "FreePlanetServerSide",
script: "./src/server/server.js",
ignore_watch : ["node_modules"],
interpreter: "/root/.nvm/versions/node/v16.19.0/bin/node",
watch: false,
//autorestart: true,
instances: 1,
env: {
"PORT": 3000,
"NODE_ENV": "development",
@@ -17,9 +20,9 @@ module.exports = {
"PORT": 3000,
"NODE_ENV": "production",
},
log_file: "combined.outerr.log",
error_file: "err.log",
out_file: "out.log",
log_file: "logs/combined.outerr.log",
error_file: "logs/err.log",
out_file: "logs/out.log",
merge_logs: true,
log_date_format: "YYYY-MM-DD HH:mm:ss.SSSS Z"
}

View File

@@ -14,7 +14,7 @@
"test-watch": "nodemon --exec 'npm test'"
},
"engines": {
"node": "^18.17.0"
"node": "^18.19.0"
},
"author": "Paolo Arena",
"license": "MIT",

View File

@@ -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' },
idScontisticas: [{ type: Schema.Types.ObjectId, ref: 'Scontistica' }],
idProvider: { type: Schema.Types.ObjectId, ref: 'Provider' },
price: {
type: Number
@@ -167,13 +168,23 @@ module.exports.findAllIdApp = async function (idapp) {
},
{
$unwind: {
path: '$scontistica',
path: '$product',
preserveNullAndEmptyArrays: true,
},
},
{ $unwind: '$product' },
{ $unwind: '$producer' },
{ $unwind: '$provider' },
{
$unwind: {
path: '$producer',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$provider',
preserveNullAndEmptyArrays: true,
},
}
];
return await Order.aggregate(query)
@@ -263,19 +274,33 @@ module.exports.getTotalOrderById = async function (id) {
from: 'scontisticas',
localField: 'idScontisticas',
foreignField: '_id',
as: 'scontistica'
as: 'scontisticas'
}
},
{
$unwind: {
path: '$scontistica',
path: '$product',
preserveNullAndEmptyArrays: true,
},
},
{ $unwind: '$product' },
{ $unwind: '$producer' },
{ $unwind: '$storehouse' },
{ $unwind: '$provider' },
{
$unwind: {
path: '$producer',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$storehouse',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$provider',
preserveNullAndEmptyArrays: true,
},
}
];
return await Order.aggregate(query);

View File

@@ -223,6 +223,13 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
model: 'Storehouse'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idScontisticas',
model: 'Scontistica'
},
})
.populate({
path: 'userId',
model: 'User',
@@ -237,13 +244,15 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
if (item.order) {
try {
item.order.product = item.order.idProduct;
item.order.idProduct = item.order.product._id;
item.order.idProduct = item.order.product ? item.order.product._id : '';
item.order.producer = item.order.idProducer;
item.order.idProducer = item.order.producer._id;
item.order.idProducer = item.order.producer ? item.order.producer._id : '';
item.order.storehouse = item.order.idStorehouse;
item.order.idStorehouse = item.order.storehouse._id;
item.order.idStorehouse = item.order.storehouse ? item.order.storehouse._id : '';
item.order.provider = item.order.idProvider;
item.order.idProvider = item.order.provider._id;
item.order.idProvider = item.order.provider ? item.order.provider._id : '';
item.order.scontisticas = item.order.scontisticas;
item.order.idScontisticas = item.order.idScontisticas ? item.order.idScontisticas._id : '';
} catch (e) {
console.error('Err: ', e);
}

View File

@@ -206,6 +206,9 @@ module.exports.findAllIdApp = async function (idapp, code, id) {
query.push(myqueryadd);
}
// DA TOGLIEREE
// myfind = { ...myfind, code: '4012824406094' };
// return await Product.find(myfind);
query.push(
@@ -243,15 +246,9 @@ module.exports.findAllIdApp = async function (idapp, code, id) {
from: 'scontisticas',
localField: 'idScontisticas',
foreignField: '_id',
as: 'scontistica'
as: 'scontisticas'
}
},
{
$unwind: {
path: '$scontistica',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'storehouses',
@@ -372,9 +369,9 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
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
})
recproducer = new Producer({ idapp, name: prod.producer_name });
ris = await recproducer.save();
recproducer = await Producer.findOne({ idapp, name: prod.producer_name }).lean();
}
if (recproducer) {
@@ -391,9 +388,9 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
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
})
recstorehouse = new Storehouse({ idapp, name: prod.magazzino_name });
ris = await recstorehouse.save();
recstorehouse = await Storehouse.findOne({ idapp, name: prod.magazzino_name }).lean();
}
if (recstorehouse) {
@@ -409,10 +406,10 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
// Cerca il produttore
let recprovider = await Provider.findOne({ idapp, name: prod.provider_name }).lean();
if (!recprovider) {
recprovider = new Provider({ idapp, name: prod.provider_name });
// Non esiste questo produttore, quindi lo creo !
recprovider = await Provider.create({ idapp, name: prod.provider_name }, (err, recordCreato) => {
return recordCreato
})
ris = await recprovider.save();
recprovider = await Provider.findOne({ idapp, name: prod.provider_name }).lean();
}
if (recprovider) {

View File

@@ -40,7 +40,10 @@ const scontisticaSchema = new Schema({
var Scontistica = module.exports = mongoose.model('Scontistica', scontisticaSchema);
module.exports.getFieldsForSearch = function () {
return [{ field: 'name', type: tools.FieldType.string }]
return [
{ field: 'code', type: tools.FieldType.string },
{ field: 'description', type: tools.FieldType.string }
]
};
module.exports.executeQueryTable = function (idapp, params) {

View File

@@ -338,7 +338,8 @@ module.exports.createFirstUserAdmin = async function () {
const numusers = await User.countDocuments({ idapp: mysite.idapp });
if (numusers === 0) {
// Non esistono utenti, quindi creo quello di Admin
const utenteadmin = await User.findOne({ idapp: '13', username: telegrambot.ADMIN_USER_SERVER }).lean()
const utenteadmin = { idapp: '13', username: telegrambot.ADMIN_USER_SERVER };
const newuser = new User(utenteadmin);
newuser._id = new ObjectID();

View File

@@ -5378,6 +5378,7 @@ UserSchema.statics.addNewSite = async function (idappPass, body) {
// cerca un IdApp Libero
let idapp = await Site.generateNewSite_IdApp(idappPass, body, true);
if (idapp) {
let arrSite = await Site.find({ idapp }).lean();
let numutenti = 0;
@@ -5385,7 +5386,7 @@ UserSchema.statics.addNewSite = async function (idappPass, body) {
numutenti = await User.countDocuments({ idapp });
};
if (arrSite && arrSite.length === 1 && numutenti === 0) {
if (arrSite && arrSite.length === 1 && numutenti < 2) {
// Nessun Sito Installato e Nessun Utente installato !
let myuser = new User();
myuser._id = new ObjectID();

View File

@@ -99,9 +99,59 @@ class Cart {
order = this.items[rec];
}
this.totalQty += order.quantity;
this.totalPrice += order.price * order.quantity;
}
// Calcolo Sconto
let sconti_da_applicare = [];
let mypricecalc = 0;
if (order.scontisticas) {
let qtadascontare = order.quantity
let qtanonscontata = 0
while (qtadascontare > 0) {
let scontoapplicato = null
for (const sconto of order.scontisticas.filter((rec) => !rec.cumulativo)) {
if (qtadascontare >= sconto.qta) {
scontoapplicato = sconto
scontoapplicato.qtadascontare = sconto.qta
}
}
if (scontoapplicato && scontoapplicato.qtadascontare > 0) {
sconti_da_applicare.push(scontoapplicato)
qtadascontare -= scontoapplicato.qtadascontare
} else {
qtanonscontata = qtadascontare
qtadascontare = 0
}
}
/*for (const sconto of order.scontisticas.filter((rec) => rec.cumulativo)) {
if ((sconto.qta % order.quantity) === 0) {
sconti_da_applicare.push(sconto)
}
}*/
mypricecalc = order.price * order.quantity;
if (sconti_da_applicare.length > 0) {
mypricecalc = 0
for (const sconto of sconti_da_applicare) {
if (sconto.perc_sconto > 0) {
mypricecalc += (sconto.qtadascontare * order.price) * (1 - (sconto.perc_sconto / 100))
} else {
mypricecalc += sconto.price
}
}
}
if (qtanonscontata > 0) {
mypricecalc += order.price * qtanonscontata;
}
} else {
mypricecalc = order.price * order.quantity;
}
this.totalPrice += mypricecalc;
}
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
} catch (e) {
console.error('Err: ', e);

View File

@@ -1434,7 +1434,7 @@ function load(req, res, version) {
let workers = User.getusersWorkersList(idapp);
let storehouses = Storehouse.findAllIdApp(idapp);
let providers = Provider.findAllIdApp(idapp);
let scontistica = Scontistica.findAllIdApp(idapp);
let scontisticas = Scontistica.findAllIdApp(idapp);
let departments = Department.findAllIdApp(idapp);
let categories = Category.findAllIdApp(idapp);
@@ -1522,7 +1522,7 @@ function load(req, res, version) {
myelems, // 38
categories, // 39
providers,
scontistica,
scontisticas,
]).then((arrdata) => {
// console.table(arrdata);
let myuser = req.user;
@@ -1606,7 +1606,7 @@ function load(req, res, version) {
myelems: arrdata[38],
categories: arrdata[39],
providers: arrdata[40],
scontistica: arrdata[41],
scontisticas: arrdata[41],
});
const prova = 1;

View File

@@ -566,8 +566,7 @@ router.post('/login', (req, res) => {
// tools.mylog("CREDENZIALI ! ");
if (!user) {
await tools.snooze(3000);
const msg = 'Tentativo di Login ERRATO [' + body.username + ' , ' +
body.password + ']\n' + '[IP: ' + tools.getiPAddressUser(req) +
const msg = 'Tentativo di Login ERRATO [' + body.username + ' , ' + ']\n' + '[IP: ' + tools.getiPAddressUser(req) +
']';
tools.mylogshow(msg);
await telegrambot.sendMsgTelegramToTheAdmin(user.idapp, msg, true);