[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
This commit is contained in:
Laurent Smet
2019-12-10 13:21:53 +00:00
parent 905eed0de1
commit 04287b45c8
2 changed files with 72 additions and 6 deletions
+8 -6
View File
@@ -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')
@@ -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,
},
],
)