From 9da81898996d0bf9afb862fd77243932b729cd17 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 19 Nov 2019 10:17:20 +1100 Subject: [PATCH] Allow non-integer stock movement --- .../migrations/0025_auto_20191118_2316.py | 18 ++++++++++++++++++ InvenTree/part/models.py | 2 +- .../migrations/0017_auto_20191118_2311.py | 19 +++++++++++++++++++ InvenTree/stock/models.py | 18 ++++++++++++++---- InvenTree/stock/views.py | 2 +- 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 InvenTree/part/migrations/0025_auto_20191118_2316.py create mode 100644 InvenTree/stock/migrations/0017_auto_20191118_2311.py diff --git a/InvenTree/part/migrations/0025_auto_20191118_2316.py b/InvenTree/part/migrations/0025_auto_20191118_2316.py new file mode 100644 index 0000000000..cbdd7be75a --- /dev/null +++ b/InvenTree/part/migrations/0025_auto_20191118_2316.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.5 on 2019-11-18 23:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('part', '0024_auto_20191118_2139'), + ] + + operations = [ + migrations.AlterField( + model_name='part', + name='units', + field=models.CharField(blank=True, default='', help_text='Stock keeping units for this part', max_length=20), + ), + ] diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 6d2554dc0f..809ef0a428 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -407,7 +407,7 @@ class Part(models.Model): minimum_stock = models.PositiveIntegerField(default=0, validators=[MinValueValidator(0)], help_text=_('Minimum allowed stock level')) - units = models.CharField(max_length=20, default="pcs", blank=True, help_text=_('Stock keeping units for this part')) + units = models.CharField(max_length=20, default="", blank=True, help_text=_('Stock keeping units for this part')) assembly = models.BooleanField(default=False, verbose_name='Assembly', help_text=_('Can this part be built from other parts?')) diff --git a/InvenTree/stock/migrations/0017_auto_20191118_2311.py b/InvenTree/stock/migrations/0017_auto_20191118_2311.py new file mode 100644 index 0000000000..32e6de6cea --- /dev/null +++ b/InvenTree/stock/migrations/0017_auto_20191118_2311.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.5 on 2019-11-18 23:11 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('stock', '0016_auto_20191118_2146'), + ] + + operations = [ + migrations.AlterField( + model_name='stockitemtracking', + name='quantity', + field=models.DecimalField(decimal_places=5, default=1, max_digits=15, validators=[django.core.validators.MinValueValidator(0)]), + ), + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index a5a40df46a..cba7f2433e 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -511,6 +511,11 @@ class StockItem(models.Model): if self.serialized: return + try: + quantity = Decimal(quantity) + except (InvalidOperation, ValueError): + return + # Doesn't make sense for a zero quantity if quantity <= 0: return @@ -603,12 +608,17 @@ class StockItem(models.Model): if self.serialized: return + try: + self.quantity = Decimal(quantity) + except (InvalidOperation, ValueError): + return + if quantity < 0: quantity = 0 - self.quantity = quantity - - if quantity <= 0 and self.delete_on_deplete and self.can_delete(): + if quantity == 0 and self.delete_on_deplete and self.can_delete(): + + # TODO - Do not actually "delete" stock at this point - instead give it a "DELETED" flag self.delete() return False else: @@ -745,7 +755,7 @@ class StockItemTracking(models.Model): system = models.BooleanField(default=False) - quantity = models.PositiveIntegerField(validators=[MinValueValidator(0)], default=1) + quantity = models.DecimalField(max_digits=15, decimal_places=5, validators=[MinValueValidator(0)], default=1) # TODO # image = models.ImageField(upload_to=func, max_length=255, null=True, blank=True) diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 9a325300fc..dc56aa87ff 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -546,7 +546,7 @@ class StockAdjust(AjaxView, FormMixin): if destination == item.location and item.new_quantity == item.quantity: continue - item.move(destination, note, self.request.user, quantity=int(item.new_quantity)) + item.move(destination, note, self.request.user, quantity=item.new_quantity) count += 1