aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDennis Eriksen <dennis.se@gmail.com>2013-05-24 19:00:10 +0200
committerDennis Eriksen <dennis.se@gmail.com>2013-05-24 19:00:10 +0200
commit984c35cf8e7f8d374d504d3abf5cf8671985d471 (patch)
tree76ed62a1d1ede163944fbc6b7799dcfa3c659b65
parentsped things up by not requiring so much data (diff)
downloadDivid-984c35cf8e7f8d374d504d3abf5cf8671985d471.tar.gz
made the 'total' calculations work.. w00p. closes #30 and improves on #9
-rw-r--r--controllers/system.js41
-rw-r--r--views/project.ejs6
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 @@
<div class="row-fluid">
<table class="table table-bordered table-condensed">
<tr>
- <td class="success">3000</td>
- <td class="error">4000</td>
- <td>1000</td>
+ <td class="success"><%= pro.user[req.user._id].total %></td>
+ <td class="error"><%= pro.total %></td>
+ <td><%= pro.user[req.user._id].diff %></td>
</tr>
</table><a href="#table-info" class="accordion-toggle" data-toggle="collapse"><i class="icon-info-sign"></i></a>
</div>