From 984c35cf8e7f8d374d504d3abf5cf8671985d471 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Fri, 24 May 2013 19:00:10 +0200 Subject: made the 'total' calculations work.. w00p. closes #30 and improves on #9 --- controllers/system.js | 41 ++++++++++++++++++++++++++++++++++++++--- views/project.ejs | 6 +++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/controllers/system.js b/controllers/system.js index 12bef2a..9629c17 100644 --- a/controllers/system.js +++ b/controllers/system.js @@ -142,11 +142,45 @@ exports.project = function(req, res) { if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack }); Access.loadProject(project._id, function(err, access) { if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack }); - access.forEach(function(a) { - if (String(a.user._id) === String(req.user._id)) req.user.permissions = a.permissions; - }); pPost.loadProject(project._id, function(err, posts) { if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack }); + + // ALRIGHT! This is where the FUN starts! + + // first we create an object that will hold all the calculational data + var pro = { + users: 0 // number of users + , user: [] // this array will contain every user. Every user will then have it's own object inside this. + , total: 0 // the overall total. + , each: 0 // what each person has to pay + }; + + // then we calculate how many users we have, and initiate objects foreach user + access.forEach(function(a) { + if (String(a.user._id) === String(req.user._id)) req.user.permissions = a.permissions; //sets YOUR permissions in this project + + pro.users++; + pro.user[a.user.id] = { + total: 0 + , diff: 0 + }; + }); + + // now we must collect all the money! + posts.forEach(function(p) { + pro.total += parseFloat(p.value); + pro.user[p.user._id].total += parseFloat(p.value); + }); + + // then calculate how much each user must pay in total + pro.each = pro.total / pro.users; + + // then calculate how much each person owe and is owned + for(var i in pro.user) { + pro.user[i].diff = parseFloat(pro.user[i].total - pro.each).toFixed(2); + } + + console.log(pro); res.render('project', { title: project.name , user: req.user @@ -154,6 +188,7 @@ exports.project = function(req, res) { , project: project , access: access , posts: posts + , pro: pro }); }); }); diff --git a/views/project.ejs b/views/project.ejs index 88207db..d78d11c 100644 --- a/views/project.ejs +++ b/views/project.ejs @@ -63,9 +63,9 @@
- - - + + +
300040001000<%= pro.user[req.user._id].total %><%= pro.total %><%= pro.user[req.user._id].diff %>
-- cgit v1.2.3