diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index e195130f03..56d6474af2 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -147,15 +147,25 @@ class BuildItem(models.Model): The following checks are performed: - StockItem.part must be in the BOM of the Part object referenced by Build + - Allocation quantity cannot exceed available quantity """ + + super(BuildItem, self).clean() - if self.stock_item.part not in self.build.part.required_parts(): - print('stock_item:', self.stock_item.part) - for p in self.build.part.bom_items.all(): - print('bom_part:', p) - raise ValidationError( - {'stock_item': _("Selected stock item not found in BOM for part '{p}'".format(p=str(self.build.part)))} - ) + errors = {} + + if self.stock_item is not None and self.stock_item.part is not None: + if self.stock_item.part not in self.build.part.required_parts(): + errors['stock_item'] = _("Selected stock item not found in BOM for part '{p}'".format(p=str(self.build.part))) + + if self.stock_item is not None and self.quantity > self.stock_item.quantity: + errors['quantity'] = _("Allocated quantity ({n}) must not exceed available quantity ({q})".format( + n=self.quantity, + q=self.stock_item.quantity + )) + + if len(errors) > 0: + raise ValidationError(errors) build = models.ForeignKey( Build,