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-10 18:02:19 +02:00
|
|
|
var angular = require('angular');
|
|
|
|
|
|
|
|
var ngResource = require('angular-resource'),
|
|
|
|
ngMessages = require('angular-messages'),
|
|
|
|
ngUiNotification = require('angular-ui-notification'),
|
|
|
|
ngBootbox = require('ngbootbox');
|
|
|
|
|
|
|
|
// Note: ngBootbox seems to have no module.exports.
|
|
|
|
ngBootbox = 'ngBootbox';
|
|
|
|
|
|
|
|
// FIXME Alexis Lahouze 2017-06-06 move into templates.js.
|
|
|
|
require('../views/account.form.tmpl.html');
|
|
|
|
|
|
|
|
var accountModule = angular.module('accountant.accounts', [
|
|
|
|
ngResource,
|
|
|
|
ngMessages,
|
|
|
|
ngUiNotification,
|
|
|
|
ngBootbox
|
2016-04-12 10:55:08 +02:00
|
|
|
])
|
|
|
|
|
2016-10-12 23:32:54 +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-12 23:32:54 +02:00
|
|
|
})
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
.factory('Account', function($resource) {
|
2016-04-12 10:55:08 +02:00
|
|
|
var Account = $resource(
|
|
|
|
'/api/account/:id', {
|
|
|
|
id: '@id'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Account.prototype.getSolds = function() {
|
2017-06-10 18:02:34 +02:00
|
|
|
var Solds = $resource('/api/account/:id/balances', {id: this.id});
|
2016-04-12 10:55:08 +02:00
|
|
|
|
|
|
|
this.solds = Solds.get();
|
|
|
|
};
|
|
|
|
|
|
|
|
Account.prototype.getBalance = function(begin, end) {
|
|
|
|
var Balance = $resource(
|
|
|
|
'/api/account/:id/balance', {
|
|
|
|
id: this.id,
|
|
|
|
begin: begin.format('YYYY-MM-DD'),
|
|
|
|
end: end.format('YYYY-MM-DD')
|
|
|
|
});
|
|
|
|
|
|
|
|
this.balance = Balance.get();
|
|
|
|
};
|
|
|
|
|
|
|
|
return Account;
|
2016-10-12 23:32:54 +02:00
|
|
|
})
|
|
|
|
|
2016-10-14 08:19:58 +02:00
|
|
|
.controller('AccountController', function($ngBootbox, Account, Notification) {
|
|
|
|
var vm = this;
|
2016-10-12 23:32:54 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the class for an account current value compared to authorized
|
|
|
|
* overdraft.
|
|
|
|
*/
|
|
|
|
vm.rowClass = function(account) {
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
if (!account || !account.authorized_overdraft || !account.current) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-09 20:17:11 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
if (account.current < account.authorized_overdraft) {
|
|
|
|
return 'danger';
|
|
|
|
} else if (account.current < 0) {
|
|
|
|
return 'warning';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the class for a value compared to account authorized overdraft.
|
|
|
|
*/
|
|
|
|
vm.valueClass = function(account, value) {
|
|
|
|
if (!account || !value) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
if (value < account.authorized_overdraft) {
|
|
|
|
return 'text-danger';
|
|
|
|
} else if (value < 0) {
|
|
|
|
return 'text-warning';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an empty account.
|
|
|
|
*/
|
|
|
|
vm.add = function() {
|
|
|
|
var account = new Account({
|
2016-10-12 19:57:19 +02:00
|
|
|
// eslint-disable-next-line camelcase
|
2016-10-12 23:32:54 +02:00
|
|
|
authorized_overdraft: 0
|
|
|
|
});
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
// Insert account at the begining of the array.
|
2016-10-14 08:19:58 +02:00
|
|
|
vm.accounts.splice(0, 0, account);
|
2016-10-12 23:32:54 +02:00
|
|
|
};
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
/*
|
|
|
|
* Cancel account edition. Remove it from array if a new one.
|
|
|
|
*/
|
|
|
|
vm.cancelEdit = function(rowform, account, $index) {
|
|
|
|
if (account.id) {
|
|
|
|
rowform.$cancel();
|
|
|
|
} else {
|
|
|
|
// Account not saved, just remove it from array.
|
2016-10-14 08:19:58 +02:00
|
|
|
vm.accounts.splice($index, 1);
|
2016-10-12 23:32:54 +02:00
|
|
|
}
|
|
|
|
};
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
/*
|
|
|
|
* Save account.
|
|
|
|
*/
|
|
|
|
vm.save = function(account) {
|
2016-10-14 08:19:58 +02:00
|
|
|
// var account = vm.accounts[$index];
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
// account = angular.merge(account, $data);
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
return account.$save().then(function(data) {
|
|
|
|
Notification.success('Account #' + data.id + ' saved.');
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
// TODO Alexis Lahouze 2016-03-08 Update solds
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete an account.
|
|
|
|
*/
|
|
|
|
vm.delete = function(account, $index) {
|
|
|
|
var id = account.id;
|
|
|
|
|
|
|
|
$ngBootbox.confirm(
|
|
|
|
'Voulez-vous supprimer le compte \'' + account.name + '\' ?',
|
|
|
|
function(result) {
|
|
|
|
if (result) {
|
|
|
|
account.$delete().then(function() {
|
|
|
|
Notification.success('Account #' + id + ' deleted.');
|
|
|
|
|
|
|
|
// Remove account from array.
|
2016-10-14 08:19:58 +02:00
|
|
|
vm.accounts.splice($index, 1);
|
2016-10-12 23:32:54 +02:00
|
|
|
});
|
2016-10-09 20:17:11 +02:00
|
|
|
}
|
2016-10-12 23:32:54 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2016-04-12 10:55:08 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
// Load accounts.
|
|
|
|
vm.accounts = Account.query();
|
|
|
|
})
|
2016-10-12 19:57:19 +02:00
|
|
|
|
2016-10-12 23:32:54 +02:00
|
|
|
.directive('accountFormDialog', function(Account, $ngBootbox, Notification, $log) {
|
2016-10-12 19:57:19 +02:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
scope: {
|
|
|
|
account: '=ngModel'
|
|
|
|
},
|
|
|
|
link: function(scope, element) {
|
|
|
|
var title = 'Account';
|
|
|
|
|
|
|
|
if (scope.account && scope.account.id) {
|
|
|
|
title = title + ' #' + scope.account.id;
|
|
|
|
}
|
2016-05-22 21:47:33 +02:00
|
|
|
|
2016-10-12 19:57:19 +02:00
|
|
|
scope.form = {};
|
2016-05-22 21:47:33 +02:00
|
|
|
|
2016-10-12 19:57:19 +02:00
|
|
|
scope.submitForm = function() {
|
|
|
|
// check to make sure the form is completely valid
|
|
|
|
if (!scope.form.$valid) {
|
|
|
|
return false;
|
2016-05-22 21:47:33 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 19:57:19 +02:00
|
|
|
// Authorized overdraft is a positive integer but data is a negative integer.
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
scope.data.authorized_overdraft = -scope.data.authorized_overdraft;
|
2016-07-15 16:40:39 +02:00
|
|
|
|
2016-10-12 19:57:19 +02:00
|
|
|
angular.copy(scope.data, scope.account);
|
2016-07-15 16:40:39 +02:00
|
|
|
|
2016-10-12 19:57:19 +02:00
|
|
|
// Save account
|
2016-10-12 23:32:54 +02:00
|
|
|
$log.log(scope.account);
|
2016-10-12 19:57:19 +02:00
|
|
|
return scope.account.$save().then(
|
|
|
|
function(data) {
|
|
|
|
Notification.success('Account #' + data.id + ' saved.');
|
2016-07-15 16:40:39 +02:00
|
|
|
|
2016-10-12 19:57:19 +02:00
|
|
|
scope.account.getSolds();
|
2016-07-15 16:40:39 +02:00
|
|
|
|
2016-10-12 19:57:19 +02:00
|
|
|
return data;
|
|
|
|
},
|
|
|
|
function(data) {
|
|
|
|
Notification.error('Error while saving account #' + data.id);
|
|
|
|
scope.account.getSolds();
|
2016-10-12 23:32:54 +02:00
|
|
|
$log.log(data);
|
2016-10-12 19:57:19 +02:00
|
|
|
return false;
|
2016-07-15 16:40:39 +02:00
|
|
|
}
|
2016-10-12 19:57:19 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
element.on('click', function() {
|
|
|
|
// Create new account if not passed in ng-model.
|
|
|
|
if (!scope.account) {
|
|
|
|
scope.account = new Account({
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
authorized_overdraft: 0
|
|
|
|
});
|
|
|
|
}
|
2016-07-15 16:40:39 +02:00
|
|
|
|
2016-10-12 19:57:19 +02:00
|
|
|
scope.data = {};
|
|
|
|
angular.copy(scope.account, scope.data);
|
|
|
|
|
|
|
|
// Authorized overdraft must be positive in form.
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
scope.data.authorized_overdraft = -scope.data.authorized_overdraft;
|
|
|
|
|
|
|
|
// Open dialog with form.
|
|
|
|
$ngBootbox.customDialog({
|
|
|
|
scope: scope,
|
|
|
|
title: title,
|
2017-06-10 18:03:08 +02:00
|
|
|
templateUrl: '/views/account.form.tmpl.html',
|
2016-10-12 19:57:19 +02:00
|
|
|
onEscape: true,
|
|
|
|
buttons: {
|
|
|
|
save: {
|
|
|
|
label: 'Save',
|
|
|
|
className: 'btn-success',
|
|
|
|
callback: scope.submitForm
|
|
|
|
},
|
|
|
|
cancel: {
|
|
|
|
label: 'Cancel',
|
|
|
|
className: 'btn-default',
|
|
|
|
callback: true
|
2016-05-22 21:47:33 +02:00
|
|
|
}
|
2016-10-12 19:57:19 +02:00
|
|
|
}
|
2016-05-22 21:47:33 +02:00
|
|
|
});
|
2016-10-12 19:57:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2017-06-10 18:02:19 +02:00
|
|
|
|
|
|
|
module.exports = accountModule;
|