Repository: inventree/InvenTree

Issue:
Feature: The way to create initial stock amount from "Create New Part" dialogue. 
Each time I create a new part, I have to go to the Stock Items section and create new stock. It would be faster, if directly on "Create New Part" would be an option to set an initial number of stock items in default location. Just a number, and if it is 0, the initial stock will be ignored and the system work as it is now


Discussion:

Resolving patch:
diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py
--- 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
--- 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 Meta:
             'default_expiry',
             'units',
             'minimum_stock',
+            'initial_stock',
             'component',
             'assembly',
             'is_template',
diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py
--- a/InvenTree/part/views.py
+++ b/InvenTree/part/views.py
@@ -44,7 +44,7 @@
 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 @@ def get_form(self):
         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 @@ def post(self, request, *args, **kwargs):
             # 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)
 


Test patch: