diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 1ceedf63e2..2ae3f0c5a7 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -237,6 +237,7 @@ class Build(models.Model): for serial in serial_numbers: item = StockItem.objects.create( part=self.part, + build=self, location=location, quantity=1, serial=serial, @@ -250,6 +251,7 @@ class Build(models.Model): # Add stock of the newly created item item = StockItem.objects.create( part=self.part, + build=self, location=location, quantity=self.quantity, batch=str(self.batch) if self.batch else '', diff --git a/InvenTree/stock/migrations/0010_stockitem_build.py b/InvenTree/stock/migrations/0010_stockitem_build.py new file mode 100644 index 0000000000..4445d27b2e --- /dev/null +++ b/InvenTree/stock/migrations/0010_stockitem_build.py @@ -0,0 +1,20 @@ +# Generated by Django 2.2.4 on 2019-09-01 13:08 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('build', '0005_auto_20190604_2217'), + ('stock', '0009_auto_20190715_2351'), + ] + + operations = [ + migrations.AddField( + model_name='stockitem', + name='build', + field=models.ForeignKey(blank=True, help_text='Build for this stock item', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='build_outputs', to='build.Build'), + ), + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 9aa8595c49..78cef49371 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -101,6 +101,7 @@ class StockItem(models.Model): delete_on_deplete: If True, StockItem will be deleted when the stock level gets to zero status: Status of this StockItem (ref: InvenTree.status_codes.StockStatus) notes: Extra notes field + build: Link to a Build (if this stock item was created from a build) purchase_order: Link to a PurchaseOrder (if this stock item was created from a PurchaseOrder) infinite: If True this StockItem can never be exhausted """ @@ -300,6 +301,13 @@ class StockItem(models.Model): updated = models.DateField(auto_now=True, null=True) + build = models.ForeignKey( + 'build.Build', on_delete=models.SET_NULL, + blank=True, null=True, + help_text='Build for this stock item', + related_name='build_outputs', + ) + purchase_order = models.ForeignKey( 'order.PurchaseOrder', on_delete=models.SET_NULL, @@ -484,20 +492,13 @@ class StockItem(models.Model): return # Create a new StockItem object, duplicating relevant fields - new_stock = StockItem.objects.create( - part=self.part, - quantity=quantity, - supplier_part=self.supplier_part, - location=self.location, - notes=self.notes, - URL=self.URL, - batch=self.batch, - delete_on_deplete=self.delete_on_deplete - ) - + # Nullify the PK so a new record is created + new_stock = StockItem.objects.get(pk=self.pk) + new_stock.pk = None + new_stock.quantity = quantity new_stock.save() - # Copy the transaction history + # Copy the transaction history of this part into the new one new_stock.copyHistoryFrom(self) # Add a new tracking item for the new stock item diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index 1e7e44046c..87abe0adfe 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -90,6 +90,12 @@ {{ item.batch }} {% endif %} + {% if item.build %} + + Build + {{ item.build }} + + {% endif %} {% if item.purchase_order %} Purchase Order diff --git a/Makefile b/Makefile index d997c76344..c898af195d 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ migrate: python3 InvenTree/manage.py makemigrations stock python3 InvenTree/manage.py makemigrations build python3 InvenTree/manage.py makemigrations order + python3 InvenTree/manage.py migrate python3 InvenTree/manage.py migrate --run-syncdb python3 InvenTree/manage.py check