diff --git a/InvenTree/InvenTree/status_codes.py b/InvenTree/InvenTree/status_codes.py index ffe22039c9..93f213445c 100644 --- a/InvenTree/InvenTree/status_codes.py +++ b/InvenTree/InvenTree/status_codes.py @@ -255,6 +255,9 @@ class StockHistoryCode(StatusCode): # Stock merging operations MERGED_STOCK_ITEMS = 45 + # Convert stock item to variant + CONVERTED_TO_VARIANT = 48 + # Build order codes BUILD_OUTPUT_CREATED = 50 BUILD_OUTPUT_COMPLETED = 55 @@ -294,6 +297,8 @@ class StockHistoryCode(StatusCode): MERGED_STOCK_ITEMS: _('Merged stock items'), + CONVERTED_TO_VARIANT: _('Converted to variant'), + SENT_TO_CUSTOMER: _('Sent to customer'), RETURNED_FROM_CUSTOMER: _('Returned from customer'), diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index e176948599..3768cd8787 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -1234,6 +1234,15 @@ class StockTrackingList(generics.ListAPIView): if not deltas: deltas = {} + # Add part detail + if 'part' in deltas: + try: + part = Part.objects.get(pk=deltas['part']) + serializer = PartBriefSerializer(part) + deltas['part_detail'] = serializer.data + except: + pass + # Add location detail if 'location' in deltas: try: diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 171ee7e0a3..dc93f61e81 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -718,6 +718,33 @@ 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, + deltas={ + 'part': variant.pk, + }, + notes=_('Converted to part') + ': ' + variant.full_name, + ) + def get_item_owner(self): """ Return the closest "owner" for this StockItem. diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index ae0742e99e..75e53d6758 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -26,11 +26,12 @@