fucking counterparty statistics not done yet! fuck
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.apps import apps
|
||||||
|
|
||||||
from core.apps.shared.models import BaseModel, Region, District
|
from core.apps.shared.models import BaseModel, Region, District
|
||||||
from core.apps.accounts.models import User
|
from core.apps.accounts.models import User
|
||||||
@@ -99,18 +100,61 @@ class CounterpartyBalance(BaseModel):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def total_balance_usd(self):
|
def total_balance_usd(self):
|
||||||
|
Party = apps.get_model('orders', 'Party')
|
||||||
|
|
||||||
debit = Decimal(self.debit_usd or 0)
|
debit = Decimal(self.debit_usd or 0)
|
||||||
kredit = Decimal(self.kredit_usd or 0)
|
kredit = Decimal(self.kredit_usd or 0)
|
||||||
return debit - kredit
|
|
||||||
|
party_payment = Party.objects.filter(
|
||||||
|
orders__counterparty=self.counterparty,
|
||||||
|
is_deleted=False,
|
||||||
|
currency='usd'
|
||||||
|
).aggregate(
|
||||||
|
payment_amount=models.Sum('party_amount__payment_amount')
|
||||||
|
)['payment_amount'] or 0
|
||||||
|
|
||||||
|
party_overdue = Party.objects.filter(
|
||||||
|
orders__counterparty=self.counterparty,
|
||||||
|
is_deleted=False,
|
||||||
|
currency='usd'
|
||||||
|
).aggregate(
|
||||||
|
overdue_amount=models.Sum('party_amount__overdue_amount')
|
||||||
|
)['overdue_amount'] or 0
|
||||||
|
|
||||||
|
party_amount = Decimal(party_payment or 0) - Decimal(party_overdue or 0)
|
||||||
|
|
||||||
|
return debit - kredit + party_amount
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def total_balance_uzs(self):
|
def total_balance_uzs(self):
|
||||||
|
Party = apps.get_model('orders', 'Party')
|
||||||
|
|
||||||
debit = Decimal(self.debit_uzs or 0)
|
debit = Decimal(self.debit_uzs or 0)
|
||||||
kredit = Decimal(self.kredit_uzs or 0)
|
kredit = Decimal(self.kredit_uzs or 0)
|
||||||
return debit - kredit
|
|
||||||
|
party_payment = Party.objects.filter(
|
||||||
|
orders__counterparty=self.counterparty,
|
||||||
|
is_deleted=False,
|
||||||
|
currency='uzs'
|
||||||
|
).aggregate(
|
||||||
|
payment_amount=models.Sum('party_amount__payment_amount')
|
||||||
|
)['payment_amount'] or 0
|
||||||
|
|
||||||
|
party_overdue = Party.objects.filter(
|
||||||
|
orders__counterparty=self.counterparty,
|
||||||
|
is_deleted=False,
|
||||||
|
currency='uzs'
|
||||||
|
).aggregate(
|
||||||
|
overdue_amount=models.Sum('party_amount__overdue_amount')
|
||||||
|
)['overdue_amount'] or 0
|
||||||
|
|
||||||
|
party_amount = Decimal(party_payment or 0) - Decimal(party_overdue or 0)
|
||||||
|
|
||||||
|
return debit - kredit + party_amount
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.counterparty.name} | USD: {self.total_balance_usd} | UZS: {self.total_balance_uzs}"
|
return f"{self.counterparty.name}"
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = "Kontragent Balansi"
|
verbose_name = "Kontragent Balansi"
|
||||||
|
|||||||
@@ -199,11 +199,9 @@ class CounterPartyIncomeExpenceStatisticsApiView(views.APIView):
|
|||||||
def get(self, request, id):
|
def get(self, request, id):
|
||||||
counterparty = get_object_or_404(Counterparty, id=id)
|
counterparty = get_object_or_404(Counterparty, id=id)
|
||||||
|
|
||||||
# Income va Expence querysetlari
|
|
||||||
incomes = Income.objects.filter(counterparty=counterparty, is_deleted=False)
|
incomes = Income.objects.filter(counterparty=counterparty, is_deleted=False)
|
||||||
expences = Expence.objects.filter(counterparty=counterparty, is_deleted=False)
|
expences = Expence.objects.filter(counterparty=counterparty, is_deleted=False)
|
||||||
|
|
||||||
# Balanslar valyutalar bo'yicha
|
|
||||||
income_by_currency = {'uzs': {'total': Decimal(0), 'count': 0, 'amount_uzs': Decimal(0)},
|
income_by_currency = {'uzs': {'total': Decimal(0), 'count': 0, 'amount_uzs': Decimal(0)},
|
||||||
'usd': {'total': Decimal(0), 'count': 0, 'amount_uzs': Decimal(0)}}
|
'usd': {'total': Decimal(0), 'count': 0, 'amount_uzs': Decimal(0)}}
|
||||||
for income in incomes:
|
for income in incomes:
|
||||||
@@ -224,18 +222,17 @@ class CounterPartyIncomeExpenceStatisticsApiView(views.APIView):
|
|||||||
expence_by_currency[currency]['count'] += 1
|
expence_by_currency[currency]['count'] += 1
|
||||||
expence_by_currency[currency]['amount_uzs'] += amount * rate if currency == 'usd' else amount
|
expence_by_currency[currency]['amount_uzs'] += amount * rate if currency == 'usd' else amount
|
||||||
|
|
||||||
# Income/Expence summalari
|
|
||||||
total_income_uzs = sum(v['amount_uzs'] for v in income_by_currency.values())
|
total_income_uzs = sum(v['amount_uzs'] for v in income_by_currency.values())
|
||||||
total_expence_uzs = sum(v['amount_uzs'] for v in expence_by_currency.values())
|
total_expence_uzs = sum(v['amount_uzs'] for v in expence_by_currency.values())
|
||||||
total_income_usd = income_by_currency['usd']['total']
|
total_income_usd = income_by_currency['usd']['total']
|
||||||
total_expence_usd = expence_by_currency['usd']['total']
|
total_expence_usd = expence_by_currency['usd']['total']
|
||||||
|
|
||||||
# Kontragent balansi
|
# balance_obj, _ = CounterpartyBalance.objects.get_or_create(counterparty=counterparty)
|
||||||
balance_obj, _ = CounterpartyBalance.objects.get_or_create(counterparty=counterparty)
|
# balance_uzs = balance_obj.total_balance_uzs + (total_income_uzs - total_expence_uzs)
|
||||||
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_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
|
||||||
|
|
||||||
# Status aniqlash
|
|
||||||
if balance_uzs > 0:
|
if balance_uzs > 0:
|
||||||
status = 'positive'
|
status = 'positive'
|
||||||
elif balance_uzs < 0:
|
elif balance_uzs < 0:
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ from core.apps.orders.models import Party
|
|||||||
|
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MjUxMDQyMywiZXhwIjoxNzYyNTk2ODIzLCJuYmYiOjE3NjI1MTA0MjMsImp0aSI6IlNPMmx5VjJ3Mllmb3BlSXEiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.3DemwyRz2FMzMm-JRBOqGPSgu_m4s4ndWz56e_ROp8A"
|
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2Mjc3NTkzNCwiZXhwIjoxNzYyODYyMzM0LCJuYmYiOjE3NjI3NzU5MzQsImp0aSI6ImZwOFlWMmJ1MEdPdmpXeHUiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.sii0OxzYjY8hQPojwoTa9B7O1jTPcowQ_kPOafJM4ho"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ from core.apps.orders.models import Party
|
|||||||
|
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MjUxMDQyMywiZXhwIjoxNzYyNTk2ODIzLCJuYmYiOjE3NjI1MTA0MjMsImp0aSI6IlNPMmx5VjJ3Mllmb3BlSXEiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.3DemwyRz2FMzMm-JRBOqGPSgu_m4s4ndWz56e_ROp8A"
|
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2Mjc3NTkzNCwiZXhwIjoxNzYyODYyMzM0LCJuYmYiOjE3NjI3NzU5MzQsImp0aSI6ImZwOFlWMmJ1MEdPdmpXeHUiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.sii0OxzYjY8hQPojwoTa9B7O1jTPcowQ_kPOafJM4ho"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from core.apps.shared.models import BaseModel
|
from core.apps.shared.models.base import BaseModel
|
||||||
from core.apps.products.models import Product, Unity
|
from core.apps.products.models.product import Product
|
||||||
from core.apps.projects.models import Project, ProjectFolder
|
from core.apps.products.models.unity import Unity
|
||||||
from core.apps.accounts.models import User
|
from core.apps.projects.models.project import Project, ProjectFolder
|
||||||
from core.apps.wherehouse.models import WhereHouse
|
from core.apps.accounts.models.user import User
|
||||||
from core.apps.counterparty.models import Counterparty
|
from core.apps.wherehouse.models.wherehouse import WhereHouse
|
||||||
|
from core.apps.counterparty.models.conterparty import Counterparty
|
||||||
|
|
||||||
|
|
||||||
class Order(BaseModel):
|
class Order(BaseModel):
|
||||||
|
|||||||
Reference in New Issue
Block a user