Files
freeplanet_serverside/src/server/models/order.js

386 lines
7.6 KiB
JavaScript
Raw Normal View History

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' },
idScontisticas: [{ type: Schema.Types.ObjectId, ref: 'Scontistica' }],
idProvider: { type: Schema.Types.ObjectId, ref: 'Provider' },
2020-12-25 03:54:16 +01:00
price: {
2023-12-18 08:02:28 +01:00
type: Number,
default: 0,
2020-12-25 03:54:16 +01:00
},
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: {
2023-12-18 08:02:28 +01:00
type: Number,
default: 0,
},
TotalPriceProduct: {
type: Number,
default: 0,
2020-12-25 03:54:16 +01:00
},
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
},
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'
}
},
{
$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: {
path: '$product',
2023-12-16 00:51:03 +01:00
preserveNullAndEmptyArrays: true,
},
},
{
$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) {
try {
2023-12-18 12:11:12 +01:00
Order.updateTotals(order);
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-12 15:42:41 +01:00
for (const order of arrOrders) {
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: status });
}
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-13 19:17:53 +01:00
for (const order of arrOrders) {
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: myelements });
}
2023-12-13 19:17:53 +01:00
}
2023-12-18 12:11:12 +01:00
module.exports.updateTotals = function (order) {
try {
if (!order) {
return;
}
2023-12-18 12:11:12 +01:00
let mypricecalc = 0;
order.TotalPriceProduct = 0;
// Calcolo Sconto
let sconti_da_applicare = [];
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)
}
}*/
if (sconti_da_applicare.length > 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;
}
order.TotalPriceProduct += mypricecalc;
return order;
} catch (e) {
console.error('Err:', e);
}
}
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'
}
},
{
$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: 'scontisticas'
2023-12-16 00:51:03 +01:00
}
},
{
$unwind: {
path: '$product',
2023-12-16 00:51:03 +01:00
preserveNullAndEmptyArrays: true,
},
},
{
$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 };