From 9afc6cc6cbf859bfebfc6035585826d0d3a2622b Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 29 Aug 2024 12:21:38 +1000 Subject: [PATCH] Adjust "render_currency" behaviour (#8017) * Adjust "render_currency" behaviour - Fixes https://github.com/inventree/InvenTree/issues/7997 * Update docs --- docs/docs/report/helpers.md | 4 ++-- .../InvenTree/InvenTree/helpers_model.py | 23 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/docs/docs/report/helpers.md b/docs/docs/report/helpers.md index 3e8f8aff6f..6dc90fb7a6 100644 --- a/docs/docs/report/helpers.md +++ b/docs/docs/report/helpers.md @@ -130,8 +130,8 @@ The following keyword arguments are available to the `render_currency` function: | --- | --- | | 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) | +| min_decimal_places | Specify the minimum number of decimal places to render | +| max_decimal_places | Specify the maximum number of decimal places to render | | include_symbol | Include currency symbol in rendered value (default = True) | ## Maths Operations diff --git a/src/backend/InvenTree/InvenTree/helpers_model.py b/src/backend/InvenTree/InvenTree/helpers_model.py index 44b6a9f506..61e66d2900 100644 --- a/src/backend/InvenTree/InvenTree/helpers_model.py +++ b/src/backend/InvenTree/InvenTree/helpers_model.py @@ -210,9 +210,6 @@ def render_currency( except Exception: pass - if decimal_places is None: - decimal_places = get_global_setting('PRICING_DECIMAL_PLACES', 6) - if min_decimal_places is None: min_decimal_places = get_global_setting('PRICING_DECIMAL_PLACES_MIN', 0) @@ -222,17 +219,19 @@ def render_currency( value = Decimal(str(money.amount)).normalize() value = str(value) - if '.' in value: - decimals = len(value.split('.')[-1]) - - decimals = max(decimals, min_decimal_places) - decimals = min(decimals, decimal_places) - - decimal_places = decimals + if decimal_places is not None: + # Decimal place count is provided, use it + pass + elif '.' in value: + # If the value has a decimal point, use the number of decimal places in the value + decimal_places = len(value.split('.')[-1]) else: - decimal_places = max(decimal_places, 2) + # No decimal point, use 2 as a default + decimal_places = 2 - decimal_places = max(decimal_places, max_decimal_places) + # Clip the decimal places to the specified range + decimal_places = max(decimal_places, min_decimal_places) + decimal_places = min(decimal_places, max_decimal_places) return format_money( money, decimal_places=decimal_places, include_symbol=include_symbol