From b4a5db028ed06b93e82dfc91edb5bce59dc6a2b9 Mon Sep 17 00:00:00 2001 From: martin chaparro Date: Sun, 2 Oct 2022 08:03:49 -0500 Subject: [PATCH] ahorros --- APP/components/Formularios/Fahorro.vue | 69 +++++ APP/layouts/default.vue | 9 + APP/pages/ahorros.vue | 348 +++++++++++++++++++++++++ APP/pages/configuracion.vue | 8 +- APP/store/index.js | 2 +- index.js | 1 + models/ahorros.js | 22 ++ routes/ahorro.js | 111 ++++++++ 8 files changed, 568 insertions(+), 2 deletions(-) create mode 100644 APP/components/Formularios/Fahorro.vue create mode 100644 APP/pages/ahorros.vue create mode 100755 models/ahorros.js create mode 100644 routes/ahorro.js diff --git a/APP/components/Formularios/Fahorro.vue b/APP/components/Formularios/Fahorro.vue new file mode 100644 index 0000000..fde8792 --- /dev/null +++ b/APP/components/Formularios/Fahorro.vue @@ -0,0 +1,69 @@ + + + + + \ No newline at end of file diff --git a/APP/layouts/default.vue b/APP/layouts/default.vue index 32c0add..e7ca56a 100644 --- a/APP/layouts/default.vue +++ b/APP/layouts/default.vue @@ -31,6 +31,15 @@ path: '/presupuesto', }" > + + + +
+ +
+ + + + + + + + +
+
+ +
+
+
+
+ + + + + + + + + +
+ {{ + selectedAhorro.nombreAhorro + }} +
+ {{ + selectedAhorro.detalleAhorro + }} +
+
+ +
+
+ + + + + + + + + + +
+ + + + + +
+
+
+
+ +
+ + + + + + + Guardar +
+
+
+ + +
+
+ Aportes: {{ formatMoneda(totalAportes) }} +
+
+ Retiros: {{ formatMoneda(totalRetiros) }} +
+
+ Total: + {{ formatMoneda(total) }} +
+
+
+
+ + + + + \ No newline at end of file diff --git a/APP/pages/configuracion.vue b/APP/pages/configuracion.vue index 19dc828..25f2b1b 100644 --- a/APP/pages/configuracion.vue +++ b/APP/pages/configuracion.vue @@ -6,6 +6,11 @@ label="Número de presupuestos" type="number" v-model="filtro.npresupuesto" + > + Guardar @@ -190,7 +195,8 @@ export default { metodos: [], filtro: { fechas: "", - npresupuesto: 0 + npresupuesto: 0, + nahorro: 0 }, }; }, diff --git a/APP/store/index.js b/APP/store/index.js index 9fb6ac6..8e5bf08 100644 --- a/APP/store/index.js +++ b/APP/store/index.js @@ -1,7 +1,7 @@ export const state = () => ({ auth: null, notifications: [], - filtro: { fechas: fechaString().slice(0, 7), npresupuesto: 5 }, + filtro: { fechas: fechaString().slice(0, 7), npresupuesto: 5,nahorro:20 }, fecha: fechaString(), categorias: [], metodos_de_pago: [] diff --git a/index.js b/index.js index 53f38ee..191f64e 100644 --- a/index.js +++ b/index.js @@ -30,6 +30,7 @@ app.use('/api',require('./routes/users')); app.use('/api',require('./routes/compras')) app.use('/api',require('./routes/ingresos')) app.use('/api',require('./routes/presupuesto')) +app.use('/api',require('./routes/ahorro')) app.use('/api',require('./routes/creditos')) app.use('/api',require('./routes/categorias')) app.use('/api',require('./routes/metodos_pago')) diff --git a/models/ahorros.js b/models/ahorros.js new file mode 100755 index 0000000..bc05935 --- /dev/null +++ b/models/ahorros.js @@ -0,0 +1,22 @@ +const mongoose = require('mongoose'); +const {Schema} =mongoose; + +const itemSchema = new Schema({ + detalle:{type:String}, + valor:{type:Number}, + tipo:{type:String, required:true} +}) + +const AhorroSchema=new Schema({ + date: {type: Date, default: Date.now}, + nombreAhorro: {type:String,required:true}, + detalleAhorro:{type:String,required:true}, + datos:[itemSchema], + user:{type:String, required:true}, + child:itemSchema + + +}); + +module.exports.Ahorro=mongoose.model('ahorro',AhorroSchema); +module.exports.itemAhorro=mongoose.model('itemAhorro',itemSchema); \ No newline at end of file diff --git a/routes/ahorro.js b/routes/ahorro.js new file mode 100644 index 0000000..b0af972 --- /dev/null +++ b/routes/ahorro.js @@ -0,0 +1,111 @@ +const router = require("express").Router(); +const ahorro = require("../models/ahorros").Ahorro; +const itemAhorro = require("../models/ahorros").itemAhorro; +const { checkAuth } = require("../middlewares/authentication"); + +router.get("/ahorro", checkAuth, async (req, res) => { + var ahorros; + let limite = req.get('limite'); + ahorros = await ahorro.find({ user: req.userData._id }).sort({ + date: "desc", + }).limit(parseInt(limite)); + + + return res.send({ + status: "ok", + data: ahorros, + }); +}); + +router.post("/ahorro", checkAuth, async (req, res) => { + const { nombreAhorro,detalleAhorro } = req.body; + + var ahorros = await ahorro.find({ + user: req.userData._id, + nombreAhorro: nombreAhorro, + }); + + if (ahorros.length == 0) { + const Ahorro = new ahorro({ + nombreAhorro: nombreAhorro, + detalleAhorro:detalleAhorro, + }); + + console.log(Ahorro); + Ahorro.user = req.userData._id; + await Ahorro.save(); + + return res.json({ + status: "OK", + }); + } + + return res.status(500).json({ + status: "FAIL", + }); +}); + +router.delete("/ahorro", checkAuth, async (req, res) => { + try { + const userId = req.userData._id; + const id = req.query.id; + + const resultado = await ahorro.deleteOne({ user: userId, _id: id }); + + return res.json({ status: "ok", data: resultado }); + } catch (error) { + console.log(error); + return res.status(500).json({ status: "fail", error: error }); + } +}); + +router.put("/ahorro", checkAuth, async (req, res) => { + const { _id, detalle, valor, tipo } = req.body; + const ahorro_edit = await ahorro.findOne({ + _id: _id, + user: req.userData._id, + }); + const itemP = new itemAhorro({ detalle, valor, tipo }); + ahorro_edit.datos.push(itemP); + await ahorro_edit.save(); + + res.json({ + status: "OK", + }); +}); + +router.get("/ahorro_items", checkAuth, async (req, res) => { + const _id = req.query.ahorro_id; + const ahorro_edit = await ahorro.findOne({ + _id: _id, + user: req.userData._id, + }); + + return res.json({ + status: "OK", + data: ahorro_edit.datos, + }); +}); + +router.delete("/ahorroitem", checkAuth, async (req, res) => { + try { + const userId = req.userData._id; + + const iditem = req.query.iditem; + const idAhorro = req.query.idAhorro; + + var ahorro_edit = await ahorro.findOne({ + user: userId, + _id: idAhorro, + }); + ahorro_edit.datos.id(iditem).remove(); + await ahorro_edit.save(); + + return res.json({ status: "ok" }); + } catch (error) { + console.log(error); + return res.status(500).json({ status: "fail", error: error }); + } +}); + +module.exports = router;