From 93bb0bf6f4c691b8e2575302f6915cef9a4cc1bf Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 13 Apr 2019 00:08:13 +1000 Subject: [PATCH] Improved 'move stock' form - Better error checking on the form (provides form validation messages to user) --- InvenTree/stock/forms.py | 2 ++ InvenTree/stock/models.py | 15 +++++++++------ InvenTree/stock/views.py | 22 ++++++++++++++-------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 60e2382930..26f81ba958 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -38,6 +38,8 @@ class CreateStockItemForm(HelperForm): class MoveStockItemForm(forms.ModelForm): + note = forms.CharField(label='Notes', required=True) + class Meta: model = StockItem diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index b020640497..a3bbbcff74 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -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=''): diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 44df3911a0..3df9ce0893 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -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)