mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Improved 'move stock' form
- Better error checking on the form (provides form validation messages to user)
This commit is contained in:
parent
f451d31f00
commit
93bb0bf6f4
@ -38,6 +38,8 @@ class CreateStockItemForm(HelperForm):
|
||||
|
||||
class MoveStockItemForm(forms.ModelForm):
|
||||
|
||||
note = forms.CharField(label='Notes', required=True)
|
||||
|
||||
class Meta:
|
||||
model = StockItem
|
||||
|
||||
|
@ -217,21 +217,24 @@ class StockItem(models.Model):
|
||||
track.save()
|
||||
|
||||
@transaction.atomic
|
||||
def move(self, location, user):
|
||||
def move(self, location, notes, user):
|
||||
|
||||
if location == self.location:
|
||||
return
|
||||
if location.pk == self.location.pk:
|
||||
return False # raise forms.ValidationError("Cannot move item to its current location")
|
||||
|
||||
note = "Moved to {loc}".format(loc=location.name)
|
||||
msg = "Moved to {loc} (from {src})".format(loc=location.name,
|
||||
src=self.location.name)
|
||||
|
||||
self.location = location
|
||||
self.save()
|
||||
|
||||
self.add_transaction_note('Transfer',
|
||||
self.add_transaction_note(msg,
|
||||
user,
|
||||
notes=note,
|
||||
notes=notes,
|
||||
system=True)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def stocktake(self, count, user, notes=''):
|
||||
|
@ -142,20 +142,26 @@ class StockItemMove(AjaxUpdateView):
|
||||
|
||||
if form.is_valid():
|
||||
|
||||
obj = form.save()
|
||||
obj = self.get_object()
|
||||
|
||||
try:
|
||||
loc = StockLocation.objects.get(pk=form['location'].value())
|
||||
loc_path = loc.pathstring
|
||||
loc_id = form['location'].value()
|
||||
|
||||
if loc_id:
|
||||
loc = StockLocation.objects.get(pk=form['location'].value())
|
||||
if str(loc.pk) == str(obj.pk):
|
||||
form.errors['location'] = ['Item is already in this location']
|
||||
else:
|
||||
obj.move(loc, form['note'].value(), request.user)
|
||||
else:
|
||||
form.errors['location'] = ['Cannot move to an empty location']
|
||||
|
||||
except StockLocation.DoesNotExist:
|
||||
loc_path = ''
|
||||
|
||||
obj.add_transaction_note("Moved item to '{where}'".format(where=loc_path),
|
||||
request.user,
|
||||
system=True)
|
||||
form.errors['location'] = ['Location does not exist']
|
||||
|
||||
data = {
|
||||
'form_valid': form.is_valid(),
|
||||
'form_valid': form.is_valid() and len(form.errors) == 0,
|
||||
}
|
||||
|
||||
return self.renderJsonResponse(request, form, data)
|
||||
|
Loading…
Reference in New Issue
Block a user