2015-08-19 18:12:41 +02:00

146 lines
3.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:
// The AccountController.
accountantApp
.factory("Accounts", ["$resource", function($resource) {
return $resource(
"/api/accounts/:id", {
id: "@id"
}
);
}])
.controller(
"AccountController", [
"$scope", "Accounts", "notificationService",
function($scope, Accounts, notificationService) {
/*
* 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 if not already added.
*/
$scope.addAccount = function() {
var account = new Accounts();
account.authorized_overdraft = 0;
$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) {
$scope.accounts.splice($index, 1);
} else {
rowform.$cancel();
}
};
/*
* Save account.
*/
$scope.saveAccount = function($data, $index) {
var account = $scope.accounts[$index];
account = angular.merge(account, $data);
return account.$save().then(function(data) {
// Sort accounts by name.
$scope.accounts.sort(function(a, b) {
if(a.id && b.id) {
if(a.name < b.name) {
return -1;
} else if(a.name > b.name) {
return 1;
} else {
return a.id - b.id;
}
} else if (!a.id && !b.id) {
return 0;
} else if (!a.id) {
return -1;
} else if (!b.id) {
return 1;
}
return 0;
});
notificationService.success("Account #" + data.id + " saved.");
return data;
});
};
/*
* Delete an account and emit accountDeletedEvent.
*/
$scope.deleteAccount = function(account, $index) {
var id = account.id;
bootbox.confirm(
"Voulez-vous supprimer le compte \"" + account.name + "\" ?",
function(result) {
if(result) {
account.$delete().then(function() {
notificationService.success("Account #" + id + " deleted.");
// Remove account from array.
$scope.accounts.splice($index, 1);
});
}
}
);
};
// Load accounts.
$scope.accounts = Accounts.query();
}]);