From eca74810aeb618d911b0f7987a8089c5d31ab494 Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Wed, 5 Nov 2025 16:50:29 +0500 Subject: [PATCH] fix party addd --- .../apps/finance/management/commands/count.py | 45 ------------------- .../management/commands/import_party.py | 6 ++- core/apps/orders/models/party.py | 15 ++++--- 3 files changed, 14 insertions(+), 52 deletions(-) delete mode 100644 core/apps/finance/management/commands/count.py diff --git a/core/apps/finance/management/commands/count.py b/core/apps/finance/management/commands/count.py deleted file mode 100644 index 02ae631..0000000 --- a/core/apps/finance/management/commands/count.py +++ /dev/null @@ -1,45 +0,0 @@ -from django.db.models import Count -from core.apps.finance.models import Expence -from django.core.management import BaseCommand - -def get_duplicate_expenses_all(): - """ - Barcha counterparty boyicha bir xil summada yaratilgan expenselarni topadi - """ - expences = Expence.objects.filter( - is_deleted=False, - counterparty__isnull=False - ).values('counterparty', 'price', 'currency').annotate( - count=Count('id') - ).filter(count__gt=1).order_by('-count') - - return expences - -class Command(BaseCommand): - def handle(self, *args, **options): - - - duplicate_data = get_duplicate_expenses_all() - - result = [] - for item in duplicate_data: - expenses = Expence.objects.filter( - is_deleted=False, - counterparty_id=item['counterparty'], - price=item['price'], - currency=item['currency'] - ) - - counterparty = expenses.first().counterparty if expenses.exists() else None - - result.append({ - 'counterparty': counterparty, - 'amount': item['price'], - 'currency': item['currency'], - 'count': item['count'], - 'expenses': expenses - }) - - print(result) - print(len(result)) - diff --git a/core/apps/orders/management/commands/import_party.py b/core/apps/orders/management/commands/import_party.py index 83d972e..67a1b34 100644 --- a/core/apps/orders/management/commands/import_party.py +++ b/core/apps/orders/management/commands/import_party.py @@ -11,7 +11,7 @@ from core.apps.products.models import Product, Unity from core.apps.projects.models import Project, ProjectFolder from core.apps.wherehouse.models import WhereHouse -token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MjI1MDQ1MCwiZXhwIjoxNzYyMzM2ODUwLCJuYmYiOjE3NjIyNTA0NTAsImp0aSI6IkVCOFpPTVZ1ZVJpMDlTOFciLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.8K1SbHvgJDyNYHE6BYIXYesZl-AmFjU6GMeEuJwM_jA" +token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MjMzNjg2MSwiZXhwIjoxNzYyNDIzMjYxLCJuYmYiOjE3NjIzMzY4NjEsImp0aSI6Ino1SVpXRmlTVUJpMDA2eDciLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.yVOvrb-QjdYvCx6GJOW4T5y_N9-Lq2ThYFPu8_J0K3w" headers = {"Authorization": f"Bearer {token}"} @@ -71,6 +71,7 @@ class Command(BaseCommand): paid_amount = 0 calculated_amount = 0 must_pay_amount = 0 + debt_amount = 0 for i in data["warehouse_products"]: product = Product.objects.filter( name__icontains=i["product"]["name"]["uz"] @@ -108,6 +109,7 @@ class Command(BaseCommand): paid_amount += i["paid_amount"] calculated_amount += i["calculated_amount"] must_pay_amount += i["must_pay_amount"] + debt_amount += i['debt_amount'] orders.append(order) party.orders.set(orders) PartyAmount.objects.get_or_create( @@ -117,6 +119,8 @@ class Command(BaseCommand): "calculated_amount": calculated_amount, "paid_amount": paid_amount, "payment_amount": must_pay_amount, + "debt_amount": debt_amount, + "total_expense_amount": item['item'], }, ) self.stdout.write("Parties added") \ No newline at end of file diff --git a/core/apps/orders/models/party.py b/core/apps/orders/models/party.py index 43cf48b..73f27f9 100644 --- a/core/apps/orders/models/party.py +++ b/core/apps/orders/models/party.py @@ -71,12 +71,13 @@ class Party(BaseModel): self.process = 75 elif self.status == 'PARTY_IS_MADE': self.process = 100 - if hasattr(self, 'party_amount') and self.party_amount.paid_amount > 0 and self.party_amount.payment_amount > 0: - self.payment_percentage = (self.party_amount.paid_amount / self.party_amount.payment_amount) * 100 - self.party_amount.save() - else: - self.payment_percentage = 100.0 - self.status == 'PURCHASED' + if not self.payment_percentage: + if hasattr(self, 'party_amount') and self.party_amount.paid_amount > 0 and self.party_amount.payment_amount > 0: + self.payment_percentage = (self.party_amount.paid_amount / self.party_amount.payment_amount) * 100 + self.party_amount.save() + else: + self.payment_percentage = 100.0 + self.status == 'PURCHASED' return super().save(*args, **kwargs) class Meta: @@ -91,6 +92,8 @@ class PartyAmount(BaseModel): calculated_amount = models.BigIntegerField(default=0) paid_amount = models.BigIntegerField(default=0) payment_amount = models.BigIntegerField(default=0) + debt_amount = models.BigIntegerField(default=0) + total_expense_amount = models.BigIntegerField(default=0) def __str__(self): return f'{self.party} amount'