From 4360177c97cbca041a4986d392d4e3f2b49786b5 Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Tue, 18 Nov 2025 16:52:03 +0500 Subject: [PATCH] fix --- core/apps/counterparty/models/conterparty.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/core/apps/counterparty/models/conterparty.py b/core/apps/counterparty/models/conterparty.py index a5f5fe2..2ee9e39 100644 --- a/core/apps/counterparty/models/conterparty.py +++ b/core/apps/counterparty/models/conterparty.py @@ -134,9 +134,25 @@ class CounterpartyBalance(BaseModel): @property def total_balance(self): - usd_course = Decimal(UsdCourse.objects.first().value) if UsdCourse.objects.exists() else Decimal(12000) - return self.total_balance_uzs + (self.total_balance_usd * usd_course) + PartyAmount = apps.get_model('orders', 'PartyAmount') + Income = apps.get_model('finance', 'Income') + Expense = apps.get_model('finance', 'Expense') + total = Decimal(0) + + for item in PartyAmount.objects.filter(party__orders__counterparty=self.counterparty): + rate = item.exchange_rate or Decimal(1) + total += Decimal(item.calculated_amount) * rate + + for inc in Income.objects.filter(counterparty=self.counterparty): + rate = inc.exchange_rate or Decimal(1) + total += Decimal(inc.price) * rate + + for exp in Expense.objects.filter(counterparty=self.counterparty): + rate = exp.exchange_rate or Decimal(1) + total -= Decimal(exp.price) * rate + + return total def __str__(self): return f"{self.counterparty.name}"