mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Refactor tractor
This commit is contained in:
		@@ -39,7 +39,7 @@ from InvenTree.models import InvenTreeTree, InvenTreeAttachment
 | 
				
			|||||||
from InvenTree.fields import InvenTreeURLField
 | 
					from InvenTree.fields import InvenTreeURLField
 | 
				
			||||||
from InvenTree.helpers import decimal2string, normalize
 | 
					from InvenTree.helpers import decimal2string, normalize
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from InvenTree.status_codes import BuildStatus, StockStatus, PurchaseOrderStatus
 | 
					from InvenTree.status_codes import BuildStatus, PurchaseOrderStatus
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from build import models as BuildModels
 | 
					from build import models as BuildModels
 | 
				
			||||||
from order import models as OrderModels
 | 
					from order import models as OrderModels
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -149,9 +149,8 @@ class StockItem(MPTTModel):
 | 
				
			|||||||
        - Adds a transaction note when the item is first created.
 | 
					        - Adds a transaction note when the item is first created.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Query to look for duplicate serial numbers
 | 
					        self.validate_unique()
 | 
				
			||||||
        parts = PartModels.Part.objects.filter(tree_id=self.part.tree_id)
 | 
					        self.clean()
 | 
				
			||||||
        stock = StockItem.objects.filter(part__in=parts, serial=self.serial)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if not self.pk:
 | 
					        if not self.pk:
 | 
				
			||||||
            # StockItem has not yet been saved
 | 
					            # StockItem has not yet been saved
 | 
				
			||||||
@@ -160,13 +159,6 @@ class StockItem(MPTTModel):
 | 
				
			|||||||
            # StockItem has already been saved
 | 
					            # StockItem has already been saved
 | 
				
			||||||
            add_note = False
 | 
					            add_note = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            stock = stock.exclude(pk=self.pk)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if self.serial is not None:
 | 
					 | 
				
			||||||
            # Check for presence of stock with same serial number
 | 
					 | 
				
			||||||
            if stock.exists():
 | 
					 | 
				
			||||||
                raise ValidationError({"serial": _("StockItem with this serial number already exists")})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        user = kwargs.pop('user', None)
 | 
					        user = kwargs.pop('user', None)
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        add_note = add_note and kwargs.pop('note', True)
 | 
					        add_note = add_note and kwargs.pop('note', True)
 | 
				
			||||||
@@ -193,30 +185,25 @@ class StockItem(MPTTModel):
 | 
				
			|||||||
        return self.serial is not None and self.quantity == 1
 | 
					        return self.serial is not None and self.quantity == 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def validate_unique(self, exclude=None):
 | 
					    def validate_unique(self, exclude=None):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Test that this StockItem is "unique".
 | 
				
			||||||
 | 
					        If the StockItem is serialized, the same serial number.
 | 
				
			||||||
 | 
					        cannot exist for the same part (or part tree).
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        super(StockItem, self).validate_unique(exclude)
 | 
					        super(StockItem, self).validate_unique(exclude)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # If the Part object is a variant (of a template part),
 | 
					 | 
				
			||||||
        # ensure that the serial number is unique
 | 
					 | 
				
			||||||
        # across all variants of the same template part
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        print("validating...")
 | 
					 | 
				
			||||||
        print(self.pk, self.serial)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
        if self.serial is not None:
 | 
					        if self.serial is not None:
 | 
				
			||||||
 | 
					            # Query to look for duplicate serial numbers
 | 
				
			||||||
            parts = PartModels.Part.objects.filter(tree_id=self.part.tree_id)
 | 
					            parts = PartModels.Part.objects.filter(tree_id=self.part.tree_id)
 | 
				
			||||||
                stock = StockItem.objects.filter(
 | 
					            stock = StockItem.objects.filter(part__in=parts, serial=self.serial)
 | 
				
			||||||
                    part__in=parts,
 | 
					
 | 
				
			||||||
                    serial=self.serial,
 | 
					            # Exclude myself from the search
 | 
				
			||||||
                ).exclude(pk=self.pk)
 | 
					            if self.pk is not None:
 | 
				
			||||||
 | 
					                stock = stock.exclude(pk=self.pk)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if stock.exists():
 | 
					            if stock.exists():
 | 
				
			||||||
                    raise ValidationError({
 | 
					                raise ValidationError({"serial": _("StockItem with this serial number already exists")})
 | 
				
			||||||
                            'serial': _('A stock item with this serial number already exists for this part'),
 | 
					 | 
				
			||||||
                        })
 | 
					 | 
				
			||||||
        except PartModels.Part.DoesNotExist:
 | 
					 | 
				
			||||||
            pass
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def clean(self):
 | 
					    def clean(self):
 | 
				
			||||||
        """ Validate the StockItem object (separate to field validation)
 | 
					        """ Validate the StockItem object (separate to field validation)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user