Added balance summary.

This commit is contained in:
Alexis Lahouze 2013-12-03 20:23:37 +01:00
parent a290c07f61
commit d7fd74fb17
4 changed files with 61 additions and 6 deletions

View File

@ -19,6 +19,7 @@ from app import db
from api.model.accounts import Account from api.model.accounts import Account
from api.model.entries import Entry from api.model.entries import Entry
from api.model.operations import Operation
from flask import json, request from flask import json, request
@ -49,6 +50,36 @@ def get_accounts():
"future": str(i.future) "future": str(i.future)
} for i in query.all()]) } for i in query.all()])
@app.route("/api/accounts/<account_id>/<year>/<month>/")
def get_account_status(account_id, year, month):
session = db.session
query = session.query(
func.sum(case([(func.sign(Operation.value) == -1, Operation.value)], else_=0)).label("expenses"),
func.sum(case([(func.sign(Operation.value) == 1, Operation.value)], else_=0)).label("revenues"),
func.sum(Operation.value).label("balance")
).filter(
Operation.account_id == account_id
).filter(
func.date_trunc('month', Operation.operation_date) == "%s-%s-01" % (year, month)
).group_by(Operation.account_id)
if query.count() == 1:
result = query.one()
revenues = result.revenues
expenses = result.expenses
balance = result.balance
else:
revenues = 0.0
expenses = 0.0
balance = 0.0
return json.dumps({
"expenses": str(expenses),
"revenues": str(revenues),
"balance": str(balance)
})
@app.route("/api/accounts/<account_id>/months") @app.route("/api/accounts/<account_id>/months")
def get_months(account_id): def get_months(account_id):
session = db.session session = db.session

View File

@ -48,8 +48,6 @@ def get_entries(account_id, year, month):
query = session.query(base_query).select_from(base_query).filter(func.date_trunc('month', base_query.c.operation_date) == "%s-%s-01" % (year, month)) query = session.query(base_query).select_from(base_query).filter(func.date_trunc('month', base_query.c.operation_date) == "%s-%s-01" % (year, month))
print query
return json.dumps([{ return json.dumps([{
"id": i.id, "id": i.id,
"pointed": i.pointed, "pointed": i.pointed,

View File

@ -72,6 +72,17 @@ var EntryController = function($scope, $http, $rootScope, $filter) {
$scope.drawPieChart(pieChartValues, "#expense-categories-chart-placeholder"); $scope.drawPieChart(pieChartValues, "#expense-categories-chart-placeholder");
}; };
$scope.getAccountStatus = function(account, month) {
if(account != null && month != null) {
$http.get("/api/accounts/" + account.id + "/" + month.year + "/" + month.month).
success($scope.getAccountStatus_success);
}
};
$scope.getAccountStatus_success = function(status) {
$scope.accountStatus = status;
};
// Function to load entries from server for a specific account and month. // Function to load entries from server for a specific account and month.
$scope.loadEntries = function(account, month) { $scope.loadEntries = function(account, month) {
if(account) { if(account) {
@ -145,9 +156,9 @@ var EntryController = function($scope, $http, $rootScope, $filter) {
// Returns the CSS class for an entry sold. // Returns the CSS class for an entry sold.
$scope.entryValueClass = function(sold) { $scope.entryValueClass = function(sold) {
if(sold < $scope.account.authorized_overdraft) { if(sold && sold < $scope.account.authorized_overdraft) {
return 'text-error'; return 'text-error';
} else if (sold < 0) { } else if (sold && sold < 0) {
return 'text-warning'; return 'text-warning';
} }
}; };
@ -388,6 +399,10 @@ var EntryController = function($scope, $http, $rootScope, $filter) {
$scope.loadEntries(args.account, args.month); $scope.loadEntries(args.account, args.month);
}); });
$rootScope.$on("monthsLoadedEvent", function(event, args){
$scope.getAccountStatus(args.account, args.month);
});
$scope.$on("entriesLoadedEvent", function(event, args) { $scope.$on("entriesLoadedEvent", function(event, args) {
$scope.entriesLoaded(args.entries); $scope.entriesLoaded(args.entries);
}); });

View File

@ -19,18 +19,29 @@
<!-- Chart row --> <!-- Chart row -->
<div class="row-fluid"> <div class="row-fluid">
<!-- Sold evolution chart placeholder --> <!-- Sold evolution chart placeholder -->
<div class="span8"> <div class="span7">
<div id="entries-chart-placeholder"> <div id="entries-chart-placeholder">
<svg style='height:300px'/> <svg style='height:300px'/>
</div> </div>
</div> </div>
<!-- Expense category piechart --> <!-- Expense category piechart -->
<div class="span4"> <div class="span3">
<div id="expense-categories-chart-placeholder"> <div id="expense-categories-chart-placeholder">
<svg style='height:300px'/> <svg style='height:300px'/>
</div> </div>
</div> </div>
<!-- Balance -->
<div class="span2" ng-controller="EntryController">
<div class="row-fluid">
<table class="table">
<tr><td>Dépenses&nbsp;:</td><td>[[accountStatus.expenses]]</td></tr>
<tr><td>Recettes&nbsp;:</td><td>[[accountStatus.revenues]]</td></tr>
<tr><td>Balance&nbsp;:</td><td><span ng-class="entryValueClass(accountStatus.balance)">[[accountStatus.balance]]</span></td></tr>
</table>
</div>
</div>
</div> </div>
<!-- Row with entry table --> <!-- Row with entry table -->