change party command

This commit is contained in:
behruz-dev
2025-10-29 16:24:58 +05:00
parent 9efc8aef40
commit 82d85f8152
7 changed files with 5407 additions and 74 deletions

View File

@@ -1,4 +1,5 @@
import json
from django.core.management import BaseCommand
from core.apps.counterparty.models import Counterparty
@@ -9,24 +10,27 @@ class Command(BaseCommand):
parser.add_argument("file_path", type=str)
def handle(self, *args, **options):
file_path = options['file_path']
file_path = options["file_path"]
with open(file_path, 'r') as f:
with open(file_path, "r") as f:
data = json.load(f)
for item in data['data']['data']:
for item in data["data"]["data"]:
Counterparty.objects.get_or_create(
name=item['name'],
name=item["name"],
defaults={
'phone': item['person']['phone'],
'inn': item['person']['tin'],
'balance_currency': item['balances'][0]['currency']['symbol'].lower() if item['balances'] else 'uzs',
'is_archived': item['is_archived'],
'balance': item['total_amount'],
'total_debit': item['debt_amount'],
'total_kredit': item['credit_amount'],
"phone": item["person"]["phone"],
"inn": item["person"]["tin"],
"balance_currency": (
item["balances"][0]["currency"]["symbol"].lower()
if item["balances"]
else "uzs"
),
"is_archived": item["is_archived"],
"balance": item["total_amount"],
"total_debit": item["debt_amount"],
"total_kredit": item["credit_amount"],
},
)
self.stdout.write("Counterparties added")

View File

@@ -5,4 +5,14 @@ from core.apps.orders.models import Order
@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
list_display = ['id', 'product', 'wherehouse', 'currency']
list_display = ['id', 'product', 'wherehouse', 'currency', 'counterparty']
# # Statuslar: {'ORDERED', 'PARTIALLY_RECIEVED', 'pending', "open", "buying", "rejected", "recieved", "passive"}
# open -> yangi == NEW
# ordered -> partiya qilingan == PARTY_IS_MADE
# pending -> kutilmoqda == EXPECTED
# passive -> qoralama == DRAFT
# recieved -> yopilgan == RECIEVED
# rejected -> bekor qilingan == CANCELLED
# buying -> sotib olinmoqda == PURCHASED
# partially_recieved -> jarayonda == PROCESS

View File

@@ -1,6 +1,6 @@
from django.contrib import admin
from core.apps.orders.models import Party, PartyAmount, DeletedParty
from core.apps.orders.models import DeletedParty, Party, PartyAmount
class PartyAmountInline(admin.StackedInline):
@@ -11,17 +11,28 @@ class PartyAmountInline(admin.StackedInline):
@admin.register(Party)
class PartyAdmin(admin.ModelAdmin):
list_display = ['id','number','mediator', 'delivery_date', 'payment_date', 'is_deleted']
list_display = [
"id",
"number",
"mediator",
"delivery_date",
"payment_date",
"is_deleted",
]
inlines = [PartyAmountInline]
search_fields = [
"number",
]
@admin.register(PartyAmount)
class PartyAmountAdmin(admin.ModelAdmin):
list_display = ['id', 'total_price', 'cost_amount']
list_display = ["id", "total_price", "cost_amount"]
def has_module_permission(self, request):
return False
@admin.register(DeletedParty)
class DeletedPartyAdmin(admin.ModelAdmin):
list_display = ['id', 'deleted_date', 'party']
list_display = ["id", "deleted_date", "party"]

View File

@@ -20,6 +20,8 @@ class Command(BaseCommand):
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
statuses = set()
for item in data['data']['data']:
product_name = item['product']['name']['uz']
unit_name = item['unit']['name']['uz']
@@ -53,6 +55,8 @@ class Command(BaseCommand):
created_at=created_at,
date=delivery_date
)
break
statuses.add(item['status'].upper())
# break
self.stdout.write(self.style.SUCCESS("Orders imported successfully ✅"))
self.stdout.write(f"Statuslar: {statuses}")

View File

