mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Adds "items" list to API endpoint
This commit is contained in:
		@@ -628,29 +628,31 @@ class SalesOrder(Order):
 | 
				
			|||||||
        Throws a ValidationError if cannot be completed.
 | 
					        Throws a ValidationError if cannot be completed.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Order without line items cannot be completed
 | 
					            # Order without line items cannot be completed
 | 
				
			||||||
            if self.lines.count() == 0:
 | 
					            if self.lines.count() == 0:
 | 
				
			||||||
            if raise_error:
 | 
					 | 
				
			||||||
                raise ValidationError(_('Order cannot be completed as no parts have been assigned'))
 | 
					                raise ValidationError(_('Order cannot be completed as no parts have been assigned'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Only a PENDING order can be marked as SHIPPED
 | 
					            # Only a PENDING order can be marked as SHIPPED
 | 
				
			||||||
            elif self.status != SalesOrderStatus.PENDING:
 | 
					            elif self.status != SalesOrderStatus.PENDING:
 | 
				
			||||||
            if raise_error:
 | 
					 | 
				
			||||||
                raise ValidationError(_('Only a pending order can be marked as complete'))
 | 
					                raise ValidationError(_('Only a pending order can be marked as complete'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            elif self.pending_shipment_count > 0:
 | 
					            elif self.pending_shipment_count > 0:
 | 
				
			||||||
            if raise_error:
 | 
					 | 
				
			||||||
                raise ValidationError(_("Order cannot be completed as there are incomplete shipments"))
 | 
					                raise ValidationError(_("Order cannot be completed as there are incomplete shipments"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            elif self.pending_line_count > 0:
 | 
					            elif self.pending_line_count > 0:
 | 
				
			||||||
            if raise_error:
 | 
					 | 
				
			||||||
                raise ValidationError(_("Order cannot be completed as there are incomplete line items"))
 | 
					                raise ValidationError(_("Order cannot be completed as there are incomplete line items"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        else:
 | 
					        except ValidationError as e:
 | 
				
			||||||
            return True
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if raise_error:
 | 
				
			||||||
 | 
					                raise e
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
                return False
 | 
					                return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def complete_order(self, user):
 | 
					    def complete_order(self, user):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        Mark this order as "complete"
 | 
					        Mark this order as "complete"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1153,6 +1153,48 @@ class StockItem(MPTTModel):
 | 
				
			|||||||
            result.stock_item = self
 | 
					            result.stock_item = self
 | 
				
			||||||
            result.save()
 | 
					            result.save()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def can_merge(self, other=None, raise_error=False):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Check if this stock item can be merged into another
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            if not self.in_stock:
 | 
				
			||||||
 | 
					                raise ValidationError(_("Item must be in stock"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if self.serialized:
 | 
				
			||||||
 | 
					                raise ValidationError(_("Serialized stock cannot be merged"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        except ValidationError as e:
 | 
				
			||||||
 | 
					            if raise_error:
 | 
				
			||||||
 | 
					                raise e
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @transaction.atomic
 | 
				
			||||||
 | 
					    def merge_stock_item(self, other, **kwargs):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Merge another stock item into this one; the two become one!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        *This* stock item subsumes the other, which is essentially deleted:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        - The quantity of this StockItem is increased
 | 
				
			||||||
 | 
					        - Tracking history for the *other* item is deleted
 | 
				
			||||||
 | 
					        - Any allocations (build order, sales order) are moved to this StockItem
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # If the stock item cannot be merged, return
 | 
				
			||||||
 | 
					        if not self.can_merge(other):
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        user = kwargs.get('user', None)
 | 
				
			||||||
 | 
					        location = kwargs.get('location', None)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # TODO: Merge!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @transaction.atomic
 | 
					    @transaction.atomic
 | 
				
			||||||
    def splitStock(self, quantity, location, user, **kwargs):
 | 
					    def splitStock(self, quantity, location, user, **kwargs):
 | 
				
			||||||
        """ Split this stock item into two items, in the same location.
 | 
					        """ Split this stock item into two items, in the same location.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -674,6 +674,33 @@ class StockAssignmentSerializer(serializers.Serializer):
 | 
				
			|||||||
                )
 | 
					                )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class StockMergeItemSerializer(serializers.Serializer):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Serializer for a single StockItem within the StockMergeSerializer class.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Here, the individual StockItem is being checked for merge compatibility.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class Meta:
 | 
				
			||||||
 | 
					        fields = [
 | 
				
			||||||
 | 
					            'item',
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    item = serializers.PrimaryKeyRelatedField(
 | 
				
			||||||
 | 
					        queryset=StockItem.objects.all(),
 | 
				
			||||||
 | 
					        many=False,
 | 
				
			||||||
 | 
					        allow_null=False,
 | 
				
			||||||
 | 
					        required=True,
 | 
				
			||||||
 | 
					        label=_('Stock Item'),
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def validate_item(self, item):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Check that the stock item is able to be merged
 | 
				
			||||||
 | 
					        item.can_merge(raise_error=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return item
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class StockMergeSerializer(serializers.Serializer):
 | 
					class StockMergeSerializer(serializers.Serializer):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    Serializer for merging two (or more) stock items together
 | 
					    Serializer for merging two (or more) stock items together
 | 
				
			||||||
@@ -681,10 +708,15 @@ class StockMergeSerializer(serializers.Serializer):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        fields = [
 | 
					        fields = [
 | 
				
			||||||
            # 'items',
 | 
					            'items',
 | 
				
			||||||
            'location',
 | 
					            'location',
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    items = StockMergeItemSerializer(
 | 
				
			||||||
 | 
					        many=True,
 | 
				
			||||||
 | 
					        required=True,
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    location = serializers.PrimaryKeyRelatedField(
 | 
					    location = serializers.PrimaryKeyRelatedField(
 | 
				
			||||||
        queryset=StockLocation.objects.all(),
 | 
					        queryset=StockLocation.objects.all(),
 | 
				
			||||||
        many=False,
 | 
					        many=False,
 | 
				
			||||||
@@ -698,7 +730,10 @@ class StockMergeSerializer(serializers.Serializer):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        data = super().validate(data)
 | 
					        data = super().validate(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # TODO: Custom data validation
 | 
					        items = data['items']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if len(items) < 2:
 | 
				
			||||||
 | 
					            raise ValidationError(_('At least two stock items must be provided'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return data
 | 
					        return data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user