Improved 'move stock' form

- Better error checking on the form (provides form validation messages to user)
This commit is contained in:
Oliver Walters 2019-04-13 00:08:13 +10:00
parent f451d31f00
commit 93bb0bf6f4
3 changed files with 25 additions and 14 deletions

View File

@ -38,6 +38,8 @@ class CreateStockItemForm(HelperForm):
class MoveStockItemForm(forms.ModelForm): class MoveStockItemForm(forms.ModelForm):
note = forms.CharField(label='Notes', required=True)
class Meta: class Meta:
model = StockItem model = StockItem

View File

@ -217,21 +217,24 @@ class StockItem(models.Model):
track.save() track.save()
@transaction.atomic @transaction.atomic
def move(self, location, user): def move(self, location, notes, user):
if location == self.location: if location.pk == self.location.pk:
return 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.location = location
self.save() self.save()
self.add_transaction_note('Transfer', self.add_transaction_note(msg,
user, user,
notes=note, notes=notes,
system=True) system=True)
return True
@transaction.atomic @transaction.atomic
def stocktake(self, count, user, notes=''): def stocktake(self, count, user, notes=''):

View File

@ -142,20 +142,26 @@ class StockItemMove(AjaxUpdateView):
if form.is_valid(): if form.is_valid():
obj = form.save() obj = self.get_object()
try: try:
loc = StockLocation.objects.get(pk=form['location'].value()) loc_id = form['location'].value()
loc_path = loc.pathstring
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: except StockLocation.DoesNotExist:
loc_path = '' loc_path = ''
form.errors['location'] = ['Location does not exist']
obj.add_transaction_note("Moved item to '{where}'".format(where=loc_path),
request.user,
system=True)
data = { data = {
'form_valid': form.is_valid(), 'form_valid': form.is_valid() and len(form.errors) == 0,
} }
return self.renderJsonResponse(request, form, data) return self.renderJsonResponse(request, form, data)