From a686500df15a5315389d5b63a85a2a611884eb57 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 4 Oct 2020 21:02:20 +1100 Subject: [PATCH] Calculate initial values for the view --- InvenTree/stock/models.py | 3 ++- InvenTree/stock/views.py | 57 ++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index df1a628f47..62b774dd06 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -600,12 +600,13 @@ class StockItem(MPTTModel): return self.installedItemCount() > 0 @transaction.atomic - def installIntoStockItem(self, otherItem, user, notes): + def installIntoStockItem(self, otherItem, quantity, user, notes): """ Install this stock item into another stock item. Args otherItem: The stock item to install this item into + quantity: The quantity of stock to install user: The user performing the operation notes: Any notes associated with the operation """ diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 1f6ddcc0f4..541c60b2f5 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -699,25 +699,52 @@ class StockItemInstall(AjaxUpdateView): form_class = StockForms.InstallStockForm ajax_form_title = _('Install Stock Item') + def get_stock_items(self): + """ + Return a list of stock items suitable for displaying to the user. + + Requirements: + - Items must be in stock + + Filters: + - Items can be filtered by Part reference + """ + + items = StockItem.objects.filter(StockItem.IN_STOCK_FILTER) + + # Filter by Part association + try: + part = self.request.GET.get('part', None) + + print(self.request.GET) + + if part is not None: + part = Part.objects.get(pk=part) + items = items.filter(part=part) + except (ValueError, Part.DoesNotExist): + pass + + return items + + def get_initial(self): + + initials = super().get_initial() + + items = self.get_stock_items() + + # If there is a single stock item available, we can use it! + if items.count() == 1: + item = items.first() + initials['stock_item'] = item.pk + initials['quantity_to_install'] = item.quantity + + return initials + def get_form(self): form = super().get_form() - queryset = form.fields['stock_item'].queryset - - part = self.request.GET.get('part', None) - - # Filter the available stock items based on the Part reference - if part: - try: - part = Part.objects.get(pk=part) - - queryset = queryset.filter(part=part) - - except (ValueError, Part.DoesNotExist): - pass - - form.fields['stock_item'].queryset = queryset + form.fields['stock_item'].queryset = self.get_stock_items() return form