mirror of
https://github.com/inventree/InvenTree.git
synced 2026-07-22 06:33:03 +00:00
Fix total_price annotation for ExtraLineItem
This commit is contained in:
@@ -80,6 +80,7 @@ class GeneralExtraLineList(SerializerContextMixin, DataExportViewMixin):
|
|||||||
queryset = super().get_queryset(*args, **kwargs)
|
queryset = super().get_queryset(*args, **kwargs)
|
||||||
|
|
||||||
queryset = queryset.prefetch_related('order')
|
queryset = queryset.prefetch_related('order')
|
||||||
|
queryset = self.serializer_class.annotate_queryset(queryset)
|
||||||
|
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
@@ -96,6 +97,16 @@ class GeneralExtraLineList(SerializerContextMixin, DataExportViewMixin):
|
|||||||
filterset_fields = ['order']
|
filterset_fields = ['order']
|
||||||
|
|
||||||
|
|
||||||
|
class GeneralExtraLineDetail:
|
||||||
|
"""General template for ExtraLine detail API classes."""
|
||||||
|
|
||||||
|
def get_queryset(self, *args, **kwargs):
|
||||||
|
"""Return the annotated queryset for this endpoint."""
|
||||||
|
queryset = super().get_queryset(*args, **kwargs)
|
||||||
|
|
||||||
|
return self.serializer_class.annotate_queryset(queryset)
|
||||||
|
|
||||||
|
|
||||||
class OrderCreateMixin:
|
class OrderCreateMixin:
|
||||||
"""Mixin class which handles order creation via API."""
|
"""Mixin class which handles order creation via API."""
|
||||||
|
|
||||||
@@ -778,7 +789,7 @@ class PurchaseOrderExtraLineList(
|
|||||||
serializer_class = serializers.PurchaseOrderExtraLineSerializer
|
serializer_class = serializers.PurchaseOrderExtraLineSerializer
|
||||||
|
|
||||||
|
|
||||||
class PurchaseOrderExtraLineDetail(RetrieveUpdateDestroyAPI):
|
class PurchaseOrderExtraLineDetail(GeneralExtraLineDetail, RetrieveUpdateDestroyAPI):
|
||||||
"""API endpoint for detail view of a PurchaseOrderExtraLine object."""
|
"""API endpoint for detail view of a PurchaseOrderExtraLine object."""
|
||||||
|
|
||||||
queryset = models.PurchaseOrderExtraLine.objects.all()
|
queryset = models.PurchaseOrderExtraLine.objects.all()
|
||||||
@@ -1113,7 +1124,7 @@ class SalesOrderExtraLineList(GeneralExtraLineList, OutputOptionsMixin, ListCrea
|
|||||||
serializer_class = serializers.SalesOrderExtraLineSerializer
|
serializer_class = serializers.SalesOrderExtraLineSerializer
|
||||||
|
|
||||||
|
|
||||||
class SalesOrderExtraLineDetail(RetrieveUpdateDestroyAPI):
|
class SalesOrderExtraLineDetail(GeneralExtraLineDetail, RetrieveUpdateDestroyAPI):
|
||||||
"""API endpoint for detail view of a SalesOrderExtraLine object."""
|
"""API endpoint for detail view of a SalesOrderExtraLine object."""
|
||||||
|
|
||||||
queryset = models.SalesOrderExtraLine.objects.all()
|
queryset = models.SalesOrderExtraLine.objects.all()
|
||||||
@@ -1826,7 +1837,7 @@ class ReturnOrderExtraLineList(GeneralExtraLineList, OutputOptionsMixin, ListCre
|
|||||||
serializer_class = serializers.ReturnOrderExtraLineSerializer
|
serializer_class = serializers.ReturnOrderExtraLineSerializer
|
||||||
|
|
||||||
|
|
||||||
class ReturnOrderExtraLineDetail(RetrieveUpdateDestroyAPI):
|
class ReturnOrderExtraLineDetail(GeneralExtraLineDetail, RetrieveUpdateDestroyAPI):
|
||||||
"""API endpoint for detail view of a ReturnOrderExtraLine object."""
|
"""API endpoint for detail view of a ReturnOrderExtraLine object."""
|
||||||
|
|
||||||
queryset = models.ReturnOrderExtraLine.objects.all()
|
queryset = models.ReturnOrderExtraLine.objects.all()
|
||||||
|
|||||||
@@ -334,6 +334,7 @@ class AbstractExtraLineSerializer(
|
|||||||
'quantity',
|
'quantity',
|
||||||
'reference',
|
'reference',
|
||||||
'target_date',
|
'target_date',
|
||||||
|
'total_price',
|
||||||
# Filterable detail fields
|
# Filterable detail fields
|
||||||
'order_detail',
|
'order_detail',
|
||||||
'project_code_label',
|
'project_code_label',
|
||||||
@@ -341,6 +342,19 @@ class AbstractExtraLineSerializer(
|
|||||||
*extra_fields,
|
*extra_fields,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def annotate_queryset(queryset):
|
||||||
|
"""Add annotations to the queryset.
|
||||||
|
|
||||||
|
- total_price: price * quantity, adjusted for the line discount
|
||||||
|
"""
|
||||||
|
return queryset.annotate(
|
||||||
|
total_price=ExpressionWrapper(
|
||||||
|
F('price') * F('quantity') * (1 - F('discount') / Decimal(100)),
|
||||||
|
output_field=models.DecimalField(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
quantity = serializers.FloatField()
|
quantity = serializers.FloatField()
|
||||||
|
|
||||||
discount = InvenTreeDecimalField(required=False)
|
discount = InvenTreeDecimalField(required=False)
|
||||||
@@ -349,6 +363,8 @@ class AbstractExtraLineSerializer(
|
|||||||
|
|
||||||
price_currency = InvenTreeCurrencySerializer()
|
price_currency = InvenTreeCurrencySerializer()
|
||||||
|
|
||||||
|
total_price = serializers.FloatField(read_only=True)
|
||||||
|
|
||||||
project_code_label = common.filters.enable_project_label_filter()
|
project_code_label = common.filters.enable_project_label_filter()
|
||||||
|
|
||||||
project_code_detail = common.filters.enable_project_code_filter()
|
project_code_detail = common.filters.enable_project_code_filter()
|
||||||
@@ -369,6 +385,7 @@ class AbstractExtraLineMeta:
|
|||||||
'order_detail',
|
'order_detail',
|
||||||
'price',
|
'price',
|
||||||
'price_currency',
|
'price_currency',
|
||||||
|
'total_price',
|
||||||
'link',
|
'link',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user