2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-01 03:00:54 +00:00

Virtual part fix (#3265)

* Add 'virtual': False requirement to sales order items

* Adds "virtual" filter for part table

* Adds extra validation to the SalesOrderLineItem model

* Prevent creation of stock items for virtual parts

- Add validation check to clean() method of StockItem model
- Improve validation message for StockItemSerializer class

* Hide "new stock item" button for virtual parts

* Hide more buttons for 'virtual' parts
This commit is contained in:
Oliver
2022-06-28 13:39:27 +10:00
committed by GitHub
parent b63ba4b636
commit 8f92fddd2d
7 changed files with 110 additions and 30 deletions

View File

@ -342,6 +342,7 @@ class StockItem(MetadataMixin, MPTTModel):
- Unique serial number requirement
- Adds a transaction note when the item is first created.
"""
self.validate_unique()
self.clean()
@ -439,6 +440,7 @@ class StockItem(MetadataMixin, MPTTModel):
The following validation checks are performed:
- The 'part' and 'supplier_part.part' fields cannot point to the same Part object
- The 'part' is not virtual
- The 'part' does not belong to itself
- Quantity must be 1 if the StockItem has a serial number
"""
@ -453,12 +455,18 @@ class StockItem(MetadataMixin, MPTTModel):
self.batch = self.batch.strip()
try:
# Trackable parts must have integer values for quantity field!
if self.part.trackable:
# Trackable parts must have integer values for quantity field!
if self.quantity != int(self.quantity):
raise ValidationError({
'quantity': _('Quantity must be integer value for trackable parts')
})
# Virtual parts cannot have stock items created against them
if self.part.virtual:
raise ValidationError({
'part': _("Stock item cannot be created for virtual parts"),
})
except PartModels.Part.DoesNotExist:
# For some reason the 'clean' process sometimes throws errors because self.part does not exist
# It *seems* that this only occurs in unit testing, though.
@ -582,7 +590,8 @@ class StockItem(MetadataMixin, MPTTModel):
part = models.ForeignKey(
'part.Part', on_delete=models.CASCADE,
verbose_name=_('Base Part'),
related_name='stock_items', help_text=_('Base part'),
related_name='stock_items',
help_text=_('Base part'),
limit_choices_to={
'virtual': False
})

View File

@ -79,6 +79,21 @@ class StockItemSerializer(InvenTree.serializers.InvenTreeModelSerializer):
- Includes serialization for the item location
"""
part = serializers.PrimaryKeyRelatedField(
queryset=part_models.Part.objects.all(),
many=False, allow_null=False,
help_text=_("Base Part"),
label=_("Part"),
)
def validate_part(self, part):
"""Ensure the provided Part instance is valid"""
if part.virtual:
raise ValidationError(_("Stock item cannot be created for virtual parts"))
return part
def update(self, instance, validated_data):
"""Custom update method to pass the user information through to the instance."""
instance._user = self.context['user']