301 lines
10 KiB
JavaScript
301 lines
10 KiB
JavaScript
var transactionControllers = angular.module('transactionControllers', ['ngResource']);
|
|
|
|
transactionControllers.controller('TransactionListCtrl', [ '$http', '$scope', '$location', '$resource', '$q', function($http, $scope, $location, $resource, $q) {
|
|
|
|
$scope.refreshList = function() {
|
|
|
|
console.log('refreshList()');
|
|
|
|
// reset form
|
|
if ($scope.form) {
|
|
$scope.form.$setPristine();
|
|
$scope.form.$setUntouched();
|
|
}
|
|
|
|
$scope.transaction = {
|
|
amount: undefined,
|
|
date: new Date()
|
|
};
|
|
|
|
if (typeof $scope.categories !== 'undefined') {
|
|
$scope.transaction.category = $scope.categories[0];
|
|
}
|
|
|
|
// get recent transactions
|
|
var transactions = $resource('http://localhost:8080/transactions/search/forAccount', { accountId: $scope.account.id, size: $scope.pageSize, sort: 'date,desc' }).get();
|
|
transactions.$promise.then(function(data) {
|
|
$scope.transactions = transactions._embedded.transactions;
|
|
$scope.nextPageAvailable = (transactions.page.totalPages !== transactions.page.number + 1);
|
|
$scope.prevPageAvailable = (transactions.page.number > 0);
|
|
$scope.pageNumber = transactions.page.number;
|
|
});
|
|
|
|
var accountUsers = $resource($scope.account._links.users.href).get();
|
|
accountUsers.$promise.then(function(data) {
|
|
$scope.accountUsers = accountUsers._embedded.users;
|
|
|
|
// 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/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 expenseSum = 0;
|
|
for (j = 0; j < data[i]._embedded.transactions.length; j++) {
|
|
expenseSum = expenseSum + data[i]._embedded.transactions[j].amount;
|
|
}
|
|
$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() {
|
|
|
|
var radioButtons = document.getElementsByName('expenseBy');
|
|
var userLabels = document.getElementsByClassName('userLabel');
|
|
|
|
// if this is selected to be a withdrawal, preselect the current user and lock the radio buttons
|
|
if ($scope.transaction.category.id == -1) {
|
|
for (i = 0; i < userLabels.length; i++) {
|
|
console.log(userLabels[i].innerHTML.substr(-$scope.userinfo.name.length-6));
|
|
if (userLabels[i].innerHTML.indexOf($scope.userinfo.name) >= 0) {
|
|
userLabels[i].getElementsByTagName('INPUT')[0].checked = true;
|
|
}
|
|
}
|
|
for (i = 0; i < radioButtons.length; i++) {
|
|
radioButtons[i].disabled = true;
|
|
}
|
|
}
|
|
else {
|
|
for (i = 0; i < radioButtons.length; i++) {
|
|
radioButtons[i].disabled = false;
|
|
}
|
|
radioButtons[0].checked = true;
|
|
}
|
|
}
|
|
|
|
$scope.initList = function() {
|
|
console.log('initList()');
|
|
|
|
$scope.pageSize = 20;
|
|
|
|
var userinfo = $resource('/user').get();
|
|
userinfo.$promise.then(function(data) {
|
|
$scope.userid = userinfo.name;
|
|
|
|
userinfo = $resource('/users/search/findByUsername', { username: $scope.userid }).get();
|
|
userinfo.$promise.then(function(data) {
|
|
$scope.userinfo = userinfo;
|
|
});
|
|
|
|
var accountInfo = $resource('/accounts/search/findByUserName', { username: $scope.userid }).get();
|
|
accountInfo.$promise.then(function(data) {
|
|
$scope.accounts = accountInfo._embedded.accounts;
|
|
$scope.account = $scope.accounts[0];
|
|
|
|
var categories = $resource('/categories/search/listForAccount', { accountId: $scope.accounts[0].id }).get();
|
|
categories.$promise.then(function(data) {
|
|
$scope.categories = categories._embedded.categories;
|
|
|
|
//$scope.categories.push({id: -1, name: '────────────────', position: 9999, disabled: true});
|
|
$scope.categories.push({id: -1, name: 'private Entnahme', position: 10000, _links: { self: {href: 'withdrawal'}}});
|
|
|
|
$scope.refreshList();
|
|
});
|
|
});
|
|
});
|
|
|
|
}
|
|
|
|
$scope.postTransaction = function() {
|
|
|
|
var amount = $scope.transaction.amount.replace(',', '.');
|
|
|
|
if (amount.charAt(0) !== '+' && amount.charAt(0) !== '-') {
|
|
amount = '-' + amount;
|
|
}
|
|
|
|
// handle withdrawals
|
|
if ($scope.transaction.category._links.self.href === 'withdrawal') {
|
|
if (amount.charAt(0) === '-') {
|
|
amount = amount.substr(1);
|
|
}
|
|
|
|
var Withdrawal = $resource('/withdrawals');
|
|
|
|
var newWithdrawal = new Withdrawal({
|
|
"account" : $scope.account._links.self.href,
|
|
"amount" : amount,
|
|
"date" : $scope.transaction.date,
|
|
"user" : $scope.userinfo._links.self.href
|
|
});
|
|
|
|
newWithdrawal.$save(function(withdrawal) {
|
|
$scope.refreshList();
|
|
$scope.lastSubmission = newWithdrawal;
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
// 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,
|
|
"amount" : amount,
|
|
"date" : $scope.transaction.date,
|
|
"description" : $scope.transaction.description,
|
|
"category" : $scope.transaction.category._links.self.href,
|
|
"creditor" : $scope.transaction.creditor
|
|
});
|
|
|
|
newTransaction.$save(function(transaction) {
|
|
$scope.refreshList();
|
|
$scope.lastSubmission = newTransaction;
|
|
});
|
|
|
|
};
|
|
|
|
$scope.deleteLastSubmission = function() {
|
|
console.log($scope.lastSubmission._links.self.href);
|
|
var testTrns = $resource($scope.lastSubmission._links.self.href).delete(function() {
|
|
$scope.lastSubmission = undefined;
|
|
$scope.refreshList();
|
|
});
|
|
};
|
|
|
|
$scope.showTransaction = function(transaction) {
|
|
$location.path('/transaction/' + transaction._links.self.href);
|
|
};
|
|
|
|
$scope.moreTransactions = function() {
|
|
if ($scope.nextPageAvailable) {
|
|
$scope.pageNumber++;
|
|
var transactions = $resource('http://localhost:8080/transactions/search/forAccount', { accountId: $scope.account.id, size: $scope.pageSize, sort: 'date,desc', page: $scope.pageNumber }).get();
|
|
transactions.$promise.then(function(data) {
|
|
$scope.transactions = $scope.transactions.concat(transactions._embedded.transactions);
|
|
$scope.nextPageAvailable = (transactions.page.totalPages !== transactions.page.number + 1);
|
|
$scope.prevPageAvailable = (transactions.page.number > 0);
|
|
$scope.pageNumber = transactions.page.number;
|
|
});
|
|
}
|
|
};
|
|
|
|
$scope.updateSuggestions = function(val) {
|
|
var suggestions = $resource('http://localhost:8080/transactions/search/descriptions', { accountId: $scope.account.id, prefix: val.toLowerCase() }).get();
|
|
return suggestions.$promise.then(function(data) {
|
|
var newSuggestions = new Array(suggestions._embedded.transactions.length);
|
|
for (i = 0; i < suggestions._embedded.transactions.length; i++) {
|
|
newSuggestions[i] = suggestions._embedded.transactions[i].description;
|
|
}
|
|
return newSuggestions;
|
|
});
|
|
};
|
|
|
|
// initialize (http://stackoverflow.com/a/25662066/3761783)
|
|
$scope.initList();
|
|
|
|
} ]);
|
|
|
|
transactionControllers.controller('TransactionDetailCtrl', [ '$scope', '$routeParams', '$resource', '$location', function($scope, $routeParams, $resource, $location) {
|
|
|
|
$scope.initDetails = function() {
|
|
console.log('initDetails()');
|
|
|
|
var transaction = $resource($routeParams.transactionURI).get();
|
|
transaction.$promise.then(function(data) {
|
|
transaction.date = new Date(transaction.date);
|
|
transaction.category = $resource(transaction._links.category.href).get();
|
|
transaction.creditor = $resource(transaction._links.creditor.href).get();
|
|
transaction.account = $resource(transaction._links.account.href).get();
|
|
|
|
});
|
|
|
|
$scope.transaction = transaction;
|
|
|
|
// FIXME
|
|
// TODO The rest of this function is *copied code* from TransactionListCtrl and desperately needs to be moved into a service
|
|
|
|
var userinfo = $resource('/user').get();
|
|
userinfo.$promise.then(function(data) {
|
|
$scope.userid = userinfo.name;
|
|
|
|
userinfo = $resource('/users/search/findByUsername', { username: $scope.userid }).get();
|
|
userinfo.$promise.then(function(data) {
|
|
$scope.userinfo = userinfo;
|
|
});
|
|
|
|
var accountInfo = $resource('/accounts/search/findByUserName', { username: $scope.userid }).get();
|
|
accountInfo.$promise.then(function(data) {
|
|
$scope.accounts = accountInfo._embedded.accounts;
|
|
$scope.account = $scope.accounts[0];
|
|
|
|
var categories = $resource('/categories/search/listForAccount', { accountId: $scope.accounts[0].id }).get();
|
|
categories.$promise.then(function(data) {
|
|
$scope.categories = categories._embedded.categories;
|
|
|
|
//$scope.categories.push({id: -1, name: '────────────────', position: 9999, disabled: true});
|
|
$scope.categories.push({id: -1, name: 'private Entnahme', position: 10000, _links: { self: {href: 'withdrawal'}}});
|
|
|
|
// FIXME Code from TransactionListCtrl.refreshList()
|
|
var accountUsers = $resource($scope.account._links.users.href).get();
|
|
accountUsers.$promise.then(function(data) {
|
|
$scope.accountUsers = accountUsers._embedded.users;
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
}
|
|
|
|
$scope.deleteTransaction = function(transaction) {
|
|
/*console.log(angular.toJson(transaction, false));*/
|
|
|
|
// TODO auf return code reagieren
|
|
$resource(transaction._links.self.href).remove().$promise.then(function() {
|
|
$location.path('/');
|
|
});
|
|
}
|
|
|
|
$scope.initDetails();
|
|
|
|
} ]);
|
|
|