From 95cc3d2a7a61353a6184fbad6a19af067227866b Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 24 May 2020 21:09:43 +1000 Subject: [PATCH] Copy test results when a stock item is split or serialized --- InvenTree/stock/models.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 02393023b9..90a6fd365c 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -647,6 +647,9 @@ class StockItem(MPTTModel): # Copy entire transaction history new_item.copyHistoryFrom(self) + # Copy test result history + new_item.copyTestResultsFrom(self) + # Create a new stock tracking item new_item.addTransactionNote(_('Add serial number'), user, notes=notes) @@ -655,7 +658,7 @@ class StockItem(MPTTModel): @transaction.atomic def copyHistoryFrom(self, other): - """ Copy stock history from another part """ + """ Copy stock history from another StockItem """ for item in other.tracking_info.all(): @@ -663,6 +666,17 @@ class StockItem(MPTTModel): item.pk = None item.save() + @transaction.atomic + def copyTestResultsFrom(self, other, filters={}): + """ Copy all test results from another StockItem """ + + for result in other.test_results.all().filter(**filters): + + # Create a copy of the test result by nulling-out the pk + result.pk = None + result.stock_item = self + result.save() + @transaction.atomic def splitStock(self, quantity, location, user): """ Split this stock item into two items, in the same location. @@ -713,6 +727,9 @@ class StockItem(MPTTModel): # Copy the transaction history of this part into the new one new_stock.copyHistoryFrom(self) + # Copy the test results of this part to the new one + new_stock.copyTestResultsFrom(self) + # Add a new tracking item for the new stock item new_stock.addTransactionNote( "Split from existing stock",