From e4ca638a2eedb8802e38ddbc86d83e3342dd5843 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 4 Apr 2022 23:52:45 +1000 Subject: [PATCH] Add field for substitute_stock (work in progress) --- InvenTree/part/serializers.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 5d62d5414b..9e393724fa 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -579,6 +579,7 @@ class BomItemSerializer(InvenTreeModelSerializer): # Annotated fields available_stock = serializers.FloatField(read_only=True) + substitute_stock = serializers.FloatField(read_only=True) def __init__(self, *args, **kwargs): # part_detail and sub_part_detail serializers are only included if requested. @@ -637,9 +638,13 @@ class BomItemSerializer(InvenTreeModelSerializer): # Calculate "total stock" for the referenced sub_part # Calculate the "build_order_allocations" for the sub_part + # Note that these fields are only aliased, not annotated queryset = queryset.alias( total_stock=Coalesce( - SubquerySum('sub_part__stock_items__quantity', filter=StockItem.IN_STOCK_FILTER), + SubquerySum( + 'sub_part__stock_items__quantity', + filter=StockItem.IN_STOCK_FILTER + ), Decimal(0), output_field=models.DecimalField(), ), @@ -674,6 +679,18 @@ class BomItemSerializer(InvenTreeModelSerializer): ) ) + # Extract similar information for any 'substitute' parts + queryset = queryset.annotate( + substitute_stock=Coalesce( + SubquerySum( + 'substitutes__part__stock_items__quantity', + filter=StockItem.IN_STOCK_FILTER, + ), + Decimal(0), + output_field=models.DecimalField(), + ) + ) + return queryset def get_purchase_price_range(self, obj): @@ -748,7 +765,7 @@ class BomItemSerializer(InvenTreeModelSerializer): # Annotated fields describing available quantity 'available_stock', - + 'substitute_stock', ]