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")