changeset 14439dbd5633 in modules/product:default
details: https://hg.tryton.org/modules/product?cmd=changeset;node=14439dbd5633
description:
        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
diffstat:

 CHANGELOG   |  1 +
 __init__.py |  4 ++--
 product.py  |  8 +++++++-
 3 files changed, 10 insertions(+), 3 deletions(-)

diffs (49 lines):

diff -r 331053171f9a -r 14439dbd5633 CHANGELOG
--- a/CHANGELOG Mon Apr 20 14:12:54 2020 +0200
+++ b/CHANGELOG Thu Apr 23 21:44:00 2020 +0200
@@ -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 -r 331053171f9a -r 14439dbd5633 __init__.py
--- a/__init__.py       Mon Apr 20 14:12:54 2020 +0200
+++ b/__init__.py       Thu Apr 23 21:44:00 2020 +0200
@@ -6,10 +6,10 @@
 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 -r 331053171f9a -r 14439dbd5633 product.py
--- a/product.py        Mon Apr 20 14:12:54 2020 +0200
+++ b/product.py        Thu Apr 23 21:44:00 2020 +0200
@@ -26,7 +26,7 @@
 from .exceptions import InvalidIdentifierCode
 
 
-__all__ = ['price_digits', 'TemplateFunction']
+__all__ = ['price_digits', 'round_price', 'TemplateFunction']
 logger = logging.getLogger(__name__)
 
 TYPES = [
@@ -42,6 +42,12 @@
 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"

Reply via email to