diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index a58640c52e8..046589bc71e 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -1507,6 +1507,11 @@ class AccountMove(models.Model): # ONCHANGE METHODS # ------------------------------------------------------------------------- + @api.onchange('date') + def _onchange_date(self): + if not self.is_invoice(True): + self.line_ids._inverse_amount_currency() + @api.onchange('invoice_vendor_bill_id') def _onchange_invoice_vendor_bill(self): if self.invoice_vendor_bill_id: diff --git a/addons/account/models/account_move_line.py b/addons/account/models/account_move_line.py index d1dd5717dc5..92e8ad6e107 100644 --- a/addons/account/models/account_move_line.py +++ b/addons/account/models/account_move_line.py @@ -1110,6 +1110,12 @@ class AccountMoveLine(models.Model): for line in self: if line.currency_id == line.company_id.currency_id and line.balance != line.amount_currency: line.balance = line.amount_currency + elif ( + line.currency_id != line.company_id.currency_id + and not line.move_id.is_invoice(True) + and not self.env.is_protected(self._fields['balance'], line) + ): + line.balance = line.company_id.currency_id.round(line.amount_currency / line.currency_rate) @api.onchange('debit') def _inverse_debit(self): diff --git a/addons/account/tests/test_account_move_entry.py b/addons/account/tests/test_account_move_entry.py index 699e65572d9..8b974960957 100644 --- a/addons/account/tests/test_account_move_entry.py +++ b/addons/account/tests/test_account_move_entry.py @@ -441,20 +441,41 @@ class TestAccountMove(AccountTestInvoicingCommon): [ { 'currency_id': self.currency_data['currency'].id, - 'amount_currency': 1200.0, + 'amount_currency': -1200.0, 'debit': 0.0, - 'credit': 0.0, + 'credit': 400.0, }, { 'currency_id': self.currency_data['currency'].id, - 'amount_currency': -1200.0, - 'debit': 0.0, + 'amount_currency': 1200.0, + 'debit': 400.0, 'credit': 0.0, }, ], ) - # Balance and amount currency are totally independant in journal entries if the line has a foreign currency + # Change the date to change the currency conversion's rate + with Form(move) as move_form: + move_form.date = fields.Date.from_string('2017-01-01') + + self.assertRecordValues( + move.line_ids.sorted('debit'), + [ + { + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -1200.0, + 'debit': 0.0, + 'credit': 600.0, + }, + { + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 1200.0, + 'debit': 600.0, + 'credit': 0.0, + }, + ], + ) + # You can change the balance manually without changing the currency amount with Form(move) as move_form: with move_form.line_ids.edit(0) as line_form: line_form.debit = 200