From a0f18d82cbd45dce33ab7a2be53f9e23091e133f Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 5 May 2023 23:43:12 +1000 Subject: [PATCH] Improve documentation on currency rendering (#4768) * Improve documentation on currency rendering * Fix .devcontainer file --- .devcontainer/postCreateCommand.sh | 2 +- InvenTree/InvenTree/helpers.py | 8 +++++++- docs/docs/report/helpers.md | 10 ++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/.devcontainer/postCreateCommand.sh b/.devcontainer/postCreateCommand.sh index 29a964366e..ff5deb1552 100755 --- a/.devcontainer/postCreateCommand.sh +++ b/.devcontainer/postCreateCommand.sh @@ -17,6 +17,6 @@ inv update inv setup-dev # remove existing gitconfig created by "Avoiding Dubious Ownership" step -# so that it gets copyied from host to the container to have your global +# so that it gets copyied from host to the container to have your global # git config in container rm -f /home/vscode/.gitconfig diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index b0ff845010..693c3c9241 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -1139,7 +1139,7 @@ def notify_responsible(instance, sender, content: NotificationBody = InvenTreeNo ) -def render_currency(money, decimal_places=None, currency=None, include_symbol=True, min_decimal_places=None): +def render_currency(money, decimal_places=None, currency=None, include_symbol=True, min_decimal_places=None, max_decimal_places=None): """Render a currency / Money object to a formatted string (e.g. for reports) Arguments: @@ -1148,6 +1148,7 @@ def render_currency(money, decimal_places=None, currency=None, include_symbol=Tr currency: Optionally convert to the specified currency include_symbol: Render with the appropriate currency symbol min_decimal_places: The minimum number of decimal places to render to. If unspecified, uses the PRICING_DECIMAL_PLACES_MIN setting. + max_decimal_places: The maximum number of decimal places to render to. If unspecified, uses the PRICING_DECIMAL_PLACES setting. """ if money in [None, '']: @@ -1170,6 +1171,9 @@ def render_currency(money, decimal_places=None, currency=None, include_symbol=Tr if min_decimal_places is None: min_decimal_places = common.models.InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES_MIN', 0) + if max_decimal_places is None: + max_decimal_places = common.models.InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES', 6) + value = Decimal(str(money.amount)).normalize() value = str(value) @@ -1183,6 +1187,8 @@ def render_currency(money, decimal_places=None, currency=None, include_symbol=Tr else: decimal_places = max(decimal_places, 2) + decimal_places = max(decimal_places, max_decimal_places) + return moneyed.localization.format_money( money, decimal_places=decimal_places, diff --git a/docs/docs/report/helpers.md b/docs/docs/report/helpers.md index 2cf0c94511..e3d53e559c 100644 --- a/docs/docs/report/helpers.md +++ b/docs/docs/report/helpers.md @@ -34,6 +34,16 @@ Total Price: {% render_currency order.total_price currency='NZD' decimal_places= {% endraw %} ``` +The following keyword arguments are available to the `render_currency` function: + +| Argument | Description | +| --- | --- | +| currency | Specify the currency code to render in (will attempt conversion if different to provided currency) | +| decimal_places | Specify the number of decimal places to render | +| min_decimal_places | Specify the minimum number of decimal places to render (ignored if *decimal_places* is specified) | +| max_decimal_places | Specify the maximum number of decimal places to render (ignored if *decimal_places* is specified) | +| include_symbol | Include currency symbol in rendered value (default = True) | + ## Maths Operations Simple mathematical operators are available, as demonstrated in the example template below: