diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 4acd4a594e..1a8048a985 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1109,6 +1109,17 @@ class Part(MPTTModel): return self.parameters.order_by('template__name') + @property + def has_variants(self): + """ Check if this Part object has variants underneath it. """ + + return self.get_all_variants().count() > 0 + + def get_all_variants(self): + """ Return all Part object which exist as a variant under this part. """ + + return self.get_descendants(include_self=False) + def attach_file(instance, filename): """ Function for storing a file for a PartAttachment diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index edf3d28df8..ddd7390246 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -63,6 +63,18 @@ class EditStockLocationForm(HelperForm): ] +class ConvertStockItemForm(HelperForm): + """ + Form for converting a StockItem to a variant of its current part. + """ + + class Meta: + model = StockItem + fields = [ + 'part' + ] + + class CreateStockItemForm(HelperForm): """ Form for creating a new StockItem """ diff --git a/InvenTree/stock/migrations/0043_auto_20200525_0420.py b/InvenTree/stock/migrations/0043_auto_20200525_0420.py new file mode 100644 index 0000000000..4acc19edf2 --- /dev/null +++ b/InvenTree/stock/migrations/0043_auto_20200525_0420.py @@ -0,0 +1,20 @@ +# Generated by Django 3.0.5 on 2020-05-25 04:20 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('part', '0042_auto_20200518_0900'), + ('stock', '0042_auto_20200523_0121'), + ] + + operations = [ + migrations.AlterField( + model_name='stockitem', + name='part', + field=models.ForeignKey(help_text='Base part', limit_choices_to={'active': True, 'virtual': False}, on_delete=django.db.models.deletion.CASCADE, related_name='stock_items', to='part.Part', verbose_name='Base Part'), + ), + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 90a6fd365c..3d9394979c 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -331,7 +331,6 @@ class StockItem(MPTTModel): verbose_name=_('Base Part'), related_name='stock_items', help_text=_('Base part'), limit_choices_to={ - 'is_template': False, 'active': True, 'virtual': False }) diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index 819c138988..bc187588d0 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -93,6 +93,11 @@ InvenTree | {% trans "Stock Item" %} - {{ item }} {% endif %} + {% if item.part.has_variants %} + + {% endif %} {% if item.part.has_test_report_templates %}