changeset 246cb846f264 in modules/analytic_account:default
details: 
https://hg.tryton.org/modules/analytic_account?cmd=changeset&node=246cb846f264
description:
        Add monetary field

        issue10598
        review338511002
diffstat:

 account.py             |  30 +++++++++++-------------------
 line.py                |  20 +++++++++++---------
 view/account_tree2.xml |   1 -
 3 files changed, 22 insertions(+), 29 deletions(-)

diffs (128 lines):

diff -r aa6e68375f59 -r 246cb846f264 account.py
--- a/account.py        Mon Jul 05 11:39:39 2021 +0200
+++ b/account.py        Sat Aug 21 09:09:55 2021 +0200
@@ -17,6 +17,8 @@
 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 @@
     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 @@
             ('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 @@
         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 @@
                     ])
             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 @@
                 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 -r aa6e68375f59 -r 246cb846f264 line.py
--- a/line.py   Mon Jul 05 11:39:39 2021 +0200
+++ b/line.py   Sat Aug 21 09:09:55 2021 +0200
@@ -12,16 +12,19 @@
 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 @@
         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 -r aa6e68375f59 -r 246cb846f264 view/account_tree2.xml
--- a/view/account_tree2.xml    Mon Jul 05 11:39:39 2021 +0200
+++ b/view/account_tree2.xml    Sat Aug 21 09:09:55 2021 +0200
@@ -7,5 +7,4 @@
     <field name="debit"/>
     <field name="credit"/>
     <field name="balance"/>
-    <field name="currency"/>
 </tree>

Reply via email to