improve balances query.

This commit is contained in:
Alexis Lahouze 2017-05-25 21:23:56 +02:00
parent d9e9e3f2a0
commit 5b87a27667

View File

@ -30,20 +30,24 @@ class Account(db.Model):
def balances(self):
"""Return the balances of this account."""
return db.session.query(
db.func.sum(Operation.value).label("future"),
db.func.coalesce(
db.func.sum(
db.case(
[(Operation.pointed,
Operation.value)],
else_=0
)
Operation.value
), 0
).label("future"),
db.func.coalesce(
db.func.sum(
Operation.value
).filter(
Operation.pointed
), 0
).label("pointed"),
db.func.coalesce(
db.func.sum(
db.case(
[(Operation.operation_date <= db.func.current_date(),
Operation.value)],
else_=0
)
Operation.value,
).filter(
Operation.operation_date <= db.func.current_date()
), 0
).label("current"),
).filter(
Operation.account_id == self.id