mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-04-24 09:38:35 +02:00
refactor: fetch currency balance from currency instead of account
This commit is contained in:
@@ -89,10 +89,10 @@ def calculate_account_net_worth():
|
|||||||
|
|
||||||
|
|
||||||
def calculate_currency_net_worth():
|
def calculate_currency_net_worth():
|
||||||
# Subquery to calculate balance for each account
|
# Subquery to calculate balance for each currency
|
||||||
balance_subquery = (
|
balance_subquery = (
|
||||||
Transaction.objects.filter(account=OuterRef("pk"), is_paid=True)
|
Transaction.objects.filter(account__currency=OuterRef("pk"), is_paid=True)
|
||||||
.values("account")
|
.values("account__currency")
|
||||||
.annotate(
|
.annotate(
|
||||||
balance=Sum(
|
balance=Sum(
|
||||||
Case(
|
Case(
|
||||||
@@ -106,22 +106,24 @@ def calculate_currency_net_worth():
|
|||||||
.values("balance")
|
.values("balance")
|
||||||
)
|
)
|
||||||
|
|
||||||
# Main query to fetch all account data
|
# Main query to fetch all currency data
|
||||||
accounts_data = Account.objects.annotate(
|
currencies_data = Currency.objects.annotate(
|
||||||
balance=Coalesce(Subquery(balance_subquery), Decimal("0"))
|
balance=Coalesce(Subquery(balance_subquery), Decimal("0"))
|
||||||
).select_related("currency")
|
)
|
||||||
|
|
||||||
|
print(currencies_data[0].balance)
|
||||||
|
|
||||||
net_worth = {}
|
net_worth = {}
|
||||||
for item in accounts_data:
|
for item in currencies_data:
|
||||||
currency_name = item.currency.name
|
currency_name = item.name
|
||||||
net_worth[currency_name] = {
|
net_worth[currency_name] = {
|
||||||
"amount": net_worth.get(currency_name, {}).get("amount", Decimal("0"))
|
"amount": net_worth.get(currency_name, {}).get("amount", Decimal("0"))
|
||||||
+ item.balance,
|
+ item.balance,
|
||||||
"code": item.currency.code,
|
"code": item.code,
|
||||||
"name": currency_name,
|
"name": currency_name,
|
||||||
"prefix": item.currency.prefix,
|
"prefix": item.prefix,
|
||||||
"suffix": item.currency.suffix,
|
"suffix": item.suffix,
|
||||||
"decimal_places": item.currency.decimal_places,
|
"decimal_places": item.decimal_places,
|
||||||
}
|
}
|
||||||
|
|
||||||
return net_worth
|
return net_worth
|
||||||
|
|||||||
Reference in New Issue
Block a user