2015-08-13 10:56:17 +02:00

117 lines
4.3 KiB
Python

"""
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/>.
"""
from sqlalchemy import func, case, literal_column
from sqlalchemy.orm import validates
from accountant import db
from .operations import Operation
class Account(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), nullable=False)
authorized_overdraft = db.Column(db.Integer, nullable=True, default=0)
def __init__(self, name, authorized_overdraft):
self.name = name
self.authorized_overdraft = authorized_overdraft
@classmethod
def query(cls, session, begin=None, end=None):
status_query = session.query(
Operation.account_id,
func.sum(Operation.value).label("future"),
func.sum(
case(
[(Operation.pointed,
Operation.value)],
else_=0
)
).label("pointed"),
func.sum(
case(
[(Operation.operation_date <= func.current_date(),
Operation.value)],
else_=0
)
).label("current"),
).group_by(
Operation.account_id
).subquery()
if begin and end:
balance_query = session.query(
Operation.account_id,
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.operation_date >= str(begin),
Operation.operation_date <= str(end)
).group_by(
Operation.account_id
).subquery()
query = session.query(
cls.id,
cls.name,
cls.authorized_overdraft,
func.coalesce(status_query.c.current, 0).label('current'),
func.coalesce(status_query.c.pointed, 0).label('pointed'),
func.coalesce(status_query.c.future, 0).label('future'),
func.coalesce(balance_query.c.expenses, 0).label('expenses'),
func.coalesce(balance_query.c.revenues, 0).label('revenues'),
func.coalesce(balance_query.c.balance, 0).label('balance'),
).outerjoin(
status_query, status_query.c.account_id == cls.id
).outerjoin(
balance_query, balance_query.c.account_id == cls.id
)
else:
query = session.query(
cls.id,
cls.name,
cls.authorized_overdraft,
func.coalesce(status_query.c.current, 0).label('current'),
func.coalesce(status_query.c.pointed, 0).label('pointed'),
func.coalesce(status_query.c.future, 0).label('future'),
literal_column("0").label('expenses'),
literal_column("0").label('revenues'),
literal_column("0").label('balance'),
).outerjoin(
status_query, status_query.c.account_id == cls.id
)
return query.order_by(cls.name)
@validates('authorized_overdraft')
def validate_authorized_overdraft(self, key, authorized_overdraft):
assert authorized_overdraft <= 0
return authorized_overdraft