extending form for stock creation

This commit is contained in:
Matthias 2021-07-18 02:58:39 +02:00
parent 44482800e4
commit eba5512a38
2 changed files with 19 additions and 1 deletions

View File

@ -217,6 +217,11 @@ class EditPartForm(HelperForm):
label=_('Include parent categories parameter templates'), label=_('Include parent categories parameter templates'),
widget=forms.HiddenInput()) widget=forms.HiddenInput())
initial_stock = forms.IntegerField(required=False,
initial=0,
label=_('Initial stock amount'),
help_text=_('Create stock for this part'))
class Meta: class Meta:
model = Part model = Part
fields = [ fields = [
@ -238,6 +243,7 @@ class EditPartForm(HelperForm):
'default_expiry', 'default_expiry',
'units', 'units',
'minimum_stock', 'minimum_stock',
'initial_stock',
'component', 'component',
'assembly', 'assembly',
'is_template', 'is_template',

View File

@ -44,7 +44,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
@ -487,6 +487,10 @@ class PartCreate(AjaxCreateView):
if not inventree_settings.stock_expiry_enabled(): if not inventree_settings.stock_expiry_enabled():
form.fields['default_expiry'].widget = HiddenInput() form.fields['default_expiry'].widget = HiddenInput()
# Hide the "initial stock amount" field if the feature is not enabled
if not InvenTreeSetting.get_setting('PART_CREATE_INITIAL'):
form.fields['initial_stock'].widget = HiddenInput()
# Hide the default_supplier field (there are no matching supplier parts yet!) # Hide the default_supplier field (there are no matching supplier parts yet!)
form.fields['default_supplier'].widget = HiddenInput() form.fields['default_supplier'].widget = HiddenInput()
@ -547,6 +551,14 @@ class PartCreate(AjaxCreateView):
# Save part and pass category template settings # Save part and pass category template settings
part.save(**{'add_category_templates': add_category_templates}) part.save(**{'add_category_templates': add_category_templates})
# Add stock if set
init_stock = int(request.POST.get('initial_stock', 0))
if init_stock:
stock = StockItem(part=part,
quantity=init_stock,
location=part.default_location)
stock.save()
data['pk'] = part.pk data['pk'] = part.pk
data['text'] = str(part) data['text'] = str(part)