@@ -1,86 +1,110 @@
import json, requests
import json
import requests
from django.core.management import BaseCommand
from core.apps.orders.models import Order, Party, PartyAmount
from core.apps.accounts.models import User
from core.apps.products.models import Product, Unity
from core.apps.counterparty.models import Counterparty
from core.apps.wherehouse.models import WhereHouse
from core.apps.projects.models import Project, ProjectFolder
from core.apps.orders.models import Order, Party, PartyAmount
from core.apps.orders.utils.parse_date import parse_date
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.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MTczMTI4MSwiZXhwIjoxNzYxODE3NjgxLCJuYmYiOjE3NjE3MzEyODEsImp0aSI6ImszNVRQUDBiblpSU3ZFS24iLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.G5rjiIV-roIgEZzH9s8XgLYnS9tunnMgnCa2U6S3vd0"
token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MTI4NjcwMywiZXhwIjoxNzYxNjQ2NzAzLCJuYmYiOjE3NjEyODY3MDMsImp0aSI6IjRBNkh4aHI5WkRqOGxVMzUiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.O4NGZL_a3WIrjko5W2sEOBAbM5lv0miVgVa9tfYuyhM'
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('file_path', type=str)
parser.add_argument("file_path", type=str)
def handle(self, *args, **options):
file_path = options['file_path']
headers = {
"Authorization": f"Bearer {token}"
}
file_path = options["file_path"]
headers = {"Authorization": f"Bearer {token}"}
with open(file_path, 'r') as f:
with open(file_path, "r") as f:
data = json.load(f)
for item in data['data']['data']:
url = f'https://backend.app.uyqur.uz/main/supply/order-view?id={item['id']}'
statuses = {
"open": "NEW",
"ordered": "PARTY_IS_MADE",
"pending": "EXPECTED",
"passive": "DRAFT",
"recieved": "RECIEVED",
"rejected": "CANCELLED",
"buying": "PURCHASED",
"partially_recieved": "PROCESS"
}
for item in data["data"]["data"]:
url = f"https://backend.app.uyqur.uz/main/supply/order-view?id={item['id']}"
res = requests.get(url, headers=headers)
data = res.json()['data']
data = res.json()["data"]
user = None
if data.get('agent'):
user = User.objects.filter(full_name=data['agent']['full_name']).first()
if data.get("agent"):
user = User.objects.filter(full_name=data["agent"]["full_name"]).first()
if not user:
continue
party, created = Party.objects.get_or_create(
number=data['id'],
party, created = Party.objects.update_or_create(
number=data["id"],
defaults={
'mediator': user,
'delivery_date': parse_date(data['delivery_date']),
'closed_date': parse_date(data['recieved_date']),
'order_date': parse_date(data['ordered_date']),
'payment_date': parse_date(data['payment_date']) if parse_date(data['payment_date']) else parse_date(data['ordered_date']),
'status': data['status'].upper(),
'payment_percentage': data['payment_percent'],
'process': data['percent'],
}
"mediator": user,
"delivery_date": parse_date(data["delivery_date"]),
"closed_date": parse_date(data["recieved_date"]),
"order_date": parse_date(data["ordered_date"]),
"payment_date": (
parse_date(data["payment_date"])
if parse_date(data["payment_date"])
else parse_date(data["ordered_date"])
),
"status": statuses.get(data["status"].lower()),
"payment_percentage": data["payment_percent"],
"process": data["percent"],
},
)
statuses.add(data["status"].upper())
orders = []
total_price = 0
paid_amount = 0
calculated_amount = 0
must_pay_amount = 0
for i in data['warehouse_products']:
product = Product.objects.filter(name__icontains=i['product']['name']['uz']).first()
for i in data["warehouse_products"]:
product = Product.objects.filter(
name__icontains=i["product"]["name"]["uz"]
).first()
if not product:
continue
unit = Unity.objects.filter(value=i['unit']['name']['uz']).first()
counterparty = Counterparty.objects.filter(name=i['company_person']['name']).first()
unit = Unity.objects.filter(value=i["unit"]["name"]["uz"]).first()
counterparty = Counterparty.objects.filter(
name=i["company_person"]["name"]
).first()
wherehouse = None
if i.get('warehouse'):
wherehouse = WhereHouse.objects.filter(name=i.get('warehouse').get('name')).first()
if i.get("warehouse"):
wherehouse = WhereHouse.objects.filter(
name=i.get("warehouse").get("name")
).first()
project_folder = None
if i.get('project'):
project_folder = ProjectFolder.objects.filter(name=i['project']['name']).first()
if i.get("project"):
project_folder = ProjectFolder.objects.filter(
name=i["project"]["name"]
).first()
order, created = Order.objects.get_or_create(
order = Order.objects.create(
product=product,
amount=i["amount"],
total_price=i["total_amount"],
quantity=i["quantity"],
unity=unit,
counterparty=counterparty,
currency=i['currency']['symbol'].lower(),
wherehouse=wherehouse,
currency=i["currency"]["symbol"].lower(),
project_folder=project_folder,
amount=i['amount'],
total_price=i['total_amount'],
quantity=i['quantity'],
wherehouse=wherehouse,
counterparty=counterparty,
)
total_price += i['total_amount']
paid_amount += i['paid_amount']
calculated_amount += i['calculated_amount']
must_pay_amount += i['must_pay_amount']
total_price += i["total_amount"]
paid_amount += i["paid_amount"]
calculated_amount += i["calculated_amount"]
must_pay_amount += i["must_pay_amount"]
orders.append(order)
party.orders.set(orders)
PartyAmount.objects.get_or_create(
@@ -90,6 +114,7 @@ class Command(BaseCommand):
"calculated_amount": calculated_amount,
"paid_amount": paid_amount,
"payment_amount": must_pay_amount,
}
},
)
self.stdout.write("Parties added")
self.stdout.write(f"Statuslar: {statuses}")

5279
data/counterparty/cp1.json Normal file

File diff suppressed because it is too large Load Diff