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

182 lines
4.8 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 09:21:50 +02:00
export class OperationController {
private account: Account;
private minDate: Date;
private maxDate: Date;
private operations: Operation[];
constructor(
private $modal,
private accountIdService,
private toastrService: ToastrService,
private operationService: OperationService,
private AccountService: AccountService
){
AccountService.get(this.accountIdService.get()).subscribe(account => {
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 09:21:50 +02:00
this.operations = operations;
});
};
/*
* Toggle pointed indicator for an operation.
*/
2017-07-30 09:21:50 +02:00
togglePointed(operation, rowform) {
operation.pointed = !operation.pointed;
2017-07-30 09:21:50 +02:00
this.save(operation);
};
/*
* Toggle cancel indicator for an operation.
*/
2017-07-30 09:21:50 +02:00
toggleCanceled(operation) {
operation.canceled = !operation.canceled;
2017-07-30 09:21:50 +02:00
this.save(operation);
};
/*
* 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
);
});
};
/*
* Delete an operation and return a promise.
*/
2017-07-30 09:21:50 +02:00
confirmDelete(operation) {
var title = "Delete operation #" + operation.id;
2017-07-30 09:21:50 +02:00
this.$modal({
templateUrl: operationDeleteTmpl,
controller: function($scope, title, operation, $delete) {
$scope.title = title;
$scope.operation = operation;
2017-07-30 09:21:50 +02:00
$scope.$delete = () => {
$scope.$hide();
$delete($scope.operation);
};
},
locals: {
title: title,
operation: operation,
2017-07-30 09:21:50 +02:00
$delete: (operation: Operation) => {
this.delete(operation);
}
}
});
};
2017-07-30 09:21:50 +02:00
delete(operation) {
var id = operation.id;
2017-07-30 09:21:50 +02:00
return this.operationService.delete(operation).subscribe(() => {
this.toastrService.success('Operation #' + id + ' deleted.');
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(
'An error occurred while trying to delete operation #' +
id + ':<br />' + result
);
});
};
/*
* Open the popup to modify the operation, save it on confirm.
* @returns a promise.
*/
2017-07-30 09:21:50 +02:00
modify(operation) {
// FIXME Alexis Lahouze 2017-06-15 i18n
var title = "Operation";
if (operation.id) {
title = title + " #" + operation.id;
}
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);
};
};