accountant-ui/src/accounts/account.component.ts
2017-07-14 21:38:29 +02:00

165 lines
4.5 KiB
TypeScript

import { Observable } from 'rxjs/Rx';
var accountFormTmpl = require('./account.form.tmpl.html'),
accountDeleteTmpl = require('./account.delete.tmpl.html');
import { Account } from './account';
export class AccountComponent {
static $inject = ['AccountService', 'AccountBalances', 'Notification', '$log', '$modal'];
accounts: Account[];
constructor(private AccountService, private AccountBalances, private Notification, private $log, private $modal) {
// Load accounts.
this.load();
}
/*
* Return the class for an account current value compared to authorized
* overdraft.
*/
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.
*/
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';
}
};
load() {
this.AccountService.query().subscribe(accounts => {
this.accounts = accounts.map((account: Account) => {
account.balances = this.AccountBalances.get({id: account.id});
return account;
})
});
};
/*
* Add an empty account.
*/
add() {
return this.modify(new Account());
};
/*
* Save account.
*/
save(account) {
var observable: Observable<Account>;
if(account.id) {
observable = this.AccountService.update(account);
} else {
observable = this.AccountService.create(account);
}
observable.subscribe(account => {
this.Notification.success('Account #' + account.id + ' saved.');
this.load();
}, result => {
this.$log.error('Error while saving account', account, result);
this.Notification.error(
'Error while saving account: ' + result.message
);
});
};
confirmDelete(account) {
var title = "Delete account #" + account.id;
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,
$delete: (account: Account) => { this.delete(account); }
}
});
};
/*
* Delete an account.
*/
delete(account) {
var id = account.id;
this.AccountService.delete(account).subscribe(account => {
this.Notification.success('account #' + id + ' deleted.');
this.load();
return account;
}, function(result) {
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.
*/
modify(account) {
// FIXME Alexis Lahouze 2017-06-15 i18n
var title = "Account";
if (account.id) {
title = title + " #" + account.id;
}
this.$modal({
templateUrl: accountFormTmpl,
controller: function($log, $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,
$save: (account: Account) => { this.save(account); }
}
});
};
};