188 lines
5.7 KiB
JavaScript
188 lines
5.7 KiB
JavaScript
var transactionControllers = angular.module('transactionControllers', ['ngResource']);
|
|
|
|
transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location', '$resource', function($scope, $location, $resource) {
|
|
|
|
$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('/transactions/search/last10', { accountId: $scope.account.id }).get();
|
|
|
|
transactions.$promise.then(function(data) {
|
|
$scope.transactions = transactions._embedded.transactions;
|
|
|
|
for (var i = 0; i < $scope.transactions.length; i++) {
|
|
$scope.transactions[i].category = $resource($scope.transactions[i]._links.category.href).get();
|
|
$scope.transactions[i].creditor = $resource($scope.transactions[i]._links.creditor.href).get();
|
|
}
|
|
|
|
});
|
|
|
|
var accountUsers = $resource($scope.account._links.users.href).get();
|
|
accountUsers.$promise.then(function(data) {
|
|
$scope.accountUsers = accountUsers._embedded.users;
|
|
});
|
|
|
|
}
|
|
|
|
$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++) {
|
|
if (userLabels[i].innerHTML.substr(-$scope.userinfo.name.length) === $scope.userinfo.name) {
|
|
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()');
|
|
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;
|
|
console.log(newWithdrawal);
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
// via Resource (might be moved into a service)
|
|
var Transaction = $resource('/transactions');
|
|
|
|
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.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);
|
|
};
|
|
|
|
|
|
// initialize (http://stackoverflow.com/a/25662066/3761783)
|
|
$scope.initList();
|
|
|
|
} ]);
|
|
|
|
transactionControllers.controller('TransactionDetailCtrl', [ '$scope', '$routeParams', '$resource', '$location', function($scope, $routeParams, $resource, $location) {
|
|
var transaction = $resource($routeParams.transactionURI).get();
|
|
transaction.$promise.then(function(data) {
|
|
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;
|
|
|
|
$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('/');
|
|
});
|
|
}
|
|
|
|
} ]);
|
|
|