Add functions to install and uninstall stock items

This commit is contained in:
Oliver Walters 2020-09-28 21:52:23 +10:00
parent 81ce284264
commit df8d1fb32b
3 changed files with 64 additions and 1 deletions

View File

@ -278,6 +278,8 @@ class UninstallStockForm(forms.ModelForm):
location = TreeNodeChoiceField(queryset=StockLocation.objects.all(), label=_('Location'), help_text=_('Destination location for uninstalled items')) 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')) confirm = forms.BooleanField(required=False, initial=False, label=_('Confirm uninstall'), help_text=_('Confirm removal of installed stock items'))
class Meta: class Meta:
@ -286,6 +288,7 @@ class UninstallStockForm(forms.ModelForm):
fields = [ fields = [
'location', 'location',
'note',
'confirm', 'confirm',
] ]

View File

@ -599,6 +599,64 @@ class StockItem(MPTTModel):
return self.installedItemCount() > 0 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 @property
def children(self): def children(self):
""" Return a list of the child items which have been split from this stock item """ """ Return a list of the child items which have been split from this stock item """

View File

@ -774,6 +774,8 @@ class StockItemUninstall(AjaxView, FormMixin):
confirmed = str2bool(request.POST.get('confirm')) confirmed = str2bool(request.POST.get('confirm'))
note = request.POST.get('note', '')
location = request.POST.get('location', None) location = request.POST.get('location', None)
if location: if location:
@ -799,7 +801,7 @@ class StockItemUninstall(AjaxView, FormMixin):
if valid: if valid:
# Ok, now let's actually uninstall the stock items # Ok, now let's actually uninstall the stock items
for item in self.stock_items: for item in self.stock_items:
pass item.uninstallIntoLocation(location, request.user, note)
data['success'] = _('Uninstalled stock items') data['success'] = _('Uninstalled stock items')