fix
This commit is contained in:
@@ -6,44 +6,82 @@ from core.apps.finance.models import Income
|
|||||||
|
|
||||||
|
|
||||||
def update_counterparty_balance(counterparty):
|
def update_counterparty_balance(counterparty):
|
||||||
|
"""Counterpartyning debit va kredit balanslarini exchange_rate bilan hisoblash"""
|
||||||
if not counterparty:
|
if not counterparty:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# EXPENCE -> DEBIT
|
||||||
expences = Expence.objects.filter(
|
expences = Expence.objects.filter(
|
||||||
counterparty=counterparty,
|
counterparty=counterparty,
|
||||||
is_deleted=False,
|
is_deleted=False,
|
||||||
|
status='CONFIRMED'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# USD bo'lganlari
|
||||||
debit_usd = expences.filter(currency='usd').aggregate(
|
debit_usd = expences.filter(currency='usd').aggregate(
|
||||||
total=Sum('price')
|
total=Sum('price')
|
||||||
)['total'] or 0
|
)['total'] or 0
|
||||||
|
|
||||||
debit_uzs = expences.filter(currency='uzs').aggregate(
|
# UZS bo'lganlari (to'g'ridan-to'g'ri)
|
||||||
|
debit_uzs_direct = expences.filter(currency='uzs').aggregate(
|
||||||
total=Sum('price')
|
total=Sum('price')
|
||||||
)['total'] or 0
|
)['total'] or 0
|
||||||
|
|
||||||
|
# USD -> UZS konversiyasi (exchange_rate bilan ko'paytirish)
|
||||||
|
debit_uzs_from_usd = expences.filter(currency='usd').aggregate(
|
||||||
|
total=Sum(
|
||||||
|
Case(
|
||||||
|
When(exchange_rate__gt=0, then=F('price') * F('exchange_rate') / 100),
|
||||||
|
default=0,
|
||||||
|
output_field=DecimalField()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)['total'] or 0
|
||||||
|
|
||||||
|
debit_uzs = debit_uzs_direct + debit_uzs_from_usd
|
||||||
|
|
||||||
|
|
||||||
|
# INCOME -> KREDIT
|
||||||
incomes = Income.objects.filter(
|
incomes = Income.objects.filter(
|
||||||
counterparty=counterparty,
|
counterparty=counterparty,
|
||||||
is_deleted=False
|
is_deleted=False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# USD bo'lganlari
|
||||||
kredit_usd = incomes.filter(currency='usd').aggregate(
|
kredit_usd = incomes.filter(currency='usd').aggregate(
|
||||||
total=Sum('price')
|
total=Sum('price')
|
||||||
)['total'] or 0
|
)['total'] or 0
|
||||||
|
|
||||||
kredit_uzs = incomes.filter(currency='uzs').aggregate(
|
# UZS bo'lganlari (to'g'ridan-to'g'ri)
|
||||||
|
kredit_uzs_direct = incomes.filter(currency='uzs').aggregate(
|
||||||
total=Sum('price')
|
total=Sum('price')
|
||||||
)['total'] or 0
|
)['total'] or 0
|
||||||
|
|
||||||
|
# USD -> UZS konversiyasi
|
||||||
|
kredit_uzs_from_usd = incomes.filter(currency='usd').aggregate(
|
||||||
|
total=Sum(
|
||||||
|
Case(
|
||||||
|
When(exchange_rate__gt=0, then=F('price') * F('exchange_rate') / 100),
|
||||||
|
default=0,
|
||||||
|
output_field=DecimalField()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)['total'] or 0
|
||||||
|
|
||||||
|
kredit_uzs = kredit_uzs_direct + kredit_uzs_from_usd
|
||||||
|
|
||||||
|
|
||||||
|
# Total hisoblash
|
||||||
total_debit = debit_usd + debit_uzs
|
total_debit = debit_usd + debit_uzs
|
||||||
total_kredit = kredit_usd + kredit_uzs
|
total_kredit = kredit_usd + kredit_uzs
|
||||||
|
|
||||||
|
# Modelni yangilash
|
||||||
counterparty.debit_usd = debit_usd
|
counterparty.debit_usd = debit_usd
|
||||||
counterparty.debit_uzs = debit_uzs
|
counterparty.debit_uzs = int(debit_uzs)
|
||||||
counterparty.total_debit = total_debit
|
counterparty.total_debit = int(total_debit)
|
||||||
counterparty.kredit_usd = kredit_usd
|
counterparty.kredit_usd = kredit_usd
|
||||||
counterparty.kredit_uzs = kredit_uzs
|
counterparty.kredit_uzs = int(kredit_uzs)
|
||||||
counterparty.total_kredit = total_kredit
|
counterparty.total_kredit = int(total_kredit)
|
||||||
counterparty.save(update_fields=[
|
counterparty.save(update_fields=[
|
||||||
'debit_usd', 'debit_uzs', 'total_debit',
|
'debit_usd', 'debit_uzs', 'total_debit',
|
||||||
'kredit_usd', 'kredit_uzs', 'total_kredit'
|
'kredit_usd', 'kredit_uzs', 'total_kredit'
|
||||||
|
|||||||
Reference in New Issue
Block a user