accountant/api/controller/accounts.py

135 lines
4.9 KiB
Python
Raw Normal View History

2013-02-08 13:07:27 +01:00
"""
This file is part of Accountant.
2013-02-08 13:07:27 +01:00
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.
Foobar 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/>.
"""
2015-06-05 18:15:50 +02:00
from . import auth
2013-12-03 22:22:25 +01:00
from .. import api
2013-12-03 23:01:17 +01:00
from ..model import db, session_scope
2013-12-03 22:22:25 +01:00
from ..model.accounts import Account
from ..model.entries import Entry
from ..model.operations import Operation
from flask import json, request
from sqlalchemy import func, case, cast, extract, distinct
2015-06-05 18:15:50 +02:00
from forms.accounts import AccountIdForm, AccountForm
2013-12-03 22:22:25 +01:00
@api.route("/accounts", methods=["GET"])
2015-06-05 18:15:50 +02:00
@auth.login_required
def get_accounts():
"""
Returns accounts with their solds.
"""
2013-12-03 23:01:17 +01:00
with session_scope() as session:
query = session.query(
Account.id.label("id"),
Account.name.label("name"),
Account.authorized_overdraft.label("authorized_overdraft"),
func.sum(Entry.value).label("future"),
func.sum(case([(Entry.pointed, Entry.value,)], else_=cast(0, db.Numeric(15, 2)))).label("pointed"),
func.sum(case([(Entry.operation_date < func.now(), Entry.value,)], else_=cast(0, db.Numeric(15, 2)))).label("current")
).outerjoin(Entry).group_by(Account.id).order_by(Account.id)
return json.dumps([{
"id": i.id,
"name": i.name,
"authorized_overdraft": i.authorized_overdraft,
"current": str(i.current),
"pointed": str(i.pointed),
"future": str(i.future)
} for i in query.all()])
2013-12-03 22:22:25 +01:00
@api.route("/accounts/<account_id>/<year>/<month>/")
2015-06-05 18:15:50 +02:00
@auth.login_required
2013-12-03 20:23:37 +01:00
def get_account_status(account_id, year, month):
2013-12-03 23:01:17 +01:00
with session_scope() as 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)
})
2013-12-03 20:23:37 +01:00
2013-12-03 22:22:25 +01:00
@api.route("/accounts/<account_id>/months")
2015-06-05 18:15:50 +02:00
@auth.login_required
def get_months(account_id):
2013-12-03 23:01:17 +01:00
with session_scope() as session:
query = session.query(
distinct(func.lpad(cast(extract("year", Entry.operation_date), db.String), 4, '0')).label("year"),
func.lpad(cast(extract("month", Entry.operation_date), db.String), 2, '0').label("month")
).filter(Entry.account_id == account_id).order_by("year", "month")
2013-01-24 15:27:08 +01:00
2013-12-03 23:01:17 +01:00
return json.dumps([{
"year": i.year,
"month": i.month.rjust(2, '0')
} for i in query.all()])
2013-12-03 22:22:25 +01:00
@api.route("/accounts", methods=["PUT"])
2015-06-05 18:15:50 +02:00
@auth.login_required
def add_account():
2013-12-03 23:01:17 +01:00
with session_scope() as session:
2013-01-24 20:44:09 +01:00
account = Account(request.json['name'], request.json['authorized_overdraft'])
2013-01-24 15:27:08 +01:00
session.add(account)
2013-01-24 15:27:08 +01:00
return json.dumps("Account added.")
2013-12-03 22:22:25 +01:00
@api.route("/accounts/<account_id>", methods=["PUT"])
2015-06-05 18:15:50 +02:00
@auth.login_required
def update_account(account_id):
2015-06-05 18:15:50 +02:00
account_form = AccountForm()
if account_form.validate():
with session_scope() as session:
account = session.query(Account).filter(Account.id == account_id).first()
2015-06-05 18:15:50 +02:00
account.name = request.json['name']
account.authorized_overdraft = request.json['authorized_overdraft']
2015-06-05 18:15:50 +02:00
session.merge(account)
2015-06-05 18:15:50 +02:00
return json.dumps("Account #%s updated." % account_id)
else:
return json.dumps({'ok': False, 'error_type': 'validation', 'errors': account_form.errors})
2013-12-03 22:22:25 +01:00
@api.route("/accounts/<account_id>", methods=["DELETE"])
2015-06-05 18:15:50 +02:00
@auth.login_required
def delete_account(account_id):
2013-12-03 23:01:17 +01:00
with session_scope() as session:
2013-01-24 15:27:08 +01:00
account = session.query(Account).filter(Account.id == account_id).first()
2013-12-03 22:22:25 +01:00
2013-01-24 15:27:08 +01:00
session.delete(account)
2013-12-03 22:22:25 +01:00
2013-01-24 15:27:08 +01:00
return json.dumps("Account #%s deleted." % account_id)