From 90a342fad8c4dab9bf2e8d4d393502a62eaaf9a2 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 1 Sep 2019 23:09:40 +1000 Subject: [PATCH 1/5] Add a reference to a build for a stock item --- .../stock/migrations/0010_stockitem_build.py | 20 +++++++++++++++++++ InvenTree/stock/models.py | 8 ++++++++ 2 files changed, 28 insertions(+) create mode 100644 InvenTree/stock/migrations/0010_stockitem_build.py 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..37a2323992 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, From 3cef5c93ce8a8d04d9233a478cc78f5a46d87200 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 1 Sep 2019 23:18:08 +1000 Subject: [PATCH 2/5] Improve stock item splitting --- InvenTree/stock/models.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 37a2323992..6ae7803d10 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -492,20 +492,12 @@ 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.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 From c45c4e236efc45bcb709c62e8caef78cbf68a8cb Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 1 Sep 2019 23:18:28 +1000 Subject: [PATCH 3/5] Point new stockitem to the correct build --- InvenTree/build/models.py | 2 ++ 1 file changed, 2 insertions(+) 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 '', From 7db938eda032ce428a33f9ff895f0105806e983e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 1 Sep 2019 23:24:27 +1000 Subject: [PATCH 4/5] Show the build item on the stock-item page (if applicable) --- InvenTree/stock/templates/stock/item.html | 6 ++++++ Makefile | 1 + 2 files changed, 7 insertions(+) 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 From 906766cc0e0dd6fa0f808eb6c8fa16886611db81 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 1 Sep 2019 23:28:28 +1000 Subject: [PATCH 5/5] Bug fix - ensure pk is set to None --- InvenTree/stock/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 6ae7803d10..78cef49371 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -494,6 +494,7 @@ class StockItem(models.Model): # Create a new StockItem object, duplicating relevant fields # 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()