mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Decimal rendering - support "minimum places" setting (#4527)
* Add configurable setting for "minimum" decimal places * Specify minimum decimal places to 'render_currency' * Fix for rendering currency in tables
This commit is contained in:
		| @@ -1112,7 +1112,7 @@ def notify_responsible(instance, sender, content: NotificationBody = InvenTreeNo | |||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  |  | ||||||
| def render_currency(money, decimal_places=None, currency=None, include_symbol=True): | def render_currency(money, decimal_places=None, currency=None, include_symbol=True, min_decimal_places=None): | ||||||
|     """Render a currency / Money object to a formatted string (e.g. for reports) |     """Render a currency / Money object to a formatted string (e.g. for reports) | ||||||
|  |  | ||||||
|     Arguments: |     Arguments: | ||||||
| @@ -1120,6 +1120,7 @@ def render_currency(money, decimal_places=None, currency=None, include_symbol=Tr | |||||||
|         decimal_places: The number of decimal places to render to. If unspecified, uses the PRICING_DECIMAL_PLACES setting. |         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 |         currency: Optionally convert to the specified currency | ||||||
|         include_symbol: Render with the appropriate currency symbol |         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. | ||||||
|     """ |     """ | ||||||
|  |  | ||||||
|     if money is None or money.amount is None: |     if money is None or money.amount is None: | ||||||
| @@ -1136,13 +1137,16 @@ def render_currency(money, decimal_places=None, currency=None, include_symbol=Tr | |||||||
|     if decimal_places is None: |     if decimal_places is None: | ||||||
|         decimal_places = InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES', 6) |         decimal_places = InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES', 6) | ||||||
|  |  | ||||||
|  |     if min_decimal_places is None: | ||||||
|  |         min_decimal_places = InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES_MIN', 0) | ||||||
|  |  | ||||||
|     value = Decimal(str(money.amount)).normalize() |     value = Decimal(str(money.amount)).normalize() | ||||||
|     value = str(value) |     value = str(value) | ||||||
|  |  | ||||||
|     if '.' in value: |     if '.' in value: | ||||||
|         decimals = len(value.split('.')[-1]) |         decimals = len(value.split('.')[-1]) | ||||||
|  |  | ||||||
|         decimals = max(decimals, 2) |         decimals = max(decimals, min_decimal_places) | ||||||
|         decimals = min(decimals, decimal_places) |         decimals = min(decimals, decimal_places) | ||||||
|  |  | ||||||
|         decimal_places = decimals |         decimal_places = decimals | ||||||
|   | |||||||
| @@ -1216,9 +1216,20 @@ class InvenTreeSetting(BaseInvenTreeSetting): | |||||||
|             'default': '', |             'default': '', | ||||||
|         }, |         }, | ||||||
|  |  | ||||||
|  |         'PRICING_DECIMAL_PLACES_MIN': { | ||||||
|  |             'name': _('Minimum Pricing Decimal Places'), | ||||||
|  |             'description': _('Minimum number of decimal places to display when rendering pricing data'), | ||||||
|  |             'default': 0, | ||||||
|  |             'validator': [ | ||||||
|  |                 int, | ||||||
|  |                 MinValueValidator(0), | ||||||
|  |                 MaxValueValidator(4), | ||||||
|  |             ] | ||||||
|  |         }, | ||||||
|  |  | ||||||
|         'PRICING_DECIMAL_PLACES': { |         'PRICING_DECIMAL_PLACES': { | ||||||
|             'name': _('Pricing Decimal Places'), |             'name': _('Maximum Pricing Decimal Places'), | ||||||
|             'description': _('Number of decimal places to display when rendering pricing data'), |             'description': _('Maximum umber of decimal places to display when rendering pricing data'), | ||||||
|             'default': 6, |             'default': 6, | ||||||
|             'validator': [ |             'validator': [ | ||||||
|                 int, |                 int, | ||||||
|   | |||||||
| @@ -14,7 +14,8 @@ | |||||||
|             {% include "InvenTree/settings/setting.html" with key="INVENTREE_DEFAULT_CURRENCY" icon="fa-globe" %} |             {% include "InvenTree/settings/setting.html" with key="INVENTREE_DEFAULT_CURRENCY" icon="fa-globe" %} | ||||||
|             {% include "InvenTree/settings/setting.html" with key="PART_INTERNAL_PRICE" %} |             {% include "InvenTree/settings/setting.html" with key="PART_INTERNAL_PRICE" %} | ||||||
|             {% include "InvenTree/settings/setting.html" with key="PART_BOM_USE_INTERNAL_PRICE" %} |             {% include "InvenTree/settings/setting.html" with key="PART_BOM_USE_INTERNAL_PRICE" %} | ||||||
|             {% include "InvenTree/settings/setting.html" with key="PRICING_DECIMAL_PLACES" %} |             {% include "InvenTree/settings/setting.html" with key="PRICING_DECIMAL_PLACES_MIN" icon='fa-dollar-sign' %} | ||||||
|  |             {% include "InvenTree/settings/setting.html" with key="PRICING_DECIMAL_PLACES" icon='fa-dollar-sign' %} | ||||||
|             {% include "InvenTree/settings/setting.html" with key="PRICING_UPDATE_DAYS" icon='fa-calendar-alt' %} |             {% include "InvenTree/settings/setting.html" with key="PRICING_UPDATE_DAYS" icon='fa-calendar-alt' %} | ||||||
|             <tr><td colspan='5'></td></tr> |             <tr><td colspan='5'></td></tr> | ||||||
|             {% include "InvenTree/settings/setting.html" with key="PRICING_USE_SUPPLIER_PRICING" icon='fa-check-circle' %} |             {% include "InvenTree/settings/setting.html" with key="PRICING_USE_SUPPLIER_PRICING" icon='fa-check-circle' %} | ||||||
|   | |||||||
| @@ -45,24 +45,22 @@ function formatCurrency(value, options={}) { | |||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     var digits = options.digits || global_settings.PRICING_DECIMAL_PLACES || 6; |     let maxDigits = options.digits || global_settings.PRICING_DECIMAL_PLACES || 6; | ||||||
|  |     let minDigits = options.minDigits || global_settings.PRICING_DECIMAL_PLACES_MIN || 0; | ||||||
|     // Strip out any trailing zeros, etc |  | ||||||
|     value = formatDecimal(value, digits); |  | ||||||
|  |  | ||||||
|     // Extract default currency information |     // Extract default currency information | ||||||
|     var currency = options.currency || global_settings.INVENTREE_DEFAULT_CURRENCY || 'USD'; |     let currency = options.currency || global_settings.INVENTREE_DEFAULT_CURRENCY || 'USD'; | ||||||
|  |  | ||||||
|     // Exctract locale information |     // Exctract locale information | ||||||
|     var locale = options.locale || navigator.language || 'en-US'; |     let locale = options.locale || navigator.language || 'en-US'; | ||||||
|  |  | ||||||
|  |     let formatter = new Intl.NumberFormat( | ||||||
|     var formatter = new Intl.NumberFormat( |  | ||||||
|         locale, |         locale, | ||||||
|         { |         { | ||||||
|             style: 'currency', |             style: 'currency', | ||||||
|             currency: currency, |             currency: currency, | ||||||
|             maximumSignificantDigits: digits, |             maximumFractionDigits: maxDigits, | ||||||
|  |             minimumFractionDigits: minDigits, | ||||||
|         } |         } | ||||||
|     ); |     ); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user