2938d08d5c
Conflicts: src/main/resources/static/js/controllers.js src/main/resources/static/partials/transaction-list.html
299 lines
10 KiB
JavaScript
299 lines
10 KiB
JavaScript
var transactionControllers = angular.module('transactionControllers', ['ngResource']);
|
|
|
|
function amountToNumber(amount) {
|
|
console.log("typeof amount: " + (typeof amount));
|
|
amount = amount.toString().replace(',', '.');
|
|
if (amount.charAt(0) !== '+' && amount.charAt(0) !== '-') {
|
|
amount = '-' + amount;
|
|
}
|
|
return amount;
|
|
}
|
|
|
|
transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location', '$resource', '$q', '$uibModal', '$log', 'Transaction', function($scope, $location, $resource, $q, $uibModal, $log, Transaction) {
|
|
|
|
$scope.refreshList = function() {
|
|
|
|
// reset form
|
|
if ($scope.form) {
|
|
$scope.form.$setPristine();
|
|
$scope.form.$setUntouched();
|
|
}
|
|
|
|
$scope.transaction = {
|
|
amount: undefined,
|
|
date: new Date(),
|
|
creditor: ""
|
|
};
|
|
|
|
if (typeof $scope.categories !== 'undefined') {
|
|
$scope.transaction.category = $scope.categories[0];
|
|
}
|
|
|
|
// get recent transactions
|
|
$log.info('get 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) {
|
|
$log.info(transactions._embedded.transactions);
|
|
$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);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
$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() {
|
|
$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 = amountToNumber($scope.transaction.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;
|
|
}
|
|
|
|
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() {
|
|
var testTrns = $resource($scope.lastSubmission._links.self.href).delete(function() {
|
|
$scope.lastSubmission = undefined;
|
|
$scope.refreshList();
|
|
});
|
|
};
|
|
|
|
$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;
|
|
});
|
|
};
|
|
|
|
$scope.openDetails = function(transactionId) {
|
|
var modalInstance = $uibModal.open({
|
|
templateUrl: 'detailsTemplate.html',
|
|
controller: 'DetailsModalCtrl',
|
|
resolve: {
|
|
transactionId: transactionId,
|
|
categories: function () {
|
|
return $scope.categories;
|
|
},
|
|
accountUsers: function () {
|
|
return $scope.accountUsers;
|
|
}
|
|
}
|
|
});
|
|
|
|
modalInstance.result.then(function(transaction) {
|
|
$scope.lastSubmission = undefined;
|
|
$scope.refreshList();
|
|
}, function () {
|
|
$log.info('Modal dismissed at: ' + new Date());
|
|
});
|
|
};
|
|
|
|
// initialize (http://stackoverflow.com/a/25662066/3761783)
|
|
$scope.initList();
|
|
|
|
} ]);
|
|
|
|
transactionControllers.controller('DetailsModalCtrl', [ '$scope', '$resource', '$uibModalInstance', 'Transaction', 'transactionId', 'categories', 'accountUsers', function($scope, $resource, $uibModalInstance, Transaction, transactionId, categories, accountUsers) {
|
|
|
|
$scope.accountUsers = accountUsers;
|
|
|
|
var transaction = Transaction.get({ id: transactionId });
|
|
transaction.$promise.then(function(data) {
|
|
transaction.date = new Date(transaction.date);
|
|
transaction.creditor = $resource(transaction._links.creditor.href).get();
|
|
transaction.category = $resource(transaction._links.category.href).get();
|
|
|
|
transaction.category.$promise.then(function(data) {
|
|
$scope.transaction = transaction;
|
|
$scope.categories = categories;
|
|
|
|
for (i = 0; i < $scope.categories.length; i++) {
|
|
if ($scope.categories[i].name == $scope.transaction.category.name) {
|
|
$scope.transaction.category = $scope.categories[i];
|
|
break;
|
|
}
|
|
}
|
|
|
|
transaction.creditor.$promise.then(function(data) {
|
|
$scope.transaction.creditor = $scope.transaction.creditor._links.user.href;
|
|
},
|
|
function(error) { // transactions without a creditor return a HTTP 404 error
|
|
$scope.transaction.creditor = "";
|
|
});
|
|
});
|
|
|
|
});
|
|
|
|
$scope.ok = function () {
|
|
// set URI for category and save via PUT request
|
|
$scope.transaction.category = $scope.transaction.category._links.self.href;
|
|
$scope.transaction.amount = amountToNumber($scope.transaction.amount);
|
|
$scope.transaction.$update(function() {});
|
|
|
|
$uibModalInstance.close($scope.transaction);
|
|
};
|
|
|
|
$scope.cancel = function () {
|
|
$uibModalInstance.dismiss('cancel');
|
|
};
|
|
|
|
$scope.deleteTransaction = function() {
|
|
// TODO auf return code reagieren
|
|
$resource($scope.transaction._links.self.href).remove().$promise.then(function() {
|
|
$uibModalInstance.close();
|
|
});
|
|
}
|
|
} ]);
|