2016-10-09 20:17:11 +02:00
|
|
|
// vim: set tw=80 ts=4 sw=4 sts=4:
|
2016-04-12 10:55:08 +02:00
|
|
|
/*
|
|
|
|
This file is part of Accountant.
|
|
|
|
|
|
|
|
Accountant is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Accountant is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2017-06-10 18:02:19 +02:00
|
|
|
/* jshint node: true */
|
2016-04-12 10:55:08 +02:00
|
|
|
'use strict';
|
|
|
|
|
2017-06-14 22:21:25 +02:00
|
|
|
var moment = require('moment');
|
2017-06-10 18:02:19 +02:00
|
|
|
|
|
|
|
var angular = require('angular');
|
|
|
|
|
2017-06-10 22:06:36 +02:00
|
|
|
var operationFormTmpl = require('./operation.form.tmpl.html');
|
2017-06-10 18:02:19 +02:00
|
|
|
|
|
|
|
var ngResource = require('angular-resource'),
|
|
|
|
ngMessages = require('angular-messages'),
|
|
|
|
ngUiNotification = require('angular-ui-notification'),
|
|
|
|
ngBootbox = require('ngbootbox'),
|
|
|
|
ngStrap = require('angular-strap');
|
|
|
|
|
|
|
|
// Note: ngBootbox seems to have no module.exports.
|
|
|
|
ngBootbox = 'ngBootbox';
|
|
|
|
|
|
|
|
var operationModule = angular.module('accountant.operations', [
|
|
|
|
ngResource,
|
|
|
|
ngMessages,
|
|
|
|
ngUiNotification,
|
|
|
|
ngBootbox,
|
|
|
|
ngStrap
|
2016-04-12 10:55:08 +02:00
|
|
|
])
|
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
.config(function($resourceProvider) {
|
2016-04-12 10:55:08 +02:00
|
|
|
// Keep trailing slashes to avoid redirect by flask..
|
|
|
|
$resourceProvider.defaults.stripTrailingSlashes = false;
|
2016-10-14 08:19:23 +02:00
|
|
|
})
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
.factory('Operation', function($resource) {
|
2016-04-12 10:55:08 +02:00
|
|
|
return $resource(
|
|
|
|
'/api/operation/:id', {
|
|
|
|
id: '@id'
|
|
|
|
}
|
|
|
|
);
|
2016-10-14 08:19:23 +02:00
|
|
|
})
|
2016-04-12 10:55:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Controller for the operations.
|
|
|
|
*/
|
2017-06-14 22:14:40 +02:00
|
|
|
.controller('OperationController', function($rootScope, $scope, $routeParams,
|
|
|
|
$ngBootbox, Notification, Operation) {
|
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
var vm = this;
|
|
|
|
|
|
|
|
// List of operations.
|
|
|
|
vm.operations = [];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an empty operation.
|
|
|
|
*/
|
|
|
|
vm.add = function() {
|
|
|
|
var operation = new Operation({
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
account_id: $routeParams.accountId
|
|
|
|
});
|
2016-10-09 20:17:11 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
vm.operations.splice(0, 0, operation);
|
|
|
|
};
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
/*
|
|
|
|
* Load operations.
|
|
|
|
*/
|
|
|
|
vm.load = function(begin, end) {
|
|
|
|
vm.operations = Operation.query({
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
account_id: $routeParams.accountId,
|
|
|
|
begin: begin.format('YYYY-MM-DD'),
|
|
|
|
end: end.format('YYYY-MM-DD')
|
|
|
|
});
|
|
|
|
};
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
/*
|
|
|
|
* Cancel edition.
|
|
|
|
*/
|
|
|
|
vm.cancelEdit = function(operation, rowform, $index) {
|
|
|
|
if (operation.id) {
|
|
|
|
rowform.$cancel();
|
|
|
|
} else {
|
|
|
|
vm.operations.splice($index, 1);
|
|
|
|
}
|
|
|
|
};
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
/*
|
|
|
|
* Toggle pointed indicator for an operation.
|
|
|
|
*/
|
|
|
|
vm.togglePointed = function(operation, rowform) {
|
|
|
|
operation.pointed = !operation.pointed;
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
// Save operation if not editing it.
|
|
|
|
if (!rowform.$visible) {
|
|
|
|
vm.save(operation);
|
|
|
|
}
|
|
|
|
};
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
/*
|
|
|
|
* Toggle cancel indicator for an operation.
|
|
|
|
*/
|
|
|
|
vm.toggleCanceled = function(operation) {
|
|
|
|
operation.canceled = !operation.canceled;
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
vm.save(operation);
|
|
|
|
};
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
/*
|
|
|
|
* Save an operation and emit operationSavedEvent.
|
|
|
|
*/
|
|
|
|
vm.save = function($data, $index) {
|
|
|
|
// Check if $data is already a resource.
|
|
|
|
var operation;
|
|
|
|
|
|
|
|
if ($data.$save) {
|
|
|
|
operation = $data;
|
|
|
|
} else {
|
|
|
|
operation = vm.operations[$index];
|
|
|
|
operation = angular.merge(operation, $data);
|
|
|
|
}
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
operation.confirmed = true;
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
return operation.$save().then(function(data) {
|
|
|
|
Notification.success('Operation #' + data.id + ' saved.');
|
2016-05-24 08:49:55 +02:00
|
|
|
|
2016-10-14 08:51:35 +02:00
|
|
|
$scope.$emit('operationSavedEvent', data);
|
2016-10-14 08:19:23 +02:00
|
|
|
});
|
|
|
|
};
|
2016-05-24 08:49:55 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
/*
|
|
|
|
* Delete an operation and emit operationDeletedEvent.
|
|
|
|
*/
|
|
|
|
vm.delete = function(operation, $index) {
|
|
|
|
var id = operation.id;
|
2016-10-09 20:17:11 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
$ngBootbox.confirm(
|
|
|
|
'Voulez-vous supprimer l\'opération \\\'' + operation.label + '\\\' ?',
|
|
|
|
function(result) {
|
|
|
|
if (result) {
|
|
|
|
operation.$delete().then(function() {
|
|
|
|
Notification.success('Operation #' + id + ' deleted.');
|
2016-10-09 20:17:11 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
// Remove operation from array.
|
|
|
|
vm.operation.splice($index, 1);
|
2016-10-09 20:17:11 +02:00
|
|
|
|
2016-10-14 08:51:35 +02:00
|
|
|
$scope.$emit('operationDeletedEvent', operation);
|
2016-10-14 08:19:23 +02:00
|
|
|
});
|
2016-05-24 08:49:55 +02:00
|
|
|
}
|
2016-10-14 08:19:23 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2016-10-09 20:17:11 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
/*
|
|
|
|
* Reload operations on rangeSelectedEvent.
|
|
|
|
*/
|
|
|
|
vm.onRangeSelected = $rootScope.$on('rangeSelectedEvent', function(e, args) {
|
|
|
|
vm.load(args.begin, args.end);
|
|
|
|
});
|
|
|
|
|
2016-10-14 08:51:35 +02:00
|
|
|
$rootScope.$on('$destroy', function() {
|
2016-10-14 08:19:23 +02:00
|
|
|
vm.onRangeSelected = angular.noop;
|
|
|
|
});
|
2017-06-10 20:59:25 +02:00
|
|
|
|
|
|
|
vm.load(moment().date(1).year(2000), moment());
|
2016-10-14 08:19:23 +02:00
|
|
|
})
|
2016-10-09 20:17:11 +02:00
|
|
|
|
2016-10-14 08:19:23 +02:00
|
|
|
.directive('operationFormDialog', function($log, $ngBootbox) {
|
2016-10-09 20:17:11 +02:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
scope: {
|
|
|
|
operation: '=ngModel'
|
|
|
|
},
|
|
|
|
link: function(scope, element) {
|
|
|
|
var title = 'Operation';
|
|
|
|
|
|
|
|
if (scope.operation && scope.operation.id) {
|
|
|
|
title = title + ' #' + scope.operation.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.form = {};
|
|
|
|
|
|
|
|
element.on('click', function() {
|
|
|
|
scope.data = {};
|
|
|
|
angular.copy(scope.operation, scope.data);
|
|
|
|
|
|
|
|
// Open dialog with form.
|
|
|
|
$ngBootbox.customDialog({
|
|
|
|
scope: scope,
|
|
|
|
title: title,
|
2017-06-10 22:06:36 +02:00
|
|
|
templateUrl: operationFormTmpl,
|
2016-10-09 20:17:11 +02:00
|
|
|
onEscape: true,
|
|
|
|
buttons: {
|
|
|
|
save: {
|
|
|
|
label: 'Save',
|
|
|
|
className: 'btn-success',
|
|
|
|
callback: function() {
|
|
|
|
// Validate form
|
2016-10-14 08:19:23 +02:00
|
|
|
$log.log(scope.form);
|
2016-10-09 20:17:11 +02:00
|
|
|
|
|
|
|
// Save operation
|
2016-10-14 08:19:23 +02:00
|
|
|
$log.log(scope.operation);
|
2016-10-09 20:17:11 +02:00
|
|
|
|
|
|
|
// TODO Alexis Lahouze 2016-05-24 Save operation, handle return.
|
|
|
|
return false;
|
2016-05-24 08:49:55 +02:00
|
|
|
}
|
2016-10-09 20:17:11 +02:00
|
|
|
},
|
|
|
|
cancel: {
|
|
|
|
label: 'Cancel',
|
|
|
|
className: 'btn-default',
|
|
|
|
callback: true
|
2016-05-24 08:49:55 +02:00
|
|
|
}
|
2016-10-09 20:17:11 +02:00
|
|
|
}
|
2016-05-24 08:49:55 +02:00
|
|
|
});
|
2016-10-09 20:17:11 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2016-05-24 08:49:55 +02:00
|
|
|
});
|
2017-06-10 18:02:19 +02:00
|
|
|
|
|
|
|
module.exports = operationModule;
|