[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):
if not start:
start = datetime.fromtimestamp(0, tz=timezone.utc)
if not end:
end = datetime.now(tz=timezone.utc)
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
query = db.session.query(func.sum(Transaction.amount))
if start:
query = query.filter(start <= Transaction.time)
if end:
query = query.filter(Transaction.time <= end)
credit = query.filter(Transaction.receiver_ == user).scalar() or 0
debit = query.filter(Transaction.sender_ == user).scalar() or 0
return credit, debit, credit - debit