diff --git a/account.py b/account.py index 093ae04a39..55cfa916a1 100644 --- a/account.py +++ b/account.py @@ -24,6 +24,8 @@ from trytond.pyson import Eval, If, PYSONEncoder, Bool from trytond.transaction import Transaction from trytond.pool import Pool +from trytond.modules.currency.fields import Monetary + from .common import PeriodMixin, ActivePeriodMixin from .exceptions import SecondCurrencyError, ChartWarning @@ -251,13 +253,11 @@ class Type( depends=['company']) currency = fields.Function(fields.Many2One( 'currency.currency', 'Currency'), 'get_currency') - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'get_currency_digits') - amount = fields.Function(fields.Numeric('Amount', - digits=(16, Eval('currency_digits', 2)), depends=['currency_digits']), + amount = fields.Function(Monetary( + "Amount", currency='currency', digits='currency'), 'get_amount') - amount_cmp = fields.Function(fields.Numeric('Amount', - digits=(16, Eval('currency_digits', 2)), depends=['currency_digits']), + amount_cmp = fields.Function(Monetary( + "Amount", currency='currency', digits='currency'), 'get_amount_cmp') company = fields.Many2One('company.company', 'Company', required=True, @@ -289,9 +289,6 @@ class Type( def get_currency(self, name): return self.company.currency.id - def get_currency_digits(self, name): - return self.company.currency.digits - @classmethod def get_amount(cls, types, name): pool = Pool() @@ -337,8 +334,7 @@ class Type( ]) for child in childs: res[type_.id] += type_sum[child.id] - exp = Decimal(str(10.0 ** -type_.currency_digits)) - res[type_.id] = res[type_.id].quantize(exp) + res[type_.id] = type_.currency.round(res[type_.id]) if type_.statement == 'balance' and type_.assets: res[type_.id] = - res[type_.id] return res @@ -781,8 +777,6 @@ class Account(AccountMixin(), ActivePeriodMixin, tree(), ModelSQL, ModelView): ondelete="RESTRICT") currency = fields.Function(fields.Many2One('currency.currency', 'Currency'), 'get_currency') - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'get_currency_digits') second_currency = fields.Many2One('currency.currency', 'Secondary Currency', help='Force all moves for this account \n' 'to have this secondary currency.', ondelete="RESTRICT", @@ -794,8 +788,6 @@ class Account(AccountMixin(), ActivePeriodMixin, tree(), ModelSQL, ModelView): 'invisible': ~Eval('deferral', False), }, depends=['currency', 'deferral']) - second_currency_digits = fields.Function(fields.Integer( - "Second Currency Digits"), 'get_second_currency_digits') type = fields.Many2One( 'account.account.type', "Type", ondelete='RESTRICT', states={ @@ -823,22 +815,22 @@ class Account(AccountMixin(), ActivePeriodMixin, tree(), ModelSQL, ModelView): right = fields.Integer('Right', required=True, select=True) childs = fields.One2Many( 'account.account', 'parent', 'Children') - balance = fields.Function(fields.Numeric('Balance', - digits=(16, Eval('currency_digits', 2)), 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') - amount_second_currency = fields.Function(fields.Numeric( + amount_second_currency = fields.Function(Monetary( "Amount Second Currency", - digits=(16, Eval('second_currency_digits', 2)), + currency='second_currency', digits='second_currency', states={ 'invisible': ~Eval('second_currency'), }, - depends=['second_currency_digits', 'second_currency']), + depends=['second_currency']), 'get_credit_debit') note = fields.Text('Note') deferrals = fields.One2Many( @@ -917,15 +909,6 @@ class Account(AccountMixin(), ActivePeriodMixin, tree(), ModelSQL, ModelView): def get_currency(self, name): return self.company.currency.id - def get_currency_digits(self, name): - return self.company.currency.digits - - def get_second_currency_digits(self, name): - if self.second_currency: - return self.second_currency.digits - else: - return 2 - @classmethod def get_balance(cls, accounts, name): pool = Pool() @@ -956,8 +939,7 @@ class Account(AccountMixin(), ActivePeriodMixin, tree(), ModelSQL, ModelView): # SQLite uses float for SUM if not isinstance(balances[account.id], Decimal): balances[account.id] = Decimal(str(balances[account.id])) - exp = Decimal(str(10.0 ** -account.currency_digits)) - balances[account.id] = balances[account.id].quantize(exp) + balances[account.id] = account.currency.round(balances[account.id]) fiscalyears = FiscalYear.browse(fiscalyear_ids) @@ -1008,12 +990,13 @@ class Account(AccountMixin(), ActivePeriodMixin, tree(), ModelSQL, ModelView): result[name][account_id] = row[i] for account in accounts: for name in names: - if name == 'amount_second_currency': - exp = Decimal(str(10.0 ** -account.second_currency_digits)) + if (name == 'amount_second_currency' + and account.second_currency): + currency = account.second_currency else: - exp = Decimal(str(10.0 ** -account.currency_digits)) - result[name][account.id] = ( - result[name][account.id].quantize(exp)) + currency = account.currency + result[name][account.id] = currency.round( + result[name][account.id]) cumulate_names = [] if Transaction().context.get('cumulate'): @@ -1289,33 +1272,28 @@ class AccountParty(ActivePeriodMixin, ModelSQL): debit_type = fields.Many2One('account.account.type', "Debit Type") closed = fields.Boolean("Closed") - balance = fields.Function(fields.Numeric( - "Balance", digits=(16, Eval('currency_digits', 2)), - 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') - amount_second_currency = fields.Function(fields.Numeric( + amount_second_currency = fields.Function(Monetary( "Amount Second Currency", - digits=(16, Eval('second_currency_digits', 2)), + currency='second_currency', digits='second_currency', states={ 'invisible': ~Eval('second_currency'), }, - depends=['second_currency_digits', 'second_currency']), + depends=['second_currency']), 'get_credit_debit') second_currency = fields.Many2One( 'currency.currency', "Secondary Currency") - currency_digits = fields.Function( - fields.Integer("Currency Digits"), 'get_currency_digits') - second_currency_digits = fields.Function(fields.Integer( - "Second Currency Digits"), 'get_second_currency_digits') + currency = fields.Function(fields.Many2One( + 'currency.currency', "Currency"), 'get_currency') @classmethod def table_query(cls): @@ -1391,8 +1369,7 @@ class AccountParty(ActivePeriodMixin, ModelSQL): # SQLite uses float for SUM if not isinstance(balances[record.id], Decimal): balances[record.id] = Decimal(str(balances[record.id])) - exp = Decimal(str(10.0 ** -record.currency_digits)) - balances[record.id] = balances[record.id].quantize(exp) + balances[record.id] = record.currency.round(balances[record.id]) fiscalyears = FiscalYear.browse(fiscalyear_ids) @@ -1450,12 +1427,12 @@ class AccountParty(ActivePeriodMixin, ModelSQL): result[name][id_] = row[i] for record in records: for name in names: - if name == 'amount_second_currency': - exp = Decimal(str(10.0 ** -record.second_currency_digits)) + if name == 'amount_second_currency' and record.second_currency: + currency = record.second_currency else: - exp = Decimal(str(10.0 ** -record.currency_digits)) - result[name][record.id] = ( - result[name][record.id].quantize(exp)) + currency = record.currency + result[name][record.id] = currency.round( + result[name][record.id]) cumulate_names = [] if Transaction().context.get('cumulate'): @@ -1470,11 +1447,8 @@ class AccountParty(ActivePeriodMixin, ModelSQL): else: return result - def get_currency_digits(self, name): - return self.company.currency.digits - - def get_second_currency_digits(self, name): - return self.account.second_currency_digits + def get_currency(self, name): + return self.company.currency.id class AccountDeferral(ModelSQL, ModelView): @@ -1488,28 +1462,24 @@ class AccountDeferral(ModelSQL, ModelView): select=True) fiscalyear = fields.Many2One('account.fiscalyear', 'Fiscal Year', required=True, select=True) - 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']) - balance = fields.Function(fields.Numeric('Balance', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), 'get_balance') + debit = Monetary( + "Debit", currency='currency', digits='currency', required=True) + credit = Monetary( + "Credit", currency='currency', digits='currency', required=True) + balance = fields.Function(Monetary( + "Balance", currency='currency', digits='currency'), + 'get_balance') currency = fields.Function(fields.Many2One( 'currency.currency', "Currency"), 'get_currency') - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'get_currency_digits') - amount_second_currency = fields.Numeric( + amount_second_currency = Monetary( "Amount Second Currency", - digits=(16, Eval('second_currency_digits', 2)), + currency='second_currency', digits='second_currency', required=True, states={ 'invisible': ~Eval('second_currency'), }, - required=True, depends=['second_currency_digits', 'second_currency']) + depends=['second_currency']) second_currency = fields.Function(fields.Many2One( 'currency.currency', "Second Currency"), 'get_second_currency') - second_currency_digits = fields.Function(fields.Integer( - "Second Currency Digits"), 'get_second_currency_digits') @classmethod def __setup__(cls): @@ -1530,16 +1500,10 @@ class AccountDeferral(ModelSQL, ModelView): def get_currency(self, name): return self.account.currency.id - def get_currency_digits(self, name): - return self.account.currency_digits - def get_second_currency(self, name): if self.account.second_currency: return self.account.second_currency.id - def get_second_currency_digits(self, name): - return self.account.second_currency_digits - def get_rec_name(self, name): return '%s - %s' % (self.account.rec_name, self.fiscalyear.rec_name) @@ -1611,42 +1575,32 @@ class _GeneralLedgerAccount(ActivePeriodMixin, ModelSQL, ModelView): account = fields.Many2One('account.account', "Account") company = fields.Many2One('company.company', 'Company') - start_debit = fields.Function(fields.Numeric('Start Debit', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + start_debit = fields.Function(Monetary( + "Start Debit", currency='currency', digits='currency'), 'get_account', searcher='search_account') - 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_debit_credit', searcher='search_debit_credit') - end_debit = fields.Function(fields.Numeric('End Debit', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + end_debit = fields.Function(Monetary( + "End Debit", currency='currency', digits='currency'), 'get_account', searcher='search_account') - start_credit = fields.Function(fields.Numeric('Start Credit', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + start_credit = fields.Function(Monetary( + "Start Credit", currency='currency', digits='currency'), 'get_account', searcher='search_account') - 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_debit_credit', searcher='search_debit_credit') - end_credit = fields.Function(fields.Numeric('End Credit', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + end_credit = fields.Function(Monetary( + "End Credit", currency='currency', digits='currency'), 'get_account', searcher='search_account') - start_balance = fields.Function(fields.Numeric('Start Balance', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + start_balance = fields.Function(Monetary( + "Start Balance", currency='currency', digits='currency'), 'get_account', searcher='search_account') - end_balance = fields.Function(fields.Numeric('End Balance', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + end_balance = fields.Function(Monetary( + "End Balance", currency='currency', digits='currency'), 'get_account', searcher='search_account') currency = fields.Function(fields.Many2One( 'currency.currency', 'Currency'), 'get_currency') - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'get_currency_digits') @classmethod def __setup__(cls): @@ -1810,9 +1764,6 @@ class _GeneralLedgerAccount(ActivePeriodMixin, ModelSQL, ModelView): def get_currency(self, name): return self.company.currency.id - def get_currency_digits(self, name): - return self.company.currency.digits - def get_rec_name(self, name): return self.account.rec_name @@ -2001,19 +1952,14 @@ class GeneralLedgerLine(ModelSQL, ModelView): 'account.general_ledger.account.party', "Account Party"), 'get_account_party') company = fields.Many2One('company.company', 'Company') - debit = fields.Numeric('Debit', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']) - credit = fields.Numeric('Credit', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']) - internal_balance = fields.Numeric("Internal Balance", - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']) - balance = fields.Function( - fields.Numeric("Balance", - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + debit = Monetary( + "Debit", currency='currency', digits='currency') + credit = Monetary( + "Credit", currency='currency', digits='currency') + internal_balance = Monetary( + "Internal Balance", currency='currency', digits='currency') + balance = fields.Function(Monetary( + "Balance", currency='currency', digits='currency'), 'get_balance') origin = fields.Reference('Origin', selection='get_origin') description = fields.Char('Description') @@ -2027,8 +1973,6 @@ class GeneralLedgerLine(ModelSQL, ModelView): state_string = state.translated('state') currency = fields.Function(fields.Many2One( 'currency.currency', "Currency"), 'get_currency') - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'get_currency_digits') @classmethod def __setup__(cls): @@ -2088,9 +2032,6 @@ class GeneralLedgerLine(ModelSQL, ModelView): def get_currency(self, name): return self.company.currency.id - def get_currency_digits(self, name): - return self.company.currency.digits - @classmethod def get_origin(cls): Line = Pool().get('account.move.line') @@ -2474,25 +2415,18 @@ class AgedBalance(ModelSQL, ModelView): }, depends=['company']) company = fields.Many2One('company.company', 'Company') - term0 = fields.Numeric('Now', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']) - term1 = fields.Numeric('First Term', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']) - term2 = fields.Numeric('Second Term', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']) - term3 = fields.Numeric('Third Term', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']) - balance = fields.Numeric('Balance', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']) + term0 = Monetary( + "Now", currency='currency', digits='currency') + term1 = Monetary( + "First Term", currency='currency', digits='currency') + term2 = Monetary( + "Second Term", currency='currency', digits='currency') + term3 = Monetary( + "Third Term", currency='currency', digits='currency') + balance = Monetary( + "Balance", currency='currency', digits='currency') currency = fields.Function(fields.Many2One( 'currency.currency', "Currency"), 'get_currency') - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'get_currency_digits') @classmethod def __setup__(cls): @@ -2606,9 +2540,6 @@ class AgedBalance(ModelSQL, ModelView): def get_currency(self, name): return self.company.currency.id - def get_currency_digits(self, name): - return self.company.currency.digits - class AgedBalanceReport(Report): __name__ = 'account.aged_balance' diff --git a/journal.py b/journal.py index 09e400e5cd..498ee72a34 100644 --- a/journal.py +++ b/journal.py @@ -16,6 +16,7 @@ from trytond.tools import reduce_ids, grouped_slice, lstrip_wildcard from trytond.tools.multivalue import migrate_property from trytond.modules.company.model import ( CompanyMultiValueMixin, CompanyValueMixin) +from trytond.modules.currency.fields import Monetary STATES = { 'readonly': Eval('state') == 'close', @@ -50,17 +51,18 @@ class Journal( })) sequences = fields.One2Many( 'account.journal.sequence', 'journal', "Sequences") - debit = fields.Function(fields.Numeric('Debit', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), 'get_debit_credit_balance') - credit = fields.Function(fields.Numeric('Credit', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), 'get_debit_credit_balance') - balance = fields.Function(fields.Numeric('Balance', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), 'get_debit_credit_balance') - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'get_currency_digits') + debit = fields.Function(Monetary( + "Debit", currency='currency', digits='currency'), + 'get_debit_credit_balance') + credit = fields.Function(Monetary( + "Credit", currency='currency', digits='currency'), + 'get_debit_credit_balance') + balance = fields.Function(Monetary( + "Balance", currency='currency', digits='currency'), + 'get_debit_credit_balance') + + currency = fields.Function(fields.Many2One( + 'currency.currency', "Currency"), 'get_currency') @classmethod def __setup__(cls): @@ -86,16 +88,16 @@ class Journal( ] @classmethod - def get_currency_digits(cls, journals, name): + def get_currency(cls, journals, name): pool = Pool() Company = pool.get('company.company') company_id = Transaction().context.get('company') if company_id: company = Company(company_id) - digits = company.currency.digits + currency_id = company.currency.id else: - digits = 2 - return dict.fromkeys([j.id for j in journals], digits) + currency_id = None + return dict.fromkeys([j.id for j in journals], currency_id) @classmethod def get_debit_credit_balance(cls, journals, names): diff --git a/move.py b/move.py index 17986d5a42..d21738e480 100644 --- a/move.py +++ b/move.py @@ -25,6 +25,8 @@ from trytond.rpc import RPC from trytond.tools import reduce_ids, grouped_slice from trytond.config import config +from trytond.modules.currency.fields import Monetary + from .exceptions import (PostError, MoveDatesError, CancelWarning, ReconciliationError, DeleteDelegatedWarning, GroupLineError, SplitLineError, CancelDelegatedWarning) @@ -612,14 +614,14 @@ class Line(ModelSQL, ModelView): } _depends = ['move_state'] - debit = fields.Numeric('Debit', digits=(16, Eval('currency_digits', 2)), - required=True, states=_states, - depends=['currency_digits', 'credit', 'tax_lines', 'journal'] - + _depends) - credit = fields.Numeric('Credit', digits=(16, Eval('currency_digits', 2)), - required=True, states=_states, - depends=['currency_digits', 'debit', 'tax_lines', 'journal'] - + _depends) + debit = Monetary( + "Debit", currency='currency', digits='currency', required=True, + states=_states, + depends=['credit', 'tax_lines', 'journal'] + _depends) + credit = Monetary( + "Credit", currency='currency', digits='currency', required=True, + states=_states, + depends=['debit', 'tax_lines', 'journal'] + _depends) account = fields.Many2One('account.account', 'Account', required=True, domain=[ ('company', '=', Eval('company', -1)), @@ -677,14 +679,15 @@ class Line(ModelSQL, ModelView): states=_states, depends=_depends), 'get_move_field', setter='set_move_field', searcher='search_move_field') - amount_second_currency = fields.Numeric('Amount Second Currency', - digits=(16, Eval('second_currency_digits', 2)), - help='The amount expressed in a second currency.', + amount_second_currency = Monetary( + "Amount Second Currency", + currency='second_currency', digits='second_currency', states={ 'required': Bool(Eval('second_currency')), 'readonly': _states['readonly'], }, - depends=['second_currency_digits', 'second_currency'] + _depends) + depends=['second_currency'] + _depends, + help='The amount expressed in a second currency.') second_currency = fields.Many2One('currency.currency', 'Second Currency', help='The second currency.', domain=[ @@ -732,18 +735,11 @@ class Line(ModelSQL, ModelView): 'on_change_with_move_state', searcher='search_move_field') 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') - second_currency_digits = fields.Function(fields.Integer( - 'Second Currency Digits'), 'on_change_with_second_currency_digits') - amount = fields.Function(fields.Numeric('Amount', - digits=(16, Eval('amount_currency_digits', 2)), - depends=['amount_currency_digits']), + amount = fields.Function(Monetary( + "Amount", currency='amount_currency', digits='amount_currency'), 'get_amount') amount_currency = fields.Function(fields.Many2One('currency.currency', 'Amount Currency'), 'get_amount_currency') - amount_currency_digits = fields.Function(fields.Integer( - 'Amount Currency Digits'), 'get_amount_currency') del _states, _depends @@ -846,10 +842,6 @@ class Line(ModelSQL, ModelView): def default_state(): return 'draft' - @staticmethod - def default_currency_digits(): - return 2 - @staticmethod def default_debit(): return Decimal(0) @@ -863,20 +855,6 @@ class Line(ModelSQL, ModelView): if self.account: return self.account.currency.id - @fields.depends('account') - def on_change_with_currency_digits(self, name=None): - if self.account: - return self.account.currency_digits - else: - return 2 - - @fields.depends('second_currency') - def on_change_with_second_currency_digits(self, name=None): - if self.second_currency: - return self.second_currency.digits - else: - return 2 - @classmethod def _get_origin(cls): 'Return list of Model names for origin Reference' @@ -919,11 +897,8 @@ class Line(ModelSQL, ModelView): @fields.depends('account') def on_change_account(self): if self.account: - self.currency_digits = self.account.currency_digits if self.account.second_currency: self.second_currency = self.account.second_currency - self.second_currency_digits = ( - self.on_change_with_second_currency_digits()) if not self.account.party_required: self.party = None @@ -1020,10 +995,7 @@ class Line(ModelSQL, ModelView): currency = self.second_currency else: currency = self.account.currency - if name == 'amount_currency': - return currency.id - elif name == 'amount_currency_digits': - return currency.digits + return currency.id def get_rec_name(self, name): if self.debit > self.credit: @@ -1555,10 +1527,11 @@ class ReconcileLinesWriteOff(ModelView): ], depends=['company']) date = fields.Date('Date', required=True) - amount = fields.Numeric('Amount', digits=(16, Eval('currency_digits', 2)), - readonly=True, depends=['currency_digits']) - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'on_change_with_currency_digits') + amount = Monetary( + "Amount", currency='currency', digits='currency', readonly=True) + currency = fields.Function(fields.Many2One( + 'currency.currency', "Currency"), + 'on_change_with_currency') description = fields.Char('Description') @staticmethod @@ -1567,9 +1540,9 @@ class ReconcileLinesWriteOff(ModelView): return Date.today() @fields.depends('company') - def on_change_with_currency_digits(self, name=None): + def on_change_with_currency(self, name=None): if self.company: - return self.company.currency.digits + return self.company.currency.id class ReconcileLines(Wizard): @@ -1765,7 +1738,7 @@ class Reconcile(Wizard): defaults['company'] = self.show.account.company.id defaults['parties'] = [p.id for p in self.show.parties] defaults['party'] = self.show.party.id if self.show.party else None - defaults['currency_digits'] = self.show.account.company.currency.digits + defaults['currency'] = self.show.account.company.currency.id defaults['lines'] = self._default_lines() defaults['write_off_amount'] = Decimal(0) defaults['date'] = Date.today() @@ -1868,13 +1841,13 @@ class ReconcileShow(ModelView): } _write_off_depends = ['write_off_amount'] - write_off_amount = fields.Function(fields.Numeric('Amount', - digits=(16, Eval('currency_digits', 2)), - states=_write_off_states, - depends=_write_off_depends + ['currency_digits']), + write_off_amount = fields.Function(Monetary( + "Amount", currency='currency', digits='currency', + states=_write_off_states, depends=_write_off_depends), 'on_change_with_write_off_amount') - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'on_change_with_currency_digits') + currency = fields.Function(fields.Many2One( + 'currency.currency', "Currency"), + 'on_change_with_currency') write_off = fields.Many2One( 'account.move.reconcile.write_off', "Write Off", domain=[ @@ -1888,17 +1861,17 @@ class ReconcileShow(ModelView): 'invisible': _write_off_states['invisible'], }, depends=_write_off_depends) - @fields.depends('lines', 'currency_digits') + @fields.depends('lines', 'currency') def on_change_with_write_off_amount(self, name=None): - digits = self.currency_digits or 0 - exp = Decimal(str(10.0 ** -digits)) amount = sum(((l.debit - l.credit) for l in self.lines), Decimal(0)) - return amount.quantize(exp) + if self.currency: + amount = self.currency.round(amount) + return amount @fields.depends('company') - def on_change_with_currency_digits(self, name=None): + def on_change_with_currency(self, name=None): if self.company: - return self.company.currency.digits + return self.company.currency.id class CancelMoves(Wizard): @@ -2208,7 +2181,6 @@ class SplitLines(Wizard): self.get_account(self.records) currency = self.get_currency(self.records) values['currency'] = currency.id - values['currency_digits'] = currency.digits values['total_amount'] = self.get_total_amount(self.records) return values @@ -2383,8 +2355,8 @@ class SplitLinesStart(ModelView): }, depends=['frequency'], help="The length of each period, in months.") - amount = fields.Numeric( - "Amount", digits=(16, Eval('currency_digits', 2)), + amount = Monetary( + "Amount", currency='currency', digits='currency', states={ 'required': ~Eval('number'), 'invisible': Bool(Eval('number')), @@ -2400,7 +2372,7 @@ class SplitLinesStart(ModelView): ('amount', '<', 0), ]), [])], - depends=['currency_digits', 'number', 'total_amount']) + depends=['number', 'total_amount']) number = fields.Integer( "Number", domain=[ @@ -2414,7 +2386,6 @@ class SplitLinesStart(ModelView): total_amount = fields.Numeric("Total Amount", readonly=True) currency = fields.Many2One('currency.currency', "Currency", readonly=True) - currency_digits = fields.Integer("Currency Digits", readonly=True) @classmethod def default_frequency(cls): @@ -2452,18 +2423,9 @@ class SplitLinesTerm(ModelView): "Split Lines" __name__ = 'account.move.line.split.term' date = fields.Date("Date", required=True) - amount = fields.Numeric( - "Amount", digits=(16, Eval('currency_digits', 2)), required=True, - depends=['currency_digits']) + amount = Monetary( + "Amount", currency='currency', digits='currency', required=True) currency = fields.Many2One('currency.currency', "Currency", required=True) - currency_digits = fields.Function( - fields.Integer("Currency Digits", readonly=True), - 'on_change_with_currency_digits') - - @fields.depends('currency') - def on_change_with_currency_digits(self, name=None): - if self.currency: - return self.currency.digits class GeneralJournal(Report): diff --git a/party.py b/party.py index 0709f8aad6..422e0bcb6b 100644 --- a/party.py +++ b/party.py @@ -17,6 +17,7 @@ from trytond.tools.multivalue import migrate_property from trytond.modules.company.model import ( CompanyMultiValueMixin, CompanyValueMixin) from trytond.modules.party.exceptions import EraseError +from trytond.modules.currency.fields import Monetary from .exceptions import AccountMissing @@ -68,24 +69,20 @@ class Party(CompanyMultiValueMixin, metaclass=PoolMeta): states={ 'invisible': ~Eval('context', {}).get('company'), }, help='Apply this rule on taxes when party is supplier.')) - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'get_currency_digits') - receivable = fields.Function(fields.Numeric('Receivable', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), - 'get_receivable_payable', searcher='search_receivable_payable') - payable = fields.Function(fields.Numeric('Payable', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), - 'get_receivable_payable', searcher='search_receivable_payable') - receivable_today = fields.Function(fields.Numeric('Receivable Today', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), - 'get_receivable_payable', searcher='search_receivable_payable') - payable_today = fields.Function(fields.Numeric('Payable Today', - digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), - 'get_receivable_payable', searcher='search_receivable_payable') + currency = fields.Function(fields.Many2One( + 'currency.currency', "Currency"), 'get_currency') + receivable = fields.Function(Monetary( + "Receivable", currency='currency', digits='currency'), + 'get_receivable_payable', searcher='search_receivable_payable') + payable = fields.Function(Monetary( + "Payable", currency='currency', digits='currency'), + 'get_receivable_payable', searcher='search_receivable_payable') + receivable_today = fields.Function(Monetary( + "Receivable Today", currency='currency', digits='currency'), + 'get_receivable_payable', searcher='search_receivable_payable') + payable_today = fields.Function(Monetary( + "Payable Today", currency='currency', digits='currency'), + 'get_receivable_payable', searcher='search_receivable_payable') @classmethod def multivalue_model(cls, field): @@ -112,17 +109,13 @@ class Party(CompanyMultiValueMixin, metaclass=PoolMeta): def default_supplier_tax_rule(cls, **pattern): return cls._default_tax_rule('supplier', **pattern) - @classmethod - def get_currency_digits(cls, parties, name): + def get_currency(self, name): pool = Pool() Company = pool.get('company.company') company_id = Transaction().context.get('company') if company_id: company = Company(company_id) - digits = company.currency.digits - else: - digits = 2 - return {p.id: digits for p in parties} + return company.currency.id @classmethod def get_receivable_payable(cls, parties, names): diff --git a/tax.py b/tax.py index 29dd7730ce..ed92f32879 100644 --- a/tax.py +++ b/tax.py @@ -21,6 +21,8 @@ from trytond.transaction import Transaction from trytond.tools import cursor_dict, lstrip_wildcard from trytond.pool import Pool +from trytond.modules.currency.fields import Monetary + from .common import PeriodMixin, ActivePeriodMixin KINDS = [ @@ -137,11 +139,8 @@ class TaxCode(ActivePeriodMixin, tree(), ModelSQL, ModelView): childs = fields.One2Many('account.tax.code', 'parent', 'Children') 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') - amount = fields.Function(fields.Numeric( - "Amount", digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + amount = fields.Function(Monetary( + "Amount", currency='currency', digits='currency'), 'get_amount') template = fields.Many2One('account.tax.code.template', 'Template') template_override = fields.Boolean('Override Template', @@ -176,12 +175,6 @@ class TaxCode(ActivePeriodMixin, tree(), ModelSQL, ModelView): if self.company: return self.company.currency.id - @fields.depends('currency') - def on_change_with_currency_digits(self, name=None): - if self.currency: - return self.currency.digits - return 2 - @classmethod def get_amount(cls, codes, name): result = {} @@ -191,9 +184,8 @@ class TaxCode(ActivePeriodMixin, tree(), ModelSQL, ModelView): ('parent', 'child_of', [c.id for c in codes]), ]) for code in childs: - exp = Decimal(str(10.0 ** -code.currency_digits)) - result[code.id] = sum( - (l.value for l in code.lines), Decimal(0)).quantize(exp) + result[code.id] = code.currency.round( + sum((l.value for l in code.lines), Decimal(0))) parents[code.id] = code.parent.id if code.parent else None ids = set(map(int, childs)) @@ -760,25 +752,22 @@ class Tax(sequence_ordered(), ModelSQL, ModelView, DeactivableMixin): }, depends=['template']) - invoice_base_amount = fields.Function(fields.Numeric( - "Invoice Base Amount", digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + invoice_base_amount = fields.Function(Monetary( + "Invoice Base Amount", currency='currency', digits='currency'), 'get_amount') - invoice_tax_amount = fields.Function(fields.Numeric( - "Invoice Tax Amount", digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + invoice_tax_amount = fields.Function(Monetary( + "Invoice Tax Amount", currency='currency', digits='currency'), 'get_amount') - credit_base_amount = fields.Function(fields.Numeric( - "Credit Base Amount", digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + credit_base_amount = fields.Function(Monetary( + "Credit Base Amount", currency='currency', digits='currency'), 'get_amount') - credit_tax_amount = fields.Function(fields.Numeric( - "Credit Tax Amount", digits=(16, Eval('currency_digits', 2)), - depends=['currency_digits']), + credit_tax_amount = fields.Function(Monetary( + "Credit Tax Amount", currency='currency', digits='currency'), 'get_amount') - currency_digits = fields.Function(fields.Integer("Currency Digits"), - 'on_change_with_currency_digits') + currency = fields.Function(fields.Many2One( + 'currency.currency', "Currency"), + 'on_change_with_currency') del _states @@ -897,10 +886,9 @@ class Tax(sequence_ordered(), ModelSQL, ModelView, DeactivableMixin): return [('move_line.move.period', 'in', periods)] @fields.depends('company') - def on_change_with_currency_digits(self, name=None): + def on_change_with_currency(self, name=None): if self.company: - return self.company.currency.digits - return 2 + return self.company.currency.id @classmethod def copy(cls, taxes, default=None): @@ -1199,10 +1187,11 @@ class TaxableMixin(object): class TaxLine(ModelSQL, ModelView): 'Tax Line' __name__ = 'account.tax.line' - currency_digits = fields.Function(fields.Integer('Currency Digits'), - 'on_change_with_currency_digits') - amount = fields.Numeric('Amount', digits=(16, Eval('currency_digits', 2)), - required=True, depends=['currency_digits']) + currency = fields.Function(fields.Many2One( + 'currency.currency', "Currency"), + 'on_change_with_currency') + amount = Monetary( + "Amount", currency='currency', digits='currency', required=True) type = fields.Selection([ ('tax', "Tax"), ('base', "Base"), @@ -1264,11 +1253,10 @@ class TaxLine(ModelSQL, ModelView): & (table.code.in_( [invoice_base_code, credit_note_base_code])))) - @fields.depends('move_line', '_parent_move_line.currency_digits') - def on_change_with_currency_digits(self, name=None): - if self.move_line: - return self.move_line.currency_digits - return 2 + @fields.depends('move_line', '_parent_move_line.currency') + def on_change_with_currency(self, name=None): + if self.move_line and self.move_line.currency: + return self.move_line.currency.id @fields.depends('_parent_move_line.account', 'move_line') def on_change_with_company(self, name=None): diff --git a/view/account_balance_sheet_tree.xml b/view/account_balance_sheet_tree.xml index 394a89d361..cb9a798260 100644 --- a/view/account_balance_sheet_tree.xml +++ b/view/account_balance_sheet_tree.xml @@ -3,6 +3,6 @@ this repository contains the full copyright notices and license terms. --> - - + + diff --git a/view/account_deferral_form.xml b/view/account_deferral_form.xml index f0a5d032a9..307e9340f8 100644 --- a/view/account_deferral_form.xml +++ b/view/account_deferral_form.xml @@ -9,11 +9,11 @@ this repository contains the full copyright notices and license terms. -->