// 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 . */ /* jshint node: true */ 'use strict'; var moment = require('moment'); var angular = require('angular'); var operationFormTmpl = require('./operation.form.tmpl.html'); 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 ]) .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($rootScope, $scope, $routeParams, $ngBootbox, $uibModal, Notification, Operation, $log) { 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 }); vm.operations.splice(0, 0, operation); }; /* * 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') }); }; /* * Toggle pointed indicator for an operation. */ vm.togglePointed = function(operation, rowform) { operation.pointed = !operation.pointed; // Save operation if not editing it. if (!rowform.$visible) { vm.save(operation); } }; /* * Toggle cancel indicator for an operation. */ vm.toggleCanceled = function(operation) { operation.canceled = !operation.canceled; vm.save(operation); }; /* * 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); } operation.confirmed = true; return operation.$save().then(function(data) { Notification.success('Operation #' + data.id + ' saved.'); $scope.$emit('operationSavedEvent', data); }); }; /* * Delete an operation and emit operationDeletedEvent. */ vm.delete = function(operation, $index) { var id = operation.id; $ngBootbox.confirm( 'Voulez-vous supprimer l\'opération \\\'' + operation.label + '\\\' ?', function(result) { if (result) { operation.$delete().then(function() { Notification.success('Operation #' + id + ' deleted.'); // Remove operation from array. vm.operation.splice($index, 1); $scope.$emit('operationDeletedEvent', operation); }); } } ); }; vm.modify = function(operation, $index) { $uibModal.open({ component: 'operationModalComponent', resolve: { operation: function() { return operation; } } }).result.then(function(operation) { // FIXME Alexis Lahouze 2017-06-15 Save Operation and reload data to // update balances. $log.info('modal validated', operation); }, function() { $log.info('modal-component dismissed at: ' + new Date()); }); }; /* * Reload operations on rangeSelectedEvent. */ vm.onRangeSelected = $rootScope.$on('rangeSelectedEvent', function(e, args) { vm.load(args.begin, args.end); }); $rootScope.$on('$destroy', function() { vm.onRangeSelected = angular.noop; }); vm.load(moment().date(1).year(2000), moment()); }) .component('operationModalComponent', { 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"; } }; } }); module.exports = operationModule;