From 68b9a690f271ec681146ec6fb6dee39c9dc33b5c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 24 May 2020 20:22:15 +1000 Subject: [PATCH] Integer value required for trackable bom item --- InvenTree/part/models.py | 15 +++++++++++++++ InvenTree/part/test_bom_item.py | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 451c916542..4acd4a594e 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1339,6 +1339,11 @@ class BomItem(models.Model): checksum: Validation checksum for the particular BOM line item """ + def save(self, *args, **kwargs): + + self.clean() + super().save(*args, **kwargs) + def get_absolute_url(self): return reverse('bom-item-detail', kwargs={'pk': self.id}) @@ -1431,6 +1436,16 @@ class BomItem(models.Model): - A part cannot refer to a part which refers to it """ + # If the sub_part is 'trackable' then the 'quantity' field must be an integer + try: + if self.sub_part.trackable: + if not self.quantity == int(self.quantity): + raise ValidationError({ + "quantity": _("Quantity must be integer value for trackable parts") + }) + except Part.DoesNotExist: + pass + # A part cannot refer to itself in its BOM try: if self.sub_part is not None and self.part is not None: diff --git a/InvenTree/part/test_bom_item.py b/InvenTree/part/test_bom_item.py index 95e1dbb2c7..91aa95c17c 100644 --- a/InvenTree/part/test_bom_item.py +++ b/InvenTree/part/test_bom_item.py @@ -44,6 +44,20 @@ class BomItemTest(TestCase): item = BomItem.objects.create(part=self.bob, sub_part=self.bob, quantity=7) item.clean() + def test_integer_quantity(self): + """ + Test integer validation for BomItem + """ + + p = Part.objects.create(name="test", description="d", component=True, trackable=True) + + # Creation of a BOMItem with a non-integer quantity of a trackable Part should fail + with self.assertRaises(django_exceptions.ValidationError): + BomItem.objects.create(part=self.bob, sub_part=p, quantity=21.7) + + # But with an integer quantity, should be fine + BomItem.objects.create(part=self.bob, sub_part=p, quantity=21) + def test_overage(self): """ Test that BOM line overages are calculated correctly """