accountant-ui/src/operations/index.js

211 lines
6.3 KiB
JavaScript
Raw Normal View History

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');
var operationFormTmpl = require('./operation.form.tmpl.html'),
operationDeleteTmpl = require('./operation.delete.tmpl.html');
2017-06-10 18:02:19 +02:00
2017-07-02 00:13:04 +02:00
var balanceChartModule = require('./balance-chart.component.js'),
2017-07-06 10:39:14 +02:00
categoryChartModule = require('./category-chart.component.js'),
accountModule = require('../accounts');
2017-06-10 18:02:19 +02:00
var ngResource = require('angular-resource'),
ngMessages = require('angular-messages'),
ngUiNotification = require('angular-ui-notification'),
ngStrap = require('angular-strap');
module.exports = angular.module('accountant.operations', [
2017-06-10 18:02:19 +02:00
ngResource,
ngMessages,
ngUiNotification,
ngStrap,
2017-07-06 10:39:14 +02:00
accountModule,
balanceChartModule,
categoryChartModule
2016-04-12 10:55:08 +02:00
])
2017-07-05 21:48:20 +02:00
.config(function($resourceProvider) {
// Keep trailing slashes to avoid redirect by flask..
$resourceProvider.defaults.stripTrailingSlashes = false;
})
.factory('Operation', function($resource) {
return $resource(
'/api/operation/:id', {
id: '@id'
}
);
})
2016-04-12 10:55:08 +02:00
2016-10-14 08:19:23 +02:00
/*
2017-07-05 21:48:20 +02:00
* Controller for the operations.
2016-10-14 08:19:23 +02:00
*/
.controller('OperationController', function($routeParams, $modal,
2017-07-07 01:16:33 +02:00
Notification, Operation, Account) {
2016-04-12 10:55:08 +02:00
2017-07-05 21:48:20 +02:00
var vm = this;
2016-04-12 10:55:08 +02:00
2017-07-05 21:48:20 +02:00
/*
* Add an empty operation.
*/
vm.add = function() {
var operation = new Operation({
// eslint-disable-next-line camelcase
account_id: $routeParams.accountId
});
2016-04-12 10:55:08 +02:00
2017-07-05 21:48:20 +02:00
return vm.modify(operation);
};
2016-04-12 10:55:08 +02:00
2017-07-05 21:48:20 +02:00
/*
* Load operations.
*/
vm.load = function(minDate, maxDate) {
vm.minDate = minDate;
vm.maxDate = maxDate;
return Operation.query({
// eslint-disable-next-line camelcase
account_id: $routeParams.accountId,
begin: minDate ? moment(minDate).format('YYYY-MM-DD') : null,
end: maxDate ? moment(maxDate).format('YYYY-MM-DD') : null
});
};
2017-06-16 22:26:49 +02:00
2017-07-05 21:48:20 +02:00
/*
* Toggle pointed indicator for an operation.
*/
vm.togglePointed = function(operation, rowform) {
operation.pointed = !operation.pointed;
2017-06-16 22:26:49 +02:00
2017-07-05 21:48:20 +02:00
vm.save(operation);
};
2017-06-16 22:26:49 +02:00
2017-07-05 21:48:20 +02:00
/*
* Toggle cancel indicator for an operation.
*/
vm.toggleCanceled = function(operation) {
operation.canceled = !operation.canceled;
2016-05-24 08:49:55 +02:00
2017-07-05 21:48:20 +02:00
vm.save(operation);
};
2016-05-24 08:49:55 +02:00
2017-07-05 21:48:20 +02:00
/*
* Save an operation and return a promise.
2017-07-05 21:48:20 +02:00
*/
vm.save = function(operation) {
operation.confirmed = true;
2016-10-09 20:17:11 +02:00
2017-07-05 21:48:20 +02:00
return operation.$save().then(function(operation) {
Notification.success('Operation #' + operation.id + ' saved.');
2017-06-16 22:26:49 +02:00
vm.operations = vm.load();
2017-06-16 22:26:49 +02:00
return operation;
2017-07-05 21:48:20 +02:00
}, function(result){
2017-06-16 22:26:49 +02:00
Notification.error(
2017-07-05 21:48:20 +02:00
'Error while saving operation: ' + result.message
2017-06-16 22:26:49 +02:00
);
});
2017-07-05 21:48:20 +02:00
};
2016-10-09 20:17:11 +02:00
2017-07-05 21:48:20 +02:00
/*
* 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
}
});
};
2017-07-05 21:48:20 +02:00
vm.delete = function(operation) {
var id = operation.id;
return operation.$delete().then(function() {
Notification.success('Operation #' + id + ' deleted.');
vm.operations = vm.load();
return operation;
}, function(result) {
Notification.error(
'An error occurred while trying to delete operation #' +
id + ':<br />' + result
);
2016-10-09 20:17:11 +02:00
});
};
2017-07-05 21:48:20 +02:00
/*
* 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-05 21:48:20 +02:00
}
});
};
2017-07-05 21:48:20 +02:00
vm.onUpdate = function(minDate, maxDate) {
vm.operations = vm.load(minDate, maxDate);
};
2017-07-05 21:48:20 +02:00
vm.account = Account.get({id: $routeParams.accountId});
})
.name;