change counterparty akt statistics api
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from decimal import Decimal
|
||||
from collections import defaultdict
|
||||
|
||||
from django.db.models import Sum, Count, Q
|
||||
from django.shortcuts import get_object_or_404
|
||||
@@ -232,9 +233,6 @@ class CounterPartyIncomeExpenceStatisticsApiView(views.APIView):
|
||||
total_income_usd = income_by_currency['usd']['total']
|
||||
total_expence_usd = expence_by_currency['usd']['total']
|
||||
|
||||
# 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
|
||||
|
||||
@@ -317,6 +315,9 @@ class CounterpartyAKTApiView(views.APIView):
|
||||
expences = expences.filter(currency=currency)
|
||||
incomes = incomes.filter(currency=currency)
|
||||
|
||||
# TODO: date boyicha guruhlash
|
||||
daily_breakdown = self._group_by_date(parties, incomes, expences)
|
||||
|
||||
# TODO: total kreditni hisoblash kerak: Sum(party total_price) + Sum(income total_price)
|
||||
parties_total_price = parties.aggregate(total_price=Sum('party_amount__total_price'))['total_price'] or 0
|
||||
income_total_price = incomes.aggregate(total_price=Sum('price'))['total_price'] or 0
|
||||
@@ -333,9 +334,7 @@ class CounterpartyAKTApiView(views.APIView):
|
||||
type = 'debit' if final_balance < 0 else 'kredit'
|
||||
|
||||
response = {
|
||||
"parties": PartyDetailSerializer(parties, many=True).data,
|
||||
"expences": ExpenceListSerializer(expences, many=True).data,
|
||||
"incomes": IncomeListSerializer(incomes, many=True).data,
|
||||
"daily_breakdown": daily_breakdown,
|
||||
"total_kredit": str(total_kredit),
|
||||
"total_debit": str(total_debit),
|
||||
"final_balance": {
|
||||
@@ -345,3 +344,69 @@ class CounterpartyAKTApiView(views.APIView):
|
||||
}
|
||||
|
||||
return Response(response, status=200)
|
||||
|
||||
def _group_by_date(self, parties, incomes, expences):
|
||||
daily_data = defaultdict(lambda: {
|
||||
'parties': [],
|
||||
'incomes': [],
|
||||
'expences': [],
|
||||
'day_kredit': Decimal('0'),
|
||||
'day_debit': Decimal('0'),
|
||||
'day_balance': Decimal('0'),
|
||||
'balance_type': 'kredit',
|
||||
})
|
||||
|
||||
# TODO: partiyalarni kun boyicha ajratish
|
||||
for party in parties:
|
||||
date_key = party.close_date.strftime('%Y-%m-%d')
|
||||
daily_data[date_key]['parties'].append(parties)
|
||||
|
||||
# TODO: kirimlarni kun boyicha ajratish
|
||||
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
|
||||
for expence in expences:
|
||||
date_key = expence.created_at.strftime('%Y-%m-%d')
|
||||
daily_data[date_key]['expences'].append(parties)
|
||||
|
||||
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
|
||||
)
|
||||
incomes_total = sum(
|
||||
Decimal(i.price or 0)
|
||||
for i in incomes_list
|
||||
)
|
||||
day_kredit = parties_total + incomes_total
|
||||
|
||||
day_debit = sum(
|
||||
Decimal(e.price or 0)
|
||||
for e in expences_list
|
||||
)
|
||||
|
||||
daily_data[date_key].update({
|
||||
'parties': PartyDetailSerializer(parties_list, many=True).data,
|
||||
'expences': ExpenceListSerializer(expences_list, many=True).data,
|
||||
'incomes': IncomeListSerializer(incomes_list, many=True).data,
|
||||
'day_kredit': str(day_kredit),
|
||||
'day_debit': str(day_debit),
|
||||
})
|
||||
|
||||
return {
|
||||
date: {
|
||||
'date': date,
|
||||
'parties': data['parties'],
|
||||
'expences': data['expences'],
|
||||
'incomes': data['incomes'],
|
||||
'kredit': data['day_kredit'],
|
||||
'debit': data['day_debit'],
|
||||
}
|
||||
for date, data in daily_data.items()
|
||||
}
|
||||
Reference in New Issue
Block a user