diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index 2a975a432c..f7382feb84 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -23,6 +23,7 @@ from markdownx.models import MarkdownxField from mptt.models import TreeForeignKey from djmoney.contrib.exchange.models import convert_money +from djmoney.money import Money from common.settings import currency_code_default from users import models as UserModels @@ -600,6 +601,23 @@ class SalesOrder(Order): verbose_name=_('shipped by') ) + def get_total_price(self): + """ + Calculates the total price of all order lines + """ + target_currency = currency_code_default() + total = Money(0, target_currency) + + # order items + total += sum([a.quantity * convert_money(a.sale_price, target_currency) for a in self.lines.all() if a.sale_price]) + + # additional lines + total += sum([a.quantity * convert_money(a.sale_price, target_currency) for a in self.additional_lines.all() if a.sale_price]) + + # set decimal-places + total.decimal_places = 4 + return total + @property def is_overdue(self): """ diff --git a/InvenTree/order/serializers.py b/InvenTree/order/serializers.py index 529897d942..7398d37055 100644 --- a/InvenTree/order/serializers.py +++ b/InvenTree/order/serializers.py @@ -515,6 +515,8 @@ class SalesOrderSerializer(ReferenceIndexingSerializerMixin, InvenTreeModelSeria reference = serializers.CharField(required=True) + total_price_string = serializers.CharField(source='get_total_price', read_only=True) + class Meta: model = order.models.SalesOrder @@ -535,6 +537,7 @@ class SalesOrderSerializer(ReferenceIndexingSerializerMixin, InvenTreeModelSeria 'status_text', 'shipment_date', 'target_date', + 'total_price_string', ] read_only_fields = [ diff --git a/InvenTree/order/templates/order/sales_order_base.html b/InvenTree/order/templates/order/sales_order_base.html index 423090f917..9abd058996 100644 --- a/InvenTree/order/templates/order/sales_order_base.html +++ b/InvenTree/order/templates/order/sales_order_base.html @@ -183,6 +183,12 @@ src="{% static 'img/blank_image.png' %}"