Optionally add all SalesOrderAllocations to the SalesOrderLineItem serializer

This commit is contained in:
Oliver Walters 2020-04-22 20:10:23 +10:00
parent 5d1754ec32
commit b70e79b778
4 changed files with 51 additions and 5 deletions

View File

@ -19,8 +19,8 @@ from company.models import SupplierPart
from .models import PurchaseOrder, PurchaseOrderLineItem from .models import PurchaseOrder, PurchaseOrderLineItem
from .serializers import POSerializer, POLineItemSerializer from .serializers import POSerializer, POLineItemSerializer
from .models import SalesOrder, SalesOrderLineItem from .models import SalesOrder, SalesOrderLineItem, SalesOrderAllocation
from .serializers import SalesOrderSerializer, SOLineItemSerializer from .serializers import SalesOrderSerializer, SOLineItemSerializer, SalesOrderAllocationSerializer
class POList(generics.ListCreateAPIView): class POList(generics.ListCreateAPIView):
@ -324,6 +324,11 @@ class SOLineItemList(generics.ListCreateAPIView):
except AttributeError: except AttributeError:
pass pass
try:
kwargs['allocations'] = str2bool(self.request.query_params.get('allocations', False))
except AttributeError:
pass
kwargs['context'] = self.get_serializer_context() kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs) return self.serializer_class(*args, **kwargs)
@ -345,6 +350,7 @@ class SOLineItemList(generics.ListCreateAPIView):
filter_fields = [ filter_fields = [
'order', 'order',
'part',
] ]

View File

@ -451,3 +451,13 @@ class SalesOrderAllocation(models.Model):
) )
quantity = RoundingDecimalField(max_digits=15, decimal_places=5, validators=[MinValueValidator(0)], default=1) 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 ""

View File

@ -12,9 +12,11 @@ from django.db.models import Count
from InvenTree.serializers import InvenTreeModelSerializer from InvenTree.serializers import InvenTreeModelSerializer
from company.serializers import CompanyBriefSerializer from company.serializers import CompanyBriefSerializer
from part.serializers import PartBriefSerializer from part.serializers import PartBriefSerializer
from stock.serializers import StockItemSerializer
from .models import PurchaseOrder, PurchaseOrderLineItem from .models import PurchaseOrder, PurchaseOrderLineItem
from .models import SalesOrder, SalesOrderLineItem from .models import SalesOrder, SalesOrderLineItem
from .models import SalesOrderAllocation
class POSerializer(InvenTreeModelSerializer): 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): class SOLineItemSerializer(InvenTreeModelSerializer):
""" Serializer for a SalesOrderLineItem object """ """ Serializer for a SalesOrderLineItem object """
@ -150,6 +174,7 @@ class SOLineItemSerializer(InvenTreeModelSerializer):
part_detail = kwargs.pop('part_detail', False) part_detail = kwargs.pop('part_detail', False)
order_detail = kwargs.pop('order_detail', False) order_detail = kwargs.pop('order_detail', False)
allocations = kwargs.pop('allocations', False)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -159,8 +184,12 @@ class SOLineItemSerializer(InvenTreeModelSerializer):
if order_detail is not True: if order_detail is not True:
self.fields.pop('order_detail') self.fields.pop('order_detail')
if allocations is not True:
self.fields.pop('allocations')
order_detail = SalesOrderSerializer(source='order', many=False, read_only=True) order_detail = SalesOrderSerializer(source='order', many=False, read_only=True)
part_detail = PartBriefSerializer(source='part', 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() quantity = serializers.FloatField()
allocated = serializers.FloatField(source='allocated_quantity', read_only=True) allocated = serializers.FloatField(source='allocated_quantity', read_only=True)
@ -171,6 +200,7 @@ class SOLineItemSerializer(InvenTreeModelSerializer):
fields = [ fields = [
'pk', 'pk',
'allocated', 'allocated',
'allocations',
'quantity', 'quantity',
'reference', 'reference',
'notes', 'notes',

View File

@ -17,18 +17,18 @@ InvenTree | {% trans "Stock Item" %} - {{ item }}
{% for allocation in item.sales_order_allocations.all %} {% for allocation in item.sales_order_allocations.all %}
<div class='alert alert-block alert-info'> <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> </div>
{% endfor %} {% endfor %}
{% for allocation in item.allocations.all %} {% for allocation in item.allocations.all %}
<div class='alert alert-block alert-info'> <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> </div>
{% endfor %} {% endfor %}
{% if item.serialized %} {% 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." %} {% trans "This stock item is serialized - it has a unique serial number and the quantity cannot be adjusted." %}
</div> </div>
{% elif item.child_count > 0 %} {% elif item.child_count > 0 %}