From f542e01b882cbefcb7c389cca54cb5dadca37c82 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 --- move.py | 8 ++------ product.py | 9 ++++----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/move.py b/move.py index 9487a568c0..b9d47e7b47 100644 --- a/move.py +++ b/move.py @@ -19,7 +19,7 @@ from trytond.tools import reduce_ids from trytond.transaction import Transaction from trytond.pool import Pool -from trytond.modules.product import price_digits +from trytond.modules.product import price_digits, round_price from .exceptions import MoveOriginWarning @@ -477,7 +477,6 @@ class Move(Workflow, ModelSQL, ModelView): """ pool = Pool() Uom = pool.get('product.uom') - Product = pool.get('product.product') Currency = pool.get('currency.currency') if direction == 'in': @@ -505,10 +504,7 @@ class Move(Workflow, ModelSQL, ModelView): new_cost_price = unit_price elif direction == 'out': new_cost_price = cost_price - - digits = Product.cost_price.digits - return new_cost_price.quantize( - Decimal(str(10.0 ** -digits[1]))) + return round_price(new_cost_price) @staticmethod def _get_internal_quantity(quantity, uom, product): diff --git a/product.py b/product.py index 88157df380..3bc003a1e1 100644 --- a/product.py +++ b/product.py @@ -23,6 +23,8 @@ from trytond.transaction import Transaction from trytond.pool import Pool, PoolMeta from trytond.tools import grouped_slice +from trytond.modules.product import round_price + from .exceptions import ProductCostPriceError from .move import StockMixin @@ -226,7 +228,6 @@ class Product(StockMixin, object, metaclass=PoolMeta): def recompute_cost_price(cls, products, start=None): pool = Pool() Move = pool.get('stock.move') - digits = cls.cost_price.digits costs = defaultdict(list) for product in products: if product.type == 'service': @@ -234,7 +235,7 @@ class Product(StockMixin, object, metaclass=PoolMeta): cost = getattr( product, 'recompute_cost_price_%s' % product.cost_price_method)(start) - cost = cost.quantize(Decimal(str(10.0 ** -digits[1]))) + cost = round_price(cost) costs[cost].append(product) updated = [] @@ -298,7 +299,6 @@ class Product(StockMixin, object, metaclass=PoolMeta): Currency = pool.get('currency.currency') Uom = pool.get('product.uom') Revision = pool.get('product.cost_price.revision') - digits = self.__class__.cost_price.digits domain = [ ('product', '=', self.id), @@ -371,8 +371,7 @@ class Product(StockMixin, object, metaclass=PoolMeta): ) / (quantity + qty) elif qty > 0: cost_price = unit_price - current_cost_price = cost_price.quantize( - Decimal(str(10.0 ** -digits[1]))) + current_cost_price = round_price(cost_price) quantity += qty Move.write([