2017-07-14 10:24:46 +02:00
|
|
|
import { Observable } from 'rxjs/Rx';
|
|
|
|
|
2017-07-16 22:25:50 +02:00
|
|
|
import { Logger } from '@nsalaun/ng-logger';
|
2017-07-20 10:32:05 +02:00
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
|
2017-07-21 00:29:03 +02:00
|
|
|
import { ToastrService } from 'ngx-toastr';
|
2017-07-08 09:19:02 +02:00
|
|
|
|
2017-07-14 10:24:46 +02:00
|
|
|
import { Account } from './account';
|
2017-07-16 17:03:37 +02:00
|
|
|
import { AccountBalances } from './accountBalances';
|
|
|
|
import { AccountService } from './account.service';
|
|
|
|
import { AccountBalancesService } from './accountBalances.service';
|
2017-07-20 10:32:05 +02:00
|
|
|
import { AccountDeleteModalComponent } from './accountDeleteModal.component';
|
|
|
|
import { AccountEditModalComponent } from './accountEditModal.component';
|
2017-07-20 23:00:52 +02:00
|
|
|
export class AccountListComponent {
|
2017-07-21 00:29:03 +02:00
|
|
|
static $inject = [
|
|
|
|
'AccountService',
|
|
|
|
'AccountBalancesService',
|
|
|
|
'ToastrService',
|
|
|
|
'Logger',
|
|
|
|
'NgbModal'
|
|
|
|
];
|
2017-07-13 17:14:20 +02:00
|
|
|
|
|
|
|
accounts: Account[];
|
|
|
|
|
2017-07-16 17:03:37 +02:00
|
|
|
constructor(
|
|
|
|
private AccountService: AccountService,
|
|
|
|
private AccountBalancesService: AccountBalancesService,
|
2017-07-21 00:29:03 +02:00
|
|
|
private ToastrService: ToastrService,
|
2017-07-16 22:25:50 +02:00
|
|
|
private Logger: Logger,
|
2017-07-20 10:32:05 +02:00
|
|
|
private NgbModal: NgbModal
|
2017-07-16 17:03:37 +02:00
|
|
|
) {
|
2017-07-13 17:14:20 +02:00
|
|
|
// Load accounts.
|
|
|
|
this.load();
|
|
|
|
}
|
2017-07-08 09:19:02 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the class for an account current value compared to authorized
|
|
|
|
* overdraft.
|
|
|
|
*/
|
2017-07-13 17:14:20 +02:00
|
|
|
rowClass(account) {
|
2017-07-08 09:19:02 +02:00
|
|
|
// 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) {
|
2017-07-08 09:19:02 +02:00
|
|
|
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() {
|
2017-07-14 10:24:46 +02:00
|
|
|
this.AccountService.query().subscribe(accounts => {
|
2017-07-14 10:25:33 +02:00
|
|
|
this.accounts = accounts.map((account: Account) => {
|
2017-07-16 22:25:50 +02:00
|
|
|
this.Logger.log(account);
|
2017-07-16 17:03:37 +02:00
|
|
|
this.AccountBalancesService
|
|
|
|
.get(account.id)
|
|
|
|
.subscribe((accountBalances: AccountBalances) => {
|
|
|
|
account.balances = accountBalances;
|
|
|
|
})
|
2017-07-14 10:25:33 +02:00
|
|
|
return account;
|
2017-07-14 10:24:46 +02:00
|
|
|
})
|
2017-07-10 08:17:11 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-07-08 09:19:02 +02:00
|
|
|
/*
|
|
|
|
* Add an empty account.
|
|
|
|
*/
|
2017-07-13 17:14:20 +02:00
|
|
|
add() {
|
2017-07-14 10:15:07 +02:00
|
|
|
return this.modify(new Account());
|
2017-07-08 09:19:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save account.
|
|
|
|
*/
|
2017-07-13 17:14:20 +02:00
|
|
|
save(account) {
|
2017-07-14 10:24:46 +02:00
|
|
|
var observable: Observable<Account>;
|
2017-07-08 09:19:02 +02:00
|
|
|
|
2017-07-14 10:24:46 +02:00
|
|
|
if(account.id) {
|
|
|
|
observable = this.AccountService.update(account);
|
|
|
|
} else {
|
|
|
|
observable = this.AccountService.create(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
observable.subscribe(account => {
|
2017-07-21 00:29:03 +02:00
|
|
|
this.ToastrService.success('Account #' + account.id + ' saved.');
|
2017-07-08 09:19:02 +02:00
|
|
|
|
2017-07-14 10:24:46 +02:00
|
|
|
this.load();
|
|
|
|
}, result => {
|
2017-07-16 22:25:50 +02:00
|
|
|
this.Logger.error('Error while saving account', account, result);
|
2017-07-08 09:19:02 +02:00
|
|
|
|
2017-07-21 00:29:03 +02:00
|
|
|
this.ToastrService.error(
|
2017-07-08 09:19:02 +02:00
|
|
|
'Error while saving account: ' + result.message
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-07-13 17:14:20 +02:00
|
|
|
confirmDelete(account) {
|
2017-07-20 10:32:05 +02:00
|
|
|
const modal = this.NgbModal.open(AccountDeleteModalComponent, {
|
|
|
|
windowClass: 'in'
|
|
|
|
});
|
|
|
|
|
|
|
|
modal.componentInstance.account = account;
|
|
|
|
|
|
|
|
modal.result.then((account: Account) => {
|
|
|
|
this.delete(account);
|
2017-07-21 00:33:27 +02:00
|
|
|
}, (reason) => function(reason) {
|
2017-07-08 09:19:02 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete an account.
|
|
|
|
*/
|
2017-07-13 17:14:20 +02:00
|
|
|
delete(account) {
|
2017-07-08 09:19:02 +02:00
|
|
|
var id = account.id;
|
|
|
|
|
2017-07-14 10:24:46 +02:00
|
|
|
this.AccountService.delete(account).subscribe(account => {
|
2017-07-21 00:29:03 +02:00
|
|
|
this.ToastrService.success('account #' + id + ' deleted.');
|
2017-07-08 09:19:02 +02:00
|
|
|
|
2017-07-13 17:14:20 +02:00
|
|
|
this.load();
|
2017-07-08 09:19:02 +02:00
|
|
|
|
|
|
|
return account;
|
|
|
|
}, function(result) {
|
2017-07-21 00:29:03 +02:00
|
|
|
this.ToastrService.error(
|
2017-07-08 09:19:02 +02:00
|
|
|
'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) {
|
2017-07-20 10:32:05 +02:00
|
|
|
const modal = this.NgbModal.open(AccountEditModalComponent, {
|
|
|
|
windowClass: 'in'
|
|
|
|
});
|
2017-07-08 09:19:02 +02:00
|
|
|
|
2017-07-20 10:32:05 +02:00
|
|
|
modal.componentInstance.account = account;
|
2017-07-08 09:19:02 +02:00
|
|
|
|
2017-07-20 10:32:05 +02:00
|
|
|
modal.result.then((account: Account) => {
|
|
|
|
this.Logger.log("Modal closed => save account", account);
|
|
|
|
this.save(account);
|
2017-07-21 00:33:27 +02:00
|
|
|
}, (reason) => function(reason) {
|
2017-07-08 09:19:02 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|