accountant-ui/src/accounts/index.js

277 lines
7.9 KiB
JavaScript
Raw Normal View History

2016-10-09 20:17:11 +02:00
// vim: set tw=80 ts=4 sw=4 sts=4:
2016-04-12 10:55:08 +02:00
/*
This file is part of Accountant.
Accountant is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Accountant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
*/
2017-06-10 18:02:19 +02:00
/* jshint node: true */
2016-04-12 10:55:08 +02:00
'use strict';
2017-06-10 18:02:19 +02:00
var angular = require('angular');
var ngResource = require('angular-resource'),
ngMessages = require('angular-messages'),
ngUiBootstrap = require('angular-ui-bootstrap'),
ngUiNotification = require('angular-ui-notification');
2017-06-10 18:02:19 +02:00
var accountFormTmpl = require('./account.form.tmpl.html'),
accountDeleteTmpl = require('./account.delete.tmpl.html');
2017-06-10 18:02:19 +02:00
module.exports = angular.module('accountant.accounts', [
2017-06-10 18:02:19 +02:00
ngResource,
ngMessages,
ngUiBootstrap,
ngUiNotification
2016-04-12 10:55:08 +02:00
])
2017-07-05 21:48:20 +02:00
.config(function($resourceProvider) {
// Keep trailing slashes to avoid redirect by flask..
$resourceProvider.defaults.stripTrailingSlashes = false;
})
.factory('Account', function($resource) {
var Account = $resource(
'/api/account/:id', {
id: '@id'
}
);
2016-04-12 10:55:08 +02:00
2017-07-05 21:48:20 +02:00
Account.prototype.getBalances = function() {
var Balances = $resource('/api/account/:id/balances', {id: this.id});
2016-04-12 10:55:08 +02:00
2017-07-05 21:48:20 +02:00
this.balances = Balances.get();
};
2016-04-12 10:55:08 +02:00
2017-07-05 21:48:20 +02:00
Account.prototype.getBalance = function(begin, end) {
var Balance = $resource(
'/api/account/:id/balance', {
id: this.id,
begin: begin.format('YYYY-MM-DD'),
end: end.format('YYYY-MM-DD')
});
2016-04-12 10:55:08 +02:00
2017-07-05 21:48:20 +02:00
this.balance = Balance.get();
};
2016-10-12 23:32:54 +02:00
2017-07-05 21:48:20 +02:00
return Account;
})
2016-10-12 23:32:54 +02:00
2017-07-05 21:48:20 +02:00
.controller('AccountController', function(Account, Notification, $uibModal, $log, $q) {
var vm = this;
2016-04-12 10:55:08 +02:00
2017-07-05 21:48:20 +02:00
/*
* Return the class for an account current value compared to authorized
* overdraft.
*/
vm.rowClass = function(account) {
// eslint-disable-next-line camelcase
if (!account || !account.authorized_overdraft || !account.current) {
return;
}
2016-10-12 23:32:54 +02:00
2016-10-12 19:57:19 +02:00
// eslint-disable-next-line camelcase
2017-07-05 21:48:20 +02:00
if (account.current < account.authorized_overdraft) {
return 'danger';
} else if (account.current < 0) {
return 'warning';
2016-10-12 23:32:54 +02:00
}
2017-07-05 21:48:20 +02:00
};
2016-04-12 10:55:08 +02:00
2017-07-05 21:48:20 +02:00
/*
* Return the class for a value compared to account authorized overdraft.
*/
vm.valueClass = function(account, value) {
if (!account || !value) {
return;
}
2016-10-12 19:57:19 +02:00
2017-07-05 21:48:20 +02:00
// eslint-disable-next-line camelcase
if (value < account.authorized_overdraft) {
return 'text-danger';
} else if (value < 0) {
return 'text-warning';
}
};
2016-05-22 21:47:33 +02:00
2017-07-05 21:48:20 +02:00
/*
* Add an empty account.
*/
vm.add = function() {
var account = new Account({
// eslint-disable-next-line camelcase
authorized_overdraft: 0
});
2017-07-05 21:48:20 +02:00
// Insert account at the begining of the array.
return vm.modify(account);
2017-07-04 21:50:34 +02:00
};
2017-07-05 21:48:20 +02:00
/*
* Save account.
*/
vm.save = function(account) {
return account.$save().then(function(data) {
Notification.success('Account #' + data.id + ' saved.');
2017-07-05 21:48:20 +02:00
vm.accounts = Account.query();
2016-07-15 16:40:39 +02:00
2017-07-05 21:48:20 +02:00
return data;
}, function(result){
$log.error('Error while saving account', account, result);
Notification.error(
'Error while saving account: ' + result.message
);
return $q.reject(result);
});
};
2017-07-05 21:48:20 +02:00
/*
* Delete an account.
*/
vm.delete = function(account, $index) {
var id = account.id;
$uibModal.open({
component: 'accountDeleteModalComponent',
resolve: {
account: function() {
return account;
}
}
}).result.then(function(account) {
return account.$delete().then(function() {
Notification.success('account #' + id + ' deleted.');
2017-07-05 21:48:20 +02:00
vm.accounts = Account.query();
2017-07-05 21:48:20 +02:00
return account;
}, function(result) {
Notification.error(
'An error occurred while trying to delete account #' +
id + ':<br />' + result
);
return $q.reject(result);
});
}, function() {
return false;
});
};
2016-07-15 16:40:39 +02:00
2017-07-05 21:48:20 +02:00
/*
* Open the popup to modify the account, save it on confirm.
* @returns a promise.
*/
vm.modify = function(account) {
return $uibModal.open({
component: 'accountModifyModalComponent',
resolve: {
account: function() {
return account;
}
}
}).result.then(function(account) {
return vm.save(account);
}, function() {
return false;
2016-10-12 19:57:19 +02:00
});
};
2017-07-05 21:48:20 +02:00
// Load accounts.
vm.accounts = Account.query();
})
.component('accountModifyModalComponent', {
templateUrl: accountFormTmpl,
bindings: {
resolve: '<',
close: '&',
dismiss: '&'
},
controller: function() {
var vm = this;
vm.$onInit = function() {
vm.account = vm.resolve.account;
vm.authorized_overdraft = - vm.account.authorized_overdraft;
};
vm.authorizedOverdraftChange = function() {
vm.account.authorized_overdraft = - vm.authorized_overdraft;
};
vm.ok = function() {
vm.close({
$value: vm.account
});
};
vm.cancel = function() {
vm.dismiss({
$value: 'cancel'
});
};
vm.title = function() {
// FIXME Alexis Lahouze 2017-06-15 i18n
if (vm.account.id) {
return "Account #" + vm.account.id;
} else {
return "Account";
}
};
}
})
.component('accountDeleteModalComponent', {
templateUrl: accountDeleteTmpl,
bindings: {
resolve: '<',
close: '&',
dismiss: '&'
},
controller: function() {
var vm = this;
vm.$onInit = function() {
vm.account = vm.resolve.account;
};
vm.ok = function() {
vm.close({
$value: vm.account
});
};
vm.cancel = function() {
vm.dismiss({
$value: 'cancel'
});
};
vm.title = function() {
// FIXME Alexis Lahouze 2017-06-15 i18n
if (vm.account.id) {
return "Account #" + vm.account.id;
} else {
return "Account";
}
};
}
})
2017-06-10 18:02:19 +02:00
.name;