Add coelesce in income query.

This commit is contained in:
Alexis Lahouze 2017-05-25 22:46:35 +02:00
parent 554a531f57
commit d9e28fb5a9

View File

@ -56,17 +56,23 @@ class Account(db.Model):
def income(self, begin, end): def income(self, begin, end):
"""Return the income for a specific time period.""" """Return the income for a specific time period."""
query = db.session.query( query = db.session.query(
db.func.sum( db.func.coalesce(
Operation.value db.func.sum(
).filter( Operation.value
db.func.sign(Operation.value) == -1 ).filter(
db.func.sign(Operation.value) == -1
), 0
).label("expenses"), ).label("expenses"),
db.func.sum( db.func.coalesce(
Operation.value db.func.sum(
).filter( Operation.value
db.func.sign(Operation.value) == 1 ).filter(
db.func.sign(Operation.value) == 1
), 0
).label("revenues"), ).label("revenues"),
db.func.sum(Operation.value).label("income") db.func.coalesce(
db.func.sum(Operation.value), 0
).label("income")
).filter( ).filter(
Operation.account_id == self.id, Operation.account_id == self.id,
) )