From df8d1fb32b4c5d3aa69306d1c9dc52011277e36d Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 28 Sep 2020 21:52:23 +1000 Subject: [PATCH] Add functions to install and uninstall stock items --- InvenTree/stock/forms.py | 3 ++ InvenTree/stock/models.py | 58 +++++++++++++++++++++++++++++++++++++++ InvenTree/stock/views.py | 4 ++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 37d8d91c8a..04701c8eb6 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -278,6 +278,8 @@ class UninstallStockForm(forms.ModelForm): location = TreeNodeChoiceField(queryset=StockLocation.objects.all(), label=_('Location'), help_text=_('Destination location for uninstalled items')) + note = forms.CharField(label=_('Notes'), required=False, help_text=_('Add transaction note (optional)')) + confirm = forms.BooleanField(required=False, initial=False, label=_('Confirm uninstall'), help_text=_('Confirm removal of installed stock items')) class Meta: @@ -286,6 +288,7 @@ class UninstallStockForm(forms.ModelForm): fields = [ 'location', + 'note', 'confirm', ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index e2468772e2..b1e9f95e7e 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -599,6 +599,64 @@ class StockItem(MPTTModel): return self.installedItemCount() > 0 + @transaction.atomic + def installIntoStockItem(self, otherItem, user, notes): + """ + Install this stock item into another stock item. + + Args + otherItem: The stock item to install this item into + user: The user performing the operation + notes: Any notes associated with the operation + """ + + # Cannot be already installed in another stock item! + if self.belongs_to is not None: + return False + + # TODO - Are there any other checks that need to be performed at this stage? + + # Mark this stock item as belonging to the other one + self.belongs_to = otherItem + + self.save() + + # Add a transaction note! + self.addTransactionNote( + _('Installed in stock item') + ' ' + str(otherItem.pk), + user, + notes=notes + ) + + @transaction.atomic + def uninstallIntoLocation(self, location, user, notes): + """ + Uninstall this stock item from another item, into a location. + + Args: + location: The stock location where the item will be moved + user: The user performing the operation + notes: Any notes associated with the operation + """ + + # If the stock item is not installed in anything, ignore + if self.belongs_to is None: + return False + + # TODO - Are there any other checks that need to be performed at this stage? + + self.belongs_to = None + self.location = location + + self.save() + + # Add a transaction note! + self.addTransactionNote( + _('Uninstalled into location') + ' ' + str(location), + user, + notes=notes + ) + @property def children(self): """ Return a list of the child items which have been split from this stock item """ diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 885a39540a..7e62eda936 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -774,6 +774,8 @@ class StockItemUninstall(AjaxView, FormMixin): confirmed = str2bool(request.POST.get('confirm')) + note = request.POST.get('note', '') + location = request.POST.get('location', None) if location: @@ -799,7 +801,7 @@ class StockItemUninstall(AjaxView, FormMixin): if valid: # Ok, now let's actually uninstall the stock items for item in self.stock_items: - pass + item.uninstallIntoLocation(location, request.user, note) data['success'] = _('Uninstalled stock items')