2017-07-30 13:15:49 +02:00
|
|
|
// vim: set tw=80 ts=2 sw=2 sts=2 :
|
|
|
|
import { CurrencyPipe } from '@angular/common';
|
|
|
|
import { Component, Inject, Input, Output, EventEmitter } from '@angular/core';
|
|
|
|
|
2017-07-30 15:35:26 +02:00
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
|
2017-07-30 13:15:49 +02:00
|
|
|
import { ToastrService } from 'ngx-toastr';
|
|
|
|
|
2017-07-30 15:35:26 +02:00
|
|
|
var operationFormTmpl = require('./operation.form.tmpl.html');
|
2017-07-30 13:15:49 +02:00
|
|
|
|
|
|
|
import { Account } from '../accounts/account';
|
|
|
|
import { Operation } from './operation';
|
|
|
|
import { OperationService } from './operation.service';
|
2017-07-30 15:35:26 +02:00
|
|
|
import { OperationDeleteModalComponent } from './operationDeleteModal.component';
|
2017-07-30 13:15:49 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'tr[operation-row]',
|
|
|
|
host: {
|
|
|
|
"[id]": "operation.id",
|
|
|
|
"[class.stroke]": "operation.canceled",
|
|
|
|
"[class.italic]": "!operation.confirmed",
|
2017-07-30 15:12:57 +02:00
|
|
|
"[class.warning]": "operation.balance < 0",
|
|
|
|
"[class.danger]": "operation.balance < account.authorized_overdraft"
|
2017-07-30 13:15:49 +02:00
|
|
|
},
|
|
|
|
template: `
|
2017-07-30 15:34:12 +02:00
|
|
|
<td>{{ operation.id }}</td>
|
|
|
|
|
2017-07-30 13:15:49 +02:00
|
|
|
<td>{{ operation.operation_date | date:"yyyy-MM-dd" }}</td>
|
|
|
|
|
|
|
|
<td>{{ operation.label }}</td>
|
|
|
|
|
2017-07-30 15:35:46 +02:00
|
|
|
<td>{{ operation.value | currency:'EUR':true }}</td>
|
2017-07-30 13:15:49 +02:00
|
|
|
|
|
|
|
<td [class.text-warning]="operation.balance < 0"
|
|
|
|
[class.text-danger]="operation.balance < account.authorized_overdraft">
|
|
|
|
{{ operation.balance | currency:'EUR':true }}
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td>{{ operation.category }}</td>
|
|
|
|
|
|
|
|
<td>
|
|
|
|
<div class="btn-group btn-group-sm">
|
|
|
|
<!-- Edit operation, for non-canceled operation. -->
|
|
|
|
<button type="button" class="btn btn-success"
|
2017-07-30 15:12:57 +02:00
|
|
|
*ngIf="!operation.canceled"
|
2017-07-30 13:15:49 +02:00
|
|
|
(click)="modify(operation)" title="edit">
|
|
|
|
<span class="fa fa-pencil-square-o"></span>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<!-- Toggle pointed operation, for non-canceled operations. -->
|
|
|
|
<button type="button" class="btn btn-secondary"
|
2017-07-30 15:12:57 +02:00
|
|
|
*ngIf="!operation.canceled"
|
2017-07-30 13:15:49 +02:00
|
|
|
(click)="togglePointed(operation)"
|
|
|
|
[class.active]="operation.pointed" title="point">
|
|
|
|
<span class="fa" [class.fa-check-square-o]="operation.pointed"
|
|
|
|
[class.fa-square-o]="!operation.pointed"></span>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<!-- Toggle canceled operation. -->
|
|
|
|
<button type="button" class="btn btn-warning"
|
|
|
|
(click)="toggleCanceled(operation)"
|
2017-07-30 15:12:57 +02:00
|
|
|
*ngIf="operation.scheduled_operation_id"
|
2017-07-30 13:15:49 +02:00
|
|
|
[class.active]="operation.canceled" title="cancel">
|
|
|
|
<span class="fa fa-remove"></span>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<!-- Delete operation, with confirm. -->
|
|
|
|
<button type="button" class="btn btn-danger"
|
2017-07-30 15:12:57 +02:00
|
|
|
(click)="confirmDelete(operation)"
|
|
|
|
*ngIf="operation.id && !operation.scheduled_operation_id">
|
2017-07-30 13:15:49 +02:00
|
|
|
<span class="fa fa-trash-o"></span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
export class OperationRowComponent {
|
2017-07-30 15:12:57 +02:00
|
|
|
@Input('operation-row') operation: Operation;
|
|
|
|
@Input() account: Account;
|
2017-07-30 13:15:49 +02:00
|
|
|
@Output() needsReload: EventEmitter<void> = new EventEmitter<void>();
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private operationService: OperationService,
|
|
|
|
private toastrService: ToastrService,
|
2017-07-30 15:35:26 +02:00
|
|
|
private modal: NgbModal,
|
2017-07-30 13:15:49 +02:00
|
|
|
@Inject('$modal') private $modal,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
togglePointed(operation, rowform) {
|
|
|
|
operation.pointed = !operation.pointed;
|
|
|
|
|
|
|
|
this.save(operation);
|
|
|
|
};
|
|
|
|
|
|
|
|
toggleCanceled(operation) {
|
|
|
|
operation.canceled = !operation.canceled;
|
|
|
|
|
|
|
|
this.save(operation);
|
|
|
|
};
|
|
|
|
|
|
|
|
save(operation) {
|
2017-07-30 15:36:57 +02:00
|
|
|
operation.confirmed = true;
|
2017-07-30 13:15:49 +02:00
|
|
|
|
2017-07-30 15:36:57 +02:00
|
|
|
return this.operationService.update(operation).subscribe((operation) => {
|
2017-07-30 13:15:49 +02:00
|
|
|
this.toastrService.success('Operation #' + operation.id + ' saved.');
|
|
|
|
|
|
|
|
this.needsReload.emit();
|
|
|
|
}, (result) => {
|
|
|
|
this.toastrService.error(
|
|
|
|
'Error while saving operation: ' + result.message
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
confirmDelete(operation) {
|
2017-07-30 15:35:26 +02:00
|
|
|
const modal = this.modal.open(OperationDeleteModalComponent);
|
2017-07-30 13:15:49 +02:00
|
|
|
|
2017-07-30 15:35:26 +02:00
|
|
|
modal.componentInstance.operation = this.operation;
|
|
|
|
|
|
|
|
var id = operation.id;
|
|
|
|
|
|
|
|
modal.result.then((operation: Operation) => {
|
|
|
|
this.delete(operation);
|
|
|
|
}, (reason) => {
|
|
|
|
})
|
2017-07-30 13:15:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
delete(operation) {
|
|
|
|
var id = operation.id;
|
|
|
|
|
|
|
|
return this.operationService.delete(operation).subscribe(() => {
|
|
|
|
this.toastrService.success('Operation #' + id + ' deleted.');
|
|
|
|
|
|
|
|
this.needsReload.emit();
|
|
|
|
}, (result) => {
|
|
|
|
this.toastrService.error(
|
|
|
|
'An error occurred while trying to delete operation #' +
|
|
|
|
id + ':<br />' + result
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
modify(operation) {
|
|
|
|
// FIXME Alexis Lahouze 2017-06-15 i18n
|
|
|
|
var title = "Modify operation #" + operation.id;
|
|
|
|
|
|
|
|
this.$modal({
|
|
|
|
templateUrl: operationFormTmpl,
|
|
|
|
controller: function($scope, title, operation, $save) {
|
|
|
|
$scope.title = title;
|
|
|
|
$scope.operation = operation;
|
|
|
|
$scope.$save = () => {
|
|
|
|
$scope.$hide();
|
|
|
|
$save($scope.operation);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
locals: {
|
|
|
|
title: title,
|
|
|
|
operation: operation,
|
|
|
|
$save: (operation: Operation) => {
|
|
|
|
this.save(operation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|