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

@@ -6,4 +6,5 @@ from core.apps.wherehouse.models.inventory import Inventory
@admin.register(Inventory) @admin.register(Inventory)
class InventoryAdmin(admin.ModelAdmin): class InventoryAdmin(admin.ModelAdmin):
list_display = ['id', 'wherehouse', 'is_invalid'] list_display = ['id', 'wherehouse', 'is_invalid']
list_filter = ['wherehouse', 'is_invalid'] list_filter = ['wherehouse', 'is_invalid']
search_fields = ['product__name']

View File

@@ -0,0 +1,123 @@
{
"id": 854,
"type": "another",
"status": "pending",
"total_amount": 873600,
"quantity": 16,
"amount": 54600,
"created_at": "29.10.2025 18:14",
"defect_date": "29.10.2025 18:14",
"product": {
"id": 4015,
"name": {
"uz": "Саморез кровельный 30мм",
"en": "Саморез кровельный 30мм",
"ru": "Саморез кровельный 30мм"
},
"code": null,
"resource": {
"id": 2,
"name": {
"en": "Product",
"ru": "Продукт",
"uz": "Mahsulot",
"tr": "Ürün"
},
"symbol": {
"en": "PRD",
"ru": "ПPO",
"uz": "MAH",
"tr": "URN"
},
"color": "#027A48",
"type": "product"
}
},
"unit": {
"id": 33,
"name": {
"en": "KG",
"ru": "КГ",
"uz": "KG"
},
"symbol": {
"en": "KG",
"ru": "КГ",
"uz": "KG"
},
"is_piece": true
},
"warehouse": {
"id": 50,
"name": "Yunusobod ombor",
"mine": true
},
"project": {
"id": 131,
"name": "Mezbon",
"is_project": false
},
"task": null,
"confirmation_users": [
{
"id": 2726,
"status": "open",
"user": {
"id": 108,
"full_name": "Xudjaqulov Abbos",
"image": null
}
},
{
"id": 2727,
"status": "open",
"user": {
"id": 138,
"full_name": "Baratova Zilola",
"image": null
}
},
{
"id": 2728,
"status": "recieved",
"user": {
"id": 211,
"full_name": "Рустамов Шерзод",
"image": null
}
}
],
"unread_message_count": 1,
"creator": {
"id": 219,
"full_name": "Тухтаева Шахзода",
"image": null
},
"custom_field_values": [
{
"id": 22864,
"value": "5",
"custom_field": {
"id": 56,
"name": {
"uz": "Akt nomer",
"en": "Номер акта",
"ru": "Номер акта"
},
"type": "text"
},
"custom_field_option": null,
"file": null
}
],
"currency": {
"id": 1,
"name": {
"ru": "Узбекский сум",
"uz": "O'zbek so'mi"
},
"symbol": "UZS",
"icon": "https://backend.app.uyqur.uz/public/upload/image/YZfoihq4VHMSHaZziQlp.webp"
},
"description": "Курилиш чегара тортиш ишларида ишлатилди"
}

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