From 75774771dcdc88db27c8ec6a81de81356255d2f3 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 19 Nov 2019 09:18:41 +1100 Subject: [PATCH] Changes to StockItem model - Stock adjustments need to accept decimal values --- InvenTree/stock/models.py | 21 +++++++++++++++++---- InvenTree/stock/views.py | 5 ++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index a89489f902..a5a40df46a 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -18,6 +18,7 @@ from django.dispatch import receiver from mptt.models import TreeForeignKey +from decimal import Decimal, InvalidOperation from datetime import datetime from InvenTree import helpers @@ -549,7 +550,10 @@ class StockItem(models.Model): quantity: If provided, override the quantity (default = total stock quantity) """ - quantity = int(kwargs.get('quantity', self.quantity)) + try: + quantity = Decimal(kwargs.get('quantity', self.quantity)) + except InvalidOperation: + return False if quantity <= 0: return False @@ -618,7 +622,10 @@ class StockItem(models.Model): record the date of stocktake """ - count = int(count) + try: + count = Decimal(count) + except InvalidOperation: + return False if count < 0 or self.infinite: return False @@ -646,7 +653,10 @@ class StockItem(models.Model): if self.serialized: return False - quantity = int(quantity) + try: + quantity = Decimal(quantity) + except InvalidOperation: + return False # Ignore amounts that do not make sense if quantity <= 0 or self.infinite: @@ -670,7 +680,10 @@ class StockItem(models.Model): if self.serialized: return False - quantity = int(quantity) + try: + quantity = Decimal(quantity) + except InvalidOperation: + return False if quantity <= 0 or self.infinite: return False diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index a503efaba4..5dfe530a8c 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -20,6 +20,8 @@ from InvenTree.views import QRCodeView from InvenTree.helpers import str2bool, DownloadFile, GetExportFormats from InvenTree.helpers import ExtractSerialNumbers + +from decimal import Decimal from datetime import datetime from company.models import Company @@ -398,8 +400,9 @@ class StockAdjust(AjaxView, FormMixin): valid = form.is_valid() for item in self.stock_items: + try: - item.new_quantity = int(item.new_quantity) + item.new_quantity = Decimal(item.new_quantity) except ValueError: item.error = _('Must enter integer value') valid = False