fucking counterparty statistics not done yet! fuck
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from decimal import Decimal
|
||||
|
||||
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
|
||||
from core.apps.accounts.models import User
|
||||
|
||||
|
||||
class CounterpartyFolder(BaseModel):
|
||||
@@ -99,18 +100,61 @@ class CounterpartyBalance(BaseModel):
|
||||
|
||||
@property
|
||||
def total_balance_usd(self):
|
||||
Party = apps.get_model('orders', 'Party')
|
||||
|
||||
debit = Decimal(self.debit_usd or 0)
|
||||
kredit = Decimal(self.kredit_usd or 0)
|
||||
return debit - kredit
|
||||
|
||||
party_payment = Party.objects.filter(
|
||||
orders__counterparty=self.counterparty,
|
||||
is_deleted=False,
|
||||
currency='usd'
|
||||
).aggregate(
|
||||
payment_amount=models.Sum('party_amount__payment_amount')
|
||||
)['payment_amount'] or 0
|
||||
|
||||
party_overdue = Party.objects.filter(
|
||||
orders__counterparty=self.counterparty,
|
||||
is_deleted=False,
|
||||
currency='usd'
|
||||
).aggregate(
|
||||
overdue_amount=models.Sum('party_amount__overdue_amount')
|
||||
)['overdue_amount'] or 0
|
||||
|
||||
party_amount = Decimal(party_payment or 0) - Decimal(party_overdue or 0)
|
||||
|
||||
return debit - kredit + party_amount
|
||||
|
||||
|
||||
@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)
|
||||
return debit - kredit
|
||||
|
||||
|
||||
party_payment = Party.objects.filter(
|
||||
orders__counterparty=self.counterparty,
|
||||
is_deleted=False,
|
||||
currency='uzs'
|
||||
).aggregate(
|
||||
payment_amount=models.Sum('party_amount__payment_amount')
|
||||
)['payment_amount'] or 0
|
||||
|
||||
party_overdue = Party.objects.filter(
|
||||
orders__counterparty=self.counterparty,
|
||||
is_deleted=False,
|
||||
currency='uzs'
|
||||
).aggregate(
|
||||
overdue_amount=models.Sum('party_amount__overdue_amount')
|
||||
)['overdue_amount'] or 0
|
||||
|
||||
party_amount = Decimal(party_payment or 0) - Decimal(party_overdue or 0)
|
||||
|
||||
return debit - kredit + party_amount
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.counterparty.name} | USD: {self.total_balance_usd} | UZS: {self.total_balance_uzs}"
|
||||
return f"{self.counterparty.name}"
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Kontragent Balansi"
|
||||
|
||||
@@ -199,11 +199,9 @@ class CounterPartyIncomeExpenceStatisticsApiView(views.APIView):
|
||||
def get(self, request, id):
|
||||
counterparty = get_object_or_404(Counterparty, id=id)
|
||||
|
||||
# Income va Expence querysetlari
|
||||
incomes = Income.objects.filter(counterparty=counterparty, is_deleted=False)
|
||||
expences = Expence.objects.filter(counterparty=counterparty, is_deleted=False)
|
||||
|
||||
# Balanslar valyutalar bo'yicha
|
||||
income_by_currency = {'uzs': {'total': Decimal(0), 'count': 0, 'amount_uzs': Decimal(0)},
|
||||
'usd': {'total': Decimal(0), 'count': 0, 'amount_uzs': Decimal(0)}}
|
||||
for income in incomes:
|
||||
@@ -224,18 +222,17 @@ class CounterPartyIncomeExpenceStatisticsApiView(views.APIView):
|
||||
expence_by_currency[currency]['count'] += 1
|
||||
expence_by_currency[currency]['amount_uzs'] += amount * rate if currency == 'usd' else amount
|
||||
|
||||
# Income/Expence summalari
|
||||
total_income_uzs = sum(v['amount_uzs'] for v in income_by_currency.values())
|
||||
total_expence_uzs = sum(v['amount_uzs'] for v in expence_by_currency.values())
|
||||
total_income_usd = income_by_currency['usd']['total']
|
||||
total_expence_usd = expence_by_currency['usd']['total']
|
||||
|
||||
# Kontragent balansi
|
||||
balance_obj, _ = CounterpartyBalance.objects.get_or_create(counterparty=counterparty)
|
||||
balance_uzs = balance_obj.total_balance_uzs + (total_income_uzs - total_expence_uzs)
|
||||
balance_usd = balance_obj.total_balance_usd + (total_income_usd - total_expence_usd)
|
||||
# balance_obj, _ = CounterpartyBalance.objects.get_or_create(counterparty=counterparty)
|
||||
# balance_uzs = balance_obj.total_balance_uzs + (total_income_uzs - total_expence_uzs)
|
||||
# balance_usd = balance_obj.total_balance_usd + (total_income_usd - total_expence_usd)
|
||||
balance_uzs = counterparty.balance.total_balance_uzs
|
||||
balance_usd = counterparty.balance.total_balance_usd
|
||||
|
||||
# Status aniqlash
|
||||
if balance_uzs > 0:
|
||||
status = 'positive'
|
||||
elif balance_uzs < 0:
|
||||
|
||||
Reference in New Issue
Block a user