diff --git a/src/api/controller/accounts.py b/src/api/controller/accounts.py index 7d69229..dbdb403 100644 --- a/src/api/controller/accounts.py +++ b/src/api/controller/accounts.py @@ -19,6 +19,7 @@ from app import db from api.model.accounts import Account from api.model.entries import Entry +from api.model.operations import Operation from flask import json, request @@ -49,6 +50,36 @@ def get_accounts(): "future": str(i.future) } for i in query.all()]) +@app.route("/api/accounts////") +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//months") def get_months(account_id): session = db.session diff --git a/src/api/controller/entries.py b/src/api/controller/entries.py index 138a5f9..3151380 100644 --- a/src/api/controller/entries.py +++ b/src/api/controller/entries.py @@ -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)) - print query - return json.dumps([{ "id": i.id, "pointed": i.pointed, diff --git a/src/static/js/entries.js b/src/static/js/entries.js index cced6ef..0752a0f 100644 --- a/src/static/js/entries.js +++ b/src/static/js/entries.js @@ -72,6 +72,17 @@ var EntryController = function($scope, $http, $rootScope, $filter) { $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. $scope.loadEntries = function(account, month) { if(account) { @@ -145,9 +156,9 @@ var EntryController = function($scope, $http, $rootScope, $filter) { // Returns the CSS class for an entry sold. $scope.entryValueClass = function(sold) { - if(sold < $scope.account.authorized_overdraft) { + if(sold && sold < $scope.account.authorized_overdraft) { return 'text-error'; - } else if (sold < 0) { + } else if (sold && sold < 0) { return 'text-warning'; } }; @@ -388,6 +399,10 @@ var EntryController = function($scope, $http, $rootScope, $filter) { $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.entriesLoaded(args.entries); }); diff --git a/src/templates/index.html b/src/templates/index.html index e6dbc45..c23e948 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -19,18 +19,29 @@
-
+
-
+
+ + +
+
+ + + + +
Dépenses :[[accountStatus.expenses]]
Recettes :[[accountStatus.revenues]]
Balance :[[accountStatus.balance]]
+
+