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 @classmethod
def query(cls, begin=None, end=None): def query(cls, begin=None, end=None):
status_query = db.session.query( query = db.session.query(
Operation.account_id, Operation.account_id,
db.func.sum(Operation.value).label("future"), db.func.sum(Operation.value).label("future"),
db.func.sum( db.func.sum(
@ -49,10 +49,11 @@ class Account(db.Model):
).label("current"), ).label("current"),
).group_by( ).group_by(
Operation.account_id Operation.account_id
).subquery() )
if begin and end: status_query = query.subquery()
balance_query = db.session.query(
query = db.session.query(
Operation.account_id, Operation.account_id,
db.func.sum( db.func.sum(
db.case( db.case(
@ -69,12 +70,17 @@ class Account(db.Model):
) )
).label("revenues"), ).label("revenues"),
db.func.sum(Operation.value).label("balance") db.func.sum(Operation.value).label("balance")
).filter(
Operation.operation_date >= str(begin),
Operation.operation_date <= str(end)
).group_by( ).group_by(
Operation.account_id Operation.account_id
).subquery() )
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( query = db.session.query(
cls.id, cls.id,
@ -97,20 +103,6 @@ class Account(db.Model):
).outerjoin( ).outerjoin(
balance_query, balance_query.c.account_id == cls.id 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
)
return query.order_by(cls.name) return query.order_by(cls.name)