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