diff --git a/InvenTree/order/api.py b/InvenTree/order/api.py index cb33b3f680..eb8ba22ad0 100644 --- a/InvenTree/order/api.py +++ b/InvenTree/order/api.py @@ -215,6 +215,14 @@ class POLineItemList(generics.ListCreateAPIView): queryset = PurchaseOrderLineItem.objects.all() serializer_class = POLineItemSerializer + def get_queryset(self, *args, **kwargs): + + queryset = super().get_queryset(*args, **kwargs) + + queryset = POLineItemSerializer.annotate_queryset(queryset) + + return queryset + def get_serializer(self, *args, **kwargs): try: @@ -246,6 +254,7 @@ class POLineItemList(generics.ListCreateAPIView): 'received', 'reference', 'SKU', + 'total_price', ] search_fields = [ @@ -270,6 +279,14 @@ class POLineItemDetail(generics.RetrieveUpdateDestroyAPIView): queryset = PurchaseOrderLineItem.objects.all() serializer_class = POLineItemSerializer + def get_queryset(self): + + queryset = super().get_queryset() + + queryset = POLineItemSerializer.annotate_queryset(queryset) + + return queryset + class SOAttachmentList(generics.ListCreateAPIView, AttachmentMixin): """ diff --git a/InvenTree/order/serializers.py b/InvenTree/order/serializers.py index e97d19250a..fe23bd2a17 100644 --- a/InvenTree/order/serializers.py +++ b/InvenTree/order/serializers.py @@ -7,8 +7,9 @@ from __future__ import unicode_literals from django.utils.translation import ugettext_lazy as _ +from django.db import models from django.db.models import Case, When, Value -from django.db.models import BooleanField +from django.db.models import BooleanField, ExpressionWrapper, F from rest_framework import serializers from sql_util.utils import SubqueryCount @@ -109,6 +110,23 @@ class POSerializer(InvenTreeModelSerializer): class POLineItemSerializer(InvenTreeModelSerializer): + @staticmethod + def annotate_queryset(queryset): + """ + Add some extra annotations to this queryset: + + - Total price = purchase_price * quantity + """ + + queryset = queryset.annotate( + total_price=ExpressionWrapper( + F('purchase_price') * F('quantity'), + output_field=models.DecimalField() + ) + ) + + return queryset + def __init__(self, *args, **kwargs): part_detail = kwargs.pop('part_detail', False) @@ -123,6 +141,8 @@ class POLineItemSerializer(InvenTreeModelSerializer): quantity = serializers.FloatField(default=1) received = serializers.FloatField(default=0) + total_price = serializers.FloatField(read_only=True) + part_detail = PartBriefSerializer(source='get_base_part', many=False, read_only=True) supplier_part_detail = SupplierPartSerializer(source='part', many=False, read_only=True) @@ -158,6 +178,7 @@ class POLineItemSerializer(InvenTreeModelSerializer): 'purchase_price_string', 'destination', 'destination_detail', + 'total_price', ] diff --git a/InvenTree/order/templates/order/purchase_order_detail.html b/InvenTree/order/templates/order/purchase_order_detail.html index ef7a8e8703..d5f8dd048a 100644 --- a/InvenTree/order/templates/order/purchase_order_detail.html +++ b/InvenTree/order/templates/order/purchase_order_detail.html @@ -364,7 +364,8 @@ $("#po-table").inventreeTable({ } }, { - sortable: false, + field: 'total_price', + sortable: true, title: '{% trans "Total price" %}', formatter: function(value, row) { var total = row.purchase_price * row.quantity;