From 87de7e4beb29d65fcd96236d2b9ba622845625c6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 19 Apr 2022 12:52:36 +1000 Subject: [PATCH] Adds "convert_to_variant" function for the StockItem model --- InvenTree/stock/models.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 171ee7e0a3..4c221e92b9 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -718,6 +718,30 @@ class StockItem(MPTTModel): help_text=_('Select Owner'), related_name='stock_items') + @transaction.atomic + def convert_to_variant(self, variant, user, notes=None): + """ + Convert this StockItem instance to a "variant", + i.e. change the "part" reference field + """ + + if not variant: + # Ignore null values + return + + if variant == self.part: + # Variant is the same as the current part + return + + self.part = variant + self.save() + + self.add_tracking_entry( + StockHistoryCode.CONVERTED_TO_VARIANT, + user, + notes=notes + ) + def get_item_owner(self): """ Return the closest "owner" for this StockItem.