From 30cd6e4cf96398816871a7e5f4e90c25bf30c36f 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 --- commission.py | 6 ++---- invoice.py | 8 +++----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/commission.py b/commission.py index 2a1709f330..cceaf1086d 100644 --- a/commission.py +++ b/commission.py @@ -20,7 +20,7 @@ from trytond.pool import Pool from trytond.wizard import Wizard, StateView, StateAction, Button from trytond.transaction import Transaction -from trytond.modules.product import price_digits +from trytond.modules.product import price_digits, round_price from .exceptions import FormulaError @@ -122,14 +122,12 @@ class Agent(ModelSQL, ModelView): group_by=commission.agent) cursor.execute(*query) amounts.update(dict(cursor.fetchall())) - digits = cls.pending_amount.digits - exp = Decimal(str(10.0 ** -digits[1])) for agent_id, amount in amounts.items(): if amount: # SQLite uses float for SUM if not isinstance(amount, Decimal): amount = Decimal(str(amount)) - amounts[agent_id] = amount.quantize(exp) + amounts[agent_id] = round_price(amount) return amounts @property diff --git a/invoice.py b/invoice.py index 36a3cdd3b5..c9175a3600 100644 --- a/invoice.py +++ b/invoice.py @@ -1,14 +1,13 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. - -from decimal import Decimal - from trytond.pool import PoolMeta, Pool from trytond.model import ModelView, Workflow, fields from trytond.pyson import Eval, If, Bool from trytond.transaction import Transaction from trytond.tools import grouped_slice +from trytond.modules.product import round_price + class Invoice(metaclass=PoolMeta): __name__ = 'account.invoice' @@ -156,8 +155,7 @@ class InvoiceLine(metaclass=PoolMeta): self.amount, agent.currency, round=False) amount = self._get_commission_amount(amount, plan) if amount: - digits = Commission.amount.digits - amount = amount.quantize(Decimal(str(10.0 ** -digits[1]))) + amount = round_price(amount) if not amount: continue