From 750dfcda077f644633355408f3ac1cf89ae48b99 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 17 Feb 2020 21:52:31 +1100 Subject: [PATCH] Add 'parent' field for StockItem - Allows StockItem to be tracked when it is split into multiple items - Uses MPTT field --- .../migrations/0021_auto_20200215_2232.py | 44 +++++++++++++++++++ InvenTree/stock/models.py | 11 ++++- .../stock/templates/stock/item_base.html | 7 ++- 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 InvenTree/stock/migrations/0021_auto_20200215_2232.py diff --git a/InvenTree/stock/migrations/0021_auto_20200215_2232.py b/InvenTree/stock/migrations/0021_auto_20200215_2232.py new file mode 100644 index 0000000000..3ecca4d6f5 --- /dev/null +++ b/InvenTree/stock/migrations/0021_auto_20200215_2232.py @@ -0,0 +1,44 @@ +# Generated by Django 2.2.9 on 2020-02-15 22:32 + +from django.db import migrations, models +import django.db.models.deletion +import mptt.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('stock', '0020_auto_20200206_1213'), + ] + + operations = [ + migrations.AddField( + model_name='stockitem', + name='level', + field=models.PositiveIntegerField(default=0, editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='stockitem', + name='lft', + field=models.PositiveIntegerField(default=0, editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='stockitem', + name='parent', + field=mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='children', to='stock.StockItem'), + ), + migrations.AddField( + model_name='stockitem', + name='rght', + field=models.PositiveIntegerField(default=0, editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='stockitem', + name='tree_id', + field=models.PositiveIntegerField(db_index=True, default=0, editable=False), + preserve_default=False, + ), + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 57f390415f..e9e237b779 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -18,7 +18,7 @@ from django.dispatch import receiver from markdownx.models import MarkdownxField -from mptt.models import TreeForeignKey +from mptt.models import MPTTModel, TreeForeignKey from decimal import Decimal, InvalidOperation from datetime import datetime @@ -102,11 +102,12 @@ def before_delete_stock_location(sender, instance, using, **kwargs): child.save() -class StockItem(models.Model): +class StockItem(MPTTModel): """ A StockItem object represents a quantity of physical instances of a part. Attributes: + parent: Link to another StockItem from which this StockItem was created part: Link to the master abstract part that this StockItem is an instance of supplier_part: Link to a specific SupplierPart (optional) location: Where this StockItem is located @@ -296,6 +297,11 @@ class StockItem(models.Model): } ) + parent = TreeForeignKey('self', + on_delete=models.DO_NOTHING, + blank=True, null=True, + related_name='children') + part = models.ForeignKey('part.Part', on_delete=models.CASCADE, related_name='stock_items', help_text=_('Base part'), limit_choices_to={ @@ -530,6 +536,7 @@ class StockItem(models.Model): # Nullify the PK so a new record is created new_stock = StockItem.objects.get(pk=self.pk) new_stock.pk = None + new_stock.parent = self new_stock.quantity = quantity new_stock.save() diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index 5caead54a7..3783f6a43f 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -57,7 +57,12 @@ {% trans "This stock item will be automatically deleted when all stock is depleted." %} {% endif %} - + {% if item.parent %} +
+ {% trans "This stock item has been split from " %}{{ item.parent }} +
+ {% endif %} +