From 7c51b47a532ed10ae650c983b844d1fdd7180014 Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Fri, 31 Oct 2025 17:29:40 +0500 Subject: [PATCH] wherehouse: change --- .../management/commands/import_warehouse.py | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/core/apps/wherehouse/management/commands/import_warehouse.py b/core/apps/wherehouse/management/commands/import_warehouse.py index 2627840..a9c7742 100644 --- a/core/apps/wherehouse/management/commands/import_warehouse.py +++ b/core/apps/wherehouse/management/commands/import_warehouse.py @@ -1,8 +1,36 @@ -import json +import json, requests from django.core.management import BaseCommand -from core.apps.wherehouse.models import WhereHouse +from core.apps.wherehouse.models import WhereHouse, Inventory from core.apps.accounts.models import User +from core.apps.products.models import Product, Unity + +headers = { + "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MTgzMzg2OSwiZXhwIjoxNzYxOTIwMjY5LCJuYmYiOjE3NjE4MzM4NjksImp0aSI6IjZSQWE1RzlyT0pGbXF1T2kiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.ACT7oxl-A2eit_bzxeal2jF_xLa0klFObNBpp1HuheE" + } + + + +def get_warehouse_inventory(warehouse_id): + url = f"https://backend.app.uyqur.uz/main/warehouse-product/view?size=2000&warehouse_ids%5B%5D={warehouse_id}" + + response = requests.get(url, headers=headers) + if response.status_code == 200: + return response.json() + return response.json() + + +def create_inventory(data, warehouse, product, unity): + Inventory.objects.get_or_create( + wherehouse=warehouse, + product=product, + unity=unity, + quantity=data['total_quantity'], + defaults={ + "price": data['total_amount'], + "unit_price": data['amount'], + } + ) class Command(BaseCommand): @@ -15,6 +43,7 @@ class Command(BaseCommand): with open(file_path, 'r') as f: data = json.load(f) + count = 0 for item in data['data']['warehouses']: wherehouse, create = WhereHouse.objects.update_or_create( name=item['name'] @@ -24,4 +53,15 @@ class Command(BaseCommand): full_names.append(user['full_name']) users = User.objects.filter(full_name__in=full_names) wherehouse.users.set(users) - self.stdout.write("Warehouses added") \ No newline at end of file + + # Omborxonaga inventory qoshish + data = get_warehouse_inventory(item['id']) + if data: + for product in data['data']['data']: + product_obj = Product.objects.filter(name=product['product']['name']['uz']).first() + unit = Unity.objects.filter(value=product['unit']['name']['uz']).first() + create_inventory(product, wherehouse, product_obj, unit) + count += 1 + + self.stdout.write("Warehouses added") + self.stdout.write(f"{count} ta maxsulot omborga qoshildi") \ No newline at end of file