277 lines
7.2 KiB
JavaScript
277 lines
7.2 KiB
JavaScript
// vim: set tw=80 ts=4 sw=4 sts=4:
|
|
/*
|
|
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/>.
|
|
*/
|
|
/* jshint node: true */
|
|
'use strict';
|
|
|
|
var angular = require('angular');
|
|
|
|
var ngResource = require('angular-resource'),
|
|
ngMessages = require('angular-messages'),
|
|
ngUiBootstrap = require('angular-ui-bootstrap'),
|
|
ngUiNotification = require('angular-ui-notification');
|
|
|
|
var accountFormTmpl = require('./account.form.tmpl.html'),
|
|
accountDeleteTmpl = require('./account.delete.tmpl.html');
|
|
|
|
var accountModule = angular.module('accountant.accounts', [
|
|
ngResource,
|
|
ngMessages,
|
|
ngUiBootstrap,
|
|
ngUiNotification
|
|
])
|
|
|
|
.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'
|
|
}
|
|
);
|
|
|
|
Account.prototype.getBalances = function() {
|
|
var Balances = $resource('/api/account/:id/balances', {id: this.id});
|
|
|
|
this.balances = Balances.get();
|
|
};
|
|
|
|
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')
|
|
});
|
|
|
|
this.balance = Balance.get();
|
|
};
|
|
|
|
return Account;
|
|
})
|
|
|
|
.controller('AccountController', function(Account, Notification, $uibModal, $log, $q) {
|
|
var vm = this;
|
|
|
|
/*
|
|
* 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;
|
|
}
|
|
|
|
// eslint-disable-next-line camelcase
|
|
if (account.current < account.authorized_overdraft) {
|
|
return 'danger';
|
|
} else if (account.current < 0) {
|
|
return 'warning';
|
|
}
|
|
};
|
|
|
|
/*
|
|
* Return the class for a value compared to account authorized overdraft.
|
|
*/
|
|
vm.valueClass = function(account, value) {
|
|
if (!account || !value) {
|
|
return;
|
|
}
|
|
|
|
// eslint-disable-next-line camelcase
|
|
if (value < account.authorized_overdraft) {
|
|
return 'text-danger';
|
|
} else if (value < 0) {
|
|
return 'text-warning';
|
|
}
|
|
};
|
|
|
|
/*
|
|
* Add an empty account.
|
|
*/
|
|
vm.add = function() {
|
|
var account = new Account({
|
|
// eslint-disable-next-line camelcase
|
|
authorized_overdraft: 0
|
|
});
|
|
|
|
// Insert account at the begining of the array.
|
|
return vm.modify(account);
|
|
};
|
|
|
|
/*
|
|
* Save account.
|
|
*/
|
|
vm.save = function(account) {
|
|
return account.$save().then(function(data) {
|
|
Notification.success('Account #' + data.id + ' saved.');
|
|
|
|
vm.accounts = Account.query();
|
|
|
|
return data;
|
|
}, function(result){
|
|
$log.error('Error while saving account', account, result);
|
|
|
|
Notification.error(
|
|
'Error while saving account: ' + result.message
|
|
);
|
|
|
|
return $q.reject(result);
|
|
});
|
|
};
|
|
|
|
/*
|
|
* 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.');
|
|
|
|
vm.accounts = Account.query();
|
|
|
|
return account;
|
|
}, function(result) {
|
|
Notification.error(
|
|
'An error occurred while trying to delete account #' +
|
|
id + ':<br />' + result
|
|
);
|
|
|
|
return $q.reject(result);
|
|
});
|
|
}, function() {
|
|
return false;
|
|
});
|
|
};
|
|
|
|
/*
|
|
* 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;
|
|
});
|
|
};
|
|
|
|
// 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";
|
|
}
|
|
};
|
|
}
|
|
});
|
|
|
|
module.exports = accountModule;
|