mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	Add 'available' filter for BuildLine API endpoint (#5186)
* Add 'available' filter for BuildLine API endpoint - Fixes missing filter - Closes https://github.com/inventree/InvenTree/issues/1839 * Bump API version
This commit is contained in:
		| @@ -1,6 +1,6 @@ | ||||
| """JSON API for the Build app.""" | ||||
|  | ||||
| from django.db.models import F | ||||
| from django.db.models import F, Q | ||||
| from django.urls import include, path, re_path | ||||
| from django.utils.translation import gettext_lazy as _ | ||||
| from django.contrib.auth.models import User | ||||
| @@ -297,6 +297,25 @@ class BuildLineFilter(rest_filters.FilterSet): | ||||
|         else: | ||||
|             return queryset.filter(allocated__lt=F('quantity')) | ||||
|  | ||||
|     available = rest_filters.BooleanFilter(label=_('Available'), method='filter_available') | ||||
|  | ||||
|     def filter_available(self, queryset, name, value): | ||||
|         """Filter by whether there is sufficient stock available for each BuildLine: | ||||
|  | ||||
|         To determine this, we need to know: | ||||
|  | ||||
|         - The quantity required for each BuildLine | ||||
|         - The quantity available for each BuildLine | ||||
|         - The quantity allocated for each BuildLine | ||||
|         """ | ||||
|  | ||||
|         flt = Q(quantity__lte=F('total_available_stock') + F('allocated')) | ||||
|  | ||||
|         if str2bool(value): | ||||
|             return queryset.filter(flt) | ||||
|         else: | ||||
|             return queryset.exclude(flt) | ||||
|  | ||||
|  | ||||
| class BuildLineEndpoint: | ||||
|     """Mixin class for BuildLine API endpoints.""" | ||||
|   | ||||
| @@ -1063,6 +1063,7 @@ class BuildLineSerializer(InvenTreeModelSerializer): | ||||
|             'available_stock', | ||||
|             'available_substitute_stock', | ||||
|             'available_variant_stock', | ||||
|             'total_available_stock', | ||||
|         ] | ||||
|  | ||||
|         read_only_fields = [ | ||||
| @@ -1084,6 +1085,7 @@ class BuildLineSerializer(InvenTreeModelSerializer): | ||||
|     available_stock = serializers.FloatField(read_only=True) | ||||
|     available_substitute_stock = serializers.FloatField(read_only=True) | ||||
|     available_variant_stock = serializers.FloatField(read_only=True) | ||||
|     total_available_stock = serializers.FloatField(read_only=True) | ||||
|  | ||||
|     @staticmethod | ||||
|     def annotate_queryset(queryset): | ||||
| @@ -1185,6 +1187,14 @@ class BuildLineSerializer(InvenTreeModelSerializer): | ||||
|             ) | ||||
|         ) | ||||
|  | ||||
|         # Annotate with the 'total available stock' | ||||
|         queryset = queryset.annotate( | ||||
|             total_available_stock=ExpressionWrapper( | ||||
|                 F('available_stock') + F('available_substitute_stock') + F('available_variant_stock'), | ||||
|                 output_field=FloatField(), | ||||
|             ) | ||||
|         ) | ||||
|  | ||||
|         return queryset | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user