From 04287b45c8408129d514557d3d507564c5035889 Mon Sep 17 00:00:00 2001 From: Laurent Smet Date: Tue, 10 Dec 2019 12:57:07 +0000 Subject: [PATCH] [FIX] account: Prevent erasing currency when changing the date - Create a misc. journal entry. - Add a foreign currency on the line. - Change the date. => The currency is erased because the onchange works only for invoices. X-original-commit: e0bb4134512ec252b66ecfe91ce259d8cdcffedf --- addons/account/models/account_move.py | 14 ++-- .../account/tests/test_account_move_entry.py | 64 +++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 7567f418231..859a24ebe9c 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -332,13 +332,15 @@ class AccountMove(models.Model): @api.onchange('date', 'currency_id') def _onchange_currency(self): - company_currency = self.company_id.currency_id - has_foreign_currency = self.currency_id and self.currency_id != company_currency + if self.is_invoice(include_receipts=True): + company_currency = self.company_id.currency_id + has_foreign_currency = self.currency_id and self.currency_id != company_currency - for line in self.line_ids: - new_currency = has_foreign_currency and self.currency_id - line.currency_id = new_currency - line._onchange_currency() + for line in self.line_ids: + new_currency = has_foreign_currency and self.currency_id + line.currency_id = new_currency + + self.line_ids._onchange_currency() self._recompute_dynamic_lines() @api.onchange('invoice_payment_ref') diff --git a/addons/account/tests/test_account_move_entry.py b/addons/account/tests/test_account_move_entry.py index 4b1c516c1cb..f8ef40d6276 100644 --- a/addons/account/tests/test_account_move_entry.py +++ b/addons/account/tests/test_account_move_entry.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from odoo.addons.account.tests.common import AccountTestInvoicingCommon from odoo.tests import tagged, new_test_user +from odoo.tests.common import Form from odoo import fields from odoo.exceptions import ValidationError, UserError @@ -336,3 +337,66 @@ class TestAccountMove(AccountTestInvoicingCommon): move.post() self.assertEqual(move.message_partner_ids, self.env.user.partner_id | existing_partners | partner | commercial_partner) + + def test_misc_move_onchange(self): + ''' Test the behavior on onchanges for account.move having 'entry' as type. ''' + + move_form = Form(self.env['account.move']) + # Rate 1:3 + move_form.date = fields.Date.from_string('2016-01-01') + + # New line that should get 400.0 as debit. + with move_form.line_ids.new() as line_form: + line_form.name = 'debit_line' + line_form.account_id = self.company_data['default_account_revenue'] + line_form.currency_id = self.currency_data['currency'] + line_form.amount_currency = 1200.0 + + # New line that should get 400.0 as credit. + with move_form.line_ids.new() as line_form: + line_form.name = 'credit_line' + line_form.account_id = self.company_data['default_account_revenue'] + line_form.currency_id = self.currency_data['currency'] + line_form.amount_currency = -1200.0 + move = move_form.save() + + self.assertRecordValues( + move.line_ids.sorted('debit'), + [ + { + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -1200.0, + 'debit': 0.0, + 'credit': 400.0, + }, + { + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 1200.0, + 'debit': 400.0, + 'credit': 0.0, + }, + ], + ) + + # === 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, + }, + ], + )