finance: expence delete api fixed

This commit is contained in:
behruz-dev
2025-11-11 16:17:47 +05:00
parent 333ffac4e6
commit 25898a7864
3 changed files with 35 additions and 65 deletions

View File

@@ -4,7 +4,6 @@ from django.db import models
from django.apps import apps
from core.apps.shared.models import BaseModel, Region, District
from core.apps.accounts.models import User
class CounterpartyFolder(BaseModel):
@@ -101,10 +100,9 @@ class CounterpartyBalance(BaseModel):
@property
def total_balance_usd(self):
Party = apps.get_model('orders', 'Party')
Income = apps.get_model('finance', 'Income')
Expence = apps.get_model('finance', 'Expence')
debit = Decimal(self.debit_usd or 0)
kredit = Decimal(self.kredit_usd or 0)
party_payment = Party.objects.filter(
orders__counterparty=self.counterparty,
is_deleted=False,
@@ -122,16 +120,21 @@ class CounterpartyBalance(BaseModel):
)['overdue_amount'] or 0
party_amount = Decimal(party_payment or 0) - Decimal(party_overdue or 0)
return debit - kredit + party_amount
income = Income.objects.filter(currency='usd', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price")
)['total_price'] or 0
expence = Expence.objects.filter(currency='usd', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price")
)['total_price'] or 0
return (party_amount + income) - expence
@property
def total_balance_uzs(self):
Party = apps.get_model('orders', 'Party')
debit = Decimal(self.debit_uzs or 0)
kredit = Decimal(self.kredit_uzs or 0)
Income = apps.get_model('finance', 'Income')
Expence = apps.get_model('finance', 'Expence')
party_payment = Party.objects.filter(
orders__counterparty=self.counterparty,
@@ -150,8 +153,19 @@ class CounterpartyBalance(BaseModel):
)['overdue_amount'] or 0
party_amount = Decimal(party_payment or 0) - Decimal(party_overdue or 0)
return debit - kredit + party_amount
party_total_amount = Party.objects.filter(
orders__counterparty=self.counterparty,
is_deleted=False,
currency='uzs'
).aggregate(total_price=models.Sum('party_amount__paid_amount'))['total_price'] or 0
income = Income.objects.filter(currency='uzs', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price")
)['total_price'] or 0
expence = Expence.objects.filter(currency='uzs', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price")
)['total_price'] or 0
print(party_total_amount, income, expence)
return (party_total_amount + income) - expence
def __str__(self):
return f"{self.counterparty.name}"