accountant-ui/accountant-ui/js/accounts.js

212 lines
5.8 KiB
JavaScript

/*
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/>.
*/
// vim: set tw=80 ts=2 sw=2 sts=2:
'use strict';
angular.module('accountant.accounts', [
'ngResource',
'ui-notification',
'xeditable',
'ngBootbox'
])
.config(['$resourceProvider', function($resourceProvider) {
// Keep trailing slashes to avoid redirect by flask..
$resourceProvider.defaults.stripTrailingSlashes = false;
}])
.factory('Account', ['$resource', function($resource) {
var Account = $resource(
'/api/account/:id', {
id: '@id'
}
);
Account.prototype.getSolds = function() {
var Solds = $resource('/api/account/:id/solds', {id: this.id});
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;
}])
.controller(
'AccountController', [
'$scope', '$ngBootbox', 'Account', 'Notification',
function($scope, $ngBootbox, Account, Notification) {
/*
* Return the class for an account current value compared to authorized
* overdraft.
*/
$scope.rowClass = function(account) {
if(!account || !account.authorized_overdraft || !account.current) {
return;
}
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.
*/
$scope.valueClass = function(account, value) {
if(!account || !value) {
return;
}
if(value < account.authorized_overdraft) {
return 'text-danger';
} else if(value < 0) {
return 'text-warning';
}
};
/*
* Add an empty account.
*/
$scope.add = function() {
var account = new Account({
authorized_overdraft: 0
});
// Insert account at the begining of the array.
$scope.accounts.splice(0, 0, account);
};
/*
* Cancel account edition. Remove it from array if a new one.
*/
$scope.cancelEdit = function(rowform, account, $index) {
if(!account.id) {
// Account not saved, just remove it from array.
$scope.accounts.splice($index, 1);
} else {
rowform.$cancel();
}
};
/*
* Save account.
*/
$scope.save = function(account) {
//var account = $scope.accounts[$index];
//account = angular.merge(account, $data);
return account.$save().then(function(data) {
Notification.success('Account #' + data.id + ' saved.');
// TODO Alexis Lahouze 2016-03-08 Update solds
return data;
});
};
/*
* Delete an account.
*/
$scope.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.
$scope.accounts.splice($index, 1);
});
}
}
);
};
// Load accounts.
$scope.accounts = Account.query();
}])
.directive(
'accountFormDialog', function($ngBootbox) {
return {
restrict: 'A',
scope: {
account: '=ngModel'
},
link: function(scope, element) {
var title = 'Account';
if(scope.account && scope.account.id) {
title = title + ' #' + scope.account.id;
}
scope.form = {};
element.on('click', function() {
//angular.copy(scope.account, scope.form);
// Open dialog with form.
$ngBootbox.customDialog({
scope: scope,
title: title,
templateUrl: 'views/account.form.tmpl.html',
onEscape: true,
buttons: {
save: {
label: 'Save',
className: 'btn-success',
callback: function() {
// Validate form
console.log(scope.form);
// Save account
console.log(scope.account);
return false;
}
},
cancel: {
label: 'Cancel',
className: 'btn-default',
callback: true
}
}
});
});
}
};
});