mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	Optionally add all SalesOrderAllocations to the SalesOrderLineItem serializer
This commit is contained in:
		| @@ -19,8 +19,8 @@ from company.models import SupplierPart | ||||
| from .models import PurchaseOrder, PurchaseOrderLineItem | ||||
| from .serializers import POSerializer, POLineItemSerializer | ||||
|  | ||||
| from .models import SalesOrder, SalesOrderLineItem | ||||
| from .serializers import SalesOrderSerializer, SOLineItemSerializer | ||||
| from .models import SalesOrder, SalesOrderLineItem, SalesOrderAllocation | ||||
| from .serializers import SalesOrderSerializer, SOLineItemSerializer, SalesOrderAllocationSerializer | ||||
|  | ||||
|  | ||||
| class POList(generics.ListCreateAPIView): | ||||
| @@ -324,6 +324,11 @@ class SOLineItemList(generics.ListCreateAPIView): | ||||
|         except AttributeError: | ||||
|             pass | ||||
|  | ||||
|         try: | ||||
|             kwargs['allocations'] = str2bool(self.request.query_params.get('allocations', False)) | ||||
|         except AttributeError: | ||||
|             pass | ||||
|  | ||||
|         kwargs['context'] = self.get_serializer_context() | ||||
|  | ||||
|         return self.serializer_class(*args, **kwargs) | ||||
| @@ -345,6 +350,7 @@ class SOLineItemList(generics.ListCreateAPIView): | ||||
|  | ||||
|     filter_fields = [ | ||||
|         'order', | ||||
|         'part', | ||||
|     ] | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -451,3 +451,13 @@ class SalesOrderAllocation(models.Model): | ||||
|     ) | ||||
|  | ||||
|     quantity = RoundingDecimalField(max_digits=15, decimal_places=5, validators=[MinValueValidator(0)], default=1) | ||||
|  | ||||
|     def get_location(self): | ||||
|         return self.item.location.id if self.item.location else None | ||||
|  | ||||
|     def get_location_path(self): | ||||
|         if self.item.location: | ||||
|             return self.item.location.pathstring | ||||
|         else: | ||||
|             return "" | ||||
|      | ||||
| @@ -12,9 +12,11 @@ from django.db.models import Count | ||||
| from InvenTree.serializers import InvenTreeModelSerializer | ||||
| from company.serializers import CompanyBriefSerializer | ||||
| from part.serializers import PartBriefSerializer | ||||
| from stock.serializers import StockItemSerializer | ||||
|  | ||||
| from .models import PurchaseOrder, PurchaseOrderLineItem | ||||
| from .models import SalesOrder, SalesOrderLineItem | ||||
| from .models import SalesOrderAllocation | ||||
|  | ||||
|  | ||||
| class POSerializer(InvenTreeModelSerializer): | ||||
| @@ -143,6 +145,28 @@ class SalesOrderSerializer(InvenTreeModelSerializer): | ||||
|         ] | ||||
|  | ||||
|  | ||||
| class SalesOrderAllocationSerializer(InvenTreeModelSerializer): | ||||
|     """ | ||||
|     Serializer for the SalesOrderAllocation model. | ||||
|     This includes some fields from the related model objects. | ||||
|     """ | ||||
|  | ||||
|     location_path = serializers.CharField(source='get_location_path') | ||||
|     location_id = serializers.IntegerField(source='get_location') | ||||
|  | ||||
|     class Meta: | ||||
|         model = SalesOrderAllocation | ||||
|  | ||||
|         fields = [ | ||||
|             'pk', | ||||
|             'line', | ||||
|             'location_id', | ||||
|             'location_path', | ||||
|             'quantity', | ||||
|             'item', | ||||
|         ] | ||||
|  | ||||
|  | ||||
| class SOLineItemSerializer(InvenTreeModelSerializer): | ||||
|     """ Serializer for a SalesOrderLineItem object """ | ||||
|  | ||||
| @@ -150,6 +174,7 @@ class SOLineItemSerializer(InvenTreeModelSerializer): | ||||
|  | ||||
|         part_detail = kwargs.pop('part_detail', False) | ||||
|         order_detail = kwargs.pop('order_detail', False) | ||||
|         allocations = kwargs.pop('allocations', False) | ||||
|  | ||||
|         super().__init__(*args, **kwargs) | ||||
|  | ||||
| @@ -159,8 +184,12 @@ class SOLineItemSerializer(InvenTreeModelSerializer): | ||||
|         if order_detail is not True: | ||||
|             self.fields.pop('order_detail') | ||||
|  | ||||
|         if allocations is not True: | ||||
|             self.fields.pop('allocations') | ||||
|              | ||||
|     order_detail = SalesOrderSerializer(source='order', many=False, read_only=True) | ||||
|     part_detail = PartBriefSerializer(source='part', many=False, read_only=True) | ||||
|     allocations = SalesOrderAllocationSerializer(many=True, read_only=True) | ||||
|  | ||||
|     quantity = serializers.FloatField() | ||||
|     allocated = serializers.FloatField(source='allocated_quantity', read_only=True) | ||||
| @@ -171,6 +200,7 @@ class SOLineItemSerializer(InvenTreeModelSerializer): | ||||
|         fields = [ | ||||
|             'pk', | ||||
|             'allocated', | ||||
|             'allocations', | ||||
|             'quantity', | ||||
|             'reference', | ||||
|             'notes', | ||||
|   | ||||
| @@ -17,18 +17,18 @@ InvenTree | {% trans "Stock Item" %} - {{ item }} | ||||
|  | ||||
| {% for allocation in item.sales_order_allocations.all %} | ||||
| <div class='alert alert-block alert-info'> | ||||
|     {% trans "This stock item is allocated to Sales Order" %} <a href="{% url 'so-detail' allocation.line.order.id %}">#{{ allocation.line.order.id }}</a> ({% trans "Quantity" %}: {% decimal allocation.quantity %}) | ||||
|     {% trans "This stock item is allocated to Sales Order" %} <a href="{% url 'so-detail' allocation.line.order.id %}"><b>#{{ allocation.line.order.reference }}</b></a> ({% trans "Quantity" %}: {% decimal allocation.quantity %}) | ||||
| </div> | ||||
| {% endfor %} | ||||
|  | ||||
| {% for allocation in item.allocations.all %} | ||||
| <div class='alert alert-block alert-info'> | ||||
|     {% trans "This stock item is allocated to Build" %} <a href="{% url 'build-detail' allocation.build.id %}">#{{ allocation.build.id }}</a> ({% trans "Quantity" %}: {% decimal allocation.quantity %}) | ||||
|     {% trans "This stock item is allocated to Build" %} <a href="{% url 'build-detail' allocation.build.id %}"><b>#{{ allocation.build.id }}</b></a> ({% trans "Quantity" %}: {% decimal allocation.quantity %}) | ||||
| </div> | ||||
| {% endfor %} | ||||
|  | ||||
| {% if item.serialized %} | ||||
| <div class='alert alert-block alert-info'> | ||||
| <div class='alert alert-block alert-warning'> | ||||
|     {% trans "This stock item is serialized - it has a unique serial number and the quantity cannot be adjusted." %} | ||||
| </div> | ||||
| {% elif item.child_count > 0 %} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user