diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index c8a5839f4e..3d18d56880 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -233,6 +233,13 @@ class InvenTreeSetting(models.Model): 'validator': bool, }, + 'PART_CREATE_INITIAL': { + 'name': _('Create initial stock'), + 'description': _('Create initial stock on part creation'), + 'default': False, + 'validator': bool, + }, + 'PART_INTERNAL_PRICE': { 'name': _('Internal Prices'), 'description': _('Enable internal prices for parts'), diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index f04f512045..9523550198 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -217,6 +217,11 @@ class EditPartForm(HelperForm): label=_('Include parent categories parameter templates'), widget=forms.HiddenInput()) + initial_stock = forms.IntegerField(required=False, + initial=0, + label=_('Initial stock amount'), + help_text=_('Create stock for this part')) + class Meta: model = Part fields = [ @@ -238,6 +243,7 @@ class EditPartForm(HelperForm): 'default_expiry', 'units', 'minimum_stock', + 'initial_stock', 'component', 'assembly', 'is_template', diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 7919b7d412..fb69241a10 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -44,7 +44,7 @@ from common.files import FileManager from common.views import FileManagementFormView, FileManagementAjaxView from common.forms import UploadFileForm, MatchFieldForm -from stock.models import StockLocation +from stock.models import StockItem, StockLocation import common.settings as inventree_settings @@ -487,6 +487,10 @@ class PartCreate(AjaxCreateView): if not inventree_settings.stock_expiry_enabled(): 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!) form.fields['default_supplier'].widget = HiddenInput() @@ -547,6 +551,14 @@ class PartCreate(AjaxCreateView): # Save part and pass category template settings 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['text'] = str(part) diff --git a/InvenTree/templates/InvenTree/settings/part.html b/InvenTree/templates/InvenTree/settings/part.html index 57ffc95ba8..4f49a63cb4 100644 --- a/InvenTree/templates/InvenTree/settings/part.html +++ b/InvenTree/templates/InvenTree/settings/part.html @@ -23,7 +23,8 @@ {% include "InvenTree/settings/setting.html" with key="PART_SHOW_PRICE_IN_FORMS" icon="fa-dollar-sign" %} {% include "InvenTree/settings/setting.html" with key="PART_SHOW_RELATED" icon="fa-random" %} {% include "InvenTree/settings/setting.html" with key="PART_RECENT_COUNT" icon="fa-clock" %} -