diff --git a/src/main/java/de/tilman/transactions/repository/TransactionRepository.java b/src/main/java/de/tilman/transactions/repository/TransactionRepository.java index c1f624b..a20101d 100644 --- a/src/main/java/de/tilman/transactions/repository/TransactionRepository.java +++ b/src/main/java/de/tilman/transactions/repository/TransactionRepository.java @@ -30,7 +30,7 @@ public interface TransactionRepository extends PagingAndSortingRepository getDescriptionsByAccount(@Param("accountId") Long accountId, @Param("prefix") String prefix, Pageable pageable); - @RestResource(path = "outstandings") + @RestResource(path = "expenses") @Query("SELECT t FROM Transaction t INNER JOIN t.account a WHERE a.id = :accountId AND t.creditor.id = :userId ORDER BY t.date DESC") Page getTransactionsForCreditorByAccount(@Param("accountId") Long accountId, @Param("userId") Long userId, Pageable pageable); diff --git a/src/main/java/de/tilman/transactions/repository/WithdrawalRepository.java b/src/main/java/de/tilman/transactions/repository/WithdrawalRepository.java index 6aa9b92..e99bfa2 100644 --- a/src/main/java/de/tilman/transactions/repository/WithdrawalRepository.java +++ b/src/main/java/de/tilman/transactions/repository/WithdrawalRepository.java @@ -12,7 +12,7 @@ import de.tilman.transactions.domain.Withdrawal; public interface WithdrawalRepository extends PagingAndSortingRepository { @RestResource(path = "forAccount") - @Query("SELECT w FROM Withdrawal w INNER JOIN w.account a WHERE a.id = :accountId AND w.user.id = :userId ORDER BY t.date DESC") + @Query("SELECT w FROM Withdrawal w INNER JOIN w.account a WHERE a.id = :accountId AND w.user.id = :userId ORDER BY w.date DESC") Page getWithdrawalsForUserByAccount(@Param("accountId") Long accountId, @Param("userId") Long userId, Pageable pageable); } diff --git a/src/main/resources/static/js/controllers.js b/src/main/resources/static/js/controllers.js index f60826b..5202ff7 100644 --- a/src/main/resources/static/js/controllers.js +++ b/src/main/resources/static/js/controllers.js @@ -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) { diff --git a/src/main/resources/static/partials/transaction-list.html b/src/main/resources/static/partials/transaction-list.html index cf33ec0..1c38842 100644 --- a/src/main/resources/static/partials/transaction-list.html +++ b/src/main/resources/static/partials/transaction-list.html @@ -34,8 +34,14 @@ Auslage: - + + + + + + + + @@ -58,8 +64,8 @@ -
- {{user.name}} {{user.outstandings}} €
+
+ {{user.name}} {{user.outstandings | number : 2}} €