accountant-ui/src/accounts/account.controller.ts

161 lines
4.3 KiB
TypeScript
Raw Normal View History

var accountFormTmpl = require('./account.form.tmpl.html'),
accountDeleteTmpl = require('./account.delete.tmpl.html');
2017-07-13 17:14:20 +02:00
export class AccountController {
static $inject = ['Account', 'AccountBalances', 'Notification', '$log', '$modal'];
accounts: Account[];
constructor(private Account, private AccountBalances, private Notification, private $log, private $modal) {
// Load accounts.
this.load();
}
/*
* Return the class for an account current value compared to authorized
* overdraft.
*/
2017-07-13 17:14:20 +02:00
rowClass(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.
*/
2017-07-13 17:14:20 +02:00
valueClass(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';
}
};
2017-07-13 17:14:20 +02:00
load() {
vm.accounts = Account.query({}, function(result) {
return result.map(function(item) {
2017-07-13 17:14:20 +02:00
item.balances = this.AccountBalances.get({id: item.id});
return item;
});
});
};
/*
* Add an empty account.
*/
2017-07-13 17:14:20 +02:00
add() {
var account = new Account({
// eslint-disable-next-line camelcase
authorized_overdraft: 0
});
// Insert account at the begining of the array.
2017-07-13 17:14:20 +02:00
return this.modify(account);
};
/*
* Save account.
*/
2017-07-13 17:14:20 +02:00
save(account) {
return account.$save().then(function(data) {
2017-07-13 17:14:20 +02:00
this.Notification.success('Account #' + data.id + ' saved.');
2017-07-13 17:14:20 +02:00
this.load();
return data;
}, function(result){
2017-07-13 17:14:20 +02:00
this.$log.error('Error while saving account', account, result);
2017-07-13 17:14:20 +02:00
this.Notification.error(
'Error while saving account: ' + result.message
);
});
};
2017-07-13 17:14:20 +02:00
confirmDelete(account) {
var title = "Delete account #" + account.id;
2017-07-13 17:14:20 +02:00
this.$modal({
templateUrl: accountDeleteTmpl,
controller: function($scope, title, account, $delete) {
$scope.title = title;
$scope.account = account;
$scope.$delete = function() {
$scope.$hide();
$delete($scope.account);
};
},
locals: {
title: title,
account: account,
2017-07-13 17:14:20 +02:00
$delete: this.delete
}
});
};
/*
* Delete an account.
*/
2017-07-13 17:14:20 +02:00
delete(account) {
var id = account.id;
return account.$delete().then(function() {
2017-07-13 17:14:20 +02:00
this.Notification.success('account #' + id + ' deleted.');
2017-07-13 17:14:20 +02:00
this.load();
return account;
}, function(result) {
2017-07-13 17:14:20 +02:00
this.Notification.error(
'An error occurred while trying to delete account #' +
id + ':<br />' + result
);
});
};
/*
* Open the popup to modify the account, save it on confirm.
*/
2017-07-13 17:14:20 +02:00
modify(account) {
// FIXME Alexis Lahouze 2017-06-15 i18n
var title = "Account";
if (account.id) {
title = title + " #" + account.id;
}
2017-07-13 17:14:20 +02:00
this.$modal({
templateUrl: accountFormTmpl,
controller: function($scope, title, account, $save) {
$scope.title = title;
$scope.account = account;
$scope.account.authorized_overdraft *= -1;
$scope.$save = function() {
$scope.$hide();
$scope.account.authorized_overdraft *= -1;
$save($scope.account);
};
},
locals: {
title: title,
account: account,
2017-07-13 17:14:20 +02:00
$save: this.save
}
});
};
};