You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
236 lines
6.2 KiB
236 lines
6.2 KiB
//Se declara express
|
|
const express = require("express");
|
|
|
|
//Se inicializa express
|
|
var app = express();
|
|
|
|
const storeOrders = require('../orders/store');
|
|
|
|
const ModelOrders = require('../orders/model');
|
|
const ModelCategories = require('../categories/model');
|
|
const ModelProviders = require("../providers/model");
|
|
const Model = require("./model");
|
|
|
|
//Agrega el mensaje a la BD
|
|
function addSupply(supply, order) {
|
|
console.log('Este es el idprovider' + order.idProvider);
|
|
const mySupply = new Model(supply);
|
|
const newOrder = {...order, idProduct: mySupply._id}
|
|
const myOrder = new ModelOrders(newOrder)
|
|
console.log('DESDE STOREEEEEEEEEEEEEE: ' + supply.amount)
|
|
|
|
mySupply.save();
|
|
myOrder.save();
|
|
insertCategory(mySupply.category, mySupply._id);
|
|
insertProvider(mySupply.providers, mySupply._id, supply.amount, supply.quantity);
|
|
}
|
|
|
|
async function getSupplies(filterSupply) {
|
|
return new Promise((resolve, reject) => {
|
|
let filter = {statusDB: true};
|
|
if (filterSupply !== null) {
|
|
filter = { _id: filterSupply};
|
|
}
|
|
Model.find(filter)
|
|
.populate('providers', 'name description')
|
|
.populate('category', 'name description')
|
|
.exec((err, supplies) => {
|
|
if (err) {
|
|
console.log(err)
|
|
reject("Error al obtener los insumos");
|
|
} else {
|
|
resolve(supplies);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
async function getTrashSupplies() {
|
|
return new Promise((resolve, reject) => {
|
|
const filter = {statusDB: false};
|
|
Model.find(filter)
|
|
.populate('providers', 'name description')
|
|
.populate('category', 'name description')
|
|
.exec((err, supplies) => {
|
|
if (err) {
|
|
console.log(err)
|
|
reject("Error al obtener los insumos");
|
|
} else {
|
|
resolve(supplies);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
async function updateSupply(id, name, image, description, location, category, code, providers, unity) {
|
|
|
|
const filter = {_id: id};
|
|
var update = {
|
|
|
|
name: name,
|
|
description: description,
|
|
location: location,
|
|
category: category,
|
|
code: code,
|
|
providers: providers,
|
|
unity: unity
|
|
|
|
};
|
|
const opts = { new: true };
|
|
if(image != ""){
|
|
update = {...update, imageSupply: image}
|
|
}
|
|
let updatedSupply = await Model.findByIdAndUpdate(filter, update, opts);
|
|
console.log(updatedSupply);
|
|
return updatedSupply;
|
|
|
|
}
|
|
|
|
function deleteSupply(id) {
|
|
return Model.findByIdAndDelete(id);
|
|
}
|
|
|
|
|
|
// delete Supply statusDB false
|
|
async function changeStatusDB(id) {
|
|
let foundSupply = await Model.findById(id);
|
|
const filter = {_id: id};
|
|
const opts = { new: true };
|
|
var update = {}
|
|
if (foundSupply.statusDB == true){
|
|
console.log('Está activo...moviendo a la basura');
|
|
update = {statusDB: false};
|
|
|
|
}else{
|
|
console.log('Está inactivo...restableciendolo');
|
|
update = {statusDB: true};
|
|
}
|
|
|
|
foundSupply = await Model.findByIdAndUpdate(filter, update, opts);
|
|
console.log(foundSupply);
|
|
return foundSupply;
|
|
|
|
|
|
|
|
|
|
/* const newSupply = await foundSupply.save();
|
|
return newSupply; */
|
|
}
|
|
|
|
async function listProviders(filterSupply){
|
|
return new Promise((resolve, reject) => {
|
|
let filter = {statusDB: true};
|
|
if (filterSupply !== null) {
|
|
filter = { _id: filterSupply};
|
|
}
|
|
Model.find(filter)
|
|
.populate('providers', 'name description')
|
|
.populate('category', 'name description')
|
|
.exec(async (err, supplies) => {
|
|
if (err) {
|
|
console.log(err)
|
|
reject("Error al obtener los insumos");
|
|
} else {
|
|
var listProviders = [];
|
|
var allOrders = [];
|
|
var total = 0;
|
|
const orders = await storeOrders.list(filterSupply);
|
|
supplies[0].providers.map((itemProvider, index) =>(
|
|
orders.map((itemOrder, index) => {
|
|
//console.log('itemProvider: ' + itemProvider._id + ' itemOrder: ' + itemOrder.idProvider._id + ' Nro: ' + index)
|
|
if(String(itemProvider._id) === String(itemOrder.idProvider._id)){
|
|
console.log('Its a match! ' + itemProvider._id + ' = ' + itemOrder.idProvider._id + ' Nro: ' + index);
|
|
allOrders.push({date: itemOrder.date, amount: itemOrder.amount, description: itemOrder.description, quantity: itemOrder.quantity})
|
|
total += itemOrder.quantity;
|
|
console.log(allOrders)
|
|
}
|
|
}),
|
|
console.log(''),
|
|
listProviders.push({ name: itemProvider, orders: allOrders, total: total }),
|
|
allOrders = [],
|
|
total = 0
|
|
))
|
|
|
|
|
|
|
|
resolve(listProviders);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
//remove provider
|
|
async function removeProvider(id, idProvider) {
|
|
const foundSupply = await Model.findOneAndUpdate(
|
|
{
|
|
_id: id,
|
|
},
|
|
{
|
|
$pull: {
|
|
providers: idProvider,
|
|
},
|
|
}
|
|
);
|
|
return foundSupply;
|
|
}
|
|
|
|
|
|
//Inserta el producto al proveedor una vez posteado el supply
|
|
async function insertProvider(id, supply, amount, quantity) {
|
|
console.log("Este es el id del supply enviado: " + supply)
|
|
console.log(amount);
|
|
const filter = {_id: id};
|
|
var update = {$push: {
|
|
products: supply,
|
|
order: [{idProduct: supply, amount: amount, quantity: quantity, date: new Date()}]
|
|
}};
|
|
|
|
const opts = { new: true };
|
|
|
|
await ModelProviders.findByIdAndUpdate(filter, update, opts);
|
|
|
|
}
|
|
|
|
async function insertCategory(id, supply) {
|
|
|
|
const filter = {_id: id};
|
|
var update = {$push: {
|
|
|
|
products: supply,
|
|
|
|
}};
|
|
|
|
const opts = { new: true };
|
|
|
|
await ModelCategories.findByIdAndUpdate(filter, update, opts);
|
|
|
|
}
|
|
|
|
|
|
//update note
|
|
async function updateNote(id, note) {
|
|
console.log(id,note)
|
|
const filter = { _id: id };
|
|
var update = {
|
|
note: note,
|
|
};
|
|
|
|
const opts = { new: true };
|
|
|
|
let updatedSupply = await Model.findByIdAndUpdate(filter, update, opts);
|
|
console.log(updatedSupply);
|
|
return updatedSupply;
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
add: addSupply,
|
|
list: getSupplies,
|
|
update: updateSupply,
|
|
remove: deleteSupply,
|
|
removeProvider: removeProvider,
|
|
changeStatusDB: changeStatusDB,
|
|
getTrashSupplies: getTrashSupplies,
|
|
listProviders: listProviders,
|
|
updateNote: updateNote
|
|
};
|
|
|