diff --git a/InvenTree/stock/templates/stock/item_installed.html b/InvenTree/stock/templates/stock/item_installed.html index d6a4675bf4..ae4e90b8c4 100644 --- a/InvenTree/stock/templates/stock/item_installed.html +++ b/InvenTree/stock/templates/stock/item_installed.html @@ -149,7 +149,8 @@ $('#installed-table').inventreeTable({ { data: { 'items[]': [pk], - } + }, + reload: true, } ); }); @@ -159,4 +160,25 @@ $('#installed-table').inventreeTable({ ] }); +$('#multi-item-uninstall').click(function() { + + var selections = $('#installed-table').bootstrapTable('getSelections'); + + var items = []; + + selections.forEach(function(item) { + items.push(item.pk); + }); + + launchModalForm( + "{% url 'stock-item-uninstall' %}", + { + data: { + 'items[]': items, + }, + reload: true, + } + ); +}); + {% endblock %} \ No newline at end of file diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 6aedfe1b4c..885a39540a 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -706,6 +706,36 @@ class StockItemUninstall(AjaxView, FormMixin): return self.stock_items + def get_initial(self): + + initials = super().get_initial().copy() + + # Keep track of the current locations of stock items + current_locations = set() + + # Keep track of the default locations for stock items + default_locations = set() + + for item in self.stock_items: + + if item.location: + current_locations.add(item.location) + + if item.part.default_location: + default_locations.add(item.part.default_location) + + if len(current_locations) == 1: + # If the selected stock items are currently in a single location, + # select that location as the destination. + initials['location'] = next(iter(current_locations)) + elif len(current_locations) == 0: + # There are no current locations set + if len(default_locations) == 1: + # Select the single default location + initials['location'] = next(iter(default_locations)) + + return initials + def get(self, request, *args, **kwargs): """ Extract list of stock items, which are supplied as a list, @@ -717,8 +747,6 @@ class StockItemUninstall(AjaxView, FormMixin): else: self.stock_items = [] - print("GET:", request.GET) - return self.renderJsonResponse(request, self.get_form()) def post(self, request, *args, **kwargs): @@ -731,7 +759,7 @@ class StockItemUninstall(AjaxView, FormMixin): for item in self.request.POST: if item.startswith('stock-item-'): - pk = item.replace('stock-item', '') + pk = item.replace('stock-item-', '') try: stock_item = StockItem.objects.get(pk=pk) @@ -741,9 +769,22 @@ class StockItemUninstall(AjaxView, FormMixin): self.stock_items = items + # Assume the form is valid, until it isn't! + valid = True + confirmed = str2bool(request.POST.get('confirm')) - valid = False + location = request.POST.get('location', None) + + if location: + try: + location = StockLocation.objects.get(pk=location) + except (ValueError, StockLocation.DoesNotExist): + location = None + + if not location: + # Location is required! + valid = False form = self.get_form() @@ -755,6 +796,13 @@ class StockItemUninstall(AjaxView, FormMixin): 'form_valid': valid, } + if valid: + # Ok, now let's actually uninstall the stock items + for item in self.stock_items: + pass + + data['success'] = _('Uninstalled stock items') + return self.renderJsonResponse(request, form=form, data=data) def get_context_data(self):