diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index ca9d6f6360..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 @@ -62,7 +64,7 @@ class EditStockItemForm(HelperForm): model = StockItem fields = [ - 'quantity', 'batch', 'status', + 'notes' ] \ No newline at end of file diff --git a/InvenTree/stock/migrations/0005_stockitemtracking_quantity.py b/InvenTree/stock/migrations/0005_stockitemtracking_quantity.py new file mode 100644 index 0000000000..f654486075 --- /dev/null +++ b/InvenTree/stock/migrations/0005_stockitemtracking_quantity.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2 on 2019-04-12 14:09 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('stock', '0004_auto_20190412_2030'), + ] + + operations = [ + migrations.AddField( + model_name='stockitemtracking', + name='quantity', + field=models.PositiveIntegerField(default=1, validators=[django.core.validators.MinValueValidator(0)]), + ), + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index b020640497..31c48887ba 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -209,6 +209,7 @@ class StockItem(models.Model): item=self, title=title, user=user, + quantity=self.quantity, date=datetime.now().date(), notes=notes, system=system @@ -217,21 +218,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=''): @@ -343,6 +347,9 @@ class StockItemTracking(models.Model): # Was this tracking note auto-generated by the system? system = models.BooleanField(default=False) + # Keep track of the StockItem quantity throughout the tracking history + quantity = models.PositiveIntegerField(validators=[MinValueValidator(0)], default=1) + # TODO # image = models.ImageField(upload_to=func, max_length=255, null=True, blank=True) diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index 6479e4aa88..9105e78dce 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -35,6 +35,7 @@ class StockTrackingSerializer(serializers.ModelSerializer): 'date', 'title', 'notes', + 'quantity', 'user', 'system', ] @@ -43,6 +44,7 @@ class StockTrackingSerializer(serializers.ModelSerializer): 'date', 'user', 'system', + 'quantity', ] diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index 29c2d78e88..b00b77ecac 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -15,11 +15,11 @@ @@ -215,12 +215,24 @@ return html; } }, + { + field: 'quantity', + title: 'Quantity', + }, { sortable: true, field: 'user', title: 'User', formatter: function(value, row, index, field) { - return value.username; + if (value) + { + // TODO - Format the user's first and last names + return value.username; + } + else + { + return "No user information"; + } } } ], 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)