From 57123283f4ef651058655e638f1bc5394e47b647 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 26 Mar 2020 17:08:01 +1100 Subject: [PATCH] Better filtering of annotations for Part-list API --- .../InvenTree/static/script/inventree/part.js | 4 +--- InvenTree/InvenTree/status_codes.py | 10 ++++++++-- InvenTree/part/api.py | 18 ++++++++++++++---- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/InvenTree/InvenTree/static/script/inventree/part.js b/InvenTree/InvenTree/static/script/inventree/part.js index c94c512d6f..daa82485b7 100644 --- a/InvenTree/InvenTree/static/script/inventree/part.js +++ b/InvenTree/InvenTree/static/script/inventree/part.js @@ -181,9 +181,7 @@ function loadPartTable(table, url, options={}) { title: 'Stock', searchable: false, sortable: true, - formatter: function(value, row, index, field) { - console.log("On order:", row.on_order); - + formatter: function(value, row, index, field) { var html = ""; var link = "stock"; diff --git a/InvenTree/InvenTree/status_codes.py b/InvenTree/InvenTree/status_codes.py index e49f9a9824..cbc9f3565f 100644 --- a/InvenTree/InvenTree/status_codes.py +++ b/InvenTree/InvenTree/status_codes.py @@ -71,11 +71,17 @@ class StockStatus(StatusCode): LOST: _("Lost"), } - # The following codes correspond to parts that are 'available' + # The following codes correspond to parts that are 'available' or 'in stock' AVAILABLE_CODES = [ OK, ATTENTION, - DAMAGED + DAMAGED, + ] + + # The following codes correspond to parts that are 'unavailable' + UNAVAILABLE_CODES = [ + DESTROYED, + LOST, ] diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index d24f0dc6ee..2218c240c1 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -8,7 +8,7 @@ from __future__ import unicode_literals from django_filters.rest_framework import DjangoFilterBackend from django.conf import settings -from django.db.models import Sum, Count +from django.db.models import Q, Sum, Count from rest_framework import status from rest_framework.response import Response @@ -25,6 +25,7 @@ from .models import PartParameter, PartParameterTemplate from . import serializers as part_serializers +from InvenTree.status_codes import OrderStatus, StockStatus from InvenTree.views import TreeSerializer from InvenTree.helpers import str2bool @@ -153,6 +154,15 @@ class PartList(generics.ListCreateAPIView): queryset = self.filter_queryset(self.get_queryset()) + # Filters for annotations + + # "in_stock" count should only sum stock items which are "in stock" + stock_filter = Q(stock_items__status__in=StockStatus.AVAILABLE_CODES) + + # "on_order" items should only sum orders which are currently outstanding + order_filter = Q(supplier_parts__purchase_order_line_items__order__status__in=OrderStatus.OPEN) + + # Set of fields we wish to serialize data = queryset.values( 'pk', 'category', @@ -171,12 +181,12 @@ class PartList(generics.ListCreateAPIView): 'salable', 'active', ).annotate( - in_stock=Sum('stock_items__quantity'), - on_order=Sum('supplier_parts__purchase_order_line_items__quantity'), + # Quantity of items which are "in stock" + in_stock=Sum('stock_items__quantity', filter=stock_filter), + on_order=Sum('supplier_parts__purchase_order_line_items__quantity', filter=order_filter) ) # TODO - Annotate total being built - # TODO - Annotate total on order # Reduce the number of lookups we need to do for the part categories categories = {}