From 94c0102742b2729ffdbec15166a1f744ac8866a6 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 25 Jul 2019 11:04:45 +1000 Subject: [PATCH] Improve validation logic for StockItem - Allow tracked items to exist without a serial number (e.g. non-serialized tracked items) --- InvenTree/stock/forms.py | 3 ++- InvenTree/stock/models.py | 27 ++++++++++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 9e24b538f3..46a50521dd 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -118,11 +118,12 @@ class EditStockItemForm(HelperForm): fields = [ 'supplier_part', + 'serial', 'batch', - 'delete_on_deplete', 'status', 'notes', 'URL', + 'delete_on_deplete', ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index abc4702e4a..446ab65a56 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -190,20 +190,21 @@ class StockItem(models.Model): }) if self.part is not None: - # A trackable part must have a serial number - if self.part.trackable: - if not self.serial: - raise ValidationError({'serial': _('Serial number must be set for trackable items')}) + # A part with a serial number MUST have the quantity set to 1 + if self.serial is not None: + if self.quantity > 1: + raise ValidationError({ + 'quantity': _('Quantity must be 1 for item with a serial number'), + 'serial': _('Serial number cannot be set if quantity greater than 1') + }) + + if self.quantity == 0: + raise ValidationError({ + 'quantity': _('Quantity must be 1 for item with a serial number') + }) if self.delete_on_deplete: - raise ValidationError({'delete_on_deplete': _("Must be set to False for trackable items")}) - - # Serial number cannot be set for items with quantity greater than 1 - if not self.quantity == 1: - raise ValidationError({ - 'quantity': _("Quantity must be set to 1 for item with a serial number"), - 'serial': _("Serial number cannot be set if quantity > 1") - }) + raise ValidationError({'delete_on_deplete': _("Must be set to False for item with a serial number")}) # A template part cannot be instantiated as a StockItem if self.part.is_template: @@ -422,7 +423,7 @@ class StockItem(models.Model): if location is None: # TODO - Raise appropriate error (cannot move to blank location) return False - elif self.location and (location.pk == self.location.pk): + elif self.location and (location.pk == self.location.pk) and (quantity == self.quantity): # TODO - Raise appropriate error (cannot move to same location) return False