2022-09-14 11:32:04 +02:00
|
|
|
const mongoose = require('mongoose').set('debug', false)
|
2021-01-18 00:48:17 +01:00
|
|
|
const Schema = mongoose.Schema;
|
|
|
|
|
|
|
|
|
|
const shared_consts = require('../tools/shared_nodejs');
|
|
|
|
|
|
|
|
|
|
const Order = require('../models/order');
|
2021-04-30 01:31:12 +02:00
|
|
|
var { User } = require('../models/user');
|
2021-01-18 00:48:17 +01:00
|
|
|
|
2023-12-15 21:50:21 +01:00
|
|
|
const Storehouse = require('../models/storehouse');
|
|
|
|
|
const Provider = require('../models/provider');
|
2023-12-21 02:23:52 +01:00
|
|
|
const Gasordine = require('../models/gasordine');
|
2023-12-14 00:55:07 +01:00
|
|
|
const Product = require('../models/product');
|
2024-01-02 15:24:44 +01:00
|
|
|
const Cash = require('../models/cash');
|
2023-12-28 23:48:03 +01:00
|
|
|
const ProductInfo = require('../models/productInfo');
|
2023-12-14 00:55:07 +01:00
|
|
|
|
2021-02-03 01:33:30 +01:00
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
2021-01-18 00:48:17 +01:00
|
|
|
const { ObjectID } = require('mongodb');
|
|
|
|
|
|
|
|
|
|
const OrdersCartSchema = new Schema({
|
|
|
|
|
idapp: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
|
|
|
|
numorder: { type: Number },
|
2023-12-15 23:36:43 +01:00
|
|
|
numord_pers: { type: Number },
|
2021-01-18 00:48:17 +01:00
|
|
|
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
|
|
|
|
totalQty: { type: Number, default: 0 },
|
2023-12-20 21:52:17 +01:00
|
|
|
totalQtyPreordered: { type: Number, default: 0 },
|
2021-01-18 00:48:17 +01:00
|
|
|
totalPrice: { type: Number, default: 0 },
|
2021-02-03 01:33:30 +01:00
|
|
|
department: {
|
|
|
|
|
type: String, ref: 'Department'
|
|
|
|
|
},
|
2021-01-18 00:48:17 +01:00
|
|
|
items: [
|
|
|
|
|
{
|
|
|
|
|
order:
|
|
|
|
|
{ type: Schema.Types.ObjectId, ref: 'Order' }
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
status: {
|
2021-02-03 01:33:30 +01:00
|
|
|
type: Number,
|
|
|
|
|
Default: 0,
|
2021-01-18 00:48:17 +01:00
|
|
|
},
|
2023-12-30 21:33:59 +01:00
|
|
|
confermato: { // e quindi è stato tolto dal magazzino (aggiornando il campo stockBloccatiQty)
|
2023-12-13 19:17:53 +01:00
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
2023-12-30 21:33:59 +01:00
|
|
|
date_confermato: {
|
2023-12-13 19:17:53 +01:00
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
pagato: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
date_pagato: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
spedito: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
date_spedito: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
2023-12-29 21:17:17 +01:00
|
|
|
consegnato: { // e quindi è stato tolto dal magazzino (aggiornando il campo stockQty e stockBloccatiQty)
|
2023-12-13 19:17:53 +01:00
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
2023-12-29 21:17:17 +01:00
|
|
|
date_consegnato: {
|
2023-12-13 19:17:53 +01:00
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
consegnato: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
date_consegnato: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
2023-12-14 00:55:07 +01:00
|
|
|
ricevuto: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
date_ricevuto: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
2021-01-18 00:48:17 +01:00
|
|
|
note: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
|
|
|
|
modify_at: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
created_at: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
2023-12-09 19:38:23 +01:00
|
|
|
deleted: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
2021-01-18 00:48:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var OrdersCart = module.exports = mongoose.model('OrdersCart', OrdersCartSchema);
|
|
|
|
|
|
|
|
|
|
module.exports.findAllIdApp = async function (idapp, userId) {
|
2023-12-09 19:38:23 +01:00
|
|
|
const myfind = { idapp, userId, deleted: false };
|
2021-01-18 00:48:17 +01:00
|
|
|
|
2022-09-11 11:45:33 +02:00
|
|
|
return await await OrdersCart.find(myfind);
|
2021-02-03 01:33:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports.executeQueryTable = function (idapp, params) {
|
|
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
|
|
|
|
return tools.executeQueryTable(this, idapp, params);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports.getFieldsForSearch = function () {
|
2021-03-30 02:23:38 +02:00
|
|
|
return [{ field: 'note', type: tools.FieldType.string }]
|
2021-01-18 00:48:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2023-12-15 23:36:43 +01:00
|
|
|
module.exports.getLastNumOrder = async function (idapp) {
|
|
|
|
|
let query = { idapp, deleted: false }
|
|
|
|
|
let numorder = 100;
|
|
|
|
|
let numorderrec = await OrdersCart.find(query).sort({ numorder: -1 }).limit(1);
|
|
|
|
|
|
|
|
|
|
if (numorderrec && numorderrec.length > 0)
|
|
|
|
|
numorder = numorderrec[0].numorder;
|
|
|
|
|
else
|
|
|
|
|
numorder = 100;
|
|
|
|
|
|
|
|
|
|
return numorder;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports.getLastNumOrdPers = async function (uid, idapp) {
|
2023-12-09 19:38:23 +01:00
|
|
|
let query = { userId: uid, idapp, deleted: false }
|
2021-01-18 00:48:17 +01:00
|
|
|
let numorder = 1;
|
2023-12-21 01:34:33 +01:00
|
|
|
let numorderrec = await OrdersCart.find(query).sort({ numord_pers: -1 }).limit(1);
|
2023-12-14 00:55:07 +01:00
|
|
|
|
2023-12-13 19:17:53 +01:00
|
|
|
if (numorderrec && numorderrec.length > 0)
|
2023-12-21 01:34:33 +01:00
|
|
|
numorder = numorderrec[0].numord_pers;
|
2023-12-13 19:17:53 +01:00
|
|
|
else
|
|
|
|
|
numorder = 0;
|
2021-01-18 00:48:17 +01:00
|
|
|
|
|
|
|
|
return numorder;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-30 01:31:12 +02:00
|
|
|
module.exports.getStatusCartByUserId = async function (uid, idapp, numorder) {
|
2021-02-03 01:33:30 +01:00
|
|
|
let query = { userId: uid, idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
2021-03-30 02:23:38 +02:00
|
|
|
let myorderscart = null;
|
|
|
|
|
if (numorder > 0) {
|
|
|
|
|
query = { userId: uid, idapp, numorder, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 01:31:12 +02:00
|
|
|
myorderscart = await OrdersCart.findOne(query);
|
|
|
|
|
|
|
|
|
|
if (!!myorderscart)
|
|
|
|
|
return myorderscart.status;
|
|
|
|
|
else
|
|
|
|
|
return shared_consts.OrderStatus.NONE
|
|
|
|
|
|
2023-12-13 19:17:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.getRecCartByUserId = async function (uid, idapp, numorder) {
|
2023-12-15 21:50:21 +01:00
|
|
|
let query = { userId: uid, idapp, status: { $lt: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
2023-12-13 19:17:53 +01:00
|
|
|
let myorderscart = null;
|
|
|
|
|
if (numorder > 0) {
|
2023-12-15 21:50:21 +01:00
|
|
|
query = { userId: uid, idapp, numorder, status: { $lt: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
2023-12-13 19:17:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
myorderscart = await OrdersCart.findOne(query).lean();
|
|
|
|
|
|
|
|
|
|
return myorderscart
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.getOrdersCartById = async function (id) {
|
|
|
|
|
|
2023-12-17 19:19:04 +01:00
|
|
|
let query = { _id: ObjectID(id) };
|
|
|
|
|
|
|
|
|
|
const arrris = await OrdersCart.getOrdersCartByQuery(query);
|
|
|
|
|
return arrris && arrris.length > 0 ? arrris[0] : null;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-27 02:58:15 +01:00
|
|
|
|
|
|
|
|
module.exports.getOrdersCartByQuery = async function (query) {
|
2023-12-17 19:19:04 +01:00
|
|
|
|
|
|
|
|
let myorderscart = await OrdersCart.find(query)
|
|
|
|
|
.populate('items.order')
|
|
|
|
|
.populate({
|
|
|
|
|
path: 'items.order',
|
|
|
|
|
populate: {
|
|
|
|
|
path: 'idProduct',
|
2023-12-28 21:00:02 +01:00
|
|
|
model: 'Product',
|
2023-12-27 02:58:15 +01:00
|
|
|
populate: {
|
|
|
|
|
path: 'idProductInfo',
|
|
|
|
|
model: 'ProductInfo'
|
2023-12-28 21:00:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-21 01:34:33 +01:00
|
|
|
})
|
|
|
|
|
.populate({
|
|
|
|
|
path: 'items.order',
|
|
|
|
|
populate: {
|
|
|
|
|
path: 'idProducer',
|
|
|
|
|
model: 'Producer'
|
2023-12-28 21:00:02 +01:00
|
|
|
}
|
2023-12-21 01:34:33 +01:00
|
|
|
})
|
|
|
|
|
.populate({
|
|
|
|
|
path: 'items.order',
|
|
|
|
|
populate: {
|
|
|
|
|
path: 'idProvider',
|
|
|
|
|
model: 'Provider'
|
2023-12-28 21:00:02 +01:00
|
|
|
}
|
2023-12-21 01:34:33 +01:00
|
|
|
})
|
2023-12-21 02:23:52 +01:00
|
|
|
.populate({
|
|
|
|
|
path: 'items.order',
|
|
|
|
|
populate: {
|
|
|
|
|
path: 'idGasordine',
|
|
|
|
|
model: 'Gasordine'
|
2023-12-28 21:00:02 +01:00
|
|
|
}
|
2023-12-21 02:23:52 +01:00
|
|
|
})
|
2023-12-21 01:34:33 +01:00
|
|
|
.populate({
|
|
|
|
|
path: 'items.order',
|
|
|
|
|
populate: {
|
|
|
|
|
path: 'idStorehouse',
|
|
|
|
|
model: 'Storehouse'
|
2023-12-28 21:00:02 +01:00
|
|
|
}
|
2023-12-21 01:34:33 +01:00
|
|
|
})
|
|
|
|
|
.populate({
|
|
|
|
|
path: 'items.order',
|
|
|
|
|
populate: {
|
|
|
|
|
path: 'idScontisticas',
|
|
|
|
|
model: 'Scontistica'
|
2023-12-28 21:00:02 +01:00
|
|
|
}
|
2023-12-21 01:34:33 +01:00
|
|
|
})
|
|
|
|
|
.populate({
|
|
|
|
|
path: 'userId',
|
|
|
|
|
model: 'User',
|
2023-12-28 21:00:02 +01:00
|
|
|
select: '_id name surname username profile'
|
2023-12-21 01:34:33 +01:00
|
|
|
})
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
myorderscart = myorderscart.map(order => {
|
2023-12-28 21:00:02 +01:00
|
|
|
order.user = order.userId;
|
|
|
|
|
order.userId = order.user._id;
|
2023-12-21 01:34:33 +01:00
|
|
|
order.items = order.items.map(item => {
|
|
|
|
|
if (item.order) {
|
|
|
|
|
try {
|
2023-12-28 21:00:02 +01:00
|
|
|
if (item.order.idProduct) {
|
2023-12-29 21:17:17 +01:00
|
|
|
item.order.idProduct.productInfo = item.order.idProduct.productInfo ? item.order.idProduct.productInfo : { ...item.order.idProduct.idProductInfo };
|
2023-12-28 21:00:02 +01:00
|
|
|
item.order.idProduct.idProductInfo = item.order.idProduct.productInfo ? item.order.idProduct.productInfo._id : '';
|
2023-12-27 02:58:15 +01:00
|
|
|
}
|
2023-12-29 21:17:17 +01:00
|
|
|
item.order.product = { ...item.order.idProduct };
|
2023-12-28 21:00:02 +01:00
|
|
|
item.order.idProduct = item.order.product ? item.order.product._id : '';
|
2023-12-21 01:34:33 +01:00
|
|
|
item.order.producer = item.order.idProducer;
|
|
|
|
|
item.order.idProducer = item.order.producer ? item.order.producer._id : '';
|
|
|
|
|
item.order.storehouse = item.order.idStorehouse;
|
|
|
|
|
item.order.idStorehouse = item.order.storehouse ? item.order.storehouse._id : '';
|
|
|
|
|
item.order.provider = item.order.idProvider;
|
|
|
|
|
item.order.idProvider = item.order.provider ? item.order.provider._id : '';
|
2023-12-21 02:23:52 +01:00
|
|
|
item.order.gasordine = item.order.idGasordine;
|
|
|
|
|
item.order.idGasordine = item.order.gasordine ? item.order.gasordine._id : '';
|
2023-12-21 01:34:33 +01:00
|
|
|
item.order.scontisticas = item.order.scontisticas;
|
|
|
|
|
item.order.idScontisticas = item.order.idScontisticas ? item.order.idScontisticas._id : '';
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Err: ', e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
return order;
|
|
|
|
|
});
|
|
|
|
|
|
2023-12-28 21:00:02 +01:00
|
|
|
|
2023-12-21 01:34:33 +01:00
|
|
|
return myorderscart;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 21:52:17 +01:00
|
|
|
module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder, filterStatus) {
|
2021-04-30 01:31:12 +02:00
|
|
|
|
2023-12-15 21:50:21 +01:00
|
|
|
try {
|
2023-12-20 21:52:17 +01:00
|
|
|
let query = { idapp, deleted: false }
|
2023-12-15 21:50:21 +01:00
|
|
|
let myorderscart = null;
|
|
|
|
|
if (numorder > 0) {
|
|
|
|
|
query.numorder = numorder;
|
|
|
|
|
}
|
2021-04-30 01:31:12 +02:00
|
|
|
|
2023-12-15 21:50:21 +01:00
|
|
|
if (uid !== 'ALL') {
|
|
|
|
|
query.userId = uid;
|
|
|
|
|
}
|
2021-04-30 01:31:12 +02:00
|
|
|
|
2023-12-20 21:52:17 +01:00
|
|
|
if (filterStatus) {
|
|
|
|
|
query.status = { $gte: shared_consts.OrderStatus.CHECKOUT_SENT };
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-17 19:19:04 +01:00
|
|
|
myorderscart = await OrdersCart.getOrdersCartByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
2023-12-15 21:50:21 +01:00
|
|
|
/*transform: function(doc, populated) {
|
|
|
|
|
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
|
|
|
|
populated.product = populated.idProduct;
|
|
|
|
|
delete populated.idProduct;
|
|
|
|
|
return populated;
|
|
|
|
|
},*/
|
|
|
|
|
|
2021-01-18 00:48:17 +01:00
|
|
|
|
2023-12-15 21:50:21 +01:00
|
|
|
return myorderscart
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Err:', e);
|
2021-04-30 01:31:12 +02:00
|
|
|
}
|
|
|
|
|
// return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 01:33:30 +01:00
|
|
|
|
2021-01-18 00:48:17 +01:00
|
|
|
module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
|
2023-12-09 19:38:23 +01:00
|
|
|
let query = {
|
|
|
|
|
id,
|
|
|
|
|
deleted: false,
|
|
|
|
|
}
|
2021-01-18 00:48:17 +01:00
|
|
|
OrdersCart.find(query, function (err, c) {
|
|
|
|
|
if (err) throw err
|
|
|
|
|
|
|
|
|
|
//exist cart in databse
|
|
|
|
|
if (c.length > 0) {
|
2023-12-13 19:17:53 +01:00
|
|
|
return OrdersCart.findOneAndUpdate(
|
2021-01-18 00:48:17 +01:00
|
|
|
{ _id: id },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
items: newOrdersCart.items,
|
|
|
|
|
totalQty: newOrdersCart.totalQty,
|
2023-12-20 21:52:17 +01:00
|
|
|
totalQtyPreordered: newOrdersCart.totalQtyPreordered,
|
2021-01-18 00:48:17 +01:00
|
|
|
totalPrice: newOrdersCart.totalPrice,
|
|
|
|
|
userId: userId,
|
|
|
|
|
status: newOrdersCart.status,
|
|
|
|
|
numorder: newOrdersCart.numorder,
|
2023-12-15 23:36:43 +01:00
|
|
|
numord_pers: newOrdersCart.numord_pers,
|
2021-01-18 00:48:17 +01:00
|
|
|
note: newOrdersCart.note,
|
|
|
|
|
modify_at: new Date(),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ new: true },
|
|
|
|
|
callback
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
//no cart in database
|
2023-12-13 19:17:53 +01:00
|
|
|
return newOrdersCart.save(callback)
|
2021-01-18 00:48:17 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
module.exports.setFieldInOrdersById = async function (objtoset, myOrderCart) {
|
2023-12-13 19:17:53 +01:00
|
|
|
|
2023-12-15 21:50:21 +01:00
|
|
|
try {
|
|
|
|
|
let ris2 = null;
|
|
|
|
|
// Imposta su tutti i singoli prodotti ordinati (Order)
|
|
|
|
|
for (const recitem of myOrderCart.items) {
|
|
|
|
|
ris2 = await Order.findOneAndUpdate(
|
|
|
|
|
{ _id: recitem.order._id },
|
|
|
|
|
{
|
|
|
|
|
$set: objtoset
|
|
|
|
|
},
|
|
|
|
|
{ new: false }
|
|
|
|
|
)
|
|
|
|
|
// console.log('ris', ris2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ris = await OrdersCart.findOneAndUpdate(
|
|
|
|
|
{ _id: myOrderCart._id },
|
2023-12-14 15:20:21 +01:00
|
|
|
{
|
|
|
|
|
$set: objtoset
|
|
|
|
|
},
|
|
|
|
|
{ new: false }
|
|
|
|
|
)
|
2023-12-15 21:50:21 +01:00
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('Err', e);
|
2023-12-14 15:20:21 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-13 19:17:53 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-29 21:17:17 +01:00
|
|
|
module.exports.deleteRecsInOrdersById = async function (myOrderCart) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
let ris2 = null;
|
|
|
|
|
// Imposta su tutti i singoli prodotti ordinati (Order)
|
|
|
|
|
for (const recitem of myOrderCart.items) {
|
|
|
|
|
ris2 = await Order.findOneAndRemove({ _id: recitem.order._id })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ris = await OrdersCart.findOneAndRemove({ _id: myOrderCart._id })
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('Err', e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
module.exports.setConsegnatoById = async function (value, myOrderCart) {
|
|
|
|
|
|
|
|
|
|
let objtoset = {
|
|
|
|
|
consegnato: value,
|
|
|
|
|
date_consegnato: new Date(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await OrdersCart.setFieldInOrdersById(objtoset, myOrderCart);
|
2023-12-13 19:17:53 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
module.exports.setSpeditoById = async function (value, myOrderCart) {
|
|
|
|
|
|
|
|
|
|
let objtoset = {
|
|
|
|
|
spedito: value,
|
|
|
|
|
date_spedito: new Date(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await OrdersCart.setFieldInOrdersById(objtoset, myOrderCart);
|
2023-12-13 19:17:53 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
module.exports.setPagatoById = async function (value, myOrderCart) {
|
|
|
|
|
|
|
|
|
|
let objtoset = {
|
|
|
|
|
pagato: value,
|
|
|
|
|
date_pagato: new Date(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
|
objtoset.date_pagato = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await OrdersCart.setFieldInOrdersById(objtoset, myOrderCart);
|
2023-12-13 19:17:53 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 21:17:17 +01:00
|
|
|
module.exports.setConsegnatoById = async function (value, myOrderCart) {
|
2023-12-14 15:20:21 +01:00
|
|
|
|
|
|
|
|
let objtoset = {
|
2023-12-29 21:17:17 +01:00
|
|
|
consegnato: value,
|
|
|
|
|
date_consegnato: new Date(),
|
2023-12-14 15:20:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await OrdersCart.setFieldInOrdersById(objtoset, myOrderCart);
|
2023-12-13 19:17:53 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-30 21:33:59 +01:00
|
|
|
module.exports.setConfermatoById = async function (value, myOrderCart) {
|
2023-12-14 15:20:21 +01:00
|
|
|
|
|
|
|
|
let objtoset = {
|
2023-12-30 21:33:59 +01:00
|
|
|
confermato: value,
|
|
|
|
|
date_confermato: new Date(),
|
2023-12-14 15:20:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await OrdersCart.setFieldInOrdersById(objtoset, myOrderCart);
|
2023-12-14 00:55:07 +01:00
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.setRicevutoById = async function (value, myOrderCart) {
|
|
|
|
|
|
|
|
|
|
let objtoset = {
|
|
|
|
|
ricevuto: value,
|
|
|
|
|
date_ricevuto: new Date(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await OrdersCart.setFieldInOrdersById(objtoset, myOrderCart);
|
2023-12-14 00:55:07 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-29 21:17:17 +01:00
|
|
|
module.exports.deleteReally = async function (value, myOrderCart) {
|
|
|
|
|
|
|
|
|
|
return await OrdersCart.deleteRecsInOrdersById(myOrderCart);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 00:48:17 +01:00
|
|
|
module.exports.createOrdersCart = async function (newOrdersCart) {
|
|
|
|
|
return await newOrdersCart.save()
|
|
|
|
|
}
|
2023-12-29 21:17:17 +01:00
|
|
|
|
2024-01-02 15:24:44 +01:00
|
|
|
|
|
|
|
|
module.exports.addOrderToCash = async function (idorderscart, userIdStore, req) {
|
2023-12-14 00:55:07 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const myorderscart = await OrdersCart.findOne({ _id: idorderscart }).populate('items.order').lean();
|
|
|
|
|
|
2024-01-02 15:24:44 +01:00
|
|
|
const mycash = await Cash.createMovementCashByOrdersCart(myorderscart, userIdStore, req);
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Err', e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.updateStockQtaDalMagazzinoOrdineConfermato = async function (idorderscart) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const myorderscart = await OrdersCart.findOne({ _id: idorderscart }).populate('items.order').lean();
|
2023-12-14 00:55:07 +01:00
|
|
|
if (myorderscart) {
|
|
|
|
|
for (const idkey in myorderscart.items) {
|
|
|
|
|
let order = myorderscart.items[idkey].order;
|
|
|
|
|
|
2023-12-30 21:33:59 +01:00
|
|
|
if (!order.confermato) { // Se ancora non è stato confermato:
|
2023-12-20 21:52:17 +01:00
|
|
|
let update = {
|
2023-12-14 15:20:21 +01:00
|
|
|
$inc: {
|
2023-12-29 21:17:17 +01:00
|
|
|
stockQty: -order.quantity,
|
2023-12-30 21:33:59 +01:00
|
|
|
stockBloccatiQty: order.quantity,
|
|
|
|
|
bookedQtyConfirmed: order.quantity,
|
2023-12-29 21:17:17 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
|
|
|
|
|
|
|
|
|
|
update = {
|
|
|
|
|
$inc: {
|
2023-12-30 21:33:59 +01:00
|
|
|
maxbookableGASQty: -order.quantitypreordered,
|
|
|
|
|
bookableGASBloccatiQty: order.quantitypreordered,
|
|
|
|
|
bookedGASQtyOrdered: order.quantitypreordered,
|
2023-12-29 21:17:17 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Err', e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-12-30 21:33:59 +01:00
|
|
|
module.exports.updateMagazzinoOrdineInLavorazione = async function (idorderscart) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const myorderscart = await OrdersCart.findOne({ _id: idorderscart }).populate('items.order').lean();
|
|
|
|
|
|
|
|
|
|
if (myorderscart) {
|
|
|
|
|
for (const idkey in myorderscart.items) {
|
|
|
|
|
let order = myorderscart.items[idkey].order;
|
|
|
|
|
|
|
|
|
|
if (!order.confermato) { // Se ancora non è stato confermato:
|
|
|
|
|
let update = {
|
|
|
|
|
$inc: {
|
|
|
|
|
bookedQtyConfirmed: order.quantity,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
|
|
|
|
|
|
|
|
|
|
update = {
|
|
|
|
|
$inc: {
|
|
|
|
|
bookedGASQtyOrdered: order.quantitypreordered,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Err', e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
module.exports.updateStockBloccatiQtaDalMagazzinoOrdineConsegnato = async function (idorderscart) {
|
2023-12-29 21:17:17 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const myorderscart = await OrdersCart.findOne({ _id: idorderscart }).populate('items.order').lean();
|
|
|
|
|
|
|
|
|
|
if (myorderscart) {
|
|
|
|
|
for (const idkey in myorderscart.items) {
|
|
|
|
|
let order = myorderscart.items[idkey].order;
|
|
|
|
|
|
|
|
|
|
if (!order.consegnato) {
|
|
|
|
|
let update = {
|
|
|
|
|
$inc: {
|
|
|
|
|
stockBloccatiQty: -order.quantity
|
2023-12-14 00:55:07 +01:00
|
|
|
}
|
2023-12-14 15:20:21 +01:00
|
|
|
};
|
|
|
|
|
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
|
2023-12-20 21:52:17 +01:00
|
|
|
update = {
|
|
|
|
|
$inc: {
|
2023-12-30 21:33:59 +01:00
|
|
|
bookableGASBloccatiQty: -order.quantitypreordered
|
2023-12-20 21:52:17 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
|
2023-12-14 15:20:21 +01:00
|
|
|
}
|
2023-12-14 00:55:07 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Err', e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-30 21:33:59 +01:00
|
|
|
module.exports.updateStockQtaPerCancellazioneOrdine = async function (idorderscart) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const myorderscart = await OrdersCart.findOne({ _id: idorderscart }).populate('items.order').lean();
|
|
|
|
|
|
|
|
|
|
if (myorderscart) {
|
|
|
|
|
for (const idkey in myorderscart.items) {
|
|
|
|
|
let order = myorderscart.items[idkey].order;
|
|
|
|
|
let update = {};
|
|
|
|
|
|
|
|
|
|
let fieldstoUpdate = {};
|
|
|
|
|
|
|
|
|
|
if (order.consegnato) {
|
|
|
|
|
// Se l'ordine era stato già Consegnato, allora non fare niente !
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
if (order.confermato) { // Se l'ordine era stato confermato, allora rimetti le Qta in Magazzino
|
|
|
|
|
fieldstoUpdate = {
|
|
|
|
|
...fieldstoUpdate,
|
|
|
|
|
stockQty: order.quantity,
|
|
|
|
|
stockBloccatiQty: -order.quantity,
|
|
|
|
|
bookedQtyConfirmed: -order.quantity,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update = {
|
|
|
|
|
$inc: fieldstoUpdate,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
|
|
|
|
|
// --------------
|
|
|
|
|
|
|
|
|
|
fieldstoUpdate = {};
|
|
|
|
|
|
|
|
|
|
if (order.confermato) { // Se l'ordine era stato confermato, allora rimetti le Qta in Magazzino
|
|
|
|
|
fieldstoUpdate = {
|
|
|
|
|
...fieldstoUpdate,
|
|
|
|
|
maxbookableGASQty: order.quantitypreordered,
|
|
|
|
|
bookableGASBloccatiQty: -order.quantitypreordered,
|
|
|
|
|
bookedGASQtyOrdered: -order.quantitypreordered,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update = {
|
|
|
|
|
$inc: fieldstoUpdate,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Err', e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-02 15:24:44 +01:00
|
|
|
module.exports.updateCmd = async function (ordersCart, status, value, req, options) {
|
2023-12-13 19:17:53 +01:00
|
|
|
|
2024-01-02 15:24:44 +01:00
|
|
|
const userIdStore = options.userIdStore ?? null;
|
2023-12-14 15:20:21 +01:00
|
|
|
|
2023-12-17 19:19:04 +01:00
|
|
|
let myOrderCart = await OrdersCart.findOne({ _id: ordersCart._id })
|
|
|
|
|
.populate('items.order').lean();
|
2023-12-13 19:17:53 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!!myOrderCart) {
|
2023-12-14 15:20:21 +01:00
|
|
|
|
2023-12-13 19:17:53 +01:00
|
|
|
const id = myOrderCart._id;
|
2023-12-30 21:33:59 +01:00
|
|
|
if (status === shared_consts.OrderStatus.CHECKOUT_SENT) {
|
|
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
|
await OrdersCart.updateMagazzinoOrdineInLavorazione(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (status === shared_consts.OrderStatus.ORDER_CONFIRMED) {
|
2023-12-14 00:55:07 +01:00
|
|
|
|
2023-12-29 21:17:17 +01:00
|
|
|
// Aggiorna anche il Magazzino, togliendo le quantità in Stock e aggiungendole su quelle bloccate
|
2023-12-14 00:55:07 +01:00
|
|
|
if (value) {
|
2023-12-30 21:33:59 +01:00
|
|
|
await OrdersCart.updateStockQtaDalMagazzinoOrdineConfermato(id);
|
2023-12-14 00:55:07 +01:00
|
|
|
}
|
2023-12-30 21:33:59 +01:00
|
|
|
ris = await OrdersCart.setConfermatoById(value, myOrderCart);
|
2023-12-14 15:20:21 +01:00
|
|
|
|
2023-12-29 21:17:17 +01:00
|
|
|
} else if (status === shared_consts.OrderStatus.PAYED) {
|
2023-12-30 21:33:59 +01:00
|
|
|
|
2024-01-02 15:24:44 +01:00
|
|
|
if (value) {
|
|
|
|
|
await OrdersCart.addOrderToCash(id, userIdStore, req);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 21:17:17 +01:00
|
|
|
ris = await OrdersCart.setPagatoById(value, myOrderCart);
|
2023-12-30 21:33:59 +01:00
|
|
|
|
2023-12-29 21:17:17 +01:00
|
|
|
} else if (status === shared_consts.OrderStatus.DELIVERED) { // Consegnato
|
|
|
|
|
if (value) {
|
2023-12-30 21:33:59 +01:00
|
|
|
await OrdersCart.updateStockBloccatiQtaDalMagazzinoOrdineConsegnato(id);
|
2023-12-29 21:17:17 +01:00
|
|
|
}
|
2023-12-14 15:20:21 +01:00
|
|
|
ris = await OrdersCart.setConsegnatoById(value, myOrderCart);
|
|
|
|
|
} else if (status === shared_consts.OrderStatus.SHIPPED) {
|
2023-12-30 21:33:59 +01:00
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
ris = await OrdersCart.setSpeditoById(value, myOrderCart);
|
2023-12-30 21:33:59 +01:00
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
} else if (status === shared_consts.OrderStatus.RECEIVED) {
|
|
|
|
|
ris = await OrdersCart.setRicevutoById(value, myOrderCart);
|
2023-12-30 21:33:59 +01:00
|
|
|
} else if (status === shared_consts.OrderStatus.CANCELED) {
|
|
|
|
|
await OrdersCart.updateStockQtaPerCancellazioneOrdine(id)
|
|
|
|
|
|
2023-12-29 21:17:17 +01:00
|
|
|
} else if (status === shared_consts.OrderStatus.DELETE_REALLY) {
|
2023-12-30 21:33:59 +01:00
|
|
|
await OrdersCart.updateStockQtaPerCancellazioneOrdine(id)
|
2023-12-29 21:17:17 +01:00
|
|
|
ris = await OrdersCart.deleteReally(value, myOrderCart);
|
2023-12-13 19:17:53 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-29 21:17:17 +01:00
|
|
|
if (status !== shared_consts.OrderStatus.DELETE_REALLY) {
|
2023-12-14 15:20:21 +01:00
|
|
|
|
2023-12-29 21:17:17 +01:00
|
|
|
await OrdersCart.setFieldInOrdersById({ status }, myOrderCart);
|
|
|
|
|
|
|
|
|
|
myOrderCart = await OrdersCart.getOrdersCartById(ordersCart._id)
|
|
|
|
|
}
|
2023-12-13 19:17:53 +01:00
|
|
|
// myOrderCart = await OrdersCart.findOne({ _id: idorderscart });
|
|
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
return myOrderCart;
|
2023-12-13 19:17:53 +01:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Err:', e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-01-18 00:48:17 +01:00
|
|
|
|
|
|
|
|
OrdersCartSchema.pre('save', async function (next) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (this.isNew) {
|
|
|
|
|
try {
|
|
|
|
|
const myrec = await User.findOne({ idapp: this.idapp }).limit(1).sort({ neworder: -1 });
|
|
|
|
|
|
|
|
|
|
if (!!myrec) {
|
|
|
|
|
this.neworder = myrec._doc.neworder + 1;
|
|
|
|
|
} else {
|
|
|
|
|
this.neworder = 1;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.neworder = 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
if (user.isModified('password')) {
|
|
|
|
|
bcrypt.genSalt(10, (err, salt) => {
|
|
|
|
|
bcrypt.hash(user.password, salt, (err, hash) => {
|
|
|
|
|
user.password = hash;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e.message);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-12-09 11:55:58 +01:00
|
|
|
|
2023-12-17 19:19:04 +01:00
|
|
|
module.exports.getmsgorderTelegram = async function (ordersCart) {
|
|
|
|
|
try {
|
|
|
|
|
const statusstr = shared_consts.getStatusStr(ordersCart.status);
|
|
|
|
|
|
|
|
|
|
let msg = '🟢✍️ Ordine n. ' + ordersCart.numorder
|
|
|
|
|
|
|
|
|
|
msg += '<br>Stato: ' + statusstr;
|
|
|
|
|
|
|
|
|
|
msg += '<br>🙎🏻♂️ ' + tools.getNomeCognomeEUserNameByUser(ordersCart.user)
|
|
|
|
|
if (ordersCart.note)
|
|
|
|
|
msg += '<br>Note: ' + ordersCart.note;
|
|
|
|
|
|
|
|
|
|
|
2023-12-30 23:58:35 +01:00
|
|
|
msg += '<br><br>Lista Prodotti: (🍊🥑🍋)';
|
2023-12-17 19:19:04 +01:00
|
|
|
for (const ord of ordersCart.items) {
|
|
|
|
|
msg += '<br>';
|
2023-12-20 21:52:17 +01:00
|
|
|
let qtystr = ''
|
2023-12-31 14:34:01 +01:00
|
|
|
let qtynum = 0
|
2023-12-20 21:52:17 +01:00
|
|
|
if (ord.order.quantity > 0)
|
2023-12-31 14:34:01 +01:00
|
|
|
qtynum += ord.order.quantity;
|
2023-12-20 21:52:17 +01:00
|
|
|
if (ord.order.quantitypreordered > 0)
|
2023-12-31 14:34:01 +01:00
|
|
|
qtynum += ord.order.quantitypreordered;
|
2023-12-30 23:58:35 +01:00
|
|
|
|
2023-12-31 14:34:01 +01:00
|
|
|
qtystr += qtynum + ' ' + tools.getUnitsMeasure(ord.order.product.productInfo.unit, true);
|
2023-12-30 23:58:35 +01:00
|
|
|
|
|
|
|
|
if (ord.order.quantitypreordered > 0)
|
|
|
|
|
qtystr += ord.order.quantitypreordered + ' Pre-Ordinati'
|
|
|
|
|
|
|
|
|
|
msg += '✅ [' + qtystr + '] ' + ord.order.product.productInfo.name + ' a ' + ord.order.price + '€ ' + (ord.order.after_price ? ord.order.after_price : '') + '<br>Totale = ' + ord.order.TotalPriceProduct + '€';
|
2023-12-17 19:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg += '<br>';
|
|
|
|
|
|
2023-12-28 00:50:35 +01:00
|
|
|
let totqta = ordersCart.totalQty + ordersCart.totalQtyPreordered;
|
|
|
|
|
|
|
|
|
|
msg += '<br>Totale Prodotti: ' + totqta;
|
2023-12-17 19:19:04 +01:00
|
|
|
msg += '<br>Totale Ordine: ' + ordersCart.totalPrice + ' € 💰';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Err', e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-09 11:55:58 +01:00
|
|
|
module.exports.createIndexes((err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|