Improved filtering for stockitems going into a buildallocation

This commit is contained in:
Oliver Walters 2020-10-22 23:49:23 +11:00
parent fae516b38e
commit d37cdd8e50

View File

@ -566,14 +566,18 @@ class BuildItemCreate(AjaxCreateView):
# If the sub_part is supplied, limit to matching stock items # If the sub_part is supplied, limit to matching stock items
part_id = self.get_param('part') part_id = self.get_param('part')
# We need to precisely control which StockItem objects the user can choose to allocate
stock_filter = form.fields['stock_item'].queryset
# Restrict to only items which are "in stock"
stock_filter = stock_filter.filter(StockItem.IN_STOCK_FILTER)
if part_id: if part_id:
try: try:
self.part = Part.objects.get(pk=part_id) self.part = Part.objects.get(pk=part_id)
query = form.fields['stock_item'].queryset
# Only allow StockItem objects which match the current part # Only allow StockItem objects which match the current part
query = query.filter(part=part_id) stock_filter = stock_filter.filter(part=part_id)
if build_id is not None: if build_id is not None:
try: try:
@ -581,31 +585,26 @@ class BuildItemCreate(AjaxCreateView):
if build.take_from is not None: if build.take_from is not None:
# Limit query to stock items that are downstream of the 'take_from' location # Limit query to stock items that are downstream of the 'take_from' location
query = query.filter(location__in=[loc for loc in build.take_from.getUniqueChildren()]) stock_filter = stock_filter.filter(location__in=[loc for loc in build.take_from.getUniqueChildren()])
except Build.DoesNotExist: except Build.DoesNotExist:
pass pass
# Exclude StockItem objects which are already allocated to this build and part # Exclude StockItem objects which are already allocated to this build and part
query = query.exclude(id__in=[item.stock_item.id for item in BuildItem.objects.filter(build=build_id, stock_item__part=part_id)]) stock_filter = stock_filter.exclude(id__in=[item.stock_item.id for item in BuildItem.objects.filter(build=build_id, stock_item__part=part_id)])
form.fields['stock_item'].queryset = query
stocks = query.all()
self.available_stock = stocks
# If there is only one item selected, select it
if len(stocks) == 1:
form.fields['stock_item'].initial = stocks[0].id
# There is no stock available
elif len(stocks) == 0:
# TODO - Add a message to the form describing the problem
pass
except Part.DoesNotExist: except Part.DoesNotExist:
self.part = None self.part = None
pass pass
form.fields['stock_item'].query = stock_filter
self.available_stock = stock_filter.all()
# If there is only a single stockitem available, select it!
if len(self.available_stock) == 1:
form.fields['stock_item'].initial = self.available_stock[0].pk
return form return form
def get_initial(self): def get_initial(self):