Repository: inventree/InvenTree

Issue:
[FR] Filter Build Orders by Assigned User/Group
### Please verify that this feature request has NOT been suggested before.

- [X] I checked and didn't find a similar feature request

### Problem statement

I am using Groups to assign build orders to different manufacturing departments (like "CNC", "Composites", "3D Printing", "Electronics" etc). Currently I don't see a filter option to only show build orders that are assigned to a specific user or group other than yourself.

### Suggested solution

It would be very useful to have an additional filter in the build orders list to filter by `assigned to:` where users and groups can be selected.
Or maybe split into two filters: `assigned to user:` and `assigned to group:` - otherwise the options drop-down would be confusing and possibly crowded.

I think this would live here, right?
https://github.com/inventree/InvenTree/blob/75ca325892a09b6836250b863399649234d95d66/InvenTree/templates/js/translated/table_filters.js#L339-L359

is there a global variable already to access a list of users and groups? In case not, where should that be created best? Right there in `table_filters.js` from an api call, or maybe somewhere more central?

### Describe alternatives you've considered

none that I can think of really - other than sorting the table by `Assigned to` column. But that is not so helpful with huge projects and many active build orders.

### Examples of other systems

_No response_

### Do you want to develop this?

- [X] I want to develop this.


Discussion:
Nice idea! A few things going on here

### Users vs Group

We actually have a superclass of this - the "owner". In fact you will find that the ~~assigned_to~~ *responsible* field on the Build model is a reference to an "owner" - which can be either a user *or* a group.

### Filter by Owner

You'll need to add a new filter to the `BuildFilter` class in `build/api.py`. There's a similar filter already there for a reference:

https://github.com/inventree/InvenTree/blob/a2b55314cb9f4e5b3ae7cd5d84c1d884fb5c4ef1/InvenTree/build/api.py#L54

### Filter Selection

Currently, filters (as defined in *table_filters.js*) can only be "static" values. However, it would be *great* to extend this to being able to make an API call  to select from available options! If you want to address this issue, I can help you work through this part (I imagine this will be the most challenging detail)

I think we should get away with building the owner list on load.
> I think we should get away with building the owner list on load.

That would certainly make it easier to implement.
@simonkuehling happy with these suggestions?
> @simonkuehling happy with these suggestions?

Yeah, I held back for a moment to form an opinion which option is best here... I think I am not quite sure about @matmair's comment regarding gathering the list on load. The API filter is needed anyway, isn't it?

I see this as two tasks to implement:
1. get a list of all users/groups to create the JS filter *options*
2. add a new *filter* to the build-order-API

right?
You will need the API filter on the backend and add the filter to the frontend.

Resolving patch:
diff --git a/InvenTree/InvenTree/api_version.py b/InvenTree/InvenTree/api_version.py
--- a/InvenTree/InvenTree/api_version.py
+++ b/InvenTree/InvenTree/api_version.py
@@ -2,11 +2,14 @@
 
 
 # InvenTree API version
-INVENTREE_API_VERSION = 97
+INVENTREE_API_VERSION = 98
 
 """
 Increment this API version number whenever there is a significant change to the API that any clients need to know about
 
+v98 -> 2023-02-24 : https://github.com/inventree/InvenTree/pull/4408
+    - Adds "responsible" filter to Build API
+
 v97 -> 2023-02-20 : https://github.com/inventree/InvenTree/pull/4377
     - Adds "external" attribute to StockLocation model
 
diff --git a/InvenTree/build/api.py b/InvenTree/build/api.py
--- a/InvenTree/build/api.py
+++ b/InvenTree/build/api.py
@@ -2,6 +2,7 @@
 
 from django.urls import include, re_path
 from django.utils.translation import gettext_lazy as _
+from django.contrib.auth.models import User
 
 from rest_framework import filters
 from rest_framework.exceptions import ValidationError
@@ -65,6 +66,20 @@ def filter_assigned_to_me(self, queryset, name, value):
 
         return queryset
 
+    assigned_to = rest_filters.NumberFilter(label='responsible', method='filter_responsible')
+
+    def filter_responsible(self, queryset, name, value):
+        """Filter by orders which are assigned to the specified owner."""
+        owners = list(Owner.objects.filter(pk=value))
+
+        # if we query by a user, also find all ownerships through group memberships
+        if len(owners) > 0 and owners[0].label() == 'user':
+            owners = Owner.get_owners_matching_user(User.objects.get(pk=owners[0].owner_id))
+
+        queryset = queryset.filter(responsible__in=owners)
+
+        return queryset
+
     # Exact match for reference
     reference = rest_filters.CharFilter(
         label='Filter by exact reference',


Test patch: