create stock on import

This commit is contained in:
Matthias 2021-10-24 00:19:17 +02:00
parent bec845003d
commit d97e3cd4e5
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076

View File

@ -42,7 +42,7 @@ from common.files import FileManager
from common.views import FileManagementFormView, FileManagementAjaxView from common.views import FileManagementFormView, FileManagementAjaxView
from common.forms import UploadFileForm, MatchFieldForm from common.forms import UploadFileForm, MatchFieldForm
from stock.models import StockLocation from stock.models import StockItem, StockLocation
import common.settings as inventree_settings import common.settings as inventree_settings
@ -268,6 +268,7 @@ class PartImport(FileManagementFormView):
'Salable', 'Salable',
'Trackable', 'Trackable',
'Virtual', 'Virtual',
'Stock',
] ]
name = 'part' name = 'part'
@ -307,6 +308,7 @@ class PartImport(FileManagementFormView):
'salable': 'salable', 'salable': 'salable',
'trackable': 'trackable', 'trackable': 'trackable',
'virtual': 'virtual', 'virtual': 'virtual',
'stock': 'stock',
} }
file_manager_class = PartFileManager file_manager_class = PartFileManager
@ -403,6 +405,15 @@ class PartImport(FileManagementFormView):
) )
try: try:
new_part.save() new_part.save()
# add stock item if set
if part_data.get('stock', None):
stock = StockItem(
part=new_part,
location=new_part.default_location,
quantity=int(part_data.get('stock', 1)),
)
stock.save()
import_done += 1 import_done += 1
except ValidationError as _e: except ValidationError as _e:
import_error.append(', '.join(set(_e.messages))) import_error.append(', '.join(set(_e.messages)))