Change filtering on begin and end dates.

This commit is contained in:
Alexis Lahouze 2016-01-13 17:34:37 +01:00
parent 30efb9312a
commit 21c98a1dcf

View File

@ -30,7 +30,7 @@ class Account(db.Model):
@classmethod
def query(cls, begin=None, end=None):
status_query = db.session.query(
query = db.session.query(
Operation.account_id,
db.func.sum(Operation.value).label("future"),
db.func.sum(
@ -49,68 +49,60 @@ class Account(db.Model):
).label("current"),
).group_by(
Operation.account_id
).subquery()
)
if begin and end:
balance_query = db.session.query(
Operation.account_id,
db.func.sum(
db.case(
[(db.func.sign(Operation.value) == -1,
Operation.value)],
else_=0
)
).label("expenses"),
db.func.sum(
db.case(
[(db.func.sign(Operation.value) == 1,
Operation.value)],
else_=0
)
).label("revenues"),
db.func.sum(Operation.value).label("balance")
).filter(
Operation.operation_date >= str(begin),
Operation.operation_date <= str(end)
).group_by(
Operation.account_id
).subquery()
status_query = query.subquery()
query = db.session.query(
cls.id,
cls.name,
cls.authorized_overdraft,
db.func.coalesce(
status_query.c.current, 0).label('current'),
db.func.coalesce(
status_query.c.pointed, 0).label('pointed'),
db.func.coalesce(
status_query.c.future, 0).label('future'),
db.func.coalesce(
balance_query.c.expenses, 0).label('expenses'),
db.func.coalesce(
balance_query.c.revenues, 0).label('revenues'),
db.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 = db.session.query(
cls.id,
cls.name,
cls.authorized_overdraft,
db.func.coalesce(status_query.c.current, 0).label('current'),
db.func.coalesce(status_query.c.pointed, 0).label('pointed'),
db.func.coalesce(status_query.c.future, 0).label('future'),
db.literal_column("0").label('expenses'),
db.literal_column("0").label('revenues'),
db.literal_column("0").label('balance'),
).outerjoin(
status_query, status_query.c.account_id == cls.id
)
query = db.session.query(
Operation.account_id,
db.func.sum(
db.case(
[(db.func.sign(Operation.value) == -1,
Operation.value)],
else_=0
)
).label("expenses"),
db.func.sum(
db.case(
[(db.func.sign(Operation.value) == 1,
Operation.value)],
else_=0
)
).label("revenues"),
db.func.sum(Operation.value).label("balance")
).group_by(
Operation.account_id
)
if begin:
query = query.filter(Operation.operation_date >= str(begin))
if end:
query = query.filter(Operation.operation_date <= str(end))
balance_query = query.subquery()
query = db.session.query(
cls.id,
cls.name,
cls.authorized_overdraft,
db.func.coalesce(
status_query.c.current, 0).label('current'),
db.func.coalesce(
status_query.c.pointed, 0).label('pointed'),
db.func.coalesce(
status_query.c.future, 0).label('future'),
db.func.coalesce(
balance_query.c.expenses, 0).label('expenses'),
db.func.coalesce(
balance_query.c.revenues, 0).label('revenues'),
db.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
)
return query.order_by(cls.name)