From f0b19e69b8dcddcec57fadde64fe4709749097d7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 6 Mar 2022 23:21:33 +0100 Subject: [PATCH] add total price woth js reload --- InvenTree/order/models.py | 18 +++++++++++++++++ InvenTree/order/serializers.py | 3 +++ .../templates/order/sales_order_base.html | 6 ++++++ .../templates/order/sales_order_detail.html | 7 +++++++ InvenTree/templates/js/translated/order.js | 20 +++++++++++++++++++ 5 files changed, 54 insertions(+) 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' %}" {{ order.responsible }} {% endif %} + + + + {% trans "Total cost" %} + {{ order.get_total_price }} + {% endblock %} diff --git a/InvenTree/order/templates/order/sales_order_detail.html b/InvenTree/order/templates/order/sales_order_detail.html index cbdcd26d45..ef1f6e61e0 100644 --- a/InvenTree/order/templates/order/sales_order_detail.html +++ b/InvenTree/order/templates/order/sales_order_detail.html @@ -292,6 +292,13 @@ } ); + loadOrderTotal( + '#soTotalPrice', + { + url: '{% url "api-so-detail" order.pk %}', + } + ); + enableSidebar('salesorder'); {% endblock %} \ No newline at end of file diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index 55db893733..e3aaf5f7fc 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -2290,6 +2290,24 @@ function showFulfilledSubTable(index, row, element, options) { }); } +var soTotalPriceRef = '' // safes reference to total price +var soTotalPriceOptions = {} // options to reload the price + +function loadOrderTotal(reference, options={}) { + soTotalPriceRef = reference; + soTotalPriceOptions = options; +} + +function reloadTotal(){ + inventreeGet( + soTotalPriceOptions.url, + {}, + {success: function(data){ + $(soTotalPriceRef).html(data.total_price_string); + }} + ); +}; + /** * Load a table displaying line items for a particular SalesOrder @@ -2587,6 +2605,7 @@ function loadSalesOrderLineItemTable(table, options={}) { function reloadTable() { $(table).bootstrapTable('refresh'); + reloadTotal(); } // Configure callback functions once the table is loaded @@ -2954,6 +2973,7 @@ function loadSalesOrderAdditionalLineItemTable(table, options={}) { function reloadTable() { $(table).bootstrapTable('refresh'); + reloadTotal(); } // Configure callback functions once the table is loaded