2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-04 14:28:48 +00:00

Adds an "available" filter for stock item API

This commit is contained in:
Oliver 2021-12-02 21:39:49 +11:00
parent 5eccc828fa
commit 123aab89bc
2 changed files with 24 additions and 1 deletions

View File

@ -10,7 +10,7 @@ from datetime import datetime, timedelta
from django.core.exceptions import ValidationError as DjangoValidationError from django.core.exceptions import ValidationError as DjangoValidationError
from django.conf.urls import url, include from django.conf.urls import url, include
from django.http import JsonResponse from django.http import JsonResponse
from django.db.models import Q from django.db.models import Q, F
from django.db import transaction from django.db import transaction
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -304,6 +304,24 @@ class StockFilter(rest_filters.FilterSet):
return queryset return queryset
available = rest_filters.BooleanFilter(label='Available', method='filter_available')
def filter_available(self, queryset, name, value):
"""
Filter by whether the StockItem is "available" or not.
Here, "available" means that the allocated quantity is less than the total quantity
"""
if str2bool(value):
# The 'quantity' field is greater than the calculated 'allocated' field
queryset = queryset.filter(Q(quantity__gt=F('allocated')))
else:
# The 'quantity' field is less than (or equal to) the calculated 'allocated' field
queryset = queryset.filter(Q(quantity__lte=F('allocated')))
return queryset
batch = rest_filters.CharFilter(label="Batch code filter (case insensitive)", lookup_expr='iexact') batch = rest_filters.CharFilter(label="Batch code filter (case insensitive)", lookup_expr='iexact')
batch_regex = rest_filters.CharFilter(label="Batch code filter (regex)", field_name='batch', lookup_expr='iregex') batch_regex = rest_filters.CharFilter(label="Batch code filter (regex)", field_name='batch', lookup_expr='iregex')

View File

@ -173,6 +173,11 @@ function getAvailableTableFilters(tableKey) {
title: '{% trans "Is allocated" %}', title: '{% trans "Is allocated" %}',
description: '{% trans "Item has been allocated" %}', description: '{% trans "Item has been allocated" %}',
}, },
available: {
type: 'bool',
title: '{% trans "Available" %}',
description: '{% trans "Stock is available for use" %}',
},
cascade: { cascade: {
type: 'bool', type: 'bool',
title: '{% trans "Include sublocations" %}', title: '{% trans "Include sublocations" %}',