diff --git a/addons/account/models/account_bank_statement_line.py b/addons/account/models/account_bank_statement_line.py index 37463feb914..7998da64f22 100644 --- a/addons/account/models/account_bank_statement_line.py +++ b/addons/account/models/account_bank_statement_line.py @@ -330,6 +330,14 @@ class AccountBankStatementLine(models.Model): if statement.journal_id: vals['journal_id'] = statement.journal_id.id + # Avoid having the same foreign_currency_id as currency_id. + if vals.get('journal_id') and vals.get('foreign_currency_id'): + journal = self.env['account.journal'].browse(vals['journal_id']) + journal_currency = journal.currency_id or journal.company_id.currency_id + if vals['foreign_currency_id'] == journal_currency.id: + vals['foreign_currency_id'] = None + vals['amount_currency'] = 0.0 + # Force the move_type to avoid inconsistency with residual 'default_move_type' inside the context. vals['move_type'] = 'entry' diff --git a/addons/account/tests/test_account_bank_statement.py b/addons/account/tests/test_account_bank_statement.py index 0ff74f9cac1..157e921e12d 100644 --- a/addons/account/tests/test_account_bank_statement.py +++ b/addons/account/tests/test_account_bank_statement.py @@ -369,12 +369,6 @@ class TestAccountBankStatementLine(AccountTestInvoicingCommon): # ==== Test constraints at creation ==== - # Foreign currency must not be the same as the journal one. - assertStatementLineConstraint({ - **statement_line_vals, - 'foreign_currency_id': self.currency_1.id, - }) - # Can't have a stand alone amount in foreign currency without foreign currency set. assertStatementLineConstraint({ **statement_line_vals, @@ -1170,3 +1164,20 @@ class TestAccountBankStatementLine(AccountTestInvoicingCommon): 'balance_start': 0, # no value is given for balance start and it is not a split 'balance_end': 0, }]) + + def test_create_statement_line_with_inconsistent_currencies(self): + statement_line = self.env['account.bank.statement.line'].create({ + 'date': '2019-01-01', + 'journal_id': self.bank_journal_1.id, + 'payment_ref': "Happy new year", + 'amount': 200.0, + 'amount_currency': 200.0, + 'foreign_currency_id': self.env.company.currency_id.id, + }) + + self.assertRecordValues(statement_line, [{ + 'currency_id': self.env.company.currency_id.id, + 'foreign_currency_id': False, + 'amount': 200.0, + 'amount_currency': 0.0, + }])