diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index bd05e28e44..e0b0047332 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -21,9 +21,11 @@ from django.http import StreamingHttpResponse from django.test import TestCase from django.utils.translation import gettext_lazy as _ +import moneyed.localization import regex import requests from bleach import clean +from djmoney.contrib.exchange.models import convert_money from djmoney.money import Money from PIL import Image @@ -1105,3 +1107,47 @@ def notify_responsible(instance, sender, content: NotificationBody = InvenTreeNo target_exclude=[exclude], context=context, ) + + +def render_currency(money, decimal_places=None, currency=None, include_symbol=True): + """Render a currency / Money object to a formatted string (e.g. for reports) + + Arguments: + money: The Money instance to be rendered + decimal_places: The number of decimal places to render to. If unspecified, uses the PRICING_DECIMAL_PLACES setting. + currency: Optionally convert to the specified currency + include_symbol: Render with the appropriate currency symbol + """ + + if money is None or money.amount is None: + return '-' + + if currency is not None: + # Attempt to convert to the provided currency + # If cannot be done, leave the original + try: + money = convert_money(money, currency) + except Exception: + pass + + if decimal_places is None: + decimal_places = InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES', 6) + + value = Decimal(str(money.amount)).normalize() + value = str(value) + + if '.' in value: + decimals = len(value.split('.')[-1]) + + decimals = max(decimals, 2) + decimals = min(decimals, decimal_places) + + decimal_places = decimals + else: + decimal_places = max(decimal_places, 2) + + return moneyed.localization.format_money( + money, + decimal_places=decimal_places, + include_symbol=include_symbol, + ) diff --git a/InvenTree/order/templates/order/order_base.html b/InvenTree/order/templates/order/order_base.html index 1fb6586b6f..d2a5bb9871 100644 --- a/InvenTree/order/templates/order/order_base.html +++ b/InvenTree/order/templates/order/order_base.html @@ -195,7 +195,7 @@ src="{% static 'img/blank_image.png' %}" {% if tp == None %} {% trans "Total cost could not be calculated" %} {% else %} - {% include "price_data.html" with price=tp %} + {% render_currency tp currency=order.supplier.currency %} {% endif %} {% endwith %} diff --git a/InvenTree/order/templates/order/sales_order_base.html b/InvenTree/order/templates/order/sales_order_base.html index d289b72eb4..20acc35379 100644 --- a/InvenTree/order/templates/order/sales_order_base.html +++ b/InvenTree/order/templates/order/sales_order_base.html @@ -193,7 +193,7 @@ src="{% static 'img/blank_image.png' %}" {% if tp == None %} {% trans "Total cost could not be calculated" %} {% else %} - {% include "price_data.html" with price=tp %} + {% render_currency tp currency=order.customer.currency %} {% endif %} {% endwith %} diff --git a/InvenTree/part/templates/part/prices.html b/InvenTree/part/templates/part/prices.html index 380720408b..f788e4f36d 100644 --- a/InvenTree/part/templates/part/prices.html +++ b/InvenTree/part/templates/part/prices.html @@ -46,8 +46,8 @@ {% endif %} {% trans "Internal Pricing" %} - {% include "price_data.html" with price=pricing.internal_cost_min %} - {% include "price_data.html" with price=pricing.internal_cost_max %} + {% render_currency pricing.internal_cost_min %} + {% render_currency pricing.internal_cost_max %} {% if part.purchaseable %} @@ -59,8 +59,8 @@ {% endif %} {% trans "Purchase History" %} - {% include "price_data.html" with price=pricing.purchase_cost_min %} - {% include "price_data.html" with price=pricing.purchase_cost_max %} + {% render_currency pricing.purchase_cost_min %} + {% render_currency pricing.purchase_cost_max %} @@ -71,8 +71,8 @@ {% endif %} {% trans "Supplier Pricing" %} - {% include "price_data.html" with price=pricing.supplier_price_min %} - {% include "price_data.html" with price=pricing.supplier_price_max %} + {% render_currency pricing.supplier_price_min %} + {% render_currency pricing.supplier_price_max %} {% endif %} {% if part.assembly %} @@ -85,23 +85,23 @@ {% endif %} {% trans "BOM Pricing" %} - {% include "price_data.html" with price=pricing.bom_cost_min %} - {% include "price_data.html" with price=pricing.bom_cost_max %} + {% render_currency pricing.bom_cost_min %} + {% render_currency pricing.bom_cost_max %} {% endif %} {% if part.is_template %} {% trans "Variant Pricing" %} - {% include "price_data.html" with price=pricing.variant_cost_min %} - {% include "price_data.html" with price=pricing.variant_cost_max %} + {% render_currency pricing.variant_cost_min %} + {% render_currency pricing.variant_cost_max %} {% endif %} {% trans "Overall Pricing" %} - {% include "price_data.html" with price=pricing.overall_min %} - {% include "price_data.html" with price=pricing.overall_max %} + {% render_currency pricing.overall_min %} + {% render_currency pricing.overall_max %} @@ -126,8 +126,8 @@ {% trans "Sale Price" %} - {% include "price_data.html" with price=pricing.sale_price_min %} - {% include "price_data.html" with price=pricing.sale_price_max %} + {% render_currency pricing.sale_price_min %} + {% render_currency pricing.sale_price_max %} @@ -136,8 +136,8 @@ {% trans "Sale History" %} - {% include "price_data.html" with price=pricing.sale_history_min %} - {% include "price_data.html" with price=pricing.sale_history_max %} + {% render_currency pricing.sale_history_min %} + {% render_currency pricing.sale_history_max %} diff --git a/InvenTree/part/templatetags/inventree_extras.py b/InvenTree/part/templatetags/inventree_extras.py index c658267104..c64f6bcbc7 100644 --- a/InvenTree/part/templatetags/inventree_extras.py +++ b/InvenTree/part/templatetags/inventree_extras.py @@ -4,7 +4,6 @@ import logging import os import sys from datetime import date, datetime -from decimal import Decimal from django import template from django.conf import settings as djangosettings @@ -14,8 +13,6 @@ from django.utils.html import format_html from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ -import moneyed.localization - import InvenTree.helpers from common.models import ColorTheme, InvenTreeSetting, InvenTreeUserSetting from common.settings import currency_code_default @@ -104,33 +101,10 @@ def render_date(context, date_object): @register.simple_tag -def render_currency(money, decimal_places=None, include_symbol=True): +def render_currency(money, **kwargs): """Render a currency / Money object""" - if money is None or money.amount is None: - return '-' - - if decimal_places is None: - decimal_places = InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES', 6) - - value = Decimal(str(money.amount)).normalize() - value = str(value) - - if '.' in value: - decimals = len(value.split('.')[-1]) - - decimals = max(decimals, 2) - decimals = min(decimals, decimal_places) - - decimal_places = decimals - else: - decimal_places = 2 - - return moneyed.localization.format_money( - money, - decimal_places=decimal_places, - include_symbol=include_symbol, - ) + return InvenTree.helpers.render_currency(money, **kwargs) @register.simple_tag() diff --git a/InvenTree/report/templates/report/inventree_po_report_base.html b/InvenTree/report/templates/report/inventree_po_report_base.html index dd6de4296b..0aa77f6c3b 100644 --- a/InvenTree/report/templates/report/inventree_po_report_base.html +++ b/InvenTree/report/templates/report/inventree_po_report_base.html @@ -107,8 +107,8 @@ table td.expand { {{ line.reference }} {% decimal line.quantity %} - {% include "price_data.html" with price=line.purchase_price %} - {% include "price_data.html" with price=line.total_line_price %} + {% render_currency line.price decimal_places=2 %} + {% render_currency line.total_line_price decimal_places=2 %} {{ line.notes }} {% endfor %} @@ -120,8 +120,8 @@ table td.expand { {{ line.reference }} {% decimal line.quantity %} - {% include "price_data.html" with price=line.price %} - {% include "price_data.html" with price=line.total_line_price %} + {% render_currency line.price decimal_places=2 %} + {% render_currency line.total_line_price decimal_places=2 %} {{ line.notes }} {% endfor %} @@ -132,7 +132,7 @@ table td.expand { {% trans "Total" %} - {% include "price_data.html" with price=order.total_price %} + {% render_currency order.total_price decimal_places=2 currency=order.supplier.currency %} diff --git a/InvenTree/report/templates/report/inventree_so_report_base.html b/InvenTree/report/templates/report/inventree_so_report_base.html index 9f3413502c..4869a5453c 100644 --- a/InvenTree/report/templates/report/inventree_so_report_base.html +++ b/InvenTree/report/templates/report/inventree_so_report_base.html @@ -108,8 +108,8 @@ table td.expand { {{ line.reference }} {% decimal line.quantity %} - {% include "price_data.html" with price=line.sale_price %} - {% include "price_data.html" with price=line.total_line_price %} + {% render_currency line.price %} + {% render_currency line.total_line_price %} {{ line.notes }} {% endfor %} @@ -121,8 +121,8 @@ table td.expand { {{ line.reference }} {% decimal line.quantity %} - {% include "price_data.html" with price=line.price %} - {% include "price_data.html" with price=line.total_line_price %} + {% render_currency line.price %} + {% render_currency line.total_line_price %} {{ line.notes }} {% endfor %} @@ -133,7 +133,7 @@ table td.expand { {% trans "Total" %} - {% include "price_data.html" with price=order.total_price %} + {% render_currency order.total_price currency=order.customer.currency %} diff --git a/InvenTree/report/templatetags/report.py b/InvenTree/report/templatetags/report.py index 810c695a36..474cb8fa02 100644 --- a/InvenTree/report/templatetags/report.py +++ b/InvenTree/report/templatetags/report.py @@ -208,3 +208,10 @@ def multiply(x, y): def divide(x, y): """Divide one number by another""" return x / y + + +@register.simple_tag +def render_currency(money, **kwargs): + """Render a currency / Money object""" + + return InvenTree.helpers.render_currency(money, **kwargs) diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index 2ec331e233..049c0973c7 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -188,7 +188,7 @@ {% trans "Purchase Price" %} - {% include "price_data.html" with price=item.purchase_price %} + {% render_currency item.purchase_price %} {% if item.part.units %} / {{ item.part.units }}{% endif %} diff --git a/InvenTree/templates/price_data.html b/InvenTree/templates/price_data.html deleted file mode 100644 index e7cd03e17a..0000000000 --- a/InvenTree/templates/price_data.html +++ /dev/null @@ -1,8 +0,0 @@ -{% load inventree_extras %} -{% load i18n %} - -{% if price %} -{% render_currency price %} -{% else %} -{% trans "No data" %} -{% endif %}