accountant-ui/src/operations/index.js
2017-07-06 10:39:14 +02:00

272 lines
7.7 KiB
JavaScript

// vim: set tw=80 ts=4 sw=4 sts=4:
/*
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/>.
*/
/* jshint node: true */
'use strict';
var moment = require('moment');
var angular = require('angular');
var operationFormTmpl = require('./operation.form.tmpl.html'),
operationDeleteTmpl = require('./operation.delete.tmpl.html');
var balanceChartModule = require('./balance-chart.component.js'),
categoryChartModule = require('./category-chart.component.js'),
accountModule = require('../accounts');
var ngResource = require('angular-resource'),
ngMessages = require('angular-messages'),
ngUiBootstrap = require('angular-ui-bootstrap'),
ngUiNotification = require('angular-ui-notification'),
ngStrap = require('angular-strap');
module.exports = angular.module('accountant.operations', [
ngResource,
ngMessages,
ngUiBootstrap,
ngUiNotification,
ngStrap,
accountModule,
balanceChartModule,
categoryChartModule
])
.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'
}
);
})
/*
* Controller for the operations.
*/
.controller('OperationController', function($routeParams, $uibModal,
Notification, Operation, Account, $log, $q) {
var vm = this;
/*
* Add an empty operation.
*/
vm.add = function() {
var operation = new Operation({
// eslint-disable-next-line camelcase
account_id: $routeParams.accountId
});
return vm.modify(operation);
};
/*
* 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
});
};
/*
* 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;
return operation.$save().then(function(operation) {
Notification.success('Operation #' + operation.id + ' saved.');
vm.operations = vm.load();
return operation;
}, function(result){
$log.error('Error while saving operation', operation, result);
Notification.error(
'Error while saving operation: ' + result.message
);
return $q.reject(result);
});
};
/*
* Delete an operation and return a promise.
*/
vm.delete = function(operation) {
var id = operation.id;
$uibModal.open({
component: 'operationDeleteModalComponent',
resolve: {
operation: function() {
return operation;
}
}
}).result.then(function(operation) {
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
);
return $q.reject(result);
});
}, function() {
return false;
});
};
/*
* Open the popup to modify the operation, save it on confirm.
* @returns a promise.
*/
vm.modify = function(operation) {
return $uibModal.open({
component: 'operationModifyModalComponent',
resolve: {
operation: function() {
return operation;
}
}
}).result.then(function(operation) {
return vm.save(operation);
}, function() {
return false;
});
};
vm.onUpdate = function(minDate, maxDate) {
vm.operations = vm.load(minDate, maxDate);
};
vm.account = Account.get({id: $routeParams.accountId});
})
.component('operationModifyModalComponent', {
templateUrl: operationFormTmpl,
bindings: {
resolve: '<',
close: '&',
dismiss: '&'
},
controller: function() {
var vm = this;
vm.$onInit = function() {
vm.operation = vm.resolve.operation;
};
vm.ok = function() {
vm.close({
$value: vm.operation
});
};
vm.cancel = function() {
vm.dismiss({
$value: 'cancel'
});
};
vm.title = function() {
// FIXME Alexis Lahouze 2017-06-15 i18n
if (vm.operation.id) {
return "Operation #" + vm.operation.id;
} else {
return "Operation";
}
};
}
})
.component('operationDeleteModalComponent', {
templateUrl: operationDeleteTmpl,
bindings: {
resolve: '<',
close: '&',
dismiss: '&'
},
controller: function() {
var vm = this;
vm.$onInit = function() {
vm.operation = vm.resolve.operation;
};
vm.ok = function() {
vm.close({
$value: vm.operation
});
};
vm.cancel = function() {
vm.dismiss({
$value: 'cancel'
});
};
vm.title = function() {
// FIXME Alexis Lahouze 2017-06-15 i18n
if (vm.operation.id) {
return "Operation #" + vm.operation.id;
} else {
return "Operation";
}
};
}
})
.name;