2017-07-07 23:12:48 +02:00
|
|
|
var operationFormTmpl = require('./operation.form.tmpl.html'),
|
|
|
|
operationDeleteTmpl = require('./operation.delete.tmpl.html');
|
|
|
|
|
2017-07-29 22:26:38 +02:00
|
|
|
import { Observable } from 'rxjs/Rx';
|
|
|
|
|
|
|
|
import { ToastrService } from 'ngx-toastr';
|
2017-07-07 22:52:27 +02:00
|
|
|
|
2017-07-29 22:26:38 +02:00
|
|
|
import { Operation } from './operation';
|
|
|
|
import { OperationService } from './operation.service';
|
|
|
|
|
|
|
|
module.exports = function(
|
2017-07-29 22:53:41 +02:00
|
|
|
$modal,
|
|
|
|
accountIdService,
|
2017-07-29 22:26:38 +02:00
|
|
|
toastrService: ToastrService,
|
|
|
|
operationService: OperationService,
|
|
|
|
AccountService
|
|
|
|
){
|
2017-07-07 22:52:27 +02:00
|
|
|
var vm = this;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an empty operation.
|
|
|
|
*/
|
|
|
|
vm.add = function() {
|
2017-07-29 22:26:38 +02:00
|
|
|
var operation = new Operation();
|
2017-07-29 22:53:41 +02:00
|
|
|
operation.account_id = accountIdService.get();
|
2017-07-07 22:52:27 +02:00
|
|
|
|
|
|
|
return vm.modify(operation);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load operations.
|
|
|
|
*/
|
|
|
|
vm.load = function(minDate, maxDate) {
|
|
|
|
vm.minDate = minDate;
|
|
|
|
vm.maxDate = maxDate;
|
|
|
|
|
2017-07-29 22:26:38 +02:00
|
|
|
return operationService.query(
|
2017-07-29 22:53:41 +02:00
|
|
|
accountIdService.get(),
|
2017-07-29 22:26:38 +02:00
|
|
|
minDate,
|
|
|
|
maxDate
|
|
|
|
).subscribe((operations: Operation[]) => {
|
|
|
|
vm.operations = operations;
|
2017-07-07 22:52:27 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Toggle pointed indicator for an operation.
|
|
|
|
*/
|
|
|
|
vm.togglePointed = function(operation, rowform) {
|
|
|
|
operation.pointed = !operation.pointed;
|
|
|
|
|
|
|
|
vm.save(operation);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Toggle cancel indicator for an operation.
|
|
|
|
*/
|
|
|
|
vm.toggleCanceled = function(operation) {
|
|
|
|
operation.canceled = !operation.canceled;
|
|
|
|
|
|
|
|
vm.save(operation);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save an operation and return a promise.
|
|
|
|
*/
|
|
|
|
vm.save = function(operation) {
|
|
|
|
operation.confirmed = true;
|
|
|
|
|
2017-07-29 22:26:38 +02:00
|
|
|
var observable: Observable<Operation>;
|
|
|
|
|
|
|
|
if(operation.id){
|
|
|
|
observable = operationService.update(operation);
|
|
|
|
} else {
|
|
|
|
observable = operationService.create(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return observable.subscribe((operation) => {
|
2017-07-25 22:08:18 +02:00
|
|
|
toastrService.success('Operation #' + operation.id + ' saved.');
|
2017-07-07 22:52:27 +02:00
|
|
|
|
2017-07-29 22:26:38 +02:00
|
|
|
vm.load();
|
2017-07-07 22:52:27 +02:00
|
|
|
|
|
|
|
return operation;
|
2017-07-29 22:26:38 +02:00
|
|
|
}, (result) => {
|
2017-07-25 22:08:18 +02:00
|
|
|
toastrService.error(
|
2017-07-07 22:52:27 +02:00
|
|
|
'Error while saving operation: ' + result.message
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete an operation and return a promise.
|
|
|
|
*/
|
|
|
|
vm.confirmDelete = function(operation) {
|
|
|
|
var title = "Delete operation #" + operation.id;
|
|
|
|
|
|
|
|
$modal({
|
|
|
|
templateUrl: operationDeleteTmpl,
|
|
|
|
controller: function($scope, title, operation, $delete) {
|
|
|
|
$scope.title = title;
|
|
|
|
$scope.operation = operation;
|
|
|
|
$scope.$delete = function() {
|
|
|
|
$scope.$hide();
|
|
|
|
$delete($scope.operation);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
locals: {
|
|
|
|
title: title,
|
|
|
|
operation: operation,
|
|
|
|
$delete: vm.delete
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
vm.delete = function(operation) {
|
|
|
|
var id = operation.id;
|
|
|
|
|
2017-07-29 22:26:38 +02:00
|
|
|
return operationService.delete(operation).subscribe(() => {
|
2017-07-25 22:08:18 +02:00
|
|
|
toastrService.success('Operation #' + id + ' deleted.');
|
2017-07-07 22:52:27 +02:00
|
|
|
|
2017-07-29 22:26:38 +02:00
|
|
|
vm.load();
|
2017-07-07 22:52:27 +02:00
|
|
|
|
|
|
|
return operation;
|
2017-07-29 22:26:38 +02:00
|
|
|
}, (result) => {
|
2017-07-25 22:08:18 +02:00
|
|
|
toastrService.error(
|
2017-07-07 22:52:27 +02:00
|
|
|
'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.
|
|
|
|
*/
|
|
|
|
vm.modify = function(operation) {
|
|
|
|
// FIXME Alexis Lahouze 2017-06-15 i18n
|
|
|
|
var title = "Operation";
|
|
|
|
|
|
|
|
if (operation.id) {
|
|
|
|
title = title + " #" + operation.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$modal({
|
|
|
|
templateUrl: operationFormTmpl,
|
|
|
|
controller: function($scope, title, operation, $save) {
|
|
|
|
$scope.title = title;
|
|
|
|
$scope.operation = operation;
|
|
|
|
$scope.$save = function() {
|
|
|
|
$scope.$hide();
|
|
|
|
$save($scope.operation);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
locals: {
|
|
|
|
title: title,
|
|
|
|
operation: operation,
|
|
|
|
$save: vm.save
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
vm.onUpdate = function(dateRange) {
|
2017-07-29 22:26:38 +02:00
|
|
|
vm.load(dateRange.minDate, dateRange.maxDate);
|
2017-07-07 22:52:27 +02:00
|
|
|
};
|
|
|
|
|
2017-07-29 22:53:41 +02:00
|
|
|
AccountService.get(accountIdService.get()).subscribe(account => {
|
2017-07-14 10:24:46 +02:00
|
|
|
vm.account = account
|
|
|
|
});
|
2017-07-07 22:52:27 +02:00
|
|
|
};
|