2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 13:05:42 +00:00

Part table stock filtering (#4462)

* Update PartSerializer queryset annotation

- Add 'total_stock' (in_stock + variant_stock)
- Update 'unallocated_stock' to use total_stock

* Allow API filtering by total_in_stock value

* Refactor partStockLabel method

- We'll use this in the partTable also
- Allow us to prevent further API calls

* Cleanup loadPartTable

* Refactor part variant table

* More updates to part badge function

* Bump API version

* js linting
This commit is contained in:
Oliver
2023-03-08 13:59:51 +11:00
committed by GitHub
parent 106c238af5
commit 9c594ed52b
6 changed files with 111 additions and 125 deletions

View File

@ -1337,6 +1337,7 @@ class PartList(APIDownloadMixin, ListCreateAPI):
'creation_date',
'IPN',
'in_stock',
'total_in_stock',
'unallocated_stock',
'category',
'last_stocktake',

View File

@ -423,7 +423,6 @@ class PartSerializer(RemoteImageMixin, InvenTreeModelSerializer):
'full_name',
'image',
'in_stock',
'variant_stock',
'ordering',
'building',
'IPN',
@ -444,10 +443,12 @@ class PartSerializer(RemoteImageMixin, InvenTreeModelSerializer):
'stock_item_count',
'suppliers',
'thumbnail',
'total_in_stock',
'trackable',
'unallocated_stock',
'units',
'variant_of',
'variant_stock',
'virtual',
'pricing_min',
'pricing_max',
@ -554,11 +555,20 @@ class PartSerializer(RemoteImageMixin, InvenTreeModelSerializer):
allocated_to_build_orders=part.filters.annotate_build_order_allocations(),
)
# Annotate the queryset with the 'total_in_stock' quantity
# This is the 'in_stock' quantity summed with the 'variant_stock' quantity
queryset = queryset.annotate(
total_in_stock=ExpressionWrapper(
F('in_stock') + F('variant_stock'),
output_field=models.DecimalField(),
)
)
# Annotate with the total 'available stock' quantity
# This is the current stock, minus any allocations
queryset = queryset.annotate(
unallocated_stock=ExpressionWrapper(
F('in_stock') - F('allocated_to_sales_orders') - F('allocated_to_build_orders'),
F('total_in_stock') - F('allocated_to_sales_orders') - F('allocated_to_build_orders'),
output_field=models.DecimalField(),
)
)
@ -579,6 +589,7 @@ class PartSerializer(RemoteImageMixin, InvenTreeModelSerializer):
building = serializers.FloatField(read_only=True)
in_stock = serializers.FloatField(read_only=True)
variant_stock = serializers.FloatField(read_only=True)
total_in_stock = serializers.FloatField(read_only=True)
ordering = serializers.FloatField(read_only=True)
stock_item_count = serializers.IntegerField(read_only=True)
suppliers = serializers.IntegerField(read_only=True)

View File

@ -215,7 +215,7 @@
</tr>
{% endif %}
{% if part.component %}
{% if required_build_order_quantity > 0 %}
{% if required_build_order_quantity > 0 or allocated_build_order_quantity > 0 %}
<tr>
<td><span class='fas fa-tools'></span></td>
<td>{% trans "Allocated to Build Orders" %}</td>
@ -224,7 +224,7 @@
{% endif %}
{% endif %}
{% if part.salable %}
{% if required_sales_order_quantity > 0 %}
{% if required_sales_order_quantity > 0 or allocated_sales_order_quantity > 0 %}
<tr>
<td><span class='fas fa-truck'></span></td>
<td>{% trans "Allocated to Sales Orders" %}</td>

View File

@ -404,7 +404,10 @@ def progress_bar(val, max_val, *args, **kwargs):
else:
style = ''
percent = float(val / max_val) * 100
if max_val != 0:
percent = float(val / max_val) * 100
else:
percent = 0
if percent > 100:
percent = 100