From 40d374ae08402e32ff785b4a7e06552ed6784810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 23 Apr 2020 21:44:00 +0200 Subject: [PATCH] Add round_price We add a common method to round prices that are stored using the price digits. It is useless to use the digits from the field because it can not be changed in any other way than by configuration. issue9146 review299521002 --- CHANGELOG | 1 + __init__.py | 4 ++-- product.py | 8 +++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 185bb03e98..b0b9fb091a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add round_price * Make product code unique * Add code to template as variant prefix code * Hide product fields inherited from template when empty diff --git a/__init__.py b/__init__.py index 13f5a6b785..134d210c72 100644 --- a/__init__.py +++ b/__init__.py @@ -6,10 +6,10 @@ from . import uom from . import category from . import product from . import configuration -from .product import price_digits +from .product import price_digits, round_price from .uom import uom_conversion_digits -__all__ = [price_digits, uom_conversion_digits] +__all__ = [price_digits, round_price, uom_conversion_digits] def register(): diff --git a/product.py b/product.py index 48209ec08f..f81b3d19ec 100644 --- a/product.py +++ b/product.py @@ -26,7 +26,7 @@ from trytond.modules.company.model import ( from .exceptions import InvalidIdentifierCode -__all__ = ['price_digits', 'TemplateFunction'] +__all__ = ['price_digits', 'round_price', 'TemplateFunction'] logger = logging.getLogger(__name__) TYPES = [ @@ -42,6 +42,12 @@ COST_PRICE_METHODS = [ price_digits = (16, config.getint('product', 'price_decimal', default=4)) +def round_price(value, rounding=None): + "Round price using the price digits" + return value.quantize( + Decimal(1) / 10 ** price_digits[1], rounding=rounding) + + class Template( DeactivableMixin, ModelSQL, ModelView, CompanyMultiValueMixin): "Product Template"