diff --git a/InvenTree/InvenTree/static/script/inventree/part.js b/InvenTree/InvenTree/static/script/inventree/part.js index 665701defd..dd03836d49 100644 --- a/InvenTree/InvenTree/static/script/inventree/part.js +++ b/InvenTree/InvenTree/static/script/inventree/part.js @@ -87,17 +87,15 @@ function loadPartTable(table, url, options={}) { * buttons: If provided, link buttons to selection status of this table */ - // Default query params - query = options.query; - - if (!options.allowInactive) { - // Only display active parts - query.active = true; + var params = options.parms || {}; + + var filters = loadTableFilters("parts"); + + for (var key in params) { + filters[key] = params[key]; } - // Include sub-category search - // TODO - Make this user-configurable! - query.cascade = true; + setupFilterList("parts", $(table)); var columns = [ { @@ -217,10 +215,10 @@ function loadPartTable(table, url, options={}) { url: url, sortName: 'name', method: 'get', + queryParams: filters, + groupBy: false, + original: params, formatNoMatches: function() { return "No parts found"; }, - queryParams: function(p) { - return query; - }, columns: columns, }); diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 7bb31af14d..4cfa04d508 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -215,6 +215,21 @@ class PartList(generics.ListCreateAPIView): building=Sum('builds__quantity', filter=build_filter), ) + # If we are filtering by 'has_stock' status, + # Check if the 'has_stock' quantity is zero + has_stock = self.request.query_params.get('has_stock', None) + + if has_stock is not None: + has_stock = str2bool(has_stock) + + if has_stock: + # Filter items which have a non-null 'in_stock' quantity above zero + data = data.exclude(in_stock=None) + data = data.filter(in_stock__gt=0) + else: + # Filter items which a null or zero 'in_stock' quantity + data = data.filter(Q(in_stock__lte=0) | Q(in_stock=None)) + # Reduce the number of lookups we need to do for the part categories categories = {} diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index d751dca215..f9151fb1f0 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -659,7 +659,7 @@ class Part(models.Model): if total: return total else: - return 0 + return Decimal(0) @property def has_bom(self): diff --git a/InvenTree/part/templates/part/category.html b/InvenTree/part/templates/part/category.html index 2161ff1bd9..cc663a63c4 100644 --- a/InvenTree/part/templates/part/category.html +++ b/InvenTree/part/templates/part/category.html @@ -99,7 +99,7 @@
diff --git a/InvenTree/templates/table_filters.html b/InvenTree/templates/table_filters.html index cf8b609328..10f7a8a1cc 100644 --- a/InvenTree/templates/table_filters.html +++ b/InvenTree/templates/table_filters.html @@ -59,6 +59,24 @@ function getAvailableTableFilters(tableKey) { // Filters for the "Parts" table if (tableKey == "parts") { return { + cascade: { + type: 'bool', + title: '{% trans "Include subcategories" %}', + description: '{% trans "Include parts in subcategories" %}', + }, + active: { + type: 'bool', + title: '{% trans "Active" %}', + description: '{% trans "Show active parts" %}', + }, + is_template: { + type: 'bool', + title: '{% trans "Template" %}', + }, + has_stock: { + type: 'bool', + title: '{% trans "Stock Available" %}' + }, }; }