From 9194cc73da2aec78a264f60f031a7192faed5cbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 21 Aug 2021 09:09:55 +0200 Subject: [PATCH] Add monetary field issue10598 review338511002 --- account.py | 30 +++++++++++------------------- line.py | 20 +++++++++++--------- view/account_tree2.xml | 1 - 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/account.py b/account.py index a190eb2b97..064667ed9f 100644 --- a/account.py +++ b/account.py @@ -17,6 +17,8 @@ from trytond.transaction import Transaction from trytond.tools import lstrip_wildcard from trytond.pool import Pool +from trytond.modules.currency.fields import Monetary + from .exceptions import AccountValidationError @@ -31,8 +33,6 @@ class Account( currency = fields.Function( fields.Many2One('currency.currency', 'Currency'), 'on_change_with_currency') - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'on_change_with_currency_digits') type = fields.Selection([ ('root', 'Root'), ('view', 'View'), @@ -68,14 +68,14 @@ class Account( ('company', '=', Eval('company', -1)), ], depends=['company']) - balance = fields.Function(fields.Numeric('Balance', - digits=(16, Eval('currency_digits', 1)), depends=['currency_digits']), + balance = fields.Function(Monetary( + "Balance", currency='currency', digits='currency'), 'get_balance') - credit = fields.Function(fields.Numeric('Credit', - digits=(16, Eval('currency_digits', 2)), depends=['currency_digits']), + credit = fields.Function(Monetary( + "Credit", currency='currency', digits='currency'), 'get_credit_debit') - debit = fields.Function(fields.Numeric('Debit', - digits=(16, Eval('currency_digits', 2)), depends=['currency_digits']), + debit = fields.Function(Monetary( + "Debit", currency='currency', digits='currency'), 'get_credit_debit') state = fields.Selection([ ('draft', 'Draft'), @@ -143,12 +143,6 @@ class Account( if self.company: return self.company.currency.id - @fields.depends('company') - def on_change_with_currency_digits(self, name=None): - if self.company: - return self.company.currency.digits - return 2 - @fields.depends('parent', 'type', '_parent_parent.id', '_parent_parent.root', '_parent_parent.type') def on_change_parent(self): @@ -207,8 +201,7 @@ class Account( ]) for child in childs: balance += account_sum[child.id] - exp = Decimal(str(10.0 ** -account.currency_digits)) - balances[account.id] = balance.quantize(exp) + balances[account.id] = account.currency.round(balance) return balances @classmethod @@ -256,9 +249,8 @@ class Account( result[name][account_id] += value for account in accounts: for name in names: - exp = Decimal(str(10.0 ** -account.currency_digits)) - result[name][account.id] = ( - result[name][account.id].quantize(exp)) + result[name][account.id] = account.currency.round( + result[name][account.id]) return result def get_rec_name(self, name): diff --git a/line.py b/line.py index 80c712e393..017300973c 100644 --- a/line.py +++ b/line.py @@ -12,16 +12,19 @@ from trytond.pyson import Eval, PYSONEncoder from trytond.transaction import Transaction from trytond.pool import Pool, PoolMeta +from trytond.modules.currency.fields import Monetary + class Line(ModelSQL, ModelView): 'Analytic Line' __name__ = 'analytic_account.line' - debit = fields.Numeric('Debit', digits=(16, Eval('currency_digits', 2)), - required=True, depends=['currency_digits']) - credit = fields.Numeric('Credit', digits=(16, Eval('currency_digits', 2)), - required=True, depends=['currency_digits']) - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'on_change_with_currency_digits') + debit = Monetary( + "Debit", currency='currency', digits='currency', required=True) + credit = Monetary( + "Credit", currency='currency', digits='currency', required=True) + currency = fields.Function(fields.Many2One( + 'currency.currency', "Currency"), + 'on_change_with_currency') company = fields.Function(fields.Many2One('company.company', 'Company'), 'on_change_with_company', searcher='search_company') account = fields.Many2One('analytic_account.account', 'Account', @@ -74,10 +77,9 @@ class Line(ModelSQL, ModelView): return Decimal(0) @fields.depends('move_line', '_parent_move_line.account') - def on_change_with_currency_digits(self, name=None): + def on_change_with_currency(self, name=None): if self.move_line and self.move_line.account: - return self.move_line.account.company.currency.digits - return 2 + return self.move_line.account.company.currency.id @fields.depends('move_line', '_parent_move_line.account') def on_change_with_company(self, name=None): diff --git a/view/account_tree2.xml b/view/account_tree2.xml index 9c808fe67d..0f596cd7bd 100644 --- a/view/account_tree2.xml +++ b/view/account_tree2.xml @@ -7,5 +7,4 @@ this repository contains the full copyright notices and license terms. --> -