sum up expenses and withdrawals

This commit is contained in:
2016-04-20 16:16:32 +02:00
parent b0d73927e5
commit 9749a849f0
4 changed files with 48 additions and 14 deletions
+36 -8
View File
@@ -37,26 +37,52 @@ transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location'
accountUsers.$promise.then(function(data) {
$scope.accountUsers = accountUsers._embedded.users;
// get outstandings for all users of the account
// initialize attributes for outstanding amounts
for (i = 0; i < $scope.accountUsers.length; i++) {
$scope.accountUsers[i].outstandings = 0.0;
}
// get expenses for all users of the account
var promises = new Array($scope.accountUsers.length);
for (i = 0; i < $scope.accountUsers.length; i++) {
var resource = $resource('/transactions/search/outstandings', { accountId: $scope.account.id, userId: $scope.accountUsers[i].id }).get();
var resource = $resource('/transactions/search/expenses', { accountId: $scope.account.id, userId: $scope.accountUsers[i].id }).get();
promises[i] = resource.$promise;
}
$q.all(promises).then(function(data) {
for (i = 0; i < data.length; i++) {
var outstandingSum = 0;
var expenseSum = 0;
for (j = 0; j < data[i]._embedded.transactions.length; j++) {
outstandingSum = outstandingSum + data[i]._embedded.transactions[j].amount;
expenseSum = expenseSum + data[i]._embedded.transactions[j].amount;
}
$scope.accountUsers[i].outstandings = outstandingSum;
console.log('$scope.userinfo.outstandings: ' + $scope.accountUsers[i].outstandings);
$scope.accountUsers[i].outstandings -= expenseSum;
//console.log('Expenses by ' + $scope.accountUsers[i].name + ': ' + expenseSum);
}
});
// get withdrawals for all users of the account
var promises = new Array($scope.accountUsers.length);
for (i = 0; i < $scope.accountUsers.length; i++) {
var resource = $resource('/withdrawals/search/forAccount', { accountId: $scope.account.id, userId: $scope.accountUsers[i].id }).get();
promises[i] = resource.$promise;
}
$q.all(promises).then(function(data) {
for (i = 0; i < data.length; i++) {
var withdrawnSum = 0;
for (j = 0; j < data[i]._embedded.withdrawals.length; j++) {
withdrawnSum = withdrawnSum + data[i]._embedded.withdrawals[j].amount;
}
$scope.accountUsers[i].outstandings -= withdrawnSum;
//console.log('Withdrawals by ' + $scope.accountUsers[i].name + ': ' + withdrawnSum);
}
});
});
// TODO for some reason this does not work upon reload
document.getElementsByName('expenseBy')[0].checked = true;
}
$scope.checkCategory = function() {
@@ -139,7 +165,6 @@ transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location'
newWithdrawal.$save(function(withdrawal) {
$scope.refreshList();
$scope.lastSubmission = newWithdrawal;
console.log(newWithdrawal);
});
return;
@@ -147,6 +172,9 @@ transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location'
// via Resource (might be moved into a service)
var Transaction = $resource('/transactions');
console.log('Creditor:');
console.log($scope.transaction.creditor);
var newTransaction = new Transaction({
"account" : $scope.account._links.self.href,
@@ -154,7 +182,7 @@ transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location'
"date" : $scope.transaction.date,
"description" : $scope.transaction.description,
"category" : $scope.transaction.category._links.self.href,
"creditor" : $scope.creditor
"creditor" : $scope.transaction.creditor
});
newTransaction.$save(function(transaction) {