This commit is contained in:
behruz-dev
2025-10-25 16:41:09 +05:00
parent 7ba54bfeb2
commit eee3cd5064
16 changed files with 338987 additions and 2 deletions

View File

@@ -0,0 +1,95 @@
import json, 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.utils.parse_date import parse_date
token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MTI4NjcwMywiZXhwIjoxNzYxNjQ2NzAzLCJuYmYiOjE3NjEyODY3MDMsImp0aSI6IjRBNkh4aHI5WkRqOGxVMzUiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.O4NGZL_a3WIrjko5W2sEOBAbM5lv0miVgVa9tfYuyhM'
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('file_path', type=str)
def handle(self, *args, **options):
file_path = options['file_path']
headers = {
"Authorization": f"Bearer {token}"
}
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']}'
res = requests.get(url, headers=headers)
data = res.json()['data']
user = None
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'],
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'],
}
)
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()
if not product:
continue
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()
project_folder = None
if i.get('project'):
project_folder = ProjectFolder.objects.filter(name=i['project']['name']).first()
order, created = Order.objects.get_or_create(
product=product,
unity=unit,
counterparty=counterparty,
currency=i['currency']['symbol'].lower(),
wherehouse=wherehouse,
project_folder=project_folder,
amount=i['amount'],
total_price=i['total_amount'],
quantity=i['quantity'],
)
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(
party=party,
defaults={
"total_price": total_price,
"calculated_amount": calculated_amount,
"paid_amount": paid_amount,
"payment_amount": must_pay_amount,
}
)
self.stdout.write("Parties added")

View File

@@ -0,0 +1,25 @@
# Generated by Django 5.2.4 on 2025-10-25 16:24
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('orders', '0029_alter_order_status'),
('wherehouse', '0017_wherehouse_users'),
]
operations = [
migrations.AlterField(
model_name='order',
name='wherehouse',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='orders', to='wherehouse.wherehouse'),
),
migrations.AlterField(
model_name='party',
name='status',
field=models.CharField(choices=[('NEW', 'yangi'), ('PARTY_IS_MADE', 'partiya qilingan'), ('EXPECTED', 'kutilmoqda'), ('DRAFT', 'qoralama'), ('CANCELLED', 'bekor qilingan'), ('PURCHASED', 'sotib olinmoqda'), ('PROCESS', 'jarayonda'), ('RECIEVED', 'qabul qilingan')], default='NEW', max_length=20),
),
]

View File

@@ -30,7 +30,7 @@ class Order(BaseModel):
Project, on_delete=models.SET_NULL, related_name='orders', null=True, blank=True
)
wherehouse = models.ForeignKey(
WhereHouse, on_delete=models.CASCADE, related_name='orders'
WhereHouse, on_delete=models.CASCADE, related_name='orders', null=True, blank=True
)
employee = models.ForeignKey(User, on_delete=models.CASCADE, related_name='orders', null=True)
counterparty = models.ForeignKey(

View File

@@ -14,6 +14,7 @@ class Party(BaseModel):
('CANCELLED', 'bekor qilingan'),
('PURCHASED', 'sotib olinmoqda'),
('PROCESS', 'jarayonda'),
("RECIEVED", 'qabul qilingan'),
]
PAYMENT_STATUS = (
('PAID', "to'langan"),

View File

@@ -6,7 +6,7 @@ from core.apps.orders.models import Party
@receiver(post_save, sender=Party)
def set_party_number(sender, instance, created, **kwargs):
if created:
if created and not instance.number:
last_party = Party.objects.order_by('number').last()
instance.number = (last_party.number + 1) if last_party else 1
instance.save(update_fields=["number"])

View File

@@ -0,0 +1,12 @@
from datetime import datetime
def parse_date(date_str):
if not date_str:
return None
try:
return datetime.strptime(date_str, '%d.%m.%Y').date()
except ValueError:
try:
return datetime.strptime(date_str, '%Y-%m-%d').date()
except ValueError:
return None

45372
data/party/party.json Normal file

File diff suppressed because it is too large Load Diff

46434
data/party/party2.json Normal file

File diff suppressed because it is too large Load Diff

44649
data/party/party3.json Normal file

File diff suppressed because it is too large Load Diff

43171
data/party/party4.json Normal file

File diff suppressed because it is too large Load Diff

38874
data/party/party5.json Normal file

File diff suppressed because it is too large Load Diff

35056
data/party/party6.json Normal file

File diff suppressed because it is too large Load Diff

35068
data/party/party7.json Normal file

File diff suppressed because it is too large Load Diff

35102
data/party/party8.json Normal file

File diff suppressed because it is too large Load Diff

15125
data/party/party9.json Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -40,3 +40,4 @@ uritemplate==4.2.0
uvicorn==0.35.0
vine==5.1.0
wcwidth==0.2.13
requests