2023-12-09 11:55:58 +01:00
|
|
|
|
2022-09-14 11:32:04 +02:00
|
|
|
const mongoose = require('mongoose').set('debug', false)
|
2020-12-25 03:54:16 +01:00
|
|
|
const Schema = mongoose.Schema;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { ObjectID } = require('mongodb');
|
|
|
|
|
|
|
|
|
|
mongoose.Promise = global.Promise;
|
|
|
|
|
mongoose.level = "F";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
|
|
|
mongoose.plugin(schema => {
|
|
|
|
|
schema.options.usePushEach = true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const orderSchema = new Schema({
|
|
|
|
|
idapp: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2023-12-14 15:20:21 +01:00
|
|
|
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
2020-12-25 03:54:16 +01:00
|
|
|
status: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
2023-12-12 15:42:41 +01:00
|
|
|
idProduct: { type: Schema.Types.ObjectId, ref: 'Product' },
|
|
|
|
|
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
|
|
|
|
|
idStorehouse: { type: Schema.Types.ObjectId, ref: 'StoreHouse' },
|
2023-12-16 18:40:17 +01:00
|
|
|
idScontisticas: [{ type: Schema.Types.ObjectId, ref: 'Scontistica' }],
|
2023-12-15 00:57:14 +01:00
|
|
|
idProvider: { type: Schema.Types.ObjectId, ref: 'Provider' },
|
2020-12-25 03:54:16 +01:00
|
|
|
price: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
2021-06-04 10:07:57 +02:00
|
|
|
after_price: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
2020-12-25 03:54:16 +01:00
|
|
|
color: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
|
|
|
|
size: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
|
|
|
|
quantity: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
2023-12-13 19:17:53 +01:00
|
|
|
evaso: { // e quindi è stato tolto dal magazzino (aggiornando il campo StockQty)
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
date_evaso: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
pagato: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
date_pagato: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
spedito: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
date_spedito: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
2023-12-14 15:20:21 +01:00
|
|
|
completato: {
|
2023-12-13 19:17:53 +01:00
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
2023-12-14 15:20:21 +01:00
|
|
|
date_completato: {
|
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
|
|
|
|
|
},
|
2020-12-25 03:54:16 +01:00
|
|
|
weight: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
2023-11-30 14:27:37 +01:00
|
|
|
unit: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
2020-12-25 03:54:16 +01:00
|
|
|
stars: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
|
|
|
|
date_created: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
date_checkout: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
date_payment: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
date_shipping: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
date_delivered: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
notes: {
|
|
|
|
|
type: String
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var Order = module.exports = mongoose.model('Order', orderSchema);
|
|
|
|
|
|
2023-12-09 11:55:58 +01:00
|
|
|
module.exports.createIndexes((err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
module.exports.getFieldsForSearch = function () {
|
|
|
|
|
return []
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports.executeQueryTable = function (idapp, params) {
|
2023-12-09 11:55:58 +01:00
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
|
|
|
|
return tools.executeQueryTable(this, idapp, params);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports.findAllIdApp = async function (idapp) {
|
|
|
|
|
|
|
|
|
|
const query = [
|
|
|
|
|
{ $match: { idapp } },
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'products',
|
2023-12-12 15:42:41 +01:00
|
|
|
localField: 'idProduct',
|
2020-12-25 03:54:16 +01:00
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'product'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'producers',
|
2023-12-12 15:42:41 +01:00
|
|
|
localField: 'idProducer',
|
2020-12-25 03:54:16 +01:00
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'producer'
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-12-15 00:57:14 +01:00
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'providers',
|
|
|
|
|
localField: 'idProvider',
|
|
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'provider'
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-12-16 00:51:03 +01:00
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'scontisticas',
|
|
|
|
|
localField: 'idScontisticas',
|
|
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'scontistica'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$unwind: {
|
2023-12-16 18:40:17 +01:00
|
|
|
path: '$product',
|
2023-12-16 00:51:03 +01:00
|
|
|
preserveNullAndEmptyArrays: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-12-16 18:40:17 +01:00
|
|
|
{
|
|
|
|
|
$unwind: {
|
|
|
|
|
path: '$producer',
|
|
|
|
|
preserveNullAndEmptyArrays: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$unwind: {
|
|
|
|
|
path: '$provider',
|
|
|
|
|
preserveNullAndEmptyArrays: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return await Order.aggregate(query)
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports.getAllOrders = function (query, sort, callback) {
|
|
|
|
|
Order.find(query, null, sort, callback)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.getOrderByUserId = function (userId, sort, callback) {
|
|
|
|
|
Order.find({ userId }, null, sort, callback)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.getOrderByID = function (id, callback) {
|
|
|
|
|
Order.findById(id, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.createOrder = async function (order) {
|
2023-12-15 21:50:21 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return await Order.create(order)
|
|
|
|
|
.then((ris) => {
|
|
|
|
|
if (!!ris)
|
|
|
|
|
return ris._id;
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('err', e);
|
|
|
|
|
}
|
2020-12-25 03:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-12 15:42:41 +01:00
|
|
|
module.exports.updateStatusOrders = async function (arrOrders, status) {
|
2023-12-15 21:50:21 +01:00
|
|
|
|
2023-12-12 15:42:41 +01:00
|
|
|
for (const order of arrOrders) {
|
|
|
|
|
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: status });
|
|
|
|
|
}
|
2023-12-15 21:50:21 +01:00
|
|
|
|
2023-12-12 15:42:41 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-13 19:17:53 +01:00
|
|
|
module.exports.updateStatusOrdersElements = async function (arrOrders, myelements) {
|
2023-12-15 21:50:21 +01:00
|
|
|
|
2023-12-13 19:17:53 +01:00
|
|
|
for (const order of arrOrders) {
|
|
|
|
|
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: myelements });
|
|
|
|
|
}
|
2023-12-15 21:50:21 +01:00
|
|
|
|
2023-12-13 19:17:53 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
module.exports.getTotalOrderById = async function (id) {
|
|
|
|
|
const query = [
|
|
|
|
|
{ $match: { _id: ObjectID(id) } },
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'products',
|
2023-12-12 15:42:41 +01:00
|
|
|
localField: 'idProduct',
|
2020-12-25 03:54:16 +01:00
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'product'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'producers',
|
2023-12-12 15:42:41 +01:00
|
|
|
localField: 'idProducer',
|
2020-12-25 03:54:16 +01:00
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'producer'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'storehouses',
|
2023-12-12 15:42:41 +01:00
|
|
|
localField: 'idStorehouse',
|
2020-12-25 03:54:16 +01:00
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'storehouse'
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-12-15 00:57:14 +01:00
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'providers',
|
|
|
|
|
localField: 'idProvider',
|
|
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'provider'
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-12-16 00:51:03 +01:00
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'scontisticas',
|
|
|
|
|
localField: 'idScontisticas',
|
|
|
|
|
foreignField: '_id',
|
2023-12-16 18:40:17 +01:00
|
|
|
as: 'scontisticas'
|
2023-12-16 00:51:03 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$unwind: {
|
2023-12-16 18:40:17 +01:00
|
|
|
path: '$product',
|
2023-12-16 00:51:03 +01:00
|
|
|
preserveNullAndEmptyArrays: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-12-16 18:40:17 +01:00
|
|
|
{
|
|
|
|
|
$unwind: {
|
|
|
|
|
path: '$producer',
|
|
|
|
|
preserveNullAndEmptyArrays: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$unwind: {
|
|
|
|
|
path: '$storehouse',
|
|
|
|
|
preserveNullAndEmptyArrays: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$unwind: {
|
|
|
|
|
path: '$provider',
|
|
|
|
|
preserveNullAndEmptyArrays: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
2020-12-25 03:54:16 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return await Order.aggregate(query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const Order = mongoose.model('Order', OrderSchema);
|
|
|
|
|
|
|
|
|
|
// module.exports = { Order };
|