fix
This commit is contained in:
@@ -1,19 +1,18 @@
|
|||||||
from django.db.models import Sum
|
from django.db.models import Sum
|
||||||
from core.apps.finance.models import Expence, Income
|
from core.apps.finance.models import Expence, Income
|
||||||
|
from core.apps.orders.models import Party
|
||||||
|
|
||||||
|
|
||||||
def update_counterparty_balance(counterparty):
|
def update_counterparty_balance(counterparty):
|
||||||
if not counterparty:
|
if not counterparty:
|
||||||
return
|
return
|
||||||
|
|
||||||
# EXPENCE -> DEBIT (faqat CONFIRMED status)
|
|
||||||
expences = Expence.objects.filter(
|
expences = Expence.objects.filter(
|
||||||
counterparty=counterparty,
|
counterparty=counterparty,
|
||||||
is_deleted=False,
|
is_deleted=False,
|
||||||
status='CONFIRMED' # ✅ Status filteri qo'shildi
|
status='CONFIRMED'
|
||||||
)
|
)
|
||||||
|
|
||||||
# Valutasi bo'yicha qo'shish
|
|
||||||
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
|
||||||
@@ -22,8 +21,23 @@ def update_counterparty_balance(counterparty):
|
|||||||
total=Sum('price')
|
total=Sum('price')
|
||||||
)['total'] or 0
|
)['total'] or 0
|
||||||
|
|
||||||
|
parties = Party.objects.filter(
|
||||||
|
orders__counterparty=counterparty,
|
||||||
|
is_deleted=False
|
||||||
|
).distinct()
|
||||||
|
|
||||||
|
party_payment_usd = parties.filter(currency='usd').aggregate(
|
||||||
|
total=Sum('party_amount__payment_amount')
|
||||||
|
)['total'] or 0
|
||||||
|
|
||||||
|
party_payment_uzs = parties.filter(currency='uzs').aggregate(
|
||||||
|
total=Sum('party_amount__payment_amount')
|
||||||
|
)['total'] or 0
|
||||||
|
|
||||||
|
debit_usd = debit_usd - party_payment_usd
|
||||||
|
debit_uzs = debit_uzs - party_payment_uzs
|
||||||
|
|
||||||
|
|
||||||
# INCOME -> KREDIT
|
|
||||||
incomes = Income.objects.filter(
|
incomes = Income.objects.filter(
|
||||||
counterparty=counterparty,
|
counterparty=counterparty,
|
||||||
is_deleted=False
|
is_deleted=False
|
||||||
@@ -38,12 +52,9 @@ def update_counterparty_balance(counterparty):
|
|||||||
)['total'] or 0
|
)['total'] or 0
|
||||||
|
|
||||||
|
|
||||||
# Total (faqat bir valutada qo'shish)
|
|
||||||
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 = debit_uzs
|
||||||
counterparty.total_debit = total_debit
|
counterparty.total_debit = total_debit
|
||||||
@@ -53,4 +64,4 @@ def update_counterparty_balance(counterparty):
|
|||||||
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'
|
||||||
])
|
])
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
from .order import *
|
from .order import *
|
||||||
from ..order_offer import *
|
from .order_offer import *
|
||||||
from .party import *
|
from .party import *
|
||||||
Reference in New Issue
Block a user