From 3bb247a1352f86e4a17ec2416b4d346c8659361e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 20 Oct 2020 23:27:43 +1100 Subject: [PATCH] Create an initial stockitem output when a new build is created --- InvenTree/InvenTree/views.py | 8 ++++---- .../migrations/0024_auto_20201020_1144.py | 20 +++++++++++++++++++ InvenTree/build/models.py | 19 +++++++++++++++++- InvenTree/build/templates/build/detail.html | 15 +++++++++++++- InvenTree/build/views.py | 9 ++++----- InvenTree/stock/serializers.py | 3 ++- 6 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 InvenTree/build/migrations/0024_auto_20201020_1144.py diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index eb168ad547..6ddee71682 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -316,13 +316,13 @@ class AjaxCreateView(AjaxMixin, CreateView): - Handles form validation via AJAX POST requests """ - def pre_save(self, **kwargs): + def pre_save(self, form, request, **kwargs): """ Hook for doing something before the form is validated """ pass - def post_save(self, new_object, **kwargs): + def post_save(self, new_object, request, **kwargs): """ Hook for doing something with the created object after it is saved """ @@ -354,9 +354,9 @@ class AjaxCreateView(AjaxMixin, CreateView): if self.form.is_valid(): - self.pre_save() + self.pre_save(self.form, request) self.object = self.form.save() - self.post_save(self.object) + self.post_save(self.object, request) # Return the PK of the newly-created object data['pk'] = self.object.pk diff --git a/InvenTree/build/migrations/0024_auto_20201020_1144.py b/InvenTree/build/migrations/0024_auto_20201020_1144.py new file mode 100644 index 0000000000..2d7c649cd5 --- /dev/null +++ b/InvenTree/build/migrations/0024_auto_20201020_1144.py @@ -0,0 +1,20 @@ +# Generated by Django 3.0.7 on 2020-10-20 11:44 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('part', '0051_bomitem_optional'), + ('build', '0023_auto_20201020_1009'), + ] + + operations = [ + migrations.AlterField( + model_name='build', + name='part', + field=models.ForeignKey(help_text='Select part to build', limit_choices_to={'active': True, 'assembly': True, 'virtual': False}, on_delete=django.db.models.deletion.CASCADE, related_name='builds', to='part.Part', verbose_name='Part'), + ), + ] diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index ca4bb586fe..3ee6aad426 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -111,7 +111,6 @@ class Build(MPTTModel): on_delete=models.CASCADE, related_name='builds', limit_choices_to={ - 'is_template': False, 'assembly': True, 'active': True, 'virtual': False, @@ -226,6 +225,24 @@ class Build(MPTTModel): return new_ref + def createInitialStockItem(self, user): + """ + Create an initial output StockItem to be completed by this build. + """ + + output = StockModels.StockItem.objects.create( + part=self.part, # Link to the parent part + location=None, # No location (yet) until it is completed + quantity=self.quantity, + batch='', # The 'batch' code is not set until the item is completed + build=self, # Point back to this build + is_building=True, # Mark this StockItem as building + ) + + output.save() + + # TODO - Add a transaction note to the new StockItem + @transaction.atomic def cancelBuild(self, user): """ Mark the Build as CANCELLED diff --git a/InvenTree/build/templates/build/detail.html b/InvenTree/build/templates/build/detail.html index 6abbc69bc5..7d4c21aa5b 100644 --- a/InvenTree/build/templates/build/detail.html +++ b/InvenTree/build/templates/build/detail.html @@ -33,7 +33,20 @@ {% if build.take_from %} {{ build.take_from }} {% else %} - {% trans "Stock can be taken from any available location." %} + {% trans "Stock can be taken from any available location." %} + {% endif %} + + + + + {% trans "Destination" %} + + {% if build.destination %} + + {{ build.destination }} + + {% else %} + {% trans "Destination location not specified" %} {% endif %} diff --git a/InvenTree/build/views.py b/InvenTree/build/views.py index 02e4a8d735..e8aeccab36 100644 --- a/InvenTree/build/views.py +++ b/InvenTree/build/views.py @@ -439,15 +439,14 @@ class BuildCreate(AjaxCreateView): return { 'success': _('Created new build'), } - - def post_save(self, new_object): + + def post_save(self, new_object, request, **kwargs): """ - Called immediately after the build has been created. + Called immediately after a new Build object is created. """ build = new_object - - print("Created:", build) + build.createInitialStockItem(request.user) class BuildUpdate(AjaxUpdateView): diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index d257f12f97..5bea1c4aae 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -154,8 +154,9 @@ class StockItemSerializer(InvenTreeModelSerializer): 'allocated', 'batch', 'belongs_to', - 'customer', + 'build', 'build_order', + 'customer', 'in_stock', 'is_building', 'link',