[Plugin] Balance: Simplified get_balance

This commit is contained in:
Ferdinand Thiessen 2021-01-29 21:19:32 +01:00
parent 06a806da3d
commit 13e0d60d8e
1 changed files with 7 additions and 20 deletions

View File

@ -28,27 +28,14 @@ def get_limit(user: User) -> float:
def get_balance(user, start: datetime = None, end: datetime = None): def get_balance(user, start: datetime = None, end: datetime = None):
if not start: query = db.session.query(func.sum(Transaction.amount))
start = datetime.fromtimestamp(0, tz=timezone.utc) if start:
if not end: query = query.filter(start <= Transaction.time)
end = datetime.now(tz=timezone.utc) if end:
query = query.filter(Transaction.time <= end)
credit = (
db.session.query(func.sum(Transaction.amount))
.filter(Transaction.receiver_ == user)
.filter(start <= Transaction.time)
.filter(Transaction.time <= end)
.scalar()
) or 0
debit = (
db.session.query(func.sum(Transaction.amount))
.filter(Transaction.sender_ == user)
.filter(start <= Transaction.time)
.filter(Transaction.time <= end)
.scalar()
) or 0
credit = query.filter(Transaction.receiver_ == user).scalar() or 0
debit = query.filter(Transaction.sender_ == user).scalar() or 0
return credit, debit, credit - debit return credit, debit, credit - debit