This commit is contained in:
behruz-dev
2025-11-14 15:28:00 +05:00
parent e84937270e
commit 58ba6c3f58
2 changed files with 33 additions and 41 deletions

View File

@@ -69,19 +69,19 @@ class CounterpartyBalance(BaseModel):
debit_uzs = models.DecimalField(max_digits=15, decimal_places=2, default=0.00) debit_uzs = models.DecimalField(max_digits=15, decimal_places=2, default=0.00)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.balance_usd > 0: # if self.balance_usd > 0:
self.debit_usd = self.balance_usd # self.debit_usd = self.balance_usd
self.kredit_usd = Decimal('0.00') # self.kredit_usd = Decimal('0.00')
elif self.balance_usd < 0: # elif self.balance_usd < 0:
self.kredit_usd = abs(self.balance_usd) # self.kredit_usd = abs(self.balance_usd)
self.debit_usd = Decimal('0.00') # self.debit_usd = Decimal('0.00')
if self.balance_uzs > 0: # if self.balance_uzs > 0:
self.debit_uzs = self.balance_uzs # self.debit_uzs = self.balance_uzs
self.kredit_uzs = Decimal('0.00') # self.kredit_uzs = Decimal('0.00')
elif self.balance_uzs < 0: # elif self.balance_uzs < 0:
self.kredit_uzs = abs(self.balance_uzs) # self.kredit_uzs = abs(self.balance_uzs)
self.debit_uzs = Decimal('0.00') # self.debit_uzs = Decimal('0.00')
if self.total_balance_usd >= 0: if self.total_balance_usd >= 0:
self.kredit_usd = self.total_balance_usd self.kredit_usd = self.total_balance_usd
@@ -117,7 +117,6 @@ class CounterpartyBalance(BaseModel):
is_deleted=False, is_deleted=False,
currency='usd' currency='usd'
).distinct() ).distinct()
paid_amount = parties.aggregate(total_price=models.Sum('party_amount__paid_amount'))['total_price'] or 0
total_amount = parties.aggregate(total_price=models.Sum('party_amount__calculated_amount'))['total_price'] or 0 total_amount = parties.aggregate(total_price=models.Sum('party_amount__calculated_amount'))['total_price'] or 0
income = Income.objects.filter(currency='usd', counterparty=self.counterparty).aggregate( income = Income.objects.filter(currency='usd', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price") total_price=models.Sum("price")
@@ -125,10 +124,6 @@ class CounterpartyBalance(BaseModel):
expence = Expence.objects.filter(currency='usd', counterparty=self.counterparty).aggregate( expence = Expence.objects.filter(currency='usd', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price") total_price=models.Sum("price")
)['total_price'] or 0 )['total_price'] or 0
party_amount = total_amount - paid_amount
if expence != paid_amount:
return (party_amount + income) - expence
else:
return (total_amount + income) - expence return (total_amount + income) - expence
@property @property
@@ -142,18 +137,16 @@ class CounterpartyBalance(BaseModel):
is_deleted=False, is_deleted=False,
currency='uzs' currency='uzs'
).distinct() ).distinct()
paid_amount = parties.aggregate(total_price=models.Sum('party_amount__paid_amount'))['total_price'] or 0
total_amount = parties.aggregate(total_price=models.Sum('party_amount__calculated_amount'))['total_price'] or 0 total_amount = parties.aggregate(total_price=models.Sum('party_amount__calculated_amount'))['total_price'] or 0
print(total_amount)
income = Income.objects.filter(currency='uzs', counterparty=self.counterparty).aggregate( income = Income.objects.filter(currency='uzs', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price") total_price=models.Sum("price")
)['total_price'] or 0 )['total_price'] or 0
print(income)
expence = Expence.objects.filter(currency='uzs', counterparty=self.counterparty).aggregate( expence = Expence.objects.filter(currency='uzs', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price") total_price=models.Sum("price")
)['total_price'] or 0 )['total_price'] or 0
party_amount = total_amount - paid_amount print(expence)
# if expence != paid_amount:
# return (party_amount + income) - expence
# else:
return (total_amount + income) - expence return (total_amount + income) - expence
def __str__(self): def __str__(self):

View File

@@ -346,41 +346,40 @@ class CounterpartyAKTApiView(views.APIView):
return Response(response, status=200) return Response(response, status=200)
def _group_by_date(self, parties, incomes, expences): def _group_by_date(self, parties, expences, incomes):
"""Kun boyicha guruhlash"""
daily_data = defaultdict(lambda: { daily_data = defaultdict(lambda: {
'parties': [], 'parties': [],
'incomes': [],
'expences': [], 'expences': [],
'incomes': [],
'parties_total': Decimal('0'),
'day_kredit': Decimal('0'), 'day_kredit': Decimal('0'),
'day_debit': Decimal('0'), 'day_debit': Decimal('0'),
'day_balance': Decimal('0'), 'day_balance': Decimal('0'),
'balance_type': 'kredit', 'balance_type': 'kredit'
}) })
# TODO: partiyalarni kun boyicha ajratish
for party in parties: for party in parties:
date_key = party.closed_date.strftime('%Y-%m-%d') date_key = party.close_date.strftime('%Y-%m-%d')
daily_data[date_key]['parties'].append(parties) daily_data[date_key]['parties'].append(party)
party_total = getattr(party, 'total_price', 0) or 0
daily_data[date_key]['parties_total'] = daily_data[date_key].get('parties_total', Decimal('0')) + Decimal(party_total)
# TODO: kirimlarni kun boyicha ajratish
for income in incomes: for income in incomes:
date_key = income.created_at.strftime('%Y-%m-%d') date_key = income.created_at.strftime('%Y-%m-%d')
daily_data[date_key]['incomes'].append(parties) daily_data[date_key]['incomes'].append(income)
# TODO: chiqimlarni kun boyicha ajratish
for expence in expences: for expence in expences:
date_key = expence.created_at.strftime('%Y-%m-%d') date_key = expence.created_at.strftime('%Y-%m-%d')
daily_data[date_key]['expences'].append(parties) daily_data[date_key]['expences'].append(expence)
for date_key in sorted(daily_data.keys(), reverse=True): for date_key in sorted(daily_data.keys(), reverse=True):
parties_list = daily_data[date_key]['parties'] parties_list = daily_data[date_key]['parties']
incomes_list = daily_data[date_key]['incomes'] incomes_list = daily_data[date_key]['incomes']
expences_list = daily_data[date_key]['expences'] expences_list = daily_data[date_key]['expences']
parties_total = sum( parties_total = daily_data[date_key].get('parties_total', Decimal('0'))
Decimal(p.party_amount.total_price or 0)
for p in parties_list
)
incomes_total = sum( incomes_total = sum(
Decimal(i.price or 0) Decimal(i.price or 0)
for i in incomes_list for i in incomes_list