164 lines
4.5 KiB
TypeScript
164 lines
4.5 KiB
TypeScript
/// <reference types="angular" />
|
|
/// <reference types="angular-ui-notification" />
|
|
|
|
//import accountFormTmpl from './account.form.tmpl.html';
|
|
//import accountDeleteTmpl from './account.delete.tmpl.html';
|
|
|
|
import IAccount from './account.factory';
|
|
|
|
export interface IAccountController {
|
|
rowClass(account: IAccount) : string;
|
|
valueClass(account: IAccount, value: number) : string;
|
|
add() : void;
|
|
save(account: IAccount) : void;
|
|
confirmDelete(account: IAccount): void;
|
|
delete(account: IAccount): void;
|
|
modify(account: IAccount): void;
|
|
};
|
|
|
|
export default class AccountController {
|
|
static $inject = ['AccountResource', 'Notification', '$log', '$modal'];
|
|
|
|
private accounts: IAccount[];
|
|
|
|
constructor(
|
|
public AccountResource: AccountResource,
|
|
public Notification: angular.uiNotification.INotificationService,
|
|
public $modal: mgcrea.ngStrap.modal.IModalService) {
|
|
|
|
// Load accounts.
|
|
this.accounts = Account.query();
|
|
};
|
|
|
|
/*
|
|
* Return the class for an account current value compared to authorized
|
|
* overdraft.
|
|
*/
|
|
rowClass(account : IAccount): string {
|
|
if (!account || !account.authorized_overdraft || !account.current) {
|
|
return '';
|
|
}
|
|
|
|
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: IAccount, value: number): string {
|
|
if (!account || !value) {
|
|
return '';
|
|
}
|
|
|
|
if (value < account.authorized_overdraft) {
|
|
return 'text-danger';
|
|
} else if (value < 0) {
|
|
return 'text-warning';
|
|
}
|
|
};
|
|
|
|
/*
|
|
* Add an empty account.
|
|
*/
|
|
add(): void {
|
|
var account = new Account({
|
|
// eslint-disable-next-line camelcase
|
|
authorized_overdraft: 0
|
|
});
|
|
|
|
// Insert account at the begining of the array.
|
|
this.modify(account);
|
|
};
|
|
|
|
/*
|
|
* Save account.
|
|
*/
|
|
save(account: IAccount): void {
|
|
return account.$save().then(function(data) {
|
|
this.Notification.success('Account #' + data.id + ' saved.');
|
|
|
|
this.accounts = Account.query();
|
|
}, function(result){
|
|
$log.error('Error while saving account', account, result);
|
|
|
|
this.Notification.error(
|
|
'Error while saving account: ' + result.message
|
|
);
|
|
});
|
|
};
|
|
|
|
confirmDelete(account: IAccount): void {
|
|
var title = "Delete account #" + account.id;
|
|
|
|
$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: this.delete
|
|
}
|
|
});
|
|
};
|
|
|
|
/*
|
|
* Delete an account.
|
|
*/
|
|
delete(account: IAccount): void {
|
|
var id = account.id;
|
|
|
|
account.$delete().then(function() {
|
|
this.Notification.success('account #' + id + ' deleted.');
|
|
|
|
this.accounts = Account.query();
|
|
}, 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: IAccount): void {
|
|
// FIXME Alexis Lahouze 2017-06-15 i18n
|
|
var title = "Account";
|
|
|
|
if (account.id) {
|
|
title = title + " #" + account.id;
|
|
}
|
|
|
|
$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,
|
|
$save: this.save
|
|
}
|
|
});
|
|
};
|
|
};
|