Copy test results when a stock item is split or serialized

This commit is contained in:
Oliver Walters 2020-05-24 21:09:43 +10:00
parent 4292a32ab9
commit 95cc3d2a7a

View File

@ -647,6 +647,9 @@ class StockItem(MPTTModel):
# Copy entire transaction history # Copy entire transaction history
new_item.copyHistoryFrom(self) new_item.copyHistoryFrom(self)
# Copy test result history
new_item.copyTestResultsFrom(self)
# Create a new stock tracking item # Create a new stock tracking item
new_item.addTransactionNote(_('Add serial number'), user, notes=notes) new_item.addTransactionNote(_('Add serial number'), user, notes=notes)
@ -655,7 +658,7 @@ class StockItem(MPTTModel):
@transaction.atomic @transaction.atomic
def copyHistoryFrom(self, other): def copyHistoryFrom(self, other):
""" Copy stock history from another part """ """ Copy stock history from another StockItem """
for item in other.tracking_info.all(): for item in other.tracking_info.all():
@ -663,6 +666,17 @@ class StockItem(MPTTModel):
item.pk = None item.pk = None
item.save() 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 @transaction.atomic
def splitStock(self, quantity, location, user): def splitStock(self, quantity, location, user):
""" Split this stock item into two items, in the same location. """ 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 # Copy the transaction history of this part into the new one
new_stock.copyHistoryFrom(self) 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 # Add a new tracking item for the new stock item
new_stock.addTransactionNote( new_stock.addTransactionNote(
"Split from existing stock", "Split from existing stock",