accountant-ui/src/operations/operation.controller.ts

165 lines
4.3 KiB
TypeScript
Raw Normal View History

2017-07-30 09:21:50 +02:00
import { Component, Inject, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { ToastrService } from 'ngx-toastr';
2017-07-30 09:21:50 +02:00
var operationFormTmpl = require('./operation.form.tmpl.html'),
operationDeleteTmpl = require('./operation.delete.tmpl.html');
import { Account } from '../accounts/account';
2017-07-29 22:58:34 +02:00
import { AccountService } from '../accounts/account.service';
import { Operation } from './operation';
import { OperationService } from './operation.service';
2017-07-30 15:13:36 +02:00
@Component({
selector: 'operation-list',
template: `
<div>
<div class="row">
<div class="col-md-9">
<balance-chart (onUpdate)="onUpdate($event)"
[account]="account"></balance-chart>
</div>
<div class="col-md-3">
<category-chart
[minDate]="minDate"
[maxDate]="maxDate"
[account]="account"></category-chart>
</div>
</div>
<div class="row">
<table class="table table-striped table-condensed table-hover">
<thead>
<tr>
<th>Date d'op.</th>
<th>Libell&eacute; de l'op&eacute;ration</th>
<th>Montant</th>
<th>Solde</th>
<th>Cat&eacute;gorie</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="6">
<button class="btn btn-success" (click)="add()">
Ajouter
</button>
</td>
</tr>
<tr [operation-row]="operation"
[account]="account"
(needsReload)="load(minDate, maxDate)"
*ngFor="let operation of operations">
</tr>
</tbody>
</table>
</div>
</div>
`
})
export class OperationController implements OnInit {
2017-07-30 09:21:50 +02:00
private account: Account;
private minDate: Date;
private maxDate: Date;
private operations: Operation[];
constructor(
2017-07-30 15:13:36 +02:00
@Inject("$modal") private $modal,
@Inject("accountIdService") private accountIdService,
2017-07-30 09:21:50 +02:00
private toastrService: ToastrService,
private operationService: OperationService,
2017-07-30 15:13:36 +02:00
private accountService: AccountService
) {}
ngOnInit() {
this.accountService.get(this.accountIdService.get()).subscribe(account => {
2017-07-30 09:21:50 +02:00
this.account = account
});
}
/*
* Add an empty operation.
*/
2017-07-30 09:21:50 +02:00
add() {
var operation = new Operation();
2017-07-30 09:21:50 +02:00
operation.account_id = this.accountIdService.get();
2017-07-30 09:21:50 +02:00
return this.modify(operation);
};
/*
* Load operations.
*/
2017-07-30 09:21:50 +02:00
load(minDate, maxDate) {
this.minDate = minDate;
this.maxDate = maxDate;
2017-07-30 09:21:50 +02:00
return this.operationService.query(
this.accountIdService.get(),
minDate,
maxDate
).subscribe((operations: Operation[]) => {
2017-07-30 15:13:36 +02:00
this.operations = operations.reverse();
});
};
/*
* Save an operation and return a promise.
*/
2017-07-30 09:21:50 +02:00
save(operation) {
operation.confirmed = true;
var observable: Observable<Operation>;
if(operation.id){
2017-07-30 09:21:50 +02:00
observable = this.operationService.update(operation);
} else {
2017-07-30 09:21:50 +02:00
observable = this.operationService.create(operation);
}
return observable.subscribe((operation) => {
2017-07-30 09:21:50 +02:00
this.toastrService.success('Operation #' + operation.id + ' saved.');
2017-07-30 09:21:50 +02:00
this.load(this.minDate, this.maxDate);
return operation;
}, (result) => {
2017-07-30 09:21:50 +02:00
this.toastrService.error(
'Error while saving operation: ' + result.message
);
});
};
2017-07-30 09:21:50 +02:00
modify(operation) {
// FIXME Alexis Lahouze 2017-06-15 i18n
2017-07-30 15:13:36 +02:00
var title = "New operation";
2017-07-30 09:21:50 +02:00
this.$modal({
templateUrl: operationFormTmpl,
controller: function($scope, title, operation, $save) {
$scope.title = title;
$scope.operation = operation;
2017-07-30 09:21:50 +02:00
$scope.$save = () => {
$scope.$hide();
$save($scope.operation);
};
},
locals: {
title: title,
operation: operation,
2017-07-30 09:21:50 +02:00
$save: (operation: Operation) => {
this.save(operation);
}
}
});
};
2017-07-30 09:21:50 +02:00
onUpdate(dateRange) {
this.load(dateRange.minDate, dateRange.maxDate);
};
};