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)
def save(self, *args, **kwargs):
if self.balance_usd > 0:
self.debit_usd = self.balance_usd
self.kredit_usd = Decimal('0.00')
elif self.balance_usd < 0:
self.kredit_usd = abs(self.balance_usd)
self.debit_usd = Decimal('0.00')
# if self.balance_usd > 0:
# self.debit_usd = self.balance_usd
# self.kredit_usd = Decimal('0.00')
# elif self.balance_usd < 0:
# self.kredit_usd = abs(self.balance_usd)
# self.debit_usd = Decimal('0.00')
if self.balance_uzs > 0:
self.debit_uzs = self.balance_uzs
self.kredit_uzs = Decimal('0.00')
elif self.balance_uzs < 0:
self.kredit_uzs = abs(self.balance_uzs)
self.debit_uzs = Decimal('0.00')
# if self.balance_uzs > 0:
# self.debit_uzs = self.balance_uzs
# self.kredit_uzs = Decimal('0.00')
# elif self.balance_uzs < 0:
# self.kredit_uzs = abs(self.balance_uzs)
# self.debit_uzs = Decimal('0.00')
if self.total_balance_usd >= 0:
self.kredit_usd = self.total_balance_usd
@@ -117,7 +117,6 @@ class CounterpartyBalance(BaseModel):
is_deleted=False,
currency='usd'
).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
income = Income.objects.filter(currency='usd', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price")
@@ -125,11 +124,7 @@ class CounterpartyBalance(BaseModel):
expence = Expence.objects.filter(currency='usd', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price")
)['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
def total_balance_uzs(self):
@@ -142,18 +137,16 @@ class CounterpartyBalance(BaseModel):
is_deleted=False,
currency='uzs'
).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
print(total_amount)
income = Income.objects.filter(currency='uzs', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price")
)['total_price'] or 0
print(income)
expence = Expence.objects.filter(currency='uzs', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price")
)['total_price'] or 0
party_amount = total_amount - paid_amount
# if expence != paid_amount:
# return (party_amount + income) - expence
# else:
print(expence)
return (total_amount + income) - expence
def __str__(self):

View File

@@ -346,41 +346,40 @@ class CounterpartyAKTApiView(views.APIView):
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: {
'parties': [],
'incomes': [],
'expences': [],
'incomes': [],
'parties_total': Decimal('0'),
'day_kredit': Decimal('0'),
'day_debit': Decimal('0'),
'day_balance': Decimal('0'),
'balance_type': 'kredit',
'balance_type': 'kredit'
})
# TODO: partiyalarni kun boyicha ajratish
for party in parties:
date_key = party.closed_date.strftime('%Y-%m-%d')
daily_data[date_key]['parties'].append(parties)
# TODO: kirimlarni kun boyicha ajratish
date_key = party.close_date.strftime('%Y-%m-%d')
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)
for income in incomes:
date_key = income.created_at.strftime('%Y-%m-%d')
daily_data[date_key]['incomes'].append(parties)
# TODO: chiqimlarni kun boyicha ajratish
daily_data[date_key]['incomes'].append(income)
for expence in expences:
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):
parties_list = daily_data[date_key]['parties']
incomes_list = daily_data[date_key]['incomes']
expences_list = daily_data[date_key]['expences']
parties_total = sum(
Decimal(p.party_amount.total_price or 0)
for p in parties_list
)
parties_total = daily_data[date_key].get('parties_total', Decimal('0'))
incomes_total = sum(
Decimal(i.price or 0)
for i in incomes_list
@@ -410,4 +409,4 @@ class CounterpartyAKTApiView(views.APIView):
'debit': data['day_debit'],
}
for date, data in daily_data.items()
}
}