wherehouse: add import invalid product command

This commit is contained in:
behruz-dev
2025-10-31 18:39:44 +05:00
parent f2f66754b6
commit c60b3ef985
3 changed files with 196 additions and 1 deletions

View File

@@ -0,0 +1,71 @@
from datetime import datetime
import json, requests
from django.core.management import BaseCommand
from core.apps.wherehouse.models import WhereHouse, Inventory, InvalidProduct
from core.apps.products.models import Product, Unity
from core.apps.accounts.models import User
from core.apps.projects.models import ProjectFolder
headers = {
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MTgzMzg2OSwiZXhwIjoxNzYxOTIwMjY5LCJuYmYiOjE3NjE4MzM4NjksImp0aSI6IjZSQWE1RzlyT0pGbXF1T2kiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.ACT7oxl-A2eit_bzxeal2jF_xLa0klFObNBpp1HuheE"
}
def get_data():
url = f'https://backend.app.uyqur.uz/main/warehouse-defect/view?size=203'
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
return response.json()
class Command(BaseCommand):
def handle(self, *args, **options):
statuses = {
"pending": "EXPECTED",
"open": "OPEN",
"recieved": "ACCEPTED",
"rejected": "CANCELLED",
}
types = {
"another": "OTHER",
"lost": "LOST",
"broken": "BROKEN",
}
data = get_data()
for item in data['data']['data']:
product, created = Product.objects.get_or_create(name=item['product']['name']['uz'])
unity, created = Unity.objects.get_or_create(value=item['unit']['name']['uz'])
warehouse = WhereHouse.objects.filter(name=item['warehouse']['name']).first()
project_folder = ProjectFolder.objects.filter(name=item['project']['name']).first()
inventory = Inventory.objects.create(
product=product,
unity=unity,
wherehouse=warehouse,
quantity=item['quantity'],
price=item['total_amount'],
project_folder=project_folder,
is_invalid=True,
unit_price=item['amount'],
)
invalid_product, created = InvalidProduct.objects.get_or_create(
inventory=inventory,
defaults={
"project_folder": project_folder,
"created_date": datetime.strptime(item['created_at'], "%d.%m.%Y %H:%M"),
"expiry_date": datetime.strptime(item['defect_date'], "%d.%m.%Y %H:%M"),
"comment": item['description'],
"status": statuses.get(item['status']),
"amount": item['amount'],
"invalid_status": types.get(item['type']),
}
)
users = []
for user in item['confirmation_users']:
users.append(User.objects.filter(full_name=user['user']['full_name']).first())
invalid_product.witnesses.set(users)
self.stdout.write("Invalid Productlar qo'shildi")