wherehouse: change

This commit is contained in:
behruz-dev
2025-10-31 17:29:40 +05:00
parent 021bc7ed83
commit 7c51b47a53

View File

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