This commit is contained in:
behruz-dev
2025-11-12 14:45:33 +05:00
parent d376aaa6c1
commit 41a01b4251
5 changed files with 86 additions and 5 deletions

View File

@@ -118,7 +118,7 @@ class CounterpartyBalance(BaseModel):
currency='usd' currency='usd'
).distinct() ).distinct()
paid_amount = parties.aggregate(total_price=models.Sum('party_amount__paid_amount'))['total_price'] or 0 paid_amount = parties.aggregate(total_price=models.Sum('party_amount__paid_amount'))['total_price'] or 0
total_amount = parties.aggregate(total_price=models.Sum('party_amount__total_price'))['total_price'] or 0 total_amount = parties.aggregate(total_price=models.Sum('party_amount__calculated_price'))['total_price'] or 0
income = Income.objects.filter(currency='usd', counterparty=self.counterparty).aggregate( income = Income.objects.filter(currency='usd', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price") total_price=models.Sum("price")
)['total_price'] or 0 )['total_price'] or 0
@@ -141,7 +141,7 @@ class CounterpartyBalance(BaseModel):
currency='uzs' currency='uzs'
).distinct() ).distinct()
paid_amount = parties.aggregate(total_price=models.Sum('party_amount__paid_amount'))['total_price'] or 0 paid_amount = parties.aggregate(total_price=models.Sum('party_amount__paid_amount'))['total_price'] or 0
total_amount = parties.aggregate(total_price=models.Sum('party_amount__total_price'))['total_price'] or 0 total_amount = parties.aggregate(total_price=models.Sum('party_amount__calculated_price'))['total_price'] or 0
income = Income.objects.filter(currency='uzs', counterparty=self.counterparty).aggregate( income = Income.objects.filter(currency='uzs', counterparty=self.counterparty).aggregate(
total_price=models.Sum("price") total_price=models.Sum("price")
)['total_price'] or 0 )['total_price'] or 0

View File

@@ -6,7 +6,7 @@ from core.apps.finance.models import Expence, DeletedExpence
@admin.register(Expence) @admin.register(Expence)
class ExpenceAdmin(admin.ModelAdmin): class ExpenceAdmin(admin.ModelAdmin):
list_display = ['id', 'price', 'cash_transaction', 'status'] list_display = ['id', 'price', 'cash_transaction', 'status']
search_fields = ['party__number'] search_fields = ['party__number', 'counterparty__name']
@admin.register(DeletedExpence) @admin.register(DeletedExpence)
class DeletedExpenceAdmin(admin.ModelAdmin): class DeletedExpenceAdmin(admin.ModelAdmin):

View File

@@ -7,7 +7,7 @@ from core.apps.finance.models import Income, DeletedIncome
class IncomeAdmin(admin.ModelAdmin): class IncomeAdmin(admin.ModelAdmin):
list_display = ['id', 'price', 'cash_transaction'] list_display = ['id', 'price', 'cash_transaction']
list_filter = ['cash_transaction', 'payment_type'] list_filter = ['cash_transaction', 'payment_type']
search_fields = ['party__number', 'counterparty__name']
@admin.register(DeletedIncome) @admin.register(DeletedIncome)
class DeletedIncomeAdmin(admin.ModelAdmin): class DeletedIncomeAdmin(admin.ModelAdmin):

View File

@@ -0,0 +1,82 @@
import requests
from datetime import datetime
from django.core.management import BaseCommand
from core.apps.finance.models import (
Income,
Expence,
TypeIncome,
ExpenceType,
CashTransaction,
PaymentType
)
from core.apps.projects.models import ProjectFolder
from core.apps.accounts.models import User
from core.apps.wherehouse.models import WhereHouse
from core.apps.counterparty.models import Counterparty
from core.apps.orders.models import Party
headers = {
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2Mjg2MzgyOCwiZXhwIjoxNzYyOTUwMjI4LCJuYmYiOjE3NjI4NjM4MjgsImp0aSI6IlBPd2xJd1kwQXZSQlE3UkoiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.1Pi_4vJaTqPRi92zsueZ-eGoTAiIJa-xd2AbRHvg5Gk"
}
def get_data(id):
url = f"https://backend.app.uyqur.uz/main/payment/view?id={id}"
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
return response.json()
def create_expense(data):
statuses = {
"passive": "PENDING",
"active": "CONFIRMED",
"cancel": "CANCELLED"
}
user = User.objects.filter(full_name=data['creator']['full_name']).first()
cash_transaction = CashTransaction.objects.filter(name=data['cash']['name']).first()
expence_type = None
if data.get('financila'):
expence_type = ExpenceType.objects.filter(name=data['financial']['name']).first()
project_folder = None
if data.get('project'):
project_folder = ProjectFolder.objects.filter(name=data['project']['name']).first()
payment_type, created = PaymentType.objects.get_or_create(name=data['payment_type']['name'])
counterparty = None
if data.get('company_person'):
counterparty = Counterparty.objects.filter(name=data['company_person']['name']).first()
party = None
if data.get('order_ids'):
party_number = data.get('order_ids')[0]
party = Party.objects.filter(number=party_number).first()
Expence.objects.update_or_create(
user=user,
cash_transaction=cash_transaction,
expence_type=expence_type,
price=data['amount'],
currency=data['currency']['symbol'].lower(),
payment_type=payment_type,
project_folder= project_folder,
counterparty= counterparty,
exchange_rate= data['currency_amount'],
date=datetime.strptime(data['date'], "%d.%m.%Y").date(),
comment=data['description'],
status=statuses.get(data['status']),
party=party,
)
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('ids', type=int, nargs='+')
def handle(self, *args, **options):
ids = options['ids']
for id in ids:
data = get_data(id)
create_expense(data['data'])
self.stdout.write(self.style.SUCCESS("Expense qo'shildi"))

View File

@@ -100,7 +100,6 @@ class PartyAmount(BaseModel):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.total_price = self.party.orders.aggregate(total_price=models.Sum('total_price'))['total_price'] or 0 self.total_price = self.party.orders.aggregate(total_price=models.Sum('total_price'))['total_price'] or 0
self.calculated_amount = self.total_price
if self.calculated_amount < self.paid_amount: if self.calculated_amount < self.paid_amount:
self.overdue_amount = self.paid_amount - self.calculated_amount self.overdue_amount = self.paid_amount - self.calculated_amount
self.payment_amount = 0 self.payment_amount = 0