fix
This commit is contained in:
@@ -349,55 +349,51 @@ 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, incomes, expences):
|
||||||
"""Kun boyicha guruhlash"""
|
"""Kun bo'yicha kirim/chiqim/partiyalarni alohida item ko'rinishida qaytarish"""
|
||||||
daily_data = defaultdict(lambda: {
|
daily_data = defaultdict(list) # Har kun uchun ro'yxat
|
||||||
'parties': [],
|
|
||||||
'expences': [],
|
|
||||||
'incomes': [],
|
|
||||||
'day_kredit': Decimal('0'),
|
|
||||||
'day_debit': Decimal('0'),
|
|
||||||
'day_balance': Decimal('0'),
|
|
||||||
'balance_type': 'kredit'
|
|
||||||
})
|
|
||||||
|
|
||||||
# Partiyalarni kunga ajratish (close_date bo'yicha)
|
# --- PARTIES bilan bog'liq kirimlar (kredit) ---
|
||||||
for party in parties:
|
for party in parties:
|
||||||
date_key = party.closed_date.strftime('%Y-%m-%d')
|
date_key = party.closed_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]['day_kredit'] += Decimal(party_total)
|
|
||||||
|
|
||||||
# Kirimlarni kunga ajratish (created_at bo'yicha)
|
daily_data[date_key].append({
|
||||||
|
"type": "kirim",
|
||||||
|
"price": str(getattr(party, 'total_price', 0) or 0),
|
||||||
|
"partiya": PartyAKTSerializer(party).data,
|
||||||
|
"kirim": True,
|
||||||
|
"chiqim": None,
|
||||||
|
})
|
||||||
|
|
||||||
|
# --- INCOME (kirim) ---
|
||||||
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(income)
|
|
||||||
daily_data[date_key]['day_kredit'] += Decimal(income.price or 0)
|
|
||||||
|
|
||||||
# Chiqimlarni kunga ajratish (created_at bo'yicha)
|
daily_data[date_key].append({
|
||||||
|
"type": "kirim",
|
||||||
|
"price": str(income.price or 0),
|
||||||
|
"partiya": None, # Income partiya bilan bog'liq emas
|
||||||
|
"kirim": IncomeListSerializer(income).data,
|
||||||
|
"chiqim": None,
|
||||||
|
})
|
||||||
|
|
||||||
|
# --- EXPENCE (chiqim) ---
|
||||||
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(expence)
|
|
||||||
daily_data[date_key]['day_debit'] += Decimal(expence.price or 0)
|
|
||||||
|
|
||||||
# Har kun uchun balans hisobini yakuniy qilish
|
daily_data[date_key].append({
|
||||||
|
"type": "chiqim",
|
||||||
|
"price": str(expence.price or 0),
|
||||||
|
"partiya": None, # Chiqim partiya bilan bog'liq emas
|
||||||
|
"kirim": None,
|
||||||
|
"chiqim": ExpenceListSerializer(expence).data,
|
||||||
|
})
|
||||||
|
|
||||||
|
# Yakuniy formatga keltirish
|
||||||
result = []
|
result = []
|
||||||
for date_key in sorted(daily_data.keys(), reverse=True):
|
for date_key in sorted(daily_data.keys(), reverse=True):
|
||||||
data = daily_data[date_key]
|
|
||||||
|
|
||||||
day_balance = data['day_kredit'] - data['day_debit']
|
|
||||||
balance_type = 'debit' if day_balance < 0 else 'kredit'
|
|
||||||
|
|
||||||
result.append({
|
result.append({
|
||||||
'date': date_key,
|
"date": date_key,
|
||||||
'parties': PartyAKTSerializer(data['parties'], many=True).data,
|
"items": daily_data[date_key] # har bir kunga tegishli kirim/chiqimlar
|
||||||
'expences': ExpenceListSerializer(data['expences'], many=True).data,
|
|
||||||
'incomes': IncomeListSerializer(data['incomes'], many=True).data,
|
|
||||||
'kredit': str(data['day_kredit']),
|
|
||||||
'debit': str(data['day_debit']),
|
|
||||||
'balance': {
|
|
||||||
'balance': str(abs(day_balance)),
|
|
||||||
'type': balance_type
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return result
|
return result
|
||||||
Reference in New Issue
Block a user