2015-08-16 00:39:53 +02:00

166 lines
4.3 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", "$rootScope", "$routeParams", "Accounts", "notificationService",
function($scope, $rootScope, $routeParams, Accounts, notificationService) {
$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";
}
};
$scope.valueClass = function(account, value) {
if(!account || !value) {
return;
}
if(value < account.authorized_overdraft) {
return "text-danger";
} else if(value < 0) {
return "text-warning";
}
};
$scope.addAccount = function() {
if(!$scope.inserted) {
$scope.inserted = new Accounts();
$scope.inserted.authorized_overdraft = 0;
$scope.accounts.splice(0,0, $scope.inserted);
}
};
$scope.cancelEdit = function(rowform, account) {
if(account == $scope.inserted) {
$scope.inserted=false;
$scope.accounts.splice(0,1);
} else {
rowform.$cancel();
}
};
/*
* Notify on account created event.
*/
$rootScope.$on("accountCreatedEvent", function(e, account) {
notificationService.success("Account #" + account.id + " created.");
});
/*
* Reload accounts on account created event.
*/
$rootScope.$on("accountCreatedEvent", function(e, account) {
$scope.loadAccounts();
});
/*
* Save account and emit accountSavedEvent.
*/
$scope.saveAccount = function($data, $index) {
var account = $scope.accounts[$index];
account = angular.merge(account, $data);
var promise = account.$save();
if(account == $scope.inserted) {
return promise.then(function(data) {
$scope.inserted = false;
$scope.$emit("accountCreatedEvent", data);
});
} else {
return promise.then(function(data) {
$scope.$emit("accountSavedEvent", data);
});
}
};
/*
* Notify on account saved event.
*/
$rootScope.$on("accountSavedEvent", function(e, account) {
new PNnotify({
type: "success",
title: "Save",
text: "Account #" + account.id + " saved."
});
});
/*
* Delete an account and emit accountDeletedEvent.
*/
$scope.deleteAccount = function(account, modalScope) {
account.$delete(function(data) {
$scope.$emit("accountDeletedEvent", account);
});
};
/*
* Notify on account deleted event.
*/
$rootScope.$on("accountDeletedEvent", function(e, account) {
new PNotify({
type: "success",
title: "Save",
text: "Account #" + account.id + " deleted."
});
});
/*
* Reload acounts when an account is deleted.
*/
$rootScope.$on("accountDeletedEvent", function(e, account) {
$scope.loadAccounts();
});
/*
* Reload accounts and emit accountsLoadedEvent.
*/
$scope.loadAccounts = function() {
// Reset the new account.
$scope.newAccount = new Accounts();
// Load accounts using the resource.
$scope.accounts = Accounts.query(function (data) {
$scope.$emit("accountsLoadedEvent", $scope.accounts);
});
};
$scope.loadAccounts();
}]);